]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[583-cb-cmds-config-get-returns-wrong-subnet-range] Fixed prefixLengthFromRange
authorFrancis Dupont <fdupont@isc.org>
Thu, 13 Jun 2019 09:49:37 +0000 (11:49 +0200)
committerFrancis Dupont <fdupont@isc.org>
Tue, 25 Jun 2019 06:58:34 +0000 (08:58 +0200)
src/lib/asiolink/addr_utilities.cc
src/lib/asiolink/tests/addr_utilities_unittest.cc
src/lib/asiolink/tests/udp_socket_unittest.cc
src/lib/dhcpsrv/pool.h
src/lib/dhcpsrv/tests/pool_unittest.cc

index 62a59a565bb9fc7d78a92ce643a5710e889c1863..f580782313cb331e6214865131938e23024c6c56 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2012-2019 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -286,7 +286,12 @@ prefixLengthFromRange(const IOAddress& min, const IOAddress& max) {
         uint32_t min_numeric = min.toUint32();
 
         // Get the exclusive or which must be one of the bit masks
+        // and the min must be at the beginning of the prefix
+        // so it does not contribute to trailing ones.
         uint32_t xor_numeric = max_numeric ^ min_numeric;
+        if ((min_numeric & ~xor_numeric) != min_numeric) {
+            return (-1);
+        }
         for (uint8_t prefix_len = 0; prefix_len <= 32; ++prefix_len) {
             if (xor_numeric == bitMask4[prefix_len]) {
                 // Got it: the wanted value is also the index
@@ -308,6 +313,11 @@ prefixLengthFromRange(const IOAddress& min, const IOAddress& max) {
         bool zeroes = true;
         for (uint8_t i = 0; i < 16; ++i) {
             uint8_t xor_byte = min_packed[i] ^ max_packed[i];
+            // The min must be at the beginning of the prefix
+            // so it does not contribute to trailing ones.
+            if ((min_packed[i] & ~xor_byte) != min_packed[i]) {
+                return (-1);
+            }
             if (zeroes) {
                 // Skipping zero bits searching for one bits
                 if (xor_byte == 0) {
index 53279a4c7f5146273eaf1282f7c678305e090705..4c6461fc8352a1096a9db70aa1203d02552d5a9c 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2012-2018 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2012-2019 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -266,6 +266,9 @@ TEST(AddrUtilitiesTest, prefixLengthFromRange4) {
     // Fail if a network boundary is crossed
     EXPECT_EQ(-1, plfr(IOAddress("10.0.0.255"), IOAddress("10.0.1.1")));
 
+    // Fail if first is not at the begin
+    EXPECT_EQ(-1, plfr(IOAddress("10.0.0.2"), IOAddress("10.0.0.5")));
+
     // The upper bound cannot be smaller than the lower bound
     EXPECT_THROW(plfr(IOAddress("192.0.2.5"), IOAddress("192.0.2.4")),
                  isc::BadValue);
@@ -323,6 +326,13 @@ TEST(AddrUtilitiesTest, prefixLengthFromRange6) {
     EXPECT_EQ(-1, plfr(IOAddress("2001:db8::ffff"),
                        IOAddress("2001:db8::1:1")));
 
+    // Fail if first is not at the begin
+    EXPECT_EQ(-1, plfr(IOAddress("2001:db8::2"), IOAddress("2001:db8::5")));
+    EXPECT_EQ(-1, plfr(IOAddress("2001:db8::2:0"),
+                       IOAddress("2001:db8::5:ffff")));
+    EXPECT_EQ(-1, plfr(IOAddress("2001:db8::2:ff00:0"),
+                       IOAddress("2001:db8::3:00ff:ffff")));
+
     // The upper bound cannot be smaller than the lower bound
     EXPECT_THROW(plfr(IOAddress("fe80::5"), IOAddress("fe80::4")),
                  isc::BadValue);
index 1a5b2a6568f3416bc7bc88301b8589c353747084..4f61200aa3cf6278bfbc176a62d34f3e911c9f3e 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2016 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2011-2019 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -166,7 +166,7 @@ TEST(UDPSocket, processReceivedData) {
                                             // Where data is put
                                             // cppcheck-suppress variableScope
     size_t                  expected;       // Expected amount of data
-                                            // cppcheck-suppress variableScope    
+                                            // cppcheck-suppress variableScope
     size_t                  offset;         // Where to put next data
                                             // cppcheck-suppress variableScope
     size_t                  cumulative;     // Cumulative data received
index f7cd0136544fe5586ee237efe1144ed7f3c4a91d..d81d2ab2c4ff71498a8aaa50020e5e5d1cdb6181 100644 (file)
@@ -26,7 +26,7 @@ namespace dhcp {
 ///
 /// Stores information about pool of IPv4 or IPv6 addresses.
 /// That is a basic component of a configuration.
-class Pool : public isc::data::UserContext {
+class Pool : public isc::data::UserContext, public isc::data::CfgToElement {
 
 public:
     /// @note:
index 494e40663b80e3495baf8a62fc116719d19212f4..fd3cef07e87bd64dbf23a43b4563e1b3df5accf5 100644 (file)
@@ -10,6 +10,7 @@
 #include <cc/data.h>
 #include <dhcp/option6_pdexclude.h>
 #include <dhcpsrv/pool.h>
+#include <testutils/test_to_element.h>
 
 #include <boost/scoped_ptr.hpp>
 
@@ -120,6 +121,33 @@ TEST(Pool4Test, toText) {
 
     Pool4 pool2(IOAddress("192.0.2.128"), 28);
     EXPECT_EQ("type=V4, 192.0.2.128-192.0.2.143", pool2.toText());
+
+    Pool4 pool3(IOAddress("192.0.2.0"), IOAddress("192.0.2.127"));
+    EXPECT_EQ("type=V4, 192.0.2.0-192.0.2.127", pool3.toText());
+}
+
+// Simple check if toElement returns reasonable values
+TEST(Pool4Test, toElement) {
+    Pool4 pool1(IOAddress("192.0.2.7"), IOAddress("192.0.2.17"));
+    std::string expected1 = "{"
+        " \"pool\": \"192.0.2.7-192.0.2.17\", "
+        " \"option-data\": [ ] "
+        "}";
+    isc::test::runToElementTest<Pool4>(expected1, pool1);
+
+    Pool4 pool2(IOAddress("192.0.2.128"), 28);
+    std::string expected2 = "{"
+        " \"pool\": \"192.0.2.128/28\", "
+        " \"option-data\": [ ] "
+        "}";
+    isc::test::runToElementTest<Pool4>(expected2, pool2);
+
+    Pool4 pool3(IOAddress("192.0.2.0"), IOAddress("192.0.2.127"));
+    std::string expected3 = "{"
+        " \"pool\": \"192.0.2.0/25\", "
+        " \"option-data\": [ ] "
+        "}";
+    isc::test::runToElementTest<Pool4>(expected3, pool3);
 }
 
 // This test checks that it is possible to specify pool specific options.
@@ -506,6 +534,50 @@ TEST(Pool6Test, toText) {
               " excluded_prefix_len=120",
               pool3.toText());
 
+    Pool6 pool4(Lease::TYPE_NA, IOAddress("2001:db8::"),
+                IOAddress("2001:db8::ffff"));
+    EXPECT_EQ("type=IA_NA, 2001:db8::-2001:db8::ffff, delegated_len=128",
+              pool4.toText());
+}
+
+// Simple check if toElement returns reasonable values
+TEST(Pool6Test, toElement) {
+    Pool6 pool1(Lease::TYPE_NA, IOAddress("2001:db8::1"),
+                IOAddress("2001:db8::2"));
+    std::string expected1 = "{"
+        " \"pool\": \"2001:db8::1-2001:db8::2\", "
+        " \"option-data\": [ ] "
+        "}";
+    isc::test::runToElementTest<Pool6>(expected1, pool1);
+
+    Pool6 pool2(Lease::TYPE_PD, IOAddress("2001:db8:1::"), 96, 112);
+    std::string expected2 = "{"
+        " \"prefix\": \"2001:db8:1::\", "
+        " \"prefix-len\": 96, "
+        " \"delegated-len\": 112, "
+        " \"option-data\": [ ] "
+        "}";
+    isc::test::runToElementTest<Pool6>(expected2, pool2);
+
+    Pool6 pool3(IOAddress("2001:db8:1::"), 96, 112,
+                IOAddress("2001:db8:1::1000"), 120);
+    std::string expected3 = "{"
+        " \"prefix\": \"2001:db8:1::\", "
+        " \"prefix-len\": 96, "
+        " \"delegated-len\": 112, "
+        " \"excluded-prefix\": \"2001:db8:1::1000\", "
+        " \"excluded-prefix-len\": 120, "
+        " \"option-data\": [ ] "
+        "}";
+    isc::test::runToElementTest<Pool6>(expected3, pool3);
+
+    Pool6 pool4(Lease::TYPE_NA, IOAddress("2001:db8::"),
+                IOAddress("2001:db8::ffff"));
+    std::string expected4 = "{"
+        " \"pool\": \"2001:db8::/112\", "
+        " \"option-data\": [ ] "
+        "}";
+    isc::test::runToElementTest<Pool6>(expected4, pool4);
 }
 
 // Checks if the number of possible leases in range is reported correctly.