| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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 | 4 |
| 5 #include "net/spdy/spdy_alt_svc_wire_format.h" | 5 #include "net/spdy/spdy_alt_svc_wire_format.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "testing/gmock/include/gmock/gmock.h" | 8 #include "testing/gmock/include/gmock/gmock.h" |
| 9 #include "testing/platform_test.h" | 9 #include "testing/platform_test.h" |
| 10 | 10 |
| 11 using ::testing::_; | 11 using ::testing::_; |
| 12 | 12 |
| 13 namespace net { | 13 namespace net { |
| 14 | 14 |
| 15 namespace test { | 15 namespace test { |
| 16 | 16 |
| 17 // Expose all private methods of class SpdyAltSvcWireFormat. | 17 // Expose all private methods of class SpdyAltSvcWireFormat. |
| 18 class SpdyAltSvcWireFormatPeer { | 18 class SpdyAltSvcWireFormatPeer { |
| 19 public: | 19 public: |
| 20 static void SkipWhiteSpace(SpdyStringPiece::const_iterator* c, | 20 static void SkipWhiteSpace(SpdyStringPiece::const_iterator* c, |
| 21 SpdyStringPiece::const_iterator end) { | 21 SpdyStringPiece::const_iterator end) { |
| 22 SpdyAltSvcWireFormat::SkipWhiteSpace(c, end); | 22 SpdyAltSvcWireFormat::SkipWhiteSpace(c, end); |
| 23 } | 23 } |
| 24 static bool PercentDecode(SpdyStringPiece::const_iterator c, | 24 static bool PercentDecode(SpdyStringPiece::const_iterator c, |
| 25 SpdyStringPiece::const_iterator end, | 25 SpdyStringPiece::const_iterator end, |
| 26 std::string* output) { | 26 SpdyString* output) { |
| 27 return SpdyAltSvcWireFormat::PercentDecode(c, end, output); | 27 return SpdyAltSvcWireFormat::PercentDecode(c, end, output); |
| 28 } | 28 } |
| 29 static bool ParseAltAuthority(SpdyStringPiece::const_iterator c, | 29 static bool ParseAltAuthority(SpdyStringPiece::const_iterator c, |
| 30 SpdyStringPiece::const_iterator end, | 30 SpdyStringPiece::const_iterator end, |
| 31 std::string* host, | 31 SpdyString* host, |
| 32 uint16_t* port) { | 32 uint16_t* port) { |
| 33 return SpdyAltSvcWireFormat::ParseAltAuthority(c, end, host, port); | 33 return SpdyAltSvcWireFormat::ParseAltAuthority(c, end, host, port); |
| 34 } | 34 } |
| 35 static bool ParsePositiveInteger16(SpdyStringPiece::const_iterator c, | 35 static bool ParsePositiveInteger16(SpdyStringPiece::const_iterator c, |
| 36 SpdyStringPiece::const_iterator end, | 36 SpdyStringPiece::const_iterator end, |
| 37 uint16_t* max_age) { | 37 uint16_t* max_age) { |
| 38 return SpdyAltSvcWireFormat::ParsePositiveInteger16(c, end, max_age); | 38 return SpdyAltSvcWireFormat::ParsePositiveInteger16(c, end, max_age); |
| 39 } | 39 } |
| 40 static bool ParsePositiveInteger32(SpdyStringPiece::const_iterator c, | 40 static bool ParsePositiveInteger32(SpdyStringPiece::const_iterator c, |
| 41 SpdyStringPiece::const_iterator end, | 41 SpdyStringPiece::const_iterator end, |
| 42 uint32_t* max_age) { | 42 uint32_t* max_age) { |
| 43 return SpdyAltSvcWireFormat::ParsePositiveInteger32(c, end, max_age); | 43 return SpdyAltSvcWireFormat::ParsePositiveInteger32(c, end, max_age); |
| 44 } | 44 } |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 } // namespace test | 47 } // namespace test |
| 48 | 48 |
| 49 namespace { | 49 namespace { |
| 50 | 50 |
| 51 // Generate header field values, possibly with multiply defined parameters and | 51 // Generate header field values, possibly with multiply defined parameters and |
| 52 // random case, and corresponding AlternativeService entries. | 52 // random case, and corresponding AlternativeService entries. |
| 53 void FuzzHeaderFieldValue( | 53 void FuzzHeaderFieldValue( |
| 54 int i, | 54 int i, |
| 55 std::string* header_field_value, | 55 SpdyString* header_field_value, |
| 56 SpdyAltSvcWireFormat::AlternativeService* expected_altsvc) { | 56 SpdyAltSvcWireFormat::AlternativeService* expected_altsvc) { |
| 57 if (!header_field_value->empty()) { | 57 if (!header_field_value->empty()) { |
| 58 header_field_value->push_back(','); | 58 header_field_value->push_back(','); |
| 59 } | 59 } |
| 60 expected_altsvc->protocol_id = "a=b%c"; | 60 expected_altsvc->protocol_id = "a=b%c"; |
| 61 header_field_value->append("a%3Db%25c=\""); | 61 header_field_value->append("a%3Db%25c=\""); |
| 62 expected_altsvc->host = ""; | 62 expected_altsvc->host = ""; |
| 63 if (i & 1 << 0) { | 63 if (i & 1 << 0) { |
| 64 expected_altsvc->host = "foo\"bar\\baz"; | 64 expected_altsvc->host = "foo\"bar\\baz"; |
| 65 header_field_value->append("foo\\\"bar\\\\baz"); | 65 header_field_value->append("foo\\\"bar\\\\baz"); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 } | 102 } |
| 103 if (i & 1 << 10) { | 103 if (i & 1 << 10) { |
| 104 header_field_value->append(" "); | 104 header_field_value->append(" "); |
| 105 } | 105 } |
| 106 } | 106 } |
| 107 | 107 |
| 108 // Generate AlternativeService entries and corresponding header field values in | 108 // Generate AlternativeService entries and corresponding header field values in |
| 109 // canonical form, that is, what SerializeHeaderFieldValue() should output. | 109 // canonical form, that is, what SerializeHeaderFieldValue() should output. |
| 110 void FuzzAlternativeService(int i, | 110 void FuzzAlternativeService(int i, |
| 111 SpdyAltSvcWireFormat::AlternativeService* altsvc, | 111 SpdyAltSvcWireFormat::AlternativeService* altsvc, |
| 112 std::string* expected_header_field_value) { | 112 SpdyString* expected_header_field_value) { |
| 113 if (!expected_header_field_value->empty()) { | 113 if (!expected_header_field_value->empty()) { |
| 114 expected_header_field_value->push_back(','); | 114 expected_header_field_value->push_back(','); |
| 115 } | 115 } |
| 116 altsvc->protocol_id = "a=b%c"; | 116 altsvc->protocol_id = "a=b%c"; |
| 117 altsvc->port = 42; | 117 altsvc->port = 42; |
| 118 expected_header_field_value->append("a%3Db%25c=\""); | 118 expected_header_field_value->append("a%3Db%25c=\""); |
| 119 if (i & 1 << 0) { | 119 if (i & 1 << 0) { |
| 120 altsvc->host = "foo\"bar\\baz"; | 120 altsvc->host = "foo\"bar\\baz"; |
| 121 expected_header_field_value->append("foo\\\"bar\\\\baz"); | 121 expected_header_field_value->append("foo\\\"bar\\\\baz"); |
| 122 } | 122 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 ASSERT_TRUE( | 155 ASSERT_TRUE( |
| 156 SpdyAltSvcWireFormat::ParseHeaderFieldValue("clear", &altsvc_vector)); | 156 SpdyAltSvcWireFormat::ParseHeaderFieldValue("clear", &altsvc_vector)); |
| 157 EXPECT_EQ(0u, altsvc_vector.size()); | 157 EXPECT_EQ(0u, altsvc_vector.size()); |
| 158 } | 158 } |
| 159 | 159 |
| 160 // Fuzz test of ParseHeaderFieldValue() with optional whitespaces, ignored | 160 // Fuzz test of ParseHeaderFieldValue() with optional whitespaces, ignored |
| 161 // parameters, duplicate parameters, trailing space, trailing alternate service | 161 // parameters, duplicate parameters, trailing space, trailing alternate service |
| 162 // separator, etc. Single alternative service at a time. | 162 // separator, etc. Single alternative service at a time. |
| 163 TEST(SpdyAltSvcWireFormatTest, ParseHeaderFieldValue) { | 163 TEST(SpdyAltSvcWireFormatTest, ParseHeaderFieldValue) { |
| 164 for (int i = 0; i < 1 << 11; ++i) { | 164 for (int i = 0; i < 1 << 11; ++i) { |
| 165 std::string header_field_value; | 165 SpdyString header_field_value; |
| 166 SpdyAltSvcWireFormat::AlternativeService expected_altsvc; | 166 SpdyAltSvcWireFormat::AlternativeService expected_altsvc; |
| 167 FuzzHeaderFieldValue(i, &header_field_value, &expected_altsvc); | 167 FuzzHeaderFieldValue(i, &header_field_value, &expected_altsvc); |
| 168 SpdyAltSvcWireFormat::AlternativeServiceVector altsvc_vector; | 168 SpdyAltSvcWireFormat::AlternativeServiceVector altsvc_vector; |
| 169 ASSERT_TRUE(SpdyAltSvcWireFormat::ParseHeaderFieldValue(header_field_value, | 169 ASSERT_TRUE(SpdyAltSvcWireFormat::ParseHeaderFieldValue(header_field_value, |
| 170 &altsvc_vector)); | 170 &altsvc_vector)); |
| 171 ASSERT_EQ(1u, altsvc_vector.size()); | 171 ASSERT_EQ(1u, altsvc_vector.size()); |
| 172 EXPECT_EQ(expected_altsvc.protocol_id, altsvc_vector[0].protocol_id); | 172 EXPECT_EQ(expected_altsvc.protocol_id, altsvc_vector[0].protocol_id); |
| 173 EXPECT_EQ(expected_altsvc.host, altsvc_vector[0].host); | 173 EXPECT_EQ(expected_altsvc.host, altsvc_vector[0].host); |
| 174 EXPECT_EQ(expected_altsvc.port, altsvc_vector[0].port); | 174 EXPECT_EQ(expected_altsvc.port, altsvc_vector[0].port); |
| 175 EXPECT_EQ(expected_altsvc.max_age, altsvc_vector[0].max_age); | 175 EXPECT_EQ(expected_altsvc.max_age, altsvc_vector[0].max_age); |
| 176 EXPECT_EQ(expected_altsvc.version, altsvc_vector[0].version); | 176 EXPECT_EQ(expected_altsvc.version, altsvc_vector[0].version); |
| 177 | 177 |
| 178 // Roundtrip test starting with |altsvc_vector|. | 178 // Roundtrip test starting with |altsvc_vector|. |
| 179 std::string reserialized_header_field_value = | 179 SpdyString reserialized_header_field_value = |
| 180 SpdyAltSvcWireFormat::SerializeHeaderFieldValue(altsvc_vector); | 180 SpdyAltSvcWireFormat::SerializeHeaderFieldValue(altsvc_vector); |
| 181 SpdyAltSvcWireFormat::AlternativeServiceVector roundtrip_altsvc_vector; | 181 SpdyAltSvcWireFormat::AlternativeServiceVector roundtrip_altsvc_vector; |
| 182 ASSERT_TRUE(SpdyAltSvcWireFormat::ParseHeaderFieldValue( | 182 ASSERT_TRUE(SpdyAltSvcWireFormat::ParseHeaderFieldValue( |
| 183 reserialized_header_field_value, &roundtrip_altsvc_vector)); | 183 reserialized_header_field_value, &roundtrip_altsvc_vector)); |
| 184 ASSERT_EQ(1u, roundtrip_altsvc_vector.size()); | 184 ASSERT_EQ(1u, roundtrip_altsvc_vector.size()); |
| 185 EXPECT_EQ(expected_altsvc.protocol_id, | 185 EXPECT_EQ(expected_altsvc.protocol_id, |
| 186 roundtrip_altsvc_vector[0].protocol_id); | 186 roundtrip_altsvc_vector[0].protocol_id); |
| 187 EXPECT_EQ(expected_altsvc.host, roundtrip_altsvc_vector[0].host); | 187 EXPECT_EQ(expected_altsvc.host, roundtrip_altsvc_vector[0].host); |
| 188 EXPECT_EQ(expected_altsvc.port, roundtrip_altsvc_vector[0].port); | 188 EXPECT_EQ(expected_altsvc.port, roundtrip_altsvc_vector[0].port); |
| 189 EXPECT_EQ(expected_altsvc.max_age, roundtrip_altsvc_vector[0].max_age); | 189 EXPECT_EQ(expected_altsvc.max_age, roundtrip_altsvc_vector[0].max_age); |
| 190 EXPECT_EQ(expected_altsvc.version, roundtrip_altsvc_vector[0].version); | 190 EXPECT_EQ(expected_altsvc.version, roundtrip_altsvc_vector[0].version); |
| 191 } | 191 } |
| 192 } | 192 } |
| 193 | 193 |
| 194 // Fuzz test of ParseHeaderFieldValue() with optional whitespaces, ignored | 194 // Fuzz test of ParseHeaderFieldValue() with optional whitespaces, ignored |
| 195 // parameters, duplicate parameters, trailing space, trailing alternate service | 195 // parameters, duplicate parameters, trailing space, trailing alternate service |
| 196 // separator, etc. Possibly multiple alternative service at a time. | 196 // separator, etc. Possibly multiple alternative service at a time. |
| 197 TEST(SpdyAltSvcWireFormatTest, ParseHeaderFieldValueMultiple) { | 197 TEST(SpdyAltSvcWireFormatTest, ParseHeaderFieldValueMultiple) { |
| 198 for (int i = 0; i < 1 << 11;) { | 198 for (int i = 0; i < 1 << 11;) { |
| 199 std::string header_field_value; | 199 SpdyString header_field_value; |
| 200 SpdyAltSvcWireFormat::AlternativeServiceVector expected_altsvc_vector; | 200 SpdyAltSvcWireFormat::AlternativeServiceVector expected_altsvc_vector; |
| 201 // This will generate almost two hundred header field values with two, | 201 // This will generate almost two hundred header field values with two, |
| 202 // three, four, five, six, and seven alternative services each, and | 202 // three, four, five, six, and seven alternative services each, and |
| 203 // thousands with a single one. | 203 // thousands with a single one. |
| 204 do { | 204 do { |
| 205 SpdyAltSvcWireFormat::AlternativeService expected_altsvc; | 205 SpdyAltSvcWireFormat::AlternativeService expected_altsvc; |
| 206 FuzzHeaderFieldValue(i, &header_field_value, &expected_altsvc); | 206 FuzzHeaderFieldValue(i, &header_field_value, &expected_altsvc); |
| 207 expected_altsvc_vector.push_back(expected_altsvc); | 207 expected_altsvc_vector.push_back(expected_altsvc); |
| 208 ++i; | 208 ++i; |
| 209 } while ((i < 1 << 13) && (i % 6 < i % 7)); | 209 } while ((i < 1 << 13) && (i % 6 < i % 7)); |
| 210 SpdyAltSvcWireFormat::AlternativeServiceVector altsvc_vector; | 210 SpdyAltSvcWireFormat::AlternativeServiceVector altsvc_vector; |
| 211 ASSERT_TRUE(SpdyAltSvcWireFormat::ParseHeaderFieldValue(header_field_value, | 211 ASSERT_TRUE(SpdyAltSvcWireFormat::ParseHeaderFieldValue(header_field_value, |
| 212 &altsvc_vector)); | 212 &altsvc_vector)); |
| 213 ASSERT_EQ(expected_altsvc_vector.size(), altsvc_vector.size()); | 213 ASSERT_EQ(expected_altsvc_vector.size(), altsvc_vector.size()); |
| 214 for (unsigned int j = 0; j < altsvc_vector.size(); ++j) { | 214 for (unsigned int j = 0; j < altsvc_vector.size(); ++j) { |
| 215 EXPECT_EQ(expected_altsvc_vector[j].protocol_id, | 215 EXPECT_EQ(expected_altsvc_vector[j].protocol_id, |
| 216 altsvc_vector[j].protocol_id); | 216 altsvc_vector[j].protocol_id); |
| 217 EXPECT_EQ(expected_altsvc_vector[j].host, altsvc_vector[j].host); | 217 EXPECT_EQ(expected_altsvc_vector[j].host, altsvc_vector[j].host); |
| 218 EXPECT_EQ(expected_altsvc_vector[j].port, altsvc_vector[j].port); | 218 EXPECT_EQ(expected_altsvc_vector[j].port, altsvc_vector[j].port); |
| 219 EXPECT_EQ(expected_altsvc_vector[j].max_age, altsvc_vector[j].max_age); | 219 EXPECT_EQ(expected_altsvc_vector[j].max_age, altsvc_vector[j].max_age); |
| 220 EXPECT_EQ(expected_altsvc_vector[j].version, altsvc_vector[j].version); | 220 EXPECT_EQ(expected_altsvc_vector[j].version, altsvc_vector[j].version); |
| 221 } | 221 } |
| 222 | 222 |
| 223 // Roundtrip test starting with |altsvc_vector|. | 223 // Roundtrip test starting with |altsvc_vector|. |
| 224 std::string reserialized_header_field_value = | 224 SpdyString reserialized_header_field_value = |
| 225 SpdyAltSvcWireFormat::SerializeHeaderFieldValue(altsvc_vector); | 225 SpdyAltSvcWireFormat::SerializeHeaderFieldValue(altsvc_vector); |
| 226 SpdyAltSvcWireFormat::AlternativeServiceVector roundtrip_altsvc_vector; | 226 SpdyAltSvcWireFormat::AlternativeServiceVector roundtrip_altsvc_vector; |
| 227 ASSERT_TRUE(SpdyAltSvcWireFormat::ParseHeaderFieldValue( | 227 ASSERT_TRUE(SpdyAltSvcWireFormat::ParseHeaderFieldValue( |
| 228 reserialized_header_field_value, &roundtrip_altsvc_vector)); | 228 reserialized_header_field_value, &roundtrip_altsvc_vector)); |
| 229 ASSERT_EQ(expected_altsvc_vector.size(), roundtrip_altsvc_vector.size()); | 229 ASSERT_EQ(expected_altsvc_vector.size(), roundtrip_altsvc_vector.size()); |
| 230 for (unsigned int j = 0; j < roundtrip_altsvc_vector.size(); ++j) { | 230 for (unsigned int j = 0; j < roundtrip_altsvc_vector.size(); ++j) { |
| 231 EXPECT_EQ(expected_altsvc_vector[j].protocol_id, | 231 EXPECT_EQ(expected_altsvc_vector[j].protocol_id, |
| 232 roundtrip_altsvc_vector[j].protocol_id); | 232 roundtrip_altsvc_vector[j].protocol_id); |
| 233 EXPECT_EQ(expected_altsvc_vector[j].host, | 233 EXPECT_EQ(expected_altsvc_vector[j].host, |
| 234 roundtrip_altsvc_vector[j].host); | 234 roundtrip_altsvc_vector[j].host); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 248 SpdyAltSvcWireFormat::SerializeHeaderFieldValue(altsvc_vector)); | 248 SpdyAltSvcWireFormat::SerializeHeaderFieldValue(altsvc_vector)); |
| 249 } | 249 } |
| 250 | 250 |
| 251 // Test ParseHeaderFieldValue() and SerializeHeaderFieldValue() on the same pair | 251 // Test ParseHeaderFieldValue() and SerializeHeaderFieldValue() on the same pair |
| 252 // of |expected_header_field_value| and |altsvc|, with and without hostname and | 252 // of |expected_header_field_value| and |altsvc|, with and without hostname and |
| 253 // each | 253 // each |
| 254 // parameter. Single alternative service at a time. | 254 // parameter. Single alternative service at a time. |
| 255 TEST(SpdyAltSvcWireFormatTest, RoundTrip) { | 255 TEST(SpdyAltSvcWireFormatTest, RoundTrip) { |
| 256 for (int i = 0; i < 1 << 3; ++i) { | 256 for (int i = 0; i < 1 << 3; ++i) { |
| 257 SpdyAltSvcWireFormat::AlternativeService altsvc; | 257 SpdyAltSvcWireFormat::AlternativeService altsvc; |
| 258 std::string expected_header_field_value; | 258 SpdyString expected_header_field_value; |
| 259 FuzzAlternativeService(i, &altsvc, &expected_header_field_value); | 259 FuzzAlternativeService(i, &altsvc, &expected_header_field_value); |
| 260 | 260 |
| 261 // Test ParseHeaderFieldValue(). | 261 // Test ParseHeaderFieldValue(). |
| 262 SpdyAltSvcWireFormat::AlternativeServiceVector parsed_altsvc_vector; | 262 SpdyAltSvcWireFormat::AlternativeServiceVector parsed_altsvc_vector; |
| 263 ASSERT_TRUE(SpdyAltSvcWireFormat::ParseHeaderFieldValue( | 263 ASSERT_TRUE(SpdyAltSvcWireFormat::ParseHeaderFieldValue( |
| 264 expected_header_field_value, &parsed_altsvc_vector)); | 264 expected_header_field_value, &parsed_altsvc_vector)); |
| 265 ASSERT_EQ(1u, parsed_altsvc_vector.size()); | 265 ASSERT_EQ(1u, parsed_altsvc_vector.size()); |
| 266 EXPECT_EQ(altsvc.protocol_id, parsed_altsvc_vector[0].protocol_id); | 266 EXPECT_EQ(altsvc.protocol_id, parsed_altsvc_vector[0].protocol_id); |
| 267 EXPECT_EQ(altsvc.host, parsed_altsvc_vector[0].host); | 267 EXPECT_EQ(altsvc.host, parsed_altsvc_vector[0].host); |
| 268 EXPECT_EQ(altsvc.port, parsed_altsvc_vector[0].port); | 268 EXPECT_EQ(altsvc.port, parsed_altsvc_vector[0].port); |
| 269 EXPECT_EQ(altsvc.max_age, parsed_altsvc_vector[0].max_age); | 269 EXPECT_EQ(altsvc.max_age, parsed_altsvc_vector[0].max_age); |
| 270 EXPECT_EQ(altsvc.version, parsed_altsvc_vector[0].version); | 270 EXPECT_EQ(altsvc.version, parsed_altsvc_vector[0].version); |
| 271 | 271 |
| 272 // Test SerializeHeaderFieldValue(). | 272 // Test SerializeHeaderFieldValue(). |
| 273 SpdyAltSvcWireFormat::AlternativeServiceVector altsvc_vector; | 273 SpdyAltSvcWireFormat::AlternativeServiceVector altsvc_vector; |
| 274 altsvc_vector.push_back(altsvc); | 274 altsvc_vector.push_back(altsvc); |
| 275 EXPECT_EQ(expected_header_field_value, | 275 EXPECT_EQ(expected_header_field_value, |
| 276 SpdyAltSvcWireFormat::SerializeHeaderFieldValue(altsvc_vector)); | 276 SpdyAltSvcWireFormat::SerializeHeaderFieldValue(altsvc_vector)); |
| 277 } | 277 } |
| 278 } | 278 } |
| 279 | 279 |
| 280 // Test ParseHeaderFieldValue() and SerializeHeaderFieldValue() on the same pair | 280 // Test ParseHeaderFieldValue() and SerializeHeaderFieldValue() on the same pair |
| 281 // of |expected_header_field_value| and |altsvc|, with and without hostname and | 281 // of |expected_header_field_value| and |altsvc|, with and without hostname and |
| 282 // each | 282 // each |
| 283 // parameter. Multiple alternative services at a time. | 283 // parameter. Multiple alternative services at a time. |
| 284 TEST(SpdyAltSvcWireFormatTest, RoundTripMultiple) { | 284 TEST(SpdyAltSvcWireFormatTest, RoundTripMultiple) { |
| 285 SpdyAltSvcWireFormat::AlternativeServiceVector altsvc_vector; | 285 SpdyAltSvcWireFormat::AlternativeServiceVector altsvc_vector; |
| 286 std::string expected_header_field_value; | 286 SpdyString expected_header_field_value; |
| 287 for (int i = 0; i < 1 << 3; ++i) { | 287 for (int i = 0; i < 1 << 3; ++i) { |
| 288 SpdyAltSvcWireFormat::AlternativeService altsvc; | 288 SpdyAltSvcWireFormat::AlternativeService altsvc; |
| 289 FuzzAlternativeService(i, &altsvc, &expected_header_field_value); | 289 FuzzAlternativeService(i, &altsvc, &expected_header_field_value); |
| 290 altsvc_vector.push_back(altsvc); | 290 altsvc_vector.push_back(altsvc); |
| 291 } | 291 } |
| 292 | 292 |
| 293 // Test ParseHeaderFieldValue(). | 293 // Test ParseHeaderFieldValue(). |
| 294 SpdyAltSvcWireFormat::AlternativeServiceVector parsed_altsvc_vector; | 294 SpdyAltSvcWireFormat::AlternativeServiceVector parsed_altsvc_vector; |
| 295 ASSERT_TRUE(SpdyAltSvcWireFormat::ParseHeaderFieldValue( | 295 ASSERT_TRUE(SpdyAltSvcWireFormat::ParseHeaderFieldValue( |
| 296 expected_header_field_value, &parsed_altsvc_vector)); | 296 expected_header_field_value, &parsed_altsvc_vector)); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 } | 348 } |
| 349 } | 349 } |
| 350 | 350 |
| 351 // ParseHeaderFieldValue() should return false on a field values truncated | 351 // ParseHeaderFieldValue() should return false on a field values truncated |
| 352 // before closing quotation mark, without trying to access memory beyond the end | 352 // before closing quotation mark, without trying to access memory beyond the end |
| 353 // of the input. | 353 // of the input. |
| 354 TEST(SpdyAltSvcWireFormatTest, ParseTruncatedHeaderFieldValue) { | 354 TEST(SpdyAltSvcWireFormatTest, ParseTruncatedHeaderFieldValue) { |
| 355 SpdyAltSvcWireFormat::AlternativeServiceVector altsvc_vector; | 355 SpdyAltSvcWireFormat::AlternativeServiceVector altsvc_vector; |
| 356 const char* field_value_array[] = {"a=\":137\"", "a=\"foo:137\"", | 356 const char* field_value_array[] = {"a=\":137\"", "a=\"foo:137\"", |
| 357 "a%25=\"foo\\\"bar\\\\baz:137\""}; | 357 "a%25=\"foo\\\"bar\\\\baz:137\""}; |
| 358 for (const std::string& field_value : field_value_array) { | 358 for (const SpdyString& field_value : field_value_array) { |
| 359 for (size_t len = 1; len < field_value.size(); ++len) { | 359 for (size_t len = 1; len < field_value.size(); ++len) { |
| 360 EXPECT_FALSE(SpdyAltSvcWireFormat::ParseHeaderFieldValue( | 360 EXPECT_FALSE(SpdyAltSvcWireFormat::ParseHeaderFieldValue( |
| 361 field_value.substr(0, len), &altsvc_vector)) | 361 field_value.substr(0, len), &altsvc_vector)) |
| 362 << len; | 362 << len; |
| 363 } | 363 } |
| 364 } | 364 } |
| 365 } | 365 } |
| 366 | 366 |
| 367 // Tests of private methods. | 367 // Tests of private methods. |
| 368 | 368 |
| 369 // Test SkipWhiteSpace(). | 369 // Test SkipWhiteSpace(). |
| 370 TEST(SpdyAltSvcWireFormatTest, SkipWhiteSpace) { | 370 TEST(SpdyAltSvcWireFormatTest, SkipWhiteSpace) { |
| 371 SpdyStringPiece input("a \tb "); | 371 SpdyStringPiece input("a \tb "); |
| 372 SpdyStringPiece::const_iterator c = input.begin(); | 372 SpdyStringPiece::const_iterator c = input.begin(); |
| 373 test::SpdyAltSvcWireFormatPeer::SkipWhiteSpace(&c, input.end()); | 373 test::SpdyAltSvcWireFormatPeer::SkipWhiteSpace(&c, input.end()); |
| 374 ASSERT_EQ(input.begin(), c); | 374 ASSERT_EQ(input.begin(), c); |
| 375 ++c; | 375 ++c; |
| 376 test::SpdyAltSvcWireFormatPeer::SkipWhiteSpace(&c, input.end()); | 376 test::SpdyAltSvcWireFormatPeer::SkipWhiteSpace(&c, input.end()); |
| 377 ASSERT_EQ(input.begin() + 3, c); | 377 ASSERT_EQ(input.begin() + 3, c); |
| 378 ++c; | 378 ++c; |
| 379 test::SpdyAltSvcWireFormatPeer::SkipWhiteSpace(&c, input.end()); | 379 test::SpdyAltSvcWireFormatPeer::SkipWhiteSpace(&c, input.end()); |
| 380 ASSERT_EQ(input.end(), c); | 380 ASSERT_EQ(input.end(), c); |
| 381 } | 381 } |
| 382 | 382 |
| 383 // Test PercentDecode() on valid input. | 383 // Test PercentDecode() on valid input. |
| 384 TEST(SpdyAltSvcWireFormatTest, PercentDecodeValid) { | 384 TEST(SpdyAltSvcWireFormatTest, PercentDecodeValid) { |
| 385 SpdyStringPiece input(""); | 385 SpdyStringPiece input(""); |
| 386 std::string output; | 386 SpdyString output; |
| 387 ASSERT_TRUE(test::SpdyAltSvcWireFormatPeer::PercentDecode( | 387 ASSERT_TRUE(test::SpdyAltSvcWireFormatPeer::PercentDecode( |
| 388 input.begin(), input.end(), &output)); | 388 input.begin(), input.end(), &output)); |
| 389 EXPECT_EQ("", output); | 389 EXPECT_EQ("", output); |
| 390 | 390 |
| 391 input = SpdyStringPiece("foo"); | 391 input = SpdyStringPiece("foo"); |
| 392 output.clear(); | 392 output.clear(); |
| 393 ASSERT_TRUE(test::SpdyAltSvcWireFormatPeer::PercentDecode( | 393 ASSERT_TRUE(test::SpdyAltSvcWireFormatPeer::PercentDecode( |
| 394 input.begin(), input.end(), &output)); | 394 input.begin(), input.end(), &output)); |
| 395 EXPECT_EQ("foo", output); | 395 EXPECT_EQ("foo", output); |
| 396 | 396 |
| 397 input = SpdyStringPiece("%2ca%5Cb"); | 397 input = SpdyStringPiece("%2ca%5Cb"); |
| 398 output.clear(); | 398 output.clear(); |
| 399 ASSERT_TRUE(test::SpdyAltSvcWireFormatPeer::PercentDecode( | 399 ASSERT_TRUE(test::SpdyAltSvcWireFormatPeer::PercentDecode( |
| 400 input.begin(), input.end(), &output)); | 400 input.begin(), input.end(), &output)); |
| 401 EXPECT_EQ(",a\\b", output); | 401 EXPECT_EQ(",a\\b", output); |
| 402 } | 402 } |
| 403 | 403 |
| 404 // Test PercentDecode() on invalid input. | 404 // Test PercentDecode() on invalid input. |
| 405 TEST(SpdyAltSvcWireFormatTest, PercentDecodeInvalid) { | 405 TEST(SpdyAltSvcWireFormatTest, PercentDecodeInvalid) { |
| 406 const char* invalid_input_array[] = {"a%", "a%x", "a%b", "%J22", "%9z"}; | 406 const char* invalid_input_array[] = {"a%", "a%x", "a%b", "%J22", "%9z"}; |
| 407 for (const char* invalid_input : invalid_input_array) { | 407 for (const char* invalid_input : invalid_input_array) { |
| 408 SpdyStringPiece input(invalid_input); | 408 SpdyStringPiece input(invalid_input); |
| 409 std::string output; | 409 SpdyString output; |
| 410 EXPECT_FALSE(test::SpdyAltSvcWireFormatPeer::PercentDecode( | 410 EXPECT_FALSE(test::SpdyAltSvcWireFormatPeer::PercentDecode( |
| 411 input.begin(), input.end(), &output)) | 411 input.begin(), input.end(), &output)) |
| 412 << input; | 412 << input; |
| 413 } | 413 } |
| 414 } | 414 } |
| 415 | 415 |
| 416 // Test ParseAltAuthority() on valid input. | 416 // Test ParseAltAuthority() on valid input. |
| 417 TEST(SpdyAltSvcWireFormatTest, ParseAltAuthorityValid) { | 417 TEST(SpdyAltSvcWireFormatTest, ParseAltAuthorityValid) { |
| 418 SpdyStringPiece input(":42"); | 418 SpdyStringPiece input(":42"); |
| 419 std::string host; | 419 SpdyString host; |
| 420 uint16_t port; | 420 uint16_t port; |
| 421 ASSERT_TRUE(test::SpdyAltSvcWireFormatPeer::ParseAltAuthority( | 421 ASSERT_TRUE(test::SpdyAltSvcWireFormatPeer::ParseAltAuthority( |
| 422 input.begin(), input.end(), &host, &port)); | 422 input.begin(), input.end(), &host, &port)); |
| 423 EXPECT_TRUE(host.empty()); | 423 EXPECT_TRUE(host.empty()); |
| 424 EXPECT_EQ(42, port); | 424 EXPECT_EQ(42, port); |
| 425 | 425 |
| 426 input = SpdyStringPiece("foo:137"); | 426 input = SpdyStringPiece("foo:137"); |
| 427 ASSERT_TRUE(test::SpdyAltSvcWireFormatPeer::ParseAltAuthority( | 427 ASSERT_TRUE(test::SpdyAltSvcWireFormatPeer::ParseAltAuthority( |
| 428 input.begin(), input.end(), &host, &port)); | 428 input.begin(), input.end(), &host, &port)); |
| 429 EXPECT_EQ("foo", host); | 429 EXPECT_EQ("foo", host); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 450 " ", | 450 " ", |
| 451 ":12 ", | 451 ":12 ", |
| 452 "foo:12 ", | 452 "foo:12 ", |
| 453 "[2003:8:0:16::509d:9615]", | 453 "[2003:8:0:16::509d:9615]", |
| 454 "[2003:8:0:16::509d:9615]:", | 454 "[2003:8:0:16::509d:9615]:", |
| 455 "[2003:8:0:16::509d:9615]foo:443", | 455 "[2003:8:0:16::509d:9615]foo:443", |
| 456 "[2003:8:0:16::509d:9615:443", | 456 "[2003:8:0:16::509d:9615:443", |
| 457 "2003:8:0:16::509d:9615]:443"}; | 457 "2003:8:0:16::509d:9615]:443"}; |
| 458 for (const char* invalid_input : invalid_input_array) { | 458 for (const char* invalid_input : invalid_input_array) { |
| 459 SpdyStringPiece input(invalid_input); | 459 SpdyStringPiece input(invalid_input); |
| 460 std::string host; | 460 SpdyString host; |
| 461 uint16_t port; | 461 uint16_t port; |
| 462 EXPECT_FALSE(test::SpdyAltSvcWireFormatPeer::ParseAltAuthority( | 462 EXPECT_FALSE(test::SpdyAltSvcWireFormatPeer::ParseAltAuthority( |
| 463 input.begin(), input.end(), &host, &port)) | 463 input.begin(), input.end(), &host, &port)) |
| 464 << input; | 464 << input; |
| 465 } | 465 } |
| 466 } | 466 } |
| 467 | 467 |
| 468 // Test ParseInteger() on valid input. | 468 // Test ParseInteger() on valid input. |
| 469 TEST(SpdyAltSvcWireFormatTest, ParseIntegerValid) { | 469 TEST(SpdyAltSvcWireFormatTest, ParseIntegerValid) { |
| 470 SpdyStringPiece input("3"); | 470 SpdyStringPiece input("3"); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 544 EXPECT_EQ("quic", altsvc_vector[0].protocol_id); | 544 EXPECT_EQ("quic", altsvc_vector[0].protocol_id); |
| 545 EXPECT_EQ("[2003:8:0:16::509d:9615]", altsvc_vector[0].host); | 545 EXPECT_EQ("[2003:8:0:16::509d:9615]", altsvc_vector[0].host); |
| 546 EXPECT_EQ(443u, altsvc_vector[0].port); | 546 EXPECT_EQ(443u, altsvc_vector[0].port); |
| 547 EXPECT_EQ(60u, altsvc_vector[0].max_age); | 547 EXPECT_EQ(60u, altsvc_vector[0].max_age); |
| 548 EXPECT_THAT(altsvc_vector[0].version, ::testing::ElementsAre(36, 35)); | 548 EXPECT_THAT(altsvc_vector[0].version, ::testing::ElementsAre(36, 35)); |
| 549 } | 549 } |
| 550 | 550 |
| 551 } // namespace | 551 } // namespace |
| 552 | 552 |
| 553 } // namespace net | 553 } // namespace net |
| OLD | NEW |