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/browser/data_reduction_proxy_protocol.
h" | 5 #include "components/data_reduction_proxy/browser/data_reduction_proxy_protocol.
h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h" | 13 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params_te
st_utils.h" |
14 #include "net/base/completion_callback.h" | 14 #include "net/base/completion_callback.h" |
15 #include "net/base/host_port_pair.h" | 15 #include "net/base/host_port_pair.h" |
16 #include "net/base/network_delegate.h" | 16 #include "net/base/network_delegate.h" |
17 #include "net/http/http_response_headers.h" | 17 #include "net/http/http_response_headers.h" |
18 #include "net/http/http_transaction_test_util.h" | 18 #include "net/http/http_transaction_test_util.h" |
19 #include "net/socket/socket_test_util.h" | 19 #include "net/socket/socket_test_util.h" |
20 #include "net/url_request/static_http_user_agent_settings.h" | 20 #include "net/url_request/static_http_user_agent_settings.h" |
21 #include "net/url_request/url_request.h" | 21 #include "net/url_request/url_request.h" |
22 #include "net/url_request/url_request_context.h" | 22 #include "net/url_request/url_request_context.h" |
23 #include "net/url_request/url_request_test_util.h" | 23 #include "net/url_request/url_request_test_util.h" |
(...skipping 13 matching lines...) Expand all Loading... |
37 | 37 |
38 namespace { | 38 namespace { |
39 // Transform "normal"-looking headers (\n-separated) to the appropriate | 39 // Transform "normal"-looking headers (\n-separated) to the appropriate |
40 // input format for ParseRawHeaders (\0-separated). | 40 // input format for ParseRawHeaders (\0-separated). |
41 void HeadersToRaw(std::string* headers) { | 41 void HeadersToRaw(std::string* headers) { |
42 std::replace(headers->begin(), headers->end(), '\n', '\0'); | 42 std::replace(headers->begin(), headers->end(), '\n', '\0'); |
43 if (!headers->empty()) | 43 if (!headers->empty()) |
44 *headers += '\0'; | 44 *headers += '\0'; |
45 } | 45 } |
46 | 46 |
47 std::string GetDataReductionProxy() { | |
48 return "https://proxy1.com:443/"; | |
49 } | |
50 | |
51 std::string GetDataReductionProxyFallback() { | |
52 return "http://proxy2.com:80/"; | |
53 } | |
54 } // namespace | 47 } // namespace |
55 | 48 |
56 | 49 |
57 namespace data_reduction_proxy { | 50 namespace data_reduction_proxy { |
58 namespace { | |
59 class TestDataReductionProxyParams : public DataReductionProxyParams { | |
60 public: | |
61 TestDataReductionProxyParams() : DataReductionProxyParams(0, false) {} | |
62 | |
63 virtual bool WasDataReductionProxyUsed( | |
64 const net::URLRequest* request, | |
65 std::pair<GURL, GURL>* proxy_servers) const OVERRIDE; | |
66 }; | |
67 | |
68 bool TestDataReductionProxyParams::WasDataReductionProxyUsed( | |
69 const net::URLRequest* request, | |
70 std::pair<GURL, GURL>* proxy_servers) const { | |
71 if (net::HostPortPair::FromURL(GURL(GetDataReductionProxy())).Equals( | |
72 request->proxy_server())) { | |
73 proxy_servers->first = GURL(GetDataReductionProxy()); | |
74 proxy_servers->second = GURL(GetDataReductionProxyFallback()); | |
75 return true; | |
76 } | |
77 if (net::HostPortPair::FromURL( | |
78 GURL(GetDataReductionProxyFallback())).Equals( | |
79 request->proxy_server())) { | |
80 proxy_servers->first = GURL(GetDataReductionProxyFallback()); | |
81 proxy_servers->second = GURL(); | |
82 return true; | |
83 } | |
84 return false; | |
85 } | |
86 } // namespace | |
87 | 51 |
88 // A test network delegate that exercises the bypass logic of the data | 52 // A test network delegate that exercises the bypass logic of the data |
89 // reduction proxy. | 53 // reduction proxy. |
90 class TestDataReductionProxyNetworkDelegate : public net::NetworkDelegate { | 54 class TestDataReductionProxyNetworkDelegate : public net::NetworkDelegate { |
91 public: | 55 public: |
92 TestDataReductionProxyNetworkDelegate( | 56 TestDataReductionProxyNetworkDelegate( |
93 TestDataReductionProxyParams* test_params) | 57 TestDataReductionProxyParams* test_params) |
94 : net::NetworkDelegate(), test_data_reduction_proxy_params_(test_params) { | 58 : net::NetworkDelegate(), test_data_reduction_proxy_params_(test_params) { |
95 } | 59 } |
96 | 60 |
(...skipping 11 matching lines...) Expand all Loading... |
108 return net::OK; | 72 return net::OK; |
109 } | 73 } |
110 | 74 |
111 TestDataReductionProxyParams* test_data_reduction_proxy_params_; | 75 TestDataReductionProxyParams* test_data_reduction_proxy_params_; |
112 }; | 76 }; |
113 | 77 |
114 // Constructs a |TestURLRequestContext| that uses a |MockSocketFactory| to | 78 // Constructs a |TestURLRequestContext| that uses a |MockSocketFactory| to |
115 // simulate requests and responses. | 79 // simulate requests and responses. |
116 class DataReductionProxyProtocolTest : public testing::Test { | 80 class DataReductionProxyProtocolTest : public testing::Test { |
117 public: | 81 public: |
118 DataReductionProxyProtocolTest() : http_user_agent_settings_("", "") {} | 82 DataReductionProxyProtocolTest() : http_user_agent_settings_("", "") { |
| 83 proxy_params_.reset( |
| 84 new TestDataReductionProxyParams( |
| 85 DataReductionProxyParams::kAllowed | |
| 86 DataReductionProxyParams::kFallbackAllowed | |
| 87 DataReductionProxyParams::kPromoAllowed, |
| 88 TestDataReductionProxyParams::HAS_EVERYTHING & |
| 89 ~TestDataReductionProxyParams::HAS_DEV_ORIGIN)); |
| 90 } |
119 | 91 |
120 // Sets up the |TestURLRequestContext| with the provided |ProxyService|. | 92 // Sets up the |TestURLRequestContext| with the provided |ProxyService|. |
121 void ConfigureTestDependencies(ProxyService* proxy_service) { | 93 void ConfigureTestDependencies(ProxyService* proxy_service) { |
122 // Create a context with delayed initialization. | 94 // Create a context with delayed initialization. |
123 context_.reset(new TestURLRequestContext(true)); | 95 context_.reset(new TestURLRequestContext(true)); |
124 | 96 |
125 proxy_service_.reset(proxy_service); | 97 proxy_service_.reset(proxy_service); |
126 proxy_params_.reset(new TestDataReductionProxyParams()); | |
127 network_delegate_.reset(new TestDataReductionProxyNetworkDelegate( | 98 network_delegate_.reset(new TestDataReductionProxyNetworkDelegate( |
128 proxy_params_.get())); | 99 proxy_params_.get())); |
129 | 100 |
130 context_->set_client_socket_factory(&mock_socket_factory_); | 101 context_->set_client_socket_factory(&mock_socket_factory_); |
131 context_->set_proxy_service(proxy_service_.get()); | 102 context_->set_proxy_service(proxy_service_.get()); |
132 context_->set_network_delegate(network_delegate_.get()); | 103 context_->set_network_delegate(network_delegate_.get()); |
133 // This is needed to prevent the test context from adding language headers | 104 // This is needed to prevent the test context from adding language headers |
134 // to requests. | 105 // to requests. |
135 context_->set_http_user_agent_settings(&http_user_agent_settings_); | 106 context_->set_http_user_agent_settings(&http_user_agent_settings_); |
136 | 107 |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 } | 342 } |
372 } | 343 } |
373 | 344 |
374 | 345 |
375 // After each test, the proxy retry info will contain zero, one, or two of the | 346 // After each test, the proxy retry info will contain zero, one, or two of the |
376 // data reduction proxies depending on whether no bypass was indicated by the | 347 // data reduction proxies depending on whether no bypass was indicated by the |
377 // initial response, a single proxy bypass was indicated, or a double bypass | 348 // initial response, a single proxy bypass was indicated, or a double bypass |
378 // was indicated. In both the single and double bypass cases, if the request | 349 // was indicated. In both the single and double bypass cases, if the request |
379 // was idempotent, it will be retried over a direct connection. | 350 // was idempotent, it will be retried over a direct connection. |
380 TEST_F(DataReductionProxyProtocolTest, BypassLogic) { | 351 TEST_F(DataReductionProxyProtocolTest, BypassLogic) { |
381 std::string primary = GetDataReductionProxy(); | |
382 std::string fallback = GetDataReductionProxyFallback(); | |
383 const struct { | 352 const struct { |
384 const char* method; | 353 const char* method; |
385 const char* first_response; | 354 const char* first_response; |
386 bool expected_retry; | 355 bool expected_retry; |
387 size_t expected_bad_proxy_count; | 356 size_t expected_bad_proxy_count; |
388 bool expect_response_body; | 357 bool expect_response_body; |
389 int expected_duration; | 358 int expected_duration; |
390 } tests[] = { | 359 } tests[] = { |
391 // Valid data reduction proxy response with no bypass message. | 360 // Valid data reduction proxy response with no bypass message. |
392 { "GET", | 361 { "GET", |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
573 "HTTP/1.1 200 OK\r\n" | 542 "HTTP/1.1 200 OK\r\n" |
574 "Server: proxy\r\n" | 543 "Server: proxy\r\n" |
575 "Chrome-Proxy: block=1\r\n" | 544 "Chrome-Proxy: block=1\r\n" |
576 "Via: 1.1 Chrome-Compression-Proxy\r\n\r\n", | 545 "Via: 1.1 Chrome-Compression-Proxy\r\n\r\n", |
577 true, | 546 true, |
578 2u, | 547 2u, |
579 true, | 548 true, |
580 1 | 549 1 |
581 } | 550 } |
582 }; | 551 }; |
| 552 std::string primary = proxy_params_->DefaultOrigin(); |
| 553 std::string fallback = proxy_params_->DefaultFallbackOrigin(); |
583 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 554 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
584 ConfigureTestDependencies( | 555 ConfigureTestDependencies(ProxyService::CreateFixedFromPacResult( |
585 ProxyService::CreateFixedFromPacResult("PROXY " + | 556 "PROXY " + |
586 HostPortPair::FromURL(GURL(primary)).ToString() + "; PROXY " + | 557 HostPortPair::FromURL(GURL(primary)).ToString() + "; PROXY " + |
587 HostPortPair::FromURL(GURL(fallback)).ToString() + "; DIRECT")); | 558 HostPortPair::FromURL(GURL(fallback)).ToString() + "; DIRECT")); |
588 TestProxyFallback(tests[i].method, | 559 TestProxyFallback(tests[i].method, |
589 tests[i].first_response, | 560 tests[i].first_response, |
590 tests[i].expected_retry, | 561 tests[i].expected_retry, |
591 tests[i].expect_response_body); | 562 tests[i].expect_response_body); |
592 | 563 |
593 // We should also observe the bad proxy in the retry list. | 564 // We should also observe the bad proxy in the retry list. |
594 TestBadProxies(tests[i].expected_bad_proxy_count, | 565 TestBadProxies(tests[i].expected_bad_proxy_count, |
595 tests[i].expected_duration, | 566 tests[i].expected_duration, |
596 primary, fallback); | 567 primary, fallback); |
597 } | 568 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
634 EXPECT_EQ(net::URLRequestStatus::SUCCESS, r.status().status()); | 605 EXPECT_EQ(net::URLRequestStatus::SUCCESS, r.status().status()); |
635 EXPECT_EQ(net::OK, r.status().error()); | 606 EXPECT_EQ(net::OK, r.status().error()); |
636 | 607 |
637 EXPECT_EQ("Bypass message", d.data_received()); | 608 EXPECT_EQ("Bypass message", d.data_received()); |
638 | 609 |
639 // We should have no entries in our bad proxy list. | 610 // We should have no entries in our bad proxy list. |
640 TestBadProxies(0, -1, "", ""); | 611 TestBadProxies(0, -1, "", ""); |
641 } | 612 } |
642 | 613 |
643 } // namespace data_reduction_proxy | 614 } // namespace data_reduction_proxy |
OLD | NEW |