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

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

Issue 8102019: redesign and reimplement proxy config service and tracker, revise proxy ui on cros (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/net/pref_proxy_config_tracker_impl.h"
6
7 #include "base/command_line.h"
8 #include "base/file_path.h"
9 #include "base/message_loop.h"
10 #include "chrome/browser/net/chrome_url_request_context.h"
11 #include "chrome/browser/prefs/pref_service_mock_builder.h"
12 #include "chrome/browser/prefs/proxy_config_dictionary.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/test/base/testing_pref_service.h"
16 #include "content/test/test_browser_thread.h"
17 #include "net/proxy/proxy_config_service_common_unittest.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 using content::BrowserThread;
22 using testing::_;
23 using testing::Mock;
24
25 namespace {
26
27 const char kFixedPacUrl[] = "http://chromium.org/fixed_pac_url";
28
29 // Testing proxy config service that allows us to fire notifications at will.
30 class TestProxyConfigService : public net::ProxyConfigService {
31 public:
32 TestProxyConfigService(const net::ProxyConfig& config,
33 ConfigAvailability availability)
34 : config_(config),
35 availability_(availability) {}
36
37 void SetProxyConfig(const net::ProxyConfig config,
38 ConfigAvailability availability) {
39 config_ = config;
40 availability_ = availability;
41 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_,
42 OnProxyConfigChanged(config, availability));
43 }
44
45 private:
46 virtual void AddObserver(net::ProxyConfigService::Observer* observer) {
47 observers_.AddObserver(observer);
48 }
49
50 virtual void RemoveObserver(net::ProxyConfigService::Observer* observer) {
51 observers_.RemoveObserver(observer);
52 }
53
54 virtual net::ProxyConfigService::ConfigAvailability GetLatestProxyConfig(
55 net::ProxyConfig* config) {
56 *config = config_;
57 return availability_;
58 }
59
60 net::ProxyConfig config_;
61 ConfigAvailability availability_;
62 ObserverList<net::ProxyConfigService::Observer, true> observers_;
63 };
64
65 // A mock observer for capturing callbacks.
66 class MockObserver : public net::ProxyConfigService::Observer {
67 public:
68 MOCK_METHOD2(OnProxyConfigChanged,
69 void(const net::ProxyConfig&,
70 net::ProxyConfigService::ConfigAvailability));
71 };
72
73 template<typename TESTBASE>
74 class PrefProxyConfigTrackerImplTestBase : public TESTBASE {
75 protected:
76 PrefProxyConfigTrackerImplTestBase()
77 : ui_thread_(BrowserThread::UI, &loop_),
78 io_thread_(BrowserThread::IO, &loop_) {}
79
80 virtual void Init(PrefService* pref_service) {
81 ASSERT_TRUE(pref_service);
82 PrefProxyConfigTrackerImpl::RegisterPrefs(pref_service);
83 fixed_config_.set_pac_url(GURL(kFixedPacUrl));
84 delegate_service_ =
85 new TestProxyConfigService(fixed_config_,
86 net::ProxyConfigService::CONFIG_VALID);
87 proxy_config_service_.reset(
88 new ChromeProxyConfigService(delegate_service_));
89 proxy_config_tracker_.reset(new PrefProxyConfigTrackerImpl(pref_service));
90 proxy_config_tracker_->SetChromeProxyConfigService(
91 proxy_config_service_.get());
92 // SetChromeProxyConfigService triggers update of initial prefs proxy
93 // config by tracker to chrome proxy config service, so flush all pending
94 // tasks so that tests start fresh.
95 loop_.RunAllPending();
96 }
97
98 virtual void TearDown() {
99 proxy_config_tracker_->DetachFromPrefService();
100 loop_.RunAllPending();
101 proxy_config_tracker_.reset();
102 proxy_config_service_.reset();
103 }
104
105 MessageLoop loop_;
106 TestProxyConfigService* delegate_service_; // weak
107 scoped_ptr<ChromeProxyConfigService> proxy_config_service_;
108 net::ProxyConfig fixed_config_;
109
110 private:
111 scoped_ptr<PrefProxyConfigTrackerImpl> proxy_config_tracker_;
112 content::TestBrowserThread ui_thread_;
113 content::TestBrowserThread io_thread_;
114 };
115
116 class PrefProxyConfigTrackerImplTest
117 : public PrefProxyConfigTrackerImplTestBase<testing::Test> {
118 protected:
119 virtual void SetUp() {
120 pref_service_.reset(new TestingPrefService());
121 Init(pref_service_.get());
122 }
123
124 scoped_ptr<TestingPrefService> pref_service_;
125 };
126
127 TEST_F(PrefProxyConfigTrackerImplTest, BaseConfiguration) {
128 net::ProxyConfig actual_config;
129 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID,
130 proxy_config_service_->GetLatestProxyConfig(&actual_config));
131 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url());
132 }
133
134 TEST_F(PrefProxyConfigTrackerImplTest, DynamicPrefOverrides) {
135 pref_service_->SetManagedPref(
136 prefs::kProxy,
137 ProxyConfigDictionary::CreateFixedServers("http://example.com:3128", ""));
138 loop_.RunAllPending();
139
140 net::ProxyConfig actual_config;
141 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID,
142 proxy_config_service_->GetLatestProxyConfig(&actual_config));
143 EXPECT_FALSE(actual_config.auto_detect());
144 EXPECT_EQ(net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY,
145 actual_config.proxy_rules().type);
146 EXPECT_EQ(actual_config.proxy_rules().single_proxy,
147 net::ProxyServer::FromURI("http://example.com:3128",
148 net::ProxyServer::SCHEME_HTTP));
149
150 pref_service_->SetManagedPref(prefs::kProxy,
151 ProxyConfigDictionary::CreateAutoDetect());
152 loop_.RunAllPending();
153
154 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID,
155 proxy_config_service_->GetLatestProxyConfig(&actual_config));
156 EXPECT_TRUE(actual_config.auto_detect());
157 }
158
159 // Compares proxy configurations, but allows different identifiers.
160 MATCHER_P(ProxyConfigMatches, config, "") {
161 net::ProxyConfig reference(config);
162 reference.set_id(arg.id());
163 return reference.Equals(arg);
164 }
165
166 TEST_F(PrefProxyConfigTrackerImplTest, Observers) {
167 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID =
168 net::ProxyConfigService::CONFIG_VALID;
169 MockObserver observer;
170 proxy_config_service_->AddObserver(&observer);
171
172 // Firing the observers in the delegate should trigger a notification.
173 net::ProxyConfig config2;
174 config2.set_auto_detect(true);
175 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config2),
176 CONFIG_VALID)).Times(1);
177 delegate_service_->SetProxyConfig(config2, CONFIG_VALID);
178 loop_.RunAllPending();
179 Mock::VerifyAndClearExpectations(&observer);
180
181 // Override configuration, this should trigger a notification.
182 net::ProxyConfig pref_config;
183 pref_config.set_pac_url(GURL(kFixedPacUrl));
184
185 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(pref_config),
186 CONFIG_VALID)).Times(1);
187 pref_service_->SetManagedPref(
188 prefs::kProxy,
189 ProxyConfigDictionary::CreatePacScript(kFixedPacUrl, false));
190 loop_.RunAllPending();
191 Mock::VerifyAndClearExpectations(&observer);
192
193 // Since there are pref overrides, delegate changes should be ignored.
194 net::ProxyConfig config3;
195 config3.proxy_rules().ParseFromString("http=config3:80");
196 EXPECT_CALL(observer, OnProxyConfigChanged(_, _)).Times(0);
197 fixed_config_.set_auto_detect(true);
198 delegate_service_->SetProxyConfig(config3, CONFIG_VALID);
199 loop_.RunAllPending();
200 Mock::VerifyAndClearExpectations(&observer);
201
202 // Clear the override should switch back to the fixed configuration.
203 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config3),
204 CONFIG_VALID)).Times(1);
205 pref_service_->RemoveManagedPref(prefs::kProxy);
206 loop_.RunAllPending();
207 Mock::VerifyAndClearExpectations(&observer);
208
209 // Delegate service notifications should show up again.
210 net::ProxyConfig config4;
211 config4.proxy_rules().ParseFromString("socks:config4");
212 EXPECT_CALL(observer, OnProxyConfigChanged(ProxyConfigMatches(config4),
213 CONFIG_VALID)).Times(1);
214 delegate_service_->SetProxyConfig(config4, CONFIG_VALID);
215 loop_.RunAllPending();
216 Mock::VerifyAndClearExpectations(&observer);
217
218 proxy_config_service_->RemoveObserver(&observer);
219 }
220
221 TEST_F(PrefProxyConfigTrackerImplTest, Fallback) {
222 const net::ProxyConfigService::ConfigAvailability CONFIG_VALID =
223 net::ProxyConfigService::CONFIG_VALID;
224 MockObserver observer;
225 net::ProxyConfig actual_config;
226 delegate_service_->SetProxyConfig(net::ProxyConfig::CreateDirect(),
227 net::ProxyConfigService::CONFIG_UNSET);
228 proxy_config_service_->AddObserver(&observer);
229
230 // Prepare test data.
231 net::ProxyConfig recommended_config = net::ProxyConfig::CreateAutoDetect();
232 net::ProxyConfig user_config =
233 net::ProxyConfig::CreateFromCustomPacURL(GURL(kFixedPacUrl));
234
235 // Set a recommended pref.
236 EXPECT_CALL(observer,
237 OnProxyConfigChanged(ProxyConfigMatches(recommended_config),
238 CONFIG_VALID)).Times(1);
239 pref_service_->SetRecommendedPref(
240 prefs::kProxy,
241 ProxyConfigDictionary::CreateAutoDetect());
242 loop_.RunAllPending();
243 Mock::VerifyAndClearExpectations(&observer);
244 EXPECT_EQ(CONFIG_VALID,
245 proxy_config_service_->GetLatestProxyConfig(&actual_config));
246 EXPECT_TRUE(actual_config.Equals(recommended_config));
247
248 // Override in user prefs.
249 EXPECT_CALL(observer,
250 OnProxyConfigChanged(ProxyConfigMatches(user_config),
251 CONFIG_VALID)).Times(1);
252 pref_service_->SetManagedPref(
253 prefs::kProxy,
254 ProxyConfigDictionary::CreatePacScript(kFixedPacUrl, false));
255 loop_.RunAllPending();
256 Mock::VerifyAndClearExpectations(&observer);
257 EXPECT_EQ(CONFIG_VALID,
258 proxy_config_service_->GetLatestProxyConfig(&actual_config));
259 EXPECT_TRUE(actual_config.Equals(user_config));
260
261 // Go back to recommended pref.
262 EXPECT_CALL(observer,
263 OnProxyConfigChanged(ProxyConfigMatches(recommended_config),
264 CONFIG_VALID)).Times(1);
265 pref_service_->RemoveManagedPref(prefs::kProxy);
266 loop_.RunAllPending();
267 Mock::VerifyAndClearExpectations(&observer);
268 EXPECT_EQ(CONFIG_VALID,
269 proxy_config_service_->GetLatestProxyConfig(&actual_config));
270 EXPECT_TRUE(actual_config.Equals(recommended_config));
271
272 proxy_config_service_->RemoveObserver(&observer);
273 }
274
275 TEST_F(PrefProxyConfigTrackerImplTest, ExplicitSystemSettings) {
276 pref_service_->SetRecommendedPref(
277 prefs::kProxy,
278 ProxyConfigDictionary::CreateAutoDetect());
279 pref_service_->SetUserPref(
280 prefs::kProxy,
281 ProxyConfigDictionary::CreateSystem());
282 loop_.RunAllPending();
283
284 // Test if we actually use the system setting, which is |kFixedPacUrl|.
285 net::ProxyConfig actual_config;
286 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID,
287 proxy_config_service_->GetLatestProxyConfig(&actual_config));
288 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url());
289 }
290
291 // Test parameter object for testing command line proxy configuration.
292 struct CommandLineTestParams {
293 // Explicit assignment operator, so testing::TestWithParam works with MSVC.
294 CommandLineTestParams& operator=(const CommandLineTestParams& other) {
295 description = other.description;
296 for (unsigned int i = 0; i < arraysize(switches); i++)
297 switches[i] = other.switches[i];
298 is_null = other.is_null;
299 auto_detect = other.auto_detect;
300 pac_url = other.pac_url;
301 proxy_rules = other.proxy_rules;
302 return *this;
303 }
304
305 // Short description to identify the test.
306 const char* description;
307
308 // The command line to build a ProxyConfig from.
309 struct SwitchValue {
310 const char* name;
311 const char* value;
312 } switches[2];
313
314 // Expected outputs (fields of the ProxyConfig).
315 bool is_null;
316 bool auto_detect;
317 GURL pac_url;
318 net::ProxyRulesExpectation proxy_rules;
319 };
320
321 void PrintTo(const CommandLineTestParams& params, std::ostream* os) {
322 *os << params.description;
323 }
324
325 class PrefProxyConfigTrackerImplCommandLineTest
326 : public PrefProxyConfigTrackerImplTestBase<
327 testing::TestWithParam<CommandLineTestParams> > {
328 protected:
329 PrefProxyConfigTrackerImplCommandLineTest()
330 : command_line_(CommandLine::NO_PROGRAM) {}
331
332 virtual void SetUp() {
333 for (size_t i = 0; i < arraysize(GetParam().switches); i++) {
334 const char* name = GetParam().switches[i].name;
335 const char* value = GetParam().switches[i].value;
336 if (name && value)
337 command_line_.AppendSwitchASCII(name, value);
338 else if (name)
339 command_line_.AppendSwitch(name);
340 }
341 pref_service_.reset(
342 PrefServiceMockBuilder().WithCommandLine(&command_line_).Create());
343 Init(pref_service_.get());
344 }
345
346 private:
347 CommandLine command_line_;
348 scoped_ptr<PrefService> pref_service_;
349 };
350
351 TEST_P(PrefProxyConfigTrackerImplCommandLineTest, CommandLine) {
352 net::ProxyConfig config;
353 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID,
354 proxy_config_service_->GetLatestProxyConfig(&config));
355
356 if (GetParam().is_null) {
357 EXPECT_EQ(GURL(kFixedPacUrl), config.pac_url());
358 } else {
359 EXPECT_NE(GURL(kFixedPacUrl), config.pac_url());
360 EXPECT_EQ(GetParam().auto_detect, config.auto_detect());
361 EXPECT_EQ(GetParam().pac_url, config.pac_url());
362 EXPECT_TRUE(GetParam().proxy_rules.Matches(config.proxy_rules()));
363 }
364 }
365
366 static const CommandLineTestParams kCommandLineTestParams[] = {
367 {
368 "Empty command line",
369 // Input
370 { },
371 // Expected result
372 true, // is_null
373 false, // auto_detect
374 GURL(), // pac_url
375 net::ProxyRulesExpectation::Empty(),
376 },
377 {
378 "No proxy",
379 // Input
380 {
381 { switches::kNoProxyServer, NULL },
382 },
383 // Expected result
384 false, // is_null
385 false, // auto_detect
386 GURL(), // pac_url
387 net::ProxyRulesExpectation::Empty(),
388 },
389 {
390 "No proxy with extra parameters.",
391 // Input
392 {
393 { switches::kNoProxyServer, NULL },
394 { switches::kProxyServer, "http://proxy:8888" },
395 },
396 // Expected result
397 false, // is_null
398 false, // auto_detect
399 GURL(), // pac_url
400 net::ProxyRulesExpectation::Empty(),
401 },
402 {
403 "Single proxy.",
404 // Input
405 {
406 { switches::kProxyServer, "http://proxy:8888" },
407 },
408 // Expected result
409 false, // is_null
410 false, // auto_detect
411 GURL(), // pac_url
412 net::ProxyRulesExpectation::Single(
413 "proxy:8888", // single proxy
414 ""), // bypass rules
415 },
416 {
417 "Per scheme proxy.",
418 // Input
419 {
420 { switches::kProxyServer, "http=httpproxy:8888;ftp=ftpproxy:8889" },
421 },
422 // Expected result
423 false, // is_null
424 false, // auto_detect
425 GURL(), // pac_url
426 net::ProxyRulesExpectation::PerScheme(
427 "httpproxy:8888", // http
428 "", // https
429 "ftpproxy:8889", // ftp
430 ""), // bypass rules
431 },
432 {
433 "Per scheme proxy with bypass URLs.",
434 // Input
435 {
436 { switches::kProxyServer, "http=httpproxy:8888;ftp=ftpproxy:8889" },
437 { switches::kProxyBypassList,
438 ".google.com, foo.com:99, 1.2.3.4:22, 127.0.0.1/8" },
439 },
440 // Expected result
441 false, // is_null
442 false, // auto_detect
443 GURL(), // pac_url
444 net::ProxyRulesExpectation::PerScheme(
445 "httpproxy:8888", // http
446 "", // https
447 "ftpproxy:8889", // ftp
448 "*.google.com,foo.com:99,1.2.3.4:22,127.0.0.1/8"),
449 },
450 {
451 "Pac URL",
452 // Input
453 {
454 { switches::kProxyPacUrl, "http://wpad/wpad.dat" },
455 },
456 // Expected result
457 false, // is_null
458 false, // auto_detect
459 GURL("http://wpad/wpad.dat"), // pac_url
460 net::ProxyRulesExpectation::Empty(),
461 },
462 {
463 "Autodetect",
464 // Input
465 {
466 { switches::kProxyAutoDetect, NULL },
467 },
468 // Expected result
469 false, // is_null
470 true, // auto_detect
471 GURL(), // pac_url
472 net::ProxyRulesExpectation::Empty(),
473 },
474 };
475
476 INSTANTIATE_TEST_CASE_P(
477 PrefProxyConfigTrackerImplCommandLineTestInstance,
478 PrefProxyConfigTrackerImplCommandLineTest,
479 testing::ValuesIn(kCommandLineTestParams));
480
481 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698