xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/common/quiche_data_reader_test.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 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/quiche_data_reader.h"
6 
7 #include <cstdint>
8 
9 #include "quiche/common/platform/api/quiche_test.h"
10 #include "quiche/common/quiche_endian.h"
11 
12 namespace quiche {
13 
14 // TODO(b/214573190): Test Endianness::HOST_BYTE_ORDER.
15 // TODO(b/214573190): Test ReadUInt8, ReadUInt24, ReadUInt64, ReadBytesToUInt64,
16 // ReadStringPiece8, ReadStringPiece, ReadTag, etc.
17 
TEST(QuicheDataReaderTest,ReadUInt16)18 TEST(QuicheDataReaderTest, ReadUInt16) {
19   // Data in network byte order.
20   const uint16_t kData[] = {
21       QuicheEndian::HostToNet16(1),
22       QuicheEndian::HostToNet16(1 << 15),
23   };
24 
25   QuicheDataReader reader(reinterpret_cast<const char*>(kData), sizeof(kData));
26   EXPECT_FALSE(reader.IsDoneReading());
27 
28   uint16_t uint16_val;
29   EXPECT_TRUE(reader.ReadUInt16(&uint16_val));
30   EXPECT_FALSE(reader.IsDoneReading());
31   EXPECT_EQ(1, uint16_val);
32 
33   EXPECT_TRUE(reader.ReadUInt16(&uint16_val));
34   EXPECT_TRUE(reader.IsDoneReading());
35   EXPECT_EQ(1 << 15, uint16_val);
36 }
37 
TEST(QuicheDataReaderTest,ReadUInt32)38 TEST(QuicheDataReaderTest, ReadUInt32) {
39   // Data in network byte order.
40   const uint32_t kData[] = {
41       QuicheEndian::HostToNet32(1),
42       QuicheEndian::HostToNet32(0x80000000),
43   };
44 
45   QuicheDataReader reader(reinterpret_cast<const char*>(kData),
46                           ABSL_ARRAYSIZE(kData) * sizeof(uint32_t));
47   EXPECT_FALSE(reader.IsDoneReading());
48 
49   uint32_t uint32_val;
50   EXPECT_TRUE(reader.ReadUInt32(&uint32_val));
51   EXPECT_FALSE(reader.IsDoneReading());
52   EXPECT_EQ(1u, uint32_val);
53 
54   EXPECT_TRUE(reader.ReadUInt32(&uint32_val));
55   EXPECT_TRUE(reader.IsDoneReading());
56   EXPECT_EQ(1u << 31, uint32_val);
57 }
58 
TEST(QuicheDataReaderTest,ReadStringPiece16)59 TEST(QuicheDataReaderTest, ReadStringPiece16) {
60   // Data in network byte order.
61   const char kData[] = {
62       0x00, 0x02,  // uint16_t(2)
63       0x48, 0x69,  // "Hi"
64       0x00, 0x10,  // uint16_t(16)
65       0x54, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67, 0x2c,
66       0x20, 0x31, 0x2c, 0x20, 0x32, 0x2c, 0x20, 0x33,  // "Testing, 1, 2, 3"
67   };
68 
69   QuicheDataReader reader(kData, ABSL_ARRAYSIZE(kData));
70   EXPECT_FALSE(reader.IsDoneReading());
71 
72   absl::string_view stringpiece_val;
73   EXPECT_TRUE(reader.ReadStringPiece16(&stringpiece_val));
74   EXPECT_FALSE(reader.IsDoneReading());
75   EXPECT_EQ(0, stringpiece_val.compare("Hi"));
76 
77   EXPECT_TRUE(reader.ReadStringPiece16(&stringpiece_val));
78   EXPECT_TRUE(reader.IsDoneReading());
79   EXPECT_EQ(0, stringpiece_val.compare("Testing, 1, 2, 3"));
80 }
81 
TEST(QuicheDataReaderTest,ReadUInt16WithBufferTooSmall)82 TEST(QuicheDataReaderTest, ReadUInt16WithBufferTooSmall) {
83   // Data in network byte order.
84   const char kData[] = {
85       0x00,  // part of a uint16_t
86   };
87 
88   QuicheDataReader reader(kData, ABSL_ARRAYSIZE(kData));
89   EXPECT_FALSE(reader.IsDoneReading());
90 
91   uint16_t uint16_val;
92   EXPECT_FALSE(reader.ReadUInt16(&uint16_val));
93 }
94 
TEST(QuicheDataReaderTest,ReadUInt32WithBufferTooSmall)95 TEST(QuicheDataReaderTest, ReadUInt32WithBufferTooSmall) {
96   // Data in network byte order.
97   const char kData[] = {
98       0x00, 0x00, 0x00,  // part of a uint32_t
99   };
100 
101   QuicheDataReader reader(kData, ABSL_ARRAYSIZE(kData));
102   EXPECT_FALSE(reader.IsDoneReading());
103 
104   uint32_t uint32_val;
105   EXPECT_FALSE(reader.ReadUInt32(&uint32_val));
106 
107   // Also make sure that trying to read a uint16_t, which technically could
108   // work, fails immediately due to previously encountered failed read.
109   uint16_t uint16_val;
110   EXPECT_FALSE(reader.ReadUInt16(&uint16_val));
111 }
112 
113 // Tests ReadStringPiece16() with a buffer too small to fit the entire string.
TEST(QuicheDataReaderTest,ReadStringPiece16WithBufferTooSmall)114 TEST(QuicheDataReaderTest, ReadStringPiece16WithBufferTooSmall) {
115   // Data in network byte order.
116   const char kData[] = {
117       0x00, 0x03,  // uint16_t(3)
118       0x48, 0x69,  // "Hi"
119   };
120 
121   QuicheDataReader reader(kData, ABSL_ARRAYSIZE(kData));
122   EXPECT_FALSE(reader.IsDoneReading());
123 
124   absl::string_view stringpiece_val;
125   EXPECT_FALSE(reader.ReadStringPiece16(&stringpiece_val));
126 
127   // Also make sure that trying to read a uint16_t, which technically could
128   // work, fails immediately due to previously encountered failed read.
129   uint16_t uint16_val;
130   EXPECT_FALSE(reader.ReadUInt16(&uint16_val));
131 }
132 
133 // Tests ReadStringPiece16() with a buffer too small even to fit the length.
TEST(QuicheDataReaderTest,ReadStringPiece16WithBufferWayTooSmall)134 TEST(QuicheDataReaderTest, ReadStringPiece16WithBufferWayTooSmall) {
135   // Data in network byte order.
136   const char kData[] = {
137       0x00,  // part of a uint16_t
138   };
139 
140   QuicheDataReader reader(kData, ABSL_ARRAYSIZE(kData));
141   EXPECT_FALSE(reader.IsDoneReading());
142 
143   absl::string_view stringpiece_val;
144   EXPECT_FALSE(reader.ReadStringPiece16(&stringpiece_val));
145 
146   // Also make sure that trying to read a uint16_t, which technically could
147   // work, fails immediately due to previously encountered failed read.
148   uint16_t uint16_val;
149   EXPECT_FALSE(reader.ReadUInt16(&uint16_val));
150 }
151 
TEST(QuicheDataReaderTest,ReadBytes)152 TEST(QuicheDataReaderTest, ReadBytes) {
153   // Data in network byte order.
154   const char kData[] = {
155       0x66, 0x6f, 0x6f,  // "foo"
156       0x48, 0x69,        // "Hi"
157   };
158 
159   QuicheDataReader reader(kData, ABSL_ARRAYSIZE(kData));
160   EXPECT_FALSE(reader.IsDoneReading());
161 
162   char dest1[3] = {};
163   EXPECT_TRUE(reader.ReadBytes(&dest1, ABSL_ARRAYSIZE(dest1)));
164   EXPECT_FALSE(reader.IsDoneReading());
165   EXPECT_EQ("foo", absl::string_view(dest1, ABSL_ARRAYSIZE(dest1)));
166 
167   char dest2[2] = {};
168   EXPECT_TRUE(reader.ReadBytes(&dest2, ABSL_ARRAYSIZE(dest2)));
169   EXPECT_TRUE(reader.IsDoneReading());
170   EXPECT_EQ("Hi", absl::string_view(dest2, ABSL_ARRAYSIZE(dest2)));
171 }
172 
TEST(QuicheDataReaderTest,ReadBytesWithBufferTooSmall)173 TEST(QuicheDataReaderTest, ReadBytesWithBufferTooSmall) {
174   // Data in network byte order.
175   const char kData[] = {
176       0x01,
177   };
178 
179   QuicheDataReader reader(kData, ABSL_ARRAYSIZE(kData));
180   EXPECT_FALSE(reader.IsDoneReading());
181 
182   char dest[ABSL_ARRAYSIZE(kData) + 2] = {};
183   EXPECT_FALSE(reader.ReadBytes(&dest, ABSL_ARRAYSIZE(kData) + 1));
184   EXPECT_STREQ("", dest);
185 }
186 
187 }  // namespace quiche
188