OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/net/chrome_network_delegate.h" | 5 #include "chrome/browser/net/chrome_network_delegate.h" |
6 | 6 |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "chrome/browser/api/prefs/pref_member.h" | 10 #include "chrome/browser/api/prefs/pref_member.h" |
11 #include "chrome/browser/extensions/event_router_forwarder.h" | 11 #include "chrome/browser/extensions/event_router_forwarder.h" |
| 12 #include "chrome/common/pref_names.h" |
| 13 #include "chrome/common/url_constants.h" |
| 14 #include "chrome/test/base/testing_pref_service.h" |
| 15 #include "chrome/test/base/testing_profile.h" |
| 16 #include "net/base/completion_callback.h" |
| 17 #include "net/url_request/url_request.h" |
12 #include "net/url_request/url_request_test_util.h" | 18 #include "net/url_request/url_request_test_util.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
14 | 20 |
15 class ChromeNetworkDelegateTest : public testing::Test { | 21 class ChromeNetworkDelegateTest : public testing::Test { |
16 protected: | 22 protected: |
17 ChromeNetworkDelegateTest() | 23 ChromeNetworkDelegateTest() |
18 : forwarder_(new extensions::EventRouterForwarder()) { | 24 : forwarder_(new extensions::EventRouterForwarder()) { |
19 } | 25 } |
20 | 26 |
21 virtual void SetUp() OVERRIDE { | 27 virtual void SetUp() OVERRIDE { |
22 never_throttle_requests_original_value_ = | 28 never_throttle_requests_original_value_ = |
23 ChromeNetworkDelegate::g_never_throttle_requests_; | 29 ChromeNetworkDelegate::g_never_throttle_requests_; |
24 ChromeNetworkDelegate::g_never_throttle_requests_ = false; | 30 ChromeNetworkDelegate::g_never_throttle_requests_ = false; |
25 } | 31 } |
26 | 32 |
27 virtual void TearDown() OVERRIDE { | 33 virtual void TearDown() OVERRIDE { |
28 ChromeNetworkDelegate::g_never_throttle_requests_ = | 34 ChromeNetworkDelegate::g_never_throttle_requests_ = |
29 never_throttle_requests_original_value_; | 35 never_throttle_requests_original_value_; |
30 } | 36 } |
31 | 37 |
32 scoped_ptr<ChromeNetworkDelegate> CreateNetworkDelegate() { | 38 scoped_ptr<ChromeNetworkDelegate> CreateNetworkDelegate() { |
33 return scoped_ptr<ChromeNetworkDelegate>(new ChromeNetworkDelegate( | 39 return scoped_ptr<ChromeNetworkDelegate>(new ChromeNetworkDelegate( |
34 forwarder_.get(), NULL, NULL, NULL, NULL, NULL, &pref_member_, NULL, | 40 forwarder_.get(), NULL, NULL, NULL, NULL, NULL, &pref_member_, NULL, |
35 NULL)); | 41 NULL, NULL)); |
36 } | 42 } |
37 | 43 |
38 // Implementation moved here for access to private bits. | 44 // Implementation moved here for access to private bits. |
39 void NeverThrottleLogicImpl() { | 45 void NeverThrottleLogicImpl() { |
40 scoped_ptr<ChromeNetworkDelegate> delegate(CreateNetworkDelegate()); | 46 scoped_ptr<ChromeNetworkDelegate> delegate(CreateNetworkDelegate()); |
41 | 47 |
42 TestURLRequestContext context; | 48 TestURLRequestContext context; |
43 TestURLRequest extension_request( | 49 TestURLRequest extension_request( |
44 GURL("http://example.com/"), NULL, &context); | 50 GURL("http://example.com/"), NULL, &context); |
45 extension_request.set_first_party_for_cookies( | 51 extension_request.set_first_party_for_cookies( |
(...skipping 27 matching lines...) Expand all Loading... |
73 bool never_throttle_requests_original_value_; | 79 bool never_throttle_requests_original_value_; |
74 MessageLoopForIO message_loop_; | 80 MessageLoopForIO message_loop_; |
75 | 81 |
76 scoped_refptr<extensions::EventRouterForwarder> forwarder_; | 82 scoped_refptr<extensions::EventRouterForwarder> forwarder_; |
77 BooleanPrefMember pref_member_; | 83 BooleanPrefMember pref_member_; |
78 }; | 84 }; |
79 | 85 |
80 TEST_F(ChromeNetworkDelegateTest, NeverThrottleLogic) { | 86 TEST_F(ChromeNetworkDelegateTest, NeverThrottleLogic) { |
81 NeverThrottleLogicImpl(); | 87 NeverThrottleLogicImpl(); |
82 } | 88 } |
| 89 |
| 90 class ChromeNetworkDelegateSafeSearchTest : public testing::Test { |
| 91 public: |
| 92 scoped_ptr<net::NetworkDelegate> CreateNetworkDelegate() { |
| 93 return scoped_ptr<net::NetworkDelegate>(new ChromeNetworkDelegate( |
| 94 forwarder_.get(), NULL, NULL, NULL, NULL, NULL, &enable_referrers_, |
| 95 NULL, &force_google_safe_search_, NULL)); |
| 96 } |
| 97 |
| 98 void SetSafeSearch(bool value) { |
| 99 force_google_safe_search_.SetValue(value); |
| 100 } |
| 101 |
| 102 void SetDelegate(net::NetworkDelegate* delegate) { |
| 103 context_.set_network_delegate(delegate); |
| 104 } |
| 105 |
| 106 protected: |
| 107 ChromeNetworkDelegateSafeSearchTest() |
| 108 : forwarder_(new extensions::EventRouterForwarder()) { |
| 109 } |
| 110 |
| 111 virtual void SetUp() OVERRIDE { |
| 112 prefs_.RegisterBooleanPref(prefs::kForceSafeSearch, false, |
| 113 PrefService::UNSYNCABLE_PREF); |
| 114 force_google_safe_search_.Init(prefs::kForceSafeSearch, |
| 115 profile_.GetTestingPrefService(), NULL); |
| 116 prefs_.RegisterBooleanPref(prefs::kEnableReferrers, false, |
| 117 PrefService::UNSYNCABLE_PREF); |
| 118 enable_referrers_.Init(prefs::kEnableReferrers, |
| 119 profile_.GetTestingPrefService(), NULL); |
| 120 loop_.reset(new MessageLoopForIO()); |
| 121 } |
| 122 |
| 123 // Does a request using the |url_string| URL and verifies that the expected |
| 124 // string is equal to the query part (between ? and #) of the final url of |
| 125 // that request. |
| 126 void CheckAddedParameters(const std::string& url_string, |
| 127 const std::string& expected_query_parameters) { |
| 128 // Show the URL in the trace so we know where we failed. |
| 129 SCOPED_TRACE(url_string); |
| 130 |
| 131 TestURLRequest request(GURL(url_string), &delegate_, &context_); |
| 132 |
| 133 request.Start(); |
| 134 MessageLoop::current()->RunAllPending(); |
| 135 |
| 136 EXPECT_EQ(expected_query_parameters, request.url().query()); |
| 137 } |
| 138 |
| 139 private: |
| 140 scoped_refptr<extensions::EventRouterForwarder> forwarder_; |
| 141 TestingProfile profile_; |
| 142 TestingPrefService prefs_; |
| 143 BooleanPrefMember enable_referrers_; |
| 144 BooleanPrefMember force_google_safe_search_; |
| 145 scoped_ptr<net::URLRequest> request_; |
| 146 TestURLRequestContext context_; |
| 147 TestDelegate delegate_; |
| 148 scoped_ptr<MessageLoopForIO> loop_; |
| 149 }; |
| 150 |
| 151 TEST_F(ChromeNetworkDelegateSafeSearchTest, SafeSearchOn) { |
| 152 // Tests with SafeSearch on, request parameters should be rewritten. |
| 153 const std::string kSafeParameter = chrome::kSafeSearchSafeParameter; |
| 154 const std::string kSsuiParameter = chrome::kSafeSearchSsuiParameter; |
| 155 const std::string kBothParameters = kSafeParameter + "&" + kSsuiParameter; |
| 156 SetSafeSearch(true); |
| 157 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate()); |
| 158 SetDelegate(delegate.get()); |
| 159 |
| 160 // Test the home page. |
| 161 CheckAddedParameters("http://google.com/", kBothParameters); |
| 162 |
| 163 // Test the search home page. |
| 164 CheckAddedParameters("http://google.com/webhp", |
| 165 kBothParameters); |
| 166 |
| 167 // Test different valid search pages with parameters. |
| 168 CheckAddedParameters("http://google.com/search?q=google", |
| 169 "q=google&" + kBothParameters); |
| 170 |
| 171 CheckAddedParameters("http://google.com/?q=google", |
| 172 "q=google&" + kBothParameters); |
| 173 |
| 174 CheckAddedParameters("http://google.com/webhp?q=google", |
| 175 "q=google&" + kBothParameters); |
| 176 |
| 177 // Test the valid pages with safe set to off. |
| 178 CheckAddedParameters("http://google.com/search?q=google&safe=off", |
| 179 "q=google&" + kBothParameters); |
| 180 |
| 181 CheckAddedParameters("http://google.com/?q=google&safe=off", |
| 182 "q=google&" + kBothParameters); |
| 183 |
| 184 CheckAddedParameters("http://google.com/webhp?q=google&safe=off", |
| 185 "q=google&" + kBothParameters); |
| 186 |
| 187 CheckAddedParameters("http://google.com/webhp?q=google&%73afe=off", |
| 188 "q=google&%73afe=off&" + kBothParameters); |
| 189 |
| 190 // Test the home page, different TLDs. |
| 191 CheckAddedParameters("http://google.de/", kBothParameters); |
| 192 CheckAddedParameters("http://google.ro/", kBothParameters); |
| 193 CheckAddedParameters("http://google.nl/", kBothParameters); |
| 194 |
| 195 // Test the search home page, different TLD. |
| 196 CheckAddedParameters("http://google.de/webhp", kBothParameters); |
| 197 |
| 198 // Test the search page with parameters, different TLD. |
| 199 CheckAddedParameters("http://google.de/search?q=google", |
| 200 "q=google&" + kBothParameters); |
| 201 |
| 202 // Test the home page with parameters, different TLD. |
| 203 CheckAddedParameters("http://google.de/?q=google", |
| 204 "q=google&" + kBothParameters); |
| 205 |
| 206 // Test the search page with the parameters set. |
| 207 CheckAddedParameters("http://google.de/?q=google&" + kBothParameters, |
| 208 "q=google&" + kBothParameters); |
| 209 |
| 210 // Test some possibly tricky combinations. |
| 211 CheckAddedParameters("http://google.com/?q=goog&" + kSafeParameter + |
| 212 "&ssui=one", |
| 213 "q=goog&" + kBothParameters); |
| 214 |
| 215 CheckAddedParameters("http://google.de/?q=goog&unsafe=active&" + |
| 216 kSsuiParameter, |
| 217 "q=goog&unsafe=active&" + kBothParameters); |
| 218 |
| 219 CheckAddedParameters("http://google.de/?q=goog&safe=off&ssui=off", |
| 220 "q=goog&" + kBothParameters); |
| 221 |
| 222 // Test various combinations where we should not add anything. |
| 223 CheckAddedParameters("http://google.com/?q=goog&" + kSsuiParameter + "&" + |
| 224 kSafeParameter, |
| 225 "q=goog&" + kBothParameters); |
| 226 |
| 227 CheckAddedParameters("http://google.com/?" + kSsuiParameter + "&q=goog&" + |
| 228 kSafeParameter, |
| 229 "q=goog&" + kBothParameters); |
| 230 |
| 231 CheckAddedParameters("http://google.com/?" + kSsuiParameter + "&" + |
| 232 kSafeParameter + "&q=goog", |
| 233 "q=goog&" + kBothParameters); |
| 234 |
| 235 // Test that another website is not affected, without parameters. |
| 236 CheckAddedParameters("http://google.com/finance", ""); |
| 237 |
| 238 // Test that another website is not affected, with parameters. |
| 239 CheckAddedParameters("http://google.com/finance?q=goog", "q=goog"); |
| 240 |
| 241 // Test that another website is not affected with redirects, with parameters. |
| 242 CheckAddedParameters("http://finance.google.com/?q=goog", "q=goog"); |
| 243 |
| 244 // Test with percent-encoded data (%26 is &) |
| 245 CheckAddedParameters("http://google.com/?q=%26%26%26&" + kSsuiParameter + |
| 246 "&" + kSafeParameter + "¶m=%26%26%26", |
| 247 "q=%26%26%26¶m=%26%26%26&" + kBothParameters); |
| 248 } |
| 249 |
| 250 TEST_F(ChromeNetworkDelegateSafeSearchTest, SafeSearchOff) { |
| 251 // Tests with SafeSearch settings off, delegate should not alter requests. |
| 252 SetSafeSearch(false); |
| 253 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate()); |
| 254 SetDelegate(delegate.get()); |
| 255 |
| 256 // Test the home page. |
| 257 CheckAddedParameters("http://google.com/", ""); |
| 258 |
| 259 // Test the search home page. |
| 260 CheckAddedParameters("http://google.com/webhp", ""); |
| 261 |
| 262 // Test the home page with parameters. |
| 263 CheckAddedParameters("http://google.com/search?q=google", |
| 264 "q=google"); |
| 265 |
| 266 // Test the search page with parameters. |
| 267 CheckAddedParameters("http://google.com/?q=google", |
| 268 "q=google"); |
| 269 |
| 270 // Test the search webhp page with parameters. |
| 271 CheckAddedParameters("http://google.com/webhp?q=google", |
| 272 "q=google"); |
| 273 |
| 274 // Test the home page with parameters and safe set to off. |
| 275 CheckAddedParameters("http://google.com/search?q=google&safe=off", |
| 276 "q=google&safe=off"); |
| 277 |
| 278 // Test the home page with parameters and safe set to active. |
| 279 CheckAddedParameters("http://google.com/search?q=google&safe=active", |
| 280 "q=google&safe=active"); |
| 281 } |
OLD | NEW |