xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/common/wire_serialization_test.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2022 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/common/wire_serialization.h"
6 
7 #include <limits>
8 #include <optional>
9 #include <string>
10 
11 #include "absl/status/status.h"
12 #include "absl/status/statusor.h"
13 #include "absl/strings/escaping.h"
14 #include "absl/strings/str_cat.h"
15 #include "absl/strings/string_view.h"
16 #include "quiche/common/platform/api/quiche_expect_bug.h"
17 #include "quiche/common/platform/api/quiche_test.h"
18 #include "quiche/common/quiche_buffer_allocator.h"
19 #include "quiche/common/quiche_endian.h"
20 #include "quiche/common/quiche_status_utils.h"
21 #include "quiche/common/simple_buffer_allocator.h"
22 #include "quiche/common/test_tools/quiche_test_utils.h"
23 
24 namespace quiche::test {
25 namespace {
26 
27 using ::testing::ElementsAre;
28 
29 constexpr uint64_t kInvalidVarInt = std::numeric_limits<uint64_t>::max();
30 
31 template <typename... Ts>
SerializeIntoSimpleBuffer(Ts...data)32 absl::StatusOr<quiche::QuicheBuffer> SerializeIntoSimpleBuffer(Ts... data) {
33   return SerializeIntoBuffer(quiche::SimpleBufferAllocator::Get(), data...);
34 }
35 
36 template <typename... Ts>
ExpectEncoding(const std::string & description,absl::string_view expected,Ts...data)37 void ExpectEncoding(const std::string& description, absl::string_view expected,
38                     Ts... data) {
39   absl::StatusOr<quiche::QuicheBuffer> actual =
40       SerializeIntoSimpleBuffer(data...);
41   QUICHE_ASSERT_OK(actual);
42   quiche::test::CompareCharArraysWithHexError(description, actual->data(),
43                                               actual->size(), expected.data(),
44                                               expected.size());
45 }
46 
47 template <typename... Ts>
ExpectEncodingHex(const std::string & description,absl::string_view expected_hex,Ts...data)48 void ExpectEncodingHex(const std::string& description,
49                        absl::string_view expected_hex, Ts... data) {
50   std::string expected;
51   ASSERT_TRUE(absl::HexStringToBytes(expected_hex, &expected));
52   ExpectEncoding(description, expected, data...);
53 }
54 
TEST(SerializationTest,SerializeStrings)55 TEST(SerializationTest, SerializeStrings) {
56   absl::StatusOr<quiche::QuicheBuffer> one_string =
57       SerializeIntoSimpleBuffer(WireBytes("test"));
58   QUICHE_ASSERT_OK(one_string);
59   EXPECT_EQ(one_string->AsStringView(), "test");
60 
61   absl::StatusOr<quiche::QuicheBuffer> two_strings =
62       SerializeIntoSimpleBuffer(WireBytes("Hello"), WireBytes("World"));
63   QUICHE_ASSERT_OK(two_strings);
64   EXPECT_EQ(two_strings->AsStringView(), "HelloWorld");
65 }
66 
TEST(SerializationTest,SerializeIntegers)67 TEST(SerializationTest, SerializeIntegers) {
68   ExpectEncodingHex("one uint8_t value", "42", WireUint8(0x42));
69   ExpectEncodingHex("two uint8_t values", "ab01", WireUint8(0xab),
70                     WireUint8(0x01));
71   ExpectEncodingHex("one uint16_t value", "1234", WireUint16(0x1234));
72   ExpectEncodingHex("one uint32_t value", "12345678", WireUint32(0x12345678));
73   ExpectEncodingHex("one uint64_t value", "123456789abcdef0",
74                     WireUint64(UINT64_C(0x123456789abcdef0)));
75   ExpectEncodingHex("mix of values", "aabbcc000000dd", WireUint8(0xaa),
76                     WireUint16(0xbbcc), WireUint32(0xdd));
77 }
78 
TEST(SerializationTest,SerializeLittleEndian)79 TEST(SerializationTest, SerializeLittleEndian) {
80   char buffer[4];
81   QuicheDataWriter writer(sizeof(buffer), buffer,
82                           quiche::Endianness::HOST_BYTE_ORDER);
83   QUICHE_ASSERT_OK(
84       SerializeIntoWriter(writer, WireUint16(0x1234), WireUint16(0xabcd)));
85   absl::string_view actual(writer.data(), writer.length());
86   std::string expected;
87   ASSERT_TRUE(absl::HexStringToBytes("3412cdab", &expected));
88   EXPECT_EQ(actual, expected);
89 }
90 
TEST(SerializationTest,SerializeVarInt62)91 TEST(SerializationTest, SerializeVarInt62) {
92   // Test cases from RFC 9000, Appendix A.1
93   ExpectEncodingHex("1-byte varint", "25", WireVarInt62(37));
94   ExpectEncodingHex("2-byte varint", "7bbd", WireVarInt62(15293));
95   ExpectEncodingHex("4-byte varint", "9d7f3e7d", WireVarInt62(494878333));
96   ExpectEncodingHex("8-byte varint", "c2197c5eff14e88c",
97                     WireVarInt62(UINT64_C(151288809941952652)));
98 }
99 
TEST(SerializationTest,SerializeStringWithVarInt62Length)100 TEST(SerializationTest, SerializeStringWithVarInt62Length) {
101   ExpectEncodingHex("short string", "0474657374",
102                     WireStringWithVarInt62Length("test"));
103   const std::string long_string(15293, 'a');
104   ExpectEncoding("long string", absl::StrCat("\x7b\xbd", long_string),
105                  WireStringWithVarInt62Length(long_string));
106   ExpectEncodingHex("empty string", "00", WireStringWithVarInt62Length(""));
107 }
108 
TEST(SerializationTest,SerializeOptionalValues)109 TEST(SerializationTest, SerializeOptionalValues) {
110   std::optional<uint8_t> has_no_value;
111   std::optional<uint8_t> has_value = 0x42;
112   ExpectEncodingHex("optional without value", "00", WireUint8(0),
113                     WireOptional<WireUint8>(has_no_value));
114   ExpectEncodingHex("optional with value", "0142", WireUint8(1),
115                     WireOptional<WireUint8>(has_value));
116   ExpectEncodingHex("empty data", "", WireOptional<WireUint8>(has_no_value));
117 
118   std::optional<std::string> has_no_string;
119   std::optional<std::string> has_string = "\x42";
120   ExpectEncodingHex("optional no string", "",
121                     WireOptional<WireStringWithVarInt62Length>(has_no_string));
122   ExpectEncodingHex("optional string", "0142",
123                     WireOptional<WireStringWithVarInt62Length>(has_string));
124 }
125 
126 enum class TestEnum {
127   kValue1 = 0x17,
128   kValue2 = 0x19,
129 };
130 
TEST(SerializationTest,SerializeEnumValue)131 TEST(SerializationTest, SerializeEnumValue) {
132   ExpectEncodingHex("enum value", "17", WireVarInt62(TestEnum::kValue1));
133 }
134 
TEST(SerializationTest,SerializeLotsOfValues)135 TEST(SerializationTest, SerializeLotsOfValues) {
136   ExpectEncodingHex("ten values", "00010203040506070809", WireUint8(0),
137                     WireUint8(1), WireUint8(2), WireUint8(3), WireUint8(4),
138                     WireUint8(5), WireUint8(6), WireUint8(7), WireUint8(8),
139                     WireUint8(9));
140 }
141 
TEST(SerializationTest,FailDueToLackOfSpace)142 TEST(SerializationTest, FailDueToLackOfSpace) {
143   char buffer[4];
144   QuicheDataWriter writer(sizeof(buffer), buffer);
145   QUICHE_EXPECT_OK(SerializeIntoWriter(writer, WireUint32(0)));
146   ASSERT_EQ(writer.remaining(), 0u);
147   EXPECT_THAT(
148       SerializeIntoWriter(writer, WireUint32(0)),
149       StatusIs(absl::StatusCode::kInternal, "Failed to serialize field #0"));
150   EXPECT_THAT(
151       SerializeIntoWriter(writer, WireStringWithVarInt62Length("test")),
152       StatusIs(
153           absl::StatusCode::kInternal,
154           "Failed to serialize the length prefix while serializing field #0"));
155 }
156 
TEST(SerializationTest,FailDueToInvalidValue)157 TEST(SerializationTest, FailDueToInvalidValue) {
158   EXPECT_QUICHE_BUG(
159       ExpectEncoding("invalid varint", "", WireVarInt62(kInvalidVarInt)),
160       "too big for VarInt62");
161 }
162 
TEST(SerializationTest,InvalidValueCausesPartialWrite)163 TEST(SerializationTest, InvalidValueCausesPartialWrite) {
164   char buffer[3] = {'\0'};
165   QuicheDataWriter writer(sizeof(buffer), buffer);
166   QUICHE_EXPECT_OK(SerializeIntoWriter(writer, WireBytes("a")));
167   EXPECT_THAT(
168       SerializeIntoWriter(writer, WireBytes("b"),
169                           WireBytes("A considerably long string, writing which "
170                                     "will most likely cause ASAN to crash"),
171                           WireBytes("c")),
172       StatusIs(absl::StatusCode::kInternal, "Failed to serialize field #1"));
173   EXPECT_THAT(buffer, ElementsAre('a', 'b', '\0'));
174 
175   QUICHE_EXPECT_OK(SerializeIntoWriter(writer, WireBytes("z")));
176   EXPECT_EQ(buffer[2], 'z');
177 }
178 
TEST(SerializationTest,SerializeVector)179 TEST(SerializationTest, SerializeVector) {
180   std::vector<absl::string_view> strs = {"foo", "test", "bar"};
181   absl::StatusOr<quiche::QuicheBuffer> serialized =
182       SerializeIntoSimpleBuffer(WireSpan<WireBytes>(absl::MakeSpan(strs)));
183   QUICHE_ASSERT_OK(serialized);
184   EXPECT_EQ(serialized->AsStringView(), "footestbar");
185 }
186 
187 struct AwesomeStruct {
188   uint64_t awesome_number;
189   std::string awesome_text;
190 };
191 
192 class WireAwesomeStruct {
193  public:
194   using DataType = AwesomeStruct;
195 
WireAwesomeStruct(const AwesomeStruct & awesome)196   WireAwesomeStruct(const AwesomeStruct& awesome) : awesome_(awesome) {}
197 
GetLengthOnWire()198   size_t GetLengthOnWire() {
199     return quiche::ComputeLengthOnWire(WireUint16(awesome_.awesome_number),
200                                        WireBytes(awesome_.awesome_text));
201   }
SerializeIntoWriter(QuicheDataWriter & writer)202   absl::Status SerializeIntoWriter(QuicheDataWriter& writer) {
203     return AppendToStatus(::quiche::SerializeIntoWriter(
204                               writer, WireUint16(awesome_.awesome_number),
205                               WireBytes(awesome_.awesome_text)),
206                           " while serializing AwesomeStruct");
207   }
208 
209  private:
210   const AwesomeStruct& awesome_;
211 };
212 
TEST(SerializationTest,CustomStruct)213 TEST(SerializationTest, CustomStruct) {
214   AwesomeStruct awesome;
215   awesome.awesome_number = 0xabcd;
216   awesome.awesome_text = "test";
217   ExpectEncodingHex("struct", "abcd74657374", WireAwesomeStruct(awesome));
218 }
219 
TEST(SerializationTest,CustomStructSpan)220 TEST(SerializationTest, CustomStructSpan) {
221   std::array<AwesomeStruct, 2> awesome;
222   awesome[0].awesome_number = 0xabcd;
223   awesome[0].awesome_text = "test";
224   awesome[1].awesome_number = 0x1234;
225   awesome[1].awesome_text = std::string(3, '\0');
226   ExpectEncodingHex("struct", "abcd746573741234000000",
227                     WireSpan<WireAwesomeStruct>(absl::MakeSpan(awesome)));
228 }
229 
230 class WireFormatterThatWritesTooLittle {
231  public:
232   using DataType = absl::string_view;
233 
WireFormatterThatWritesTooLittle(absl::string_view s)234   explicit WireFormatterThatWritesTooLittle(absl::string_view s) : s_(s) {}
235 
GetLengthOnWire() const236   size_t GetLengthOnWire() const { return s_.size(); }
SerializeIntoWriter(QuicheDataWriter & writer)237   bool SerializeIntoWriter(QuicheDataWriter& writer) {
238     return writer.WriteStringPiece(s_.substr(0, s_.size() - 1));
239   }
240 
241  private:
242   absl::string_view s_;
243 };
244 
TEST(SerializationTest,CustomStructWritesTooLittle)245 TEST(SerializationTest, CustomStructWritesTooLittle) {
246   absl::Status status;
247 #if defined(NDEBUG)
248   constexpr absl::string_view kStr = "\xaa\xbb\xcc\xdd";
249   status = SerializeIntoSimpleBuffer(WireFormatterThatWritesTooLittle(kStr))
250                .status();
251   EXPECT_THAT(status, StatusIs(absl::StatusCode::kInternal,
252                                ::testing::HasSubstr("Excess 1 bytes")));
253 #elif GTEST_HAS_DEATH_TEST
254   constexpr absl::string_view kStr = "\xaa\xbb\xcc\xdd";
255   EXPECT_QUICHE_DEBUG_DEATH(
256       status = SerializeIntoSimpleBuffer(WireFormatterThatWritesTooLittle(kStr))
257                    .status(),
258       "while serializing field #0");
259   EXPECT_THAT(status, StatusIs(absl::StatusCode::kOk));
260 #endif
261 }
262 
TEST(SerializationTest,Empty)263 TEST(SerializationTest, Empty) { ExpectEncodingHex("nothing", ""); }
264 
265 }  // namespace
266 }  // namespace quiche::test
267