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

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: Minor fixes and refactoring from comments Created 8 years, 2 months 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 "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
21 namespace {
Bernhard Bauer 2012/10/22 09:50:18 Nit: unnecessary.
Sergiu 2012/10/30 01:08:11 Whoops. Done.
22 }
23
15 class ChromeNetworkDelegateTest : public testing::Test { 24 class ChromeNetworkDelegateTest : public testing::Test {
16 protected: 25 protected:
17 ChromeNetworkDelegateTest() 26 ChromeNetworkDelegateTest()
18 : forwarder_(new extensions::EventRouterForwarder()) { 27 : forwarder_(new extensions::EventRouterForwarder()) {
19 } 28 }
20 29
21 virtual void SetUp() OVERRIDE { 30 virtual void SetUp() OVERRIDE {
22 never_throttle_requests_original_value_ = 31 never_throttle_requests_original_value_ =
23 ChromeNetworkDelegate::g_never_throttle_requests_; 32 ChromeNetworkDelegate::g_never_throttle_requests_;
24 ChromeNetworkDelegate::g_never_throttle_requests_ = false; 33 ChromeNetworkDelegate::g_never_throttle_requests_ = false;
25 } 34 }
26 35
27 virtual void TearDown() OVERRIDE { 36 virtual void TearDown() OVERRIDE {
28 ChromeNetworkDelegate::g_never_throttle_requests_ = 37 ChromeNetworkDelegate::g_never_throttle_requests_ =
29 never_throttle_requests_original_value_; 38 never_throttle_requests_original_value_;
30 } 39 }
31 40
32 scoped_ptr<ChromeNetworkDelegate> CreateNetworkDelegate() { 41 scoped_ptr<ChromeNetworkDelegate> CreateNetworkDelegate() {
33 return scoped_ptr<ChromeNetworkDelegate>(new ChromeNetworkDelegate( 42 return scoped_ptr<ChromeNetworkDelegate>(new ChromeNetworkDelegate(
34 forwarder_.get(), NULL, NULL, NULL, NULL, NULL, &pref_member_, NULL, 43 forwarder_.get(), NULL, NULL, NULL, NULL, NULL, &pref_member_, NULL,
35 NULL)); 44 NULL, NULL));
36 } 45 }
37 46
38 // Implementation moved here for access to private bits. 47 // Implementation moved here for access to private bits.
39 void NeverThrottleLogicImpl() { 48 void NeverThrottleLogicImpl() {
40 scoped_ptr<ChromeNetworkDelegate> delegate(CreateNetworkDelegate()); 49 scoped_ptr<ChromeNetworkDelegate> delegate(CreateNetworkDelegate());
41 50
42 TestURLRequestContext context; 51 TestURLRequestContext context;
43 TestURLRequest extension_request( 52 TestURLRequest extension_request(
44 GURL("http://example.com/"), NULL, &context); 53 GURL("http://example.com/"), NULL, &context);
45 extension_request.set_first_party_for_cookies( 54 extension_request.set_first_party_for_cookies(
(...skipping 27 matching lines...) Expand all
73 bool never_throttle_requests_original_value_; 82 bool never_throttle_requests_original_value_;
74 MessageLoopForIO message_loop_; 83 MessageLoopForIO message_loop_;
75 84
76 scoped_refptr<extensions::EventRouterForwarder> forwarder_; 85 scoped_refptr<extensions::EventRouterForwarder> forwarder_;
77 BooleanPrefMember pref_member_; 86 BooleanPrefMember pref_member_;
78 }; 87 };
79 88
80 TEST_F(ChromeNetworkDelegateTest, NeverThrottleLogic) { 89 TEST_F(ChromeNetworkDelegateTest, NeverThrottleLogic) {
81 NeverThrottleLogicImpl(); 90 NeverThrottleLogicImpl();
82 } 91 }
92
93 class ChromeNetworkDelegateSafesearchTest : public testing::Test {
94 public:
95 scoped_ptr<net::NetworkDelegate> CreateNetworkDelegate() {
96 return scoped_ptr<net::NetworkDelegate>(new ChromeNetworkDelegate(
97 forwarder_.get(), NULL, NULL, NULL, NULL, NULL, &enable_referrers_,
98 NULL, &force_google_safesearch_, NULL));
99 }
100
101 void SetSafesearch(bool value) {
102 force_google_safesearch_.SetValue(value);
103 }
104
105 void SetDelegate(net::NetworkDelegate* delegate) {
106 context_.set_network_delegate(delegate);
107 }
108
109 protected:
110 ChromeNetworkDelegateSafesearchTest()
111 : forwarder_(new extensions::EventRouterForwarder()) {
112 }
113
114 virtual void SetUp() OVERRIDE {
115 prefs_.RegisterBooleanPref(prefs::kForceSafeSearch, false,
116 PrefService::UNSYNCABLE_PREF);
117 force_google_safesearch_.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 loop_.reset(new MessageLoopForIO());
124 }
125
126 // Verifies that the expected string is equal to the query part (between
127 // ? and #) of the url_string.
128 void CheckAddedParameters(const std::string& url_string,
129 std::string expected) {
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()->RunAllPending();
137
138 EXPECT_EQ(expected, 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_safesearch_;
147 scoped_ptr<net::URLRequest> request_;
148 TestURLRequestContext context_;
149 TestDelegate delegate_;
150 scoped_ptr<MessageLoopForIO> loop_;
151 };
152
153 TEST_F(ChromeNetworkDelegateSafesearchTest, SafesearchOn) {
154 // Tests with SafeSearch on, request parameters should be rewritten.
155 const std::string kSafeParameter = chrome::kSafeSearchSafeParameter;
156 const std::string kSsuiParameter = chrome::kSafeSearchSsuiParameter;
157 const std::string kBothParameters = kSafeParameter + "&" + kSsuiParameter;
158 SetSafesearch(true);
159 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate());
160 SetDelegate(delegate.get());
161
162 // Test the home page.
163 CheckAddedParameters("http://google.com/", kBothParameters);
164
165 // Test the search home page.
166 CheckAddedParameters("http://google.com/webhp",
167 kBothParameters);
168
169 // Test different valid search pages with parameters.
170 CheckAddedParameters("http://google.com/search?q=google",
171 "q=google&" + kBothParameters);
172
173 CheckAddedParameters("http://google.com/?q=google",
174 "q=google&" + kBothParameters);
175
176 CheckAddedParameters("http://google.com/webhp?q=google",
177 "q=google&" + kBothParameters);
178
179 // Test the valid pages with safe set to off.
180 CheckAddedParameters("http://google.com/search?q=google&safe=off",
181 "q=google&" + kBothParameters);
182
183 CheckAddedParameters("http://google.com/?q=google&safe=off",
184 "q=google&" + kBothParameters);
185
186 CheckAddedParameters("http://google.com/webhp?q=google&safe=off",
187 "q=google&" + kBothParameters);
188
189 // Test the home page, different TLDs.
190 CheckAddedParameters("http://google.de/", kBothParameters);
191 CheckAddedParameters("http://google.ro/", kBothParameters);
192 CheckAddedParameters("http://google.nl/", kBothParameters);
193
194 // Test the search home page, different TLD.
195 CheckAddedParameters("http://google.de/webhp", kBothParameters);
196
197 // Test the home page with parameters, different TLD.
198 CheckAddedParameters("http://google.de/search?q=google",
199 "q=google&" + kBothParameters);
200
201 // Test the search page with parameters, different TLD.
202 CheckAddedParameters("http://google.de/?q=google",
203 "q=google&" + kBothParameters);
204
205 // Test the search page with the parameters set.
206 CheckAddedParameters("http://google.de/?q=google&" + kBothParameters,
207 "q=google&" + kBothParameters);
208
209 // Test some possibly tricky combinations.
210 CheckAddedParameters("http://google.com/?q=goog&" + kSafeParameter +
211 "&ssui=one",
212 "q=goog&" + kBothParameters);
213
214 CheckAddedParameters("http://google.de/?q=goog&unsafe=active&" +
215 kSsuiParameter,
216 "q=goog&unsafe=active&" + kSsuiParameter + "&" +
217 kSafeParameter);
218
219 CheckAddedParameters("http://google.de/?q=goog&safe=off&ssui=off",
220 "q=goog&" + kSafeParameter + "&" + kSsuiParameter);
221
222 // Test various combinations where we should not add anything.
223 CheckAddedParameters("http://google.com/?q=goog&" + kSsuiParameter + "&" +
224 kSafeParameter,
225 "q=goog&" + kSsuiParameter + "&" + kSafeParameter);
226
227 CheckAddedParameters("http://google.com/?" + kSsuiParameter + "&q=goog&" +
228 kSafeParameter,
229 kSsuiParameter + "&q=goog&" + kSafeParameter);
230
231 CheckAddedParameters("http://google.com/?" + kSsuiParameter + "&" +
Bernhard Bauer 2012/10/22 09:50:18 Nit: I think it might be more readable if you just
Sergiu 2012/10/30 01:08:11 It's a bit simpler now but still uses variables...
232 kSafeParameter + "&q=goog",
233 kSsuiParameter + "&" + kSafeParameter + "&q=goog");
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 + "&param=%26%26%26",
247 "q=%26%26%26&" + kSsuiParameter + "&" +
248 kSafeParameter + "&param=%26%26%26");
249
250 }
251
252 TEST_F(ChromeNetworkDelegateSafesearchTest, SafesearchOff) {
253 // Tests with SafeSearch settings off, delegate should not alter requests.
254 SetSafesearch(false);
255 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate());
256 SetDelegate(delegate.get());
257
258 // Test the home page.
259 CheckAddedParameters("http://google.com/", "");
260
261 // Test the search home page.
262 CheckAddedParameters("http://google.com/webhp", "");
263
264 // Test the home page with parameters.
265 CheckAddedParameters("http://google.com/search?q=google",
266 "q=google");
267
268 // Test the search page with parameters.
269 CheckAddedParameters("http://google.com/?q=google",
270 "q=google");
271
272 // Test the search webhp page with parameters.
273 CheckAddedParameters("http://google.com/webhp?q=google",
274 "q=google");
275
276 // Test the home page with parameters and safe set to off.
277 CheckAddedParameters("http://google.com/search?q=google&safe=off",
278 "q=google&safe=off");
279
280 // Test the home page with parameters and safe set to active.
281 CheckAddedParameters("http://google.com/search?q=google&safe=active",
282 "q=google&safe=active");
283 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698