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

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

Issue 14846004: Migrate ProxyConfigServiceImpl to NetworkStateHandler and NetworkProfileHandler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Renamed ConfigStateToString. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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>
8 #include <string>
9 #include <vector> 7 #include <vector>
10 8
11 #include "base/format_macros.h" 9 #include "base/format_macros.h"
10 #include "base/json/json_writer.h"
12 #include "base/logging.h" 11 #include "base/logging.h"
13 #include "base/message_loop.h" 12 #include "base/message_loop.h"
14 #include "base/prefs/testing_pref_service.h" 13 #include "base/prefs/testing_pref_service.h"
15 #include "base/string_util.h"
16 #include "base/stringprintf.h" 14 #include "base/stringprintf.h"
17 #include "chrome/browser/chromeos/cros/cros_library.h"
18 #include "chrome/browser/chromeos/settings/cros_settings.h" 15 #include "chrome/browser/chromeos/settings/cros_settings.h"
19 #include "chrome/browser/chromeos/settings/device_settings_service.h" 16 #include "chrome/browser/chromeos/settings/device_settings_service.h"
20 #include "chrome/common/pref_names.h" 17 #include "chrome/common/pref_names.h"
21 #include "chromeos/dbus/dbus_thread_manager.h" 18 #include "chromeos/dbus/dbus_thread_manager.h"
19 #include "chromeos/dbus/shill_profile_client.h"
20 #include "chromeos/dbus/shill_service_client.h"
21 #include "chromeos/network/network_profile_handler.h"
22 #include "chromeos/network/network_state.h"
23 #include "chromeos/network/network_state_handler.h"
22 #include "content/public/test/test_browser_thread.h" 24 #include "content/public/test/test_browser_thread.h"
23 #include "net/proxy/proxy_config_service_common_unittest.h" 25 #include "net/proxy/proxy_config_service_common_unittest.h"
24 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "third_party/cros_system_api/dbus/service_constants.h"
25 28
26 using content::BrowserThread; 29 using content::BrowserThread;
27 30
28 namespace chromeos { 31 namespace chromeos {
29 32
30 namespace { 33 namespace {
31 34
32 struct Input { // Fields of chromeos::ProxyConfigServiceImpl::ProxyConfig. 35 struct Input {
33 ProxyConfigServiceImpl::ProxyConfig::Mode mode; 36 UIProxyConfig::Mode mode;
34 const char* pac_url; 37 std::string pac_url;
35 const char* single_uri; 38 std::string server;
36 const char* http_uri; 39 std::string bypass_rules;
37 const char* https_uri;
38 const char* ftp_uri;
39 const char* socks_uri;
40 const char* bypass_rules;
41 }; 40 };
42 41
43 // Builds an identifier for each test in an array. 42 // Builds an identifier for each test in an array.
44 #define TEST_DESC(desc) base::StringPrintf("at line %d <%s>", __LINE__, desc) 43 #define TEST_DESC(desc) base::StringPrintf("at line %d <%s>", __LINE__, desc)
45 44
46 // Shortcuts to declare enums within chromeos's ProxyConfig. 45 // Shortcuts to declare enums within chromeos's ProxyConfig.
47 #define MK_MODE(mode) ProxyConfigServiceImpl::ProxyConfig::MODE_##mode 46 #define MK_MODE(mode) UIProxyConfig::MODE_##mode
48 #define MK_SCHM(scheme) net::ProxyServer::SCHEME_##scheme
49 #define MK_AVAIL(avail) net::ProxyConfigService::CONFIG_##avail
50 47
51 // Inspired from net/proxy/proxy_config_service_linux_unittest.cc. 48 // Inspired from net/proxy/proxy_config_service_linux_unittest.cc.
52 const struct TestParams { 49 const struct TestParams {
53 // Short description to identify the test 50 // Short description to identify the test
54 std::string description; 51 std::string description;
55 52
56 bool is_valid;
57
58 Input input; 53 Input input;
59 54
60 // Expected outputs from fields of net::ProxyConfig (via IO). 55 // Expected outputs from fields of net::ProxyConfig (via IO).
61 bool auto_detect; 56 bool auto_detect;
62 GURL pac_url; 57 GURL pac_url;
63 net::ProxyRulesExpectation proxy_rules; 58 net::ProxyRulesExpectation proxy_rules;
64 } tests[] = { 59 } tests[] = {
65 { // 0 60 { // 0
66 TEST_DESC("No proxying"), 61 TEST_DESC("No proxying"),
67 62
68 true, // is_valid
69
70 { // Input. 63 { // Input.
71 MK_MODE(DIRECT), // mode 64 MK_MODE(DIRECT), // mode
72 }, 65 },
73 66
74 // Expected result. 67 // Expected result.
75 false, // auto_detect 68 false, // auto_detect
76 GURL(), // pac_url 69 GURL(), // pac_url
77 net::ProxyRulesExpectation::Empty(), // proxy_rules 70 net::ProxyRulesExpectation::Empty(), // proxy_rules
78 }, 71 },
79 72
80 { // 1 73 { // 1
81 TEST_DESC("Auto detect"), 74 TEST_DESC("Auto detect"),
82 75
83 true, // is_valid
84
85 { // Input. 76 { // Input.
86 MK_MODE(AUTO_DETECT), // mode 77 MK_MODE(AUTO_DETECT), // mode
87 }, 78 },
88 79
89 // Expected result. 80 // Expected result.
90 true, // auto_detect 81 true, // auto_detect
91 GURL(), // pac_url 82 GURL(), // pac_url
92 net::ProxyRulesExpectation::Empty(), // proxy_rules 83 net::ProxyRulesExpectation::Empty(), // proxy_rules
93 }, 84 },
94 85
95 { // 2 86 { // 2
96 TEST_DESC("Valid PAC URL"), 87 TEST_DESC("Valid PAC URL"),
97 88
98 true, // is_valid
99
100 { // Input. 89 { // Input.
101 MK_MODE(PAC_SCRIPT), // mode 90 MK_MODE(PAC_SCRIPT), // mode
102 "http://wpad/wpad.dat", // pac_url 91 "http://wpad/wpad.dat", // pac_url
103 }, 92 },
104 93
105 // Expected result. 94 // Expected result.
106 false, // auto_detect 95 false, // auto_detect
107 GURL("http://wpad/wpad.dat"), // pac_url 96 GURL("http://wpad/wpad.dat"), // pac_url
108 net::ProxyRulesExpectation::Empty(), // proxy_rules 97 net::ProxyRulesExpectation::Empty(), // proxy_rules
109 }, 98 },
110 99
111 { // 3 100 { // 3
112 TEST_DESC("Invalid PAC URL"), 101 TEST_DESC("Invalid PAC URL"),
113 102
114 false, // is_valid
115
116 { // Input. 103 { // Input.
117 MK_MODE(PAC_SCRIPT), // mode 104 MK_MODE(PAC_SCRIPT), // mode
118 "wpad.dat", // pac_url 105 "wpad.dat", // pac_url
119 }, 106 },
120 107
121 // Expected result. 108 // Expected result.
122 false, // auto_detect 109 false, // auto_detect
123 GURL(), // pac_url 110 GURL(), // pac_url
124 net::ProxyRulesExpectation::Empty(), // proxy_rules 111 net::ProxyRulesExpectation::Empty(), // proxy_rules
125 }, 112 },
126 113
127 { // 4 114 { // 4
128 TEST_DESC("Single-host in proxy list"), 115 TEST_DESC("Single-host in proxy list"),
129 116
130 true, // is_valid
131
132 { // Input. 117 { // Input.
133 MK_MODE(SINGLE_PROXY), // mode 118 MK_MODE(SINGLE_PROXY), // mode
134 NULL, // pac_url 119 "", // pac_url
135 "www.google.com", // single_uri 120 "www.google.com", // server
136 }, 121 },
137 122
138 // Expected result. 123 // Expected result.
139 false, // auto_detect 124 false, // auto_detect
140 GURL(), // pac_url 125 GURL(), // pac_url
141 net::ProxyRulesExpectation::Single( // proxy_rules 126 net::ProxyRulesExpectation::Single( // proxy_rules
142 "www.google.com:80", // single proxy 127 "www.google.com:80", // single proxy
143 "<local>"), // bypass rules 128 "<local>"), // bypass rules
144 }, 129 },
145 130
146 { // 5 131 { // 5
147 TEST_DESC("Single-host, different port"), 132 TEST_DESC("Single-host, different port"),
148 133
149 true, // is_valid
150
151 { // Input. 134 { // Input.
152 MK_MODE(SINGLE_PROXY), // mode 135 MK_MODE(SINGLE_PROXY), // mode
153 NULL, // pac_url 136 "", // pac_url
154 "www.google.com:99", // single_uri 137 "www.google.com:99", // server
155 }, 138 },
156 139
157 // Expected result. 140 // Expected result.
158 false, // auto_detect 141 false, // auto_detect
159 GURL(), // pac_url 142 GURL(), // pac_url
160 net::ProxyRulesExpectation::Single( // proxy_rules 143 net::ProxyRulesExpectation::Single( // proxy_rules
161 "www.google.com:99", // single 144 "www.google.com:99", // single
162 "<local>"), // bypass rules 145 "<local>"), // bypass rules
163 }, 146 },
164 147
165 { // 6 148 { // 6
166 TEST_DESC("Tolerate a scheme"), 149 TEST_DESC("Tolerate a scheme"),
167 150
168 true, // is_valid
169
170 { // Input. 151 { // Input.
171 MK_MODE(SINGLE_PROXY), // mode 152 MK_MODE(SINGLE_PROXY), // mode
172 NULL, // pac_url 153 "", // pac_url
173 "http://www.google.com:99", // single_uri 154 "http://www.google.com:99", // server
174 }, 155 },
175 156
176 // Expected result. 157 // Expected result.
177 false, // auto_detect 158 false, // auto_detect
178 GURL(), // pac_url 159 GURL(), // pac_url
179 net::ProxyRulesExpectation::Single( // proxy_rules 160 net::ProxyRulesExpectation::Single( // proxy_rules
180 "www.google.com:99", // single proxy 161 "www.google.com:99", // single proxy
181 "<local>"), // bypass rules 162 "<local>"), // bypass rules
182 }, 163 },
183 164
184 { // 7 165 { // 7
185 TEST_DESC("Per-scheme proxy rules"), 166 TEST_DESC("Per-scheme proxy rules"),
186 167
187 true, // is_valid
188
189 { // Input. 168 { // Input.
190 MK_MODE(PROXY_PER_SCHEME), // mode 169 MK_MODE(PROXY_PER_SCHEME), // mode
191 NULL, // pac_url 170 "", // pac_url
192 NULL, // single_uri 171 "http=www.google.com:80;https=https://www.foo.com:110;"
193 "www.google.com:80", // http_uri 172 "ftp=ftp.foo.com:121;socks=socks5://socks.com:888", // server
194 "www.foo.com:110", // https_uri
195 "ftp.foo.com:121", // ftp_uri
196 "socks.com:888", // socks_uri
197 }, 173 },
198 174
199 // Expected result. 175 // Expected result.
200 false, // auto_detect 176 false, // auto_detect
201 GURL(), // pac_url 177 GURL(), // pac_url
202 net::ProxyRulesExpectation::PerSchemeWithSocks( // proxy_rules 178 net::ProxyRulesExpectation::PerSchemeWithSocks( // proxy_rules
203 "www.google.com:80", // http 179 "www.google.com:80", // http
204 "https://www.foo.com:110", // https 180 "https://www.foo.com:110", // https
205 "ftp.foo.com:121", // ftp 181 "ftp.foo.com:121", // ftp
206 "socks5://socks.com:888", // fallback proxy 182 "socks5://socks.com:888", // fallback proxy
207 "<local>"), // bypass rules 183 "<local>"), // bypass rules
208 }, 184 },
209 185
210 { // 8 186 { // 8
211 TEST_DESC("Bypass rules"), 187 TEST_DESC("Bypass rules"),
212 188
213 true, // is_valid
214
215 { // Input. 189 { // Input.
216 MK_MODE(SINGLE_PROXY), // mode 190 MK_MODE(SINGLE_PROXY), // mode
217 NULL, // pac_url 191 "", // pac_url
218 "www.google.com", // single_uri 192 "www.google.com", // server
219 NULL, NULL, NULL, NULL, // per-proto
220 "*.google.com, *foo.com:99, 1.2.3.4:22, 127.0.0.1/8", // bypass_rules 193 "*.google.com, *foo.com:99, 1.2.3.4:22, 127.0.0.1/8", // bypass_rules
221 }, 194 },
222 195
223 // Expected result. 196 // Expected result.
224 false, // auto_detect 197 false, // auto_detect
225 GURL(), // pac_url 198 GURL(), // pac_url
226 net::ProxyRulesExpectation::Single( // proxy_rules 199 net::ProxyRulesExpectation::Single( // proxy_rules
227 "www.google.com:80", // single proxy 200 "www.google.com:80", // single proxy
228 // bypass_rules 201 // bypass_rules
229 "*.google.com,*foo.com:99,1.2.3.4:22,127.0.0.1/8,<local>"), 202 "*.google.com,*foo.com:99,1.2.3.4:22,127.0.0.1/8,<local>"),
230 }, 203 },
231 }; // tests 204 }; // tests
232 205
233 template<typename TESTBASE> 206 const char* kUserProfilePath = "user_profile";
234 class ProxyConfigServiceImplTestBase : public TESTBASE { 207
208 } // namespace
209
210 class ProxyConfigServiceImplTest : public testing::Test {
235 protected: 211 protected:
236 ProxyConfigServiceImplTestBase() 212 ProxyConfigServiceImplTest()
237 : ui_thread_(BrowserThread::UI, &loop_), 213 : ui_thread_(BrowserThread::UI, &loop_),
238 io_thread_(BrowserThread::IO, &loop_) {} 214 io_thread_(BrowserThread::IO, &loop_) {}
239 215
240 virtual void Init(TestingPrefServiceSimple* pref_service) { 216 virtual void SetUp() {
241 ASSERT_TRUE(pref_service); 217 DBusThreadManager::InitializeWithStub();
242 DBusThreadManager::Initialize(); 218 NetworkStateHandler::Initialize();
243 PrefProxyConfigTrackerImpl::RegisterPrefs(pref_service->registry()); 219 NetworkProfileHandler::Initialize();
244 ProxyConfigServiceImpl::RegisterPrefs(pref_service->registry()); 220
221 SetUpNetwork();
222
223 PrefProxyConfigTrackerImpl::RegisterPrefs(pref_service_.registry());
224 ProxyConfigServiceImpl::RegisterPrefs(pref_service_.registry());
245 proxy_config_service_.reset(new ChromeProxyConfigService(NULL)); 225 proxy_config_service_.reset(new ChromeProxyConfigService(NULL));
246 config_service_impl_.reset(new ProxyConfigServiceImpl(pref_service)); 226 config_service_impl_.reset(new ProxyConfigServiceImpl(&pref_service_));
247 config_service_impl_->SetChromeProxyConfigService( 227 config_service_impl_->SetChromeProxyConfigService(
248 proxy_config_service_.get()); 228 proxy_config_service_.get());
249 // SetChromeProxyConfigService triggers update of initial prefs proxy 229 // SetChromeProxyConfigService triggers update of initial prefs proxy
250 // config by tracker to chrome proxy config service, so flush all pending 230 // config by tracker to chrome proxy config service, so flush all pending
251 // tasks so that tests start fresh. 231 // tasks so that tests start fresh.
252 loop_.RunUntilIdle(); 232 loop_.RunUntilIdle();
253 } 233 }
254 234
235 void SetUpNetwork() {
236 ShillProfileClient::TestInterface* profile_test =
237 DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface();
238 ShillServiceClient::TestInterface* service_test =
239 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
240
241 service_test->ClearServices();
242
243 // Sends a notification about the added profile.
244 profile_test->AddProfile(kUserProfilePath, "user_hash");
245
246 service_test->AddService("stub_wifi2", "wifi2_PSK",
247 flimflam::kTypeWifi, flimflam::kStateOnline,
248 true /* add to watchlist */);
249 service_test->SetServiceProperty("stub_wifi2",
250 flimflam::kGuidProperty,
251 base::StringValue("stub_wifi2"));
252 service_test->SetServiceProperty("stub_wifi2",
253 flimflam::kProfileProperty,
254 base::StringValue(kUserProfilePath));
255 profile_test->AddService("stub_wifi2");
256
257 loop_.RunUntilIdle();
258 }
259
255 virtual void TearDown() { 260 virtual void TearDown() {
256 config_service_impl_->DetachFromPrefService(); 261 config_service_impl_->DetachFromPrefService();
257 loop_.RunUntilIdle(); 262 loop_.RunUntilIdle();
258 config_service_impl_.reset(); 263 config_service_impl_.reset();
259 proxy_config_service_.reset(); 264 proxy_config_service_.reset();
265 NetworkProfileHandler::Shutdown();
266 NetworkStateHandler::Shutdown();
260 DBusThreadManager::Shutdown(); 267 DBusThreadManager::Shutdown();
261 } 268 }
262 269
263 void SetAutomaticProxy( 270 void InitConfigWithTestInput(const Input& input,
264 ProxyConfigServiceImpl::ProxyConfig::Mode mode, 271 base::DictionaryValue* result) {
265 const char* pac_url, 272 base::DictionaryValue* new_config = NULL;
266 ProxyConfigServiceImpl::ProxyConfig* config, 273 switch (input.mode) {
267 ProxyConfigServiceImpl::ProxyConfig::AutomaticProxy* automatic_proxy) { 274 case MK_MODE(DIRECT):
268 config->mode = mode; 275 new_config = ProxyConfigDictionary::CreateDirect();
269 config->state = ProxyPrefs::CONFIG_SYSTEM; 276 break;
270 if (pac_url) 277 case MK_MODE(AUTO_DETECT):
271 automatic_proxy->pac_url = GURL(pac_url); 278 new_config = ProxyConfigDictionary::CreateAutoDetect();
279 break;
280 case MK_MODE(PAC_SCRIPT):
281 new_config =
282 ProxyConfigDictionary::CreatePacScript(input.pac_url, false);
283 break;
284 case MK_MODE(SINGLE_PROXY):
285 case MK_MODE(PROXY_PER_SCHEME):
286 new_config =
287 ProxyConfigDictionary::CreateFixedServers(input.server,
288 input.bypass_rules);
289 break;
290 }
291 result->Swap(new_config);
292 delete new_config;
272 } 293 }
273 294
274 void SetManualProxy( 295 void SetConfig(base::DictionaryValue* pref_proxy_config_dict) {
275 ProxyConfigServiceImpl::ProxyConfig::Mode mode, 296 std::string proxy_config;
276 const char* server_uri, 297 if (pref_proxy_config_dict)
277 net::ProxyServer::Scheme scheme, 298 base::JSONWriter::Write(pref_proxy_config_dict, &proxy_config);
278 ProxyConfigServiceImpl::ProxyConfig* config,
279 ProxyConfigServiceImpl::ProxyConfig::ManualProxy* manual_proxy) {
280 if (!server_uri)
281 return;
282 config->mode = mode;
283 config->state = ProxyPrefs::CONFIG_SYSTEM;
284 manual_proxy->server = net::ProxyServer::FromURI(server_uri, scheme);
285 }
286 299
287 void InitConfigWithTestInput( 300 NetworkStateHandler* network_state_handler = NetworkStateHandler::Get();
288 const Input& input, ProxyConfigServiceImpl::ProxyConfig* test_config) { 301 const NetworkState* network = network_state_handler->DefaultNetwork();
289 switch (input.mode) { 302 ASSERT_TRUE(network);
290 case MK_MODE(DIRECT): 303 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface()->
291 case MK_MODE(AUTO_DETECT): 304 SetServiceProperty(network->path(),
292 case MK_MODE(PAC_SCRIPT): 305 flimflam::kProxyConfigProperty,
293 SetAutomaticProxy(input.mode, input.pac_url, test_config, 306 StringValue(proxy_config));
294 &test_config->automatic_proxy);
295 return;
296 case MK_MODE(SINGLE_PROXY):
297 SetManualProxy(input.mode, input.single_uri, MK_SCHM(HTTP),
298 test_config, &test_config->single_proxy);
299 break;
300 case MK_MODE(PROXY_PER_SCHEME):
301 SetManualProxy(input.mode, input.http_uri, MK_SCHM(HTTP),
302 test_config, &test_config->http_proxy);
303 SetManualProxy(input.mode, input.https_uri, MK_SCHM(HTTPS),
304 test_config, &test_config->https_proxy);
305 SetManualProxy(input.mode, input.ftp_uri, MK_SCHM(HTTP),
306 test_config, &test_config->ftp_proxy);
307 SetManualProxy(input.mode, input.socks_uri, MK_SCHM(SOCKS5),
308 test_config, &test_config->socks_proxy);
309 break;
310 }
311 if (input.bypass_rules)
312 test_config->bypass_rules.ParseFromString(input.bypass_rules);
313 } 307 }
314 308
315 // Synchronously gets the latest proxy config. 309 // Synchronously gets the latest proxy config.
316 net::ProxyConfigService::ConfigAvailability SyncGetLatestProxyConfig( 310 void SyncGetLatestProxyConfig(net::ProxyConfig* config) {
317 net::ProxyConfig* config) {
318 *config = net::ProxyConfig(); 311 *config = net::ProxyConfig();
319 // Let message loop process all messages. 312 // Let message loop process all messages. This will run
313 // ChromeProxyConfigService::UpdateProxyConfig, which is posted on IO from
314 // PrefProxyConfigTrackerImpl::OnProxyConfigChanged.
320 loop_.RunUntilIdle(); 315 loop_.RunUntilIdle();
321 // Calls ChromeProIOGetProxyConfig (which is called from 316 net::ProxyConfigService::ConfigAvailability availability =
322 // ProxyConfigService::GetLatestProxyConfig), running on faked IO thread. 317 proxy_config_service_->GetLatestProxyConfig(config);
323 return proxy_config_service_->GetLatestProxyConfig(config); 318
319 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, availability);
324 } 320 }
325 321
326 MessageLoop loop_; 322 MessageLoop loop_;
327 scoped_ptr<ChromeProxyConfigService> proxy_config_service_; 323 scoped_ptr<ChromeProxyConfigService> proxy_config_service_;
328 scoped_ptr<ProxyConfigServiceImpl> config_service_impl_; 324 scoped_ptr<ProxyConfigServiceImpl> config_service_impl_;
325 TestingPrefServiceSimple pref_service_;
329 326
330 private: 327 private:
331 // Default stub state has ethernet as the active connected network and
332 // PROFILE_SHARED as profile type, which this unittest expects.
333 ScopedStubCrosEnabler stub_cros_enabler_;
334 ScopedTestDeviceSettingsService test_device_settings_service_; 328 ScopedTestDeviceSettingsService test_device_settings_service_;
335 ScopedTestCrosSettings test_cros_settings_; 329 ScopedTestCrosSettings test_cros_settings_;
336 content::TestBrowserThread ui_thread_; 330 content::TestBrowserThread ui_thread_;
337 content::TestBrowserThread io_thread_; 331 content::TestBrowserThread io_thread_;
338 }; 332 };
339 333
340 class ProxyConfigServiceImplTest
341 : public ProxyConfigServiceImplTestBase<testing::Test> {
342 protected:
343 virtual void SetUp() {
344 Init(&pref_service_);
345 }
346
347 TestingPrefServiceSimple pref_service_;
348 };
349
350 TEST_F(ProxyConfigServiceImplTest, NetworkProxy) { 334 TEST_F(ProxyConfigServiceImplTest, NetworkProxy) {
351 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 335 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
352 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "] %s", i, 336 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "] %s", i,
353 tests[i].description.c_str())); 337 tests[i].description.c_str()));
354 338
355 ProxyConfigServiceImpl::ProxyConfig test_config; 339 base::DictionaryValue test_config;
356 InitConfigWithTestInput(tests[i].input, &test_config); 340 InitConfigWithTestInput(tests[i].input, &test_config);
357 config_service_impl_->SetTesting(&test_config); 341 SetConfig(&test_config);
358 342
359 net::ProxyConfig config; 343 net::ProxyConfig config;
360 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&config)); 344 SyncGetLatestProxyConfig(&config);
361 345
362 EXPECT_EQ(tests[i].auto_detect, config.auto_detect()); 346 EXPECT_EQ(tests[i].auto_detect, config.auto_detect());
363 EXPECT_EQ(tests[i].pac_url, config.pac_url()); 347 EXPECT_EQ(tests[i].pac_url, config.pac_url());
364 EXPECT_TRUE(tests[i].proxy_rules.Matches(config.proxy_rules())); 348 EXPECT_TRUE(tests[i].proxy_rules.Matches(config.proxy_rules()));
365 } 349 }
366 } 350 }
367 351
368 TEST_F(ProxyConfigServiceImplTest, ModifyFromUI) {
369 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
370 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "] %s", i,
371 tests[i].description.c_str()));
372
373 // Init with direct.
374 ProxyConfigServiceImpl::ProxyConfig test_config;
375 SetAutomaticProxy(MK_MODE(DIRECT), NULL, &test_config,
376 &test_config.automatic_proxy);
377 config_service_impl_->SetTesting(&test_config);
378
379 // Set config to tests[i].input via UI.
380 net::ProxyBypassRules bypass_rules;
381 const Input& input = tests[i].input;
382 switch (input.mode) {
383 case MK_MODE(DIRECT) :
384 config_service_impl_->UISetProxyConfigToDirect();
385 break;
386 case MK_MODE(AUTO_DETECT) :
387 config_service_impl_->UISetProxyConfigToAutoDetect();
388 break;
389 case MK_MODE(PAC_SCRIPT) :
390 config_service_impl_->UISetProxyConfigToPACScript(GURL(input.pac_url));
391 break;
392 case MK_MODE(SINGLE_PROXY) :
393 config_service_impl_->UISetProxyConfigToSingleProxy(
394 net::ProxyServer::FromURI(input.single_uri, MK_SCHM(HTTP)));
395 if (input.bypass_rules) {
396 bypass_rules.ParseFromString(input.bypass_rules);
397 config_service_impl_->UISetProxyConfigBypassRules(bypass_rules);
398 }
399 break;
400 case MK_MODE(PROXY_PER_SCHEME) :
401 if (input.http_uri) {
402 config_service_impl_->UISetProxyConfigToProxyPerScheme("http",
403 net::ProxyServer::FromURI(input.http_uri, MK_SCHM(HTTP)));
404 }
405 if (input.https_uri) {
406 config_service_impl_->UISetProxyConfigToProxyPerScheme("https",
407 net::ProxyServer::FromURI(input.https_uri, MK_SCHM(HTTPS)));
408 }
409 if (input.ftp_uri) {
410 config_service_impl_->UISetProxyConfigToProxyPerScheme("ftp",
411 net::ProxyServer::FromURI(input.ftp_uri, MK_SCHM(HTTP)));
412 }
413 if (input.socks_uri) {
414 config_service_impl_->UISetProxyConfigToProxyPerScheme("socks",
415 net::ProxyServer::FromURI(input.socks_uri, MK_SCHM(SOCKS5)));
416 }
417 if (input.bypass_rules) {
418 bypass_rules.ParseFromString(input.bypass_rules);
419 config_service_impl_->UISetProxyConfigBypassRules(bypass_rules);
420 }
421 break;
422 }
423
424 // Retrieve config from IO thread.
425 net::ProxyConfig io_config;
426 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&io_config));
427 EXPECT_EQ(tests[i].auto_detect, io_config.auto_detect());
428 EXPECT_EQ(tests[i].pac_url, io_config.pac_url());
429 EXPECT_TRUE(tests[i].proxy_rules.Matches(io_config.proxy_rules()));
430
431 // Retrieve config from UI thread.
432 ProxyConfigServiceImpl::ProxyConfig ui_config;
433 config_service_impl_->UIGetProxyConfig(&ui_config);
434 EXPECT_EQ(input.mode, ui_config.mode);
435 if (tests[i].is_valid) {
436 if (input.pac_url)
437 EXPECT_EQ(GURL(input.pac_url), ui_config.automatic_proxy.pac_url);
438 const net::ProxyRulesExpectation& proxy_rules = tests[i].proxy_rules;
439 if (input.single_uri)
440 EXPECT_EQ(proxy_rules.single_proxy,
441 ui_config.single_proxy.server.ToURI());
442 if (input.http_uri)
443 EXPECT_EQ(proxy_rules.proxy_for_http,
444 ui_config.http_proxy.server.ToURI());
445 if (input.https_uri)
446 EXPECT_EQ(proxy_rules.proxy_for_https,
447 ui_config.https_proxy.server.ToURI());
448 if (input.ftp_uri)
449 EXPECT_EQ(proxy_rules.proxy_for_ftp,
450 ui_config.ftp_proxy.server.ToURI());
451 if (input.socks_uri) {
452 EXPECT_EQ(proxy_rules.fallback_proxy,
453 ui_config.socks_proxy.server.ToURI());
454 }
455 if (input.bypass_rules)
456 EXPECT_TRUE(bypass_rules.Equals(ui_config.bypass_rules));
457 }
458 }
459 }
460
461 TEST_F(ProxyConfigServiceImplTest, DynamicPrefsOverride) { 352 TEST_F(ProxyConfigServiceImplTest, DynamicPrefsOverride) {
462 // Groupings of 3 test inputs to use for managed, recommended and network 353 // Groupings of 3 test inputs to use for managed, recommended and network
463 // proxies respectively. Only valid and non-direct test inputs are used. 354 // proxies respectively. Only valid and non-direct test inputs are used.
464 const size_t proxies[][3] = { 355 const size_t proxies[][3] = {
465 { 1, 2, 4, }, 356 { 1, 2, 4, },
466 { 1, 4, 2, }, 357 { 1, 4, 2, },
467 { 4, 2, 1, }, 358 { 4, 2, 1, },
468 { 2, 1, 4, }, 359 { 2, 1, 4, },
469 { 2, 4, 5, }, 360 { 2, 4, 5, },
470 { 2, 5, 4, }, 361 { 2, 5, 4, },
(...skipping 16 matching lines...) Expand all
487 const TestParams& managed_params = tests[proxies[i][0]]; 378 const TestParams& managed_params = tests[proxies[i][0]];
488 const TestParams& recommended_params = tests[proxies[i][1]]; 379 const TestParams& recommended_params = tests[proxies[i][1]];
489 const TestParams& network_params = tests[proxies[i][2]]; 380 const TestParams& network_params = tests[proxies[i][2]];
490 381
491 SCOPED_TRACE(base::StringPrintf( 382 SCOPED_TRACE(base::StringPrintf(
492 "Test[%" PRIuS "] managed=[%s], recommended=[%s], network=[%s]", i, 383 "Test[%" PRIuS "] managed=[%s], recommended=[%s], network=[%s]", i,
493 managed_params.description.c_str(), 384 managed_params.description.c_str(),
494 recommended_params.description.c_str(), 385 recommended_params.description.c_str(),
495 network_params.description.c_str())); 386 network_params.description.c_str()));
496 387
497 ProxyConfigServiceImpl::ProxyConfig managed_config; 388 base::DictionaryValue managed_config;
498 InitConfigWithTestInput(managed_params.input, &managed_config); 389 InitConfigWithTestInput(managed_params.input, &managed_config);
499 ProxyConfigServiceImpl::ProxyConfig recommended_config; 390 base::DictionaryValue recommended_config;
500 InitConfigWithTestInput(recommended_params.input, &recommended_config); 391 InitConfigWithTestInput(recommended_params.input, &recommended_config);
501 ProxyConfigServiceImpl::ProxyConfig network_config; 392 base::DictionaryValue network_config;
502 InitConfigWithTestInput(network_params.input, &network_config); 393 InitConfigWithTestInput(network_params.input, &network_config);
503 394
504 // Managed proxy pref should take effect over recommended proxy and 395 // Managed proxy pref should take effect over recommended proxy and
505 // non-existent network proxy. 396 // non-existent network proxy.
506 config_service_impl_->SetTesting(NULL); 397 SetConfig(NULL);
507 pref_service_.SetManagedPref(prefs::kProxy, 398 pref_service_.SetManagedPref(prefs::kProxy, managed_config.DeepCopy());
508 managed_config.ToPrefProxyConfig());
509 pref_service_.SetRecommendedPref(prefs::kProxy, 399 pref_service_.SetRecommendedPref(prefs::kProxy,
510 recommended_config.ToPrefProxyConfig()); 400 recommended_config.DeepCopy());
511 net::ProxyConfig actual_config; 401 net::ProxyConfig actual_config;
512 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config)); 402 SyncGetLatestProxyConfig(&actual_config);
513 EXPECT_EQ(managed_params.auto_detect, actual_config.auto_detect()); 403 EXPECT_EQ(managed_params.auto_detect, actual_config.auto_detect());
514 EXPECT_EQ(managed_params.pac_url, actual_config.pac_url()); 404 EXPECT_EQ(managed_params.pac_url, actual_config.pac_url());
515 EXPECT_TRUE(managed_params.proxy_rules.Matches( 405 EXPECT_TRUE(managed_params.proxy_rules.Matches(
516 actual_config.proxy_rules())); 406 actual_config.proxy_rules()));
517 407
518 // Recommended proxy pref should take effect when managed proxy pref is 408 // Recommended proxy pref should take effect when managed proxy pref is
519 // removed. 409 // removed.
520 pref_service_.RemoveManagedPref(prefs::kProxy); 410 pref_service_.RemoveManagedPref(prefs::kProxy);
521 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config)); 411 SyncGetLatestProxyConfig(&actual_config);
522 EXPECT_EQ(recommended_params.auto_detect, actual_config.auto_detect()); 412 EXPECT_EQ(recommended_params.auto_detect, actual_config.auto_detect());
523 EXPECT_EQ(recommended_params.pac_url, actual_config.pac_url()); 413 EXPECT_EQ(recommended_params.pac_url, actual_config.pac_url());
524 EXPECT_TRUE(recommended_params.proxy_rules.Matches( 414 EXPECT_TRUE(recommended_params.proxy_rules.Matches(
525 actual_config.proxy_rules())); 415 actual_config.proxy_rules()));
526 416
527 // Network proxy should take take effect over recommended proxy pref. 417 // Network proxy should take take effect over recommended proxy pref.
528 config_service_impl_->SetTesting(&network_config); 418 SetConfig(&network_config);
529 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config)); 419 SyncGetLatestProxyConfig(&actual_config);
530 EXPECT_EQ(network_params.auto_detect, actual_config.auto_detect()); 420 EXPECT_EQ(network_params.auto_detect, actual_config.auto_detect());
531 EXPECT_EQ(network_params.pac_url, actual_config.pac_url()); 421 EXPECT_EQ(network_params.pac_url, actual_config.pac_url());
532 EXPECT_TRUE(network_params.proxy_rules.Matches( 422 EXPECT_TRUE(network_params.proxy_rules.Matches(
533 actual_config.proxy_rules())); 423 actual_config.proxy_rules()));
534 424
535 // Managed proxy pref should take effect over network proxy. 425 // Managed proxy pref should take effect over network proxy.
536 pref_service_.SetManagedPref(prefs::kProxy, 426 pref_service_.SetManagedPref(prefs::kProxy, managed_config.DeepCopy());
537 managed_config.ToPrefProxyConfig()); 427 SyncGetLatestProxyConfig(&actual_config);
538 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config));
539 EXPECT_EQ(managed_params.auto_detect, actual_config.auto_detect()); 428 EXPECT_EQ(managed_params.auto_detect, actual_config.auto_detect());
540 EXPECT_EQ(managed_params.pac_url, actual_config.pac_url()); 429 EXPECT_EQ(managed_params.pac_url, actual_config.pac_url());
541 EXPECT_TRUE(managed_params.proxy_rules.Matches( 430 EXPECT_TRUE(managed_params.proxy_rules.Matches(
542 actual_config.proxy_rules())); 431 actual_config.proxy_rules()));
543 432
544 // Network proxy should take effect over recommended proxy pref when managed 433 // Network proxy should take effect over recommended proxy pref when managed
545 // proxy pref is removed. 434 // proxy pref is removed.
546 pref_service_.RemoveManagedPref(prefs::kProxy); 435 pref_service_.RemoveManagedPref(prefs::kProxy);
547 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config)); 436 SyncGetLatestProxyConfig(&actual_config);
548 EXPECT_EQ(network_params.auto_detect, actual_config.auto_detect()); 437 EXPECT_EQ(network_params.auto_detect, actual_config.auto_detect());
549 EXPECT_EQ(network_params.pac_url, actual_config.pac_url()); 438 EXPECT_EQ(network_params.pac_url, actual_config.pac_url());
550 EXPECT_TRUE(network_params.proxy_rules.Matches( 439 EXPECT_TRUE(network_params.proxy_rules.Matches(
551 actual_config.proxy_rules())); 440 actual_config.proxy_rules()));
552 441
553 // Removing recommended proxy pref should have no effect on network proxy. 442 // Removing recommended proxy pref should have no effect on network proxy.
554 pref_service_.RemoveRecommendedPref(prefs::kProxy); 443 pref_service_.RemoveRecommendedPref(prefs::kProxy);
555 EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config)); 444 SyncGetLatestProxyConfig(&actual_config);
556 EXPECT_EQ(network_params.auto_detect, actual_config.auto_detect()); 445 EXPECT_EQ(network_params.auto_detect, actual_config.auto_detect());
557 EXPECT_EQ(network_params.pac_url, actual_config.pac_url()); 446 EXPECT_EQ(network_params.pac_url, actual_config.pac_url());
558 EXPECT_TRUE(network_params.proxy_rules.Matches( 447 EXPECT_TRUE(network_params.proxy_rules.Matches(
559 actual_config.proxy_rules())); 448 actual_config.proxy_rules()));
560 } 449 }
561 } 450 }
562 451
563 } // namespace
564
565 } // namespace chromeos 452 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698