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 { |
Mattias Nissler (ping if slow)
2011/10/25 12:43:11
No need for static, we're in a anonymous namespace
kuan
2011/10/25 16:01:50
Done.
| |
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 Init(&pref_service_); | |
408 } | 334 } |
409 | 335 |
410 private: | 336 TestingPrefService pref_service_; |
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 }; | 337 }; |
423 | 338 |
424 TEST_F(ProxyConfigServiceImplTest, ChromeosProxyConfigToNetProxyConfig) { | 339 TEST_F(ProxyConfigServiceImplTest, SerializeAndDeserializeForNetwork) { |
340 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | |
341 if (!tests[i].is_valid) | |
342 continue; | |
343 | |
344 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i, | |
345 tests[i].description.c_str())); | |
346 | |
347 ProxyConfigServiceImpl::ProxyConfig source_config; | |
348 InitConfigWithTestInput(tests[i].input, &source_config); | |
349 | |
350 // Serialize source_config into std::string. | |
351 std::string serialized_value; | |
352 EXPECT_TRUE(source_config.SerializeForNetwork(&serialized_value)); | |
353 | |
354 // Deserialize std:string into target_config. | |
355 ProxyConfigServiceImpl::ProxyConfig target_config; | |
356 EXPECT_TRUE(target_config.DeserializeForNetwork(serialized_value)); | |
357 | |
358 // Compare the configs after serialization and deserialization. | |
359 net::ProxyConfig net_src_cfg; | |
360 net::ProxyConfig net_tgt_cfg; | |
361 source_config.ToNetProxyConfig(&net_src_cfg); | |
362 target_config.ToNetProxyConfig(&net_tgt_cfg); | |
363 #if !defined(NDEBUG) | |
364 if (!net_src_cfg.Equals(net_tgt_cfg)) { | |
365 std::string src_output, tgt_output; | |
366 JSONStringValueSerializer src_serializer(&src_output); | |
367 src_serializer.Serialize(*net_src_cfg.ToValue()); | |
368 JSONStringValueSerializer tgt_serializer(&tgt_output); | |
369 tgt_serializer.Serialize(*net_tgt_cfg.ToValue()); | |
370 VLOG(1) << "source:\n" << src_output | |
371 << "\ntarget:\n" << tgt_output; | |
Mattias Nissler (ping if slow)
2011/10/24 10:18:28
It seems like this should really be the message th
kuan
2011/10/25 02:01:54
test is no longer valid, so it's removed.
| |
372 } | |
373 #endif // !defined(NDEBUG) | |
374 EXPECT_TRUE(net_src_cfg.Equals(net_tgt_cfg)); | |
375 } | |
376 } | |
377 | |
378 TEST_F(ProxyConfigServiceImplTest, NetworkProxy) { | |
425 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 379 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
426 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i, | 380 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i, |
427 tests[i].description.c_str())); | 381 tests[i].description.c_str())); |
428 | 382 |
429 ProxyConfigServiceImpl::ProxyConfig init_config; | 383 ProxyConfigServiceImpl::ProxyConfig test_config; |
430 InitConfigWithTestInput(tests[i].input, MK_SRC(OWNER), &init_config); | 384 InitConfigWithTestInput(tests[i].input, &test_config); |
431 CreateConfigService(init_config); | 385 config_service_impl_->SetTesting(&test_config); |
432 | 386 |
433 net::ProxyConfig config; | 387 net::ProxyConfig config; |
434 SyncGetLatestProxyConfig(&config); | 388 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&config)); |
435 | 389 |
436 EXPECT_EQ(tests[i].auto_detect, config.auto_detect()); | 390 EXPECT_EQ(tests[i].auto_detect, config.auto_detect()); |
437 EXPECT_EQ(tests[i].pac_url, config.pac_url()); | 391 EXPECT_EQ(tests[i].pac_url, config.pac_url()); |
438 EXPECT_TRUE(tests[i].proxy_rules.Matches(config.proxy_rules())); | 392 EXPECT_TRUE(tests[i].proxy_rules.Matches(config.proxy_rules())); |
439 } | 393 } |
440 } | 394 } |
441 | 395 |
442 TEST_F(ProxyConfigServiceImplTest, ModifyFromUI) { | 396 TEST_F(ProxyConfigServiceImplTest, ModifyFromUI) { |
443 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 397 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
444 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i, | 398 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i, |
445 tests[i].description.c_str())); | 399 tests[i].description.c_str())); |
446 | 400 |
447 // Init with direct. | 401 // Init with direct. |
448 ProxyConfigServiceImpl::ProxyConfig init_config; | 402 ProxyConfigServiceImpl::ProxyConfig test_config; |
449 SetAutomaticProxy(MK_MODE(DIRECT), MK_SRC(OWNER), NULL, &init_config, | 403 SetAutomaticProxy(MK_MODE(DIRECT), NULL, &test_config, |
450 &init_config.automatic_proxy); | 404 &test_config.automatic_proxy); |
451 CreateConfigService(init_config); | 405 config_service_impl_->SetTesting(&test_config); |
452 | 406 |
453 // Set config to tests[i].input via UI. | 407 // Set config to tests[i].input via UI. |
454 net::ProxyBypassRules bypass_rules; | 408 net::ProxyBypassRules bypass_rules; |
455 const Input& input = tests[i].input; | 409 const Input& input = tests[i].input; |
456 switch (input.mode) { | 410 switch (input.mode) { |
457 case MK_MODE(DIRECT) : | 411 case MK_MODE(DIRECT) : |
458 config_service()->UISetProxyConfigToDirect(); | 412 config_service_impl_->UISetProxyConfigToDirect(); |
459 break; | 413 break; |
460 case MK_MODE(AUTO_DETECT) : | 414 case MK_MODE(AUTO_DETECT) : |
461 config_service()->UISetProxyConfigToAutoDetect(); | 415 config_service_impl_->UISetProxyConfigToAutoDetect(); |
462 break; | 416 break; |
463 case MK_MODE(PAC_SCRIPT) : | 417 case MK_MODE(PAC_SCRIPT) : |
464 config_service()->UISetProxyConfigToPACScript(GURL(input.pac_url)); | 418 config_service_impl_->UISetProxyConfigToPACScript(GURL(input.pac_url)); |
465 break; | 419 break; |
466 case MK_MODE(SINGLE_PROXY) : | 420 case MK_MODE(SINGLE_PROXY) : |
467 config_service()->UISetProxyConfigToSingleProxy( | 421 config_service_impl_->UISetProxyConfigToSingleProxy( |
468 net::ProxyServer::FromURI(input.single_uri, MK_SCHM(HTTP))); | 422 net::ProxyServer::FromURI(input.single_uri, MK_SCHM(HTTP))); |
469 if (input.bypass_rules) { | 423 if (input.bypass_rules) { |
470 bypass_rules.ParseFromString(input.bypass_rules); | 424 bypass_rules.ParseFromString(input.bypass_rules); |
471 config_service()->UISetProxyConfigBypassRules(bypass_rules); | 425 config_service_impl_->UISetProxyConfigBypassRules(bypass_rules); |
472 } | 426 } |
473 break; | 427 break; |
474 case MK_MODE(PROXY_PER_SCHEME) : | 428 case MK_MODE(PROXY_PER_SCHEME) : |
475 if (input.http_uri) { | 429 if (input.http_uri) { |
476 config_service()->UISetProxyConfigToProxyPerScheme("http", | 430 config_service_impl_->UISetProxyConfigToProxyPerScheme("http", |
477 net::ProxyServer::FromURI(input.http_uri, MK_SCHM(HTTP))); | 431 net::ProxyServer::FromURI(input.http_uri, MK_SCHM(HTTP))); |
478 } | 432 } |
479 if (input.https_uri) { | 433 if (input.https_uri) { |
480 config_service()->UISetProxyConfigToProxyPerScheme("https", | 434 config_service_impl_->UISetProxyConfigToProxyPerScheme("https", |
481 net::ProxyServer::FromURI(input.https_uri, MK_SCHM(HTTPS))); | 435 net::ProxyServer::FromURI(input.https_uri, MK_SCHM(HTTPS))); |
482 } | 436 } |
483 if (input.ftp_uri) { | 437 if (input.ftp_uri) { |
484 config_service()->UISetProxyConfigToProxyPerScheme("ftp", | 438 config_service_impl_->UISetProxyConfigToProxyPerScheme("ftp", |
485 net::ProxyServer::FromURI(input.ftp_uri, MK_SCHM(HTTP))); | 439 net::ProxyServer::FromURI(input.ftp_uri, MK_SCHM(HTTP))); |
486 } | 440 } |
487 if (input.socks_uri) { | 441 if (input.socks_uri) { |
488 config_service()->UISetProxyConfigToProxyPerScheme("socks", | 442 config_service_impl_->UISetProxyConfigToProxyPerScheme("socks", |
489 net::ProxyServer::FromURI(input.socks_uri, MK_SCHM(SOCKS5))); | 443 net::ProxyServer::FromURI(input.socks_uri, MK_SCHM(SOCKS5))); |
490 } | 444 } |
491 if (input.bypass_rules) { | 445 if (input.bypass_rules) { |
492 bypass_rules.ParseFromString(input.bypass_rules); | 446 bypass_rules.ParseFromString(input.bypass_rules); |
493 config_service()->UISetProxyConfigBypassRules(bypass_rules); | 447 config_service_impl_->UISetProxyConfigBypassRules(bypass_rules); |
494 } | 448 } |
495 break; | 449 break; |
496 } | 450 } |
497 | 451 |
498 // Retrieve config from IO thread. | 452 // Retrieve config from IO thread. |
499 net::ProxyConfig io_config; | 453 net::ProxyConfig io_config; |
500 SyncGetLatestProxyConfig(&io_config); | 454 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&io_config)); |
501 EXPECT_EQ(tests[i].auto_detect, io_config.auto_detect()); | 455 EXPECT_EQ(tests[i].auto_detect, io_config.auto_detect()); |
502 EXPECT_EQ(tests[i].pac_url, io_config.pac_url()); | 456 EXPECT_EQ(tests[i].pac_url, io_config.pac_url()); |
503 EXPECT_TRUE(tests[i].proxy_rules.Matches(io_config.proxy_rules())); | 457 EXPECT_TRUE(tests[i].proxy_rules.Matches(io_config.proxy_rules())); |
504 | 458 |
505 // Retrieve config from UI thread. | 459 // Retrieve config from UI thread. |
506 ProxyConfigServiceImpl::ProxyConfig ui_config; | 460 ProxyConfigServiceImpl::ProxyConfig ui_config; |
507 config_service()->UIGetProxyConfig(&ui_config); | 461 config_service_impl_->UIGetProxyConfig(&ui_config); |
508 EXPECT_EQ(input.mode, ui_config.mode); | 462 EXPECT_EQ(input.mode, ui_config.mode); |
509 if (tests[i].is_valid) { | 463 if (tests[i].is_valid) { |
510 if (input.pac_url) | 464 if (input.pac_url) |
511 EXPECT_EQ(GURL(input.pac_url), ui_config.automatic_proxy.pac_url); | 465 EXPECT_EQ(GURL(input.pac_url), ui_config.automatic_proxy.pac_url); |
512 const net::ProxyRulesExpectation& proxy_rules = tests[i].proxy_rules; | 466 const net::ProxyRulesExpectation& proxy_rules = tests[i].proxy_rules; |
513 if (input.single_uri) | 467 if (input.single_uri) |
514 EXPECT_EQ(proxy_rules.single_proxy, | 468 EXPECT_EQ(proxy_rules.single_proxy, |
515 ui_config.single_proxy.server.ToURI()); | 469 ui_config.single_proxy.server.ToURI()); |
516 if (input.http_uri) | 470 if (input.http_uri) |
517 EXPECT_EQ(proxy_rules.proxy_for_http, | 471 EXPECT_EQ(proxy_rules.proxy_for_http, |
518 ui_config.http_proxy.server.ToURI()); | 472 ui_config.http_proxy.server.ToURI()); |
519 if (input.https_uri) | 473 if (input.https_uri) |
520 EXPECT_EQ(proxy_rules.proxy_for_https, | 474 EXPECT_EQ(proxy_rules.proxy_for_https, |
521 ui_config.https_proxy.server.ToURI()); | 475 ui_config.https_proxy.server.ToURI()); |
522 if (input.ftp_uri) | 476 if (input.ftp_uri) |
523 EXPECT_EQ(proxy_rules.proxy_for_ftp, | 477 EXPECT_EQ(proxy_rules.proxy_for_ftp, |
524 ui_config.ftp_proxy.server.ToURI()); | 478 ui_config.ftp_proxy.server.ToURI()); |
525 if (input.socks_uri) { | 479 if (input.socks_uri) { |
526 EXPECT_EQ(proxy_rules.fallback_proxy, | 480 EXPECT_EQ(proxy_rules.fallback_proxy, |
527 ui_config.socks_proxy.server.ToURI()); | 481 ui_config.socks_proxy.server.ToURI()); |
528 } | 482 } |
529 if (input.bypass_rules) | 483 if (input.bypass_rules) |
530 EXPECT_TRUE(bypass_rules.Equals(ui_config.bypass_rules)); | 484 EXPECT_TRUE(bypass_rules.Equals(ui_config.bypass_rules)); |
531 } | 485 } |
532 } | 486 } |
533 } | 487 } |
534 | 488 |
535 TEST_F(ProxyConfigServiceImplTest, ProxyChangedObserver) { | 489 TEST_F(ProxyConfigServiceImplTest, DynamicPrefsOverride) { |
536 // This is used to observe for OnProxyConfigChanged notification. | 490 // Determine groups of 3 test inputs to use for managed, recommended and |
537 class ProxyChangedObserver : public net::ProxyConfigService::Observer { | 491 // network proxies. |
538 public: | 492 int test = 0; |
539 explicit ProxyChangedObserver( | 493 for (size_t i = 0, j = 0; i < ARRAYSIZE_UNSAFE(tests);) { |
540 const scoped_refptr<ProxyConfigServiceImpl>& config_service) | 494 int managed_idx = -1; |
541 : config_service_(config_service) { | 495 int recommended_idx = -1; |
542 config_service_->AddObserver(this); | 496 int network_idx = -1; |
497 for (j = i; j < ARRAYSIZE_UNSAFE(tests) && network_idx == -1; ++j) { | |
498 // Only use valid input that has proxy. | |
499 if (!tests[j].is_valid || tests[j].input.mode == MK_MODE(DIRECT)) | |
500 continue; | |
501 if (managed_idx == -1) | |
502 managed_idx = j; | |
503 else if (recommended_idx == -1) | |
504 recommended_idx = j; | |
505 else if (network_idx == -1) | |
506 network_idx = j; | |
543 } | 507 } |
544 virtual ~ProxyChangedObserver() { | 508 if (network_idx == -1) // Insufficient inputs for a set of 3 proxies. |
545 config_service_->RemoveObserver(this); | 509 break; |
546 } | |
547 net::ProxyConfigService::ConfigAvailability availability() const { | |
548 return availability_; | |
549 } | |
550 const net::ProxyConfig& config() const { | |
551 return config_; | |
552 } | |
553 | 510 |
554 private: | 511 // Next group starts after the current managed proxy. |
555 virtual void OnProxyConfigChanged( | 512 i = managed_idx + 1; |
Mattias Nissler (ping if slow)
2011/10/24 10:18:28
This whole index-searching seems pretty brittle to
kuan
2011/10/25 02:01:54
i intentionally don't want to hardcode the triple-
Mattias Nissler (ping if slow)
2011/10/25 12:43:11
I can see that it might be slightly more work for
kuan
2011/10/25 16:01:50
Done.
| |
556 const net::ProxyConfig& config, | |
557 net::ProxyConfigService::ConfigAvailability availability) { | |
558 config_ = config; | |
559 availability_ = availability; | |
560 } | |
561 | 513 |
562 scoped_refptr<ProxyConfigServiceImpl> config_service_; | 514 const TestParams& managed_params = tests[managed_idx]; |
563 net::ProxyConfigService::ConfigAvailability availability_; | 515 const TestParams& recommended_params = tests[recommended_idx]; |
564 net::ProxyConfig config_; | 516 const TestParams& network_params = tests[network_idx]; |
565 }; | |
566 | 517 |
567 // Init with direct. | 518 SCOPED_TRACE(StringPrintf( |
568 ProxyConfigServiceImpl::ProxyConfig init_config; | 519 "Test[%" PRIuS "] managed=[%s], recommended=[%s], network=[%s]", test++, |
569 SetAutomaticProxy(MK_MODE(DIRECT), MK_SRC(OWNER), NULL, &init_config, | 520 managed_params.description.c_str(), |
570 &init_config.automatic_proxy); | 521 recommended_params.description.c_str(), |
571 CreateConfigService(init_config); | 522 network_params.description.c_str())); |
572 | 523 |
573 ProxyChangedObserver observer(config_service()); | 524 ProxyConfigServiceImpl::ProxyConfig managed_config; |
525 InitConfigWithTestInput(managed_params.input, &managed_config); | |
526 ProxyConfigServiceImpl::ProxyConfig recommended_config; | |
527 InitConfigWithTestInput(recommended_params.input, &recommended_config); | |
528 ProxyConfigServiceImpl::ProxyConfig network_config; | |
529 InitConfigWithTestInput(network_params.input, &network_config); | |
574 | 530 |
575 // Set to pac script from UI. | 531 // hManaged proxy pref should take effect over recommended proxy and |
Mattias Nissler (ping if slow)
2011/10/24 10:18:28
s/hManaged/Managed/
kuan
2011/10/25 02:01:54
Done.
| |
576 EXPECT_TRUE(config_service()->UISetProxyConfigToPACScript( | 532 // non-existent network proxy. |
577 GURL("http://wpad.dat"))); | 533 config_service_impl_->SetTesting(NULL); |
578 // Retrieve config from IO thread. | 534 pref_service_.SetManagedPref(prefs::kProxy, |
579 net::ProxyConfig io_config; | 535 managed_config.ToPrefProxyConfig()); |
580 SyncGetLatestProxyConfig(&io_config); | 536 pref_service_.SetRecommendedPref(prefs::kProxy, |
537 recommended_config.ToPrefProxyConfig()); | |
538 net::ProxyConfig actual_config; | |
539 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config)); | |
540 EXPECT_EQ(managed_params.auto_detect, actual_config.auto_detect()); | |
541 EXPECT_EQ(managed_params.pac_url, actual_config.pac_url()); | |
542 EXPECT_TRUE(managed_params.proxy_rules.Matches( | |
543 actual_config.proxy_rules())); | |
581 | 544 |
582 // Observer should have gotten the same new proxy config. | 545 // Recommended proxy pref should take effect when managed proxy pref is |
583 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, observer.availability()); | 546 // removed. |
584 EXPECT_TRUE(io_config.Equals(observer.config())); | 547 pref_service_.RemoveManagedPref(prefs::kProxy); |
585 } | 548 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config)); |
549 EXPECT_EQ(recommended_params.auto_detect, actual_config.auto_detect()); | |
550 EXPECT_EQ(recommended_params.pac_url, actual_config.pac_url()); | |
551 EXPECT_TRUE(recommended_params.proxy_rules.Matches( | |
552 actual_config.proxy_rules())); | |
586 | 553 |
587 TEST_F(ProxyConfigServiceImplTest, SerializeAndDeserializeForNetwork) { | 554 // Network proxy should take take effect over recommended proxy pref. |
588 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 555 config_service_impl_->SetTesting(&network_config); |
589 if (!tests[i].is_valid) | 556 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config)); |
590 continue; | 557 EXPECT_EQ(network_params.auto_detect, actual_config.auto_detect()); |
558 EXPECT_EQ(network_params.pac_url, actual_config.pac_url()); | |
559 EXPECT_TRUE(network_params.proxy_rules.Matches( | |
560 actual_config.proxy_rules())); | |
591 | 561 |
592 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "] %s", i, | 562 // Managed proxy pref should take effect over network proxy. |
593 tests[i].description.c_str())); | 563 pref_service_.SetManagedPref(prefs::kProxy, |
564 managed_config.ToPrefProxyConfig()); | |
565 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config)); | |
566 EXPECT_EQ(managed_params.auto_detect, actual_config.auto_detect()); | |
567 EXPECT_EQ(managed_params.pac_url, actual_config.pac_url()); | |
568 EXPECT_TRUE(managed_params.proxy_rules.Matches( | |
569 actual_config.proxy_rules())); | |
594 | 570 |
595 ProxyConfigServiceImpl::ProxyConfig source_config; | 571 // Network proxy should take effect over recommended proxy pref when managed |
596 InitConfigWithTestInput(tests[i].input, MK_SRC(OWNER), &source_config); | 572 // proxy pref is removed. |
573 pref_service_.RemoveManagedPref(prefs::kProxy); | |
574 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config)); | |
575 EXPECT_EQ(network_params.auto_detect, actual_config.auto_detect()); | |
576 EXPECT_EQ(network_params.pac_url, actual_config.pac_url()); | |
577 EXPECT_TRUE(network_params.proxy_rules.Matches( | |
578 actual_config.proxy_rules())); | |
597 | 579 |
598 // Serialize source_config into std::string. | 580 // Removing recommended proxy pref should have no effect on network proxy. |
599 std::string serialized_value; | 581 pref_service_.RemoveRecommendedPref(prefs::kProxy); |
600 EXPECT_TRUE(source_config.SerializeForNetwork(&serialized_value)); | 582 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config)); |
601 | 583 EXPECT_EQ(network_params.auto_detect, actual_config.auto_detect()); |
602 // Deserialize std:string into target_config. | 584 EXPECT_EQ(network_params.pac_url, actual_config.pac_url()); |
603 ProxyConfigServiceImpl::ProxyConfig target_config; | 585 EXPECT_TRUE(network_params.proxy_rules.Matches( |
604 EXPECT_TRUE(target_config.DeserializeForNetwork(serialized_value)); | 586 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 } | 587 } |
624 } | 588 } |
625 | 589 |
626 TEST_F(ProxyConfigServiceImplTest, ReadWriteAccessForPolicySource) { | 590 } // 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 | 591 |
665 } // namespace chromeos | 592 } // namespace chromeos |
OLD | NEW |