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