From: bert hubert Date: Tue, 7 May 2013 11:55:27 +0000 (+0200) Subject: add bunch of netmask/netmaskgroup tests X-Git-Tag: auth-3.3-rc1~105 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=60af67b847f8124b8743de06cf06b05dc2f5131e;p=thirdparty%2Fpdns.git add bunch of netmask/netmaskgroup tests --- diff --git a/pdns/iputils.hh b/pdns/iputils.hh index 312f68ad82..8619eaeaa7 100644 --- a/pdns/iputils.hh +++ b/pdns/iputils.hh @@ -318,6 +318,7 @@ class NetmaskGroup { public: //! If this IP address is matched by any of the classes within + bool match(const ComboAddress *ip) { for(container_t::const_iterator i=d_masks.begin();i!=d_masks.end();++i) @@ -326,6 +327,12 @@ public: return false; } + + bool match(const ComboAddress& ip) + { + return match(&ip); + } + //! Add this Netmask to the list of possible matches void addMask(const string &ip) { diff --git a/pdns/test-iputils_hh.cc b/pdns/test-iputils_hh.cc index b65fb5478e..d3f735d2e8 100644 --- a/pdns/test-iputils_hh.cc +++ b/pdns/test-iputils_hh.cc @@ -18,4 +18,48 @@ BOOST_AUTO_TEST_CASE(test_ComboAddress) { BOOST_CHECK(!(local == remote)); } +BOOST_AUTO_TEST_CASE(test_Netmask) { + ComboAddress local("127.0.0.1", 53); + ComboAddress remote("130.161.252.29", 53); + + Netmask nm("127.0.0.1/24"); + BOOST_CHECK(nm.match(local)); + BOOST_CHECK(!nm.match(remote)); + + Netmask nm6("fe80::92fb:a6ff:fe4a:51da/64"); + BOOST_CHECK(nm6.match("fe80::92fb:a6ff:fe4a:51db")); + BOOST_CHECK(!nm6.match("fe81::92fb:a6ff:fe4a:51db")); + + Netmask nmp("130.161.252.29/32"); + BOOST_CHECK(nmp.match(remote)); + + Netmask nmp6("fe80::92fb:a6ff:fe4a:51da/128"); + BOOST_CHECK(nmp6.match("fe80::92fb:a6ff:fe4a:51da")); + BOOST_CHECK(!nmp6.match("fe81::92fb:a6ff:fe4a:51db")); + + Netmask all("0.0.0.0/0"); + BOOST_CHECK(all.match(local) && all.match(remote)); + + Netmask all6("::/0"); + BOOST_CHECK(all6.match("::1") && all6.match("fe80::92fb:a6ff:fe4a:51da")); +} + +BOOST_AUTO_TEST_CASE(test_NetmaskGroup) { + NetmaskGroup ng; + ng.addMask("127.0.0.0/8"); + ng.addMask("10.0.0.0/24"); + BOOST_CHECK(ng.match(ComboAddress("127.0.0.1"))); + BOOST_CHECK(ng.match(ComboAddress("10.0.0.3"))); + BOOST_CHECK(!ng.match(ComboAddress("128.1.2.3"))); + BOOST_CHECK(!ng.match(ComboAddress("10.0.1.0"))); + BOOST_CHECK(!ng.match(ComboAddress("::1"))); + ng.addMask("::1"); + BOOST_CHECK(ng.match(ComboAddress("::1"))); + BOOST_CHECK(!ng.match(ComboAddress("::2"))); + ng.addMask("fe80::/16"); + BOOST_CHECK(ng.match(ComboAddress("fe80::1"))); + BOOST_CHECK(!ng.match(ComboAddress("fe81::1"))); +} + + BOOST_AUTO_TEST_SUITE_END()