| 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/spdy_utils.h" | 4 #include "net/quic/spdy_utils.h" |
| 5 | 5 |
| 6 #include "base/macros.h" | 6 #include "base/macros.h" |
| 7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/strings/string_piece.h" |
| 8 #include "net/test/gtest_util.h" | 9 #include "net/test/gtest_util.h" |
| 9 | 10 |
| 11 using base::StringPiece; |
| 10 using std::string; | 12 using std::string; |
| 13 using testing::UnorderedElementsAre; |
| 14 using testing::Pair; |
| 11 | 15 |
| 12 namespace net { | 16 namespace net { |
| 13 namespace test { | 17 namespace test { |
| 14 | 18 |
| 15 TEST(SpdyUtilsTest, SerializeAndParseHeaders) { | 19 TEST(SpdyUtilsTest, SerializeAndParseHeaders) { |
| 16 // Creates a SpdyHeaderBlock with some key->value pairs, serializes it, then | 20 // Creates a SpdyHeaderBlock with some key->value pairs, serializes it, then |
| 17 // 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 |
| 18 // as the headers that the test started with. | 22 // as the headers that the test started with. |
| 19 | 23 |
| 20 SpdyHeaderBlock input_headers; | 24 SpdyHeaderBlock input_headers; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 string serialized_trailers = | 105 string serialized_trailers = |
| 102 SpdyUtils::SerializeUncompressedHeaders(input_trailers); | 106 SpdyUtils::SerializeUncompressedHeaders(input_trailers); |
| 103 | 107 |
| 104 // Parsing the serialized trailers fails because of the extra pseudo header. | 108 // Parsing the serialized trailers fails because of the extra pseudo header. |
| 105 SpdyHeaderBlock output_trailers; | 109 SpdyHeaderBlock output_trailers; |
| 106 size_t final_byte_offset = 0; | 110 size_t final_byte_offset = 0; |
| 107 EXPECT_FALSE(SpdyUtils::ParseTrailers(serialized_trailers.data(), | 111 EXPECT_FALSE(SpdyUtils::ParseTrailers(serialized_trailers.data(), |
| 108 serialized_trailers.size(), | 112 serialized_trailers.size(), |
| 109 &final_byte_offset, &output_trailers)); | 113 &final_byte_offset, &output_trailers)); |
| 110 } | 114 } |
| 115 static std::unique_ptr<QuicHeaderList> FromList( |
| 116 const QuicHeaderList::ListType& src) { |
| 117 std::unique_ptr<QuicHeaderList> headers(new QuicHeaderList); |
| 118 headers->OnHeaderBlockStart(); |
| 119 for (const auto& p : src) { |
| 120 headers->OnHeader(p.first, p.second); |
| 121 } |
| 122 headers->OnHeaderBlockEnd(0); |
| 123 return headers; |
| 124 } |
| 125 |
| 126 TEST(SpdyUtilsTest, CopyAndValidateHeaders) { |
| 127 auto headers = |
| 128 FromList({{"foo", "foovalue"}, {"bar", "barvalue"}, {"baz", ""}}); |
| 129 int64_t content_length = -1; |
| 130 SpdyHeaderBlock block; |
| 131 ASSERT_TRUE( |
| 132 SpdyUtils::CopyAndValidateHeaders(*headers, &content_length, &block)); |
| 133 EXPECT_THAT(block, |
| 134 UnorderedElementsAre(Pair("foo", "foovalue"), |
| 135 Pair("bar", "barvalue"), Pair("baz", ""))); |
| 136 EXPECT_EQ(-1, content_length); |
| 137 } |
| 138 |
| 139 TEST(SpdyUtilsTest, CopyAndValidateHeadersEmptyName) { |
| 140 auto headers = FromList({{"foo", "foovalue"}, {"", "barvalue"}, {"baz", ""}}); |
| 141 int64_t content_length = -1; |
| 142 SpdyHeaderBlock block; |
| 143 ASSERT_FALSE( |
| 144 SpdyUtils::CopyAndValidateHeaders(*headers, &content_length, &block)); |
| 145 } |
| 146 |
| 147 TEST(SpdyUtilsTest, CopyAndValidateHeadersUpperCaseName) { |
| 148 auto headers = |
| 149 FromList({{"foo", "foovalue"}, {"bar", "barvalue"}, {"bAz", ""}}); |
| 150 int64_t content_length = -1; |
| 151 SpdyHeaderBlock block; |
| 152 ASSERT_FALSE( |
| 153 SpdyUtils::CopyAndValidateHeaders(*headers, &content_length, &block)); |
| 154 } |
| 155 |
| 156 TEST(SpdyUtilsTest, CopyAndValidateHeadersMultipleContentLengths) { |
| 157 auto headers = FromList({{"content-length", "9"}, |
| 158 {"foo", "foovalue"}, |
| 159 {"content-length", "9"}, |
| 160 {"bar", "barvalue"}, |
| 161 {"baz", ""}}); |
| 162 int64_t content_length = -1; |
| 163 SpdyHeaderBlock block; |
| 164 ASSERT_TRUE( |
| 165 SpdyUtils::CopyAndValidateHeaders(*headers, &content_length, &block)); |
| 166 EXPECT_THAT(block, UnorderedElementsAre( |
| 167 Pair("foo", "foovalue"), Pair("bar", "barvalue"), |
| 168 Pair("content-length", StringPiece("9\09", 3)), |
| 169 Pair("baz", ""))); |
| 170 EXPECT_EQ(9, content_length); |
| 171 } |
| 172 |
| 173 TEST(SpdyUtilsTest, CopyAndValidateHeadersInconsistentContentLengths) { |
| 174 auto headers = FromList({{"content-length", "9"}, |
| 175 {"foo", "foovalue"}, |
| 176 {"content-length", "8"}, |
| 177 {"bar", "barvalue"}, |
| 178 {"baz", ""}}); |
| 179 int64_t content_length = -1; |
| 180 SpdyHeaderBlock block; |
| 181 ASSERT_FALSE( |
| 182 SpdyUtils::CopyAndValidateHeaders(*headers, &content_length, &block)); |
| 183 } |
| 184 |
| 185 TEST(SpdyUtilsTest, CopyAndValidateHeadersMultipleValues) { |
| 186 auto headers = FromList({{"foo", "foovalue"}, |
| 187 {"bar", "barvalue"}, |
| 188 {"baz", ""}, |
| 189 {"foo", "boo"}, |
| 190 {"baz", "buzz"}}); |
| 191 int64_t content_length = -1; |
| 192 SpdyHeaderBlock block; |
| 193 ASSERT_TRUE( |
| 194 SpdyUtils::CopyAndValidateHeaders(*headers, &content_length, &block)); |
| 195 EXPECT_THAT( |
| 196 block, UnorderedElementsAre(Pair("foo", StringPiece("foovalue\0boo", 12)), |
| 197 Pair("bar", "barvalue"), |
| 198 Pair("baz", StringPiece("\0buzz", 5)))); |
| 199 EXPECT_EQ(-1, content_length); |
| 200 } |
| 111 | 201 |
| 112 TEST(SpdyUtilsTest, GetUrlFromHeaderBlock) { | 202 TEST(SpdyUtilsTest, GetUrlFromHeaderBlock) { |
| 113 SpdyHeaderBlock headers; | 203 SpdyHeaderBlock headers; |
| 114 EXPECT_EQ(SpdyUtils::GetUrlFromHeaderBlock(headers), ""); | 204 EXPECT_EQ(SpdyUtils::GetUrlFromHeaderBlock(headers), ""); |
| 115 headers[":scheme"] = "https"; | 205 headers[":scheme"] = "https"; |
| 116 EXPECT_EQ(SpdyUtils::GetUrlFromHeaderBlock(headers), ""); | 206 EXPECT_EQ(SpdyUtils::GetUrlFromHeaderBlock(headers), ""); |
| 117 headers[":authority"] = "www.google.com"; | 207 headers[":authority"] = "www.google.com"; |
| 118 EXPECT_EQ(SpdyUtils::GetUrlFromHeaderBlock(headers), ""); | 208 EXPECT_EQ(SpdyUtils::GetUrlFromHeaderBlock(headers), ""); |
| 119 headers[":path"] = "/index.html"; | 209 headers[":path"] = "/index.html"; |
| 120 EXPECT_EQ(SpdyUtils::GetUrlFromHeaderBlock(headers), | 210 EXPECT_EQ(SpdyUtils::GetUrlFromHeaderBlock(headers), |
| (...skipping 30 matching lines...) Expand all Loading... |
| 151 headers[":scheme"] = "https"; | 241 headers[":scheme"] = "https"; |
| 152 EXPECT_FALSE(SpdyUtils::UrlIsValid(headers)); | 242 EXPECT_FALSE(SpdyUtils::UrlIsValid(headers)); |
| 153 headers[":authority"] = "www.google.com"; | 243 headers[":authority"] = "www.google.com"; |
| 154 EXPECT_FALSE(SpdyUtils::UrlIsValid(headers)); | 244 EXPECT_FALSE(SpdyUtils::UrlIsValid(headers)); |
| 155 headers[":path"] = "/index.html"; | 245 headers[":path"] = "/index.html"; |
| 156 EXPECT_TRUE(SpdyUtils::UrlIsValid(headers)); | 246 EXPECT_TRUE(SpdyUtils::UrlIsValid(headers)); |
| 157 } | 247 } |
| 158 | 248 |
| 159 } // namespace test | 249 } // namespace test |
| 160 } // namespace net | 250 } // namespace net |
| OLD | NEW |