From: Razvan Becheriu Date: Thu, 29 Jun 2023 10:29:15 +0000 (+0300) Subject: [#2725] added unittests X-Git-Tag: Kea-2.4.0~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c4a823983cc17865b26e0dd2f445c335ae299e1e;p=thirdparty%2Fkea.git [#2725] added unittests --- diff --git a/src/hooks/dhcp/lease_cmds/lease_parser.cc b/src/hooks/dhcp/lease_cmds/lease_parser.cc index 32ac214ff6..2ba0c39785 100644 --- a/src/hooks/dhcp/lease_cmds/lease_parser.cc +++ b/src/hooks/dhcp/lease_cmds/lease_parser.cc @@ -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 +#include #include #include -#include #include #include #include @@ -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(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(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, diff --git a/src/hooks/dhcp/lease_cmds/tests/lease_cmds6_unittest.cc b/src/hooks/dhcp/lease_cmds/tests/lease_cmds6_unittest.cc index 9ac9e88414..3fa9a5b635 100644 --- a/src/hooks/dhcp/lease_cmds/tests/lease_cmds6_unittest.cc +++ b/src/hooks/dhcp/lease_cmds/tests/lease_cmds6_unittest.cc @@ -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); diff --git a/src/lib/dhcpsrv/parsers/host_reservation_parser.cc b/src/lib/dhcpsrv/parsers/host_reservation_parser.cc index 1c10daa391..ab6f5fc8b2 100644 --- a/src/lib/dhcpsrv/parsers/host_reservation_parser.cc +++ b/src/lib/dhcpsrv/parsers/host_reservation_parser.cc @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -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(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, diff --git a/src/lib/dhcpsrv/pool.cc b/src/lib/dhcpsrv/pool.cc index 1fb447a2f7..2a47333a34 100644 --- a/src/lib/dhcpsrv/pool.cc +++ b/src/lib/dhcpsrv/pool.cc @@ -308,9 +308,8 @@ Pool6::init(const Lease::Type& type, << static_cast(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(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(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(prefix_len)); + } } /// @todo: We should probably implement checks against weird addresses diff --git a/src/lib/dhcpsrv/tests/host_reservation_parser_unittest.cc b/src/lib/dhcpsrv/tests/host_reservation_parser_unittest.cc index f819d75d86..fd2ed2e0b9 100644 --- a/src/lib/dhcpsrv/tests/host_reservation_parser_unittest.cc +++ b/src/lib/dhcpsrv/tests/host_reservation_parser_unittest.cc @@ -946,11 +946,19 @@ TEST_F(HostReservationParserTest, dhcp6NullAddress) { testInvalidConfig(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(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(config); }