Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: chrome/browser/net/chrome_network_delegate_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698