86463bc3ba28e26123471d5b07f186f6422933d9
[pingcheck] / test / test_configurationoptions.cpp
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
30 #include "host/loglevel.h"
31 #include "host/logoutput.h"
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
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
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         }
69         ++first;
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
107     BOOST_CHECK_EQUAL( options.size(), 7 );
108     // help, version, daemon, config-file, log-level, log-output, log-file
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 );
114     BOOST_CHECK_EQUAL( option_present( options, "log-level" ), true );
115     BOOST_CHECK_EQUAL( option_present( options, "log-output" ), true );
116     BOOST_CHECK_EQUAL( option_present( options, "log-file" ), true );
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
125     // test below. Will probably find them all in
126     // src/config/configurationoptions.cpp constructor
127     BOOST_CHECK_EQUAL( options.size(), 17 );
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 );
134     BOOST_CHECK_EQUAL( option_present( options, "default-source-network-interface" ), true );
135     BOOST_CHECK_EQUAL( option_present( options, "status-notifier-cmd" ), true );
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 );
139     BOOST_CHECK_EQUAL( option_present( options, "dns-cache-file" ), true );
140     BOOST_CHECK_EQUAL( option_present( options, "ratio-random-hosts" ), true );
141     BOOST_CHECK_EQUAL( option_present( options, "host.name" ), true );
142     BOOST_CHECK_EQUAL( option_present( options, "host.port" ), true );
143     BOOST_CHECK_EQUAL( option_present( options, "host.source-network-interface" ), true );
144     BOOST_CHECK_EQUAL( option_present( options, "host.ping-protocol" ), true );
145     BOOST_CHECK_EQUAL( option_present( options, "host.interval" ), true );
146 }
147
148 BOOST_AUTO_TEST_CASE( parse_generic_options )
149 {
150     boost::program_options::variables_map vm;
151
152     option_insert( "config-file", boost::any( std::string("pingcheck.conf") ), vm );
153     option_insert( "daemon", boost::any(), vm );
154     option_insert( "log-level", boost::any( std::string("EMERGENCY") ), vm );
155     option_insert( "log-output", boost::any( std::string("TERMINAL") ), vm );
156     option_insert( "log-file", boost::any( std::string("pingcheck_test.log") ), vm );
157
158     ConfigurationOptions config_options;
159     Configuration configuration;
160     config_options.parse_generic_options( vm, &configuration );
161
162     BOOST_CHECK_EQUAL( configuration.get_config_file_name(), "pingcheck.conf" );
163     BOOST_CHECK_EQUAL( configuration.get_daemon(), true );
164     BOOST_CHECK_EQUAL( static_cast<int>(configuration.get_log_level()), static_cast<int>(I2n::Logger::LogLevel::Emergency) );
165     BOOST_CHECK_EQUAL( static_cast<int>(configuration.get_log_output()), static_cast<int>(LogOutput_TERMINAL) );
166     BOOST_CHECK_EQUAL( configuration.get_log_file(), "pingcheck_test.log" );
167 }
168
169 BOOST_AUTO_TEST_CASE( parse_configuration_options )
170 {
171     boost::program_options::variables_map vm;
172
173     option_insert( "hosts-down-limit", boost::any( 0 ), vm );
174     option_insert( "link-down-interval", boost::any( 30 ), vm );
175     option_insert( "link-up-interval", boost::any( 40 ), vm );
176     option_insert( "nameserver", boost::any( std::string("localhost") ), vm );
177     option_insert( "ping-fail-limit", boost::any( 50 ), vm );
178     option_insert( "default-source-network-interface", boost::any( std::string("wlan1") ), vm );
179     option_insert( "status-notifier-cmd", boost::any( std::string("/tmp/command.sh") ), vm );
180
181     ConfigurationOptions config_options;
182     Configuration configuration;
183     config_options.parse_configuration_options( vm, &configuration );
184
185     BOOST_CHECK_EQUAL( configuration.get_hosts_down_limit(), 0 );
186     BOOST_CHECK_EQUAL( configuration.get_link_down_interval_in_min(), 30 );
187     BOOST_CHECK_EQUAL( configuration.get_link_up_interval_in_min(), 40 );
188     BOOST_CHECK_EQUAL( configuration.get_nameserver(), "localhost" );
189     BOOST_CHECK_EQUAL( configuration.get_ping_fail_limit(), 50 );
190     BOOST_CHECK_EQUAL( configuration.get_source_network_interface(), "wlan1" );
191     BOOST_CHECK_EQUAL( configuration.get_status_notifier_cmd(), "/tmp/command.sh" );
192 }
193
194 BOOST_AUTO_TEST_CASE( parse_hosts_options )
195 {
196     std::vector<std::string> hosts_names;
197     std::vector<int> hosts_ports;
198     std::vector<std::string> hosts_source_interfaces;
199     std::vector<std::string> hosts_ping_protocols;
200     std::vector<std::string> hosts_intervals;
201     // host 1
202     hosts_names.push_back( "www.fazenda.gov.br" );
203     hosts_ports.push_back( 61 );
204     hosts_source_interfaces.push_back( "eth1" );
205     hosts_ping_protocols.push_back( "TCP" );
206     hosts_intervals.push_back( "71" );
207     // host 2
208     hosts_names.push_back( "www.intra2net.de" );
209     hosts_ports.push_back( 62 );
210     hosts_source_interfaces.push_back( "eth2" );
211     hosts_ping_protocols.push_back( "ICMP" );
212     hosts_intervals.push_back( "72" );
213     // host 3
214     hosts_names.push_back( "www.ufsc.br" );
215     hosts_ports.push_back( 63 );
216     hosts_source_interfaces.push_back( "eth3" );
217     hosts_ping_protocols.push_back( "ICMPv6" );
218     hosts_intervals.push_back( "73" );
219     // host 4
220     hosts_names.push_back( "www.google.com" );
221     hosts_ports.push_back( 64 );
222     hosts_source_interfaces.push_back( "eth4" );
223     hosts_ping_protocols.push_back( "TCP_IPv6" );
224     hosts_intervals.push_back( "74" );
225     // host 5
226     hosts_names.push_back( "www.kernel.org" );
227     hosts_ports.push_back( 64 );
228     hosts_source_interfaces.push_back( "eth5" );
229     hosts_ping_protocols.push_back( "TCP_IPv4,ICMPv4" );
230     hosts_intervals.push_back( "75" );
231     // host 6
232     hosts_names.push_back( "www.linux.com" );
233     hosts_ports.push_back( 64 );
234     hosts_source_interfaces.push_back( "eth6" );
235     hosts_ping_protocols.push_back( "ICMPv6,TCP_IPv6" );
236     hosts_intervals.push_back( "76" );
237
238     ConfigurationOptions config_options;
239     Configuration configuration;
240     boost::program_options::variables_map vm;
241     option_insert( "host.name", boost::any( hosts_names ), vm );
242     option_insert( "host.port", boost::any( hosts_ports ), vm );
243     option_insert( "host.ping-protocol", boost::any( hosts_ping_protocols ), vm );
244     option_insert( "host.source-network-interface", boost::any( hosts_source_interfaces ), vm );
245     option_insert( "host.interval", boost::any( hosts_intervals ), vm );
246
247     config_options.parse_configuration_options( vm, &configuration );
248
249     // host 1
250      HostItem host1 = configuration.get_hosts().at(0);
251     BOOST_CHECK_EQUAL( host1->get_address(), "www.fazenda.gov.br" );
252     BOOST_CHECK_EQUAL( host1->get_port(), 61 );
253     BOOST_CHECK_EQUAL( host1->get_source_network_interface(), "eth1" );
254     BOOST_CHECK_EQUAL( host1->get_ping_protocol_list().front(), PingProtocol_TCP );
255     BOOST_CHECK_EQUAL( host1->get_interval_in_sec(), 71 );
256     // host 2
257      HostItem host2 = configuration.get_hosts().at(1);
258     BOOST_CHECK_EQUAL( host2->get_address(), "www.intra2net.de" );
259     BOOST_CHECK_EQUAL( host2->get_port(), 62 );
260     BOOST_CHECK_EQUAL( host2->get_source_network_interface(), "eth2" );
261     BOOST_CHECK_EQUAL( host2->get_ping_protocol_list().front(), PingProtocol_ICMP );
262     BOOST_CHECK_EQUAL( host2->get_interval_in_sec(), 72 );
263     // host 3
264      HostItem host3 = configuration.get_hosts().at(2);
265     BOOST_CHECK_EQUAL( host3->get_address(), "www.ufsc.br" );
266     BOOST_CHECK_EQUAL( host3->get_port(), 63 );
267     BOOST_CHECK_EQUAL( host3->get_source_network_interface(), "eth3" );
268     BOOST_CHECK_EQUAL( host3->get_ping_protocol_list().front(), PingProtocol_ICMPv6 );
269     BOOST_CHECK_EQUAL( host3->get_interval_in_sec(), 73 );
270     // host 4
271     HostItem host4 = configuration.get_hosts().at(3);
272     BOOST_CHECK_EQUAL( host4->get_address(), "www.google.com" );
273     BOOST_CHECK_EQUAL( host4->get_port(), 64 );
274     BOOST_CHECK_EQUAL( host4->get_source_network_interface(), "eth4" );
275     BOOST_CHECK_EQUAL( host4->get_ping_protocol_list().front(), PingProtocol_TCP_IPv6 );
276     BOOST_CHECK_EQUAL( host4->get_interval_in_sec(), 74 );
277     // host 5
278     HostItem host5 = configuration.get_hosts().at(4);
279     BOOST_CHECK_EQUAL( host5->get_address(), "www.kernel.org" );
280     BOOST_CHECK_EQUAL( host5->get_port(), 64 );
281     BOOST_CHECK_EQUAL( host5->get_source_network_interface(), "eth5" );
282     BOOST_CHECK_EQUAL( host5->get_ping_protocol_list().size(), 2 );
283     BOOST_CHECK_EQUAL( host5->get_ping_protocol_list().front(), PingProtocol_TCP );
284     BOOST_CHECK_EQUAL( host5->get_ping_protocol_list().back(), PingProtocol_ICMP );
285     BOOST_CHECK_EQUAL( host5->get_interval_in_sec(), 75 );
286     // host 6
287     HostItem host6 = configuration.get_hosts().at(5);
288     BOOST_CHECK_EQUAL( host6->get_address(), "www.linux.com" );
289     BOOST_CHECK_EQUAL( host6->get_port(), 64 );
290     BOOST_CHECK_EQUAL( host6->get_source_network_interface(), "eth6" );
291     BOOST_CHECK_EQUAL( host6->get_ping_protocol_list().size(), 2 );
292     BOOST_CHECK_EQUAL( host6->get_ping_protocol_list().front(), PingProtocol_ICMPv6 );
293     BOOST_CHECK_EQUAL( host6->get_ping_protocol_list().back(), PingProtocol_TCP_IPv6 );
294     BOOST_CHECK_EQUAL( host6->get_interval_in_sec(), 76 );
295 }
296
297 BOOST_AUTO_TEST_CASE( halt_on_generic_options )
298 {
299     ConfigurationOptions config_options;
300     boost::program_options::variables_map vm;
301     boost::any value;
302
303     option_clear_and_insert( "config-file", value, vm );
304     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
305
306     option_clear_and_insert( "daemon", value, vm );
307     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
308
309     option_clear_and_insert( "help", value, vm );
310     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), true );
311
312     option_clear_and_insert( "version", value, vm );
313     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), true );
314
315     option_clear_and_insert( "log-level", value, vm );
316     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
317
318     option_clear_and_insert( "log-output", value, vm );
319     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
320
321     option_clear_and_insert( "log-file", value, vm );
322     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
323
324     option_clear_and_insert( "hosts-down-limit", value, vm );
325     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
326
327     option_clear_and_insert( "link-down-interval", value, vm );
328     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
329
330     option_clear_and_insert( "link-up-interval", value, vm );
331     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
332
333     option_clear_and_insert( "nameserver", value, vm );
334     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
335
336     option_clear_and_insert( "ping-fail-limit", value, vm );
337     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
338
339     option_clear_and_insert( "default-source-network-interface", value, vm );
340     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
341
342     option_clear_and_insert( "status-notifier-cmd", value, vm );
343     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
344
345     option_clear_and_insert( "host.name", value, vm );
346     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
347
348     option_clear_and_insert( "host.port", value, vm );
349     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
350
351     option_clear_and_insert( "host.source-network-interface", value, vm );
352     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
353
354     option_clear_and_insert( "host.ping-protocol", value, vm );
355     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
356
357     option_clear_and_insert( "host.interval", value, vm );
358     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
359 }
360
361 BOOST_AUTO_TEST_SUITE_END()