OLD | NEW |
---|---|
1 // Copyright (c) 2011 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/json/json_value_serializer.h" | 12 #include "base/json/json_value_serializer.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
15 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
16 #include "chrome/browser/chromeos/cros/cros_library.h" | 16 #include "chrome/browser/chromeos/cros/cros_library.h" |
17 #include "chrome/common/pref_names.h" | |
18 #include "chrome/test/base/testing_pref_service.h" | |
17 #include "content/browser/browser_thread.h" | 19 #include "content/browser/browser_thread.h" |
18 #include "net/proxy/proxy_config_service_common_unittest.h" | 20 #include "net/proxy/proxy_config_service_common_unittest.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
20 #include "testing/platform_test.h" | |
21 | 22 |
22 namespace chromeos { | 23 namespace chromeos { |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 struct Input { // Fields of chromeos::ProxyConfigServiceImpl::ProxyConfig. | 27 struct Input { // Fields of chromeos::ProxyConfigServiceImpl::ProxyConfig. |
27 ProxyConfigServiceImpl::ProxyConfig::Mode mode; | 28 ProxyConfigServiceImpl::ProxyConfig::Mode mode; |
28 const char* pac_url; | 29 const char* pac_url; |
29 const char* single_uri; | 30 const char* single_uri; |
30 const char* http_uri; | 31 const char* http_uri; |
31 const char* https_uri; | 32 const char* https_uri; |
32 const char* ftp_uri; | 33 const char* ftp_uri; |
33 const char* socks_uri; | 34 const char* socks_uri; |
34 const char* bypass_rules; | 35 const char* bypass_rules; |
35 }; | 36 }; |
36 | 37 |
37 // Builds an identifier for each test in an array. | 38 // Builds an identifier for each test in an array. |
38 #define TEST_DESC(desc) base::StringPrintf("at line %d <%s>", __LINE__, desc) | 39 #define TEST_DESC(desc) base::StringPrintf("at line %d <%s>", __LINE__, desc) |
39 | 40 |
40 // Shortcuts to declare enums within chromeos's ProxyConfig. | 41 // Shortcuts to declare enums within chromeos's ProxyConfig. |
41 #define MK_MODE(mode) ProxyConfigServiceImpl::ProxyConfig::MODE_##mode | 42 #define MK_MODE(mode) ProxyConfigServiceImpl::ProxyConfig::MODE_##mode |
42 #define MK_SRC(src) ProxyConfigServiceImpl::ProxyConfig::SOURCE_##src | |
43 #define MK_SCHM(scheme) net::ProxyServer::SCHEME_##scheme | 43 #define MK_SCHM(scheme) net::ProxyServer::SCHEME_##scheme |
44 #define MK_AVAIL(avail) net::ProxyConfigService::CONFIG_##avail | |
44 | 45 |
45 // Inspired from net/proxy/proxy_config_service_linux_unittest.cc. | 46 // Inspired from net/proxy/proxy_config_service_linux_unittest.cc. |
46 const struct { | 47 static const struct TestParams { |
47 // Short description to identify the test | 48 // Short description to identify the test |
48 std::string description; | 49 std::string description; |
49 | 50 |
50 bool is_valid; | 51 bool is_valid; |
51 bool test_read_write_access; | |
52 | 52 |
53 Input input; | 53 Input input; |
54 | 54 |
55 // Expected outputs from fields of net::ProxyConfig (via IO). | 55 // Expected outputs from fields of net::ProxyConfig (via IO). |
56 bool auto_detect; | 56 bool auto_detect; |
57 GURL pac_url; | 57 GURL pac_url; |
58 net::ProxyRulesExpectation proxy_rules; | 58 net::ProxyRulesExpectation proxy_rules; |
59 } tests[] = { | 59 } tests[] = { |
60 { | 60 { |
61 TEST_DESC("No proxying"), | 61 TEST_DESC("No proxying"), |
62 | 62 |
63 true, // is_valid | 63 true, // is_valid |
64 true, // test_read_write_access | |
65 | 64 |
66 { // Input. | 65 { // Input. |
67 MK_MODE(DIRECT), // mode | 66 MK_MODE(DIRECT), // mode |
68 }, | 67 }, |
69 | 68 |
70 // Expected result. | 69 // Expected result. |
71 false, // auto_detect | 70 false, // auto_detect |
72 GURL(), // pac_url | 71 GURL(), // pac_url |
73 net::ProxyRulesExpectation::Empty(), // proxy_rules | 72 net::ProxyRulesExpectation::Empty(), // proxy_rules |
74 }, | 73 }, |
75 | 74 |
76 { | 75 { |
77 TEST_DESC("Auto detect"), | 76 TEST_DESC("Auto detect"), |
78 | 77 |
79 true, // is_valid | 78 true, // is_valid |
80 true, // test_read_write_access | |
81 | 79 |
82 { // Input. | 80 { // Input. |
83 MK_MODE(AUTO_DETECT), // mode | 81 MK_MODE(AUTO_DETECT), // mode |
84 }, | 82 }, |
85 | 83 |
86 // Expected result. | 84 // Expected result. |
87 true, // auto_detect | 85 true, // auto_detect |
88 GURL(), // pac_url | 86 GURL(), // pac_url |
89 net::ProxyRulesExpectation::Empty(), // proxy_rules | 87 net::ProxyRulesExpectation::Empty(), // proxy_rules |
90 }, | 88 }, |
91 | 89 |
92 { | 90 { |
93 TEST_DESC("Valid PAC URL"), | 91 TEST_DESC("Valid PAC URL"), |
94 | 92 |
95 true, // is_valid | 93 true, // is_valid |
96 true, // test_read_write_access | |
97 | 94 |
98 { // Input. | 95 { // Input. |
99 MK_MODE(PAC_SCRIPT), // mode | 96 MK_MODE(PAC_SCRIPT), // mode |
100 "http://wpad/wpad.dat", // pac_url | 97 "http://wpad/wpad.dat", // pac_url |
101 }, | 98 }, |
102 | 99 |
103 // Expected result. | 100 // Expected result. |
104 false, // auto_detect | 101 false, // auto_detect |
105 GURL("http://wpad/wpad.dat"), // pac_url | 102 GURL("http://wpad/wpad.dat"), // pac_url |
106 net::ProxyRulesExpectation::Empty(), // proxy_rules | 103 net::ProxyRulesExpectation::Empty(), // proxy_rules |
107 }, | 104 }, |
108 | 105 |
109 { | 106 { |
110 TEST_DESC("Invalid PAC URL"), | 107 TEST_DESC("Invalid PAC URL"), |
111 | 108 |
112 false, // is_valid | 109 false, // is_valid |
113 false, // test_read_write_access | |
114 | 110 |
115 { // Input. | 111 { // Input. |
116 MK_MODE(PAC_SCRIPT), // mode | 112 MK_MODE(PAC_SCRIPT), // mode |
117 "wpad.dat", // pac_url | 113 "wpad.dat", // pac_url |
118 }, | 114 }, |
119 | 115 |
120 // Expected result. | 116 // Expected result. |
121 false, // auto_detect | 117 false, // auto_detect |
122 GURL(), // pac_url | 118 GURL(), // pac_url |
123 net::ProxyRulesExpectation::Empty(), // proxy_rules | 119 net::ProxyRulesExpectation::Empty(), // proxy_rules |
124 }, | 120 }, |
125 | 121 |
126 { | 122 { |
127 TEST_DESC("Single-host in proxy list"), | 123 TEST_DESC("Single-host in proxy list"), |
128 | 124 |
129 true, // is_valid | 125 true, // is_valid |
130 true, // test_read_write_access | |
131 | 126 |
132 { // Input. | 127 { // Input. |
133 MK_MODE(SINGLE_PROXY), // mode | 128 MK_MODE(SINGLE_PROXY), // mode |
134 NULL, // pac_url | 129 NULL, // pac_url |
135 "www.google.com", // single_uri | 130 "www.google.com", // single_uri |
136 }, | 131 }, |
137 | 132 |
138 // Expected result. | 133 // Expected result. |
139 false, // auto_detect | 134 false, // auto_detect |
140 GURL(), // pac_url | 135 GURL(), // pac_url |
141 net::ProxyRulesExpectation::Single( // proxy_rules | 136 net::ProxyRulesExpectation::Single( // proxy_rules |
142 "www.google.com:80", // single proxy | 137 "www.google.com:80", // single proxy |
143 ""), // bypass rules | 138 ""), // bypass rules |
144 }, | 139 }, |
145 | 140 |
146 { | 141 { |
147 TEST_DESC("Single-host, different port"), | 142 TEST_DESC("Single-host, different port"), |
148 | 143 |
149 true, // is_valid | 144 true, // is_valid |
150 false, // test_read_write_access | |
151 | 145 |
152 { // Input. | 146 { // Input. |
153 MK_MODE(SINGLE_PROXY), // mode | 147 MK_MODE(SINGLE_PROXY), // mode |
154 NULL, // pac_url | 148 NULL, // pac_url |
155 "www.google.com:99", // single_uri | 149 "www.google.com:99", // single_uri |
156 }, | 150 }, |
157 | 151 |
158 // Expected result. | 152 // Expected result. |
159 false, // auto_detect | 153 false, // auto_detect |
160 GURL(), // pac_url | 154 GURL(), // pac_url |
161 net::ProxyRulesExpectation::Single( // proxy_rules | 155 net::ProxyRulesExpectation::Single( // proxy_rules |
162 "www.google.com:99", // single | 156 "www.google.com:99", // single |
163 ""), // bypass rules | 157 ""), // bypass rules |
164 }, | 158 }, |
165 | 159 |
166 { | 160 { |
167 TEST_DESC("Tolerate a scheme"), | 161 TEST_DESC("Tolerate a scheme"), |
168 | 162 |
169 true, // is_valid | 163 true, // is_valid |
170 false, // test_read_write_access | |
171 | 164 |
172 { // Input. | 165 { // Input. |
173 MK_MODE(SINGLE_PROXY), // mode | 166 MK_MODE(SINGLE_PROXY), // mode |
174 NULL, // pac_url | 167 NULL, // pac_url |
175 "http://www.google.com:99", // single_uri | 168 "http://www.google.com:99", // single_uri |
176 }, | 169 }, |
177 | 170 |
178 // Expected result. | 171 // Expected result. |
179 false, // auto_detect | 172 false, // auto_detect |
180 GURL(), // pac_url | 173 GURL(), // pac_url |
181 net::ProxyRulesExpectation::Single( // proxy_rules | 174 net::ProxyRulesExpectation::Single( // proxy_rules |
182 "www.google.com:99", // single proxy | 175 "www.google.com:99", // single proxy |
183 ""), // bypass rules | 176 ""), // bypass rules |
184 }, | 177 }, |
185 | 178 |
186 { | 179 { |
187 TEST_DESC("Per-scheme proxy rules"), | 180 TEST_DESC("Per-scheme proxy rules"), |
188 | 181 |
189 true, // is_valid | 182 true, // is_valid |
190 true, // test_read_write_access | |
191 | 183 |
192 { // Input. | 184 { // Input. |
193 MK_MODE(PROXY_PER_SCHEME), // mode | 185 MK_MODE(PROXY_PER_SCHEME), // mode |
194 NULL, // pac_url | 186 NULL, // pac_url |
195 NULL, // single_uri | 187 NULL, // single_uri |
196 "www.google.com:80", // http_uri | 188 "www.google.com:80", // http_uri |
197 "www.foo.com:110", // https_uri | 189 "www.foo.com:110", // https_uri |
198 "ftp.foo.com:121", // ftp_uri | 190 "ftp.foo.com:121", // ftp_uri |
199 "socks.com:888", // socks_uri | 191 "socks.com:888", // socks_uri |
200 }, | 192 }, |
201 | 193 |
202 // Expected result. | 194 // Expected result. |
203 false, // auto_detect | 195 false, // auto_detect |
204 GURL(), // pac_url | 196 GURL(), // pac_url |
205 net::ProxyRulesExpectation::PerSchemeWithSocks( // proxy_rules | 197 net::ProxyRulesExpectation::PerSchemeWithSocks( // proxy_rules |
206 "www.google.com:80", // http | 198 "www.google.com:80", // http |
207 "https://www.foo.com:110", // https | 199 "https://www.foo.com:110", // https |
208 "ftp.foo.com:121", // ftp | 200 "ftp.foo.com:121", // ftp |
209 "socks5://socks.com:888", // fallback proxy | 201 "socks5://socks.com:888", // fallback proxy |
210 ""), // bypass rules | 202 ""), // bypass rules |
211 }, | 203 }, |
212 | 204 |
213 { | 205 { |
214 TEST_DESC("Bypass rules"), | 206 TEST_DESC("Bypass rules"), |
215 | 207 |
216 true, // is_valid | 208 true, // is_valid |
217 true, // test_read_write_access | |
218 | 209 |
219 { // Input. | 210 { // Input. |
220 MK_MODE(SINGLE_PROXY), // mode | 211 MK_MODE(SINGLE_PROXY), // mode |
221 NULL, // pac_url | 212 NULL, // pac_url |
222 "www.google.com", // single_uri | 213 "www.google.com", // single_uri |
223 NULL, NULL, NULL, NULL, // per-proto | 214 NULL, NULL, NULL, NULL, // per-proto |
224 "*.google.com, *foo.com:99, 1.2.3.4:22, 127.0.0.1/8", // bypass_rules | 215 "*.google.com, *foo.com:99, 1.2.3.4:22, 127.0.0.1/8", // bypass_rules |
225 }, | 216 }, |
226 | 217 |
227 // Expected result. | 218 // Expected result. |
228 false, // auto_detect | 219 false, // auto_detect |
229 GURL(), // pac_url | 220 GURL(), // pac_url |
230 net::ProxyRulesExpectation::Single( // proxy_rules | 221 net::ProxyRulesExpectation::Single( // proxy_rules |
231 "www.google.com:80", // single proxy | 222 "www.google.com:80", // single proxy |
232 "*.google.com,*foo.com:99,1.2.3.4:22,127.0.0.1/8"), // bypass_rules | 223 "*.google.com,*foo.com:99,1.2.3.4:22,127.0.0.1/8"), // bypass_rules |
233 }, | 224 }, |
234 }; // tests | 225 }; // tests |
235 | 226 |
236 } // namespace | 227 template<typename TESTBASE> |
228 class ProxyConfigServiceImplTestBase : public TESTBASE { | |
229 protected: | |
230 ProxyConfigServiceImplTestBase() | |
231 : ui_thread_(BrowserThread::UI, &loop_), | |
232 io_thread_(BrowserThread::IO, &loop_) {} | |
237 | 233 |
238 class ProxyConfigServiceImplTest : public PlatformTest { | 234 virtual void Init(PrefService* pref_service) { |
239 protected: | 235 ASSERT_TRUE(pref_service); |
240 ProxyConfigServiceImplTest() | 236 PrefProxyConfigTracker::RegisterPrefs(pref_service); |
241 : ui_thread_(BrowserThread::UI, &message_loop_), | 237 proxy_config_service_.reset(new ChromeProxyConfigService(NULL)); |
242 io_thread_(BrowserThread::IO, &message_loop_) { | 238 config_service_impl_.reset(new ProxyConfigServiceImpl(pref_service)); |
239 config_service_impl_->SetChromeProxyConfigService( | |
240 proxy_config_service_.get()); | |
241 // SetChromeProxyConfigService triggers update of initial prefs proxy | |
242 // config by tracker to chrome proxy config service, so flush all pending | |
243 // tasks so that tests start fresh. | |
244 loop_.RunAllPending(); | |
243 } | 245 } |
244 | 246 |
245 virtual ~ProxyConfigServiceImplTest() { | 247 virtual void TearDown() { |
246 config_service_ = NULL; | 248 config_service_impl_->DetachFromPrefService(); |
247 MessageLoop::current()->RunAllPending(); | 249 loop_.RunAllPending(); |
248 } | 250 config_service_impl_.reset(); |
249 | 251 proxy_config_service_.reset(); |
250 void CreateConfigService( | |
251 const ProxyConfigServiceImpl::ProxyConfig& init_config) { | |
252 // Instantiate proxy config service with |init_config|. | |
253 config_service_ = new ProxyConfigServiceImpl(init_config); | |
254 config_service_->SetTesting(); | |
255 } | 252 } |
256 | 253 |
257 void SetAutomaticProxy( | 254 void SetAutomaticProxy( |
258 ProxyConfigServiceImpl::ProxyConfig::Mode mode, | 255 ProxyConfigServiceImpl::ProxyConfig::Mode mode, |
259 ProxyConfigServiceImpl::ProxyConfig::Source source, | |
260 const char* pac_url, | 256 const char* pac_url, |
261 ProxyConfigServiceImpl::ProxyConfig* config, | 257 ProxyConfigServiceImpl::ProxyConfig* config, |
262 ProxyConfigServiceImpl::ProxyConfig::AutomaticProxy* automatic_proxy) { | 258 ProxyConfigServiceImpl::ProxyConfig::AutomaticProxy* automatic_proxy) { |
263 config->mode = mode; | 259 config->mode = mode; |
264 automatic_proxy->source = source; | 260 config->state = ProxyPrefs::CONFIG_SYSTEM; |
265 if (pac_url) | 261 if (pac_url) |
266 automatic_proxy->pac_url = GURL(pac_url); | 262 automatic_proxy->pac_url = GURL(pac_url); |
267 } | 263 } |
268 | 264 |
269 void SetManualProxy( | 265 void SetManualProxy( |
270 ProxyConfigServiceImpl::ProxyConfig::Mode mode, | 266 ProxyConfigServiceImpl::ProxyConfig::Mode mode, |
271 ProxyConfigServiceImpl::ProxyConfig::Source source, | |
272 const char* server_uri, | 267 const char* server_uri, |
273 net::ProxyServer::Scheme scheme, | 268 net::ProxyServer::Scheme scheme, |
274 ProxyConfigServiceImpl::ProxyConfig* config, | 269 ProxyConfigServiceImpl::ProxyConfig* config, |
275 ProxyConfigServiceImpl::ProxyConfig::ManualProxy* manual_proxy) { | 270 ProxyConfigServiceImpl::ProxyConfig::ManualProxy* manual_proxy) { |
276 if (!server_uri) | 271 if (!server_uri) |
277 return; | 272 return; |
278 config->mode = mode; | 273 config->mode = mode; |
279 manual_proxy->source = source; | 274 config->state = ProxyPrefs::CONFIG_SYSTEM; |
280 manual_proxy->server = net::ProxyServer::FromURI(server_uri, scheme); | 275 manual_proxy->server = net::ProxyServer::FromURI(server_uri, scheme); |
281 } | 276 } |
282 | 277 |
283 void InitConfigWithTestInput( | 278 void InitConfigWithTestInput( |
284 const Input& input, ProxyConfigServiceImpl::ProxyConfig::Source source, | 279 const Input& input, ProxyConfigServiceImpl::ProxyConfig* test_config) { |
285 ProxyConfigServiceImpl::ProxyConfig* init_config) { | |
286 switch (input.mode) { | 280 switch (input.mode) { |
287 case MK_MODE(DIRECT): | 281 case MK_MODE(DIRECT): |
288 case MK_MODE(AUTO_DETECT): | 282 case MK_MODE(AUTO_DETECT): |
289 case MK_MODE(PAC_SCRIPT): | 283 case MK_MODE(PAC_SCRIPT): |
290 SetAutomaticProxy(input.mode, source, input.pac_url, init_config, | 284 SetAutomaticProxy(input.mode, input.pac_url, test_config, |
291 &init_config->automatic_proxy); | 285 &test_config->automatic_proxy); |
292 return; | 286 return; |
293 case MK_MODE(SINGLE_PROXY): | 287 case MK_MODE(SINGLE_PROXY): |
294 SetManualProxy(input.mode, source, input.single_uri, MK_SCHM(HTTP), | 288 SetManualProxy(input.mode, input.single_uri, MK_SCHM(HTTP), |
295 init_config, &init_config->single_proxy); | 289 test_config, &test_config->single_proxy); |
296 break; | 290 break; |
297 case MK_MODE(PROXY_PER_SCHEME): | 291 case MK_MODE(PROXY_PER_SCHEME): |
298 SetManualProxy(input.mode, source, input.http_uri, MK_SCHM(HTTP), | 292 SetManualProxy(input.mode, input.http_uri, MK_SCHM(HTTP), |
299 init_config, &init_config->http_proxy); | 293 test_config, &test_config->http_proxy); |
300 SetManualProxy(input.mode, source, input.https_uri, MK_SCHM(HTTPS), | 294 SetManualProxy(input.mode, input.https_uri, MK_SCHM(HTTPS), |
301 init_config, &init_config->https_proxy); | 295 test_config, &test_config->https_proxy); |
302 SetManualProxy(input.mode, source, input.ftp_uri, MK_SCHM(HTTP), | 296 SetManualProxy(input.mode, input.ftp_uri, MK_SCHM(HTTP), |
303 init_config, &init_config->ftp_proxy); | 297 test_config, &test_config->ftp_proxy); |
304 SetManualProxy(input.mode, source, input.socks_uri, MK_SCHM(SOCKS5), | 298 SetManualProxy(input.mode, input.socks_uri, MK_SCHM(SOCKS5), |
305 init_config, &init_config->socks_proxy); | 299 test_config, &test_config->socks_proxy); |
306 break; | 300 break; |
307 } | 301 } |
308 if (input.bypass_rules) { | 302 if (input.bypass_rules) |
309 init_config->bypass_rules.ParseFromString(input.bypass_rules); | 303 test_config->bypass_rules.ParseFromString(input.bypass_rules); |
310 } | |
311 } | |
312 | |
313 void TestReadWriteAccessForMode(const Input& input, | |
314 ProxyConfigServiceImpl::ProxyConfig::Source source) { | |
315 // Init config from |source|. | |
316 ProxyConfigServiceImpl::ProxyConfig init_config; | |
317 InitConfigWithTestInput(input, source, &init_config); | |
318 CreateConfigService(init_config); | |
319 | |
320 ProxyConfigServiceImpl::ProxyConfig config; | |
321 config_service()->UIGetProxyConfig(&config); | |
322 | |
323 // For owner, write access to config should be equal CanBeWrittenByOwner(). | |
324 // For non-owner, config is never writeable. | |
325 bool expected_writeable_by_owner = CanBeWrittenByOwner(source); | |
326 if (config.mode == MK_MODE(PROXY_PER_SCHEME)) { | |
327 if (input.http_uri) { | |
328 EXPECT_EQ(expected_writeable_by_owner, | |
329 config.CanBeWrittenByUser(true, "http")); | |
330 EXPECT_FALSE(config.CanBeWrittenByUser(false, "http")); | |
331 } | |
332 if (input.https_uri) { | |
333 EXPECT_EQ(expected_writeable_by_owner, | |
334 config.CanBeWrittenByUser(true, "http")); | |
335 EXPECT_FALSE(config.CanBeWrittenByUser(false, "https")); | |
336 } | |
337 if (input.ftp_uri) { | |
338 EXPECT_EQ(expected_writeable_by_owner, | |
339 config.CanBeWrittenByUser(true, "http")); | |
340 EXPECT_FALSE(config.CanBeWrittenByUser(false, "ftp")); | |
341 } | |
342 if (input.socks_uri) { | |
343 EXPECT_EQ(expected_writeable_by_owner, | |
344 config.CanBeWrittenByUser(true, "http")); | |
345 EXPECT_FALSE(config.CanBeWrittenByUser(false, "socks")); | |
346 } | |
347 } else { | |
348 EXPECT_EQ(expected_writeable_by_owner, | |
349 config.CanBeWrittenByUser(true, std::string())); | |
350 EXPECT_FALSE(config.CanBeWrittenByUser(false, std::string())); | |
351 } | |
352 } | |
353 | |
354 void TestReadWriteAccessForScheme( | |
355 ProxyConfigServiceImpl::ProxyConfig::Source source, | |
356 const char* server_uri, | |
357 const std::string& scheme) { | |
358 // Init with manual |scheme| proxy. | |
359 ProxyConfigServiceImpl::ProxyConfig init_config; | |
360 ProxyConfigServiceImpl::ProxyConfig::ManualProxy* proxy = | |
361 init_config.MapSchemeToProxy(scheme); | |
362 net::ProxyServer::Scheme net_scheme = MK_SCHM(HTTP); | |
363 if (scheme == "http" || scheme == "ftp") | |
364 net_scheme = MK_SCHM(HTTP); | |
365 else if (scheme == "https") | |
366 net_scheme = MK_SCHM(HTTPS); | |
367 else if (scheme == "socks") | |
368 net_scheme = MK_SCHM(SOCKS4); | |
369 SetManualProxy(MK_MODE(PROXY_PER_SCHEME), source, server_uri, net_scheme, | |
370 &init_config, proxy); | |
371 CreateConfigService(init_config); | |
372 | |
373 ProxyConfigServiceImpl::ProxyConfig config; | |
374 config_service()->UIGetProxyConfig(&config); | |
375 | |
376 // For owner, write access to config should be equal CanBeWrittenByOwner(). | |
377 // For non-owner, config is never writeable. | |
378 bool expected_writeable_by_owner = CanBeWrittenByOwner(source); | |
379 EXPECT_EQ(expected_writeable_by_owner, | |
380 config.CanBeWrittenByUser(true, scheme)); | |
381 EXPECT_FALSE(config.CanBeWrittenByUser(false, scheme)); | |
382 | |
383 const char* all_schemes[] = { | |
384 "http", "https", "ftp", "socks", | |
385 }; | |
386 | |
387 // Rest of protos should be writeable by owner, but not writeable by | |
388 // non-owner. | |
389 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(all_schemes); ++i) { | |
390 if (scheme == all_schemes[i]) | |
391 continue; | |
392 EXPECT_TRUE(config.CanBeWrittenByUser(true, all_schemes[i])); | |
393 EXPECT_FALSE(config.CanBeWrittenByUser(false, all_schemes[i])); | |
394 } | |
395 } | 304 } |
396 | 305 |
397 // Synchronously gets the latest proxy config. | 306 // Synchronously gets the latest proxy config. |
398 bool SyncGetLatestProxyConfig(net::ProxyConfig* config) { | 307 net::ProxyConfigService::ConfigAvailability SyncGetLatestProxyConfig( |
308 net::ProxyConfig* config) { | |
309 *config = net::ProxyConfig(); | |
399 // Let message loop process all messages. | 310 // Let message loop process all messages. |
400 MessageLoop::current()->RunAllPending(); | 311 loop_.RunAllPending(); |
401 // Calls IOGetProxyConfig (which is called from | 312 // Calls ChromeProIOGetProxyConfig (which is called from |
402 // ProxyConfigService::GetLatestProxyConfig), running on faked IO thread. | 313 // ProxyConfigService::GetLatestProxyConfig), running on faked IO thread. |
403 return config_service_->IOGetProxyConfig(config); | 314 return proxy_config_service_->GetLatestProxyConfig(config); |
404 } | 315 } |
405 | 316 |
406 ProxyConfigServiceImpl* config_service() const { | 317 MessageLoop loop_; |
407 return config_service_; | 318 scoped_ptr<ChromeProxyConfigService> proxy_config_service_; |
319 scoped_ptr<ProxyConfigServiceImpl> config_service_impl_; | |
320 | |
321 private: | |
322 // Default stub state has ethernet as the active connected network and | |
323 // PROFILE_SHARED as profile type, which this unittest expects. | |
324 ScopedStubCrosEnabler stub_cros_enabler_; | |
325 BrowserThread ui_thread_; | |
326 BrowserThread io_thread_; | |
327 }; | |
328 | |
329 class ProxyConfigServiceImplTest | |
330 : public ProxyConfigServiceImplTestBase<testing::Test> { | |
331 protected: | |
332 virtual void SetUp() { | |
333 pref_service_.reset(new TestingPrefService()); | |
334 Init(pref_service_.get()); | |
408 } | 335 } |
409 | 336 |
410 private: | 337 scoped_ptr<TestingPrefService> pref_service_; |
Mattias Nissler (ping if slow)
2011/10/19 19:27:36
just declare a plain TestingPrefService member var
kuan
2011/10/20 00:41:11
Done.
| |
411 bool CanBeWrittenByOwner( | |
412 ProxyConfigServiceImpl::ProxyConfig::Source source) const { | |
413 return source == MK_SRC(POLICY) ? false : true; | |
414 } | |
415 | |
416 ScopedStubCrosEnabler stub_cros_enabler_; | |
417 MessageLoop message_loop_; | |
418 BrowserThread ui_thread_; | |
419 BrowserThread io_thread_; | |
420 | |
421 scoped_refptr<ProxyConfigServiceImpl> config_service_; | |
422 }; | 338 }; |
423 | 339 |
424 TEST_F(ProxyConfigServiceImplTest, ChromeosProxyConfigToNetProxyConfig) { | 340 TEST_F(ProxyConfigServiceImplTest, SerializeAndDeserializeForNetwork) { |
341 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | |
342 if (!tests[i].is_valid) | |
343 continue; | |
344 | |
345 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i, | |
346 tests[i].description.c_str())); | |
347 | |
348 ProxyConfigServiceImpl::ProxyConfig source_config; | |
349 InitConfigWithTestInput(tests[i].input, &source_config); | |
350 | |
351 // Serialize source_config into std::string. | |
352 std::string serialized_value; | |
353 EXPECT_TRUE(source_config.SerializeForNetwork(&serialized_value)); | |
354 | |
355 // Deserialize std:string into target_config. | |
356 ProxyConfigServiceImpl::ProxyConfig target_config; | |
357 EXPECT_TRUE(target_config.DeserializeForNetwork(serialized_value)); | |
358 | |
359 // Compare the configs after serialization and deserialization. | |
360 net::ProxyConfig net_src_cfg; | |
361 net::ProxyConfig net_tgt_cfg; | |
362 source_config.ToNetProxyConfig(&net_src_cfg); | |
363 target_config.ToNetProxyConfig(&net_tgt_cfg); | |
364 #if !defined(NDEBUG) | |
365 if (!net_src_cfg.Equals(net_tgt_cfg)) { | |
366 std::string src_output, tgt_output; | |
367 JSONStringValueSerializer src_serializer(&src_output); | |
368 src_serializer.Serialize(*net_src_cfg.ToValue()); | |
369 JSONStringValueSerializer tgt_serializer(&tgt_output); | |
370 tgt_serializer.Serialize(*net_tgt_cfg.ToValue()); | |
371 VLOG(1) << "source:\n" << src_output | |
372 << "\ntarget:\n" << tgt_output; | |
373 } | |
374 #endif // !defined(NDEBUG) | |
375 EXPECT_TRUE(net_src_cfg.Equals(net_tgt_cfg)); | |
376 } | |
377 } | |
378 | |
379 TEST_F(ProxyConfigServiceImplTest, NetworkProxy) { | |
425 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 380 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
426 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i, | 381 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i, |
427 tests[i].description.c_str())); | 382 tests[i].description.c_str())); |
428 | 383 |
429 ProxyConfigServiceImpl::ProxyConfig init_config; | 384 ProxyConfigServiceImpl::ProxyConfig test_config; |
430 InitConfigWithTestInput(tests[i].input, MK_SRC(OWNER), &init_config); | 385 InitConfigWithTestInput(tests[i].input, &test_config); |
431 CreateConfigService(init_config); | 386 config_service_impl_->SetTesting(&test_config); |
432 | 387 |
433 net::ProxyConfig config; | 388 net::ProxyConfig config; |
434 SyncGetLatestProxyConfig(&config); | 389 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&config)); |
435 | 390 |
436 EXPECT_EQ(tests[i].auto_detect, config.auto_detect()); | 391 EXPECT_EQ(tests[i].auto_detect, config.auto_detect()); |
437 EXPECT_EQ(tests[i].pac_url, config.pac_url()); | 392 EXPECT_EQ(tests[i].pac_url, config.pac_url()); |
438 EXPECT_TRUE(tests[i].proxy_rules.Matches(config.proxy_rules())); | 393 EXPECT_TRUE(tests[i].proxy_rules.Matches(config.proxy_rules())); |
439 } | 394 } |
440 } | 395 } |
441 | 396 |
442 TEST_F(ProxyConfigServiceImplTest, ModifyFromUI) { | 397 TEST_F(ProxyConfigServiceImplTest, ModifyFromUI) { |
443 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 398 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
444 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i, | 399 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i, |
445 tests[i].description.c_str())); | 400 tests[i].description.c_str())); |
446 | 401 |
447 // Init with direct. | 402 // Init with direct. |
448 ProxyConfigServiceImpl::ProxyConfig init_config; | 403 ProxyConfigServiceImpl::ProxyConfig test_config; |
449 SetAutomaticProxy(MK_MODE(DIRECT), MK_SRC(OWNER), NULL, &init_config, | 404 SetAutomaticProxy(MK_MODE(DIRECT), NULL, &test_config, |
450 &init_config.automatic_proxy); | 405 &test_config.automatic_proxy); |
451 CreateConfigService(init_config); | 406 config_service_impl_->SetTesting(&test_config); |
452 | 407 |
453 // Set config to tests[i].input via UI. | 408 // Set config to tests[i].input via UI. |
454 net::ProxyBypassRules bypass_rules; | 409 net::ProxyBypassRules bypass_rules; |
455 const Input& input = tests[i].input; | 410 const Input& input = tests[i].input; |
456 switch (input.mode) { | 411 switch (input.mode) { |
457 case MK_MODE(DIRECT) : | 412 case MK_MODE(DIRECT) : |
458 config_service()->UISetProxyConfigToDirect(); | 413 config_service_impl_->UISetProxyConfigToDirect(); |
459 break; | 414 break; |
460 case MK_MODE(AUTO_DETECT) : | 415 case MK_MODE(AUTO_DETECT) : |
461 config_service()->UISetProxyConfigToAutoDetect(); | 416 config_service_impl_->UISetProxyConfigToAutoDetect(); |
462 break; | 417 break; |
463 case MK_MODE(PAC_SCRIPT) : | 418 case MK_MODE(PAC_SCRIPT) : |
464 config_service()->UISetProxyConfigToPACScript(GURL(input.pac_url)); | 419 config_service_impl_->UISetProxyConfigToPACScript(GURL(input.pac_url)); |
465 break; | 420 break; |
466 case MK_MODE(SINGLE_PROXY) : | 421 case MK_MODE(SINGLE_PROXY) : |
467 config_service()->UISetProxyConfigToSingleProxy( | 422 config_service_impl_->UISetProxyConfigToSingleProxy( |
468 net::ProxyServer::FromURI(input.single_uri, MK_SCHM(HTTP))); | 423 net::ProxyServer::FromURI(input.single_uri, MK_SCHM(HTTP))); |
469 if (input.bypass_rules) { | 424 if (input.bypass_rules) { |
470 bypass_rules.ParseFromString(input.bypass_rules); | 425 bypass_rules.ParseFromString(input.bypass_rules); |
471 config_service()->UISetProxyConfigBypassRules(bypass_rules); | 426 config_service_impl_->UISetProxyConfigBypassRules(bypass_rules); |
472 } | 427 } |
473 break; | 428 break; |
474 case MK_MODE(PROXY_PER_SCHEME) : | 429 case MK_MODE(PROXY_PER_SCHEME) : |
475 if (input.http_uri) { | 430 if (input.http_uri) { |
476 config_service()->UISetProxyConfigToProxyPerScheme("http", | 431 config_service_impl_->UISetProxyConfigToProxyPerScheme("http", |
477 net::ProxyServer::FromURI(input.http_uri, MK_SCHM(HTTP))); | 432 net::ProxyServer::FromURI(input.http_uri, MK_SCHM(HTTP))); |
478 } | 433 } |
479 if (input.https_uri) { | 434 if (input.https_uri) { |
480 config_service()->UISetProxyConfigToProxyPerScheme("https", | 435 config_service_impl_->UISetProxyConfigToProxyPerScheme("https", |
481 net::ProxyServer::FromURI(input.https_uri, MK_SCHM(HTTPS))); | 436 net::ProxyServer::FromURI(input.https_uri, MK_SCHM(HTTPS))); |
482 } | 437 } |
483 if (input.ftp_uri) { | 438 if (input.ftp_uri) { |
484 config_service()->UISetProxyConfigToProxyPerScheme("ftp", | 439 config_service_impl_->UISetProxyConfigToProxyPerScheme("ftp", |
485 net::ProxyServer::FromURI(input.ftp_uri, MK_SCHM(HTTP))); | 440 net::ProxyServer::FromURI(input.ftp_uri, MK_SCHM(HTTP))); |
486 } | 441 } |
487 if (input.socks_uri) { | 442 if (input.socks_uri) { |
488 config_service()->UISetProxyConfigToProxyPerScheme("socks", | 443 config_service_impl_->UISetProxyConfigToProxyPerScheme("socks", |
489 net::ProxyServer::FromURI(input.socks_uri, MK_SCHM(SOCKS5))); | 444 net::ProxyServer::FromURI(input.socks_uri, MK_SCHM(SOCKS5))); |
490 } | 445 } |
491 if (input.bypass_rules) { | 446 if (input.bypass_rules) { |
492 bypass_rules.ParseFromString(input.bypass_rules); | 447 bypass_rules.ParseFromString(input.bypass_rules); |
493 config_service()->UISetProxyConfigBypassRules(bypass_rules); | 448 config_service_impl_->UISetProxyConfigBypassRules(bypass_rules); |
494 } | 449 } |
495 break; | 450 break; |
496 } | 451 } |
497 | 452 |
498 // Retrieve config from IO thread. | 453 // Retrieve config from IO thread. |
499 net::ProxyConfig io_config; | 454 net::ProxyConfig io_config; |
500 SyncGetLatestProxyConfig(&io_config); | 455 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&io_config)); |
501 EXPECT_EQ(tests[i].auto_detect, io_config.auto_detect()); | 456 EXPECT_EQ(tests[i].auto_detect, io_config.auto_detect()); |
502 EXPECT_EQ(tests[i].pac_url, io_config.pac_url()); | 457 EXPECT_EQ(tests[i].pac_url, io_config.pac_url()); |
503 EXPECT_TRUE(tests[i].proxy_rules.Matches(io_config.proxy_rules())); | 458 EXPECT_TRUE(tests[i].proxy_rules.Matches(io_config.proxy_rules())); |
504 | 459 |
505 // Retrieve config from UI thread. | 460 // Retrieve config from UI thread. |
506 ProxyConfigServiceImpl::ProxyConfig ui_config; | 461 ProxyConfigServiceImpl::ProxyConfig ui_config; |
507 config_service()->UIGetProxyConfig(&ui_config); | 462 config_service_impl_->UIGetProxyConfig(&ui_config); |
508 EXPECT_EQ(input.mode, ui_config.mode); | 463 EXPECT_EQ(input.mode, ui_config.mode); |
509 if (tests[i].is_valid) { | 464 if (tests[i].is_valid) { |
510 if (input.pac_url) | 465 if (input.pac_url) |
511 EXPECT_EQ(GURL(input.pac_url), ui_config.automatic_proxy.pac_url); | 466 EXPECT_EQ(GURL(input.pac_url), ui_config.automatic_proxy.pac_url); |
512 const net::ProxyRulesExpectation& proxy_rules = tests[i].proxy_rules; | 467 const net::ProxyRulesExpectation& proxy_rules = tests[i].proxy_rules; |
513 if (input.single_uri) | 468 if (input.single_uri) |
514 EXPECT_EQ(proxy_rules.single_proxy, | 469 EXPECT_EQ(proxy_rules.single_proxy, |
515 ui_config.single_proxy.server.ToURI()); | 470 ui_config.single_proxy.server.ToURI()); |
516 if (input.http_uri) | 471 if (input.http_uri) |
517 EXPECT_EQ(proxy_rules.proxy_for_http, | 472 EXPECT_EQ(proxy_rules.proxy_for_http, |
518 ui_config.http_proxy.server.ToURI()); | 473 ui_config.http_proxy.server.ToURI()); |
519 if (input.https_uri) | 474 if (input.https_uri) |
520 EXPECT_EQ(proxy_rules.proxy_for_https, | 475 EXPECT_EQ(proxy_rules.proxy_for_https, |
521 ui_config.https_proxy.server.ToURI()); | 476 ui_config.https_proxy.server.ToURI()); |
522 if (input.ftp_uri) | 477 if (input.ftp_uri) |
523 EXPECT_EQ(proxy_rules.proxy_for_ftp, | 478 EXPECT_EQ(proxy_rules.proxy_for_ftp, |
524 ui_config.ftp_proxy.server.ToURI()); | 479 ui_config.ftp_proxy.server.ToURI()); |
525 if (input.socks_uri) { | 480 if (input.socks_uri) { |
526 EXPECT_EQ(proxy_rules.fallback_proxy, | 481 EXPECT_EQ(proxy_rules.fallback_proxy, |
527 ui_config.socks_proxy.server.ToURI()); | 482 ui_config.socks_proxy.server.ToURI()); |
528 } | 483 } |
529 if (input.bypass_rules) | 484 if (input.bypass_rules) |
530 EXPECT_TRUE(bypass_rules.Equals(ui_config.bypass_rules)); | 485 EXPECT_TRUE(bypass_rules.Equals(ui_config.bypass_rules)); |
531 } | 486 } |
532 } | 487 } |
533 } | 488 } |
534 | 489 |
535 TEST_F(ProxyConfigServiceImplTest, ProxyChangedObserver) { | 490 TEST_F(ProxyConfigServiceImplTest, DynamicPrefsOverride) { |
536 // This is used to observe for OnProxyConfigChanged notification. | 491 // Determine groups of 3 test inputs to use for managed, recommended and |
537 class ProxyChangedObserver : public net::ProxyConfigService::Observer { | 492 // network proxies. |
538 public: | 493 int test = 0; |
539 explicit ProxyChangedObserver( | 494 for (size_t i = 0, j = 0; i < ARRAYSIZE_UNSAFE(tests);) { |
540 const scoped_refptr<ProxyConfigServiceImpl>& config_service) | 495 int managed_idx = -1; |
541 : config_service_(config_service) { | 496 int recommended_idx = -1; |
542 config_service_->AddObserver(this); | 497 int network_idx = -1; |
498 for (j = i; j < ARRAYSIZE_UNSAFE(tests) && network_idx == -1; ++j) { | |
499 // Only use valid input that has proxy. | |
500 if (!tests[j].is_valid || tests[j].input.mode == MK_MODE(DIRECT)) | |
501 continue; | |
502 if (managed_idx == -1) | |
503 managed_idx = j; | |
504 else if (recommended_idx == -1) | |
505 recommended_idx = j; | |
506 else if (network_idx == -1) | |
507 network_idx = j; | |
543 } | 508 } |
544 virtual ~ProxyChangedObserver() { | 509 if (network_idx == -1) // Insufficient inputs for a set of 3 proxies. |
545 config_service_->RemoveObserver(this); | 510 break; |
546 } | |
547 net::ProxyConfigService::ConfigAvailability availability() const { | |
548 return availability_; | |
549 } | |
550 const net::ProxyConfig& config() const { | |
551 return config_; | |
552 } | |
553 | 511 |
554 private: | 512 // Next group starts after the current managed proxy. |
555 virtual void OnProxyConfigChanged( | 513 i = managed_idx + 1; |
556 const net::ProxyConfig& config, | |
557 net::ProxyConfigService::ConfigAvailability availability) { | |
558 config_ = config; | |
559 availability_ = availability; | |
560 } | |
561 | 514 |
562 scoped_refptr<ProxyConfigServiceImpl> config_service_; | 515 const TestParams& managed_params = tests[managed_idx]; |
563 net::ProxyConfigService::ConfigAvailability availability_; | 516 const TestParams& recommended_params = tests[recommended_idx]; |
564 net::ProxyConfig config_; | 517 const TestParams& network_params = tests[network_idx]; |
565 }; | |
566 | 518 |
567 // Init with direct. | 519 std::cout << "TESTING " << test |
568 ProxyConfigServiceImpl::ProxyConfig init_config; | 520 << ": managed=[" << managed_params.description.c_str() |
569 SetAutomaticProxy(MK_MODE(DIRECT), MK_SRC(OWNER), NULL, &init_config, | 521 << "], recommended=[" << recommended_params.description.c_str() |
570 &init_config.automatic_proxy); | 522 << "], network=[" << network_params.description.c_str() |
571 CreateConfigService(init_config); | 523 << "]" << std::endl; |
Mattias Nissler (ping if slow)
2011/10/19 19:27:36
debugging code? convert to proper vlog or remove?
kuan
2011/10/20 00:41:11
Done.
| |
524 SCOPED_TRACE(StringPrintf( | |
525 "Test[%" PRIuS "] managed=[%s], recommended=[%s], network=[%s]", test++, | |
526 managed_params.description.c_str(), | |
527 recommended_params.description.c_str(), | |
528 network_params.description.c_str())); | |
572 | 529 |
573 ProxyChangedObserver observer(config_service()); | 530 ProxyConfigServiceImpl::ProxyConfig managed_config; |
531 InitConfigWithTestInput(managed_params.input, &managed_config); | |
532 ProxyConfigServiceImpl::ProxyConfig recommended_config; | |
533 InitConfigWithTestInput(recommended_params.input, &recommended_config); | |
534 ProxyConfigServiceImpl::ProxyConfig network_config; | |
535 InitConfigWithTestInput(network_params.input, &network_config); | |
574 | 536 |
575 // Set to pac script from UI. | 537 // hManaged proxy pref should take effect over recommended proxy and |
576 EXPECT_TRUE(config_service()->UISetProxyConfigToPACScript( | 538 // non-existent network proxy. |
577 GURL("http://wpad.dat"))); | 539 config_service_impl_->SetTesting(NULL); |
578 // Retrieve config from IO thread. | 540 pref_service_->SetManagedPref(prefs::kProxy, |
579 net::ProxyConfig io_config; | 541 managed_config.ToPrefProxyConfig()); |
580 SyncGetLatestProxyConfig(&io_config); | 542 pref_service_->SetRecommendedPref(prefs::kProxy, |
543 recommended_config.ToPrefProxyConfig()); | |
544 net::ProxyConfig actual_config; | |
545 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config)); | |
546 EXPECT_EQ(managed_params.auto_detect, actual_config.auto_detect()); | |
547 EXPECT_EQ(managed_params.pac_url, actual_config.pac_url()); | |
548 EXPECT_TRUE(managed_params.proxy_rules.Matches( | |
549 actual_config.proxy_rules())); | |
581 | 550 |
582 // Observer should have gotten the same new proxy config. | 551 // Recommended proxy pref should take effect when managed proxy pref is |
583 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, observer.availability()); | 552 // removed. |
584 EXPECT_TRUE(io_config.Equals(observer.config())); | 553 pref_service_->RemoveManagedPref(prefs::kProxy); |
585 } | 554 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config)); |
555 EXPECT_EQ(recommended_params.auto_detect, actual_config.auto_detect()); | |
556 EXPECT_EQ(recommended_params.pac_url, actual_config.pac_url()); | |
557 EXPECT_TRUE(recommended_params.proxy_rules.Matches( | |
558 actual_config.proxy_rules())); | |
586 | 559 |
587 TEST_F(ProxyConfigServiceImplTest, SerializeAndDeserializeForNetwork) { | 560 // Network proxy should take take effect over recommended proxy pref. |
588 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 561 config_service_impl_->SetTesting(&network_config); |
589 if (!tests[i].is_valid) | 562 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config)); |
590 continue; | 563 EXPECT_EQ(network_params.auto_detect, actual_config.auto_detect()); |
564 EXPECT_EQ(network_params.pac_url, actual_config.pac_url()); | |
565 EXPECT_TRUE(network_params.proxy_rules.Matches( | |
566 actual_config.proxy_rules())); | |
591 | 567 |
592 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i, | 568 // Managed proxy pref should take effect over network proxy. |
593 tests[i].description.c_str())); | 569 pref_service_->SetManagedPref(prefs::kProxy, |
570 managed_config.ToPrefProxyConfig()); | |
571 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config)); | |
572 EXPECT_EQ(managed_params.auto_detect, actual_config.auto_detect()); | |
573 EXPECT_EQ(managed_params.pac_url, actual_config.pac_url()); | |
574 EXPECT_TRUE(managed_params.proxy_rules.Matches( | |
575 actual_config.proxy_rules())); | |
594 | 576 |
595 ProxyConfigServiceImpl::ProxyConfig source_config; | 577 // Network proxy should take effect over recommended proxy pref when managed |
596 InitConfigWithTestInput(tests[i].input, MK_SRC(OWNER), &source_config); | 578 // proxy pref is removed. |
579 pref_service_->RemoveManagedPref(prefs::kProxy); | |
580 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config)); | |
581 EXPECT_EQ(network_params.auto_detect, actual_config.auto_detect()); | |
582 EXPECT_EQ(network_params.pac_url, actual_config.pac_url()); | |
583 EXPECT_TRUE(network_params.proxy_rules.Matches( | |
584 actual_config.proxy_rules())); | |
597 | 585 |
598 // Serialize source_config into std::string. | 586 // Removing recommended proxy pref should have no effect on network proxy. |
599 std::string serialized_value; | 587 pref_service_->RemoveRecommendedPref(prefs::kProxy); |
600 EXPECT_TRUE(source_config.SerializeForNetwork(&serialized_value)); | 588 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config)); |
601 | 589 EXPECT_EQ(network_params.auto_detect, actual_config.auto_detect()); |
602 // Deserialize std:string into target_config. | 590 EXPECT_EQ(network_params.pac_url, actual_config.pac_url()); |
603 ProxyConfigServiceImpl::ProxyConfig target_config; | 591 EXPECT_TRUE(network_params.proxy_rules.Matches( |
604 EXPECT_TRUE(target_config.DeserializeForNetwork(serialized_value)); | 592 actual_config.proxy_rules())); |
605 | |
606 // Compare the configs after serialization and deserialization. | |
607 net::ProxyConfig net_src_cfg; | |
608 net::ProxyConfig net_tgt_cfg; | |
609 source_config.ToNetProxyConfig(&net_src_cfg); | |
610 target_config.ToNetProxyConfig(&net_tgt_cfg); | |
611 #if !defined(NDEBUG) | |
612 if (!net_src_cfg.Equals(net_tgt_cfg)) { | |
613 std::string src_output, tgt_output; | |
614 JSONStringValueSerializer src_serializer(&src_output); | |
615 src_serializer.Serialize(*net_src_cfg.ToValue()); | |
616 JSONStringValueSerializer tgt_serializer(&tgt_output); | |
617 tgt_serializer.Serialize(*net_tgt_cfg.ToValue()); | |
618 VLOG(1) << "source:\n" << src_output | |
619 << "\ntarget:\n" << tgt_output; | |
620 } | |
621 #endif // !defined(NDEBUG) | |
622 EXPECT_TRUE(net_src_cfg.Equals(net_tgt_cfg)); | |
623 } | 593 } |
624 } | 594 } |
625 | 595 |
626 TEST_F(ProxyConfigServiceImplTest, ReadWriteAccessForPolicySource) { | 596 } // namespace |
627 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | |
628 if (!tests[i].test_read_write_access) | |
629 continue; | |
630 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i, | |
631 tests[i].description.c_str())); | |
632 TestReadWriteAccessForMode(tests[i].input, MK_SRC(POLICY)); | |
633 } | |
634 } | |
635 | |
636 TEST_F(ProxyConfigServiceImplTest, ReadWriteAccessForOwnerSource) { | |
637 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | |
638 if (!tests[i].test_read_write_access) | |
639 continue; | |
640 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i, | |
641 tests[i].description.c_str())); | |
642 TestReadWriteAccessForMode(tests[i].input, MK_SRC(OWNER)); | |
643 } | |
644 } | |
645 | |
646 TEST_F(ProxyConfigServiceImplTest, ReadWriteAccessForMixedSchemes) { | |
647 const char* http_uri = "www.google.com:80"; | |
648 const char* https_uri = "www.foo.com:110"; | |
649 const char* ftp_uri = "ftp.foo.com:121"; | |
650 const char* socks_uri = "socks.com:888"; | |
651 | |
652 // Init with policy source. | |
653 TestReadWriteAccessForScheme(MK_SRC(POLICY), http_uri, "http"); | |
654 TestReadWriteAccessForScheme(MK_SRC(POLICY), https_uri, "https"); | |
655 TestReadWriteAccessForScheme(MK_SRC(POLICY), ftp_uri, "ftp"); | |
656 TestReadWriteAccessForScheme(MK_SRC(POLICY), socks_uri, "socks"); | |
657 | |
658 // Init with owner source. | |
659 TestReadWriteAccessForScheme(MK_SRC(OWNER), http_uri, "http"); | |
660 TestReadWriteAccessForScheme(MK_SRC(OWNER), https_uri, "https"); | |
661 TestReadWriteAccessForScheme(MK_SRC(OWNER), ftp_uri, "ftp"); | |
662 TestReadWriteAccessForScheme(MK_SRC(OWNER), socks_uri, "socks"); | |
663 } | |
664 | 597 |
665 } // namespace chromeos | 598 } // namespace chromeos |
OLD | NEW |