From: Remi Gacogne Date: Thu, 20 Jul 2017 15:19:42 +0000 (+0200) Subject: rec: Test removal from a NetmaskTree X-Git-Tag: dnsdist-1.2.0~48^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ee32ab25d1694812d854582ec0d8965d3f8b7217;p=pdns rec: Test removal from a NetmaskTree --- diff --git a/pdns/test-nmtree.cc b/pdns/test-nmtree.cc index 2aaedf8f8..b929acad4 100644 --- a/pdns/test-nmtree.cc +++ b/pdns/test-nmtree.cc @@ -95,4 +95,48 @@ BOOST_AUTO_TEST_CASE(test_scale) { } } +BOOST_AUTO_TEST_CASE(test_removal) { + std::string prefix = "192."; + NetmaskTree nmt; + + size_t count = 0; + for(unsigned int i = 0; i < 256; ++i) { + for(unsigned int j = 16; j <= 32; ++j) { + nmt.insert(Netmask(prefix + std::to_string(i) +".127.255/"+std::to_string(j))).second = j; + count++; + } + } + + BOOST_CHECK_EQUAL(nmt.size(), count); + + for(unsigned int i = 0; i < 256; ++i) { + ComboAddress key(prefix + std::to_string(i) + ".127.255"); + const auto result = nmt.lookup(key); + BOOST_CHECK_EQUAL(result->first.getBits(), 32); + BOOST_CHECK_EQUAL(result->first.getMaskedNetwork().toString(), key.toString()); + BOOST_CHECK_EQUAL(result->second, 32); + } + + for(unsigned int i = 0; i < 256; ++i) { + for(unsigned int j = 32; j >= 16; --j) { + ComboAddress key(prefix + std::to_string(i) + ".127.255"); + nmt.erase(Netmask(key, j)); + const auto result = nmt.lookup(key); + + if (j > 16) { + BOOST_REQUIRE(result != nullptr); + BOOST_CHECK_EQUAL(result->first.getBits(), j-1); + BOOST_CHECK_EQUAL(result->first.getMaskedNetwork().toString(), Netmask(key, j-1).getMaskedNetwork().toString()); + BOOST_CHECK_EQUAL(result->second, j - 1); + } + else { + BOOST_CHECK(result == nullptr); + } + } + } + + BOOST_CHECK_EQUAL(nmt.size(), 0); + BOOST_CHECK(nmt.empty()); +} + BOOST_AUTO_TEST_SUITE_END()