]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2725] added unittests
authorRazvan Becheriu <razvan@isc.org>
Thu, 29 Jun 2023 10:29:15 +0000 (13:29 +0300)
committerRazvan Becheriu <razvan@isc.org>
Thu, 29 Jun 2023 17:54:14 +0000 (20:54 +0300)
src/hooks/dhcp/lease_cmds/lease_parser.cc
src/hooks/dhcp/lease_cmds/tests/lease_cmds6_unittest.cc
src/lib/dhcpsrv/parsers/host_reservation_parser.cc
src/lib/dhcpsrv/pool.cc
src/lib/dhcpsrv/tests/host_reservation_parser_unittest.cc

index 32ac214ff690acd91a7c3b383857e593054e8877..2ba0c397850281153639281dc4da6929d08d5475 100644 (file)
@@ -4,9 +4,11 @@
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
+
+#include <asiolink/io_address.h>
+#include <asiolink/addr_utilities.h>
 #include <cc/data.h>
 #include <dhcp/hwaddr.h>
-#include <asiolink/io_address.h>
 #include <dhcpsrv/lease.h>
 #include <dhcpsrv/cfgmgr.h>
 #include <dhcpsrv/cfg_consistency.h>
@@ -383,6 +385,21 @@ Lease6Parser::parse(ConstSrvConfigPtr& cfg,
         ctx = copied;
     }
 
