From: Aki Tuomi Date: Sun, 15 Nov 2015 10:35:20 +0000 (+0200) Subject: Add test suite for NetmaskTree X-Git-Tag: dnsdist-1.0.0-alpha1~215^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0775e60e2307e186060655340dbce21d15902187;p=pdns Add test suite for NetmaskTree --- diff --git a/pdns/Makefile.am b/pdns/Makefile.am index 386da5f8c..34d6b2630 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -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 index 000000000..5cb758357 --- /dev/null +++ b/pdns/test-nmtree.cc @@ -0,0 +1,92 @@ +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_NO_MAIN +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif +#include +#include +#include "iputils.hh" + +using namespace boost; + +BOOST_AUTO_TEST_SUITE(nmtree) + +BOOST_AUTO_TEST_CASE(test_basic) { + NetmaskTree 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 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()