1 // Copyright (c) 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "quiche/quic/qbone/platform/tcp_packet.h"
6
7 #include <netinet/ip6.h>
8
9 #include <cstdint>
10
11 #include "absl/strings/string_view.h"
12 #include "quiche/quic/platform/api/quic_test.h"
13 #include "quiche/common/quiche_text_utils.h"
14
15 namespace quic {
16 namespace {
17
18 // clang-format off
19 constexpr uint8_t kReferenceTCPSYNPacket[] = {
20 // START IPv6 Header
21 // IPv6 with zero ToS and flow label
22 0x60, 0x00, 0x00, 0x00,
23 // Payload is 40 bytes
24 0x00, 0x28,
25 // Next header is TCP (6)
26 0x06,
27 // Hop limit is 64
28 0x40,
29 // Source address of ::1
30 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
31 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
32 // Destination address of ::1
33 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
34 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
35 // END IPv6 Header
36 // START TCPv6 Header
37 // Source port
38 0xac, 0x1e,
39 // Destination port
40 0x27, 0x0f,
41 // Sequence number
42 0x4b, 0x01, 0xe8, 0x99,
43 // Acknowledgement Sequence number,
44 0x00, 0x00, 0x00, 0x00,
45 // Offset
46 0xa0,
47 // Flags
48 0x02,
49 // Window
50 0xaa, 0xaa,
51 // Checksum
52 0x2e, 0x21,
53 // Urgent
54 0x00, 0x00,
55 // END TCPv6 Header
56 // Options
57 0x02, 0x04, 0xff, 0xc4, 0x04, 0x02, 0x08, 0x0a,
58 0x1b, 0xb8, 0x52, 0xa1, 0x00, 0x00, 0x00, 0x00,
59 0x01, 0x03, 0x03, 0x07,
60 };
61
62 constexpr uint8_t kReferenceTCPRSTPacket[] = {
63 // START IPv6 Header
64 // IPv6 with zero ToS and flow label
65 0x60, 0x00, 0x00, 0x00,
66 // Payload is 20 bytes
67 0x00, 0x14,
68 // Next header is TCP (6)
69 0x06,
70 // Hop limit is 64
71 0x40,
72 // Source address of ::1
73 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
75 // Destination address of ::1
76 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
78 // END IPv6 Header
79 // START TCPv6 Header
80 // Source port
81 0x27, 0x0f,
82 // Destination port
83 0xac, 0x1e,
84 // Sequence number
85 0x00, 0x00, 0x00, 0x00,
86 // Acknowledgement Sequence number,
87 0x4b, 0x01, 0xe8, 0x9a,
88 // Offset
89 0x50,
90 // Flags
91 0x14,
92 // Window
93 0x00, 0x00,
94 // Checksum
95 0xa9, 0x05,
96 // Urgent
97 0x00, 0x00,
98 // END TCPv6 Header
99 };
100 // clang-format on
101
102 } // namespace
103
TEST(TcpPacketTest,CreatedPacketMatchesReference)104 TEST(TcpPacketTest, CreatedPacketMatchesReference) {
105 absl::string_view syn =
106 absl::string_view(reinterpret_cast<const char*>(kReferenceTCPSYNPacket),
107 sizeof(kReferenceTCPSYNPacket));
108 absl::string_view expected_packet =
109 absl::string_view(reinterpret_cast<const char*>(kReferenceTCPRSTPacket),
110 sizeof(kReferenceTCPRSTPacket));
111 CreateTcpResetPacket(syn, [&expected_packet](absl::string_view packet) {
112 QUIC_LOG(INFO) << quiche::QuicheTextUtils::HexDump(packet);
113 ASSERT_EQ(packet, expected_packet);
114 });
115 }
116
117 } // namespace quic
118