srdusr
aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_ntp.cpp
blob: 2c933020da173fecb862b8150dcec1bb56e017cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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);
}