From: Francis Dupont Date: Mon, 15 Sep 2025 13:02:49 +0000 (+0200) Subject: [#4096] Added unit tests X-Git-Tag: Kea-3.1.2~7 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a4a334c0c447a1b240676b759d97ee4121a69d14;p=thirdparty%2Fkea.git [#4096] Added unit tests --- diff --git a/src/bin/dhcp4/tests/dhcp4_srv_unittest.cc b/src/bin/dhcp4/tests/dhcp4_srv_unittest.cc index ff9c900de4..c3d6cdd83b 100644 --- a/src/bin/dhcp4/tests/dhcp4_srv_unittest.cc +++ b/src/bin/dhcp4/tests/dhcp4_srv_unittest.cc @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -4946,6 +4947,77 @@ TEST_F(Dhcpv4SrvTest, statisticsUnknownRcvd) { EXPECT_EQ(1, drop_stat->getInteger().first); } +// Test checks if reception of a packet when the service is disabled +// increases pkt4-service-disabled and pkt4-receive-drop. +TEST_F(Dhcpv4SrvTest, receiveServiceDisabledStat) { + IfaceMgrTestConfig test_config(true); + IfaceMgr::instance().openSockets4(); + + // Apply the configuration we just received. + configure(CONFIGS[0]); + + // Let's get a simple DHCPDISCOVER... + Pkt4Ptr pkt = PktCaptures::captureRelayedDiscover(); + + // And disable the service. + ASSERT_TRUE(srv_->network_state_); + srv_->network_state_->disableService(NetworkState::USER_COMMAND); + + // Simulate that we have received that traffic + srv_->fakeReceive(pkt); + srv_->run(); + + // All expected statistics must be present. + using namespace isc::stats; + StatsMgr& mgr = StatsMgr::instance(); + ObservationPtr pkt4_rcvd = mgr.getObservation("pkt4-received"); + ObservationPtr parse_fail = mgr.getObservation("pkt4-service-disabled"); + ObservationPtr recv_drop = mgr.getObservation("pkt4-receive-drop"); + ASSERT_TRUE(pkt4_rcvd); + ASSERT_TRUE(parse_fail); + ASSERT_TRUE(recv_drop); + + // They also must have expected values. + EXPECT_EQ(1, pkt4_rcvd->getInteger().first); + EXPECT_EQ(1, parse_fail->getInteger().first); + EXPECT_EQ(1, recv_drop->getInteger().first); +} + +// Test checks if reception of a malformed packet increases pkt4-parse-failed +// and pkt4-receive-drop +TEST_F(Dhcpv4SrvTest, receiveParseFailedStat) { + IfaceMgrTestConfig test_config(true); + IfaceMgr::instance().openSockets4(); + + // Apply the configuration we just received. + configure(CONFIGS[0]); + + // Let's get a simple DHCPDISCOVER... + Pkt4Ptr pkt = PktCaptures::captureRelayedDiscover(); + + // And pretend its packet is only 100 bytes long. + pkt->data_.resize(100); + + // Simulate that we have received that traffic + srv_->fakeReceive(pkt); + srv_->run(); + + // All expected statistics must be present. + using namespace isc::stats; + StatsMgr& mgr = StatsMgr::instance(); + ObservationPtr pkt4_rcvd = mgr.getObservation("pkt4-received"); + ObservationPtr parse_fail = mgr.getObservation("pkt4-parse-failed"); + ObservationPtr recv_drop = mgr.getObservation("pkt4-receive-drop"); + ASSERT_TRUE(pkt4_rcvd); + ASSERT_TRUE(parse_fail); + ASSERT_TRUE(recv_drop); + + // They also must have expected values. + EXPECT_EQ(1, pkt4_rcvd->getInteger().first); + EXPECT_EQ(1, parse_fail->getInteger().first); + EXPECT_EQ(1, recv_drop->getInteger().first); +} + // This test verifies that the server is able to handle an empty client-id // in incoming client message. TEST_F(Dhcpv4SrvTest, emptyClientId) { diff --git a/src/bin/dhcp4/tests/dhcp4_test_utils.h b/src/bin/dhcp4/tests/dhcp4_test_utils.h index 2422341369..578f6653b1 100644 --- a/src/bin/dhcp4/tests/dhcp4_test_utils.h +++ b/src/bin/dhcp4/tests/dhcp4_test_utils.h @@ -319,6 +319,7 @@ public: using Dhcpv4Srv::alloc_engine_; using Dhcpv4Srv::server_port_; using Dhcpv4Srv::client_port_; + using Dhcpv4Srv::network_state_; /// @brief Mutex to protect the packet buffers. std::mutex mutex_; diff --git a/src/bin/dhcp6/tests/dhcp6_srv_unittest.cc b/src/bin/dhcp6/tests/dhcp6_srv_unittest.cc index f1d3240e1f..6a0ed1ae7f 100644 --- a/src/bin/dhcp6/tests/dhcp6_srv_unittest.cc +++ b/src/bin/dhcp6/tests/dhcp6_srv_unittest.cc @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -3626,6 +3627,52 @@ TEST_F(Dhcpv6SrvTest, receiveAddrRegInformStat) { testReceiveStats(DHCPV6_ADDR_REG_INFORM, "pkt6-addr-reg-inform-received"); } +// Test checks if reception of a packet when the service is disabled +// increases pkt6-service-disabled and pkt6-receive-drop. +TEST_F(Dhcpv6SrvTest, receiveServiceDisableddStat) { + using namespace isc::stats; + StatsMgr& mgr = StatsMgr::instance(); + + // Let's get a simple SOLICIT... + Pkt6Ptr pkt = PktCaptures::captureSimpleSolicit(); + + // And disable the service. + ASSERT_TRUE(srv_->network_state_); + srv_->network_state_->disableService(NetworkState::USER_COMMAND); + + // Check that the tested statistics is initially set to 0 + ObservationPtr pkt6_rcvd = mgr.getObservation("pkt6-received"); + ObservationPtr parse_fail = mgr.getObservation("pkt6-service-disabled"); + ObservationPtr recv_drop = mgr.getObservation("pkt6-receive-drop"); + ASSERT_TRUE(pkt6_rcvd); + ASSERT_TRUE(parse_fail); + ASSERT_TRUE(recv_drop); + EXPECT_EQ(0, pkt6_rcvd->getInteger().first); + EXPECT_EQ(0, parse_fail->getInteger().first); + EXPECT_EQ(0, recv_drop->getInteger().first); + + // Simulate that we have received that traffic + srv_->fakeReceive(pkt); + + // Server will now process to run its normal loop, but instead of calling + // IfaceMgr::receive6(), it will read all packets from the list set by + // fakeReceive() + srv_->run(); + + // All expected statistics must be present. + pkt6_rcvd = mgr.getObservation("pkt6-received"); + parse_fail = mgr.getObservation("pkt6-service-disabled"); + recv_drop = mgr.getObservation("pkt6-receive-drop"); + ASSERT_TRUE(pkt6_rcvd); + ASSERT_TRUE(parse_fail); + ASSERT_TRUE(recv_drop); + + // They also must have expected values. + EXPECT_EQ(1, pkt6_rcvd->getInteger().first); + EXPECT_EQ(1, parse_fail->getInteger().first); + EXPECT_EQ(1, recv_drop->getInteger().first); +} + // Test checks if reception of a malformed packet increases pkt6-parse-failed // and pkt6-receive-drop TEST_F(Dhcpv6SrvTest, receiveParseFailedStat) { diff --git a/src/bin/dhcp6/tests/dhcp6_test_utils.h b/src/bin/dhcp6/tests/dhcp6_test_utils.h index e739e2af4c..65a45cd741 100644 --- a/src/bin/dhcp6/tests/dhcp6_test_utils.h +++ b/src/bin/dhcp6/tests/dhcp6_test_utils.h @@ -406,6 +406,7 @@ public: using Dhcpv6Srv::initContext; using Dhcpv6Srv::server_port_; using Dhcpv6Srv::client_port_; + using Dhcpv6Srv::network_state_; /// @brief packets we pretend to receive. ///