]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1836] added extra unittests
authorRazvan Becheriu <razvan@isc.org>
Fri, 25 Jun 2021 11:28:10 +0000 (14:28 +0300)
committerRazvan Becheriu <razvan@isc.org>
Fri, 25 Jun 2021 14:09:04 +0000 (17:09 +0300)
src/bin/dhcp4/tests/dhcp4_srv_unittest.cc
src/bin/dhcp4/tests/dhcp4_test_utils.cc
src/bin/dhcp4/tests/dhcp4_test_utils.h
src/lib/dhcpsrv/tests/client_class_def_unittest.cc
src/lib/dhcpsrv/tests/subnet_unittest.cc

index bcc78fabb53acc1bcb00c110850ebbdc368cce3b..f85f7fcaa35889a182eef69f0987a2227998ce13 100644 (file)
@@ -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));
 
index 12788780762991d75ee44b70f6c0834823ce9446..1ae11ad888f1c037caa566d57a4913f12c0df2a0 100644 (file)
@@ -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<uint8_t> 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,
index 73162c40c2fa847298e88af35f8debd85b1b59b3..6fe8c87c403885e30734613c26bfc8580baf594d 100644 (file)
@@ -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
index 2e820bb04d0d1f1ca0994d210135c7764e932e75..443ce11d98e01f27a5808d0ce2795898a106906e 100644 (file)
@@ -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));
index 1eb4d2965d10403417da83ba649ffbfedeb1870e..be4adb2dfa860661989d23615ef730d90ffd6930 100644 (file)
@@ -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"));