1 // Copyright (c) 2012 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.
8 #include "base/memory/scoped_ptr.h"
9 #include "base/sys_byteorder.h"
10 #include "net/spdy/spdy_frame_reader.h"
11 #include "testing/platform_test.h"
15 TEST(SpdyFrameReaderTest
, ReadUInt16
) {
16 // Frame data in network byte order.
17 const uint16 kFrameData
[] = {
18 htons(1), htons(1<<15),
21 SpdyFrameReader
frame_reader(reinterpret_cast<const char *>(kFrameData
),
22 arraysize(kFrameData
) * sizeof(uint16
));
23 EXPECT_FALSE(frame_reader
.IsDoneReading());
26 EXPECT_TRUE(frame_reader
.ReadUInt16(&uint16_val
));
27 EXPECT_FALSE(frame_reader
.IsDoneReading());
28 EXPECT_EQ(1, uint16_val
);
30 EXPECT_TRUE(frame_reader
.ReadUInt16(&uint16_val
));
31 EXPECT_TRUE(frame_reader
.IsDoneReading());
32 EXPECT_EQ(1<<15, uint16_val
);
35 TEST(SpdyFrameReaderTest
, ReadUInt32
) {
36 // Frame data in network byte order.
37 const uint32 kFrameData
[] = {
38 htonl(1), htonl(1<<31),
41 SpdyFrameReader
frame_reader(reinterpret_cast<const char *>(kFrameData
),
42 arraysize(kFrameData
) * sizeof(uint32
));
43 EXPECT_FALSE(frame_reader
.IsDoneReading());
46 EXPECT_TRUE(frame_reader
.ReadUInt32(&uint32_val
));
47 EXPECT_FALSE(frame_reader
.IsDoneReading());
48 EXPECT_EQ(1u, uint32_val
);
50 EXPECT_TRUE(frame_reader
.ReadUInt32(&uint32_val
));
51 EXPECT_TRUE(frame_reader
.IsDoneReading());
52 EXPECT_EQ(1u<<31, uint32_val
);
55 TEST(SpdyFrameReaderTest
, ReadStringPiece16
) {
56 // Frame data in network byte order.
57 const char kFrameData
[] = {
58 0x00, 0x02, // uint16(2)
60 0x00, 0x10, // uint16(16)
61 0x54, 0x65, 0x73, 0x74,
62 0x69, 0x6e, 0x67, 0x2c,
63 0x20, 0x31, 0x2c, 0x20,
64 0x32, 0x2c, 0x20, 0x33, // "Testing, 1, 2, 3"
67 SpdyFrameReader
frame_reader(kFrameData
, arraysize(kFrameData
));
68 EXPECT_FALSE(frame_reader
.IsDoneReading());
70 base::StringPiece stringpiece_val
;
71 EXPECT_TRUE(frame_reader
.ReadStringPiece16(&stringpiece_val
));
72 EXPECT_FALSE(frame_reader
.IsDoneReading());
73 EXPECT_EQ(0, stringpiece_val
.compare("Hi"));
75 EXPECT_TRUE(frame_reader
.ReadStringPiece16(&stringpiece_val
));
76 EXPECT_TRUE(frame_reader
.IsDoneReading());
77 EXPECT_EQ(0, stringpiece_val
.compare("Testing, 1, 2, 3"));
80 TEST(SpdyFrameReaderTest
, ReadStringPiece32
) {
81 // Frame data in network byte order.
82 const char kFrameData
[] = {
83 0x00, 0x00, 0x00, 0x03, // uint32(3)
84 0x66, 0x6f, 0x6f, // "foo"
85 0x00, 0x00, 0x00, 0x10, // uint32(16)
86 0x54, 0x65, 0x73, 0x74,
87 0x69, 0x6e, 0x67, 0x2c,
88 0x20, 0x34, 0x2c, 0x20,
89 0x35, 0x2c, 0x20, 0x36, // "Testing, 4, 5, 6"
92 SpdyFrameReader
frame_reader(kFrameData
, arraysize(kFrameData
));
93 EXPECT_FALSE(frame_reader
.IsDoneReading());
95 base::StringPiece stringpiece_val
;
96 EXPECT_TRUE(frame_reader
.ReadStringPiece32(&stringpiece_val
));
97 EXPECT_FALSE(frame_reader
.IsDoneReading());
98 EXPECT_EQ(0, stringpiece_val
.compare("foo"));
100 EXPECT_TRUE(frame_reader
.ReadStringPiece32(&stringpiece_val
));
101 EXPECT_TRUE(frame_reader
.IsDoneReading());
102 EXPECT_EQ(0, stringpiece_val
.compare("Testing, 4, 5, 6"));
105 TEST(SpdyFrameReaderTest
, ReadUInt16WithBufferTooSmall
) {
106 // Frame data in network byte order.
107 const char kFrameData
[] = {
108 0x00, // part of a uint16
111 SpdyFrameReader
frame_reader(kFrameData
, arraysize(kFrameData
));
112 EXPECT_FALSE(frame_reader
.IsDoneReading());
115 EXPECT_FALSE(frame_reader
.ReadUInt16(&uint16_val
));
118 TEST(SpdyFrameReaderTest
, ReadUInt32WithBufferTooSmall
) {
119 // Frame data in network byte order.
120 const char kFrameData
[] = {
121 0x00, 0x00, 0x00, // part of a uint32
124 SpdyFrameReader
frame_reader(kFrameData
, arraysize(kFrameData
));
125 EXPECT_FALSE(frame_reader
.IsDoneReading());
128 EXPECT_FALSE(frame_reader
.ReadUInt32(&uint32_val
));
130 // Also make sure that trying to read a uint16, which technically could work,
131 // fails immediately due to previously encountered failed read.
133 EXPECT_FALSE(frame_reader
.ReadUInt16(&uint16_val
));
136 // Tests ReadStringPiece16() with a buffer too small to fit the entire string.
137 TEST(SpdyFrameReaderTest
, ReadStringPiece16WithBufferTooSmall
) {
138 // Frame data in network byte order.
139 const char kFrameData
[] = {
140 0x00, 0x03, // uint16(3)
144 SpdyFrameReader
frame_reader(kFrameData
, arraysize(kFrameData
));
145 EXPECT_FALSE(frame_reader
.IsDoneReading());
147 base::StringPiece stringpiece_val
;
148 EXPECT_FALSE(frame_reader
.ReadStringPiece16(&stringpiece_val
));
150 // Also make sure that trying to read a uint16, which technically could work,
151 // fails immediately due to previously encountered failed read.
153 EXPECT_FALSE(frame_reader
.ReadUInt16(&uint16_val
));
156 // Tests ReadStringPiece16() with a buffer too small even to fit the length.
157 TEST(SpdyFrameReaderTest
, ReadStringPiece16WithBufferWayTooSmall
) {
158 // Frame data in network byte order.
159 const char kFrameData
[] = {
160 0x00, // part of a uint16
163 SpdyFrameReader
frame_reader(kFrameData
, arraysize(kFrameData
));
164 EXPECT_FALSE(frame_reader
.IsDoneReading());
166 base::StringPiece stringpiece_val
;
167 EXPECT_FALSE(frame_reader
.ReadStringPiece16(&stringpiece_val
));
169 // Also make sure that trying to read a uint16, which technically could work,
170 // fails immediately due to previously encountered failed read.
172 EXPECT_FALSE(frame_reader
.ReadUInt16(&uint16_val
));
175 // Tests ReadStringPiece32() with a buffer too small to fit the entire string.
176 TEST(SpdyFrameReaderTest
, ReadStringPiece32WithBufferTooSmall
) {
177 // Frame data in network byte order.
178 const char kFrameData
[] = {
179 0x00, 0x00, 0x00, 0x03, // uint32(3)
183 SpdyFrameReader
frame_reader(kFrameData
, arraysize(kFrameData
));
184 EXPECT_FALSE(frame_reader
.IsDoneReading());
186 base::StringPiece stringpiece_val
;
187 EXPECT_FALSE(frame_reader
.ReadStringPiece32(&stringpiece_val
));
189 // Also make sure that trying to read a uint16, which technically could work,
190 // fails immediately due to previously encountered failed read.
192 EXPECT_FALSE(frame_reader
.ReadUInt16(&uint16_val
));
195 // Tests ReadStringPiece32() with a buffer too small even to fit the length.
196 TEST(SpdyFrameReaderTest
, ReadStringPiece32WithBufferWayTooSmall
) {
197 // Frame data in network byte order.
198 const char kFrameData
[] = {
199 0x00, 0x00, 0x00, // part of a uint32
202 SpdyFrameReader
frame_reader(kFrameData
, arraysize(kFrameData
));
203 EXPECT_FALSE(frame_reader
.IsDoneReading());
205 base::StringPiece stringpiece_val
;
206 EXPECT_FALSE(frame_reader
.ReadStringPiece32(&stringpiece_val
));
208 // Also make sure that trying to read a uint16, which technically could work,
209 // fails immediately due to previously encountered failed read.
211 EXPECT_FALSE(frame_reader
.ReadUInt16(&uint16_val
));
214 TEST(SpdyFrameReaderTest
, ReadBytes
) {
215 // Frame data in network byte order.
216 const char kFrameData
[] = {
217 0x66, 0x6f, 0x6f, // "foo"
221 SpdyFrameReader
frame_reader(kFrameData
, arraysize(kFrameData
));
222 EXPECT_FALSE(frame_reader
.IsDoneReading());
225 EXPECT_TRUE(frame_reader
.ReadBytes(&dest1
, arraysize(dest1
)));
226 EXPECT_FALSE(frame_reader
.IsDoneReading());
227 EXPECT_EQ("foo", base::StringPiece(dest1
, arraysize(dest1
)));
230 EXPECT_TRUE(frame_reader
.ReadBytes(&dest2
, arraysize(dest2
)));
231 EXPECT_TRUE(frame_reader
.IsDoneReading());
232 EXPECT_EQ("Hi", base::StringPiece(dest2
, arraysize(dest2
)));
235 TEST(SpdyFrameReaderTest
, ReadBytesWithBufferTooSmall
) {
236 // Frame data in network byte order.
237 const char kFrameData
[] = {
241 SpdyFrameReader
frame_reader(kFrameData
, arraysize(kFrameData
));
242 EXPECT_FALSE(frame_reader
.IsDoneReading());
244 char dest
[arraysize(kFrameData
) + 2] = {};
245 EXPECT_FALSE(frame_reader
.ReadBytes(&dest
, arraysize(kFrameData
) + 1));
246 EXPECT_STREQ("", dest
);