summaryrefslogtreecommitdiffstats
path: root/src/lib/dhcp/tests/pkt4_unittest.cc
diff options
context:
space:
mode:
authorThomas Markwalder <tmark@isc.org>2024-02-01 21:27:14 +0100
committerRazvan Becheriu <razvan@isc.org>2024-02-15 07:13:44 +0100
commita4703a85dd91ed86014358c5b2d81a258a682b45 (patch)
tree11770a92476c9f8249cf49f5a1bf6d0cecc957e1 /src/lib/dhcp/tests/pkt4_unittest.cc
parent[#3209] Further review comments (diff)
downloadkea-a4703a85dd91ed86014358c5b2d81a258a682b45.tar.xz
kea-a4703a85dd91ed86014358c5b2d81a258a682b45.zip
[#3230] Initial commit
Adds dhcp::PktEvent, modifies packet filters and tests src/lib/dhcp/pkt.* Add PktEvent class, add stack and methods to Pkt src/lib/dhcp/pkt_filter_bpf.cc PktFilterBPF::receive() - fetch timestamp from bpfhdr, add SOCKET_RECEIVED and BUFFER_READ events PktFilterBPF::send() - add RESPONSE_SENT event src/lib/dhcp/pkt_filter_inet.cc PktFilterInet::openSocket() - enable SO_TIMESTAMP socket option PktFilterInet::receive() - fetch SO_TIMESTAMP via CMSG, add SOCKET_RECEIVED and BUFFER_READ events PktFilterInet::send() - add RESPONSE_SENT event src/lib/dhcp/pkt_filter_inet6.cc PktFilterInet6::openSocket() - enable SO_TIMESTAMP socket option PktFilterInet6::receive() - fetch SO_TIMESTAMP via CMSG, add SOCKET_RECEIVED and BUFFER_READ events PktFilterInet::send() - add RESPONSE_SENT event src/lib/dhcp/pkt_filter_lpf.cc PktFilterLPF::openSocket() - enable SO_TIMESTAMP socket option PktFilterLPF::receive() - added conditonal compilation version based on WITH_CMSG macro that fetches SO_TIMESTAMP and add SOCKET_RECEIVED and BUFFER_READ events PktFilterLPF::send() - add RESPONSE_SENT event src/lib/dhcp/tests/pkt4_unittest.cc TEST_F(Pkt4Test, PktEvents) - new test src/lib/dhcp/tests/pkt6_unittest.cc TEST_F(Pkt6Test, PktEvents) - new test src/lib/dhcp/tests/pkt_filter6_test_utils.cc src/lib/dhcp/tests/pkt_filter6_test_utils.h src/lib/dhcp/tests/pkt_filter_inet6_unittest.cc src/lib/dhcp/tests/pkt_filter_inet_unittest.cc src/lib/dhcp/tests/pkt_filter_test_utils.cc src/lib/dhcp/tests/pkt_filter_test_utils.h Updated tests to check PktEvent stack contents src/lib/dhcp/tests/pkt_filter_bpf_unittest.cc src/lib/dhcp/tests/pkt_filter_lpf_unittest.cc Updated tests to check PktEvent stack contents, Changed DISABLED_ tests to use SKIP_IF(notRoot)
Diffstat (limited to 'src/lib/dhcp/tests/pkt4_unittest.cc')
-rw-r--r--src/lib/dhcp/tests/pkt4_unittest.cc67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/lib/dhcp/tests/pkt4_unittest.cc b/src/lib/dhcp/tests/pkt4_unittest.cc
index db07f2bd58..c9f120eeef 100644
--- a/src/lib/dhcp/tests/pkt4_unittest.cc
+++ b/src/lib/dhcp/tests/pkt4_unittest.cc
@@ -1525,4 +1525,71 @@ TEST_F(Pkt4Test, getHWAddrLabel) {
EXPECT_EQ ("hwaddr=02:04:06:08:0a:0c", pkt.getHWAddrLabel());
}
+// Exercises packet event stack and helper functions.
+TEST_F(Pkt4Test, PktEvents) {
+ // Get current time.
+ auto start_time = PktEvent::now();
+
+ // Verify that a set time is not equal to an EMPTY_TIME.
+ ASSERT_NE(start_time, PktEvent::EMPTY_TIME());
+
+ // Create a test packet.
+ scoped_ptr<Pkt4> pkt(new Pkt4(DHCPOFFER, 1234));
+
+ // Upon creation, the events table should be empty.
+ ASSERT_TRUE(pkt->getPktEvents().empty());
+
+ // An non-existant event should return an empty time.
+ auto event_time = pkt->getPktEventTime(PktEvent::BUFFER_READ);
+ ASSERT_EQ(event_time, PktEvent::EMPTY_TIME());
+
+ // Sleep for 200 microseconds to put some distance between now and start_time.
+ usleep(200);
+
+ // Should be able to add an event, defaulting the event time to current time.
+ pkt->addPktEvent(PktEvent::BUFFER_READ);
+ event_time = pkt->getPktEventTime(PktEvent::BUFFER_READ);
+ ASSERT_GT(event_time, start_time);
+
+ // Should be able to overwrite an existing event's time.
+ pkt->setPktEvent(PktEvent::BUFFER_READ, start_time);
+ event_time = pkt->getPktEventTime(PktEvent::BUFFER_READ);
+ ASSERT_EQ(event_time, start_time);
+
+ // Should be able to add an event with an explicit time.
+ pkt->addPktEvent(PktEvent::RESPONSE_SENT, start_time);
+ event_time = pkt->getPktEventTime(PktEvent::RESPONSE_SENT);
+ ASSERT_EQ(event_time, start_time);
+
+ // Should be able to fetch the list of events.
+ const auto& events = pkt->getPktEvents();
+ ASSERT_FALSE(events.empty());
+ auto event = events.begin();
+ ASSERT_EQ((*event).label_, PktEvent::BUFFER_READ);
+ ++event;
+ ASSERT_EQ((*event).label_, PktEvent::RESPONSE_SENT);
+
+ // Discard the event stack contents.
+ pkt->clearPktEvents();
+ ASSERT_TRUE(pkt->getPktEvents().empty());
+
+ // Verify dumpPktEvent terse output. Also serves to
+ // verify adding events using struct timeval.
+ struct timeval log_time = {1706802676, 100};
+ struct timeval log_time_plus = {1706802676, 250};
+ pkt->addPktEvent("first-event", log_time);
+ pkt->addPktEvent("second-event", log_time_plus);
+ std::string log = pkt->dumpPktEvents();
+ EXPECT_EQ(log, "2024-Feb-01 15:51:16.000100 : first-event, 2024-Feb-01 15:51:16.000250 : second-event");
+
+ // Verify dumpPktEvent verbose output.
+ log = pkt->dumpPktEvents(true);
+ EXPECT_EQ(log,
+ "Event log: \n"
+ "2024-Feb-01 15:51:16.000100 : first-event\n"
+ "2024-Feb-01 15:51:16.000250 : second-event elapsed: 00:00:00.000150\n"
+ "total elapsed: 00:00:00.000150");
+}
+
+
} // end of anonymous namespace