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