]> granicus.if.org Git - pdns/commitdiff
Add test suite for NetmaskTree
authorAki Tuomi <cmouse@desteem.org>
Sun, 15 Nov 2015 10:35:20 +0000 (12:35 +0200)
committerAki Tuomi <cmouse@desteem.org>
Tue, 17 Nov 2015 18:28:49 +0000 (20:28 +0200)
pdns/Makefile.am
pdns/test-nmtree.cc [new file with mode: 0644]

index 386da5f8cad8ef3fca96c4d4a4ce07720487c53c..34d6b2630ad0e26173d84c1a90069f987195d9bd 100644 (file)
@@ -1049,6 +1049,7 @@ testrunner_SOURCES = \
        test-md5_hh.cc \
        test-misc_hh.cc \
        test-nameserver_cc.cc \
+       test-nmtree.cc \
        test-packetcache_cc.cc \
        test-rcpgenerator_cc.cc \
        test-sha_hh.cc \
diff --git a/pdns/test-nmtree.cc b/pdns/test-nmtree.cc
new file mode 100644 (file)
index 0000000..5cb7583
--- /dev/null
@@ -0,0 +1,92 @@
+#define BOOST_TEST_DYN_LINK
+#define BOOST_TEST_NO_MAIN
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include <boost/test/unit_test.hpp>
+#include <bitset>
+#include "iputils.hh"
+
+using namespace boost;
+
+BOOST_AUTO_TEST_SUITE(nmtree)
+
+BOOST_AUTO_TEST_CASE(test_basic) {
+  NetmaskTree<int> nmt;
+  nmt.insert(Netmask("130.161.252.0/24")).second=0;
+  nmt.insert(Netmask("130.161.0.0/16")).second=1;
+  nmt.insert(Netmask("130.0.0.0/8")).second=2;
+
+  BOOST_CHECK_EQUAL(nmt.lookup(ComboAddress("213.244.168.210")), (void*)0);
+  auto found=nmt.lookup(ComboAddress("130.161.252.29"));
+  BOOST_CHECK(found);
+  BOOST_CHECK_EQUAL(found->second, 0);
+  found=nmt.lookup(ComboAddress("130.161.180.1"));
+  BOOST_CHECK(found);
+  BOOST_CHECK_EQUAL(found->second, 1);
+
+  BOOST_CHECK_EQUAL(nmt.lookup(ComboAddress("130.255.255.255"))->second, 2);
+  BOOST_CHECK_EQUAL(nmt.lookup(ComboAddress("130.161.252.255"))->second, 0);
+  BOOST_CHECK_EQUAL(nmt.lookup(ComboAddress("130.161.253.255"))->second, 1);
+
+  found=nmt.lookup(ComboAddress("130.145.180.1"));
+  BOOST_CHECK(found);
+  BOOST_CHECK_EQUAL(found->second, 2);
+
+  nmt.clear();
+  BOOST_CHECK(!nmt.lookup(ComboAddress("130.161.180.1")));
+
+  nmt.insert(Netmask("::1")).second=1;
+  nmt.insert(Netmask("::/0")).second=0;
+  nmt.insert(Netmask("fe80::/16")).second=2;
+  BOOST_CHECK_EQUAL(nmt.lookup(ComboAddress("130.161.253.255")), (void*)0);
+  BOOST_CHECK_EQUAL(nmt.lookup(ComboAddress("::2"))->second, 0);
+  BOOST_CHECK_EQUAL(nmt.lookup(ComboAddress("::ffff"))->second, 0);
+  BOOST_CHECK_EQUAL(nmt.lookup(ComboAddress("::1"))->second, 1);
+  BOOST_CHECK_EQUAL(nmt.lookup(ComboAddress("fe80::1"))->second, 2);
+}
+
+BOOST_AUTO_TEST_CASE(test_scale) {
+  string start="192.168.";
+  NetmaskTree<int> works;
+  for(int i=0; i < 256; ++i) {
+    for(int j=0; j < 256; ++j) {
+      works.insert(Netmask(start+std::to_string(i)+"."+std::to_string(j))).second=i*j;
+    }
+  }
+
+  for(int i=0; i < 256; ++i) {
+    for(int j=0; j < 256; ++j) {
+      BOOST_CHECK_EQUAL(works.lookup(ComboAddress(start+std::to_string(i)+"."+std::to_string(j)))->second, i*j);
+    }
+  }
+
+  start="130.161.";
+  for(int i=0; i < 256; ++i) {
+    for(int j=0; j < 256; ++j) {
+      BOOST_CHECK_EQUAL(works.lookup(ComboAddress(start+std::to_string(i)+"."+std::to_string(j))), (void*)0);
+    }
+  }
+
+  start="2000:123:";
+  for(int i=0; i < 256; ++i) {
+    for(int j=0; j < 256; ++j) {
+      works.insert(Netmask(start+std::to_string(i)+":"+std::to_string(j)+"::/64")).second=i*j;
+    }
+  }
+
+  for(int i=0; i < 256; ++i) {
+    for(int j=0; j < 256; ++j) {
+      BOOST_CHECK_EQUAL(works.lookup(ComboAddress(start+std::to_string(i)+":"+std::to_string(j)+"::"+std::to_string(i)+":"+std::to_string(j)))->second, i*j);
+    }
+  }
+
+  start="2001:123:";
+  for(int i=0; i < 256; ++i) {
+    for(int j=0; j < 256; ++j) {
+      BOOST_CHECK_EQUAL(works.lookup(ComboAddress(start+std::to_string(i)+":"+std::to_string(j)+"::"+std::to_string(i)+":"+std::to_string(j))), (void*)0);
+    }
+  }
+}
+
+BOOST_AUTO_TEST_SUITE_END()