1 //--------------------------------------------------------------------------
2 // Copyright (C) 2015-2021 Cisco and/or its affiliates. All rights reserved.
3 //
4 // This program is free software; you can redistribute it and/or modify it
5 // under the terms of the GNU General Public License Version 2 as published
6 // by the Free Software Foundation.  You may not use, modify or distribute
7 // this program under any other version of the GNU General Public License.
8 //
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License along
15 // with this program; if not, write to the Free Software Foundation, Inc.,
16 // 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 //--------------------------------------------------------------------------
18 
19 // tcp_connector_module_test.cc author Ed Borgoyn <eborgoyn@cisco.com>
20 // unit test main
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include "connectors/tcp_connector/tcp_connector_module.h"
27 #include "profiler/profiler.h"
28 
29 #include <CppUTest/CommandLineTestRunner.h>
30 #include <CppUTest/TestHarness.h>
31 
32 using namespace snort;
33 
34 THREAD_LOCAL SimpleStats tcp_connector_stats;
35 THREAD_LOCAL ProfileStats tcp_connector_perfstats;
36 
show_stats(PegCount *,const PegInfo *,unsigned,const char *)37 void show_stats(PegCount*, const PegInfo*, unsigned, const char*) { }
show_stats(PegCount *,const PegInfo *,const IndexVec &,const char *,FILE *)38 void show_stats(PegCount*, const PegInfo*, const IndexVec&, const char*, FILE*) { }
39 
40 namespace snort
41 {
snort_strdup(const char * s)42 char* snort_strdup(const char* s)
43 { return strdup(s); }
44 }
45 
TEST_GROUP(tcp_connector_module)46 TEST_GROUP(tcp_connector_module)
47 {
48 };
49 
TEST(tcp_connector_module,test_call)50 TEST(tcp_connector_module, test_call)
51 {
52     Value connector_val("tcp-c");
53     Value address_val("127.0.0.1");
54     Value base_port_val((double)10000);
55     Value setup_val("call");
56     Parameter address_param =
57         {"address", Parameter::PT_STRING, nullptr, nullptr, "address"};
58     Parameter connector_param =
59         {"connector", Parameter::PT_STRING, nullptr, nullptr, "connector"};
60     Parameter base_port_param =
61         {"base_port", Parameter::PT_PORT, nullptr, nullptr, "base_port"};
62     Parameter setup_param =
63         {"setup", Parameter::PT_ENUM, "call | answer", nullptr, "establishment"};
64 
65     TcpConnectorModule module;
66 
67     address_val.set(&address_param);
68     base_port_val.set(&base_port_param);
69     setup_val.set(&setup_param);
70     connector_val.set(&connector_param);
71 
72     module.begin("tcp_connector", 0, nullptr);
73     module.begin("tcp_connector", 1, nullptr);
74     module.set("tcp_connector.base_port", base_port_val, nullptr);
75     module.set("tcp_connector.address", address_val, nullptr);
76     module.set("tcp_connector.connector", connector_val, nullptr);
77     module.set("tcp_connector.setup", setup_val, nullptr);
78     module.end("tcp_connector", 1, nullptr);
79     module.end("tcp_connector", 0, nullptr);
80 
81     TcpConnectorConfig::TcpConnectorConfigSet* config_set = module.get_and_clear_config();
82 
83     CHECK(config_set != nullptr);
84 
85     CHECK(config_set->size() == 1);
86 
87     TcpConnectorConfig config = *(config_set->front());
88     CHECK(config.base_port == 10000);
89     CHECK(config.address == "127.0.0.1");
90     CHECK(config.setup == TcpConnectorConfig::Setup::CALL);
91     CHECK(config.connector_name == "tcp-c");
92     CHECK(config.direction == Connector::CONN_DUPLEX);
93 
94     CHECK(module.get_pegs() != nullptr );
95     CHECK(module.get_counts() != nullptr );
96     CHECK(module.get_profile() != nullptr );
97 
98     for ( auto conf : *config_set )
99         delete conf;
100 
101     config_set->clear();
102     delete config_set;
103 }
104 
TEST(tcp_connector_module,test_answer)105 TEST(tcp_connector_module, test_answer)
106 {
107     Value connector_val("tcp-a");
108     Value base_port_val((double)20000);
109     Value setup_val("answer");
110     Parameter connector_param =
111         {"connector", Parameter::PT_STRING, nullptr, nullptr, "connector"};
112     Parameter base_port_param =
113         {"base_port", Parameter::PT_PORT, nullptr, nullptr, "base_port"};
114     Parameter setup_param =
115         {"setup", Parameter::PT_ENUM, "call | answer", nullptr, "establishment"};
116 
117     TcpConnectorModule module;
118 
119     base_port_val.set(&base_port_param);
120     CHECK( base_port_param.validate(base_port_val) == true );
121     setup_val.set(&setup_param);
122     CHECK( setup_param.validate(setup_val) == true );
123     connector_val.set(&connector_param);
124     CHECK( connector_param.validate(connector_val) == true );
125 
126     module.begin("tcp_connector", 0, nullptr);
127     module.begin("tcp_connector", 1, nullptr);
128     module.set("tcp_connector.base_port", base_port_val, nullptr);
129     module.set("tcp_connector.connector", connector_val, nullptr);
130     module.set("tcp_connector.setup", setup_val, nullptr);
131     module.end("tcp_connector", 1, nullptr);
132     module.end("tcp_connector", 0, nullptr);
133 
134     TcpConnectorConfig::TcpConnectorConfigSet* config_set = module.get_and_clear_config();
135 
136     CHECK(config_set != nullptr);
137 
138     CHECK(config_set->size() == 1);
139 
140     TcpConnectorConfig config = *(config_set->front());
141     CHECK(config.base_port == 20000);
142 //    CHECK(config.setup == TcpConnectorConfig::Setup::ANSWER);
143     CHECK(config.connector_name == "tcp-a");
144     CHECK(config.direction == Connector::CONN_DUPLEX);
145 
146     for ( auto conf : *config_set )
147         delete conf;
148 
149     config_set->clear();
150     delete config_set;
151 }
152 
main(int argc,char ** argv)153 int main(int argc, char** argv)
154 {
155     return CommandLineTestRunner::RunAllTests(argc, argv);
156 }
157 
158