OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "net/spdy/spdy_frame_reader.h" | |
6 | |
7 #include <algorithm> | |
8 #include <iostream> | |
9 #include <memory> | |
10 | |
11 #include "base/sys_byteorder.h" | |
12 #include "testing/platform_test.h" | |
13 | |
14 namespace net { | |
15 | |
16 TEST(SpdyFrameReaderTest, ReadUInt16) { | |
17 // Frame data in network byte order. | |
18 const uint16_t kFrameData[] = { | |
19 base::HostToNet16(1), base::HostToNet16(1 << 15), | |
20 }; | |
21 | |
22 SpdyFrameReader frame_reader(reinterpret_cast<const char*>(kFrameData), | |
23 sizeof(kFrameData)); | |
24 EXPECT_FALSE(frame_reader.IsDoneReading()); | |
25 | |
26 uint16_t uint16_val; | |
27 EXPECT_TRUE(frame_reader.ReadUInt16(&uint16_val)); | |
28 EXPECT_FALSE(frame_reader.IsDoneReading()); | |
29 EXPECT_EQ(1, uint16_val); | |
30 | |
31 EXPECT_TRUE(frame_reader.ReadUInt16(&uint16_val)); | |
32 EXPECT_TRUE(frame_reader.IsDoneReading()); | |
33 EXPECT_EQ(1<<15, uint16_val); | |
34 } | |
35 | |
36 TEST(SpdyFrameReaderTest, ReadUInt32) { | |
37 // Frame data in network byte order. | |
38 const uint32_t kFrameData[] = { | |
39 base::HostToNet32(1), base::HostToNet32(0x80000000), | |
40 }; | |
41 | |
42 SpdyFrameReader frame_reader(reinterpret_cast<const char*>(kFrameData), | |
43 arraysize(kFrameData) * sizeof(uint32_t)); | |
44 EXPECT_FALSE(frame_reader.IsDoneReading()); | |
45 | |
46 uint32_t uint32_val; | |
47 EXPECT_TRUE(frame_reader.ReadUInt32(&uint32_val)); | |
48 EXPECT_FALSE(frame_reader.IsDoneReading()); | |
49 EXPECT_EQ(1u, uint32_val); | |
50 | |
51 EXPECT_TRUE(frame_reader.ReadUInt32(&uint32_val)); | |
52 EXPECT_TRUE(frame_reader.IsDoneReading()); | |
53 EXPECT_EQ(1u<<31, uint32_val); | |
54 } | |
55 | |
56 TEST(SpdyFrameReaderTest, ReadStringPiece16) { | |
57 // Frame data in network byte order. | |
58 const char kFrameData[] = { | |
59 0x00, 0x02, // uint16_t(2) | |
60 0x48, 0x69, // "Hi" | |
61 0x00, 0x10, // uint16_t(16) | |
62 0x54, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67, 0x2c, | |
63 0x20, 0x31, 0x2c, 0x20, 0x32, 0x2c, 0x20, 0x33, // "Testing, 1, 2, 3" | |
64 }; | |
65 | |
66 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | |
67 EXPECT_FALSE(frame_reader.IsDoneReading()); | |
68 | |
69 SpdyStringPiece stringpiece_val; | |
70 EXPECT_TRUE(frame_reader.ReadStringPiece16(&stringpiece_val)); | |
71 EXPECT_FALSE(frame_reader.IsDoneReading()); | |
72 EXPECT_EQ(0, stringpiece_val.compare("Hi")); | |
73 | |
74 EXPECT_TRUE(frame_reader.ReadStringPiece16(&stringpiece_val)); | |
75 EXPECT_TRUE(frame_reader.IsDoneReading()); | |
76 EXPECT_EQ(0, stringpiece_val.compare("Testing, 1, 2, 3")); | |
77 } | |
78 | |
79 TEST(SpdyFrameReaderTest, ReadStringPiece32) { | |
80 // Frame data in network byte order. | |
81 const char kFrameData[] = { | |
82 0x00, 0x00, 0x00, 0x03, // uint32_t(3) | |
83 0x66, 0x6f, 0x6f, // "foo" | |
84 0x00, 0x00, 0x00, 0x10, // uint32_t(16) | |
85 0x54, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67, 0x2c, | |
86 0x20, 0x34, 0x2c, 0x20, 0x35, 0x2c, 0x20, 0x36, // "Testing, 4, 5, 6" | |
87 }; | |
88 | |
89 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | |
90 EXPECT_FALSE(frame_reader.IsDoneReading()); | |
91 | |
92 SpdyStringPiece stringpiece_val; | |
93 EXPECT_TRUE(frame_reader.ReadStringPiece32(&stringpiece_val)); | |
94 EXPECT_FALSE(frame_reader.IsDoneReading()); | |
95 EXPECT_EQ(0, stringpiece_val.compare("foo")); | |
96 | |
97 EXPECT_TRUE(frame_reader.ReadStringPiece32(&stringpiece_val)); | |
98 EXPECT_TRUE(frame_reader.IsDoneReading()); | |
99 EXPECT_EQ(0, stringpiece_val.compare("Testing, 4, 5, 6")); | |
100 } | |
101 | |
102 TEST(SpdyFrameReaderTest, ReadUInt16WithBufferTooSmall) { | |
103 // Frame data in network byte order. | |
104 const char kFrameData[] = { | |
105 0x00, // part of a uint16_t | |
106 }; | |
107 | |
108 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | |
109 EXPECT_FALSE(frame_reader.IsDoneReading()); | |
110 | |
111 uint16_t uint16_val; | |
112 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); | |
113 } | |
114 | |
115 TEST(SpdyFrameReaderTest, ReadUInt32WithBufferTooSmall) { | |
116 // Frame data in network byte order. | |
117 const char kFrameData[] = { | |
118 0x00, 0x00, 0x00, // part of a uint32_t | |
119 }; | |
120 | |
121 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | |
122 EXPECT_FALSE(frame_reader.IsDoneReading()); | |
123 | |
124 uint32_t uint32_val; | |
125 EXPECT_FALSE(frame_reader.ReadUInt32(&uint32_val)); | |
126 | |
127 // Also make sure that trying to read a uint16_t, which technically could | |
128 // work, | |
129 // fails immediately due to previously encountered failed read. | |
130 uint16_t uint16_val; | |
131 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); | |
132 } | |
133 | |
134 // Tests ReadStringPiece16() with a buffer too small to fit the entire string. | |
135 TEST(SpdyFrameReaderTest, ReadStringPiece16WithBufferTooSmall) { | |
136 // Frame data in network byte order. | |
137 const char kFrameData[] = { | |
138 0x00, 0x03, // uint16_t(3) | |
139 0x48, 0x69, // "Hi" | |
140 }; | |
141 | |
142 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | |
143 EXPECT_FALSE(frame_reader.IsDoneReading()); | |
144 | |
145 SpdyStringPiece stringpiece_val; | |
146 EXPECT_FALSE(frame_reader.ReadStringPiece16(&stringpiece_val)); | |
147 | |
148 // Also make sure that trying to read a uint16_t, which technically could | |
149 // work, | |
150 // fails immediately due to previously encountered failed read. | |
151 uint16_t uint16_val; | |
152 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); | |
153 } | |
154 | |
155 // Tests ReadStringPiece16() with a buffer too small even to fit the length. | |
156 TEST(SpdyFrameReaderTest, ReadStringPiece16WithBufferWayTooSmall) { | |
157 // Frame data in network byte order. | |
158 const char kFrameData[] = { | |
159 0x00, // part of a uint16_t | |
160 }; | |
161 | |
162 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | |
163 EXPECT_FALSE(frame_reader.IsDoneReading()); | |
164 | |
165 SpdyStringPiece stringpiece_val; | |
166 EXPECT_FALSE(frame_reader.ReadStringPiece16(&stringpiece_val)); | |
167 | |
168 // Also make sure that trying to read a uint16_t, which technically could | |
169 // work, | |
170 // fails immediately due to previously encountered failed read. | |
171 uint16_t uint16_val; | |
172 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); | |
173 } | |
174 | |
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_t(3) | |
180 0x48, 0x69, // "Hi" | |
181 }; | |
182 | |
183 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | |
184 EXPECT_FALSE(frame_reader.IsDoneReading()); | |
185 | |
186 SpdyStringPiece stringpiece_val; | |
187 EXPECT_FALSE(frame_reader.ReadStringPiece32(&stringpiece_val)); | |
188 | |
189 // Also make sure that trying to read a uint16_t, which technically could | |
190 // work, | |
191 // fails immediately due to previously encountered failed read. | |
192 uint16_t uint16_val; | |
193 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); | |
194 } | |
195 | |
196 // Tests ReadStringPiece32() with a buffer too small even to fit the length. | |
197 TEST(SpdyFrameReaderTest, ReadStringPiece32WithBufferWayTooSmall) { | |
198 // Frame data in network byte order. | |
199 const char kFrameData[] = { | |
200 0x00, 0x00, 0x00, // part of a uint32_t | |
201 }; | |
202 | |
203 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | |
204 EXPECT_FALSE(frame_reader.IsDoneReading()); | |
205 | |
206 SpdyStringPiece stringpiece_val; | |
207 EXPECT_FALSE(frame_reader.ReadStringPiece32(&stringpiece_val)); | |
208 | |
209 // Also make sure that trying to read a uint16_t, which technically could | |
210 // work, | |
211 // fails immediately due to previously encountered failed read. | |
212 uint16_t uint16_val; | |
213 EXPECT_FALSE(frame_reader.ReadUInt16(&uint16_val)); | |
214 } | |
215 | |
216 TEST(SpdyFrameReaderTest, ReadBytes) { | |
217 // Frame data in network byte order. | |
218 const char kFrameData[] = { | |
219 0x66, 0x6f, 0x6f, // "foo" | |
220 0x48, 0x69, // "Hi" | |
221 }; | |
222 | |
223 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | |
224 EXPECT_FALSE(frame_reader.IsDoneReading()); | |
225 | |
226 char dest1[3] = {}; | |
227 EXPECT_TRUE(frame_reader.ReadBytes(&dest1, arraysize(dest1))); | |
228 EXPECT_FALSE(frame_reader.IsDoneReading()); | |
229 EXPECT_EQ("foo", SpdyStringPiece(dest1, arraysize(dest1))); | |
230 | |
231 char dest2[2] = {}; | |
232 EXPECT_TRUE(frame_reader.ReadBytes(&dest2, arraysize(dest2))); | |
233 EXPECT_TRUE(frame_reader.IsDoneReading()); | |
234 EXPECT_EQ("Hi", SpdyStringPiece(dest2, arraysize(dest2))); | |
235 } | |
236 | |
237 TEST(SpdyFrameReaderTest, ReadBytesWithBufferTooSmall) { | |
238 // Frame data in network byte order. | |
239 const char kFrameData[] = { | |
240 0x01, | |
241 }; | |
242 | |
243 SpdyFrameReader frame_reader(kFrameData, arraysize(kFrameData)); | |
244 EXPECT_FALSE(frame_reader.IsDoneReading()); | |
245 | |
246 char dest[arraysize(kFrameData) + 2] = {}; | |
247 EXPECT_FALSE(frame_reader.ReadBytes(&dest, arraysize(kFrameData) + 1)); | |
248 EXPECT_STREQ("", dest); | |
249 } | |
250 | |
251 } // namespace net | |
OLD | NEW |