+    // Check if the prefix length is sane
+    if (prefix_len == 0 || prefix_len > 128) {
+        isc_throw(BadValue, "Invalid prefix length: "
+                  << static_cast<unsigned>(prefix_len));
+    }
+
+    if (prefix_len != 128) {
+        IOAddress first_address = firstAddrInPrefix(addr, prefix_len);
+        if (first_address != addr) {
+            isc_throw(BadValue, "Invalid Pool6 address boundaries: " << addr
+                      << " is not the first address in prefix: " << first_address
+                      << "/" << static_cast<uint32_t>(prefix_len));
+        }
+    }
+
     // Let's fabricate some data and we're ready to go.
     Lease6Ptr l(new Lease6(type, addr, duid_ptr, iaid, pref_lft, valid_lft,
                            subnet_id, fqdn_fwd, fqdn_rev, hostname,
index 9ac9e88414c9f73e18d69c66c59116958c77be69..3fa9a5b635d38eaa197f25185e3c21c1825b4ae6 100644 (file)
@@ -688,7 +688,7 @@ void Lease6CmdsTest::testLease6AddBadParams() {
         "    \"command\": \"lease6-add\",\n"
         "    \"arguments\": {"
         "        \"subnet-id\": 66,\n"
-        "        \"ip-address\": \"2001:db8:1::1\",\n"
+        "        \"ip-address\": \"2001:db8:1::\",\n"
         "        \"prefix-len\": 48,\n"
         "        \"type\": \"IA_PD\",\n"
         "        \"duid\": \"1a:1b:1c:1d:1e:1f\",\n"
@@ -698,6 +698,22 @@ void Lease6CmdsTest::testLease6AddBadParams() {
         "}";
     exp_rsp = "Invalid declined state for PD prefix.";
     testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);
+
+    // Invalid prefix len for PD prefix.
+    txt =
+        "{\n"
+        "    \"command\": \"lease6-add\",\n"
+        "    \"arguments\": {"
+        "        \"subnet-id\": 66,\n"
+        "        \"ip-address\": \"2001:db8:1::1\",\n"
+        "        \"prefix-len\": 48,\n"
+        "        \"type\": \"IA_PD\",\n"
+        "        \"duid\": \"1a:1b:1c:1d:1e:1f\",\n"
+        "        \"iaid\": 1234\n"
+        "    }\n"
+        "}";
+    exp_rsp = "Invalid Pool6 address boundaries: 2001:db8:1::1 is not the first address in prefix: 2001:db8:1::/48";
+    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);
 }
 
 void Lease6CmdsTest::testLease6Add() {
@@ -2175,7 +2191,7 @@ void Lease6CmdsTest::testLease6UpdateBadParams() {
         "    \"command\": \"lease6-update\",\n"
         "    \"arguments\": {"
         "        \"subnet-id\": 66,\n"
-        "        \"ip-address\": \"2001:db8:1::1\",\n"
+        "        \"ip-address\": \"2001:db8:1::\",\n"
         "        \"prefix-len\": 48,\n"
         "        \"type\": \"IA_PD\",\n"
         "        \"duid\": \"1a:1b:1c:1d:1e:1f\",\n"
@@ -2185,6 +2201,22 @@ void Lease6CmdsTest::testLease6UpdateBadParams() {
         "}";
     exp_rsp = "Invalid declined state for PD prefix.";
     testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);
+
+    // Invalid prefix len for PD prefix.
+    txt =
+        "{\n"
+        "    \"command\": \"lease6-update\",\n"
+        "    \"arguments\": {"
+        "        \"subnet-id\": 66,\n"
+        "        \"ip-address\": \"2001:db8:1::1\",\n"
+        "        \"prefix-len\": 48,\n"
+        "        \"type\": \"IA_PD\",\n"
+        "        \"duid\": \"1a:1b:1c:1d:1e:1f\",\n"
+        "        \"iaid\": 1234\n"
+        "    }\n"
+        "}";
+    exp_rsp = "Invalid Pool6 address boundaries: 2001:db8:1::1 is not the first address in prefix: 2001:db8:1::/48";
+    testCommand(txt, CONTROL_RESULT_ERROR, exp_rsp);
 }
 
 void Lease6CmdsTest::testLease6UpdateNoLease() {
@@ -3178,7 +3210,7 @@ void Lease6CmdsTest::testLease6BulkApplyAddsOnlyBadParam() {
         "        \"leases\": ["
         "            {"
         "                \"subnet-id\": 66,\n"
-        "                \"ip-address\": \"2001:db8:1::123\",\n"
+        "                \"ip-address\": \"2001:db8:1::\",\n"
         "                \"prefix-len\": 48,\n"
         "                \"type\": \"IA_PD\",\n"
         "                \"duid\": \"11:11:11:11:11:11\",\n"
@@ -3191,6 +3223,26 @@ void Lease6CmdsTest::testLease6BulkApplyAddsOnlyBadParam() {
     string exp_rsp = "Invalid declined state for PD prefix.";
     testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
 
+    // Invalid prefix len for PD prefix.
+    cmd =
+        "{\n"
+        "    \"command\": \"lease6-bulk-apply\",\n"
+        "    \"arguments\": {"
+        "        \"leases\": ["
+        "            {"
+        "                \"subnet-id\": 66,\n"
+        "                \"ip-address\": \"2001:db8:1::123\",\n"
+        "                \"prefix-len\": 48,\n"
+        "                \"type\": \"IA_PD\",\n"
+        "                \"duid\": \"11:11:11:11:11:11\",\n"
+        "                \"iaid\": 1234\n"
+        "            }"
+        "        ]"
+        "    }"
+        "}";
+    exp_rsp = "Invalid Pool6 address boundaries: 2001:db8:1::123 is not the first address in prefix: 2001:db8:1::/48";
+    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
+
     // Check that the lease was not inserted.
     EXPECT_FALSE(lmptr_->getLease6(Lease::TYPE_NA, IOAddress("2001:db8:1::123")));
 }
@@ -3259,7 +3311,7 @@ void Lease6CmdsTest::testLease6BulkApplyUpdatesOnlyBadParam() {
         "            {"
         "                \"subnet-id\": 66,\n"
         "                \"ip-address\": \"2001:db8:1234:ab::\",\n"
-        "                \"prefix-len\": 56,\n"
+        "                \"prefix-len\": 64,\n"
         "                \"type\": \"IA_PD\",\n"
         "                \"duid\": \"77:77:77:77:77:77\",\n"
         "                \"iaid\": 1234,\n"
@@ -3271,6 +3323,26 @@ void Lease6CmdsTest::testLease6BulkApplyUpdatesOnlyBadParam() {
     string exp_rsp = "Invalid declined state for PD prefix.";
     testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
 
+    // Invalid prefix len for PD prefix.
+    cmd =
+        "{\n"
+        "    \"command\": \"lease6-bulk-apply\",\n"
+        "    \"arguments\": {"
+        "        \"leases\": ["
+        "            {"
+        "                \"subnet-id\": 66,\n"
+        "                \"ip-address\": \"2001:db8:1234:ab::\",\n"
+        "                \"prefix-len\": 56,\n"
+        "                \"type\": \"IA_PD\",\n"
+        "                \"duid\": \"77:77:77:77:77:77\",\n"
+        "                \"iaid\": 1234\n"
+        "            }"
+        "        ]"
+        "    }"
+        "}";
+    exp_rsp = "Invalid Pool6 address boundaries: 2001:db8:1234:ab:: is not the first address in prefix: 2001:db8:1234::/56";
+    testCommand(cmd, CONTROL_RESULT_ERROR, exp_rsp);
+
     // Check that the lease we inserted is stored.
     Lease6Ptr lease1 = lmptr_->getLease6(Lease::TYPE_PD, IOAddress("2001:db8:1234:ab::"));
     ASSERT_TRUE(lease1);
index 1c10daa39144ea5a49186561d2e1149ac86804e0..ab6f5fc8b26a7e3bbb10793cb6f7fff5ed9bcf00 100644 (file)
@@ -6,6 +6,7 @@
 
 #include <config.h>
 #include <asiolink/io_address.h>
+#include <asiolink/addr_utilities.h>
 #include <dhcpsrv/cfgmgr.h>
 #include <dhcpsrv/parsers/host_reservation_parser.h>
 #include <dhcpsrv/parsers/option_data_parser.h>
@@ -307,12 +308,27 @@ HostReservationParser6::parseInternal(const SubnetID& subnet_id,
                                       << "' is invalid");
                         }
 
-                        // Remove the  slash character and the prefix length
+                        if ((prefix_len == 0) || (prefix_len > 128)) {
+                            isc_throw(OutOfRange,
+                                      "'prefix-len' value must be in range of [1..128]");
+                        }
+
+                        // Remove the slash character and the prefix length
                         // from the parsed value.
                         prefix.erase(len_pos);
 
                         // Finally, set the reservation type.
                         resrv_type = IPv6Resrv::TYPE_PD;
+
+                        if (prefix_len != 128) {
+                            IOAddress addr(prefix);
+                            IOAddress first_address = firstAddrInPrefix(addr, prefix_len);
+                            if (first_address != addr) {
+                                isc_throw(BadValue, "Invalid lease address boundaries: " << addr
+                                          << " is not the first address in prefix: " << first_address
+                                          << "/" << static_cast<uint32_t>(prefix_len));
+                            }
+                        }
                     }
 
                     // Create a reservation for an address or prefix.
@@ -327,7 +343,6 @@ HostReservationParser6::parseInternal(const SubnetID& subnet_id,
                 }
             }
 
-
         } else if (element.first == "client-classes") {
             try {
                 BOOST_FOREACH(ConstElementPtr class_element,
index 1fb447a2f79eb6061d4c8154a35f0878466225c9..2a47333a34c79085675078206876903f23778ce3 100644 (file)
@@ -308,9 +308,8 @@ Pool6::init(const Lease::Type& type,
                   << static_cast<int>(prefix_len) << ")");
     }
 
-    if (((type == Lease::TYPE_NA) || (type == Lease::TYPE_TA)) &&
-        (delegated_len != 128)) {
-        isc_throw(BadValue, "For IA or TA pools, delegated prefix length must"
+    if ((type != Lease::TYPE_PD) && (delegated_len != 128)) {
+        isc_throw(BadValue, "For NA or TA pools, delegated prefix length must"
                   << " be 128.");
     }
 
@@ -322,11 +321,13 @@ Pool6::init(const Lease::Type& type,
                   << static_cast<int>(delegated_len) << ")");
     }
 
-    IOAddress first_address = firstAddrInPrefix(prefix, prefix_len);
-    if (first_address != prefix) {
-        isc_throw(BadValue, "Invalid Pool6 address boundaries: " << prefix
-                  << " is not the first address in prefix: " << first_address
-                  << "/" << static_cast<uint32_t>(prefix_len));
+    if (prefix_len != 128) {
+        IOAddress first_address = firstAddrInPrefix(prefix, prefix_len);
+        if (first_address != prefix) {
+            isc_throw(BadValue, "Invalid Pool6 address boundaries: " << prefix
+                      << " is not the first address in prefix: " << first_address
+                      << "/" << static_cast<uint32_t>(prefix_len));
+        }
     }
 
     /// @todo: We should probably implement checks against weird addresses
index f819d75d86f5be8971a7cf56478e8b1643877ef5..fd2ed2e0b98ca56dce4ece5435137f176bdc80d5 100644 (file)
@@ -946,11 +946,19 @@ TEST_F(HostReservationParserTest, dhcp6NullAddress) {
     testInvalidConfig<HostReservationParser6>(config);
 }
 
+// This test verifies that the configuration parser throws an exception
+// when invalid prefix length type is specified.
+TEST_F(HostReservationParserTest, dhcp6InvalidPrefixLengthType) {
+    std::string config = "{ \"duid\": \"01:02:03:04:05:06:07:08:09:0A\","
+        "\"prefixes\": [ \"2001:db8:1::/abc\" ] }";
+    testInvalidConfig<HostReservationParser6>(config);
+}
+
 // This test verifies that the configuration parser throws an exception
 // when invalid prefix length is specified.
 TEST_F(HostReservationParserTest, dhcp6InvalidPrefixLength) {
     std::string config = "{ \"duid\": \"01:02:03:04:05:06:07:08:09:0A\","
-        "\"prefixes\": [ \"2001:db8:1::/abc\" ] }";
+        "\"prefixes\": [ \"2001:db8:1::/32\" ] }";
     testInvalidConfig<HostReservationParser6>(config);
 }