srdusr
aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_ntp.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_ntp.cpp')
-rw-r--r--tests/test_ntp.cpp64
1 files changed, 64 insertions, 0 deletions
diff --git a/tests/test_ntp.cpp b/tests/test_ntp.cpp
new file mode 100644
index 0000000..2c93302
--- /dev/null
+++ b/tests/test_ntp.cpp
@@ -0,0 +1,64 @@
+#include <doctest/doctest.h>
+
+#include <vector>
+
+#include "packeteer/l7/ntp.hpp"
+
+using namespace packeteer::net;
+
+namespace {
+
+std::vector<unsigned char> ntp_client_request() {
+ std::vector<unsigned char> bytes(48, 0);
+ bytes[0] = (4 << 3) | 3; // version 4, mode 3 (client)
+ bytes[1] = 0; // stratum: not applicable on a client request
+ return bytes;
+}
+
+std::vector<unsigned char> ntp_server_reply() {
+ std::vector<unsigned char> bytes(48, 0);
+ bytes[0] = (4 << 3) | 4; // version 4, mode 4 (server)
+ bytes[1] = 2; // stratum 2
+ return bytes;
+}
+
+} // namespace
+
+TEST_CASE("parse_ntp decodes a client request") {
+ auto ntp = parse_ntp(ntp_client_request());
+ REQUIRE(ntp.has_value());
+ CHECK(ntp->version == 4);
+ CHECK(ntp->mode == 3);
+}
+
+TEST_CASE("parse_ntp decodes a server reply with its stratum") {
+ auto ntp = parse_ntp(ntp_server_reply());
+ REQUIRE(ntp.has_value());
+ CHECK(ntp->mode == 4);
+ CHECK(ntp->stratum == 2);
+}
+
+TEST_CASE("parse_ntp rejects a payload shorter than the fixed header") {
+ std::vector<unsigned char> bytes(20, 0);
+ CHECK_FALSE(parse_ntp(bytes).has_value());
+}
+
+TEST_CASE("NtpDissector claims port 123 and includes stratum for client/server modes") {
+ NtpDissector dissector;
+ CHECK(dissector.port() == kNtpPort);
+
+ auto summary = dissector.summarize(ntp_server_reply());
+ REQUIRE(summary.has_value());
+ CHECK(*summary == "NTP v4 server stratum=2");
+}
+
+TEST_CASE("NtpDissector omits stratum for non-client/server modes") {
+ NtpDissector dissector;
+ std::vector<unsigned char> bytes(48, 0);
+ bytes[0] = (4 << 3) | 5; // mode 5: broadcast
+ bytes[1] = 1;
+
+ auto summary = dissector.summarize(bytes);
+ REQUIRE(summary.has_value());
+ CHECK(summary->find("stratum") == std::string::npos);
+}