1 // Copyright 2023 The Chromium Authors
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/test_tools/test_ip_packets.h"
6
7 #include <string>
8
9 #include "absl/strings/string_view.h"
10 #include "quiche/quic/platform/api/quic_socket_address.h"
11 #include "quiche/common/platform/api/quiche_test.h"
12 #include "quiche/common/quiche_ip_address.h"
13
14 namespace quic::test {
15 namespace {
16
TEST(TestIpPacketsTest,CreateIpv4Packet)17 TEST(TestIpPacketsTest, CreateIpv4Packet) {
18 quiche::QuicheIpAddress source_ip;
19 ASSERT_TRUE(source_ip.FromString("192.0.2.45"));
20 ASSERT_TRUE(source_ip.IsIPv4());
21 QuicSocketAddress source_address{source_ip, /*port=*/54131};
22
23 quiche::QuicheIpAddress destination_ip;
24 ASSERT_TRUE(destination_ip.FromString("192.0.2.67"));
25 ASSERT_TRUE(destination_ip.IsIPv4());
26 QuicSocketAddress destination_address(destination_ip, /*port=*/57542);
27
28 std::string packet =
29 CreateIpPacket(source_ip, destination_ip,
30 CreateUdpPacket(source_address, destination_address,
31 /*payload=*/"foo"),
32 IpPacketPayloadType::kUdp);
33
34 constexpr static char kExpected[] =
35 "\x45" // Version: 4, Header length: 5 words
36 "\x00" // DSCP: 0, ECN: 0
37 "\x00\x1F" // Total length: 31
38 "\x00\x00" // Id: 0
39 "\x00\x00" // Flags: 0, Fragment offset: 0
40 "\x40" // TTL: 64 hops
41 "\x11" // Protocol: 17 (UDP)
42 "\x00\x00" // Header checksum: 0
43 "\xC0\x00\x02\x2D" // Source IP
44 "\xC0\x00\x02\x43" // Destination IP
45 "\xD3\x73" // Source port
46 "\xE0\xC6" // Destination port
47 "\x00\x0B" // Length: 11
48 "\xF1\xBC" // Checksum: 0xF1BC
49 "foo"; // Payload
50 EXPECT_EQ(absl::string_view(packet),
51 absl::string_view(kExpected, sizeof(kExpected) - 1));
52 }
53
TEST(TestIpPacketsTest,CreateIpv6Packet)54 TEST(TestIpPacketsTest, CreateIpv6Packet) {
55 quiche::QuicheIpAddress source_ip;
56 ASSERT_TRUE(source_ip.FromString("2001:db8::45"));
57 ASSERT_TRUE(source_ip.IsIPv6());
58 QuicSocketAddress source_address{source_ip, /*port=*/51941};
59
60 quiche::QuicheIpAddress destination_ip;
61 ASSERT_TRUE(destination_ip.FromString("2001:db8::67"));
62 ASSERT_TRUE(destination_ip.IsIPv6());
63 QuicSocketAddress destination_address(destination_ip, /*port=*/55341);
64
65 std::string packet =
66 CreateIpPacket(source_ip, destination_ip,
67 CreateUdpPacket(source_address, destination_address,
68 /*payload=*/"foo"),
69 IpPacketPayloadType::kUdp);
70
71 constexpr static char kExpected[] =
72 "\x60\x00\x00\x00" // Version: 6, Traffic class: 0, Flow label: 0
73 "\x00\x0b" // Payload length: 11
74 "\x11" // Next header: 17 (UDP)
75 "\x40" // Hop limit: 64
76 // Source IP
77 "\x20\x01\x0D\xB8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x45"
78 // Destination IP
79 "\x20\x01\x0D\xB8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x67"
80 "\xCA\xE5" // Source port
81 "\xD8\x2D" // Destination port
82 "\x00\x0B" // Length: 11
83 "\x2B\x37" // Checksum: 0x2B37
84 "foo"; // Payload
85 EXPECT_EQ(absl::string_view(packet),
86 absl::string_view(kExpected, sizeof(kExpected) - 1));
87 }
88
89 } // namespace
90 } // namespace quic::test
91