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

Side by Side Diff: chrome/browser/chromeos/proxy_config_service_impl_unittest.cc

Issue 3047052: chromeos: 1st draft of ProxyConfigService for chromeos... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 4 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
Property Changes:
Added: svn:eol-style
+ LF
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/chromeos/proxy_config_service_impl.h"
6
7 #include <map>
8 #include <string>
9 #include <vector>
10
11 #include "base/format_macros.h"
12 #include "base/logging.h"
13 #include "base/string_util.h"
14 #include "chrome/browser/chrome_thread.h"
15 #include "net/proxy/proxy_config_service_common_unittest.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "testing/platform_test.h"
18
19 namespace chromeos {
20
21 namespace {
22
23 struct Input { // Fields of chromeos::ProxyConfigServiceImpl::ProxyConfig.
24 ProxyConfigServiceImpl::ProxyConfig::Mode mode;
25 const char* pac_url;
26 const char* single_uri;
27 const char* http_uri;
28 const char* https_uri;
29 const char* ftp_uri;
30 const char* socks_uri;
31 const char* bypass_rules;
32 };
33
34 // Builds an identifier for each test in an array.
35 #define TEST_DESC(desc) StringPrintf("at line %d <%s>", __LINE__, desc)
36
37 // Shortcuts to declare enums within chromeos's ProxyConfig.
38 #define MK_MODE(mode) ProxyConfigServiceImpl::ProxyConfig::MODE_##mode
39 #define MK_SRC(src) ProxyConfigServiceImpl::ProxyConfig::SOURCE_##src
40
41 // Inspired from net/proxy/proxy_config_service_linux_unittest.cc.
42 const struct {
43 // Short description to identify the test
44 std::string description;
45
46 Input input;
47
48 // Expected outputs from fields of net::ProxyConfig (via IO).
49 bool auto_detect;
50 GURL pac_url;
51 net::ProxyRulesExpectation proxy_rules;
52 } tests[] = {
53 {
54 TEST_DESC("No proxying"),
55 { // Input.
56 MK_MODE(DIRECT), // mode
57 },
58
59 // Expected result.
60 false, // auto_detect
61 GURL(), // pac_url
62 net::ProxyRulesExpectation::Empty(), // proxy_rules
63 },
64
65 {
66 TEST_DESC("Auto detect"),
67 { // Input.
68 MK_MODE(AUTO_DETECT), // mode
69 },
70
71 // Expected result.
72 true, // auto_detect
73 GURL(), // pac_url
74 net::ProxyRulesExpectation::Empty(), // proxy_rules
75 },
76
77 {
78 TEST_DESC("Valid PAC URL"),
79 { // Input.
80 MK_MODE(PAC_SCRIPT), // mode
81 "http://wpad/wpad.dat", // pac_url
82 },
83
84 // Expected result.
85 false, // auto_detect
86 GURL("http://wpad/wpad.dat"), // pac_url
87 net::ProxyRulesExpectation::Empty(), // proxy_rules
88 },
89
90 {
91 TEST_DESC("Invalid PAC URL"),
92 { // Input.
93 MK_MODE(PAC_SCRIPT), // mode
94 "wpad.dat", // pac_url
95 },
96
97 // Expected result.
98 false, // auto_detect
99 GURL(), // pac_url
100 net::ProxyRulesExpectation::Empty(), // proxy_rules
101 },
102
103 {
104 TEST_DESC("Single-host in proxy list"),
105 { // Input.
106 MK_MODE(SINGLE_PROXY), // mode
107 NULL, // pac_url
108 "www.google.com", // single_uri
109 },
110
111 // Expected result.
112 false, // auto_detect
113 GURL(), // pac_url
114 net::ProxyRulesExpectation::Single( // proxy_rules
115 "www.google.com:80", // single proxy
116 ""), // bypass rules
117 },
118
119 {
120 TEST_DESC("Single-host, different port"),
121 { // Input.
122 MK_MODE(SINGLE_PROXY), // mode
123 NULL, // pac_url
124 "www.google.com:99", // single_uri
125 },
126
127 // Expected result.
128 false, // auto_detect
129 GURL(), // pac_url
130 net::ProxyRulesExpectation::Single( // proxy_rules
131 "www.google.com:99", // single
132 ""), // bypass rules
133 },
134
135 {
136 TEST_DESC("Tolerate a scheme"),
137 { // Input.
138 MK_MODE(SINGLE_PROXY), // mode
139 NULL, // pac_url
140 "http://www.google.com:99", // single_uri
141 },
142
143 // Expected result.
144 false, // auto_detect
145 GURL(), // pac_url
146 net::ProxyRulesExpectation::Single( // proxy_rules
147 "www.google.com:99", // single proxy
148 ""), // bypass rules
149 },
150
151 {
152 TEST_DESC("Per-scheme proxy rules"),
153 { // Input.
154 MK_MODE(PROXY_PER_SCHEME), // mode
155 NULL, // pac_url
156 NULL, // single_uri
157 "www.google.com:80", "www.foo.com:110", "ftp.foo.com:121", // per-proto
158 },
159
160 // Expected result.
161 false, // auto_detect
162 GURL(), // pac_url
163 net::ProxyRulesExpectation::PerScheme( // proxy_rules
164 "www.google.com:80", // http
165 "www.foo.com:110", // https
166 "ftp.foo.com:121", // ftp
167 ""), // bypass rules
168 },
169
170 // TODO(kuan): enable these.
171 #if defined(TO_ENABLE_SOON)
172 {
173 TEST_DESC("socks"),
174 { // Input.
175 MK_MODE(PROXY_PER_SCHEME), // mode
176 NULL, // pac_url
177 NULL, // single_uri
178 NULL, NULL, NULL, // per-proto proxies
179 "socks.com:888", // socks_uri
180 },
181
182 // Expected result.
183 false, // auto_detect
184 GURL(), // pac_url
185 net::ProxyRulesExpectation::Single( // proxy_rules
186 "socks4://socks.com:888", // single proxy
187 ""), // bypass rules
188 },
189
190 {
191 TEST_DESC("socks5"),
192 { // Input.
193 NULL, // auto_proxy
194 "", // all_proxy
195 NULL, NULL, NULL, // per-proto proxies
196 "socks.com:888", "5", // SOCKS
197 NULL, // no_proxy
198 },
199
200 // Expected result.
201 false, // auto_detect
202 GURL(), // pac_url
203 net::ProxyRulesExpectation::Single( // proxy_rules
204 "socks5://socks.com:888", // single proxy
205 ""), // bypass rules
206 ProxyConfigServiceImpl::READ_ONLY_MAIN, // readonly for owner
207 ProxyConfigServiceImpl::READ_ONLY_MAIN, // readonly for non-owner
208 },
209
210 {
211 TEST_DESC("socks default port"),
212 { // Input.
213 NULL, // auto_proxy
214 "", // all_proxy
215 NULL, NULL, NULL, // per-proto proxies
216 "socks.com", NULL, // SOCKS
217 NULL, // no_proxy
218 },
219
220 // Expected result.
221 false, // auto_detect
222 GURL(), // pac_url
223 net::ProxyRulesExpectation::Single( // proxy_rules
224 "socks4://socks.com:1080", // single proxy
225 ""), // bypass rules
226 },
227
228 {
229 TEST_DESC("bypass"),
230 { // Input.
231 MK_MODE(PROXY_PER_SCHEME), // mode
232 NULL, // pac_url
233 "www.google.com", // single_uri
234 NULL, NULL, NULL, NULL, // per-proto & socks proxies
235 ".google.com, foo.com:99, 1.2.3.4:22, 127.0.0.1/8", // bypass_rules
236 },
237
238 // Expected result;
239 false, // auto_detect
240 GURL(), // pac_url
241 net::ProxyRulesExpectation::Single( // proxy_rules
242 "www.google.com:80",
243 "*.google.com,*foo.com:99,1.2.3.4:22,127.0.0.1/8"),
244 },
245 #endif // TO_ENABLE_SOON
246 }; // tests
247
248 } // namespace
249
250 class ProxyConfigServiceImplTest : public PlatformTest {
251 protected:
252 ProxyConfigServiceImplTest()
253 : ui_thread_(ChromeThread::UI, &message_loop_),
254 io_thread_(ChromeThread::IO, &message_loop_) {
255 }
256
257 virtual ~ProxyConfigServiceImplTest() {
258 config_service_ = NULL;
259 MessageLoop::current()->RunAllPending();
260 }
261
262 void CreateConfigService(
263 const ProxyConfigServiceImpl::ProxyConfig& init_config) {
264 // Instantiate proxy config service with |init_config|.
265 config_service_ = new ProxyConfigServiceImpl(init_config);
266 }
267
268 void SetAutomaticProxy(
269 ProxyConfigServiceImpl::ProxyConfig::Mode mode,
270 ProxyConfigServiceImpl::ProxyConfig::Source source,
271 const char* pac_url,
272 ProxyConfigServiceImpl::ProxyConfig* config,
273 ProxyConfigServiceImpl::ProxyConfig::AutomaticProxy* automatic_proxy) {
274 config->mode = mode;
275 automatic_proxy->source = source;
276 if (pac_url)
277 automatic_proxy->pac_url = GURL(pac_url);
278 }
279
280 void SetManualProxy(
281 ProxyConfigServiceImpl::ProxyConfig::Mode mode,
282 ProxyConfigServiceImpl::ProxyConfig::Source source,
283 const char* server_uri,
284 ProxyConfigServiceImpl::ProxyConfig* config,
285 ProxyConfigServiceImpl::ProxyConfig::ManualProxy* manual_proxy) {
286 if (!server_uri)
287 return;
288 config->mode = mode;
289 manual_proxy->source = source;
290 manual_proxy->server = net::ProxyServer::FromURI(server_uri,
291 net::ProxyServer::SCHEME_HTTP);
292 }
293
294 void InitConfigWithTestInput(
295 const Input& input,
296 ProxyConfigServiceImpl::ProxyConfig* init_config) {
297 ProxyConfigServiceImpl::ProxyConfig::Source source = MK_SRC(OWNER);
298 switch (input.mode) {
299 case MK_MODE(DIRECT):
300 case MK_MODE(AUTO_DETECT):
301 case MK_MODE(PAC_SCRIPT):
302 SetAutomaticProxy(input.mode, source, input.pac_url, init_config,
303 &init_config->automatic_proxy);
304 return;
305 case MK_MODE(SINGLE_PROXY):
306 SetManualProxy(input.mode, source, input.single_uri, init_config,
307 &init_config->single_proxy);
308 break;
309 case MK_MODE(PROXY_PER_SCHEME):
310 SetManualProxy(input.mode, source, input.http_uri, init_config,
311 &init_config->http_proxy);
312 SetManualProxy(input.mode, source, input.https_uri, init_config,
313 &init_config->https_proxy);
314 SetManualProxy(input.mode, source, input.ftp_uri, init_config,
315 &init_config->ftp_proxy);
316 SetManualProxy(input.mode, source, input.socks_uri, init_config,
317 &init_config->socks_proxy);
318 break;
319 }
320 if (input.bypass_rules) {
321 init_config->bypass_rules.ParseFromStringUsingSuffixMatching(
322 input.bypass_rules);
323 }
324 }
325
326 // Synchronously gets the latest proxy config.
327 bool SyncGetLatestProxyConfig(net::ProxyConfig* config) {
328 // Let message loop process all messages.
329 MessageLoop::current()->RunAllPending();
330 // Calls IOGetProxyConfig (which is called from
331 // ProxyConfigService::GetLatestProxyConfig), running on faked IO thread.
332 return config_service_->IOGetProxyConfig(config);
333 }
334
335 ProxyConfigServiceImpl* config_service() const {
336 return config_service_;
337 }
338
339 private:
340 MessageLoop message_loop_;
341 ChromeThread ui_thread_;
342 ChromeThread io_thread_;
343
344 scoped_refptr<ProxyConfigServiceImpl> config_service_;
345 };
346
347 TEST_F(ProxyConfigServiceImplTest, ChromeosProxyConfigToNetProxyConfig) {
348 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
349 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i,
350 tests[i].description.c_str()));
351
352 ProxyConfigServiceImpl::ProxyConfig init_config;
353 InitConfigWithTestInput(tests[i].input, &init_config);
354 CreateConfigService(init_config);
355
356 net::ProxyConfig config;
357 SyncGetLatestProxyConfig(&config);
358
359 EXPECT_EQ(tests[i].auto_detect, config.auto_detect());
360 EXPECT_EQ(tests[i].pac_url, config.pac_url());
361 EXPECT_TRUE(tests[i].proxy_rules.Matches(config.proxy_rules()));
362 }
363 }
364
365 TEST_F(ProxyConfigServiceImplTest, ReadWriteAccess) {
366 static const char* pac_url = "http://wpad.dat";
367
368 { // Init with pac script from policy.
369 ProxyConfigServiceImpl::ProxyConfig init_config;
370 SetAutomaticProxy(MK_MODE(PAC_SCRIPT), MK_SRC(POLICY), pac_url,
371 &init_config, &init_config.automatic_proxy);
372 CreateConfigService(init_config);
373
374 ProxyConfigServiceImpl::ProxyConfig config;
375 config_service()->UIGetProxyConfig(&config);
376
377 EXPECT_EQ(MK_SRC(POLICY), config.automatic_proxy.source);
378 // Setting should be not be writeable by owner.
379 EXPECT_FALSE(config.automatic_proxy.CanBeWrittenByUser(true));
380 // Setting should be not be writeable by non-owner.
381 EXPECT_FALSE(config.automatic_proxy.CanBeWrittenByUser(false));
382 }
383
384 { // Init with pac script from owner.
385 ProxyConfigServiceImpl::ProxyConfig init_config;
386 SetAutomaticProxy(MK_MODE(PAC_SCRIPT), MK_SRC(OWNER), pac_url,
387 &init_config, &init_config.automatic_proxy);
388 CreateConfigService(init_config);
389
390 ProxyConfigServiceImpl::ProxyConfig config;
391 config_service()->UIGetProxyConfig(&config);
392
393 EXPECT_EQ(MK_SRC(OWNER), config.automatic_proxy.source);
394 // Setting should be writeable by owner.
395 EXPECT_TRUE(config.automatic_proxy.CanBeWrittenByUser(true));
396 // Setting should not be writeable by non-owner.
397 EXPECT_FALSE(config.automatic_proxy.CanBeWrittenByUser(false));
398 }
399 }
400
401 TEST_F(ProxyConfigServiceImplTest, ModifyFromUI) {
402 // Init with direct.
403 ProxyConfigServiceImpl::ProxyConfig init_config;
404 SetAutomaticProxy(MK_MODE(DIRECT), MK_SRC(OWNER), NULL, &init_config,
405 &init_config.automatic_proxy);
406 CreateConfigService(init_config);
407
408 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
409 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i,
410 tests[i].description.c_str()));
411
412 // Set config to tests[i].input via UI.
413 net::ProxyBypassRules bypass_rules;
414 net::ProxyServer::Scheme scheme = net::ProxyServer::SCHEME_HTTP;
415 const Input& input = tests[i].input;
416 switch (input.mode) {
417 case MK_MODE(DIRECT) :
418 config_service()->UISetProxyConfigToDirect();
419 break;
420 case MK_MODE(AUTO_DETECT) :
421 config_service()->UISetProxyConfigToAutoDetect();
422 break;
423 case MK_MODE(PAC_SCRIPT) :
424 config_service()->UISetProxyConfigToPACScript(GURL(input.pac_url));
425 break;
426 case MK_MODE(SINGLE_PROXY) :
427 config_service()->UISetProxyConfigToSingleProxy(
428 net::ProxyServer::FromURI(input.single_uri,
429 net::ProxyServer::SCHEME_HTTP));
430 if (input.bypass_rules) {
431 bypass_rules.ParseFromStringUsingSuffixMatching(input.bypass_rules);
432 config_service()->UISetProxyConfigBypassRules(bypass_rules);
433 }
434 break;
435 case MK_MODE(PROXY_PER_SCHEME) :
436 if (input.http_uri) {
437 config_service()->UISetProxyConfigToProxyPerScheme(
438 "http",
439 net::ProxyServer::FromURI(input.http_uri, scheme));
440 }
441 if (input.https_uri) {
442 config_service()->UISetProxyConfigToProxyPerScheme(
443 "https",
444 net::ProxyServer::FromURI(input.https_uri, scheme));
445 }
446 if (input.ftp_uri) {
447 config_service()->UISetProxyConfigToProxyPerScheme(
448 "ftp",
449 net::ProxyServer::FromURI(input.ftp_uri, scheme));
450 }
451 if (input.socks_uri) {
452 config_service()->UISetProxyConfigToProxyPerScheme(
453 "socks",
454 net::ProxyServer::FromURI(input.socks_uri, scheme));
455 }
456 if (input.bypass_rules) {
457 bypass_rules.ParseFromStringUsingSuffixMatching(input.bypass_rules);
458 config_service()->UISetProxyConfigBypassRules(bypass_rules);
459 }
460 break;
461 }
462
463 // Retrieve config from IO thread.
464 net::ProxyConfig io_config;
465 SyncGetLatestProxyConfig(&io_config);
466 EXPECT_EQ(tests[i].auto_detect, io_config.auto_detect());
467 EXPECT_EQ(tests[i].pac_url, io_config.pac_url());
468 EXPECT_TRUE(tests[i].proxy_rules.Matches(io_config.proxy_rules()));
469
470 // Retrieve config from UI thread.
471 ProxyConfigServiceImpl::ProxyConfig ui_config;
472 config_service()->UIGetProxyConfig(&ui_config);
473 EXPECT_EQ(input.mode, ui_config.mode);
474 if (input.pac_url)
475 EXPECT_EQ(GURL(input.pac_url), ui_config.automatic_proxy.pac_url);
476 const net::ProxyRulesExpectation& proxy_rules = tests[i].proxy_rules;
477 if (input.single_uri)
478 EXPECT_EQ(proxy_rules.single_proxy,
479 ui_config.single_proxy.server.ToURI());
480 if (input.http_uri)
481 EXPECT_EQ(proxy_rules.proxy_for_http,
482 ui_config.http_proxy.server.ToURI());
483 if (input.https_uri)
484 EXPECT_EQ(proxy_rules.proxy_for_https,
485 ui_config.https_proxy.server.ToURI());
486 if (input.ftp_uri)
487 EXPECT_EQ(proxy_rules.proxy_for_ftp, ui_config.ftp_proxy.server.ToURI());
488 if (input.socks_uri)
489 EXPECT_EQ(proxy_rules.socks_proxy, ui_config.socks_proxy.server.ToURI());
490 if (input.bypass_rules)
491 EXPECT_TRUE(bypass_rules.Equals(ui_config.bypass_rules));
492 }
493 }
494
495 TEST_F(ProxyConfigServiceImplTest, ProxyChangedObserver) {
496 // This is used to observe for OnProxyConfigChanged notification.
497 class ProxyChangedObserver : public net::ProxyConfigService::Observer {
498 public:
499 explicit ProxyChangedObserver(
500 const scoped_refptr<ProxyConfigServiceImpl>& config_service)
501 : config_service_(config_service) {
502 config_service_->AddObserver(this);
503 }
504 virtual ~ProxyChangedObserver() {
505 config_service_->RemoveObserver(this);
506 }
507 const net::ProxyConfig& config() const {
508 return config_;
509 }
510
511 private:
512 virtual void OnProxyConfigChanged(const net::ProxyConfig& config) {
513 config_ = config;
514 }
515
516 scoped_refptr<ProxyConfigServiceImpl> config_service_;
517 net::ProxyConfig config_;
518 };
519
520 // Init with direct.
521 ProxyConfigServiceImpl::ProxyConfig init_config;
522 SetAutomaticProxy(MK_MODE(DIRECT), MK_SRC(OWNER), NULL, &init_config,
523 &init_config.automatic_proxy);
524 CreateConfigService(init_config);
525
526 ProxyChangedObserver observer(config_service());
527
528 // Set to pac script from UI.
529 config_service()->UISetProxyConfigToPACScript(GURL("http://wpad.dat"));
530
531 // Retrieve config from IO thread.
532 net::ProxyConfig io_config;
533 SyncGetLatestProxyConfig(&io_config);
534
535 // Observer should have gotten the same new proxy config.
536 EXPECT_TRUE(io_config.Equals(observer.config()));
537 }
538
539 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/proxy_config_service_impl.cc ('k') | chrome/browser/net/chrome_url_request_context.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698