completed partial IPv6 compatibility in DNS; does retrieve and Cache IPv6 IPs
[pingcheck] / test / test_dns.cpp
index 11e7e9d..7938aa3 100644 (file)
@@ -29,9 +29,11 @@ on this file might be covered by the GNU General Public License.
 #include <boost/bind.hpp>
 
 #include <logfunc.hpp>
+#include <filefunc.hxx>
 
 #include "host/pingprotocol.h"
 #include "dns/hostaddress.h"
+#include "dns/dnsipprotocol.h"
 #include "dns/dnsmaster.h"
 #include "dns/dnscache.h"
 #include "dns/resolverbase.h"
@@ -112,7 +114,7 @@ struct GlobalFixture
             HostAddress ip(address::from_string("192.168.42.1"), 61);
             HostAddressVec ips;
             ips.push_back(ip);
-            Cache->update("host1.test", ips);
+            Cache->update("host1.test", DNS_IPv4, ips);
         }
 
         {
@@ -121,7 +123,7 @@ struct GlobalFixture
             HostAddressVec ips;
             ips.push_back(ip1);
             ips.push_back(ip2);
-            Cache->update("host2_3.test", ips);
+            Cache->update("host2_3.test", DNS_IPv4, ips);
         }
 
         {
@@ -145,7 +147,7 @@ struct GlobalFixture
                                         resolved_ip_ttl_threshold ) );
             ips.push_back( HostAddress( address::from_string("192.168.42.7"),
                                         resolved_ip_ttl_threshold+1 ) );
-            Cache->update("host_outdated.test", ips);
+            Cache->update("host_outdated.test", DNS_IPv4, ips);
 
             Cache->update( "cname_outdated.test",
                       Cname("host_outdated.test", resolved_ip_ttl_threshold) );
@@ -158,7 +160,7 @@ struct GlobalFixture
             HostAddressVec ips;
             ips.push_back( HostAddress( address::from_string("192.128.42.8"),
                                         1 ) );
-            Cache->update("host_ttl_below_thresh.test", ips);
+            Cache->update("host_ttl_below_thresh.test", DNS_IPv4, ips);
 
             Cache->update( "cname_ttl_below_thresh.test",
                       Cname("host_ttl_below_thresh.test", 2) );
@@ -171,6 +173,18 @@ struct GlobalFixture
             Cache->update( "cname_loop3.test", Cname("cname_loop1.test", 60) );
         }
 
+        {   // create IPv4 and IPv6 for same host
+            HostAddressVec ips;
+            ips.push_back( HostAddress( address::from_string("192.168.42.8"),
+                                        432 ) );
+            Cache->update("host_v4_and_v6.test", DNS_IPv4, ips);
+            ips.clear();
+            ips.push_back( HostAddress(
+                        address::from_string("2a00:1450:4001:808::1004"),
+                        543 ) );
+            Cache->update("host_v4_and_v6.test", DNS_IPv6, ips);
+        }
+
         BOOST_TEST_MESSAGE( "Done filling cache." );
     }
 
