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

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

Issue 5005002: Dynamically refresh pref-configured proxies. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address Jochen's comments. Created 10 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
(Empty)
1 // Copyright (c) 2010 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_service.h"
6
7 #include "base/command_line.h"
8 #include "base/file_path.h"
9 #include "chrome/browser/net/chrome_url_request_context.h"
10 #include "chrome/common/chrome_switches.h"
11 #include "chrome/common/pref_names.h"
12 #include "chrome/test/testing_pref_service.h"
13 #include "net/proxy/proxy_config_service_common_unittest.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 using testing::_;
18 using testing::Mock;
19
20 namespace {
21
22 const char kFixedPacUrl[] = "http://chromium.org/fixed_pac_url";
23
24 // Testing proxy config service that allows us to fire notifications at will.
25 class TestProxyConfigService : public net::ProxyConfigService {
26 public:
27 explicit TestProxyConfigService(const net::ProxyConfig& config)
28 : config_(config) {
29 }
30
31 void SetProxyConfig(const net::ProxyConfig config) {
32 config_ = config;
33 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_,
34 OnProxyConfigChanged(config_));
35 }
36
37 private:
38 virtual void AddObserver(net::ProxyConfigService::Observer* observer) {
39 observers_.AddObserver(observer);
40 }
41
42 virtual void RemoveObserver(net::ProxyConfigService::Observer* observer) {
43 observers_.RemoveObserver(observer);
44 }
45
46 virtual bool GetLatestProxyConfig(net::ProxyConfig* config) {
47 *config = config_;
48 return true;
49 }
50
51 net::ProxyConfig config_;
52 ObserverList<net::ProxyConfigService::Observer, true> observers_;
53 };
54
55 // A mock observer for capturing callbacks.
56 class MockObserver : public net::ProxyConfigService::Observer {
57 public:
58 MOCK_METHOD1(OnProxyConfigChanged, void(const net::ProxyConfig&));
59 };
60
61 template<typename TESTBASE>
62 class PrefProxyConfigServiceTestBase : public TESTBASE {
63 protected:
64 PrefProxyConfigServiceTestBase()
65 : ui_thread_(BrowserThread::UI, &loop_),
66 io_thread_(BrowserThread::IO, &loop_) {}
67
68 virtual void SetUp() {
69 ASSERT_TRUE(pref_service_.get());
70 ChromeURLRequestContextGetter::RegisterUserPrefs(pref_service_.get());
71 fixed_config_.set_pac_url(GURL(kFixedPacUrl));
72 delegate_service_ = new TestProxyConfigService(fixed_config_);
73 proxy_config_tracker_ = new PrefProxyConfigTracker(pref_service_.get());
74 proxy_config_service_.reset(
75 new PrefProxyConfigService(proxy_config_tracker_.get(),
76 delegate_service_));
77 }
78
79 virtual void TearDown() {
80 proxy_config_tracker_->DetachFromPrefService();
81 loop_.RunAllPending();
82 proxy_config_service_.reset();
83 pref_service_.reset();
84 }
85
86 MessageLoop loop_;
87 TestProxyConfigService* delegate_service_; // weak
88 scoped_ptr<TestingPrefService> pref_service_;
89 scoped_ptr<PrefProxyConfigService> proxy_config_service_;
90 net::ProxyConfig fixed_config_;
91
92 private:
93 scoped_refptr<PrefProxyConfigTracker> proxy_config_tracker_;
94 BrowserThread ui_thread_;
95 BrowserThread io_thread_;
96 };
97
98 class PrefProxyConfigServiceTest
99 : public PrefProxyConfigServiceTestBase<testing::Test> {
100 protected:
101 virtual void SetUp() {
102 pref_service_.reset(new TestingPrefService);
103 PrefProxyConfigServiceTestBase<testing::Test>::SetUp();
104 }
105 };
106
107 TEST_F(PrefProxyConfigServiceTest, BaseConfiguration) {
108 net::ProxyConfig actual_config;
109 proxy_config_service_->GetLatestProxyConfig(&actual_config);
110 EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url());
111 }
112
113 TEST_F(PrefProxyConfigServiceTest, DynamicPrefOverrides) {
114 pref_service_->SetManagedPref(
115 prefs::kProxyServer, Value::CreateStringValue("http://example.com:3128"));
116 loop_.RunAllPending();
117
118 net::ProxyConfig actual_config;
119 proxy_config_service_->GetLatestProxyConfig(&actual_config);
120 EXPECT_FALSE(actual_config.auto_detect());
121 EXPECT_EQ(net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY,
122 actual_config.proxy_rules().type);
123 EXPECT_EQ(actual_config.proxy_rules().single_proxy,
124 net::ProxyServer::FromURI("http://example.com:3128",
125 net::ProxyServer::SCHEME_HTTP));
126
127 pref_service_->SetManagedPref(
128 prefs::kProxyAutoDetect, Value::CreateBooleanValue(true));
129 loop_.RunAllPending();
130
131 proxy_config_service_->GetLatestProxyConfig(&actual_config);
132 EXPECT_TRUE(actual_config.auto_detect());
133 }
134
135 // Compares proxy configurations, but allows different identifiers.
136 MATCHER_P(ProxyConfigMatches, config, "") {
137 net::ProxyConfig reference(config);
138 reference.set_id(arg.id());
139 return reference.Equals(arg);
140 }
141
142 TEST_F(PrefProxyConfigServiceTest, Observers) {
143 MockObserver observer;
144 proxy_config_service_->AddObserver(&observer);
145
146 // Firing the observers in the delegate should trigger a notification.
147 net::ProxyConfig config2;
148 config2.set_auto_detect(true);
149 EXPECT_CALL(observer,
150 OnProxyConfigChanged(ProxyConfigMatches(config2))).Times(1);
151 delegate_service_->SetProxyConfig(config2);
152 loop_.RunAllPending();
153 Mock::VerifyAndClearExpectations(&observer);
154
155 // Override configuration, this should trigger a notification.
156 net::ProxyConfig pref_config;
157 pref_config.set_pac_url(GURL(kFixedPacUrl));
158 EXPECT_CALL(observer,
159 OnProxyConfigChanged(ProxyConfigMatches(pref_config))).Times(1);
160 pref_service_->SetManagedPref(prefs::kProxyPacUrl,
161 Value::CreateStringValue(kFixedPacUrl));
162 loop_.RunAllPending();
163 Mock::VerifyAndClearExpectations(&observer);
164
165 // Since there are pref overrides, delegate changes should be ignored.
166 net::ProxyConfig config3;
167 config3.proxy_rules().ParseFromString("http=config3:80");
168 EXPECT_CALL(observer, OnProxyConfigChanged(_)).Times(0);
169 fixed_config_.set_auto_detect(true);
170 delegate_service_->SetProxyConfig(config3);
171 loop_.RunAllPending();
172 Mock::VerifyAndClearExpectations(&observer);
173
174 // Clear the override should switch back to the fixed configuration.
175 EXPECT_CALL(observer,
176 OnProxyConfigChanged(ProxyConfigMatches(config3))).Times(1);
177 pref_service_->RemoveManagedPref(prefs::kProxyPacUrl);
178 loop_.RunAllPending();
179 Mock::VerifyAndClearExpectations(&observer);
180
181 // Delegate service notifications should show up again.
182 net::ProxyConfig config4;
183 config4.proxy_rules().ParseFromString("socks:config4");
184 EXPECT_CALL(observer,
185 OnProxyConfigChanged(ProxyConfigMatches(config4))).Times(1);
186 delegate_service_->SetProxyConfig(config4);
187 loop_.RunAllPending();
188 Mock::VerifyAndClearExpectations(&observer);
189
190 proxy_config_service_->RemoveObserver(&observer);
191 }
192
193 // Test parameter object for testing command line proxy configuration.
194 struct CommandLineTestParams {
195 // Explicit assignment operator, so testing::TestWithParam works with MSVC.
196 CommandLineTestParams& operator=(const CommandLineTestParams& other) {
197 description = other.description;
198 for (unsigned int i = 0; i < arraysize(switches); i++)
199 switches[i] = other.switches[i];
200 is_null = other.is_null;
201 auto_detect = other.auto_detect;
202 pac_url = other.pac_url;
203 proxy_rules = other.proxy_rules;
204 return *this;
205 }
206
207 // Short description to identify the test.
208 const char* description;
209
210 // The command line to build a ProxyConfig from.
211 struct SwitchValue {
212 const char* name;
213 const char* value;
214 } switches[2];
215
216 // Expected outputs (fields of the ProxyConfig).
217 bool is_null;
218 bool auto_detect;
219 GURL pac_url;
220 net::ProxyRulesExpectation proxy_rules;
221 };
222
223 void PrintTo(const CommandLineTestParams& params, std::ostream* os) {
224 *os << params.description;
225 }
226
227 class PrefProxyConfigServiceCommandLineTest
228 : public PrefProxyConfigServiceTestBase<
229 testing::TestWithParam<CommandLineTestParams> > {
230 protected:
231 PrefProxyConfigServiceCommandLineTest()
232 : command_line_(CommandLine::NO_PROGRAM) {}
233
234 virtual void SetUp() {
235 for (size_t i = 0; i < arraysize(GetParam().switches); i++) {
236 const char* name = GetParam().switches[i].name;
237 const char* value = GetParam().switches[i].value;
238 if (name && value)
239 command_line_.AppendSwitchASCII(name, value);
240 else if (name)
241 command_line_.AppendSwitch(name);
242 }
243 pref_service_.reset(new TestingPrefService(NULL, NULL, &command_line_));
244 PrefProxyConfigServiceTestBase<
245 testing::TestWithParam<CommandLineTestParams> >::SetUp();
246 }
247
248 private:
249 CommandLine command_line_;
250 };
251
252 TEST_P(PrefProxyConfigServiceCommandLineTest, CommandLine) {
253 net::ProxyConfig config;
254 proxy_config_service_->GetLatestProxyConfig(&config);
255
256 if (GetParam().is_null) {
257 EXPECT_EQ(GURL(kFixedPacUrl), config.pac_url());
258 } else {
259 EXPECT_NE(GURL(kFixedPacUrl), config.pac_url());
260 EXPECT_EQ(GetParam().auto_detect, config.auto_detect());
261 EXPECT_EQ(GetParam().pac_url, config.pac_url());
262 EXPECT_TRUE(GetParam().proxy_rules.Matches(config.proxy_rules()));
263 }
264 }
265
266 static const CommandLineTestParams kCommandLineTestParams[] = {
267 {
268 "Empty command line",
269 // Input
270 { },
271 // Expected result
272 true, // is_null
273 false, // auto_detect
274 GURL(), // pac_url
275 net::ProxyRulesExpectation::Empty(),
276 },
277 {
278 "No proxy",
279 // Input
280 {
281 { switches::kNoProxyServer, NULL },
282 },
283 // Expected result
284 false, // is_null
285 false, // auto_detect
286 GURL(), // pac_url
287 net::ProxyRulesExpectation::Empty(),
288 },
289 {
290 "No proxy with extra parameters.",
291 // Input
292 {
293 { switches::kNoProxyServer, NULL },
294 { switches::kProxyServer, "http://proxy:8888" },
295 },
296 // Expected result
297 false, // is_null
298 false, // auto_detect
299 GURL(), // pac_url
300 net::ProxyRulesExpectation::Empty(),
301 },
302 {
303 "Single proxy.",
304 // Input
305 {
306 { switches::kProxyServer, "http://proxy:8888" },
307 },
308 // Expected result
309 false, // is_null
310 false, // auto_detect
311 GURL(), // pac_url
312 net::ProxyRulesExpectation::Single(
313 "proxy:8888", // single proxy
314 ""), // bypass rules
315 },
316 {
317 "Per scheme proxy.",
318 // Input
319 {
320 { switches::kProxyServer, "http=httpproxy:8888;ftp=ftpproxy:8889" },
321 },
322 // Expected result
323 false, // is_null
324 false, // auto_detect
325 GURL(), // pac_url
326 net::ProxyRulesExpectation::PerScheme(
327 "httpproxy:8888", // http
328 "", // https
329 "ftpproxy:8889", // ftp
330 ""), // bypass rules
331 },
332 {
333 "Per scheme proxy with bypass URLs.",
334 // Input
335 {
336 { switches::kProxyServer, "http=httpproxy:8888;ftp=ftpproxy:8889" },
337 { switches::kProxyBypassList,
338 ".google.com, foo.com:99, 1.2.3.4:22, 127.0.0.1/8" },
339 },
340 // Expected result
341 false, // is_null
342 false, // auto_detect
343 GURL(), // pac_url
344 net::ProxyRulesExpectation::PerScheme(
345 "httpproxy:8888", // http
346 "", // https
347 "ftpproxy:8889", // ftp
348 "*.google.com,foo.com:99,1.2.3.4:22,127.0.0.1/8"),
349 },
350 {
351 "Pac URL with proxy bypass URLs",
352 // Input
353 {
354 { switches::kProxyPacUrl, "http://wpad/wpad.dat" },
355 { switches::kProxyBypassList,
356 ".google.com, foo.com:99, 1.2.3.4:22, 127.0.0.1/8" },
357 },
358 // Expected result
359 false, // is_null
360 false, // auto_detect
361 GURL("http://wpad/wpad.dat"), // pac_url
362 net::ProxyRulesExpectation::EmptyWithBypass(
363 "*.google.com,foo.com:99,1.2.3.4:22,127.0.0.1/8"),
364 },
365 {
366 "Autodetect",
367 // Input
368 {
369 { switches::kProxyAutoDetect, NULL },
370 },
371 // Expected result
372 false, // is_null
373 true, // auto_detect
374 GURL(), // pac_url
375 net::ProxyRulesExpectation::Empty(),
376 },
377 };
378
379 INSTANTIATE_TEST_CASE_P(
380 PrefProxyConfigServiceCommandLineTestInstance,
381 PrefProxyConfigServiceCommandLineTest,
382 testing::ValuesIn(kCommandLineTestParams));
383
384 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698