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