| 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 "net/quic/platform/api/quic_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 std::string; | 12 using std::string; |
| 13 using testing::UnorderedElementsAre; | 13 using testing::UnorderedElementsAre; |
| 14 using testing::Pair; | 14 using testing::Pair; |
| 15 | 15 |
| 16 namespace net { | 16 namespace net { |
| 17 namespace test { | 17 namespace test { |
| 18 | 18 |
| 19 TEST(SpdyUtilsTest, SerializeAndParseHeaders) { | |
| 20 // Creates a SpdyHeaderBlock with some key->value pairs, serializes it, then | |
| 21 // parses the serialized output and verifies that the end result is the same | |
| 22 // as the headers that the test started with. | |
| 23 | |
| 24 SpdyHeaderBlock input_headers; | |
| 25 input_headers[":pseudo1"] = "pseudo value1"; | |
| 26 input_headers[":pseudo2"] = "pseudo value2"; | |
| 27 input_headers["key1"] = "value1"; | |
| 28 const int64_t kContentLength = 1234; | |
| 29 input_headers["content-length"] = | |
| 30 QuicTextUtils::Uint64ToString(kContentLength); | |
| 31 input_headers["key2"] = "value2"; | |
| 32 | |
| 33 // Serialize the header block. | |
| 34 string serialized_headers = | |
| 35 SpdyUtils::SerializeUncompressedHeaders(input_headers); | |
| 36 | |
| 37 // Take the serialized header block, and parse back into SpdyHeaderBlock. | |
| 38 SpdyHeaderBlock output_headers; | |
| 39 int64_t content_length = -1; | |
| 40 ASSERT_TRUE(SpdyUtils::ParseHeaders(serialized_headers.data(), | |
| 41 serialized_headers.size(), | |
| 42 &content_length, &output_headers)); | |
| 43 | |
| 44 // Should be back to the original headers. | |
| 45 EXPECT_EQ(content_length, kContentLength); | |
| 46 EXPECT_EQ(output_headers, input_headers); | |
| 47 } | |
| 48 | |
| 49 TEST(SpdyUtilsTest, SerializeAndParseHeadersLargeContentLength) { | |
| 50 // Creates a SpdyHeaderBlock with some key->value pairs, serializes it, then | |
| 51 // parses the serialized output and verifies that the end result is the same | |
| 52 // as the headers that the test started with. | |
| 53 | |
| 54 SpdyHeaderBlock input_headers; | |
| 55 input_headers[":pseudo1"] = "pseudo value1"; | |
| 56 input_headers[":pseudo2"] = "pseudo value2"; | |
| 57 input_headers["key1"] = "value1"; | |
| 58 const int64_t kContentLength = 12345678900; | |
| 59 input_headers["content-length"] = | |
| 60 QuicTextUtils::Uint64ToString(kContentLength); | |
| 61 input_headers["key2"] = "value2"; | |
| 62 | |
| 63 // Serialize the header block. | |
| 64 string serialized_headers = | |
| 65 SpdyUtils::SerializeUncompressedHeaders(input_headers); | |
| 66 | |
| 67 // Take the serialized header block, and parse back into SpdyHeaderBlock. | |
| 68 SpdyHeaderBlock output_headers; | |
| 69 int64_t content_length = -1; | |
| 70 ASSERT_TRUE(SpdyUtils::ParseHeaders(serialized_headers.data(), | |
| 71 serialized_headers.size(), | |
| 72 &content_length, &output_headers)); | |
| 73 | |
| 74 // Should be back to the original headers. | |
| 75 EXPECT_EQ(content_length, kContentLength); | |
| 76 EXPECT_EQ(output_headers, input_headers); | |
| 77 } | |
| 78 | |
| 79 TEST(SpdyUtilsTest, SerializeAndParseValidTrailers) { | |
| 80 // Creates a SpdyHeaderBlock with some valid Trailers key->value pairs, | |
| 81 // serializes it, then parses the serialized output and verifies that the end | |
| 82 // result is the same as the trailers that the test started with. | |
| 83 SpdyHeaderBlock input_trailers; | |
| 84 const size_t kFinalOffset = 5678; | |
| 85 input_trailers[kFinalOffsetHeaderKey] = | |
| 86 QuicTextUtils::Uint64ToString(kFinalOffset); | |
| 87 input_trailers["key1"] = "value1"; | |
| 88 input_trailers["key2"] = "value2"; | |
| 89 | |
| 90 // Serialize the trailers. | |
| 91 string serialized_trailers = | |
| 92 SpdyUtils::SerializeUncompressedHeaders(input_trailers); | |
| 93 | |
| 94 // Take the serialized trailers, and parse back into a SpdyHeaderBlock. | |
| 95 SpdyHeaderBlock output_trailers; | |
| 96 size_t final_byte_offset = 0; | |
| 97 EXPECT_TRUE(SpdyUtils::ParseTrailers(serialized_trailers.data(), | |
| 98 serialized_trailers.size(), | |
| 99 &final_byte_offset, &output_trailers)); | |
| 100 | |
| 101 // Should be back to the original trailers, without the final offset header. | |
| 102 EXPECT_EQ(final_byte_offset, kFinalOffset); | |
| 103 input_trailers.erase(kFinalOffsetHeaderKey); | |
| 104 EXPECT_EQ(output_trailers, input_trailers); | |
| 105 } | |
| 106 | |
| 107 TEST(SpdyUtilsTest, SerializeAndParseTrailersWithoutFinalOffset) { | |
| 108 // Verifies that parsing fails if Trailers are missing a final offset header. | |
| 109 | |
| 110 SpdyHeaderBlock input_trailers; | |
| 111 input_trailers["key1"] = "value1"; | |
| 112 input_trailers["key2"] = "value2"; | |
| 113 | |
| 114 // Serialize the trailers. | |
| 115 string serialized_trailers = | |
| 116 SpdyUtils::SerializeUncompressedHeaders(input_trailers); | |
| 117 | |
| 118 // Parsing the serialized trailers fails because of the missing final offset. | |
| 119 SpdyHeaderBlock output_trailers; | |
| 120 size_t final_byte_offset = 0; | |
| 121 EXPECT_FALSE(SpdyUtils::ParseTrailers(serialized_trailers.data(), | |
| 122 serialized_trailers.size(), | |
| 123 &final_byte_offset, &output_trailers)); | |
| 124 EXPECT_EQ(final_byte_offset, 0u); | |
| 125 } | |
| 126 | |
| 127 TEST(SpdyUtilsTest, SerializeAndParseTrailersWithPseudoHeaders) { | |
| 128 // Verifies that parsing fails if Trailers include pseudo-headers. | |
| 129 | |
| 130 SpdyHeaderBlock input_trailers; | |
| 131 input_trailers[kFinalOffsetHeaderKey] = "12345"; | |
| 132 input_trailers[":disallowed-pseudo-header"] = "pseudo value"; | |
| 133 input_trailers["key1"] = "value1"; | |
| 134 input_trailers["key2"] = "value2"; | |
| 135 | |
| 136 // Serialize the trailers. | |
| 137 string serialized_trailers = | |
| 138 SpdyUtils::SerializeUncompressedHeaders(input_trailers); | |
| 139 | |
| 140 // Parsing the serialized trailers fails because of the extra pseudo header. | |
| 141 SpdyHeaderBlock output_trailers; | |
| 142 size_t final_byte_offset = 0; | |
| 143 EXPECT_FALSE(SpdyUtils::ParseTrailers(serialized_trailers.data(), | |
| 144 serialized_trailers.size(), | |
| 145 &final_byte_offset, &output_trailers)); | |
| 146 } | |
| 147 static std::unique_ptr<QuicHeaderList> FromList( | 19 static std::unique_ptr<QuicHeaderList> FromList( |
| 148 const QuicHeaderList::ListType& src) { | 20 const QuicHeaderList::ListType& src) { |
| 149 std::unique_ptr<QuicHeaderList> headers(new QuicHeaderList); | 21 std::unique_ptr<QuicHeaderList> headers(new QuicHeaderList); |
| 150 headers->OnHeaderBlockStart(); | 22 headers->OnHeaderBlockStart(); |
| 151 for (const auto& p : src) { | 23 for (const auto& p : src) { |
| 152 headers->OnHeader(p.first, p.second); | 24 headers->OnHeader(p.first, p.second); |
| 153 } | 25 } |
| 154 headers->OnHeaderBlockEnd(0); | 26 headers->OnHeaderBlockEnd(0); |
| 155 return headers; | 27 return headers; |
| 156 } | 28 } |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 TEST(SpdyUtilsTest, PopulateHeaderBlockFromUrlFails) { | 250 TEST(SpdyUtilsTest, PopulateHeaderBlockFromUrlFails) { |
| 379 SpdyHeaderBlock headers; | 251 SpdyHeaderBlock headers; |
| 380 EXPECT_FALSE(SpdyUtils::PopulateHeaderBlockFromUrl("/", &headers)); | 252 EXPECT_FALSE(SpdyUtils::PopulateHeaderBlockFromUrl("/", &headers)); |
| 381 EXPECT_FALSE(SpdyUtils::PopulateHeaderBlockFromUrl("/index.html", &headers)); | 253 EXPECT_FALSE(SpdyUtils::PopulateHeaderBlockFromUrl("/index.html", &headers)); |
| 382 EXPECT_FALSE( | 254 EXPECT_FALSE( |
| 383 SpdyUtils::PopulateHeaderBlockFromUrl("www.google.com/", &headers)); | 255 SpdyUtils::PopulateHeaderBlockFromUrl("www.google.com/", &headers)); |
| 384 } | 256 } |
| 385 | 257 |
| 386 } // namespace test | 258 } // namespace test |
| 387 } // namespace net | 259 } // namespace net |
| OLD | NEW |