srdusr
aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_quic.cpp
blob: 2366176e8bc63e469b36ec7ae32e165c47e7049d (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <doctest/doctest.h>

#include <vector>

#include "packeteer/l7/quic.hpp"

using namespace packeteer::net;

namespace {

std::vector<unsigned char> long_header(std::uint8_t type_bits, std::uint32_t version,
                                        std::vector<unsigned char> dcid,
                                        std::vector<unsigned char> scid) {
    std::vector<unsigned char> bytes;
    bytes.push_back(static_cast<unsigned char>(0xC0 | (type_bits << 4)));  // long form, fixed bit
    bytes.push_back(static_cast<unsigned char>(version >> 24));
    bytes.push_back(static_cast<unsigned char>(version >> 16));
    bytes.push_back(static_cast<unsigned char>(version >> 8));
    bytes.push_back(static_cast<unsigned char>(version));
    bytes.push_back(static_cast<unsigned char>(dcid.size()));
    bytes.insert(bytes.end(), dcid.begin(), dcid.end());
    bytes.push_back(static_cast<unsigned char>(scid.size()));
    bytes.insert(bytes.end(), scid.begin(), scid.end());
    return bytes;
}

}  // namespace

TEST_CASE("parse_quic decodes a long-header Initial packet's version and connection IDs") {
    auto bytes = long_header(0x00, 0x00000001, {0xAA, 0xBB, 0xCC, 0xDD}, {0x11, 0x22});
    auto pkt = parse_quic(bytes);
    REQUIRE(pkt.has_value());
    CHECK(pkt->is_long_header);
    REQUIRE(pkt->long_header.has_value());
    CHECK(pkt->long_header->type == QuicLongPacketType::kInitial);
    CHECK(pkt->long_header->version == 0x00000001);
    CHECK(pkt->long_header->dcid == std::vector<unsigned char>{0xAA, 0xBB, 0xCC, 0xDD});
    CHECK(pkt->long_header->scid == std::vector<unsigned char>{0x11, 0x22});
}

TEST_CASE("parse_quic decodes each long-packet type from its type bits") {
    CHECK(parse_quic(long_header(0x00, 1, {}, {}))->long_header->type ==
          QuicLongPacketType::kInitial);
    CHECK(parse_quic(long_header(0x01, 1, {}, {}))->long_header->type ==
          QuicLongPacketType::kZeroRtt);
    CHECK(parse_quic(long_header(0x02, 1, {}, {}))->long_header->type ==
          QuicLongPacketType::kHandshake);
    CHECK(parse_quic(long_header(0x03, 1, {}, {}))->long_header->type ==
          QuicLongPacketType::kRetry);
}

TEST_CASE("parse_quic treats version 0 as Version Negotiation regardless of type bits") {
    auto pkt = parse_quic(long_header(0x02, 0x00000000, {0xAA}, {}));
    REQUIRE(pkt.has_value());
    CHECK(pkt->long_header->type == QuicLongPacketType::kVersionNegotiation);
}

TEST_CASE("parse_quic recognizes a short-header packet without decoding past the first byte") {
    std::vector<unsigned char> bytes = {0x40, 0xAA, 0xBB, 0xCC};  // short form, fixed bit set
    auto pkt = parse_quic(bytes);
    REQUIRE(pkt.has_value());
    CHECK_FALSE(pkt->is_long_header);
    CHECK_FALSE(pkt->long_header.has_value());
}

TEST_CASE("parse_quic rejects a packet with the Fixed Bit clear") {
    std::vector<unsigned char> bytes = {0x00, 0xAA, 0xBB, 0xCC};
    CHECK_FALSE(parse_quic(bytes).has_value());
}

TEST_CASE("parse_quic rejects a long-header packet truncated before the version field") {
    std::vector<unsigned char> bytes = {0xC0, 0x00, 0x00};
    CHECK_FALSE(parse_quic(bytes).has_value());
}

TEST_CASE("parse_quic rejects a long-header packet whose DCID length exceeds the buffer") {
    std::vector<unsigned char> bytes = {0xC0, 0x00, 0x00, 0x00, 0x01, 20};  // claims 20-byte DCID
    CHECK_FALSE(parse_quic(bytes).has_value());
}

TEST_CASE("QuicDissector claims port 443 and formats an Initial packet") {
    QuicDissector dissector;
    CHECK(dissector.port() == kQuicPort);

    auto bytes = long_header(0x00, 0x00000001, {0xAA, 0xBB}, {});
    auto summary = dissector.summarize(bytes);
    REQUIRE(summary.has_value());
    CHECK(*summary == "QUIC Initial v=0x00000001 dcid=aabb");
}

TEST_CASE("QuicDissector formats a short-header packet distinctly, without a fake dcid") {
    QuicDissector dissector;
    std::vector<unsigned char> bytes = {0x40, 0xAA, 0xBB, 0xCC};
    auto summary = dissector.summarize(bytes);
    REQUIRE(summary.has_value());
    CHECK(*summary == "QUIC 1-RTT (short header)");
}