proxy_config_service_impl_unittest.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/chromeos/proxy_config_service_impl.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <map>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/format_macros.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/message_loop.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/testing_pref_service.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/string_util.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/stringprintf.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/chromeos/cros/cros_library.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chromeos/dbus/dbus_thread_manager.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/test/test_browser_thread.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/proxy/proxy_config_service_common_unittest.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::BrowserThread;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace chromeos {
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct Input {  // Fields of chromeos::ProxyConfigServiceImpl::ProxyConfig.
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProxyConfigServiceImpl::ProxyConfig::Mode mode;
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const char* pac_url;
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const char* single_uri;
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const char* http_uri;
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const char* https_uri;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const char* ftp_uri;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const char* socks_uri;
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const char* bypass_rules;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Builds an identifier for each test in an array.
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define TEST_DESC(desc) base::StringPrintf("at line %d <%s>", __LINE__, desc)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Shortcuts to declare enums within chromeos's ProxyConfig.
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define MK_MODE(mode) ProxyConfigServiceImpl::ProxyConfig::MODE_##mode
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define MK_SCHM(scheme) net::ProxyServer::SCHEME_##scheme
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define MK_AVAIL(avail) net::ProxyConfigService::CONFIG_##avail
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Inspired from net/proxy/proxy_config_service_linux_unittest.cc.
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const struct TestParams {
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Short description to identify the test
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string description;
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_valid;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Input input;
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Expected outputs from fields of net::ProxyConfig (via IO).
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool auto_detect;
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL pac_url;
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net::ProxyRulesExpectation proxy_rules;
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} tests[] = {
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // 0
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TEST_DESC("No proxying"),
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    true,  // is_valid
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { // Input.
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      MK_MODE(DIRECT),  // mode
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Expected result.
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    false,                                   // auto_detect
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GURL(),                                  // pac_url
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::ProxyRulesExpectation::Empty(),     // proxy_rules
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  },
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // 1
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TEST_DESC("Auto detect"),
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    true,  // is_valid
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { // Input.
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      MK_MODE(AUTO_DETECT),  // mode
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Expected result.
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    true,                                    // auto_detect
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GURL(),                                  // pac_url
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::ProxyRulesExpectation::Empty(),     // proxy_rules
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  },
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // 2
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TEST_DESC("Valid PAC URL"),
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    true,  // is_valid
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { // Input.
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      MK_MODE(PAC_SCRIPT),     // mode
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "http://wpad/wpad.dat",  // pac_url
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Expected result.
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    false,                                   // auto_detect
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GURL("http://wpad/wpad.dat"),            // pac_url
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::ProxyRulesExpectation::Empty(),     // proxy_rules
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  },
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // 3
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TEST_DESC("Invalid PAC URL"),
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    false,  // is_valid
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { // Input.
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      MK_MODE(PAC_SCRIPT),  // mode
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "wpad.dat",           // pac_url
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Expected result.
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    false,                                   // auto_detect
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GURL(),                                  // pac_url
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::ProxyRulesExpectation::Empty(),     // proxy_rules
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  },
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // 4
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TEST_DESC("Single-host in proxy list"),
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    true,  // is_valid
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { // Input.
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      MK_MODE(SINGLE_PROXY),  // mode
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NULL,                   // pac_url
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "www.google.com",       // single_uri
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Expected result.
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    false,                                   // auto_detect
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GURL(),                                  // pac_url
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::ProxyRulesExpectation::Single(      // proxy_rules
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "www.google.com:80",                 // single proxy
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "<local>"),                          // bypass rules
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  },
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // 5
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TEST_DESC("Single-host, different port"),
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    true,   // is_valid
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { // Input.
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      MK_MODE(SINGLE_PROXY),  // mode
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NULL,                   // pac_url
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "www.google.com:99",    // single_uri
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Expected result.
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    false,                                   // auto_detect
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GURL(),                                  // pac_url
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::ProxyRulesExpectation::Single(      // proxy_rules
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "www.google.com:99",                 // single
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "<local>"),                          // bypass rules
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  },
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // 6
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TEST_DESC("Tolerate a scheme"),
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    true,   // is_valid
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { // Input.
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      MK_MODE(SINGLE_PROXY),       // mode
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NULL,                        // pac_url
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "http://www.google.com:99",  // single_uri
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Expected result.
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    false,                                   // auto_detect
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GURL(),                                  // pac_url
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::ProxyRulesExpectation::Single(      // proxy_rules
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "www.google.com:99",                 // single proxy
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "<local>"),                          // bypass rules
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  },
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // 7
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TEST_DESC("Per-scheme proxy rules"),
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    true,  // is_valid
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { // Input.
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      MK_MODE(PROXY_PER_SCHEME),  // mode
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NULL,                       // pac_url
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NULL,                       // single_uri
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "www.google.com:80",        // http_uri
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "www.foo.com:110",          // https_uri
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "ftp.foo.com:121",          // ftp_uri
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "socks.com:888",            // socks_uri
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Expected result.
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    false,                          // auto_detect
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GURL(),                         // pac_url
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::ProxyRulesExpectation::PerSchemeWithSocks(  // proxy_rules
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "www.google.com:80",        // http
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "https://www.foo.com:110",  // https
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "ftp.foo.com:121",          // ftp
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "socks5://socks.com:888",   // fallback proxy
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "<local>"),                 // bypass rules
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  },
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {  // 8
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TEST_DESC("Bypass rules"),
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    true,  // is_valid
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { // Input.
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      MK_MODE(SINGLE_PROXY),      // mode
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NULL,                       // pac_url
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "www.google.com",           // single_uri
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NULL, NULL, NULL, NULL,     // per-proto
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      "*.google.com, *foo.com:99, 1.2.3.4:22, 127.0.0.1/8",  // bypass_rules
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    },
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Expected result.
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    false,                          // auto_detect
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    GURL(),                         // pac_url
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::ProxyRulesExpectation::Single(  // proxy_rules
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "www.google.com:80",             // single proxy
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // bypass_rules
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "*.google.com,*foo.com:99,1.2.3.4:22,127.0.0.1/8,<local>"),
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  },
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};  // tests
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template<typename TESTBASE>
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ProxyConfigServiceImplTestBase : public TESTBASE {
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProxyConfigServiceImplTestBase()
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : ui_thread_(BrowserThread::UI, &loop_),
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        io_thread_(BrowserThread::IO, &loop_) {}
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void Init(TestingPrefServiceSimple* pref_service) {
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ASSERT_TRUE(pref_service);
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DBusThreadManager::Initialize();
2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    PrefProxyConfigTrackerImpl::RegisterPrefs(pref_service->registry());
2422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ProxyConfigServiceImpl::RegisterPrefs(pref_service->registry());
2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    proxy_config_service_.reset(new ChromeProxyConfigService(NULL));
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    config_service_impl_.reset(new ProxyConfigServiceImpl(pref_service));
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    config_service_impl_->SetChromeProxyConfigService(
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        proxy_config_service_.get());
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // SetChromeProxyConfigService triggers update of initial prefs proxy
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // config by tracker to chrome proxy config service, so flush all pending
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // tasks so that tests start fresh.
2502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    loop_.RunUntilIdle();
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void TearDown() {
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    config_service_impl_->DetachFromPrefService();
2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    loop_.RunUntilIdle();
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    config_service_impl_.reset();
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    proxy_config_service_.reset();
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DBusThreadManager::Shutdown();
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetAutomaticProxy(
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ProxyConfigServiceImpl::ProxyConfig::Mode mode,
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const char* pac_url,
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ProxyConfigServiceImpl::ProxyConfig* config,
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ProxyConfigServiceImpl::ProxyConfig::AutomaticProxy* automatic_proxy) {
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    config->mode = mode;
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    config->state = ProxyPrefs::CONFIG_SYSTEM;
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (pac_url)
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      automatic_proxy->pac_url = GURL(pac_url);
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetManualProxy(
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ProxyConfigServiceImpl::ProxyConfig::Mode mode,
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const char* server_uri,
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      net::ProxyServer::Scheme scheme,
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ProxyConfigServiceImpl::ProxyConfig* config,
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ProxyConfigServiceImpl::ProxyConfig::ManualProxy* manual_proxy) {
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!server_uri)
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    config->mode = mode;
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    config->state = ProxyPrefs::CONFIG_SYSTEM;
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    manual_proxy->server = net::ProxyServer::FromURI(server_uri, scheme);
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void InitConfigWithTestInput(
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const Input& input, ProxyConfigServiceImpl::ProxyConfig* test_config) {
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    switch (input.mode) {
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case MK_MODE(DIRECT):
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case MK_MODE(AUTO_DETECT):
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case MK_MODE(PAC_SCRIPT):
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        SetAutomaticProxy(input.mode, input.pac_url, test_config,
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          &test_config->automatic_proxy);
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return;
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case MK_MODE(SINGLE_PROXY):
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        SetManualProxy(input.mode, input.single_uri, MK_SCHM(HTTP),
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       test_config, &test_config->single_proxy);
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case MK_MODE(PROXY_PER_SCHEME):
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        SetManualProxy(input.mode, input.http_uri, MK_SCHM(HTTP),
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       test_config, &test_config->http_proxy);
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        SetManualProxy(input.mode, input.https_uri, MK_SCHM(HTTPS),
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       test_config, &test_config->https_proxy);
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        SetManualProxy(input.mode, input.ftp_uri, MK_SCHM(HTTP),
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       test_config, &test_config->ftp_proxy);
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        SetManualProxy(input.mode, input.socks_uri, MK_SCHM(SOCKS5),
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       test_config, &test_config->socks_proxy);
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (input.bypass_rules)
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      test_config->bypass_rules.ParseFromString(input.bypass_rules);
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Synchronously gets the latest proxy config.
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net::ProxyConfigService::ConfigAvailability SyncGetLatestProxyConfig(
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      net::ProxyConfig* config) {
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    *config = net::ProxyConfig();
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Let message loop process all messages.
3182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    loop_.RunUntilIdle();
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Calls ChromeProIOGetProxyConfig (which is called from
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // ProxyConfigService::GetLatestProxyConfig), running on faked IO thread.
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return proxy_config_service_->GetLatestProxyConfig(config);
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MessageLoop loop_;
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<ChromeProxyConfigService> proxy_config_service_;
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<ProxyConfigServiceImpl> config_service_impl_;
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Default stub state has ethernet as the active connected network and
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // PROFILE_SHARED as profile type, which this unittest expects.
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ScopedStubCrosEnabler stub_cros_enabler_;
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::TestBrowserThread ui_thread_;
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  content::TestBrowserThread io_thread_;
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ProxyConfigServiceImplTest
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : public ProxyConfigServiceImplTestBase<testing::Test> {
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetUp() {
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Init(&pref_service_);
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TestingPrefServiceSimple pref_service_;
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ProxyConfigServiceImplTest, NetworkProxy) {
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
3482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "] %s", i,
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              tests[i].description.c_str()));
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ProxyConfigServiceImpl::ProxyConfig test_config;
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    InitConfigWithTestInput(tests[i].input, &test_config);
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    config_service_impl_->SetTesting(&test_config);
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::ProxyConfig config;
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&config));
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(tests[i].auto_detect, config.auto_detect());
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(tests[i].pac_url, config.pac_url());
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(tests[i].proxy_rules.Matches(config.proxy_rules()));
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ProxyConfigServiceImplTest, ModifyFromUI) {
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
3662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "] %s", i,
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              tests[i].description.c_str()));
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Init with direct.
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ProxyConfigServiceImpl::ProxyConfig test_config;
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SetAutomaticProxy(MK_MODE(DIRECT), NULL, &test_config,
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      &test_config.automatic_proxy);
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    config_service_impl_->SetTesting(&test_config);
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Set config to tests[i].input via UI.
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::ProxyBypassRules bypass_rules;
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const Input& input = tests[i].input;
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    switch (input.mode) {
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case MK_MODE(DIRECT) :
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        config_service_impl_->UISetProxyConfigToDirect();
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case MK_MODE(AUTO_DETECT) :
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        config_service_impl_->UISetProxyConfigToAutoDetect();
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case MK_MODE(PAC_SCRIPT) :
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        config_service_impl_->UISetProxyConfigToPACScript(GURL(input.pac_url));
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case MK_MODE(SINGLE_PROXY) :
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        config_service_impl_->UISetProxyConfigToSingleProxy(
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            net::ProxyServer::FromURI(input.single_uri, MK_SCHM(HTTP)));
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (input.bypass_rules) {
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          bypass_rules.ParseFromString(input.bypass_rules);
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          config_service_impl_->UISetProxyConfigBypassRules(bypass_rules);
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case MK_MODE(PROXY_PER_SCHEME) :
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (input.http_uri) {
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          config_service_impl_->UISetProxyConfigToProxyPerScheme("http",
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  net::ProxyServer::FromURI(input.http_uri, MK_SCHM(HTTP)));
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (input.https_uri) {
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          config_service_impl_->UISetProxyConfigToProxyPerScheme("https",
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              net::ProxyServer::FromURI(input.https_uri, MK_SCHM(HTTPS)));
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (input.ftp_uri) {
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          config_service_impl_->UISetProxyConfigToProxyPerScheme("ftp",
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              net::ProxyServer::FromURI(input.ftp_uri, MK_SCHM(HTTP)));
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (input.socks_uri) {
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          config_service_impl_->UISetProxyConfigToProxyPerScheme("socks",
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              net::ProxyServer::FromURI(input.socks_uri, MK_SCHM(SOCKS5)));
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (input.bypass_rules) {
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          bypass_rules.ParseFromString(input.bypass_rules);
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          config_service_impl_->UISetProxyConfigBypassRules(bypass_rules);
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Retrieve config from IO thread.
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::ProxyConfig io_config;
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&io_config));
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(tests[i].auto_detect, io_config.auto_detect());
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(tests[i].pac_url, io_config.pac_url());
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(tests[i].proxy_rules.Matches(io_config.proxy_rules()));
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Retrieve config from UI thread.
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ProxyConfigServiceImpl::ProxyConfig ui_config;
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    config_service_impl_->UIGetProxyConfig(&ui_config);
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(input.mode, ui_config.mode);
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (tests[i].is_valid) {
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (input.pac_url)
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        EXPECT_EQ(GURL(input.pac_url), ui_config.automatic_proxy.pac_url);
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const net::ProxyRulesExpectation& proxy_rules = tests[i].proxy_rules;
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (input.single_uri)
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        EXPECT_EQ(proxy_rules.single_proxy,
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  ui_config.single_proxy.server.ToURI());
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (input.http_uri)
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        EXPECT_EQ(proxy_rules.proxy_for_http,
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  ui_config.http_proxy.server.ToURI());
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (input.https_uri)
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        EXPECT_EQ(proxy_rules.proxy_for_https,
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  ui_config.https_proxy.server.ToURI());
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (input.ftp_uri)
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        EXPECT_EQ(proxy_rules.proxy_for_ftp,
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  ui_config.ftp_proxy.server.ToURI());
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (input.socks_uri) {
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        EXPECT_EQ(proxy_rules.fallback_proxy,
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  ui_config.socks_proxy.server.ToURI());
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (input.bypass_rules)
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        EXPECT_TRUE(bypass_rules.Equals(ui_config.bypass_rules));
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ProxyConfigServiceImplTest, DynamicPrefsOverride) {
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Groupings of 3 test inputs to use for managed, recommended and network
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // proxies respectively.  Only valid and non-direct test inputs are used.
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const size_t proxies[][3] = {
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { 1, 2, 4, },
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { 1, 4, 2, },
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { 4, 2, 1, },
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { 2, 1, 4, },
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { 2, 4, 5, },
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { 2, 5, 4, },
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { 5, 4, 2, },
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { 4, 2, 5, },
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { 4, 5, 6, },
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { 4, 6, 5, },
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { 6, 5, 4, },
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { 5, 4, 6, },
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { 5, 6, 7, },
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { 5, 7, 6, },
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { 7, 6, 5, },
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { 6, 5, 7, },
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { 6, 7, 8, },
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { 6, 8, 7, },
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { 8, 7, 6, },
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    { 7, 6, 8, },
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(proxies); ++i) {
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const TestParams& managed_params = tests[proxies[i][0]];
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const TestParams& recommended_params = tests[proxies[i][1]];
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const TestParams& network_params = tests[proxies[i][2]];
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SCOPED_TRACE(base::StringPrintf(
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        "Test[%" PRIuS "] managed=[%s], recommended=[%s], network=[%s]", i,
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        managed_params.description.c_str(),
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        recommended_params.description.c_str(),
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        network_params.description.c_str()));
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ProxyConfigServiceImpl::ProxyConfig managed_config;
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    InitConfigWithTestInput(managed_params.input, &managed_config);
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ProxyConfigServiceImpl::ProxyConfig recommended_config;
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    InitConfigWithTestInput(recommended_params.input, &recommended_config);
4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ProxyConfigServiceImpl::ProxyConfig network_config;
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    InitConfigWithTestInput(network_params.input, &network_config);
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Managed proxy pref should take effect over recommended proxy and
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // non-existent network proxy.
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    config_service_impl_->SetTesting(NULL);
5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    pref_service_.SetManagedPref(prefs::kProxy,
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 managed_config.ToPrefProxyConfig());
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    pref_service_.SetRecommendedPref(prefs::kProxy,
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     recommended_config.ToPrefProxyConfig());
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::ProxyConfig actual_config;
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config));
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(managed_params.auto_detect, actual_config.auto_detect());
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(managed_params.pac_url, actual_config.pac_url());
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(managed_params.proxy_rules.Matches(
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        actual_config.proxy_rules()));
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Recommended proxy pref should take effect when managed proxy pref is
5155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // removed.
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    pref_service_.RemoveManagedPref(prefs::kProxy);
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config));
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(recommended_params.auto_detect, actual_config.auto_detect());
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(recommended_params.pac_url, actual_config.pac_url());
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(recommended_params.proxy_rules.Matches(
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        actual_config.proxy_rules()));
5225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Network proxy should take take effect over recommended proxy pref.
5245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    config_service_impl_->SetTesting(&network_config);
5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config));
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(network_params.auto_detect, actual_config.auto_detect());
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(network_params.pac_url, actual_config.pac_url());
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(network_params.proxy_rules.Matches(
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        actual_config.proxy_rules()));
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Managed proxy pref should take effect over network proxy.
5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    pref_service_.SetManagedPref(prefs::kProxy,
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 managed_config.ToPrefProxyConfig());
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config));
5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(managed_params.auto_detect, actual_config.auto_detect());
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(managed_params.pac_url, actual_config.pac_url());
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(managed_params.proxy_rules.Matches(
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        actual_config.proxy_rules()));
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Network proxy should take effect over recommended proxy pref when managed
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // proxy pref is removed.
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    pref_service_.RemoveManagedPref(prefs::kProxy);
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config));
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(network_params.auto_detect, actual_config.auto_detect());
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(network_params.pac_url, actual_config.pac_url());
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(network_params.proxy_rules.Matches(
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        actual_config.proxy_rules()));
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Removing recommended proxy pref should have no effect on network proxy.
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    pref_service_.RemoveRecommendedPref(prefs::kProxy);
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(MK_AVAIL(VALID), SyncGetLatestProxyConfig(&actual_config));
5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(network_params.auto_detect, actual_config.auto_detect());
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_EQ(network_params.pac_url, actual_config.pac_url());
5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    EXPECT_TRUE(network_params.proxy_rules.Matches(
5555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        actual_config.proxy_rules()));
5565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace chromeos
562