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

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

Issue 6549007: Make CrOS proxy configuration write prefs directly. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove default request context proxy config service patch. Created 9 years, 10 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
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/proxy_config_service_impl.h" 5 #include "chrome/browser/chromeos/proxy_config_service_impl.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/format_macros.h" 11 #include "base/format_macros.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/string_util.h" 13 #include "base/string_util.h"
14 #include "base/stringprintf.h" 14 #include "base/stringprintf.h"
15 #include "chrome/browser/browser_thread.h"
16 #include "chrome/browser/chromeos/cros/cros_library.h" 15 #include "chrome/browser/chromeos/cros/cros_library.h"
16 #include "chrome/browser/prefs/browser_prefs.h"
17 #include "chrome/browser/prefs/pref_change_registrar.h"
18 #include "chrome/browser/prefs/pref_observer_mock.h"
19 #include "chrome/browser/prefs/proxy_config_dictionary.h"
20 #include "chrome/browser/prefs/proxy_prefs.h"
21 #include "chrome/common/pref_names.h"
22 #include "chrome/test/testing_pref_service.h"
17 #include "net/proxy/proxy_config_service_common_unittest.h" 23 #include "net/proxy/proxy_config_service_common_unittest.h"
18 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
19 #include "testing/platform_test.h" 25 #include "testing/platform_test.h"
20 26
21 #if !defined(NDEBUG) 27 #if !defined(NDEBUG)
22 #include "chrome/common/json_value_serializer.h" 28 #include "chrome/common/json_value_serializer.h"
23 #endif // !defined(NDEBUG) 29 #endif // !defined(NDEBUG)
24 30
31 using testing::Mock;
32
25 namespace chromeos { 33 namespace chromeos {
26 34
27 namespace { 35 namespace {
28 36
29 struct Input { // Fields of chromeos::ProxyConfigServiceImpl::ProxyConfig. 37 struct Input { // Fields of chromeos::ProxyConfigServiceImpl::ProxyConfig.
30 ProxyConfigServiceImpl::ProxyConfig::Mode mode; 38 chromeos::ProxyConfigServiceImpl::ProxyConfig::Mode mode;
31 const char* pac_url; 39 const char* pac_url;
32 const char* single_uri; 40 const char* single_uri;
33 const char* http_uri; 41 const char* http_uri;
34 const char* https_uri; 42 const char* https_uri;
35 const char* ftp_uri; 43 const char* ftp_uri;
36 const char* socks_uri; 44 const char* socks_uri;
37 const char* bypass_rules; 45 const char* bypass_rules;
38 }; 46 };
39 47
40 // Builds an identifier for each test in an array. 48 // Builds an identifier for each test in an array.
41 #define TEST_DESC(desc) base::StringPrintf("at line %d <%s>", __LINE__, desc) 49 #define TEST_DESC(desc) base::StringPrintf("at line %d <%s>", __LINE__, desc)
42 50
43 // Shortcuts to declare enums within chromeos's ProxyConfig. 51 // Shortcuts to declare enums within chromeos's ProxyConfig.
44 #define MK_MODE(mode) ProxyConfigServiceImpl::ProxyConfig::MODE_##mode 52 #define MK_MODE(mode) ProxyConfigServiceImpl::ProxyConfig::MODE_##mode
45 #define MK_SRC(src) ProxyConfigServiceImpl::ProxyConfig::SOURCE_##src 53 #define MK_SRC(src) ProxyConfigServiceImpl::ProxyConfig::SOURCE_##src
46 #define MK_SCHM(scheme) net::ProxyServer::SCHEME_##scheme 54 #define MK_SCHM(scheme) net::ProxyServer::SCHEME_##scheme
47 55
48 // Inspired from net/proxy/proxy_config_service_linux_unittest.cc. 56 // Inspired from net/proxy/proxy_config_service_linux_unittest.cc.
49 const struct { 57 const struct TestData {
50 // Short description to identify the test 58 // Short description to identify the test
51 std::string description; 59 std::string description;
52 60
53 bool is_valid; 61 bool is_valid;
54 bool test_read_write_access; 62 bool test_read_write_access;
55 63
56 Input input; 64 Input input;
57 65
58 // Expected outputs from fields of net::ProxyConfig (via IO). 66 // Expected output in Chrome proxy config.
59 bool auto_detect; 67 ProxyPrefs::ProxyMode mode;
60 GURL pac_url; 68 const char* pac_url;
61 net::ProxyRulesExpectation proxy_rules; 69 net::ProxyRulesExpectation proxy_rules;
62 } tests[] = { 70 } tests[] = {
63 { 71 {
64 TEST_DESC("No proxying"), 72 TEST_DESC("No proxying"),
65 73
66 true, // is_valid 74 true, // is_valid
67 true, // test_read_write_access 75 true, // test_read_write_access
68 76
69 { // Input. 77 { // Input.
70 MK_MODE(DIRECT), // mode 78 MK_MODE(DIRECT), // mode
71 }, 79 },
72 80
73 // Expected result. 81 // Expected result.
74 false, // auto_detect 82 ProxyPrefs::MODE_DIRECT, // mode
75 GURL(), // pac_url 83 "", // pac_url
76 net::ProxyRulesExpectation::Empty(), // proxy_rules 84 net::ProxyRulesExpectation::Empty(), // proxy_rules
77 }, 85 },
78 86
79 { 87 {
80 TEST_DESC("Auto detect"), 88 TEST_DESC("Auto detect"),
81 89
82 true, // is_valid 90 true, // is_valid
83 true, // test_read_write_access 91 true, // test_read_write_access
84 92
85 { // Input. 93 { // Input.
86 MK_MODE(AUTO_DETECT), // mode 94 MK_MODE(AUTO_DETECT), // mode
87 }, 95 },
88 96
89 // Expected result. 97 // Expected result.
90 true, // auto_detect 98 ProxyPrefs::MODE_AUTO_DETECT, // mode
91 GURL(), // pac_url 99 "", // pac_url
92 net::ProxyRulesExpectation::Empty(), // proxy_rules 100 net::ProxyRulesExpectation::Empty(), // proxy_rules
93 }, 101 },
94 102
95 { 103 {
96 TEST_DESC("Valid PAC URL"), 104 TEST_DESC("Valid PAC URL"),
97 105
98 true, // is_valid 106 true, // is_valid
99 true, // test_read_write_access 107 true, // test_read_write_access
100 108
101 { // Input. 109 { // Input.
102 MK_MODE(PAC_SCRIPT), // mode 110 MK_MODE(PAC_SCRIPT), // mode
103 "http://wpad/wpad.dat", // pac_url 111 "http://wpad/wpad.dat", // pac_url
104 }, 112 },
105 113
106 // Expected result. 114 // Expected result.
107 false, // auto_detect 115 ProxyPrefs::MODE_PAC_SCRIPT, // mode
108 GURL("http://wpad/wpad.dat"), // pac_url 116 "http://wpad/wpad.dat", // pac_url
109 net::ProxyRulesExpectation::Empty(), // proxy_rules 117 net::ProxyRulesExpectation::Empty(), // proxy_rules
110 }, 118 },
111 119
112 { 120 {
113 TEST_DESC("Invalid PAC URL"), 121 TEST_DESC("Invalid PAC URL"),
114 122
115 false, // is_valid 123 false, // is_valid
116 false, // test_read_write_access 124 false, // test_read_write_access
117 125
118 { // Input. 126 { // Input.
119 MK_MODE(PAC_SCRIPT), // mode 127 MK_MODE(PAC_SCRIPT), // mode
120 "wpad.dat", // pac_url 128 "wpad.dat", // pac_url
121 }, 129 },
122 130
123 // Expected result. 131 // Expected result.
124 false, // auto_detect 132 ProxyPrefs::MODE_PAC_SCRIPT, // mode
125 GURL(), // pac_url 133 "", // pac_url
126 net::ProxyRulesExpectation::Empty(), // proxy_rules 134 net::ProxyRulesExpectation::Empty(), // proxy_rules
127 }, 135 },
128 136
129 { 137 {
130 TEST_DESC("Single-host in proxy list"), 138 TEST_DESC("Single-host in proxy list"),
131 139
132 true, // is_valid 140 true, // is_valid
133 true, // test_read_write_access 141 true, // test_read_write_access
134 142
135 { // Input. 143 { // Input.
136 MK_MODE(SINGLE_PROXY), // mode 144 MK_MODE(SINGLE_PROXY), // mode
137 NULL, // pac_url 145 NULL, // pac_url
138 "www.google.com", // single_uri 146 "www.google.com", // single_uri
139 }, 147 },
140 148
141 // Expected result. 149 // Expected result.
142 false, // auto_detect 150 ProxyPrefs::MODE_FIXED_SERVERS, // mode
143 GURL(), // pac_url 151 "", // pac_url
144 net::ProxyRulesExpectation::Single( // proxy_rules 152 net::ProxyRulesExpectation::Single( // proxy_rules
145 "www.google.com:80", // single proxy 153 "www.google.com:80", // single proxy
146 ""), // bypass rules 154 ""), // bypass rules
147 }, 155 },
148 156
149 { 157 {
150 TEST_DESC("Single-host, different port"), 158 TEST_DESC("Single-host, different port"),
151 159
152 true, // is_valid 160 true, // is_valid
153 false, // test_read_write_access 161 false, // test_read_write_access
154 162
155 { // Input. 163 { // Input.
156 MK_MODE(SINGLE_PROXY), // mode 164 MK_MODE(SINGLE_PROXY), // mode
157 NULL, // pac_url 165 NULL, // pac_url
158 "www.google.com:99", // single_uri 166 "www.google.com:99", // single_uri
159 }, 167 },
160 168
161 // Expected result. 169 // Expected result.
162 false, // auto_detect 170 ProxyPrefs::MODE_FIXED_SERVERS, // mode
163 GURL(), // pac_url 171 "", // pac_url
164 net::ProxyRulesExpectation::Single( // proxy_rules 172 net::ProxyRulesExpectation::Single( // proxy_rules
165 "www.google.com:99", // single 173 "www.google.com:99", // single
166 ""), // bypass rules 174 ""), // bypass rules
167 }, 175 },
168 176
169 { 177 {
170 TEST_DESC("Tolerate a scheme"), 178 TEST_DESC("Tolerate a scheme"),
171 179
172 true, // is_valid 180 true, // is_valid
173 false, // test_read_write_access 181 false, // test_read_write_access
174 182
175 { // Input. 183 { // Input.
176 MK_MODE(SINGLE_PROXY), // mode 184 MK_MODE(SINGLE_PROXY), // mode
177 NULL, // pac_url 185 NULL, // pac_url
178 "http://www.google.com:99", // single_uri 186 "http://www.google.com:99", // single_uri
179 }, 187 },
180 188
181 // Expected result. 189 // Expected result.
182 false, // auto_detect 190 ProxyPrefs::MODE_FIXED_SERVERS, // mode
183 GURL(), // pac_url 191 "", // pac_url
184 net::ProxyRulesExpectation::Single( // proxy_rules 192 net::ProxyRulesExpectation::Single( // proxy_rules
185 "www.google.com:99", // single proxy 193 "www.google.com:99", // single proxy
186 ""), // bypass rules 194 ""), // bypass rules
187 }, 195 },
188 196
189 { 197 {
190 TEST_DESC("Per-scheme proxy rules"), 198 TEST_DESC("Per-scheme proxy rules"),
191 199
192 true, // is_valid 200 true, // is_valid
193 true, // test_read_write_access 201 true, // test_read_write_access
194 202
195 { // Input. 203 { // Input.
196 MK_MODE(PROXY_PER_SCHEME), // mode 204 MK_MODE(PROXY_PER_SCHEME), // mode
197 NULL, // pac_url 205 NULL, // pac_url
198 NULL, // single_uri 206 NULL, // single_uri
199 "www.google.com:80", // http_uri 207 "www.google.com:80", // http_uri
200 "www.foo.com:110", // https_uri 208 "www.foo.com:110", // https_uri
201 "ftp.foo.com:121", // ftp_uri 209 "ftp.foo.com:121", // ftp_uri
202 "socks.com:888", // socks_uri 210 "socks.com:888", // socks_uri
203 }, 211 },
204 212
205 // Expected result. 213 // Expected result.
206 false, // auto_detect 214 ProxyPrefs::MODE_FIXED_SERVERS, // mode
207 GURL(), // pac_url 215 "", // pac_url
208 net::ProxyRulesExpectation::PerSchemeWithSocks( // proxy_rules 216 net::ProxyRulesExpectation::PerSchemeWithSocks( // proxy_rules
209 "www.google.com:80", // http 217 "www.google.com:80", // http
210 "https://www.foo.com:110", // https 218 "https://www.foo.com:110", // https
211 "ftp.foo.com:121", // ftp 219 "ftp.foo.com:121", // ftp
212 "socks5://socks.com:888", // fallback proxy 220 "socks5://socks.com:888", // fallback proxy
213 ""), // bypass rules 221 ""), // bypass rules
214 }, 222 },
215 223
216 { 224 {
217 TEST_DESC("Bypass rules"), 225 TEST_DESC("Bypass rules"),
218 226
219 true, // is_valid 227 true, // is_valid
220 true, // test_read_write_access 228 true, // test_read_write_access
221 229
222 { // Input. 230 { // Input.
223 MK_MODE(SINGLE_PROXY), // mode 231 MK_MODE(SINGLE_PROXY), // mode
224 NULL, // pac_url 232 NULL, // pac_url
225 "www.google.com", // single_uri 233 "www.google.com", // single_uri
226 NULL, NULL, NULL, NULL, // per-proto 234 NULL, NULL, NULL, NULL, // per-proto
227 ".google.com, foo.com:99, 1.2.3.4:22, 127.0.0.1/8", // bypass_rules 235 ".google.com, foo.com:99, 1.2.3.4:22, 127.0.0.1/8", // bypass_rules
228 }, 236 },
229 237
230 // Expected result. 238 // Expected result.
231 false, // auto_detect 239 ProxyPrefs::MODE_FIXED_SERVERS, // mode
232 GURL(), // pac_url 240 "", // pac_url
233 net::ProxyRulesExpectation::Single( // proxy_rules 241 net::ProxyRulesExpectation::Single( // proxy_rules
234 "www.google.com:80", // single proxy 242 "www.google.com:80", // single proxy
235 "*.google.com,*foo.com:99,1.2.3.4:22,127.0.0.1/8"), // bypass_rules 243 "*.google.com,*foo.com:99,1.2.3.4:22,127.0.0.1/8"), // bypass_rules
236 }, 244 },
237 }; // tests 245 }; // tests
238 246
239 } // namespace 247 } // namespace
240 248
241 class ProxyConfigServiceImplTest : public PlatformTest { 249 class ProxyConfigServiceImplTest : public PlatformTest {
242 protected: 250 protected:
243 ProxyConfigServiceImplTest() 251 ProxyConfigServiceImplTest() {
244 : ui_thread_(BrowserThread::UI, &message_loop_), 252 browser::RegisterLocalState(&local_state_);
245 io_thread_(BrowserThread::IO, &message_loop_) {
246 }
247
248 virtual ~ProxyConfigServiceImplTest() {
249 config_service_ = NULL;
250 MessageLoop::current()->RunAllPending();
251 } 253 }
252 254
253 void CreateConfigService( 255 void CreateConfigService(
254 const ProxyConfigServiceImpl::ProxyConfig& init_config) { 256 const ProxyConfigServiceImpl::ProxyConfig& init_config) {
255 // Instantiate proxy config service with |init_config|. 257 // Instantiate proxy config service with |init_config|.
256 config_service_ = new ProxyConfigServiceImpl(init_config); 258 config_service_.reset(
259 new ProxyConfigServiceImpl(&local_state_, init_config));
257 } 260 }
258 261
259 void SetAutomaticProxy( 262 void SetAutomaticProxy(
260 ProxyConfigServiceImpl::ProxyConfig::Mode mode, 263 ProxyConfigServiceImpl::ProxyConfig::Mode mode,
261 ProxyConfigServiceImpl::ProxyConfig::Source source, 264 ProxyConfigServiceImpl::ProxyConfig::Source source,
262 const char* pac_url, 265 const char* pac_url,
263 ProxyConfigServiceImpl::ProxyConfig* config, 266 ProxyConfigServiceImpl::ProxyConfig* config,
264 ProxyConfigServiceImpl::ProxyConfig::AutomaticProxy* automatic_proxy) { 267 ProxyConfigServiceImpl::ProxyConfig::AutomaticProxy* automatic_proxy) {
265 config->mode = mode; 268 config->mode = mode;
266 automatic_proxy->source = source; 269 automatic_proxy->source = source;
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 // Rest of protos should be writeable by owner, but not writeable by 393 // Rest of protos should be writeable by owner, but not writeable by
391 // non-owner. 394 // non-owner.
392 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(all_schemes); ++i) { 395 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(all_schemes); ++i) {
393 if (scheme == all_schemes[i]) 396 if (scheme == all_schemes[i])
394 continue; 397 continue;
395 EXPECT_TRUE(config.CanBeWrittenByUser(true, all_schemes[i])); 398 EXPECT_TRUE(config.CanBeWrittenByUser(true, all_schemes[i]));
396 EXPECT_FALSE(config.CanBeWrittenByUser(false, all_schemes[i])); 399 EXPECT_FALSE(config.CanBeWrittenByUser(false, all_schemes[i]));
397 } 400 }
398 } 401 }
399 402
400 // Synchronously gets the latest proxy config. 403 void CheckPrefProxyConfig(const TestData& test_data) {
401 bool SyncGetLatestProxyConfig(net::ProxyConfig* config) { 404 ProxyConfigDictionary proxy_dict(
402 // Let message loop process all messages. 405 local_state_.GetDictionary(prefs::kProxy));
403 MessageLoop::current()->RunAllPending(); 406
404 // Calls IOGetProxyConfig (which is called from 407 ProxyPrefs::ProxyMode actual_mode;
405 // ProxyConfigService::GetLatestProxyConfig), running on faked IO thread. 408 proxy_dict.GetMode(&actual_mode);
406 return config_service_->IOGetProxyConfig(config); 409 EXPECT_EQ(test_data.mode, actual_mode);
410
411 std::string actual_pac_url;
412 proxy_dict.GetPacUrl(&actual_pac_url);
413 EXPECT_EQ(test_data.pac_url, actual_pac_url);
414
415 std::string actual_rules_string;
416 proxy_dict.GetProxyServer(&actual_rules_string);
417 net::ProxyConfig::ProxyRules actual_rules;
418 actual_rules.ParseFromString(actual_rules_string);
419 std::string actual_bypass_rules_string;
420 proxy_dict.GetBypassList(&actual_bypass_rules_string);
421 actual_rules.bypass_rules.ParseFromString(actual_bypass_rules_string);
422 EXPECT_TRUE(test_data.proxy_rules.Matches(actual_rules));
407 } 423 }
408 424
409 ProxyConfigServiceImpl* config_service() const { 425 ProxyConfigServiceImpl* config_service() const {
410 return config_service_; 426 return config_service_.get();
427 }
428
429 PrefService* local_state() {
430 return &local_state_;
411 } 431 }
412 432
413 private: 433 private:
414 bool CanBeWrittenByOwner( 434 bool CanBeWrittenByOwner(
415 ProxyConfigServiceImpl::ProxyConfig::Source source) const { 435 ProxyConfigServiceImpl::ProxyConfig::Source source) const {
416 return source == MK_SRC(POLICY) ? false : true; 436 return source == MK_SRC(POLICY) ? false : true;
417 } 437 }
418 438
439 TestingPrefService local_state_;
419 ScopedStubCrosEnabler stub_cros_enabler_; 440 ScopedStubCrosEnabler stub_cros_enabler_;
420 MessageLoop message_loop_; 441 scoped_ptr<ProxyConfigServiceImpl> config_service_;
421 BrowserThread ui_thread_;
422 BrowserThread io_thread_;
423
424 scoped_refptr<ProxyConfigServiceImpl> config_service_;
425 }; 442 };
426 443
427 TEST_F(ProxyConfigServiceImplTest, ChromeosProxyConfigToNetProxyConfig) { 444 TEST_F(ProxyConfigServiceImplTest, ChromeosProxyConfigToChromeProxyConfig) {
428 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 445 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
429 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i, 446 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i,
430 tests[i].description.c_str())); 447 tests[i].description.c_str()));
431 448
432 ProxyConfigServiceImpl::ProxyConfig init_config; 449 ProxyConfigServiceImpl::ProxyConfig init_config;
433 InitConfigWithTestInput(tests[i].input, MK_SRC(OWNER), &init_config); 450 InitConfigWithTestInput(tests[i].input, MK_SRC(OWNER), &init_config);
434 CreateConfigService(init_config); 451 CreateConfigService(init_config);
435 452
436 net::ProxyConfig config; 453 // Check whether the pref has been set correctly.
437 SyncGetLatestProxyConfig(&config); 454 CheckPrefProxyConfig(tests[i]);
438
439 EXPECT_EQ(tests[i].auto_detect, config.auto_detect());
440 EXPECT_EQ(tests[i].pac_url, config.pac_url());
441 EXPECT_TRUE(tests[i].proxy_rules.Matches(config.proxy_rules()));
442 } 455 }
443 } 456 }
444 457
445 TEST_F(ProxyConfigServiceImplTest, ModifyFromUI) { 458 TEST_F(ProxyConfigServiceImplTest, ModifyFromUI) {
446 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 459 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
447 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i, 460 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i,
448 tests[i].description.c_str())); 461 tests[i].description.c_str()));
449 462
450 // Init with direct. 463 // Init with direct.
451 ProxyConfigServiceImpl::ProxyConfig init_config; 464 ProxyConfigServiceImpl::ProxyConfig init_config;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 config_service()->UISetProxyConfigToProxyPerScheme("socks", 504 config_service()->UISetProxyConfigToProxyPerScheme("socks",
492 net::ProxyServer::FromURI(input.socks_uri, MK_SCHM(SOCKS5))); 505 net::ProxyServer::FromURI(input.socks_uri, MK_SCHM(SOCKS5)));
493 } 506 }
494 if (input.bypass_rules) { 507 if (input.bypass_rules) {
495 bypass_rules.ParseFromStringUsingSuffixMatching(input.bypass_rules); 508 bypass_rules.ParseFromStringUsingSuffixMatching(input.bypass_rules);
496 config_service()->UISetProxyConfigBypassRules(bypass_rules); 509 config_service()->UISetProxyConfigBypassRules(bypass_rules);
497 } 510 }
498 break; 511 break;
499 } 512 }
500 513
501 // Retrieve config from IO thread. 514 // Check whether correct configuration has been written to the pref.
502 net::ProxyConfig io_config; 515 CheckPrefProxyConfig(tests[i]);
503 SyncGetLatestProxyConfig(&io_config);
504 EXPECT_EQ(tests[i].auto_detect, io_config.auto_detect());
505 EXPECT_EQ(tests[i].pac_url, io_config.pac_url());
506 EXPECT_TRUE(tests[i].proxy_rules.Matches(io_config.proxy_rules()));
507 516
508 // Retrieve config from UI thread. 517 // Retrieve config from UI thread.
509 ProxyConfigServiceImpl::ProxyConfig ui_config; 518 ProxyConfigServiceImpl::ProxyConfig ui_config;
510 config_service()->UIGetProxyConfig(&ui_config); 519 config_service()->UIGetProxyConfig(&ui_config);
511 EXPECT_EQ(input.mode, ui_config.mode); 520 EXPECT_EQ(input.mode, ui_config.mode);
512 if (tests[i].is_valid) { 521 if (tests[i].is_valid) {
513 if (input.pac_url) 522 if (input.pac_url)
514 EXPECT_EQ(GURL(input.pac_url), ui_config.automatic_proxy.pac_url); 523 EXPECT_EQ(GURL(input.pac_url), ui_config.automatic_proxy.pac_url);
515 const net::ProxyRulesExpectation& proxy_rules = tests[i].proxy_rules; 524 const net::ProxyRulesExpectation& proxy_rules = tests[i].proxy_rules;
516 if (input.single_uri) 525 if (input.single_uri)
(...skipping 12 matching lines...) Expand all
529 EXPECT_EQ(proxy_rules.fallback_proxy, 538 EXPECT_EQ(proxy_rules.fallback_proxy,
530 ui_config.socks_proxy.server.ToURI()); 539 ui_config.socks_proxy.server.ToURI());
531 } 540 }
532 if (input.bypass_rules) 541 if (input.bypass_rules)
533 EXPECT_TRUE(bypass_rules.Equals(ui_config.bypass_rules)); 542 EXPECT_TRUE(bypass_rules.Equals(ui_config.bypass_rules));
534 } 543 }
535 } 544 }
536 } 545 }
537 546
538 TEST_F(ProxyConfigServiceImplTest, ProxyChangedObserver) { 547 TEST_F(ProxyConfigServiceImplTest, ProxyChangedObserver) {
539 // This is used to observe for OnProxyConfigChanged notification.
540 class ProxyChangedObserver : public net::ProxyConfigService::Observer {
541 public:
542 explicit ProxyChangedObserver(
543 const scoped_refptr<ProxyConfigServiceImpl>& config_service)
544 : config_service_(config_service) {
545 config_service_->AddObserver(this);
546 }
547 virtual ~ProxyChangedObserver() {
548 config_service_->RemoveObserver(this);
549 }
550 const net::ProxyConfig& config() const {
551 return config_;
552 }
553
554 private:
555 virtual void OnProxyConfigChanged(const net::ProxyConfig& config) {
556 config_ = config;
557 }
558
559 scoped_refptr<ProxyConfigServiceImpl> config_service_;
560 net::ProxyConfig config_;
561 };
562
563 // Init with direct. 548 // Init with direct.
564 ProxyConfigServiceImpl::ProxyConfig init_config; 549 ProxyConfigServiceImpl::ProxyConfig init_config;
565 SetAutomaticProxy(MK_MODE(DIRECT), MK_SRC(OWNER), NULL, &init_config, 550 SetAutomaticProxy(MK_MODE(DIRECT), MK_SRC(OWNER), NULL, &init_config,
566 &init_config.automatic_proxy); 551 &init_config.automatic_proxy);
567 CreateConfigService(init_config); 552 CreateConfigService(init_config);
568 553
569 ProxyChangedObserver observer(config_service()); 554 PrefObserverMock observer;
555 PrefChangeRegistrar registrar;
556 registrar.Init(local_state());
557 registrar.Add(prefs::kProxy, &observer);
570 558
571 // Set to pac script from UI. 559 // Set to pac script from UI.
572 EXPECT_TRUE(config_service()->UISetProxyConfigToPACScript( 560 GURL pac_url("http://wpad.dat");
573 GURL("http://wpad.dat"))); 561 scoped_ptr<DictionaryValue> expected_proxy_config(
574 // Retrieve config from IO thread. 562 ProxyConfigDictionary::CreatePacScript(pac_url.spec()));
575 net::ProxyConfig io_config; 563
576 SyncGetLatestProxyConfig(&io_config); 564 observer.Expect(local_state(), prefs::kProxy, expected_proxy_config.get());
565 EXPECT_TRUE(config_service()->UISetProxyConfigToPACScript(pac_url));
566 Mock::VerifyAndClearExpectations(&observer);
577 567
578 // Observer should have gotten the same new proxy config. 568 // Observer should have gotten the same new proxy config.
579 EXPECT_TRUE(io_config.Equals(observer.config())); 569 const DictionaryValue* actual_config =
570 local_state()->GetDictionary(prefs::kProxy);
571 EXPECT_TRUE(expected_proxy_config->Equals(actual_config));
580 } 572 }
581 573
582 TEST_F(ProxyConfigServiceImplTest, SerializeAndDeserialize) { 574 TEST_F(ProxyConfigServiceImplTest, SerializeAndDeserialize) {
583 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 575 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
584 if (!tests[i].is_valid) 576 if (!tests[i].is_valid)
585 continue; 577 continue;
586 578
587 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i, 579 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i,
588 tests[i].description.c_str())); 580 tests[i].description.c_str()));
589 581
590 ProxyConfigServiceImpl::ProxyConfig source_config; 582 ProxyConfigServiceImpl::ProxyConfig source_config;
591 InitConfigWithTestInput(tests[i].input, MK_SRC(OWNER), &source_config); 583 InitConfigWithTestInput(tests[i].input, MK_SRC(OWNER), &source_config);
592 584
593 // Serialize source_config into std::string. 585 // Serialize source_config into std::string.
594 std::string serialized_value; 586 std::string serialized_value;
595 EXPECT_TRUE(source_config.Serialize(&serialized_value)); 587 EXPECT_TRUE(source_config.Serialize(&serialized_value));
596 588
597 // Deserialize std:string into target_config. 589 // Deserialize std:string into target_config.
598 ProxyConfigServiceImpl::ProxyConfig target_config; 590 ProxyConfigServiceImpl::ProxyConfig target_config;
599 EXPECT_TRUE(target_config.Deserialize(serialized_value)); 591 EXPECT_TRUE(target_config.Deserialize(serialized_value));
600 592
601 // Compare the configs after serialization and deserialization. 593 // Compare the configs after serialization and deserialization.
602 net::ProxyConfig net_src_cfg; 594 scoped_ptr<DictionaryValue> src_cfg_dict(
603 net::ProxyConfig net_tgt_cfg; 595 source_config.ToProxyConfigDictionary());
604 source_config.ToNetProxyConfig(&net_src_cfg); 596 scoped_ptr<DictionaryValue> tgt_cfg_dict(
605 target_config.ToNetProxyConfig(&net_tgt_cfg); 597 target_config.ToProxyConfigDictionary());
606 #if !defined(NDEBUG) 598 #if !defined(NDEBUG)
607 if (!net_src_cfg.Equals(net_tgt_cfg)) { 599 if (!src_cfg_dict->Equals(tgt_cfg_dict.get())) {
608 std::string src_output, tgt_output; 600 std::string src_output, tgt_output;
609 JSONStringValueSerializer src_serializer(&src_output); 601 JSONStringValueSerializer src_serializer(&src_output);
610 src_serializer.Serialize(*net_src_cfg.ToValue()); 602 src_serializer.Serialize(*src_cfg_dict);
611 JSONStringValueSerializer tgt_serializer(&tgt_output); 603 JSONStringValueSerializer tgt_serializer(&tgt_output);
612 tgt_serializer.Serialize(*net_tgt_cfg.ToValue()); 604 tgt_serializer.Serialize(*tgt_cfg_dict);
613 VLOG(1) << "source:\n" << src_output 605 VLOG(1) << "source:\n" << src_output
614 << "\ntarget:\n" << tgt_output; 606 << "\ntarget:\n" << tgt_output;
615 } 607 }
616 #endif // !defined(NDEBUG) 608 #endif // !defined(NDEBUG)
617 EXPECT_TRUE(net_src_cfg.Equals(net_tgt_cfg)); 609 EXPECT_TRUE(src_cfg_dict->Equals(tgt_cfg_dict.get()));
618 } 610 }
619 } 611 }
620 612
621 TEST_F(ProxyConfigServiceImplTest, ReadWriteAccessForPolicySource) { 613 TEST_F(ProxyConfigServiceImplTest, ReadWriteAccessForPolicySource) {
622 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 614 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
623 if (!tests[i].test_read_write_access) 615 if (!tests[i].test_read_write_access)
624 continue; 616 continue;
625 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i, 617 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i,
626 tests[i].description.c_str())); 618 tests[i].description.c_str()));
627 TestReadWriteAccessForMode(tests[i].input, MK_SRC(POLICY)); 619 TestReadWriteAccessForMode(tests[i].input, MK_SRC(POLICY));
(...skipping 23 matching lines...) Expand all
651 TestReadWriteAccessForScheme(MK_SRC(POLICY), socks_uri, "socks"); 643 TestReadWriteAccessForScheme(MK_SRC(POLICY), socks_uri, "socks");
652 644
653 // Init with owner source. 645 // Init with owner source.
654 TestReadWriteAccessForScheme(MK_SRC(OWNER), http_uri, "http"); 646 TestReadWriteAccessForScheme(MK_SRC(OWNER), http_uri, "http");
655 TestReadWriteAccessForScheme(MK_SRC(OWNER), https_uri, "https"); 647 TestReadWriteAccessForScheme(MK_SRC(OWNER), https_uri, "https");
656 TestReadWriteAccessForScheme(MK_SRC(OWNER), ftp_uri, "ftp"); 648 TestReadWriteAccessForScheme(MK_SRC(OWNER), ftp_uri, "ftp");
657 TestReadWriteAccessForScheme(MK_SRC(OWNER), socks_uri, "socks"); 649 TestReadWriteAccessForScheme(MK_SRC(OWNER), socks_uri, "socks");
658 } 650 }
659 651
660 } // namespace chromeos 652 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698