Commit | Line | Data |
---|---|---|
33695392 GMF |
1 | /* |
2 | The software in this package is distributed under the GNU General | |
3 | Public License version 2 (with a special exception described below). | |
4 | ||
5 | A copy of GNU General Public License (GPL) is included in this distribution, | |
6 | in the file COPYING.GPL. | |
7 | ||
8 | As a special exception, if other files instantiate templates or use macros | |
9 | or inline functions from this file, or you compile this file and link it | |
10 | with other works to produce a work based on this file, this file | |
11 | does not by itself cause the resulting work to be covered | |
12 | by the GNU General Public License. | |
13 | ||
14 | However the source code for this file must still be made available | |
15 | in accordance with section (3) of the GNU General Public License. | |
16 | ||
17 | This exception does not invalidate any other reasons why a work based | |
18 | on this file might be covered by the GNU General Public License. | |
19 | */ | |
20 | ||
21 | #define BOOST_TEST_MAIN | |
22 | #define BOOST_TEST_DYN_LINK | |
23 | ||
24 | #include <algorithm> | |
25 | ||
26 | #include <boost/program_options.hpp> | |
27 | #include <boost/shared_ptr.hpp> | |
28 | #include <boost/test/unit_test.hpp> | |
29 | ||
2c40f493 | 30 | #include "host/loglevel.h" |
e3c7d9ac | 31 | #include "host/logoutput.h" |
33695392 GMF |
32 | #include "host/pingprotocol.h" |
33 | #include "config/configurationoptions.h" | |
34 | ||
35 | //------------------------------------------------------------------------------ | |
36 | ||
37 | typedef boost::shared_ptr<boost::program_options::option_description> option_description_item; | |
38 | ||
39 | bool option_present( | |
40 | const std::vector< option_description_item > &options, | |
41 | const std::string &option_to_find | |
2811d686 GMF |
42 | ); |
43 | void option_insert( | |
44 | const std::string &key, | |
45 | const boost::any &value, | |
46 | boost::program_options::variables_map &vm | |
47 | ); | |
48 | void option_clear_and_insert( | |
49 | const std::string &key, | |
50 | const boost::any &value, | |
51 | boost::program_options::variables_map &vm | |
52 | ); | |
53 | ||
54 | //------------------------------------------------------------------------------ | |
55 | ||
56 | bool option_present( | |
57 | const std::vector< option_description_item > &options, | |
58 | const std::string &option_to_find | |
33695392 GMF |
59 | ) |
60 | { | |
61 | std::vector< option_description_item >::const_iterator first = options.begin(); | |
62 | std::vector< option_description_item >::const_iterator last = options.end(); | |
63 | ||
64 | while ( first != last ) | |
65 | { | |
66 | if ( (*first)->long_name() == option_to_find ) { | |
67 | return true; | |
68 | } | |
0a670586 | 69 | ++first; |
33695392 GMF |
70 | } |
71 | return false; | |
72 | } | |
73 | ||
74 | void option_insert( | |
75 | const std::string &key, | |
76 | const boost::any &value, | |
77 | boost::program_options::variables_map &vm | |
78 | ) | |
79 | { | |
80 | boost::program_options::variable_value var_value( value, true ); | |
81 | ||
82 | vm.insert( std::pair<std::string, boost::program_options::variable_value>( key, var_value ) ); | |
83 | } | |
84 | ||
85 | void option_clear_and_insert( | |
86 | const std::string &key, | |
87 | const boost::any &value, | |
88 | boost::program_options::variables_map &vm | |
89 | ) | |
90 | { | |
91 | vm.clear(); | |
92 | ||
93 | option_insert( key, value, vm ); | |
94 | } | |
95 | ||
96 | //------------------------------------------------------------------------------ | |
97 | ||
98 | BOOST_AUTO_TEST_SUITE( TestConfigurationOptions ) | |
99 | ||
100 | BOOST_AUTO_TEST_CASE( get_generic_options ) | |
101 | { | |
102 | ConfigurationOptions config_options; | |
103 | std::vector< option_description_item > options = config_options.get_generic_options().options(); | |
104 | ||
105 | // if this assert fails, you must add or remove one of the options in the | |
106 | // test bellow | |
fda777ea CH |
107 | BOOST_CHECK_EQUAL( options.size(), 7 ); |
108 | // help, version, daemon, config-file, log-level, log-output, log-file | |
33695392 GMF |
109 | |
110 | BOOST_CHECK_EQUAL( option_present( options, "help" ), true ); | |
111 | BOOST_CHECK_EQUAL( option_present( options, "config-file" ), true ); | |
112 | BOOST_CHECK_EQUAL( option_present( options, "daemon" ), true ); | |
113 | BOOST_CHECK_EQUAL( option_present( options, "version" ), true ); | |
2c40f493 | 114 | BOOST_CHECK_EQUAL( option_present( options, "log-level" ), true ); |
e3c7d9ac | 115 | BOOST_CHECK_EQUAL( option_present( options, "log-output" ), true ); |
fda777ea | 116 | BOOST_CHECK_EQUAL( option_present( options, "log-file" ), true ); |
33695392 GMF |
117 | } |
118 | ||
119 | BOOST_AUTO_TEST_CASE( get_configuration_options ) | |
120 | { | |
121 | ConfigurationOptions config_options; | |
122 | std::vector< option_description_item > options = config_options.get_configuration_options().options(); | |
123 | ||
124 | // if this assert fails, you must add or remove one of the options in the | |
c4911662 CH |
125 | // test below. Will probably find them all in |
126 | // src/config/configurationoptions.cpp constructor | |
f833126b | 127 | BOOST_CHECK_EQUAL( options.size(), 18 ); |
33695392 GMF |
128 | |
129 | BOOST_CHECK_EQUAL( option_present( options, "hosts-down-limit" ), true ); | |
130 | BOOST_CHECK_EQUAL( option_present( options, "link-down-interval" ), true ); | |
131 | BOOST_CHECK_EQUAL( option_present( options, "link-up-interval" ), true ); | |
132 | BOOST_CHECK_EQUAL( option_present( options, "nameserver" ), true ); | |
133 | BOOST_CHECK_EQUAL( option_present( options, "ping-fail-limit" ), true ); | |
504adbea | 134 | BOOST_CHECK_EQUAL( option_present( options, "default-source-network-interface" ), true ); |
33695392 | 135 | BOOST_CHECK_EQUAL( option_present( options, "status-notifier-cmd" ), true ); |
c4911662 CH |
136 | BOOST_CHECK_EQUAL( option_present( options, "ping-reply-timeout" ), true ); |
137 | BOOST_CHECK_EQUAL( option_present( options, "max-address-resolution-attempts" ), true ); | |
138 | BOOST_CHECK_EQUAL( option_present( options, "resolved-ip-ttl-threshold" ), true ); | |
f833126b | 139 | BOOST_CHECK_EQUAL( option_present( options, "min-time-between-resolves" ), true ); |
e91538f0 | 140 | BOOST_CHECK_EQUAL( option_present( options, "dns-cache-file" ), true ); |
c4911662 | 141 | BOOST_CHECK_EQUAL( option_present( options, "ratio-random-hosts" ), true ); |
33695392 GMF |
142 | BOOST_CHECK_EQUAL( option_present( options, "host.name" ), true ); |
143 | BOOST_CHECK_EQUAL( option_present( options, "host.port" ), true ); | |
504adbea | 144 | BOOST_CHECK_EQUAL( option_present( options, "host.source-network-interface" ), true ); |
33695392 GMF |
145 | BOOST_CHECK_EQUAL( option_present( options, "host.ping-protocol" ), true ); |
146 | BOOST_CHECK_EQUAL( option_present( options, "host.interval" ), true ); | |
147 | } | |
148 | ||
149 | BOOST_AUTO_TEST_CASE( parse_generic_options ) | |
150 | { | |
151 | boost::program_options::variables_map vm; | |
152 | ||
153 | option_insert( "config-file", boost::any( std::string("pingcheck.conf") ), vm ); | |
154 | option_insert( "daemon", boost::any(), vm ); | |
2c40f493 | 155 | option_insert( "log-level", boost::any( std::string("EMERGENCY") ), vm ); |
e3c7d9ac | 156 | option_insert( "log-output", boost::any( std::string("TERMINAL") ), vm ); |
fda777ea | 157 | option_insert( "log-file", boost::any( std::string("pingcheck_test.log") ), vm ); |
33695392 GMF |
158 | |
159 | ConfigurationOptions config_options; | |
160 | Configuration configuration; | |
161 | config_options.parse_generic_options( vm, &configuration ); | |
162 | ||
163 | BOOST_CHECK_EQUAL( configuration.get_config_file_name(), "pingcheck.conf" ); | |
164 | BOOST_CHECK_EQUAL( configuration.get_daemon(), true ); | |
d9ce1ade | 165 | BOOST_CHECK_EQUAL( static_cast<int>(configuration.get_log_level()), static_cast<int>(I2n::Logger::LogLevel::Emergency) ); |
e3c7d9ac | 166 | BOOST_CHECK_EQUAL( static_cast<int>(configuration.get_log_output()), static_cast<int>(LogOutput_TERMINAL) ); |
fda777ea | 167 | BOOST_CHECK_EQUAL( configuration.get_log_file(), "pingcheck_test.log" ); |
33695392 GMF |
168 | } |
169 | ||
170 | BOOST_AUTO_TEST_CASE( parse_configuration_options ) | |
171 | { | |
172 | boost::program_options::variables_map vm; | |
173 | ||
174 | option_insert( "hosts-down-limit", boost::any( 0 ), vm ); | |
97837af8 CH |
175 | option_insert( "link-down-interval", boost::any( 30*60 ), vm ); |
176 | option_insert( "link-up-interval", boost::any( 40*60 ), vm ); | |
33695392 GMF |
177 | option_insert( "nameserver", boost::any( std::string("localhost") ), vm ); |
178 | option_insert( "ping-fail-limit", boost::any( 50 ), vm ); | |
504adbea | 179 | option_insert( "default-source-network-interface", boost::any( std::string("wlan1") ), vm ); |
33695392 GMF |
180 | option_insert( "status-notifier-cmd", boost::any( std::string("/tmp/command.sh") ), vm ); |
181 | ||
182 | ConfigurationOptions config_options; | |
183 | Configuration configuration; | |
184 | config_options.parse_configuration_options( vm, &configuration ); | |
185 | ||
186 | BOOST_CHECK_EQUAL( configuration.get_hosts_down_limit(), 0 ); | |
97837af8 CH |
187 | BOOST_CHECK_EQUAL( configuration.get_link_down_interval_in_sec(), 30*60 ); |
188 | BOOST_CHECK_EQUAL( configuration.get_link_up_interval_in_sec(), 40*60 ); | |
33695392 GMF |
189 | BOOST_CHECK_EQUAL( configuration.get_nameserver(), "localhost" ); |
190 | BOOST_CHECK_EQUAL( configuration.get_ping_fail_limit(), 50 ); | |
191 | BOOST_CHECK_EQUAL( configuration.get_source_network_interface(), "wlan1" ); | |
192 | BOOST_CHECK_EQUAL( configuration.get_status_notifier_cmd(), "/tmp/command.sh" ); | |
193 | } | |
194 | ||
195 | BOOST_AUTO_TEST_CASE( parse_hosts_options ) | |
196 | { | |
197 | std::vector<std::string> hosts_names; | |
198 | std::vector<int> hosts_ports; | |
504adbea | 199 | std::vector<std::string> hosts_source_interfaces; |
33695392 | 200 | std::vector<std::string> hosts_ping_protocols; |
c4911662 | 201 | std::vector<std::string> hosts_intervals; |
33695392 GMF |
202 | // host 1 |
203 | hosts_names.push_back( "www.fazenda.gov.br" ); | |
204 | hosts_ports.push_back( 61 ); | |
504adbea | 205 | hosts_source_interfaces.push_back( "eth1" ); |
33695392 | 206 | hosts_ping_protocols.push_back( "TCP" ); |
c4911662 | 207 | hosts_intervals.push_back( "71" ); |
33695392 GMF |
208 | // host 2 |
209 | hosts_names.push_back( "www.intra2net.de" ); | |
210 | hosts_ports.push_back( 62 ); | |
504adbea | 211 | hosts_source_interfaces.push_back( "eth2" ); |
33695392 | 212 | hosts_ping_protocols.push_back( "ICMP" ); |
c4911662 | 213 | hosts_intervals.push_back( "72" ); |
33695392 GMF |
214 | // host 3 |
215 | hosts_names.push_back( "www.ufsc.br" ); | |
216 | hosts_ports.push_back( 63 ); | |
504adbea | 217 | hosts_source_interfaces.push_back( "eth3" ); |
33695392 | 218 | hosts_ping_protocols.push_back( "ICMPv6" ); |
c4911662 | 219 | hosts_intervals.push_back( "73" ); |
33695392 GMF |
220 | // host 4 |
221 | hosts_names.push_back( "www.google.com" ); | |
222 | hosts_ports.push_back( 64 ); | |
504adbea | 223 | hosts_source_interfaces.push_back( "eth4" ); |
33695392 | 224 | hosts_ping_protocols.push_back( "TCP_IPv6" ); |
c4911662 | 225 | hosts_intervals.push_back( "74" ); |
47f51204 GMF |
226 | // host 5 |
227 | hosts_names.push_back( "www.kernel.org" ); | |
228 | hosts_ports.push_back( 64 ); | |
7c85a848 | 229 | hosts_source_interfaces.push_back( "eth5" ); |
0a670586 | 230 | hosts_ping_protocols.push_back( "TCP_IPv4,ICMPv4" ); |
c4911662 | 231 | hosts_intervals.push_back( "75" ); |
47f51204 GMF |
232 | // host 6 |
233 | hosts_names.push_back( "www.linux.com" ); | |
234 | hosts_ports.push_back( 64 ); | |
7c85a848 | 235 | hosts_source_interfaces.push_back( "eth6" ); |
7724b119 | 236 | hosts_ping_protocols.push_back( "ICMPv6,TCP_IPv6" ); |
c4911662 | 237 | hosts_intervals.push_back( "76" ); |
33695392 GMF |
238 | |
239 | ConfigurationOptions config_options; | |
240 | Configuration configuration; | |
241 | boost::program_options::variables_map vm; | |
242 | option_insert( "host.name", boost::any( hosts_names ), vm ); | |
243 | option_insert( "host.port", boost::any( hosts_ports ), vm ); | |
244 | option_insert( "host.ping-protocol", boost::any( hosts_ping_protocols ), vm ); | |
504adbea | 245 | option_insert( "host.source-network-interface", boost::any( hosts_source_interfaces ), vm ); |
33695392 GMF |
246 | option_insert( "host.interval", boost::any( hosts_intervals ), vm ); |
247 | ||
248 | config_options.parse_configuration_options( vm, &configuration ); | |
249 | ||
250 | // host 1 | |
251 | HostItem host1 = configuration.get_hosts().at(0); | |
252 | BOOST_CHECK_EQUAL( host1->get_address(), "www.fazenda.gov.br" ); | |
253 | BOOST_CHECK_EQUAL( host1->get_port(), 61 ); | |
504adbea | 254 | BOOST_CHECK_EQUAL( host1->get_source_network_interface(), "eth1" ); |
47f51204 | 255 | BOOST_CHECK_EQUAL( host1->get_ping_protocol_list().front(), PingProtocol_TCP ); |
33695392 GMF |
256 | BOOST_CHECK_EQUAL( host1->get_interval_in_sec(), 71 ); |
257 | // host 2 | |
258 | HostItem host2 = configuration.get_hosts().at(1); | |
259 | BOOST_CHECK_EQUAL( host2->get_address(), "www.intra2net.de" ); | |
260 | BOOST_CHECK_EQUAL( host2->get_port(), 62 ); | |
504adbea | 261 | BOOST_CHECK_EQUAL( host2->get_source_network_interface(), "eth2" ); |
47f51204 | 262 | BOOST_CHECK_EQUAL( host2->get_ping_protocol_list().front(), PingProtocol_ICMP ); |
33695392 GMF |
263 | BOOST_CHECK_EQUAL( host2->get_interval_in_sec(), 72 ); |
264 | // host 3 | |
265 | HostItem host3 = configuration.get_hosts().at(2); | |
266 | BOOST_CHECK_EQUAL( host3->get_address(), "www.ufsc.br" ); | |
267 | BOOST_CHECK_EQUAL( host3->get_port(), 63 ); | |
504adbea | 268 | BOOST_CHECK_EQUAL( host3->get_source_network_interface(), "eth3" ); |
47f51204 | 269 | BOOST_CHECK_EQUAL( host3->get_ping_protocol_list().front(), PingProtocol_ICMPv6 ); |
33695392 GMF |
270 | BOOST_CHECK_EQUAL( host3->get_interval_in_sec(), 73 ); |
271 | // host 4 | |
47f51204 | 272 | HostItem host4 = configuration.get_hosts().at(3); |
33695392 GMF |
273 | BOOST_CHECK_EQUAL( host4->get_address(), "www.google.com" ); |
274 | BOOST_CHECK_EQUAL( host4->get_port(), 64 ); | |
504adbea | 275 | BOOST_CHECK_EQUAL( host4->get_source_network_interface(), "eth4" ); |
47f51204 | 276 | BOOST_CHECK_EQUAL( host4->get_ping_protocol_list().front(), PingProtocol_TCP_IPv6 ); |
33695392 | 277 | BOOST_CHECK_EQUAL( host4->get_interval_in_sec(), 74 ); |
47f51204 GMF |
278 | // host 5 |
279 | HostItem host5 = configuration.get_hosts().at(4); | |
280 | BOOST_CHECK_EQUAL( host5->get_address(), "www.kernel.org" ); | |
281 | BOOST_CHECK_EQUAL( host5->get_port(), 64 ); | |
7c85a848 | 282 | BOOST_CHECK_EQUAL( host5->get_source_network_interface(), "eth5" ); |
47f51204 GMF |
283 | BOOST_CHECK_EQUAL( host5->get_ping_protocol_list().size(), 2 ); |
284 | BOOST_CHECK_EQUAL( host5->get_ping_protocol_list().front(), PingProtocol_TCP ); | |
285 | BOOST_CHECK_EQUAL( host5->get_ping_protocol_list().back(), PingProtocol_ICMP ); | |
286 | BOOST_CHECK_EQUAL( host5->get_interval_in_sec(), 75 ); | |
287 | // host 6 | |
288 | HostItem host6 = configuration.get_hosts().at(5); | |
289 | BOOST_CHECK_EQUAL( host6->get_address(), "www.linux.com" ); | |
290 | BOOST_CHECK_EQUAL( host6->get_port(), 64 ); | |
7c85a848 | 291 | BOOST_CHECK_EQUAL( host6->get_source_network_interface(), "eth6" ); |
47f51204 GMF |
292 | BOOST_CHECK_EQUAL( host6->get_ping_protocol_list().size(), 2 ); |
293 | BOOST_CHECK_EQUAL( host6->get_ping_protocol_list().front(), PingProtocol_ICMPv6 ); | |
294 | BOOST_CHECK_EQUAL( host6->get_ping_protocol_list().back(), PingProtocol_TCP_IPv6 ); | |
295 | BOOST_CHECK_EQUAL( host6->get_interval_in_sec(), 76 ); | |
33695392 GMF |
296 | } |
297 | ||
298 | BOOST_AUTO_TEST_CASE( halt_on_generic_options ) | |
299 | { | |
300 | ConfigurationOptions config_options; | |
301 | boost::program_options::variables_map vm; | |
302 | boost::any value; | |
303 | ||
304 | option_clear_and_insert( "config-file", value, vm ); | |
305 | BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false ); | |
306 | ||
307 | option_clear_and_insert( "daemon", value, vm ); | |
308 | BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false ); | |
309 | ||
310 | option_clear_and_insert( "help", value, vm ); | |
311 | BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), true ); | |
312 | ||
313 | option_clear_and_insert( "version", value, vm ); | |
314 | BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), true ); | |
315 | ||
2c40f493 GMF |
316 | option_clear_and_insert( "log-level", value, vm ); |
317 | BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false ); | |
318 | ||
e3c7d9ac GMF |
319 | option_clear_and_insert( "log-output", value, vm ); |
320 | BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false ); | |
321 | ||
fda777ea CH |
322 | option_clear_and_insert( "log-file", value, vm ); |
323 | BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false ); | |
324 | ||
33695392 GMF |
325 | option_clear_and_insert( "hosts-down-limit", value, vm ); |
326 | BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false ); | |
327 | ||
328 | option_clear_and_insert( "link-down-interval", value, vm ); | |
329 | BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false ); | |
330 | ||
331 | option_clear_and_insert( "link-up-interval", value, vm ); | |
332 | BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false ); | |
333 | ||
334 | option_clear_and_insert( "nameserver", value, vm ); | |
335 | BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false ); | |
336 | ||
337 | option_clear_and_insert( "ping-fail-limit", value, vm ); | |
338 | BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false ); | |
339 | ||
504adbea | 340 | option_clear_and_insert( "default-source-network-interface", value, vm ); |
33695392 GMF |
341 | BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false ); |
342 | ||
343 | option_clear_and_insert( "status-notifier-cmd", value, vm ); | |
344 | BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false ); | |
345 | ||
346 | option_clear_and_insert( "host.name", value, vm ); | |
347 | BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false ); | |
348 | ||
349 | option_clear_and_insert( "host.port", value, vm ); | |
350 | BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false ); | |
351 | ||
504adbea GMF |
352 | option_clear_and_insert( "host.source-network-interface", value, vm ); |
353 | BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false ); | |
354 | ||
33695392 GMF |
355 | option_clear_and_insert( "host.ping-protocol", value, vm ); |
356 | BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false ); | |
357 | ||
358 | option_clear_and_insert( "host.interval", value, vm ); | |
359 | BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false ); | |
360 | } | |
361 | ||
362 | BOOST_AUTO_TEST_SUITE_END() |