From: Razvan Becheriu Date: Fri, 25 Jun 2021 11:28:10 +0000 (+0300) Subject: [#1836] added extra unittests X-Git-Tag: Kea-1.9.9~20 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=22cc625725b178a8aa40e1ecc652b6564247d745;p=thirdparty%2Fkea.git [#1836] added extra unittests --- diff --git a/src/bin/dhcp4/tests/dhcp4_srv_unittest.cc b/src/bin/dhcp4/tests/dhcp4_srv_unittest.cc index bcc78fabb5..f85f7fcaa3 100644 --- a/src/bin/dhcp4/tests/dhcp4_srv_unittest.cc +++ b/src/bin/dhcp4/tests/dhcp4_srv_unittest.cc @@ -2378,13 +2378,40 @@ TEST_F(Dhcpv4SrvTest, buildCfgOptionsList) { query_with_classes->setHWAddr(generateHWAddr(6)); query_with_classes->setIface("eth0"); query_with_classes->setIndex(ETH0_INDEX); - query_with_classes->addClass("foo"); // Server id should come from subnet5's client-class value. buildCfgOptionTest(IOAddress("192.0.5.254"), query_with_classes, IOAddress("192.0.5.101"), IOAddress("192.0.5.254")); } + { + SCOPED_TRACE("Global value if client class does not define it"); + + Pkt4Ptr query_with_classes(new Pkt4(DHCPREQUEST, 1234)); + query_with_classes->addOption(generateClientId()); + query_with_classes->setHWAddr(generateHWAddr(6)); + query_with_classes->setIface("eth0"); + query_with_classes->setIndex(ETH0_INDEX); + query_with_classes->addClass("bar"); + + // Server id should be global value as subnet6's client-class does not define it. + buildCfgOptionTest(IOAddress("10.0.0.254"), query_with_classes, IOAddress("192.0.6.101"), IOAddress("192.0.6.100")); + } + + { + SCOPED_TRACE("Global value if client class does not define any option"); + + Pkt4Ptr query_with_classes(new Pkt4(DHCPREQUEST, 1234)); + query_with_classes->addOption(generateClientId()); + query_with_classes->setHWAddr(generateHWAddr(6)); + query_with_classes->setIface("eth0"); + query_with_classes->setIndex(ETH0_INDEX); + query_with_classes->addClass("xyz"); + + // Server id should be global value as subnet7's client-class does not define any option. + buildCfgOptionTest(IOAddress("10.0.0.254"), query_with_classes, IOAddress("192.0.7.101"), IOAddress("192.0.7.100")); + } + { SCOPED_TRACE("Global value"); @@ -2472,7 +2499,7 @@ TEST_F(Dhcpv4SrvTest, acceptServerId) { // A DHCPv4 message holding this server identifier should be accepted. Pkt4Ptr pkt_with_classes(new Pkt4(DHCPREQUEST, 1234)); OptionCustomPtr class_serverid(new OptionCustom(def, Option::V6)); - class_serverid->writeAddress(IOAddress("10.0.0.254")); + class_serverid->writeAddress(IOAddress("192.0.5.254")); ASSERT_NO_THROW(pkt_with_classes->addOption(class_serverid)); pkt_with_classes->addClass("foo"); EXPECT_TRUE(srv.acceptServerId(pkt_with_classes)); @@ -2481,9 +2508,31 @@ TEST_F(Dhcpv4SrvTest, acceptServerId) { ASSERT_NO_THROW(pkt_with_classes->delOption(DHO_DHCP_SERVER_IDENTIFIER)); // Add a server id being an IPv4 address configured on global level. + // The configured class does not define the server id option. // A DHCPv4 message holding this server identifier should be accepted. + Pkt4Ptr pkt_with_classes_option_not_defined(new Pkt4(DHCPREQUEST, 1234)); OptionCustomPtr global_serverid(new OptionCustom(def, Option::V6)); global_serverid->writeAddress(IOAddress("10.0.0.254")); + ASSERT_NO_THROW(pkt_with_classes_option_not_defined->addOption(global_serverid)); + pkt_with_classes_option_not_defined->addClass("bar"); + EXPECT_TRUE(srv.acceptServerId(pkt_with_classes_option_not_defined)); + + // Remove the server identifier. + ASSERT_NO_THROW(pkt_with_classes_option_not_defined->delOption(DHO_DHCP_SERVER_IDENTIFIER)); + + // Add a server id being an IPv4 address configured on global level. + // The configured class does not define any option. + // A DHCPv4 message holding this server identifier should be accepted. + Pkt4Ptr pkt_with_classes_no_options(new Pkt4(DHCPREQUEST, 1234)); + ASSERT_NO_THROW(pkt_with_classes_no_options->addOption(global_serverid)); + pkt_with_classes_no_options->addClass("xyz"); + EXPECT_TRUE(srv.acceptServerId(pkt_with_classes_no_options)); + + // Remove the server identifier. + ASSERT_NO_THROW(pkt_with_classes_no_options->delOption(DHO_DHCP_SERVER_IDENTIFIER)); + + // Add a server id being an IPv4 address configured on global level. + // A DHCPv4 message holding this server identifier should be accepted. ASSERT_NO_THROW(pkt->addOption(global_serverid)); EXPECT_TRUE(srv.acceptServerId(pkt)); diff --git a/src/bin/dhcp4/tests/dhcp4_test_utils.cc b/src/bin/dhcp4/tests/dhcp4_test_utils.cc index 1278878076..1ae11ad888 100644 --- a/src/bin/dhcp4/tests/dhcp4_test_utils.cc +++ b/src/bin/dhcp4/tests/dhcp4_test_utils.cc @@ -205,6 +205,33 @@ Dhcpv4SrvTest::configureServerIdentifier() { CfgMgr::instance().getStagingCfg()->getClientClassDictionary()->addClass("foo", ExpressionPtr(), "", true, false, options); subnet5->requireClientClass("foo"); + // Build and add subnet6. + Subnet4Ptr subnet6(new Subnet4(IOAddress("192.0.6.0"), 24, unspec, unspec, 3600, 6)); + pool.reset(new Pool4(IOAddress("192.0.6.100"), IOAddress("192.0.6.200"))); + subnet6->addPool(pool); + subnet6->setCalculateTeeTimes(false); + + subnets->add(subnet6); + + options.reset(new CfgOption()); + OptionDescriptor desc_other(false); + desc_other.option_ = makeFqdnListOption(); + options->add(desc_other, DHCP4_OPTION_SPACE); + CfgMgr::instance().getStagingCfg()->getClientClassDictionary()->addClass("bar", ExpressionPtr(), "", true, false, options); + subnet6->requireClientClass("bar"); + + // Build and add subnet7. + Subnet4Ptr subnet7(new Subnet4(IOAddress("192.0.7.0"), 24, unspec, unspec, 3600, 7)); + pool.reset(new Pool4(IOAddress("192.0.7.100"), IOAddress("192.0.7.200"))); + subnet7->addPool(pool); + subnet7->setCalculateTeeTimes(false); + + subnets->add(subnet7); + + options.reset(); + CfgMgr::instance().getStagingCfg()->getClientClassDictionary()->addClass("xyz", ExpressionPtr(), "", true, false, options); + subnet7->requireClientClass("xyz"); + // Build and add a shared-network. CfgSharedNetworks4Ptr networks = cfg_mgr.getStagingCfg()->getCfgSharedNetworks4(); SharedNetwork4Ptr network1(new SharedNetwork4("one")); @@ -372,6 +399,28 @@ Dhcpv4SrvTest::makeServerIdOption(const IOAddress& address) { return (server_id); } +OptionPtr +Dhcpv4SrvTest::makeFqdnListOption() { + OptionDefinitionPtr def = LibDHCP::getOptionDef(DHCP4_OPTION_SPACE, + DHO_DOMAIN_SEARCH); + + // Prepare buffer holding an array of FQDNs. + const uint8_t fqdn[] = { + 8, 109, 121, 100, 111, 109, 97, 105, 110, // "mydomain" + 7, 101, 120, 97, 109, 112, 108, 101, // "example" + 3, 99, 111, 109, // "com" + 0 + }; + + // Initialize a vector with the FQDN data. + std::vector fqdn_buf(fqdn, fqdn + sizeof(fqdn)); + + OptionPtr option = def->optionFactory(Option::V4, DHO_DOMAIN_SEARCH, + fqdn_buf.begin(), fqdn_buf.end()); + + return (option); +} + void Dhcpv4SrvTest::checkAddressParams(const Pkt4Ptr& rsp, const Subnet4Ptr subnet, diff --git a/src/bin/dhcp4/tests/dhcp4_test_utils.h b/src/bin/dhcp4/tests/dhcp4_test_utils.h index 73162c40c2..6fe8c87c40 100644 --- a/src/bin/dhcp4/tests/dhcp4_test_utils.h +++ b/src/bin/dhcp4/tests/dhcp4_test_utils.h @@ -415,6 +415,11 @@ public: /// @return Pointer to the newly constructed option. OptionCustomPtr makeServerIdOption(const isc::asiolink::IOAddress& address); + /// @brief Convenience method for making a fqdn list option instance. + /// + /// @return Pointer to the newly constructed option. + OptionPtr makeFqdnListOption(); + /// Check that address was returned from proper range, that its lease /// lifetime is correct, that T1 and T2 are returned properly /// @param rsp response to be checked diff --git a/src/lib/dhcpsrv/tests/client_class_def_unittest.cc b/src/lib/dhcpsrv/tests/client_class_def_unittest.cc index 2e820bb04d..443ce11d98 100644 --- a/src/lib/dhcpsrv/tests/client_class_def_unittest.cc +++ b/src/lib/dhcpsrv/tests/client_class_def_unittest.cc @@ -195,9 +195,10 @@ TEST(ClientClassDef, copyAndEquality) { // that the equality tools reflect that the classes are not equal. ASSERT_NO_THROW(cclass2.reset(new ClientClassDef(*cclass))); EXPECT_TRUE(cclass->equals(*cclass2)); - OptionDefinitionPtr def = LibDHCP::getOptionDef(DHCP4_OPTION_SPACE, 43); + OptionDefinitionPtr def = LibDHCP::getOptionDef(DHCP4_OPTION_SPACE, + DHO_VENDOR_ENCAPSULATED_OPTIONS); EXPECT_FALSE(def); - def = LibDHCP::getLastResortOptionDef(DHCP4_OPTION_SPACE, 43); + def = LibDHCP::getLastResortOptionDef(DHCP4_OPTION_SPACE, DHO_VENDOR_ENCAPSULATED_OPTIONS); EXPECT_TRUE(def); CfgOptionDefPtr cfg(new CfgOptionDef()); ASSERT_NO_THROW(cfg->add(def)); diff --git a/src/lib/dhcpsrv/tests/subnet_unittest.cc b/src/lib/dhcpsrv/tests/subnet_unittest.cc index 1eb4d2965d..be4adb2dfa 100644 --- a/src/lib/dhcpsrv/tests/subnet_unittest.cc +++ b/src/lib/dhcpsrv/tests/subnet_unittest.cc @@ -792,7 +792,7 @@ TEST(Subnet4Test, getServerId) { // Add server identifier. OptionDefinitionPtr option_def = LibDHCP::getOptionDef(DHCP4_OPTION_SPACE, - DHO_DHCP_SERVER_IDENTIFIER); + DHO_DHCP_SERVER_IDENTIFIER); OptionCustomPtr option_server_id(new OptionCustom(*option_def, Option::V4)); option_server_id->writeAddress(IOAddress("1.2.3.4"));