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

Side by Side Diff: chrome/browser/extensions/api/proxy/proxy_api_helpers_unittest.cc

Issue 2785883003: Use unique_ptr<DictionaryValue> in ProxyConfigDictionary (Closed)
Patch Set: Fix compilation Created 3 years, 8 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
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 // Unit tests for helper functions for the Chrome Extensions Proxy Settings API. 5 // Unit tests for helper functions for the Chrome Extensions Proxy Settings API.
6 6
7 #include "chrome/browser/extensions/api/proxy/proxy_api_helpers.h" 7 #include "chrome/browser/extensions/api/proxy/proxy_api_helpers.h"
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility>
10 11
11 #include "base/values.h" 12 #include "base/values.h"
12 #include "chrome/browser/extensions/api/proxy/proxy_api_constants.h" 13 #include "chrome/browser/extensions/api/proxy/proxy_api_constants.h"
13 #include "components/proxy_config/proxy_config_dictionary.h" 14 #include "components/proxy_config/proxy_config_dictionary.h"
14 #include "components/proxy_config/proxy_prefs.h" 15 #include "components/proxy_config/proxy_prefs.h"
15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
16 17
17 namespace extensions { 18 namespace extensions {
18 19
19 namespace keys = proxy_api_constants; 20 namespace keys = proxy_api_constants;
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 ASSERT_TRUE( 216 ASSERT_TRUE(
216 GetBypassListFromExtensionPref(&proxy_config, &out, &error, 217 GetBypassListFromExtensionPref(&proxy_config, &out, &error,
217 &bad_message)); 218 &bad_message));
218 EXPECT_EQ("host1,host2", out); 219 EXPECT_EQ("host1,host2", out);
219 EXPECT_EQ(std::string(), error); 220 EXPECT_EQ(std::string(), error);
220 EXPECT_FALSE(bad_message); 221 EXPECT_FALSE(bad_message);
221 } 222 }
222 223
223 TEST(ExtensionProxyApiHelpers, CreateProxyConfigDict) { 224 TEST(ExtensionProxyApiHelpers, CreateProxyConfigDict) {
224 std::string error; 225 std::string error;
225 std::unique_ptr<base::DictionaryValue> exp_direct( 226 std::unique_ptr<base::DictionaryValue> exp_direct =
226 ProxyConfigDictionary::CreateDirect()); 227 ProxyConfigDictionary::CreateDirect();
227 std::unique_ptr<base::DictionaryValue> out_direct(CreateProxyConfigDict( 228 std::unique_ptr<base::DictionaryValue> out_direct(CreateProxyConfigDict(
228 ProxyPrefs::MODE_DIRECT, false, std::string(), std::string(), 229 ProxyPrefs::MODE_DIRECT, false, std::string(), std::string(),
229 std::string(), std::string(), &error)); 230 std::string(), std::string(), &error));
230 EXPECT_TRUE(base::Value::Equals(exp_direct.get(), out_direct.get())); 231 EXPECT_TRUE(base::Value::Equals(exp_direct.get(), out_direct.get()));
231 232
232 std::unique_ptr<base::DictionaryValue> exp_auto( 233 std::unique_ptr<base::DictionaryValue> exp_auto =
233 ProxyConfigDictionary::CreateAutoDetect()); 234 ProxyConfigDictionary::CreateAutoDetect();
234 std::unique_ptr<base::DictionaryValue> out_auto(CreateProxyConfigDict( 235 std::unique_ptr<base::DictionaryValue> out_auto(CreateProxyConfigDict(
235 ProxyPrefs::MODE_AUTO_DETECT, false, std::string(), std::string(), 236 ProxyPrefs::MODE_AUTO_DETECT, false, std::string(), std::string(),
236 std::string(), std::string(), &error)); 237 std::string(), std::string(), &error));
237 EXPECT_TRUE(base::Value::Equals(exp_auto.get(), out_auto.get())); 238 EXPECT_TRUE(base::Value::Equals(exp_auto.get(), out_auto.get()));
238 239
239 std::unique_ptr<base::DictionaryValue> exp_pac_url( 240 std::unique_ptr<base::DictionaryValue> exp_pac_url =
240 ProxyConfigDictionary::CreatePacScript(kSamplePacScriptUrl, false)); 241 ProxyConfigDictionary::CreatePacScript(kSamplePacScriptUrl, false);
241 std::unique_ptr<base::DictionaryValue> out_pac_url(CreateProxyConfigDict( 242 std::unique_ptr<base::DictionaryValue> out_pac_url(CreateProxyConfigDict(
242 ProxyPrefs::MODE_PAC_SCRIPT, false, kSamplePacScriptUrl, std::string(), 243 ProxyPrefs::MODE_PAC_SCRIPT, false, kSamplePacScriptUrl, std::string(),
243 std::string(), std::string(), &error)); 244 std::string(), std::string(), &error));
244 EXPECT_TRUE(base::Value::Equals(exp_pac_url.get(), out_pac_url.get())); 245 EXPECT_TRUE(base::Value::Equals(exp_pac_url.get(), out_pac_url.get()));
245 246
246 std::unique_ptr<base::DictionaryValue> exp_pac_data( 247 std::unique_ptr<base::DictionaryValue> exp_pac_data =
247 ProxyConfigDictionary::CreatePacScript(kSamplePacScriptAsDataUrl, false)); 248 ProxyConfigDictionary::CreatePacScript(kSamplePacScriptAsDataUrl, false);
248 std::unique_ptr<base::DictionaryValue> out_pac_data(CreateProxyConfigDict( 249 std::unique_ptr<base::DictionaryValue> out_pac_data(CreateProxyConfigDict(
249 ProxyPrefs::MODE_PAC_SCRIPT, false, std::string(), kSamplePacScript, 250 ProxyPrefs::MODE_PAC_SCRIPT, false, std::string(), kSamplePacScript,
250 std::string(), std::string(), &error)); 251 std::string(), std::string(), &error));
251 EXPECT_TRUE(base::Value::Equals(exp_pac_data.get(), out_pac_data.get())); 252 EXPECT_TRUE(base::Value::Equals(exp_pac_data.get(), out_pac_data.get()));
252 253
253 std::unique_ptr<base::DictionaryValue> exp_fixed( 254 std::unique_ptr<base::DictionaryValue> exp_fixed =
254 ProxyConfigDictionary::CreateFixedServers("foo:80", "localhost")); 255 ProxyConfigDictionary::CreateFixedServers("foo:80", "localhost");
255 std::unique_ptr<base::DictionaryValue> out_fixed(CreateProxyConfigDict( 256 std::unique_ptr<base::DictionaryValue> out_fixed(CreateProxyConfigDict(
256 ProxyPrefs::MODE_FIXED_SERVERS, false, std::string(), std::string(), 257 ProxyPrefs::MODE_FIXED_SERVERS, false, std::string(), std::string(),
257 "foo:80", "localhost", &error)); 258 "foo:80", "localhost", &error));
258 EXPECT_TRUE(base::Value::Equals(exp_fixed.get(), out_fixed.get())); 259 EXPECT_TRUE(base::Value::Equals(exp_fixed.get(), out_fixed.get()));
259 260
260 std::unique_ptr<base::DictionaryValue> exp_system( 261 std::unique_ptr<base::DictionaryValue> exp_system =
261 ProxyConfigDictionary::CreateSystem()); 262 ProxyConfigDictionary::CreateSystem();
262 std::unique_ptr<base::DictionaryValue> out_system(CreateProxyConfigDict( 263 std::unique_ptr<base::DictionaryValue> out_system(CreateProxyConfigDict(
263 ProxyPrefs::MODE_SYSTEM, false, std::string(), std::string(), 264 ProxyPrefs::MODE_SYSTEM, false, std::string(), std::string(),
264 std::string(), std::string(), &error)); 265 std::string(), std::string(), &error));
265 EXPECT_TRUE(base::Value::Equals(exp_system.get(), out_system.get())); 266 EXPECT_TRUE(base::Value::Equals(exp_system.get(), out_system.get()));
266 267
267 // Neither of them should have set an error. 268 // Neither of them should have set an error.
268 EXPECT_EQ(std::string(), error); 269 EXPECT_EQ(std::string(), error);
269 } 270 }
270 271
271 TEST(ExtensionProxyApiHelpers, GetProxyServer) { 272 TEST(ExtensionProxyApiHelpers, GetProxyServer) {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 EXPECT_EQ("s1;s2;s3", out); 306 EXPECT_EQ("s1;s2;s3", out);
306 EXPECT_FALSE(bad_message); 307 EXPECT_FALSE(bad_message);
307 } 308 }
308 309
309 // This tests CreateProxyServerDict as well. 310 // This tests CreateProxyServerDict as well.
310 TEST(ExtensionProxyApiHelpers, CreateProxyRulesDict) { 311 TEST(ExtensionProxyApiHelpers, CreateProxyRulesDict) {
311 std::unique_ptr<base::DictionaryValue> browser_pref( 312 std::unique_ptr<base::DictionaryValue> browser_pref(
312 ProxyConfigDictionary::CreateFixedServers( 313 ProxyConfigDictionary::CreateFixedServers(
313 "http=proxy1:80;https=proxy2:80;ftp=proxy3:80;socks=proxy4:80", 314 "http=proxy1:80;https=proxy2:80;ftp=proxy3:80;socks=proxy4:80",
314 "localhost")); 315 "localhost"));
315 ProxyConfigDictionary config(browser_pref.get()); 316 ProxyConfigDictionary config(std::move(browser_pref));
316 std::unique_ptr<base::DictionaryValue> extension_pref( 317 std::unique_ptr<base::DictionaryValue> extension_pref(
317 CreateProxyRulesDict(config)); 318 CreateProxyRulesDict(config));
318 ASSERT_TRUE(extension_pref.get()); 319 ASSERT_TRUE(extension_pref.get());
319 320
320 std::unique_ptr<base::DictionaryValue> expected(new base::DictionaryValue); 321 std::unique_ptr<base::DictionaryValue> expected(new base::DictionaryValue);
321 expected->Set("proxyForHttp", 322 expected->Set("proxyForHttp",
322 CreateTestProxyServerDict("http", "proxy1", 80)); 323 CreateTestProxyServerDict("http", "proxy1", 80));
323 expected->Set("proxyForHttps", 324 expected->Set("proxyForHttps",
324 CreateTestProxyServerDict("http", "proxy2", 80)); 325 CreateTestProxyServerDict("http", "proxy2", 80));
325 expected->Set("proxyForFtp", 326 expected->Set("proxyForFtp",
326 CreateTestProxyServerDict("http", "proxy3", 80)); 327 CreateTestProxyServerDict("http", "proxy3", 80));
327 expected->Set("fallbackProxy", 328 expected->Set("fallbackProxy",
328 CreateTestProxyServerDict("socks4", "proxy4", 80)); 329 CreateTestProxyServerDict("socks4", "proxy4", 80));
329 base::ListValue* bypass_list = new base::ListValue; 330 base::ListValue* bypass_list = new base::ListValue;
330 bypass_list->AppendString("localhost"); 331 bypass_list->AppendString("localhost");
331 expected->Set(keys::kProxyConfigBypassList, bypass_list); 332 expected->Set(keys::kProxyConfigBypassList, bypass_list);
332 333
333 EXPECT_TRUE(base::Value::Equals(expected.get(), extension_pref.get())); 334 EXPECT_TRUE(base::Value::Equals(expected.get(), extension_pref.get()));
334 } 335 }
335 336
336 // Test multiple proxies per scheme -- expect that only the first is returned. 337 // Test multiple proxies per scheme -- expect that only the first is returned.
337 TEST(ExtensionProxyApiHelpers, CreateProxyRulesDictMultipleProxies) { 338 TEST(ExtensionProxyApiHelpers, CreateProxyRulesDictMultipleProxies) {
338 std::unique_ptr<base::DictionaryValue> browser_pref( 339 std::unique_ptr<base::DictionaryValue> browser_pref(
339 ProxyConfigDictionary::CreateFixedServers( 340 ProxyConfigDictionary::CreateFixedServers(
340 "http=proxy1:80,default://;https=proxy2:80,proxy1:80;ftp=proxy3:80," 341 "http=proxy1:80,default://;https=proxy2:80,proxy1:80;ftp=proxy3:80,"
341 "https://proxy5:443;socks=proxy4:80,proxy1:80", 342 "https://proxy5:443;socks=proxy4:80,proxy1:80",
342 "localhost")); 343 "localhost"));
343 ProxyConfigDictionary config(browser_pref.get()); 344 ProxyConfigDictionary config(std::move(browser_pref));
344 std::unique_ptr<base::DictionaryValue> extension_pref( 345 std::unique_ptr<base::DictionaryValue> extension_pref(
345 CreateProxyRulesDict(config)); 346 CreateProxyRulesDict(config));
346 ASSERT_TRUE(extension_pref.get()); 347 ASSERT_TRUE(extension_pref.get());
347 348
348 std::unique_ptr<base::DictionaryValue> expected(new base::DictionaryValue); 349 std::unique_ptr<base::DictionaryValue> expected(new base::DictionaryValue);
349 expected->Set("proxyForHttp", 350 expected->Set("proxyForHttp",
350 CreateTestProxyServerDict("http", "proxy1", 80)); 351 CreateTestProxyServerDict("http", "proxy1", 80));
351 expected->Set("proxyForHttps", 352 expected->Set("proxyForHttps",
352 CreateTestProxyServerDict("http", "proxy2", 80)); 353 CreateTestProxyServerDict("http", "proxy2", 80));
353 expected->Set("proxyForFtp", 354 expected->Set("proxyForFtp",
354 CreateTestProxyServerDict("http", "proxy3", 80)); 355 CreateTestProxyServerDict("http", "proxy3", 80));
355 expected->Set("fallbackProxy", 356 expected->Set("fallbackProxy",
356 CreateTestProxyServerDict("socks4", "proxy4", 80)); 357 CreateTestProxyServerDict("socks4", "proxy4", 80));
357 base::ListValue* bypass_list = new base::ListValue; 358 base::ListValue* bypass_list = new base::ListValue;
358 bypass_list->AppendString("localhost"); 359 bypass_list->AppendString("localhost");
359 expected->Set(keys::kProxyConfigBypassList, bypass_list); 360 expected->Set(keys::kProxyConfigBypassList, bypass_list);
360 361
361 EXPECT_TRUE(base::Value::Equals(expected.get(), extension_pref.get())); 362 EXPECT_TRUE(base::Value::Equals(expected.get(), extension_pref.get()));
362 } 363 }
363 364
364 // Test if a PAC script URL is specified. 365 // Test if a PAC script URL is specified.
365 TEST(ExtensionProxyApiHelpers, CreatePacScriptDictWithUrl) { 366 TEST(ExtensionProxyApiHelpers, CreatePacScriptDictWithUrl) {
366 std::unique_ptr<base::DictionaryValue> browser_pref( 367 std::unique_ptr<base::DictionaryValue> browser_pref(
367 ProxyConfigDictionary::CreatePacScript(kSamplePacScriptUrl, false)); 368 ProxyConfigDictionary::CreatePacScript(kSamplePacScriptUrl, false));
368 ProxyConfigDictionary config(browser_pref.get()); 369 ProxyConfigDictionary config(std::move(browser_pref));
369 std::unique_ptr<base::DictionaryValue> extension_pref( 370 std::unique_ptr<base::DictionaryValue> extension_pref(
370 CreatePacScriptDict(config)); 371 CreatePacScriptDict(config));
371 ASSERT_TRUE(extension_pref.get()); 372 ASSERT_TRUE(extension_pref.get());
372 373
373 std::unique_ptr<base::DictionaryValue> expected(new base::DictionaryValue); 374 std::unique_ptr<base::DictionaryValue> expected(new base::DictionaryValue);
374 expected->SetString(keys::kProxyConfigPacScriptUrl, kSamplePacScriptUrl); 375 expected->SetString(keys::kProxyConfigPacScriptUrl, kSamplePacScriptUrl);
375 expected->SetBoolean(keys::kProxyConfigPacScriptMandatory, false); 376 expected->SetBoolean(keys::kProxyConfigPacScriptMandatory, false);
376 377
377 EXPECT_TRUE(base::Value::Equals(expected.get(), extension_pref.get())); 378 EXPECT_TRUE(base::Value::Equals(expected.get(), extension_pref.get()));
378 } 379 }
379 380
380 // Test if a PAC script is encoded in a data URL. 381 // Test if a PAC script is encoded in a data URL.
381 TEST(ExtensionProxyApiHelpers, CreatePacScriptDictWidthData) { 382 TEST(ExtensionProxyApiHelpers, CreatePacScriptDictWidthData) {
382 std::unique_ptr<base::DictionaryValue> browser_pref( 383 std::unique_ptr<base::DictionaryValue> browser_pref(
383 ProxyConfigDictionary::CreatePacScript(kSamplePacScriptAsDataUrl, false)); 384 ProxyConfigDictionary::CreatePacScript(kSamplePacScriptAsDataUrl, false));
384 ProxyConfigDictionary config(browser_pref.get()); 385 ProxyConfigDictionary config(std::move(browser_pref));
385 std::unique_ptr<base::DictionaryValue> extension_pref( 386 std::unique_ptr<base::DictionaryValue> extension_pref(
386 CreatePacScriptDict(config)); 387 CreatePacScriptDict(config));
387 ASSERT_TRUE(extension_pref.get()); 388 ASSERT_TRUE(extension_pref.get());
388 389
389 std::unique_ptr<base::DictionaryValue> expected(new base::DictionaryValue); 390 std::unique_ptr<base::DictionaryValue> expected(new base::DictionaryValue);
390 expected->SetString(keys::kProxyConfigPacScriptData, kSamplePacScript); 391 expected->SetString(keys::kProxyConfigPacScriptData, kSamplePacScript);
391 expected->SetBoolean(keys::kProxyConfigPacScriptMandatory, false); 392 expected->SetBoolean(keys::kProxyConfigPacScriptMandatory, false);
392 393
393 EXPECT_TRUE(base::Value::Equals(expected.get(), extension_pref.get())); 394 EXPECT_TRUE(base::Value::Equals(expected.get(), extension_pref.get()));
394 } 395 }
395 396
396 TEST(ExtensionProxyApiHelpers, TokenizeToStringList) { 397 TEST(ExtensionProxyApiHelpers, TokenizeToStringList) {
397 base::ListValue expected; 398 base::ListValue expected;
398 expected.AppendString("s1"); 399 expected.AppendString("s1");
399 expected.AppendString("s2"); 400 expected.AppendString("s2");
400 expected.AppendString("s3"); 401 expected.AppendString("s3");
401 402
402 std::unique_ptr<base::ListValue> out(TokenizeToStringList("s1;s2;s3", ";")); 403 std::unique_ptr<base::ListValue> out(TokenizeToStringList("s1;s2;s3", ";"));
403 EXPECT_TRUE(base::Value::Equals(&expected, out.get())); 404 EXPECT_TRUE(base::Value::Equals(&expected, out.get()));
404 } 405 }
405 406
406 } // namespace proxy_api_helpers 407 } // namespace proxy_api_helpers
407 } // namespace extensions 408 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698