]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#4142] dhcpsrv and v4 UTs
authorThomas Markwalder <tmark@isc.org>
Thu, 2 Oct 2025 19:25:49 +0000 (15:25 -0400)
committerAndrei Pavel <andrei@isc.org>
Mon, 27 Oct 2025 15:19:08 +0000 (17:19 +0200)
modified:   src/bin/dhcp4/tests/fqdn_unittest.cc
modified:   src/lib/dhcpsrv/tests/d2_client_unittest.cc

src/bin/dhcp4/tests/fqdn_unittest.cc
src/lib/dhcpsrv/tests/d2_client_unittest.cc

index a5d3e4c21aae2906a1727880f33d0a970b739c15..4bb5dc18d42d1a2da885d7269229ce3894814a60 100644 (file)
@@ -2253,7 +2253,7 @@ TEST_F(NameDhcpv4SrvTest, sanitizeHostDefault) {
         },
         {
             "qualified host name with nuls",
-            std::string("four-ok-host\000.other.org",23),
+            std::string("four-ok-host\000.other.org", 23),
             "four-ok-host.other.org"
         }
     };
@@ -3203,4 +3203,62 @@ TEST_F(NameDhcpv4SrvTest, poolDdnsParametersTest) {
     }
 }
 
+// Verifies that when the FQDN option is scrubbed empty it is logged
+// and ignored.
+TEST_F(NameDhcpv4SrvTest, hostnameScrubbedEmpty) {
+    Dhcp4Client client(srv_, Dhcp4Client::SELECTING);
+
+    // Configure DHCP server.
+    configure(CONFIGS[2], *client.getServer());
+
+    // Set the hostname option.
+    ASSERT_NO_THROW(client.includeHostname("___"));
+
+    // Send the DHCPDISCOVER and make sure that the server responded.
+    ASSERT_NO_THROW(client.doDiscover());
+    auto resp = client.getContext().response_;
+    ASSERT_TRUE(resp);
+    ASSERT_EQ(DHCPOFFER, static_cast<int>(resp->getType()));
+
+    // Should have logged that it was scrubbed empty.
+    std::string log = "DHCP4_CLIENT_HOSTNAME_SCRUBBED_EMPTY"
+                      " [hwtype=1 67:c6:69:73:51:ff], cid=[no info], tid=0x0:"
+                      " sanitiziing client's Hostname option '___'"
+                      " yielded an empty string";
+    EXPECT_EQ(1, countFile(log));
+
+    // Hostname should not be in the response.
+    ASSERT_FALSE(resp->getOption(DHO_HOST_NAME));
+}
+
+// Verifies that when the FQDN option is scrubbed empty it is logged
+// and ignored.
+TEST_F(NameDhcpv4SrvTest, fqdnScrubbedEmpty) {
+    Dhcp4Client client(srv_, Dhcp4Client::SELECTING);
+
+    // Configure DHCP server.
+    configure(CONFIGS[2], *client.getServer());
+
+    // Include the Client FQDN option.
+    ASSERT_NO_THROW(client.includeFQDN(Option4ClientFqdn::FLAG_S | Option4ClientFqdn::FLAG_E,
+                                       "___", Option4ClientFqdn::PARTIAL));
+
+    // Send the DHCPDISCOVER and make sure that the server responded.
+    ASSERT_NO_THROW(client.doDiscover());
+    auto resp = client.getContext().response_;
+    ASSERT_TRUE(resp);
+    ASSERT_EQ(DHCPOFFER, static_cast<int>(resp->getType()));
+
+    // Should have logged that it was scrubbed empty.
+    std::string log = "DHCP4_CLIENT_FQDN_SCRUBBED_EMPTY"
+                      " [hwtype=1 67:c6:69:73:51:ff], cid=[no info], tid=0x0:"
+                      " sanitiziing client's FQDN option '___'"
+                      " yielded an empty string";
+    EXPECT_EQ(1, countFile(log));
+
+    // Hostname should not be in the response.
+    ASSERT_FALSE(resp->getOption(DHO_FQDN));
+}
+
+
 } // end of anonymous namespace
index 68ad2189d6c0d3bf73c023b4227809dfa87ce6a0..520acbbb88e6fc365ccc019d6f0b1f2fa62e2c3f 100644 (file)
@@ -9,6 +9,7 @@
 #include <dhcp/option6_client_fqdn.h>
 #include <dhcpsrv/d2_client_mgr.h>
 #include <testutils/test_to_element.h>
+#include <testutils/gtest_utils.h>
 #include <exceptions/exceptions.h>
 #include <util/str.h>
 
@@ -1257,4 +1258,43 @@ TEST_F(D2ClientMgrParamsTest, sanitizeFqdnV6) {
     }
 }
 
+/// @brief Tests adjustDomainName template method with Option4ClientFqdn
+/// when sanitizing scrubs input name empty.
+TEST_F(D2ClientMgrParamsTest, adjustDomainNameV4ScrubbedEmpty) {
+    D2ClientMgr mgr;
+
+    // Create enabled configuration
+    subnet_->setDdnsSendUpdates(false);
+    subnet_->setDdnsQualifyingSuffix("suffix.com");
+    subnet_->setHostnameCharSet("[^A-Za-z0-9.-]");
+    subnet_->setHostnameCharReplacement("");
+
+    Option4ClientFqdn request(0, Option4ClientFqdn::RCODE_CLIENT(),
+                              "___", Option4ClientFqdn::FULL);
+
+    Option4ClientFqdn response(request);
+    ASSERT_THROW_MSG(mgr.adjustDomainName<Option4ClientFqdn>(request, response, *ddns_params_),
+                     FQDNScrubbedEmpty, "___.");
+}
+
+/// @brief Tests adjustDomainName template method with Option4ClientFqdn
+/// when sanitizing scrubs input name empty.
+TEST_F(D2ClientMgrParamsTest, adjustDomainNameV6ScrubbedEmpty) {
+    D2ClientMgr mgr;
+
+    // Create enabled configuration
+    subnet_->setDdnsSendUpdates(false);
+    subnet_->setDdnsQualifyingSuffix("suffix.com");
+    subnet_->setHostnameCharSet("[^A-Za-z0-9.-]");
+    subnet_->setHostnameCharReplacement("");
+
+    Option6ClientFqdn request(0, "___", Option6ClientFqdn::FULL);
+
+    Option6ClientFqdn response(request);
+    ASSERT_THROW_MSG(mgr.adjustDomainName<Option6ClientFqdn>(request, response, *ddns_params_),
+                     FQDNScrubbedEmpty, "___.");
+}
+
+
+
 } // end of anonymous namespace