| 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 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 StringPiece::const_iterator end, | 46 StringPiece::const_iterator end, |
| 47 double* probability) { | 47 double* probability) { |
| 48 return SpdyAltSvcWireFormat::ParseProbability(c, end, probability); | 48 return SpdyAltSvcWireFormat::ParseProbability(c, end, probability); |
| 49 } | 49 } |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 } // namespace test | 52 } // namespace test |
| 53 | 53 |
| 54 namespace { | 54 namespace { |
| 55 | 55 |
| 56 // Generate header field values, possibly with multiply defined parameters and |
| 57 // random case, and corresponding AlternativeService entries. |
| 56 void FuzzHeaderFieldValue( | 58 void FuzzHeaderFieldValue( |
| 57 int i, | 59 int i, |
| 58 std::string* header_field_value, | 60 std::string* header_field_value, |
| 59 SpdyAltSvcWireFormat::AlternativeService* expected_altsvc) { | 61 SpdyAltSvcWireFormat::AlternativeService* expected_altsvc) { |
| 60 if (!header_field_value->empty()) { | 62 if (!header_field_value->empty()) { |
| 61 header_field_value->push_back(','); | 63 header_field_value->push_back(','); |
| 62 } | 64 } |
| 63 expected_altsvc->protocol_id = "a=b%c"; | 65 expected_altsvc->protocol_id = "a=b%c"; |
| 64 header_field_value->append("a%3Db%25c=\""); | 66 header_field_value->append("a%3Db%25c=\""); |
| 65 expected_altsvc->host = ""; | 67 expected_altsvc->host = ""; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 header_field_value->append(" "); | 112 header_field_value->append(" "); |
| 111 } | 113 } |
| 112 if (i & 1 << 11) { | 114 if (i & 1 << 11) { |
| 113 header_field_value->append(","); | 115 header_field_value->append(","); |
| 114 } | 116 } |
| 115 if (i & 1 << 12) { | 117 if (i & 1 << 12) { |
| 116 header_field_value->append(" "); | 118 header_field_value->append(" "); |
| 117 } | 119 } |
| 118 } | 120 } |
| 119 | 121 |
| 122 // Generate AlternativeService entries and corresponding header field values in |
| 123 // canonical form, that is, what SerializeHeaderFieldValue() should output. |
| 120 void FuzzAlternativeService(int i, | 124 void FuzzAlternativeService(int i, |
| 121 SpdyAltSvcWireFormat::AlternativeService* altsvc, | 125 SpdyAltSvcWireFormat::AlternativeService* altsvc, |
| 122 std::string* expected_header_field_value) { | 126 std::string* expected_header_field_value) { |
| 123 if (!expected_header_field_value->empty()) { | 127 if (!expected_header_field_value->empty()) { |
| 124 expected_header_field_value->push_back(','); | 128 expected_header_field_value->push_back(','); |
| 125 } | 129 } |
| 126 altsvc->protocol_id = "a=b%c"; | 130 altsvc->protocol_id = "a=b%c"; |
| 127 altsvc->port = 42; | 131 altsvc->port = 42; |
| 128 expected_header_field_value->append("a%3Db%25c=\""); | 132 expected_header_field_value->append("a%3Db%25c=\""); |
| 129 if (i & 1 << 0) { | 133 if (i & 1 << 0) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 SpdyAltSvcWireFormat::AlternativeServiceVector altsvc_vector; | 186 SpdyAltSvcWireFormat::AlternativeServiceVector altsvc_vector; |
| 183 ASSERT_TRUE(SpdyAltSvcWireFormat::ParseHeaderFieldValue(header_field_value, | 187 ASSERT_TRUE(SpdyAltSvcWireFormat::ParseHeaderFieldValue(header_field_value, |
| 184 &altsvc_vector)); | 188 &altsvc_vector)); |
| 185 ASSERT_EQ(1u, altsvc_vector.size()); | 189 ASSERT_EQ(1u, altsvc_vector.size()); |
| 186 EXPECT_EQ(expected_altsvc.protocol_id, altsvc_vector[0].protocol_id); | 190 EXPECT_EQ(expected_altsvc.protocol_id, altsvc_vector[0].protocol_id); |
| 187 EXPECT_EQ(expected_altsvc.host, altsvc_vector[0].host); | 191 EXPECT_EQ(expected_altsvc.host, altsvc_vector[0].host); |
| 188 EXPECT_EQ(expected_altsvc.port, altsvc_vector[0].port); | 192 EXPECT_EQ(expected_altsvc.port, altsvc_vector[0].port); |
| 189 EXPECT_EQ(expected_altsvc.version, altsvc_vector[0].version); | 193 EXPECT_EQ(expected_altsvc.version, altsvc_vector[0].version); |
| 190 EXPECT_EQ(expected_altsvc.max_age, altsvc_vector[0].max_age); | 194 EXPECT_EQ(expected_altsvc.max_age, altsvc_vector[0].max_age); |
| 191 EXPECT_DOUBLE_EQ(expected_altsvc.probability, altsvc_vector[0].probability); | 195 EXPECT_DOUBLE_EQ(expected_altsvc.probability, altsvc_vector[0].probability); |
| 196 |
| 197 // Roundtrip test starting with |altsvc_vector|. |
| 198 std::string reserialized_header_field_value = |
| 199 SpdyAltSvcWireFormat::SerializeHeaderFieldValue(altsvc_vector); |
| 200 SpdyAltSvcWireFormat::AlternativeServiceVector roundtrip_altsvc_vector; |
| 201 ASSERT_TRUE(SpdyAltSvcWireFormat::ParseHeaderFieldValue( |
| 202 reserialized_header_field_value, &roundtrip_altsvc_vector)); |
| 203 ASSERT_EQ(1u, roundtrip_altsvc_vector.size()); |
| 204 EXPECT_EQ(expected_altsvc.protocol_id, |
| 205 roundtrip_altsvc_vector[0].protocol_id); |
| 206 EXPECT_EQ(expected_altsvc.host, roundtrip_altsvc_vector[0].host); |
| 207 EXPECT_EQ(expected_altsvc.port, roundtrip_altsvc_vector[0].port); |
| 208 EXPECT_EQ(expected_altsvc.version, roundtrip_altsvc_vector[0].version); |
| 209 EXPECT_EQ(expected_altsvc.max_age, roundtrip_altsvc_vector[0].max_age); |
| 210 EXPECT_DOUBLE_EQ(expected_altsvc.probability, |
| 211 roundtrip_altsvc_vector[0].probability); |
| 192 } | 212 } |
| 193 } | 213 } |
| 194 | 214 |
| 195 // Fuzz test of ParseHeaderFieldValue() with optional whitespaces, ignored | 215 // Fuzz test of ParseHeaderFieldValue() with optional whitespaces, ignored |
| 196 // parameters, duplicate parameters, trailing space, trailing alternate service | 216 // parameters, duplicate parameters, trailing space, trailing alternate service |
| 197 // separator, etc. Possibly multiple alternative service at a time. | 217 // separator, etc. Possibly multiple alternative service at a time. |
| 198 TEST(SpdyAltSvcWireFormatTest, ParseHeaderFieldValueMultiple) { | 218 TEST(SpdyAltSvcWireFormatTest, ParseHeaderFieldValueMultiple) { |
| 199 for (int i = 0; i < 1 << 13;) { | 219 for (int i = 0; i < 1 << 13;) { |
| 200 std::string header_field_value; | 220 std::string header_field_value; |
| 201 SpdyAltSvcWireFormat::AlternativeServiceVector expected_altsvc_vector; | 221 SpdyAltSvcWireFormat::AlternativeServiceVector expected_altsvc_vector; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 215 for (unsigned int j = 0; j < altsvc_vector.size(); ++j) { | 235 for (unsigned int j = 0; j < altsvc_vector.size(); ++j) { |
| 216 EXPECT_EQ(expected_altsvc_vector[j].protocol_id, | 236 EXPECT_EQ(expected_altsvc_vector[j].protocol_id, |
| 217 altsvc_vector[j].protocol_id); | 237 altsvc_vector[j].protocol_id); |
| 218 EXPECT_EQ(expected_altsvc_vector[j].host, altsvc_vector[j].host); | 238 EXPECT_EQ(expected_altsvc_vector[j].host, altsvc_vector[j].host); |
| 219 EXPECT_EQ(expected_altsvc_vector[j].port, altsvc_vector[j].port); | 239 EXPECT_EQ(expected_altsvc_vector[j].port, altsvc_vector[j].port); |
| 220 EXPECT_EQ(expected_altsvc_vector[j].version, altsvc_vector[j].version); | 240 EXPECT_EQ(expected_altsvc_vector[j].version, altsvc_vector[j].version); |
| 221 EXPECT_EQ(expected_altsvc_vector[j].max_age, altsvc_vector[j].max_age); | 241 EXPECT_EQ(expected_altsvc_vector[j].max_age, altsvc_vector[j].max_age); |
| 222 EXPECT_DOUBLE_EQ(expected_altsvc_vector[j].probability, | 242 EXPECT_DOUBLE_EQ(expected_altsvc_vector[j].probability, |
| 223 altsvc_vector[j].probability); | 243 altsvc_vector[j].probability); |
| 224 } | 244 } |
| 245 |
| 246 // Roundtrip test starting with |altsvc_vector|. |
| 247 std::string reserialized_header_field_value = |
| 248 SpdyAltSvcWireFormat::SerializeHeaderFieldValue(altsvc_vector); |
| 249 SpdyAltSvcWireFormat::AlternativeServiceVector roundtrip_altsvc_vector; |
| 250 ASSERT_TRUE(SpdyAltSvcWireFormat::ParseHeaderFieldValue( |
| 251 reserialized_header_field_value, &roundtrip_altsvc_vector)); |
| 252 ASSERT_EQ(expected_altsvc_vector.size(), roundtrip_altsvc_vector.size()); |
| 253 for (unsigned int j = 0; j < roundtrip_altsvc_vector.size(); ++j) { |
| 254 EXPECT_EQ(expected_altsvc_vector[j].protocol_id, |
| 255 roundtrip_altsvc_vector[j].protocol_id); |
| 256 EXPECT_EQ(expected_altsvc_vector[j].host, |
| 257 roundtrip_altsvc_vector[j].host); |
| 258 EXPECT_EQ(expected_altsvc_vector[j].port, |
| 259 roundtrip_altsvc_vector[j].port); |
| 260 EXPECT_EQ(expected_altsvc_vector[j].version, |
| 261 roundtrip_altsvc_vector[j].version); |
| 262 EXPECT_EQ(expected_altsvc_vector[j].max_age, |
| 263 roundtrip_altsvc_vector[j].max_age); |
| 264 EXPECT_DOUBLE_EQ(expected_altsvc_vector[j].probability, |
| 265 roundtrip_altsvc_vector[j].probability); |
| 266 } |
| 225 } | 267 } |
| 226 } | 268 } |
| 227 | 269 |
| 228 TEST(SpdyAltSvcWireFormatTest, SerializeEmptyHeaderFieldValue) { | 270 TEST(SpdyAltSvcWireFormatTest, SerializeEmptyHeaderFieldValue) { |
| 229 SpdyAltSvcWireFormat::AlternativeServiceVector altsvc_vector; | 271 SpdyAltSvcWireFormat::AlternativeServiceVector altsvc_vector; |
| 230 EXPECT_EQ("clear", | 272 EXPECT_EQ("clear", |
| 231 SpdyAltSvcWireFormat::SerializeHeaderFieldValue(altsvc_vector)); | 273 SpdyAltSvcWireFormat::SerializeHeaderFieldValue(altsvc_vector)); |
| 232 } | 274 } |
| 233 | 275 |
| 234 // Test SerializeHeaderFieldValue() with and without hostname and each | 276 // Test ParseHeaderFieldValue() and SerializeHeaderFieldValue() on the same pair |
| 277 // of |expected_header_field_value| and |altsvc|, with and without hostname and |
| 278 // each |
| 235 // parameter. Single alternative service at a time. | 279 // parameter. Single alternative service at a time. |
| 236 TEST(SpdyAltSvcWireFormatTest, SerializeHeaderFieldValue) { | 280 TEST(SpdyAltSvcWireFormatTest, RoundTrip) { |
| 237 for (int i = 0; i < 1 << 4; ++i) { | 281 for (int i = 0; i < 1 << 4; ++i) { |
| 238 SpdyAltSvcWireFormat::AlternativeService altsvc; | 282 SpdyAltSvcWireFormat::AlternativeService altsvc; |
| 239 std::string expected_header_field_value; | 283 std::string expected_header_field_value; |
| 240 FuzzAlternativeService(i, &altsvc, &expected_header_field_value); | 284 FuzzAlternativeService(i, &altsvc, &expected_header_field_value); |
| 285 |
| 286 // Test ParseHeaderFieldValue(). |
| 287 SpdyAltSvcWireFormat::AlternativeServiceVector parsed_altsvc_vector; |
| 288 ASSERT_TRUE(SpdyAltSvcWireFormat::ParseHeaderFieldValue( |
| 289 expected_header_field_value, &parsed_altsvc_vector)); |
| 290 ASSERT_EQ(1u, parsed_altsvc_vector.size()); |
| 291 EXPECT_EQ(altsvc.protocol_id, parsed_altsvc_vector[0].protocol_id); |
| 292 EXPECT_EQ(altsvc.host, parsed_altsvc_vector[0].host); |
| 293 EXPECT_EQ(altsvc.port, parsed_altsvc_vector[0].port); |
| 294 EXPECT_EQ(altsvc.version, parsed_altsvc_vector[0].version); |
| 295 EXPECT_EQ(altsvc.max_age, parsed_altsvc_vector[0].max_age); |
| 296 EXPECT_DOUBLE_EQ(altsvc.probability, parsed_altsvc_vector[0].probability); |
| 297 |
| 298 // Test SerializeHeaderFieldValue(). |
| 241 SpdyAltSvcWireFormat::AlternativeServiceVector altsvc_vector; | 299 SpdyAltSvcWireFormat::AlternativeServiceVector altsvc_vector; |
| 242 altsvc_vector.push_back(altsvc); | 300 altsvc_vector.push_back(altsvc); |
| 243 EXPECT_EQ(expected_header_field_value, | 301 EXPECT_EQ(expected_header_field_value, |
| 244 SpdyAltSvcWireFormat::SerializeHeaderFieldValue(altsvc_vector)); | 302 SpdyAltSvcWireFormat::SerializeHeaderFieldValue(altsvc_vector)); |
| 245 } | 303 } |
| 246 } | 304 } |
| 247 | 305 |
| 248 // Test SerializeHeaderFieldValue() with and without hostname and each | 306 // Test ParseHeaderFieldValue() and SerializeHeaderFieldValue() on the same pair |
| 307 // of |expected_header_field_value| and |altsvc|, with and without hostname and |
| 308 // each |
| 249 // parameter. Multiple alternative services at a time. | 309 // parameter. Multiple alternative services at a time. |
| 250 TEST(SpdyAltSvcWireFormatTest, SerializeHeaderFieldValueMultiple) { | 310 TEST(SpdyAltSvcWireFormatTest, RoundTripMultiple) { |
| 251 SpdyAltSvcWireFormat::AlternativeServiceVector altsvc_vector; | 311 SpdyAltSvcWireFormat::AlternativeServiceVector altsvc_vector; |
| 252 std::string expected_header_field_value; | 312 std::string expected_header_field_value; |
| 253 for (int i = 0; i < 1 << 4; ++i) { | 313 for (int i = 0; i < 1 << 4; ++i) { |
| 254 SpdyAltSvcWireFormat::AlternativeService altsvc; | 314 SpdyAltSvcWireFormat::AlternativeService altsvc; |
| 255 FuzzAlternativeService(i, &altsvc, &expected_header_field_value); | 315 FuzzAlternativeService(i, &altsvc, &expected_header_field_value); |
| 256 altsvc_vector.push_back(altsvc); | 316 altsvc_vector.push_back(altsvc); |
| 257 } | 317 } |
| 318 |
| 319 // Test ParseHeaderFieldValue(). |
| 320 SpdyAltSvcWireFormat::AlternativeServiceVector parsed_altsvc_vector; |
| 321 ASSERT_TRUE(SpdyAltSvcWireFormat::ParseHeaderFieldValue( |
| 322 expected_header_field_value, &parsed_altsvc_vector)); |
| 323 ASSERT_EQ(altsvc_vector.size(), parsed_altsvc_vector.size()); |
| 324 SpdyAltSvcWireFormat::AlternativeServiceVector::iterator expected_it = |
| 325 altsvc_vector.begin(); |
| 326 SpdyAltSvcWireFormat::AlternativeServiceVector::iterator parsed_it = |
| 327 parsed_altsvc_vector.begin(); |
| 328 for (; expected_it != altsvc_vector.end(); ++expected_it, ++parsed_it) { |
| 329 EXPECT_EQ(expected_it->protocol_id, parsed_it->protocol_id); |
| 330 EXPECT_EQ(expected_it->host, parsed_it->host); |
| 331 EXPECT_EQ(expected_it->port, parsed_it->port); |
| 332 EXPECT_EQ(expected_it->version, parsed_it->version); |
| 333 EXPECT_EQ(expected_it->max_age, parsed_it->max_age); |
| 334 EXPECT_DOUBLE_EQ(expected_it->probability, parsed_it->probability); |
| 335 } |
| 336 |
| 337 // Test SerializeHeaderFieldValue(). |
| 258 EXPECT_EQ(expected_header_field_value, | 338 EXPECT_EQ(expected_header_field_value, |
| 259 SpdyAltSvcWireFormat::SerializeHeaderFieldValue(altsvc_vector)); | 339 SpdyAltSvcWireFormat::SerializeHeaderFieldValue(altsvc_vector)); |
| 260 } | 340 } |
| 261 | 341 |
| 262 // ParseHeaderFieldValue() should return false on malformed field values: | 342 // ParseHeaderFieldValue() should return false on malformed field values: |
| 263 // invalid percent encoding, unmatched quotation mark, empty port, non-numeric | 343 // invalid percent encoding, unmatched quotation mark, empty port, non-numeric |
| 264 // characters in numeric fields, negative or larger than 1.0 probability. | 344 // characters in numeric fields, negative or larger than 1.0 probability. |
| 265 TEST(SpdyAltSvcWireFormatTest, ParseHeaderFieldValueInvalid) { | 345 TEST(SpdyAltSvcWireFormatTest, ParseHeaderFieldValueInvalid) { |
| 266 SpdyAltSvcWireFormat::AlternativeServiceVector altsvc_vector; | 346 SpdyAltSvcWireFormat::AlternativeServiceVector altsvc_vector; |
| 267 const char* invalid_field_value_array[] = {"a%", "a%x", "a%b", "a%9z", | 347 const char* invalid_field_value_array[] = {"a%", "a%x", "a%b", "a%9z", |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 StringPiece input(invalid_input); | 583 StringPiece input(invalid_input); |
| 504 double probability; | 584 double probability; |
| 505 EXPECT_FALSE(test::SpdyAltSvcWireFormatPeer::ParseProbability( | 585 EXPECT_FALSE(test::SpdyAltSvcWireFormatPeer::ParseProbability( |
| 506 input.begin(), input.end(), &probability)); | 586 input.begin(), input.end(), &probability)); |
| 507 } | 587 } |
| 508 } | 588 } |
| 509 | 589 |
| 510 } // namespace | 590 } // namespace |
| 511 | 591 |
| 512 } // namespace net | 592 } // namespace net |
| OLD | NEW |