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

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: Implement tests. 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"
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::_;
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 {
26 public:
27 explicit TestProxyConfigService(const net::ProxyConfig& config)
28 : net::ProxyConfigServiceFixed(config) {
29 }
30
31 void FireObservers() {
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_;
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_service_.reset(
70 new PrefProxyConfigService(pref_service_.get(), delegate_service_));
71 }
72
73 virtual void TearDown() {
74 loop_.RunAllPending();
75 proxy_config_service_.reset();
76 pref_service_.reset();
77 }
78
79 MessageLoop loop_;
80 TestProxyConfigService* delegate_service_; // weak
81 scoped_ptr<PrefProxyConfigService> proxy_config_service_;
82 scoped_ptr<TestingPrefService> pref_service_;
83 net::ProxyConfig fixed_config_;
84
85 private:
86 BrowserThread ui_thread_;
87 BrowserThread io_thread_;
88 };
89
90 class PrefProxyConfigServiceTest :
91 public PrefProxyConfigServiceTestBase<testing::Test> {
92 protected:
93 virtual void SetUp() {
94 pref_service_.reset(new TestingPrefService);
95 PrefProxyConfigServiceTestBase<testing::Test>::SetUp();
96 }
97 };
98
99 TEST_F(PrefProxyConfigServiceTest, BaseConfiguration) {
100 net::ProxyConfig actual_config;
101 proxy_config_service_->GetLatestProxyConfig(&actual_config);
102 EXPECT_TRUE(actual_config.is_valid());
103 EXPECT_EQ(GURL(kFixedPACURL), actual_config.pac_url());
104 }
105
106 TEST_F(PrefProxyConfigServiceTest, DynamicPrefOverrides) {
107 pref_service_->SetManagedPref(
108 prefs::kProxyServer, Value::CreateStringValue("http://example.com:3128"));
109 loop_.RunAllPending();
110
111 net::ProxyConfig actual_config;
112 proxy_config_service_->GetLatestProxyConfig(&actual_config);
113 EXPECT_TRUE(actual_config.is_valid());
114 EXPECT_FALSE(actual_config.auto_detect());
115 EXPECT_EQ(net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY,
116 actual_config.proxy_rules().type);
117 EXPECT_EQ(actual_config.proxy_rules().single_proxy,
118 net::ProxyServer::FromURI("http://example.com:3128",
119 net::ProxyServer::SCHEME_HTTP));
120
121 pref_service_->SetManagedPref(
122 prefs::kProxyAutoDetect, Value::CreateBooleanValue(true));
123 loop_.RunAllPending();
124
125 proxy_config_service_->GetLatestProxyConfig(&actual_config);
126 EXPECT_TRUE(actual_config.is_valid());
127 EXPECT_TRUE(actual_config.auto_detect());
128 }
129
130 // Compares proxy configurations, but allows different identifiers.
131 MATCHER_P(ProxyConfigMatches, config, "") {
132 net::ProxyConfig reference(config);
133 reference.set_id(arg.id());
134 return reference.Equals(arg);
135 }
136
137 TEST_F(PrefProxyConfigServiceTest, Observers) {
138 MockObserver observer;
139 proxy_config_service_->AddObserver(&observer);
140
141 // Firing the observers in the delegate should trigger a notification.
142 EXPECT_CALL(observer,
143 OnProxyConfigChanged(ProxyConfigMatches(fixed_config_))).Times(1);
144 delegate_service_->FireObservers();
145 loop_.RunAllPending();
146 Mock::VerifyAndClearExpectations(&observer);
147
148 // Override configuration, this should trigger a notification.
149 net::ProxyConfig pref_config;
150 pref_config.set_auto_detect(true);
151 EXPECT_CALL(observer,
152 OnProxyConfigChanged(ProxyConfigMatches(pref_config))).Times(1);
153 pref_service_->SetManagedPref(
154 prefs::kProxyAutoDetect, Value::CreateBooleanValue(true));
155 loop_.RunAllPending();
156 Mock::VerifyAndClearExpectations(&observer);
157
158 // Since there are pref overrides, delegate changes should be ignored.
159 EXPECT_CALL(observer, OnProxyConfigChanged(_)).Times(0);
160 delegate_service_->FireObservers();
161 loop_.RunAllPending();
162 Mock::VerifyAndClearExpectations(&observer);
163
164 // Clear the override should switch back to the fixed configuration.
165 EXPECT_CALL(observer,
166 OnProxyConfigChanged(ProxyConfigMatches(fixed_config_))).Times(1);
167 pref_service_->RemoveManagedPref(prefs::kProxyAutoDetect);
168 loop_.RunAllPending();
169 Mock::VerifyAndClearExpectations(&observer);
170
171 // Delegate service notifications should show up again.
172 EXPECT_CALL(observer,
173 OnProxyConfigChanged(ProxyConfigMatches(fixed_config_))).Times(1);
174 delegate_service_->FireObservers();
175 loop_.RunAllPending();
176 Mock::VerifyAndClearExpectations(&observer);
177
178 proxy_config_service_->RemoveObserver(&observer);
179 }
180
181 // Test parameter object for testing command line proxy configuration.
182 struct CommandLineTestParams {
183 // Short description to identify the test.
184 const char* description;
185
186 // The command line to build a ProxyConfig from.
187 const struct SwitchValue {
188 const char* name;
189 const char* value;
190 } switches[2];
191
192 // Expected outputs (fields of the ProxyConfig).
193 bool is_null;
194 bool auto_detect;
195 GURL pac_url;
196 net::ProxyRulesExpectation proxy_rules;
197 };
198
199 void PrintTo(const CommandLineTestParams& params, std::ostream* os) {
200 *os << params.description;
201 }
202
203 class PrefProxyConfigServiceCommandLineTest
204 : public PrefProxyConfigServiceTestBase<
205 testing::TestWithParam<CommandLineTestParams> > {
206 protected:
207 PrefProxyConfigServiceCommandLineTest()
208 : command_line_(CommandLine::NO_PROGRAM) {}
209
210 virtual void SetUp() {
211 for (unsigned int i = 0; i < arraysize(GetParam().switches); i++) {
212 const char* name = GetParam().switches[i].name;
213 const char* value = GetParam().switches[i].value;
214 if (name && value)
215 command_line_.AppendSwitchASCII(name, value);
216 else if (name)
217 command_line_.AppendSwitch(name);
218 }
219 pref_service_.reset(new TestingPrefService(NULL, &command_line_));
220 PrefProxyConfigServiceTestBase<
221 testing::TestWithParam<CommandLineTestParams> >::SetUp();
222 }
223
224 private:
225 CommandLine command_line_;
226 };
227
228 TEST_P(PrefProxyConfigServiceCommandLineTest, CommandLine) {
229 net::ProxyConfig config;
230 proxy_config_service_->GetLatestProxyConfig(&config);
231
232 if (GetParam().is_null) {
233 EXPECT_EQ(GURL(kFixedPACURL), config.pac_url());
234 } else {
235 EXPECT_NE(GURL(kFixedPACURL), config.pac_url());
236 EXPECT_EQ(GetParam().auto_detect, config.auto_detect());
237 EXPECT_EQ(GetParam().pac_url, config.pac_url());
238 EXPECT_TRUE(GetParam().proxy_rules.Matches(config.proxy_rules()));
239 }
240 }
241
242 static const CommandLineTestParams kCommandLineTestParams[] = {
243 {
244 "Empty command line",
245 // Input
246 { },
247 // Expected result
248 true, // is_null
249 false, // auto_detect
250 GURL(), // pac_url
251 net::ProxyRulesExpectation::Empty(),
252 },
253 {
254 "No proxy",
255 // Input
256 {
257 { switches::kNoProxyServer, NULL },
258 },
259 // Expected result
260 false, // is_null
261 false, // auto_detect
262 GURL(), // pac_url
263 net::ProxyRulesExpectation::Empty(),
264 },
265 {
266 "No proxy with extra parameters.",
267 // Input
268 {
269 { switches::kNoProxyServer, NULL },
270 { switches::kProxyServer, "http://proxy:8888" },
271 },
272 // Expected result
273 false, // is_null
274 false, // auto_detect
275 GURL(), // pac_url
276 net::ProxyRulesExpectation::Empty(),
277 },
278 {
279 "Single proxy.",
280 // Input
281 {
282 { switches::kProxyServer, "http://proxy:8888" },
283 },
284 // Expected result
285 false, // is_null
286 false, // auto_detect
287 GURL(), // pac_url
288 net::ProxyRulesExpectation::Single(
289 "proxy:8888", // single proxy
290 ""), // bypass rules
291 },
292 {
293 "Per scheme proxy.",
294 // Input
295 {
296 { switches::kProxyServer, "http=httpproxy:8888;ftp=ftpproxy:8889" },
297 },
298 // Expected result
299 false, // is_null
300 false, // auto_detect
301 GURL(), // pac_url
302 net::ProxyRulesExpectation::PerScheme(
303 "httpproxy:8888", // http
304 "", // https
305 "ftpproxy:8889", // ftp
306 ""), // bypass rules
307 },
308 {
309 "Per scheme proxy with bypass URLs.",
310 // Input
311 {
312 { switches::kProxyServer, "http=httpproxy:8888;ftp=ftpproxy:8889" },
313 { switches::kProxyBypassList,
314 ".google.com, foo.com:99, 1.2.3.4:22, 127.0.0.1/8" },
315 },
316 // Expected result
317 false, // is_null
318 false, // auto_detect
319 GURL(), // pac_url
320 net::ProxyRulesExpectation::PerScheme(
321 "httpproxy:8888", // http
322 "", // https
323 "ftpproxy:8889", // ftp
324 "*.google.com,foo.com:99,1.2.3.4:22,127.0.0.1/8"),
325 },
326 {
327 "Pac URL with proxy bypass URLs",
328 // Input
329 {
330 { switches::kProxyPacUrl, "http://wpad/wpad.dat" },
331 { switches::kProxyBypassList,
332 ".google.com, foo.com:99, 1.2.3.4:22, 127.0.0.1/8" },
333 },
334 // Expected result
335 false, // is_null
336 false, // auto_detect
337 GURL("http://wpad/wpad.dat"), // pac_url
338 net::ProxyRulesExpectation::EmptyWithBypass(
339 "*.google.com,foo.com:99,1.2.3.4:22,127.0.0.1/8"),
340 },
341 {
342 "Autodetect",
343 // Input
344 {
345 { switches::kProxyAutoDetect, NULL },
346 },
347 // Expected result
348 false, // is_null
349 true, // auto_detect
350 GURL(), // pac_url
351 net::ProxyRulesExpectation::Empty(),
352 },
353 };
354
355 INSTANTIATE_TEST_CASE_P(
356 PrefProxyConfigServiceCommandLineTestInstance,
357 PrefProxyConfigServiceCommandLineTest,
358 testing::ValuesIn(kCommandLineTestParams));
359
360 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698