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

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

Powered by Google App Engine
This is Rietveld 408576698