Bring aboard log level parsing function.
[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/pingprotocol.h"
31 #include "config/configurationoptions.h"
32
33 //------------------------------------------------------------------------------
34
35 typedef boost::shared_ptr<boost::program_options::option_description> option_description_item;
36
37 bool option_present(
38         const std::vector< option_description_item > &options,
39         const std::string &option_to_find
40 );
41 void option_insert(
42         const std::string &key,
43         const boost::any &value,
44         boost::program_options::variables_map &vm
45 );
46 void option_clear_and_insert(
47         const std::string &key,
48         const boost::any &value,
49         boost::program_options::variables_map &vm
50 );
51
52 //------------------------------------------------------------------------------
53
54 bool option_present(
55         const std::vector< option_description_item > &options,
56         const std::string &option_to_find
57 )
58 {
59     std::vector< option_description_item >::const_iterator first = options.begin();
60     std::vector< option_description_item >::const_iterator last = options.end();
61
62     while ( first != last )
63     {
64         if ( (*first)->long_name() == option_to_find ) {
65             return true;
66         }
67         first++;
68     }
69     return false;
70 }
71
72 void option_insert(
73         const std::string &key,
74         const boost::any &value,
75         boost::program_options::variables_map &vm
76 )
77 {
78     boost::program_options::variable_value var_value( value, true );
79
80     vm.insert( std::pair<std::string, boost::program_options::variable_value>( key, var_value ) );
81 }
82
83 void option_clear_and_insert(
84         const std::string &key,
85         const boost::any &value,
86         boost::program_options::variables_map &vm
87 )
88 {
89     vm.clear();
90
91     option_insert( key, value, vm );
92 }
93
94 //------------------------------------------------------------------------------
95
96 BOOST_AUTO_TEST_SUITE( TestConfigurationOptions )
97
98 BOOST_AUTO_TEST_CASE( get_generic_options )
99 {
100     ConfigurationOptions config_options;
101     std::vector< option_description_item > options = config_options.get_generic_options().options();
102
103     // if this assert fails, you must add or remove one of the options in the
104     // test bellow
105     BOOST_CHECK_EQUAL( options.size(), 4 ); // help, version, daemon and config-file
106
107     BOOST_CHECK_EQUAL( option_present( options, "help" ), true );
108     BOOST_CHECK_EQUAL( option_present( options, "config-file" ), true );
109     BOOST_CHECK_EQUAL( option_present( options, "daemon" ), true );
110     BOOST_CHECK_EQUAL( option_present( options, "version" ), true );
111 }
112
113 BOOST_AUTO_TEST_CASE( get_configuration_options )
114 {
115     ConfigurationOptions config_options;
116     std::vector< option_description_item > options = config_options.get_configuration_options().options();
117
118     // if this assert fails, you must add or remove one of the options in the
119     // test bellow
120     BOOST_CHECK_EQUAL( options.size(), 11 );
121
122     BOOST_CHECK_EQUAL( option_present( options, "hosts-down-limit" ), true );
123     BOOST_CHECK_EQUAL( option_present( options, "link-down-interval" ), true );
124     BOOST_CHECK_EQUAL( option_present( options, "link-up-interval" ), true );
125     BOOST_CHECK_EQUAL( option_present( options, "nameserver" ), true );
126     BOOST_CHECK_EQUAL( option_present( options, "ping-fail-limit" ), true );
127     BOOST_CHECK_EQUAL( option_present( options, "source-network-interface" ), true );
128     BOOST_CHECK_EQUAL( option_present( options, "status-notifier-cmd" ), true );
129     BOOST_CHECK_EQUAL( option_present( options, "host.name" ), true );
130     BOOST_CHECK_EQUAL( option_present( options, "host.port" ), true );
131     BOOST_CHECK_EQUAL( option_present( options, "host.ping-protocol" ), true );
132     BOOST_CHECK_EQUAL( option_present( options, "host.interval" ), true );
133 }
134
135 BOOST_AUTO_TEST_CASE( parse_generic_options )
136 {
137     boost::program_options::variables_map vm;
138
139     option_insert( "config-file", boost::any( std::string("pingcheck.conf") ), vm );
140     option_insert( "daemon", boost::any(), vm );
141
142     ConfigurationOptions config_options;
143     Configuration configuration;
144     config_options.parse_generic_options( vm, &configuration );
145
146     BOOST_CHECK_EQUAL( configuration.get_config_file_name(), "pingcheck.conf" );
147     BOOST_CHECK_EQUAL( configuration.get_daemon(), true );
148 }
149
150 BOOST_AUTO_TEST_CASE( parse_configuration_options )
151 {
152     boost::program_options::variables_map vm;
153
154     option_insert( "hosts-down-limit", boost::any( 0 ), vm );
155     option_insert( "link-down-interval", boost::any( 30 ), vm );
156     option_insert( "link-up-interval", boost::any( 40 ), vm );
157     option_insert( "nameserver", boost::any( std::string("localhost") ), vm );
158     option_insert( "ping-fail-limit", boost::any( 50 ), vm );
159     option_insert( "source-network-interface", boost::any( std::string("wlan1") ), vm );
160     option_insert( "status-notifier-cmd", boost::any( std::string("/tmp/command.sh") ), vm );
161
162     ConfigurationOptions config_options;
163     Configuration configuration;
164     config_options.parse_configuration_options( vm, &configuration );
165
166     BOOST_CHECK_EQUAL( configuration.get_hosts_down_limit(), 0 );
167     BOOST_CHECK_EQUAL( configuration.get_link_down_interval_in_min(), 30 );
168     BOOST_CHECK_EQUAL( configuration.get_link_up_interval_in_min(), 40 );
169     BOOST_CHECK_EQUAL( configuration.get_nameserver(), "localhost" );
170     BOOST_CHECK_EQUAL( configuration.get_ping_fail_limit(), 50 );
171     BOOST_CHECK_EQUAL( configuration.get_source_network_interface(), "wlan1" );
172     BOOST_CHECK_EQUAL( configuration.get_status_notifier_cmd(), "/tmp/command.sh" );
173 }
174
175 BOOST_AUTO_TEST_CASE( parse_hosts_options )
176 {
177     std::vector<std::string> hosts_names;
178     std::vector<int> hosts_ports;
179     std::vector<std::string> hosts_ping_protocols;
180     std::vector<int> hosts_intervals;
181     // host 1
182     hosts_names.push_back( "www.fazenda.gov.br" );
183     hosts_ports.push_back( 61 );
184     hosts_ping_protocols.push_back( "TCP" );
185     hosts_intervals.push_back( 71 );
186     // host 2
187     hosts_names.push_back( "www.intra2net.de" );
188     hosts_ports.push_back( 62 );
189     hosts_ping_protocols.push_back( "ICMP" );
190     hosts_intervals.push_back( 72 );
191     // host 3
192     hosts_names.push_back( "www.ufsc.br" );
193     hosts_ports.push_back( 63 );
194     hosts_ping_protocols.push_back( "ICMPv6" );
195     hosts_intervals.push_back( 73 );
196     // host 4
197     hosts_names.push_back( "www.google.com" );
198     hosts_ports.push_back( 64 );
199     hosts_ping_protocols.push_back( "TCP_IPv6" );
200     hosts_intervals.push_back( 74 );
201
202     ConfigurationOptions config_options;
203     Configuration configuration;
204     boost::program_options::variables_map vm;
205     option_insert( "host.name", boost::any( hosts_names ), vm );
206     option_insert( "host.port", boost::any( hosts_ports ), vm );
207     option_insert( "host.ping-protocol", boost::any( hosts_ping_protocols ), vm );
208     option_insert( "host.interval", boost::any( hosts_intervals ), vm );
209
210     config_options.parse_configuration_options( vm, &configuration );
211
212     // host 1
213      HostItem host1 = configuration.get_hosts().at(0);
214     BOOST_CHECK_EQUAL( host1->get_address(), "www.fazenda.gov.br" );
215     BOOST_CHECK_EQUAL( host1->get_port(), 61 );
216     BOOST_CHECK_EQUAL( host1->get_ping_protocol(), PingProtocol_TCP );
217     BOOST_CHECK_EQUAL( host1->get_interval_in_sec(), 71 );
218     // host 2
219      HostItem host2 = configuration.get_hosts().at(1);
220     BOOST_CHECK_EQUAL( host2->get_address(), "www.intra2net.de" );
221     BOOST_CHECK_EQUAL( host2->get_port(), 62 );
222     BOOST_CHECK_EQUAL( host2->get_ping_protocol(), PingProtocol_ICMP );
223     BOOST_CHECK_EQUAL( host2->get_interval_in_sec(), 72 );
224     // host 3
225      HostItem host3 = configuration.get_hosts().at(2);
226     BOOST_CHECK_EQUAL( host3->get_address(), "www.ufsc.br" );
227     BOOST_CHECK_EQUAL( host3->get_port(), 63 );
228     BOOST_CHECK_EQUAL( host3->get_ping_protocol(), PingProtocol_ICMPv6 );
229     BOOST_CHECK_EQUAL( host3->get_interval_in_sec(), 73 );
230     // host 4
231      HostItem host4 = configuration.get_hosts().at(3);
232     BOOST_CHECK_EQUAL( host4->get_address(), "www.google.com" );
233     BOOST_CHECK_EQUAL( host4->get_port(), 64 );
234     BOOST_CHECK_EQUAL( host4->get_ping_protocol(), PingProtocol_TCP_IPv6 );
235     BOOST_CHECK_EQUAL( host4->get_interval_in_sec(), 74 );
236 }
237
238 BOOST_AUTO_TEST_CASE( halt_on_generic_options )
239 {
240     ConfigurationOptions config_options;
241     boost::program_options::variables_map vm;
242     boost::any value;
243
244     option_clear_and_insert( "config-file", value, vm );
245     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
246
247     option_clear_and_insert( "daemon", value, vm );
248     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
249
250     option_clear_and_insert( "help", value, vm );
251     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), true );
252
253     option_clear_and_insert( "version", value, vm );
254     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), true );
255
256     option_clear_and_insert( "hosts-down-limit", value, vm );
257     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
258
259     option_clear_and_insert( "link-down-interval", value, vm );
260     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
261
262     option_clear_and_insert( "link-up-interval", value, vm );
263     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
264
265     option_clear_and_insert( "nameserver", value, vm );
266     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
267
268     option_clear_and_insert( "ping-fail-limit", value, vm );
269     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
270
271     option_clear_and_insert( "source-network-interface", value, vm );
272     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
273
274     option_clear_and_insert( "status-notifier-cmd", value, vm );
275     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
276
277     option_clear_and_insert( "host.name", value, vm );
278     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
279
280     option_clear_and_insert( "host.port", value, vm );
281     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
282
283     option_clear_and_insert( "host.ping-protocol", value, vm );
284     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
285
286     option_clear_and_insert( "host.interval", value, vm );
287     BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );
288 }
289
290 BOOST_AUTO_TEST_SUITE_END()