merged PingRotate into PingScheduler; fixed save/load of cache to/from file; started...
[pingcheck] / test / test_dns.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/test/unit_test.hpp>
27 #include <boost/asio/io_service.hpp>
28 #include <boost/asio/ip/address.hpp>
29
30 #include "host/pingprotocol.h"
31 #include "dns/hostaddress.h"
32 #include "dns/dnsmaster.h"
33 #include "dns/dnscache.h"
34 #include "dns/resolverbase.h"
35
36 //------------------------------------------------------------------------------
37 // helper functions
38 //------------------------------------------------------------------------------
39
40 void create_master();
41
42 // TODO: try to run this only once for test suite
43 void create_master()
44 {
45     // only create if it does not exist yet
46     DnsMasterItem master = DnsMaster::get_instance();
47     if ( !master )
48     {
49         boost::asio::ip::address name_server =
50                              boost::asio::ip::address::from_string("127.0.0.1");
51         int resolved_ip_ttl_threshold = 3;
52         int max_address_resolution_attempts = 2;
53         std::string cache_file = DO_NOT_USE_CACHE_FILE;
54         IoServiceItem io_serv;
55
56         // create it
57         DnsMaster::create_master(io_serv,
58                                  name_server,
59                                  resolved_ip_ttl_threshold,
60                                  max_address_resolution_attempts,
61                                  cache_file);
62         master = DnsMaster::get_instance();
63
64         // simple checks
65         BOOST_CHECK_EQUAL( master->get_resolved_ip_ttl_threshold(),
66                                        resolved_ip_ttl_threshold );
67         BOOST_CHECK_EQUAL( master->get_max_address_resolution_attempts(),
68                                        max_address_resolution_attempts );
69     }
70 }
71
72 //------------------------------------------------------------------------------
73 // test suite
74 //------------------------------------------------------------------------------
75
76 BOOST_AUTO_TEST_SUITE( TestDns )
77
78 BOOST_AUTO_TEST_CASE( create_master )
79 {
80     create_master();
81 }
82
83 BOOST_AUTO_TEST_CASE( create_v4 )
84 {
85     create_master();
86
87     // create resolver
88     std::string hostname = "www.intra2net.com";
89     ResolverItem resolver = DnsMaster::get_instance()
90                                 ->get_resolver_for(hostname, PingProtocol_ICMP);
91     BOOST_CHECK_EQUAL( resolver->get_hostname(), hostname );
92     BOOST_CHECK( !resolver->is_resolving() );
93
94     // cancel should have no effect
95     resolver->cancel_resolve();
96     BOOST_CHECK( !resolver->is_resolving() );
97
98     // should not have any ips since cache did not load anything
99     BOOST_CHECK_EQUAL( resolver->get_resolved_ip_count(), 0 );
100     BOOST_CHECK( !resolver->have_up_to_date_ip() );
101     std::string no_ip = "0.0.0.0";
102     BOOST_CHECK_EQUAL( resolver->get_next_ip().get_ip().to_string(), no_ip );
103 }
104
105 BOOST_AUTO_TEST_SUITE_END()