OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 #include "net/quic/core/spdy_utils.h" | 4 #include "net/quic/core/spdy_utils.h" |
5 | 5 |
6 #include "base/macros.h" | 6 #include "base/macros.h" |
7 #include "base/strings/string_piece.h" | 7 #include "net/quic/platform/api/quic_string_piece.h" |
8 #include "net/quic/platform/api/quic_text_utils.h" | 8 #include "net/quic/platform/api/quic_text_utils.h" |
9 #include "net/test/gtest_util.h" | 9 #include "net/test/gtest_util.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 using base::StringPiece; | |
13 using std::string; | 12 using std::string; |
14 using testing::UnorderedElementsAre; | 13 using testing::UnorderedElementsAre; |
15 using testing::Pair; | 14 using testing::Pair; |
16 | 15 |
17 namespace net { | 16 namespace net { |
18 namespace test { | 17 namespace test { |
19 | 18 |
20 TEST(SpdyUtilsTest, SerializeAndParseHeaders) { | 19 TEST(SpdyUtilsTest, SerializeAndParseHeaders) { |
21 // Creates a SpdyHeaderBlock with some key->value pairs, serializes it, then | 20 // Creates a SpdyHeaderBlock with some key->value pairs, serializes it, then |
22 // parses the serialized output and verifies that the end result is the same | 21 // parses the serialized output and verifies that the end result is the same |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 {"empty-joined", ""}, | 177 {"empty-joined", ""}, |
179 {"empty-joined", ""}, | 178 {"empty-joined", ""}, |
180 | 179 |
181 // Non-continguous cookie crumb. | 180 // Non-continguous cookie crumb. |
182 {"cookie", " fin!"}}); | 181 {"cookie", " fin!"}}); |
183 | 182 |
184 int64_t content_length = -1; | 183 int64_t content_length = -1; |
185 SpdyHeaderBlock block; | 184 SpdyHeaderBlock block; |
186 ASSERT_TRUE( | 185 ASSERT_TRUE( |
187 SpdyUtils::CopyAndValidateHeaders(*headers, &content_length, &block)); | 186 SpdyUtils::CopyAndValidateHeaders(*headers, &content_length, &block)); |
188 EXPECT_THAT(block, UnorderedElementsAre( | 187 EXPECT_THAT(block, |
189 Pair("cookie", " part 1; part 2 ; part3; fin!"), | 188 UnorderedElementsAre( |
190 Pair("passed-through", StringPiece("foo\0baz", 7)), | 189 Pair("cookie", " part 1; part 2 ; part3; fin!"), |
191 Pair("joined", StringPiece("value 1\0value 2", 15)), | 190 Pair("passed-through", QuicStringPiece("foo\0baz", 7)), |
192 Pair("empty", ""), | 191 Pair("joined", QuicStringPiece("value 1\0value 2", 15)), |
193 Pair("empty-joined", StringPiece("\0foo\0\0", 6)))); | 192 Pair("empty", ""), |
| 193 Pair("empty-joined", QuicStringPiece("\0foo\0\0", 6)))); |
194 EXPECT_EQ(-1, content_length); | 194 EXPECT_EQ(-1, content_length); |
195 } | 195 } |
196 | 196 |
197 TEST(SpdyUtilsTest, CopyAndValidateHeadersEmptyName) { | 197 TEST(SpdyUtilsTest, CopyAndValidateHeadersEmptyName) { |
198 auto headers = FromList({{"foo", "foovalue"}, {"", "barvalue"}, {"baz", ""}}); | 198 auto headers = FromList({{"foo", "foovalue"}, {"", "barvalue"}, {"baz", ""}}); |
199 int64_t content_length = -1; | 199 int64_t content_length = -1; |
200 SpdyHeaderBlock block; | 200 SpdyHeaderBlock block; |
201 ASSERT_FALSE( | 201 ASSERT_FALSE( |
202 SpdyUtils::CopyAndValidateHeaders(*headers, &content_length, &block)); | 202 SpdyUtils::CopyAndValidateHeaders(*headers, &content_length, &block)); |
203 } | 203 } |
(...skipping 12 matching lines...) Expand all Loading... |
216 {"foo", "foovalue"}, | 216 {"foo", "foovalue"}, |
217 {"content-length", "9"}, | 217 {"content-length", "9"}, |
218 {"bar", "barvalue"}, | 218 {"bar", "barvalue"}, |
219 {"baz", ""}}); | 219 {"baz", ""}}); |
220 int64_t content_length = -1; | 220 int64_t content_length = -1; |
221 SpdyHeaderBlock block; | 221 SpdyHeaderBlock block; |
222 ASSERT_TRUE( | 222 ASSERT_TRUE( |
223 SpdyUtils::CopyAndValidateHeaders(*headers, &content_length, &block)); | 223 SpdyUtils::CopyAndValidateHeaders(*headers, &content_length, &block)); |
224 EXPECT_THAT(block, UnorderedElementsAre( | 224 EXPECT_THAT(block, UnorderedElementsAre( |
225 Pair("foo", "foovalue"), Pair("bar", "barvalue"), | 225 Pair("foo", "foovalue"), Pair("bar", "barvalue"), |
226 Pair("content-length", StringPiece("9" | 226 Pair("content-length", QuicStringPiece("9" |
227 "\0" | 227 "\0" |
228 "9", | 228 "9", |
229 3)), | 229 3)), |
230 Pair("baz", ""))); | 230 Pair("baz", ""))); |
231 EXPECT_EQ(9, content_length); | 231 EXPECT_EQ(9, content_length); |
232 } | 232 } |
233 | 233 |
234 TEST(SpdyUtilsTest, CopyAndValidateHeadersInconsistentContentLengths) { | 234 TEST(SpdyUtilsTest, CopyAndValidateHeadersInconsistentContentLengths) { |
235 auto headers = FromList({{"content-length", "9"}, | 235 auto headers = FromList({{"content-length", "9"}, |
236 {"foo", "foovalue"}, | 236 {"foo", "foovalue"}, |
237 {"content-length", "8"}, | 237 {"content-length", "8"}, |
238 {"bar", "barvalue"}, | 238 {"bar", "barvalue"}, |
239 {"baz", ""}}); | 239 {"baz", ""}}); |
240 int64_t content_length = -1; | 240 int64_t content_length = -1; |
241 SpdyHeaderBlock block; | 241 SpdyHeaderBlock block; |
242 ASSERT_FALSE( | 242 ASSERT_FALSE( |
243 SpdyUtils::CopyAndValidateHeaders(*headers, &content_length, &block)); | 243 SpdyUtils::CopyAndValidateHeaders(*headers, &content_length, &block)); |
244 } | 244 } |
245 | 245 |
246 TEST(SpdyUtilsTest, CopyAndValidateHeadersLargeContentLength) { | 246 TEST(SpdyUtilsTest, CopyAndValidateHeadersLargeContentLength) { |
247 auto headers = FromList({{"content-length", "9000000000"}, | 247 auto headers = FromList({{"content-length", "9000000000"}, |
248 {"foo", "foovalue"}, | 248 {"foo", "foovalue"}, |
249 {"bar", "barvalue"}, | 249 {"bar", "barvalue"}, |
250 {"baz", ""}}); | 250 {"baz", ""}}); |
251 int64_t content_length = -1; | 251 int64_t content_length = -1; |
252 SpdyHeaderBlock block; | 252 SpdyHeaderBlock block; |
253 ASSERT_TRUE( | 253 ASSERT_TRUE( |
254 SpdyUtils::CopyAndValidateHeaders(*headers, &content_length, &block)); | 254 SpdyUtils::CopyAndValidateHeaders(*headers, &content_length, &block)); |
255 EXPECT_THAT(block, UnorderedElementsAre( | 255 EXPECT_THAT(block, UnorderedElementsAre( |
256 Pair("foo", "foovalue"), Pair("bar", "barvalue"), | 256 Pair("foo", "foovalue"), Pair("bar", "barvalue"), |
257 Pair("content-length", StringPiece("9000000000")), | 257 Pair("content-length", QuicStringPiece("9000000000")), |
258 Pair("baz", ""))); | 258 Pair("baz", ""))); |
259 EXPECT_EQ(9000000000, content_length); | 259 EXPECT_EQ(9000000000, content_length); |
260 } | 260 } |
261 | 261 |
262 TEST(SpdyUtilsTest, CopyAndValidateHeadersMultipleValues) { | 262 TEST(SpdyUtilsTest, CopyAndValidateHeadersMultipleValues) { |
263 auto headers = FromList({{"foo", "foovalue"}, | 263 auto headers = FromList({{"foo", "foovalue"}, |
264 {"bar", "barvalue"}, | 264 {"bar", "barvalue"}, |
265 {"baz", ""}, | 265 {"baz", ""}, |
266 {"foo", "boo"}, | 266 {"foo", "boo"}, |
267 {"baz", "buzz"}}); | 267 {"baz", "buzz"}}); |
268 int64_t content_length = -1; | 268 int64_t content_length = -1; |
269 SpdyHeaderBlock block; | 269 SpdyHeaderBlock block; |
270 ASSERT_TRUE( | 270 ASSERT_TRUE( |
271 SpdyUtils::CopyAndValidateHeaders(*headers, &content_length, &block)); | 271 SpdyUtils::CopyAndValidateHeaders(*headers, &content_length, &block)); |
272 EXPECT_THAT( | 272 EXPECT_THAT(block, UnorderedElementsAre( |
273 block, UnorderedElementsAre(Pair("foo", StringPiece("foovalue\0boo", 12)), | 273 Pair("foo", QuicStringPiece("foovalue\0boo", 12)), |
274 Pair("bar", "barvalue"), | 274 Pair("bar", "barvalue"), |
275 Pair("baz", StringPiece("\0buzz", 5)))); | 275 Pair("baz", QuicStringPiece("\0buzz", 5)))); |
276 EXPECT_EQ(-1, content_length); | 276 EXPECT_EQ(-1, content_length); |
277 } | 277 } |
278 | 278 |
279 TEST(SpdyUtilsTest, CopyAndValidateHeadersMoreThanTwoValues) { | 279 TEST(SpdyUtilsTest, CopyAndValidateHeadersMoreThanTwoValues) { |
280 auto headers = FromList({{"set-cookie", "value1"}, | 280 auto headers = FromList({{"set-cookie", "value1"}, |
281 {"set-cookie", "value2"}, | 281 {"set-cookie", "value2"}, |
282 {"set-cookie", "value3"}}); | 282 {"set-cookie", "value3"}}); |
283 int64_t content_length = -1; | 283 int64_t content_length = -1; |
284 SpdyHeaderBlock block; | 284 SpdyHeaderBlock block; |
285 ASSERT_TRUE( | 285 ASSERT_TRUE( |
286 SpdyUtils::CopyAndValidateHeaders(*headers, &content_length, &block)); | 286 SpdyUtils::CopyAndValidateHeaders(*headers, &content_length, &block)); |
287 EXPECT_THAT(block, | 287 EXPECT_THAT( |
288 UnorderedElementsAre(Pair( | 288 block, UnorderedElementsAre(Pair( |
289 "set-cookie", StringPiece("value1\0value2\0value3", 20)))); | 289 "set-cookie", QuicStringPiece("value1\0value2\0value3", 20)))); |
290 EXPECT_EQ(-1, content_length); | 290 EXPECT_EQ(-1, content_length); |
291 } | 291 } |
292 | 292 |
293 TEST(SpdyUtilsTest, CopyAndValidateHeadersCookie) { | 293 TEST(SpdyUtilsTest, CopyAndValidateHeadersCookie) { |
294 auto headers = FromList({{"foo", "foovalue"}, | 294 auto headers = FromList({{"foo", "foovalue"}, |
295 {"bar", "barvalue"}, | 295 {"bar", "barvalue"}, |
296 {"cookie", "value1"}, | 296 {"cookie", "value1"}, |
297 {"baz", ""}}); | 297 {"baz", ""}}); |
298 int64_t content_length = -1; | 298 int64_t content_length = -1; |
299 SpdyHeaderBlock block; | 299 SpdyHeaderBlock block; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 TEST(SpdyUtilsTest, PopulateHeaderBlockFromUrlFails) { | 378 TEST(SpdyUtilsTest, PopulateHeaderBlockFromUrlFails) { |
379 SpdyHeaderBlock headers; | 379 SpdyHeaderBlock headers; |
380 EXPECT_FALSE(SpdyUtils::PopulateHeaderBlockFromUrl("/", &headers)); | 380 EXPECT_FALSE(SpdyUtils::PopulateHeaderBlockFromUrl("/", &headers)); |
381 EXPECT_FALSE(SpdyUtils::PopulateHeaderBlockFromUrl("/index.html", &headers)); | 381 EXPECT_FALSE(SpdyUtils::PopulateHeaderBlockFromUrl("/index.html", &headers)); |
382 EXPECT_FALSE( | 382 EXPECT_FALSE( |
383 SpdyUtils::PopulateHeaderBlockFromUrl("www.google.com/", &headers)); | 383 SpdyUtils::PopulateHeaderBlockFromUrl("www.google.com/", &headers)); |
384 } | 384 } |
385 | 385 |
386 } // namespace test | 386 } // namespace test |
387 } // namespace net | 387 } // namespace net |
OLD | NEW |