@@ -231,7 +245,7 @@ BOOST_FIXTURE_TEST_SUITE( TestDnsCache, TestFixture )
 
 BOOST_AUTO_TEST_CASE( cache_retrieve_ip1 )
 {
-    HostAddressVec ips = Cache->get_ips("host1.test");
+    HostAddressVec ips = Cache->get_ips("host1.test", DNS_IPv4);
     BOOST_REQUIRE_EQUAL( ips.size(), 1 );
     HostAddress ip = ips.front();
     BOOST_CHECK( ip.is_valid() );
@@ -242,7 +256,7 @@ BOOST_AUTO_TEST_CASE( cache_retrieve_ip1 )
 
 BOOST_AUTO_TEST_CASE( cache_retrieve_ip2 )
 {
-    HostAddressVec ips = Cache->get_ips("host2_3.test");
+    HostAddressVec ips = Cache->get_ips("host2_3.test", DNS_IPv4);
     BOOST_CHECK_EQUAL( ips.size(), 2 );
     HostAddress ip = ips[0];
     BOOST_CHECK( ip.is_valid() );
@@ -256,7 +270,7 @@ BOOST_AUTO_TEST_CASE( cache_retrieve_ip2 )
 
 BOOST_AUTO_TEST_CASE( cache_retrieve_cname )
 {
-    HostAddressVec ips = Cache->get_ips("cname.test");
+    HostAddressVec ips = Cache->get_ips("cname.test", DNS_IPv4);
     BOOST_CHECK( ips.empty() );
 
     Cname cname = Cache->get_cname("cname.test");
@@ -267,7 +281,7 @@ BOOST_AUTO_TEST_CASE( cache_retrieve_cname )
 BOOST_AUTO_TEST_CASE( cache_retrieve_recursive1 )
 {
     // should get IP from host1 but ttl from cname since is smaller
-    HostAddressVec ips = Cache->get_ips_recursive("cname.test");
+    HostAddressVec ips = Cache->get_ips_recursive("cname.test", DNS_IPv4);
     BOOST_REQUIRE_EQUAL( ips.size(), 1 );
     HostAddress ip = ips.front();
     BOOST_CHECK( ip.is_valid() );
@@ -278,7 +292,7 @@ BOOST_AUTO_TEST_CASE( cache_retrieve_recursive1 )
 BOOST_AUTO_TEST_CASE( cache_retrieve_recursive2 )
 {
     // should get IP from host1 but ttl from cname2 since is smaller
-    HostAddressVec ips = Cache->get_ips_recursive("cname3.test");
+    HostAddressVec ips = Cache->get_ips_recursive("cname3.test", DNS_IPv4);
     BOOST_REQUIRE_EQUAL( ips.size(), 1 );
     HostAddress ip = ips.front();
     BOOST_CHECK( ip.is_valid() );
@@ -303,12 +317,13 @@ void cname_skip_test(const uint32_t ttl1, const uint32_t ttl2,
         HostAddressVec ips;
         ips.push_back( HostAddress( address::from_string("192.168.42.100"),
                                     ttl4 ) );
-        cache->update("skip_chain_fourth.test", ips);
+        cache->update("skip_chain_fourth.test", DNS_IPv4, ips);
     }
 
     // normal recursive call should give nothing since one cname is outdated
     bool check_up_to_date = true;
     HostAddressVec ips = cache->get_ips_recursive("skip_chain_first.test",
+                                                  DNS_IPv4,
                                                   check_up_to_date);
     bool one_is_out_of_date = (ttl1 < 5) || (ttl2 < 5)
                            || (ttl3 < 5) || (ttl4 < 5);
@@ -347,59 +362,118 @@ BOOST_AUTO_TEST_CASE( cache_skip_tests )
     BOOST_CHECK_EQUAL( Cache->get_first_outdated_cname("host1.test", 5), "" );
 }
 
-BOOST_AUTO_TEST_CASE( cache_load_test )
+
+void create_cache(const std::string &file_name, IoServiceItem io_serv)
 {
-    std::stringstream file_name;
-    file_name << DATA_DIR_STRING << "/" << "dns_cache_example.xml";
-    BOOST_TEST_MESSAGE( "loading cache from file " << file_name.str() );
-    DnsCache loaded_cache( IoService,
-                           file_name.str(),
+    BOOST_TEST_MESSAGE( "creating cache for file " << file_name );
+    if ( I2n::file_exists(file_name) )
+        BOOST_CHECK( I2n::unlink(file_name) );   // try to remove
+    DnsCache save_cache( io_serv, file_name, min_time_between_resolves );
+
+    HostAddressVec ips;
+    ips.push_back( HostAddress(address::from_string("11.22.33.44"), 567) );
+    save_cache.update("abc.xyz", DNS_IPv4, ips);
+
+    save_cache.update("cname1.xyz", Cname("abc.xyz", 27));
+
+    // is saved when destructed
+}
+
+
+void test_cache(const std::string &file_name, IoServiceItem io_serv)
+{
+    BOOST_TEST_MESSAGE( "loading cache from file " << file_name );
+    DnsCache loaded_cache( io_serv,
+                           file_name,
                            min_time_between_resolves );
-    HostAddressVec ips = loaded_cache.get_ips("abc.xyz");
-    BOOST_REQUIRE_EQUAL( ips.size(), 1 );
-    HostAddress ip = ips.front();
-    BOOST_CHECK_EQUAL( ip.get_ip().to_string(), "11.22.33.44" );
-    BOOST_CHECK_EQUAL( ip.get_ttl().get_value(), 567 );
-    BOOST_CHECK_EQUAL( ip.get_ttl().get_updated_value(), 0 );
+    HostAddressVec ips = loaded_cache.get_ips("abc.xyz", DNS_IPv4);
+    BOOST_CHECK_EQUAL( ips.size(), 1 );
+    if (ips.size() == 1)
+    {
+        HostAddress ip = ips.front();
+        BOOST_CHECK_EQUAL( ip.get_ip().to_string(), "11.22.33.44" );
+        BOOST_CHECK_EQUAL( ip.get_ttl().get_value(), 567 );
+        //BOOST_CHECK_EQUAL( ip.get_ttl().get_updated_value(), 0 );
+    }
 
     Cname cname = loaded_cache.get_cname("cname1.xyz");
     BOOST_CHECK_EQUAL( cname.Host, "abc.xyz" );
     BOOST_CHECK_EQUAL( cname.Ttl.get_value(), 27 );
-    BOOST_CHECK_EQUAL( cname.Ttl.get_updated_value(), 0 );
+    //BOOST_CHECK_EQUAL( cname.Ttl.get_updated_value(), 0 );
 
     // not testing Ttl set time since is private
 }
 
+
+BOOST_AUTO_TEST_CASE( cache_save_and_load_test )
+{
+    std::stringstream file_name_temp;
+    file_name_temp << DATA_DIR_STRING << "/" << "dns_cache_save_load_test.xml";
+    std::string file_name = file_name_temp.str();
+
+    // create and save
+    create_cache( file_name, IoService );
+
+    //  now load and test
+    test_cache( file_name, IoService );
+
+}
+
+BOOST_AUTO_TEST_CASE( cache_load_compatibility_test )
+{
+    std::stringstream file_name;
+    file_name << DATA_DIR_STRING << "/" << "dns_cache_compatibility_test.xml";
+    test_cache( file_name.str(), IoService );
+
+    // If this test fails because the cache structure was changed, 
+    // you can overwrite the dns_cache_compatibility_test.xml by uncommenting
+    // the following code line.
+    //create_cache( file_name.str(), IoService );
+    //
+    // Run test again with arg --log_level=MESSAGE and look out for message
+    //   "create cache for file [...]dns_cache_compatibility_test.xml"
+    // You should then copy the dns_cache_compatibility_test.xml to test/data
+    // in git and commit together with the new cache structure code
+}
+
 BOOST_AUTO_TEST_CASE( cache_outdated_test )
 {
     bool check_up_to_date = false;
-    HostAddressVec ips = Cache->get_ips("host_outdated.test", check_up_to_date);
+    HostAddressVec ips = Cache->get_ips("host_outdated.test", DNS_IPv4,
+                                                              check_up_to_date);
     BOOST_CHECK_EQUAL( ips.size(), 4 );
-    ips = Cache->get_ips_recursive("host_outdated.test", check_up_to_date);
+    ips = Cache->get_ips_recursive("host_outdated.test", DNS_IPv4,
+                                                         check_up_to_date);
     BOOST_CHECK_EQUAL( ips.size(), 4 );
     Cname cname = Cache->get_cname("cname_outdated.test", check_up_to_date);
     BOOST_CHECK( !cname.Host.empty() );
-    ips = Cache->get_ips_recursive("cname_outdated.test", check_up_to_date);
+    ips = Cache->get_ips_recursive("cname_outdated.test", DNS_IPv4,
+                                                          check_up_to_date);
     BOOST_CHECK_EQUAL( ips.size(), 4 );
-    ips = Cache->get_ips_recursive("cname_up_to_date.test", check_up_to_date);
+    ips = Cache->get_ips_recursive("cname_up_to_date.test", DNS_IPv4,
+                                                            check_up_to_date);
     BOOST_CHECK_EQUAL( ips.size(), 4 );
 
     check_up_to_date = true;
-    ips = Cache->get_ips( "host_outdated.test", check_up_to_date );
+    ips = Cache->get_ips( "host_outdated.test", DNS_IPv4, check_up_to_date );
     BOOST_CHECK_EQUAL( ips.size(), 1 );
-    ips = Cache->get_ips_recursive("host_outdated.test", check_up_to_date);
+    ips = Cache->get_ips_recursive("host_outdated.test", DNS_IPv4,
+                                                         check_up_to_date);
     BOOST_CHECK_EQUAL( ips.size(), 1 );
     cname = Cache->get_cname("cname_outdated.test", check_up_to_date);
     BOOST_CHECK( cname.Host.empty() );
-    ips = Cache->get_ips_recursive("cname_outdated.test", check_up_to_date);
+    ips = Cache->get_ips_recursive("cname_outdated.test", DNS_IPv4,
+                                                          check_up_to_date);
     BOOST_CHECK_EQUAL( ips.size(), 0 );
-    ips = Cache->get_ips_recursive("cname_up_to_date.test", check_up_to_date);
+    ips = Cache->get_ips_recursive("cname_up_to_date.test", DNS_IPv4,
+                                                            check_up_to_date);
     BOOST_CHECK_EQUAL( ips.size(), 1 );
 }
 
 BOOST_AUTO_TEST_CASE( cache_ttl_below_thresh_test )
 {
-    HostAddressVec ips = Cache->get_ips("host_ttl_below_thresh.test", false);
+    HostAddressVec ips = Cache->get_ips("host_ttl_below_thresh.test", DNS_IPv4,
+                                                                      false);
     BOOST_REQUIRE_EQUAL( ips.size(), 1 );
     HostAddress ip = ips.front();
     BOOST_CHECK_EQUAL( ip.get_ttl().get_value(), min_time_between_resolves );
@@ -410,9 +484,30 @@ BOOST_AUTO_TEST_CASE( cache_ttl_below_thresh_test )
 
 BOOST_AUTO_TEST_CASE( cache_retrieve_loop )
 {
-    BOOST_CHECK_EQUAL( Cache->get_ips_recursive("cname_loop1.test").size(), 0 );
-    BOOST_CHECK_EQUAL( Cache->get_ips_recursive("cname_loop2.test").size(), 0 );
-    BOOST_CHECK_EQUAL( Cache->get_ips_recursive("cname_loop3.test").size(), 0 );
+    BOOST_CHECK_EQUAL(
+            Cache->get_ips_recursive("cname_loop1.test", DNS_IPv4).size(), 0 );
+    BOOST_CHECK_EQUAL(
+            Cache->get_ips_recursive("cname_loop2.test", DNS_IPv4).size(), 0 );
+    BOOST_CHECK_EQUAL(
+            Cache->get_ips_recursive("cname_loop3.test", DNS_IPv4).size(), 0 );
+}
+
+
+BOOST_AUTO_TEST_CASE( cache_IPv6 )
+{
+    HostAddressVec ips = Cache->get_ips("host_v4_and_v6.test", DNS_IPv4);
+    BOOST_REQUIRE_EQUAL( ips.size(), 1 );
+    HostAddress ip = ips.front();
+    BOOST_CHECK( ip.is_valid() );
+    BOOST_CHECK_EQUAL( ip.get_ip().to_string(), "192.168.42.8" );
+    BOOST_CHECK_EQUAL( ip.get_ttl().get_value(), 432 );
+
+    ips = Cache->get_ips("host_v4_and_v6.test", DNS_IPv6);
+    BOOST_REQUIRE_EQUAL( ips.size(), 1 );
+    ip = ips.front();
+    BOOST_CHECK( ip.is_valid() );
+    BOOST_CHECK_EQUAL( ip.get_ip().to_string(), "2a00:1450:4001:808::1004" );
+    BOOST_CHECK_EQUAL( ip.get_ttl().get_value(), 543 );
 }