]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Add more NetmaskGroup unit tests 4977/head
authorRemi Gacogne <remi.gacogne@powerdns.com>
Mon, 6 Feb 2017 11:01:57 +0000 (12:01 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Mon, 6 Feb 2017 11:01:57 +0000 (12:01 +0100)
* negative entries using the explicit flag
* IPv6 negative entries
* regular and negative entries from Netmask objects instead of strings

pdns/test-iputils_hh.cc

index b3727c29e7f40f58782612a392d03a2fa5116164..278fa8e5f1300e921fc7414cce7fc008c60fa062 100644 (file)
@@ -187,35 +187,88 @@ BOOST_AUTO_TEST_CASE(test_Netmask) {
 }
 
 BOOST_AUTO_TEST_CASE(test_NetmaskGroup) {
-  NetmaskGroup ng;
-  ng.addMask("10.0.1.0");
-  BOOST_CHECK(ng.match(ComboAddress("10.0.1.0")));
-  ng.toMasks("127.0.0.0/8, 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("10.0.1.0")));
-  BOOST_CHECK(!ng.match(ComboAddress("128.1.2.3")));
-  BOOST_CHECK(!ng.match(ComboAddress("10.0.1.1")));
-  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_CHECK_EQUAL(ng.toString(), "10.0.1.0/32, 127.0.0.0/8, 10.0.0.0/24, ::1/128, fe80::/16");
-
-  ng.addMask("172.16.0.0/16");
-  BOOST_CHECK(ng.match(ComboAddress("172.16.1.1")));
-  BOOST_CHECK(ng.match(ComboAddress("172.16.4.50")));
-  ng.addMask("172.16.4.0/24", false);
-  BOOST_CHECK(ng.match(ComboAddress("172.16.1.1")));
-  BOOST_CHECK(!ng.match(ComboAddress("172.16.4.50")));
-
-  BOOST_CHECK(ng.match(ComboAddress("172.16.10.80")));
-  ng.addMask("!172.16.10.0/24");
-  BOOST_CHECK(!ng.match(ComboAddress("172.16.10.80")));
-  BOOST_CHECK_EQUAL(ng.toString(), "10.0.1.0/32, 127.0.0.0/8, 10.0.0.0/24, ::1/128, fe80::/16, 172.16.0.0/16, !172.16.4.0/24, !172.16.10.0/24");
+
+  {
+    NetmaskGroup ng;
+    ng.addMask("10.0.1.0");
+    BOOST_CHECK(ng.match(ComboAddress("10.0.1.0")));
+    ng.toMasks("127.0.0.0/8, 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("10.0.1.0")));
+    BOOST_CHECK(!ng.match(ComboAddress("128.1.2.3")));
+    BOOST_CHECK(!ng.match(ComboAddress("10.0.1.1")));
+    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_CHECK_EQUAL(ng.toString(), "10.0.1.0/32, 127.0.0.0/8, 10.0.0.0/24, ::1/128, fe80::/16");
+
+    /* negative entries using the explicit flag */
+    ng.addMask("172.16.0.0/16", true);
+    BOOST_CHECK(ng.match(ComboAddress("172.16.1.1")));
+    BOOST_CHECK(ng.match(ComboAddress("172.16.4.50")));
+    ng.addMask("172.16.4.0/24", false);
+    BOOST_CHECK(ng.match(ComboAddress("172.16.1.1")));
+    BOOST_CHECK(!ng.match(ComboAddress("172.16.4.50")));
+    ng.addMask("fe80::/24", false);
+    BOOST_CHECK(!ng.match(ComboAddress("fe80::1")));
+    BOOST_CHECK(!ng.match(ComboAddress("fe81::1")));
+    /* not in fe80::/24 but in fe80::/16, should match */
+    BOOST_CHECK(ng.match(ComboAddress("fe80:0100::1")));
+
+    /* negative entries using '!' */
+    BOOST_CHECK(ng.match(ComboAddress("172.16.10.80")));
+    ng.addMask("!172.16.10.0/24");
+    BOOST_CHECK(!ng.match(ComboAddress("172.16.10.80")));
+    ng.addMask("2001:db8::/32");
+    ng.addMask("!2001:db8::/64");
+    BOOST_CHECK(!ng.match(ComboAddress("2001:db8::1")));
+    /* not in 2001:db8::/64 but in 2001:db8::/32, should match */
+    BOOST_CHECK(ng.match(ComboAddress("2001:db8:1::1")));
+
+    BOOST_CHECK_EQUAL(ng.toString(), "10.0.1.0/32, 127.0.0.0/8, 10.0.0.0/24, ::1/128, fe80::/16, 172.16.0.0/16, !172.16.4.0/24, !fe80::/24, !172.16.10.0/24, 2001:db8::/32, !2001:db8::/64");
+  }
+
+  {
+    /* this time using Netmask objects instead of strings */
+    NetmaskGroup ng;
+    ng.addMask(Netmask("10.0.1.0"));
+    BOOST_CHECK(ng.match(ComboAddress("10.0.1.0")));
+    ng.addMask(Netmask("127.0.0.0/8"));
+    ng.addMask(Netmask("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("10.0.1.0")));
+    BOOST_CHECK(!ng.match(ComboAddress("128.1.2.3")));
+    BOOST_CHECK(!ng.match(ComboAddress("10.0.1.1")));
+    BOOST_CHECK(!ng.match(ComboAddress("::1")));
+    ng.addMask(Netmask("::1"));
+    BOOST_CHECK(ng.match(ComboAddress("::1")));
+    BOOST_CHECK(!ng.match(ComboAddress("::2")));
+    ng.addMask(Netmask("fe80::/16"));
+    BOOST_CHECK(ng.match(ComboAddress("fe80::1")));
+    BOOST_CHECK(!ng.match(ComboAddress("fe81::1")));
+    BOOST_CHECK_EQUAL(ng.toString(), "10.0.1.0/32, 127.0.0.0/8, 10.0.0.0/24, ::1/128, fe80::/16");
+
+    /* negative entries using the explicit flag */
+    ng.addMask(Netmask("172.16.0.0/16"), true);
+    BOOST_CHECK(ng.match(ComboAddress("172.16.1.1")));
+    BOOST_CHECK(ng.match(ComboAddress("172.16.4.50")));
+    ng.addMask(Netmask("172.16.4.0/24"), false);
+    BOOST_CHECK(ng.match(ComboAddress("172.16.1.1")));
+    BOOST_CHECK(!ng.match(ComboAddress("172.16.4.50")));
+    ng.addMask("fe80::/24", false);
+    BOOST_CHECK(!ng.match(ComboAddress("fe80::1")));
+    BOOST_CHECK(!ng.match(ComboAddress("fe81::1")));
+    /* not in fe80::/24 but in fe80::/16, should match */
+    BOOST_CHECK(ng.match(ComboAddress("fe80:0100::1")));
+
+    BOOST_CHECK_EQUAL(ng.toString(), "10.0.1.0/32, 127.0.0.0/8, 10.0.0.0/24, ::1/128, fe80::/16, 172.16.0.0/16, !172.16.4.0/24, !fe80::/24");
+  }
 }