OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/data_reduction_proxy/common/data_reduction_proxy_headers.h" | 5 #include "components/data_reduction_proxy/common/data_reduction_proxy_headers.h" |
6 | 6 |
7 #include "net/http/http_response_headers.h" | 7 #include "net/http/http_response_headers.h" |
8 #include "net/proxy/proxy_service.h" | 8 #include "net/proxy/proxy_service.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 namespace { | 11 namespace { |
12 | 12 |
13 // Transform "normal"-looking headers (\n-separated) to the appropriate | 13 // Transform "normal"-looking headers (\n-separated) to the appropriate |
14 // input format for ParseRawHeaders (\0-separated). | 14 // input format for ParseRawHeaders (\0-separated). |
15 void HeadersToRaw(std::string* headers) { | 15 void HeadersToRaw(std::string* headers) { |
16 std::replace(headers->begin(), headers->end(), '\n', '\0'); | 16 std::replace(headers->begin(), headers->end(), '\n', '\0'); |
17 if (!headers->empty()) | 17 if (!headers->empty()) |
18 *headers += '\0'; | 18 *headers += '\0'; |
19 } | 19 } |
20 | 20 |
21 } // namespace | 21 } // namespace |
22 | 22 |
23 namespace data_reduction_proxy { | 23 namespace data_reduction_proxy { |
24 | 24 |
25 class DataReductionProxyHeadersTest : public testing::Test {}; | 25 class DataReductionProxyHeadersTest : public testing::Test {}; |
26 | 26 |
| 27 TEST_F(DataReductionProxyHeadersTest, GetDataReductionProxyActionValue) { |
| 28 const struct { |
| 29 const char* headers; |
| 30 std::string action_key; |
| 31 bool expected_result; |
| 32 std::string expected_action_value; |
| 33 } tests[] = { |
| 34 { "HTTP/1.1 200 OK\n" |
| 35 "Content-Length: 999\n", |
| 36 "a", |
| 37 false, |
| 38 "", |
| 39 }, |
| 40 { "HTTP/1.1 200 OK\n" |
| 41 "connection: keep-alive\n" |
| 42 "Content-Length: 999\n", |
| 43 "a", |
| 44 false, |
| 45 "", |
| 46 }, |
| 47 { "HTTP/1.1 200 OK\n" |
| 48 "connection: keep-alive\n" |
| 49 "Chrome-Proxy: bypass=86400\n" |
| 50 "Content-Length: 999\n", |
| 51 "bypass", |
| 52 true, |
| 53 "86400", |
| 54 }, |
| 55 { "HTTP/1.1 200 OK\n" |
| 56 "connection: keep-alive\n" |
| 57 "Chrome-Proxy: bypass86400\n" |
| 58 "Content-Length: 999\n", |
| 59 "bypass", |
| 60 false, |
| 61 "", |
| 62 }, |
| 63 { "HTTP/1.1 200 OK\n" |
| 64 "connection: keep-alive\n" |
| 65 "Chrome-Proxy: bypass=0\n" |
| 66 "Content-Length: 999\n", |
| 67 "bypass", |
| 68 true, |
| 69 "0", |
| 70 }, |
| 71 { "HTTP/1.1 200 OK\n" |
| 72 "connection: keep-alive\n" |
| 73 "Chrome-Proxy: bypass=1500\n" |
| 74 "Chrome-Proxy: bypass=86400\n" |
| 75 "Content-Length: 999\n", |
| 76 "bypass", |
| 77 true, |
| 78 "1500", |
| 79 }, |
| 80 { "HTTP/1.1 200 OK\n" |
| 81 "connection: keep-alive\n" |
| 82 "Chrome-Proxy: block=1500, block=3600\n" |
| 83 "Content-Length: 999\n", |
| 84 "block", |
| 85 true, |
| 86 "1500", |
| 87 }, |
| 88 { "HTTP/1.1 200 OK\n" |
| 89 "connection: proxy-bypass\n" |
| 90 "Chrome-Proxy: key=123 \n" |
| 91 "Content-Length: 999\n", |
| 92 "key", |
| 93 true, |
| 94 "123", |
| 95 }, |
| 96 }; |
| 97 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 98 std::string headers(tests[i].headers); |
| 99 HeadersToRaw(&headers); |
| 100 scoped_refptr<net::HttpResponseHeaders> parsed( |
| 101 new net::HttpResponseHeaders(headers)); |
| 102 |
| 103 std::string action_value; |
| 104 bool has_action_key = GetDataReductionProxyActionValue( |
| 105 parsed, tests[i].action_key, &action_value); |
| 106 EXPECT_EQ(tests[i].expected_result, has_action_key); |
| 107 if (has_action_key) { |
| 108 EXPECT_EQ(tests[i].expected_action_value, action_value); |
| 109 } |
| 110 } |
| 111 } |
| 112 |
27 TEST_F(DataReductionProxyHeadersTest, GetProxyBypassInfo) { | 113 TEST_F(DataReductionProxyHeadersTest, GetProxyBypassInfo) { |
28 const struct { | 114 const struct { |
29 const char* headers; | 115 const char* headers; |
30 bool expected_result; | 116 bool expected_result; |
31 int64 expected_retry_delay; | 117 int64 expected_retry_delay; |
32 bool expected_bypass_all; | 118 bool expected_bypass_all; |
33 } tests[] = { | 119 } tests[] = { |
34 { "HTTP/1.1 200 OK\n" | 120 { "HTTP/1.1 200 OK\n" |
35 "Content-Length: 999\n", | 121 "Content-Length: 999\n", |
36 false, | 122 false, |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 | 281 |
196 DataReductionProxyInfo data_reduction_proxy_info; | 282 DataReductionProxyInfo data_reduction_proxy_info; |
197 EXPECT_TRUE(ParseHeadersAndSetProxyInfo(parsed, &data_reduction_proxy_info)); | 283 EXPECT_TRUE(ParseHeadersAndSetProxyInfo(parsed, &data_reduction_proxy_info)); |
198 EXPECT_LE(60, data_reduction_proxy_info.bypass_duration.InSeconds()); | 284 EXPECT_LE(60, data_reduction_proxy_info.bypass_duration.InSeconds()); |
199 EXPECT_GE(5 * 60, data_reduction_proxy_info.bypass_duration.InSeconds()); | 285 EXPECT_GE(5 * 60, data_reduction_proxy_info.bypass_duration.InSeconds()); |
200 EXPECT_FALSE(data_reduction_proxy_info.bypass_all); | 286 EXPECT_FALSE(data_reduction_proxy_info.bypass_all); |
201 } | 287 } |
202 | 288 |
203 TEST_F(DataReductionProxyHeadersTest, HasDataReductionProxyViaHeader) { | 289 TEST_F(DataReductionProxyHeadersTest, HasDataReductionProxyViaHeader) { |
204 const struct { | 290 const struct { |
205 const char* headers; | 291 const char* headers; |
206 bool expected_result; | 292 bool expected_result; |
| 293 bool expected_has_intermediary; |
| 294 bool is_has_intermediary_null; |
207 } tests[] = { | 295 } tests[] = { |
208 { "HTTP/1.1 200 OK\n" | 296 { "HTTP/1.1 200 OK\n" |
209 "Via: 1.1 Chrome-Proxy\n", | 297 "Via: 1.1 Chrome-Proxy\n", |
210 false, | 298 false, |
| 299 false, |
| 300 false, |
211 }, | 301 }, |
212 { "HTTP/1.1 200 OK\n" | 302 { "HTTP/1.1 200 OK\n" |
213 "Via: 1\n", | 303 "Via: 1\n", |
214 false, | 304 false, |
| 305 false, |
| 306 false, |
215 }, | 307 }, |
216 { "HTTP/1.1 200 OK\n" | 308 { "HTTP/1.1 200 OK\n" |
217 "Via: 1.1 Chrome-Compression-Proxy\n", | 309 "Via: 1.1 Chrome-Compression-Proxy\n", |
218 true, | 310 true, |
| 311 true, |
| 312 false, |
219 }, | 313 }, |
220 { "HTTP/1.1 200 OK\n" | 314 { "HTTP/1.1 200 OK\n" |
221 "Via: 1.0 Chrome-Compression-Proxy\n", | 315 "Via: 1.0 Chrome-Compression-Proxy\n", |
222 true, | 316 true, |
| 317 true, |
| 318 false, |
223 }, | 319 }, |
224 { "HTTP/1.1 200 OK\n" | 320 { "HTTP/1.1 200 OK\n" |
225 "Via: 1.1 Foo-Bar, 1.1 Chrome-Compression-Proxy\n", | 321 "Via: 1.1 Foo-Bar, 1.1 Chrome-Compression-Proxy\n", |
226 true, | 322 true, |
| 323 true, |
| 324 false, |
227 }, | 325 }, |
228 { "HTTP/1.1 200 OK\n" | 326 { "HTTP/1.1 200 OK\n" |
229 "Via: 1.1 Chrome-Compression-Proxy, 1.1 Bar-Foo\n", | 327 "Via: 1.1 Chrome-Compression-Proxy, 1.1 Bar-Foo\n", |
230 true, | 328 true, |
| 329 false, |
| 330 false, |
231 }, | 331 }, |
232 { "HTTP/1.1 200 OK\n" | 332 { "HTTP/1.1 200 OK\n" |
233 "Via: 1.1 chrome-compression-proxy\n", | 333 "Via: 1.1 chrome-compression-proxy\n", |
234 false, | 334 false, |
| 335 false, |
| 336 false, |
235 }, | 337 }, |
236 { "HTTP/1.1 200 OK\n" | 338 { "HTTP/1.1 200 OK\n" |
237 "Via: 1.1 Foo-Bar\n" | 339 "Via: 1.1 Foo-Bar\n" |
238 "Via: 1.1 Chrome-Compression-Proxy\n", | 340 "Via: 1.1 Chrome-Compression-Proxy\n", |
239 true, | 341 true, |
| 342 true, |
| 343 false, |
| 344 }, |
| 345 { "HTTP/1.1 200 OK\n" |
| 346 "Via: 1.1 Chrome-Compression-Proxy\n" |
| 347 "Via: 1.1 Foo-Bar\n", |
| 348 true, |
| 349 false, |
| 350 false, |
240 }, | 351 }, |
241 { "HTTP/1.1 200 OK\n" | 352 { "HTTP/1.1 200 OK\n" |
242 "Via: 1.1 Chrome-Proxy\n", | 353 "Via: 1.1 Chrome-Proxy\n", |
243 false, | 354 false, |
| 355 false, |
| 356 false, |
244 }, | 357 }, |
245 { "HTTP/1.1 200 OK\n" | 358 { "HTTP/1.1 200 OK\n" |
246 "Via: 1.1 Chrome Compression Proxy\n", | 359 "Via: 1.1 Chrome Compression Proxy\n", |
247 true, | 360 true, |
| 361 true, |
| 362 false, |
248 }, | 363 }, |
249 { "HTTP/1.1 200 OK\n" | 364 { "HTTP/1.1 200 OK\n" |
250 "Via: 1.1 Foo-Bar, 1.1 Chrome Compression Proxy\n", | 365 "Via: 1.1 Foo-Bar, 1.1 Chrome Compression Proxy\n", |
251 true, | 366 true, |
| 367 true, |
| 368 false, |
252 }, | 369 }, |
253 { "HTTP/1.1 200 OK\n" | 370 { "HTTP/1.1 200 OK\n" |
254 "Via: 1.1 Chrome Compression Proxy, 1.1 Bar-Foo\n", | 371 "Via: 1.1 Chrome Compression Proxy, 1.1 Bar-Foo\n", |
255 true, | 372 true, |
| 373 false, |
| 374 false, |
256 }, | 375 }, |
257 { "HTTP/1.1 200 OK\n" | 376 { "HTTP/1.1 200 OK\n" |
258 "Via: 1.1 chrome compression proxy\n", | 377 "Via: 1.1 chrome compression proxy\n", |
259 false, | 378 false, |
| 379 false, |
| 380 false, |
260 }, | 381 }, |
261 { "HTTP/1.1 200 OK\n" | 382 { "HTTP/1.1 200 OK\n" |
262 "Via: 1.1 Foo-Bar\n" | 383 "Via: 1.1 Foo-Bar\n" |
263 "Via: 1.1 Chrome Compression Proxy\n", | 384 "Via: 1.1 Chrome Compression Proxy\n", |
264 true, | 385 true, |
| 386 true, |
| 387 false, |
| 388 }, |
| 389 { "HTTP/1.1 200 OK\n" |
| 390 "Via: 1.1 Chrome Compression Proxy\n" |
| 391 "Via: 1.1 Foo-Bar\n", |
| 392 true, |
| 393 false, |
| 394 false, |
| 395 }, |
| 396 { "HTTP/1.1 200 OK\n" |
| 397 "Via: 1.1 Chrome Compression Proxy\n" |
| 398 "Via: 1.1 Foo-Bar\n", |
| 399 true, |
| 400 false, |
| 401 true, |
265 }, | 402 }, |
266 }; | 403 }; |
267 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 404 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
268 std::string headers(tests[i].headers); | 405 std::string headers(tests[i].headers); |
269 HeadersToRaw(&headers); | 406 HeadersToRaw(&headers); |
270 scoped_refptr<net::HttpResponseHeaders> parsed( | 407 scoped_refptr<net::HttpResponseHeaders> parsed( |
271 new net::HttpResponseHeaders(headers)); | 408 new net::HttpResponseHeaders(headers)); |
272 | 409 |
273 EXPECT_EQ(tests[i].expected_result, | 410 bool has, has_intermediary; |
274 HasDataReductionProxyViaHeader(parsed)); | 411 if (tests[i].is_has_intermediary_null) |
| 412 has = HasDataReductionProxyViaHeader(parsed, NULL); |
| 413 else |
| 414 has = HasDataReductionProxyViaHeader(parsed, &has_intermediary); |
| 415 EXPECT_EQ(tests[i].expected_result, has); |
| 416 if (has && !tests[i].is_has_intermediary_null) { |
| 417 EXPECT_EQ(tests[i].expected_has_intermediary, has_intermediary); |
| 418 } |
275 } | 419 } |
276 } | 420 } |
277 | 421 |
278 TEST_F(DataReductionProxyHeadersTest, GetDataReductionProxyBypassEventType) { | 422 TEST_F(DataReductionProxyHeadersTest, GetDataReductionProxyBypassEventType) { |
279 const struct { | 423 const struct { |
280 const char* headers; | 424 const char* headers; |
281 net::ProxyService::DataReductionProxyBypassType expected_result; | 425 net::ProxyService::DataReductionProxyBypassType expected_result; |
282 } tests[] = { | 426 } tests[] = { |
283 { "HTTP/1.1 200 OK\n" | 427 { "HTTP/1.1 200 OK\n" |
284 "Chrome-Proxy: bypass=0\n" | 428 "Chrome-Proxy: bypass=0\n" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 518 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
375 std::string headers(tests[i].headers); | 519 std::string headers(tests[i].headers); |
376 HeadersToRaw(&headers); | 520 HeadersToRaw(&headers); |
377 scoped_refptr<net::HttpResponseHeaders> parsed( | 521 scoped_refptr<net::HttpResponseHeaders> parsed( |
378 new net::HttpResponseHeaders(headers)); | 522 new net::HttpResponseHeaders(headers)); |
379 DataReductionProxyInfo chrome_proxy_info; | 523 DataReductionProxyInfo chrome_proxy_info; |
380 EXPECT_EQ(tests[i].expected_result, | 524 EXPECT_EQ(tests[i].expected_result, |
381 GetDataReductionProxyBypassType(parsed, &chrome_proxy_info)); | 525 GetDataReductionProxyBypassType(parsed, &chrome_proxy_info)); |
382 } | 526 } |
383 } | 527 } |
| 528 |
| 529 TEST_F(DataReductionProxyHeadersTest, |
| 530 GetDataReductionProxyHeaderWithFingerprintRemoved) { |
| 531 struct { |
| 532 std::string label; |
| 533 const char* headers; |
| 534 std::string expected_output_values_string; |
| 535 } test[] = { |
| 536 { |
| 537 "Checks the case that there is no Chrome-Proxy header's fingerprint.", |
| 538 "HTTP/1.1 200 OK\n" |
| 539 "Chrome-Proxy: 1,2,3,5\n", |
| 540 "1,2,3,5,", |
| 541 }, |
| 542 { |
| 543 "Checks the case that there is Chrome-Proxy header's fingerprint.", |
| 544 "HTTP/1.1 200 OK\n" |
| 545 "Chrome-Proxy: 1,2,3,fcp=4,5\n", |
| 546 "1,2,3,5,", |
| 547 }, |
| 548 { |
| 549 "Checks the case that there is Chrome-Proxy header's fingerprint, and it" |
| 550 "occurs at the end.", |
| 551 "HTTP/1.1 200 OK\n" |
| 552 "Chrome-Proxy: 1,2,3,fcp=4,", |
| 553 "1,2,3,", |
| 554 }, |
| 555 { |
| 556 "Checks the case that there is Chrome-Proxy header's fingerprint, and it" |
| 557 "occurs at the beginning.", |
| 558 "HTTP/1.1 200 OK\n" |
| 559 "Chrome-Proxy: fcp=1,2,3,", |
| 560 "2,3,", |
| 561 }, |
| 562 }; |
| 563 |
| 564 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test); ++i) { |
| 565 std::string headers(test[i].headers); |
| 566 HeadersToRaw(&headers); |
| 567 scoped_refptr<net::HttpResponseHeaders> parsed( |
| 568 new net::HttpResponseHeaders(headers)); |
| 569 |
| 570 std::vector<std::string> output_values; |
| 571 GetDataReductionProxyHeaderWithFingerprintRemoved(parsed, &output_values); |
| 572 |
| 573 std::string output_values_string; |
| 574 for (size_t j = 0; j < output_values.size(); ++j) |
| 575 output_values_string += output_values[j] + ","; |
| 576 |
| 577 EXPECT_EQ(test[i].expected_output_values_string, output_values_string) |
| 578 << test[i].label; |
| 579 } |
| 580 } |
| 581 |
384 } // namespace data_reduction_proxy | 582 } // namespace data_reduction_proxy |
OLD | NEW |