Home | History | Annotate | Download | only in importer
      1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "testing/gtest/include/gtest/gtest.h"
      6 
      7 #include "base/file_path.h"
      8 #include "base/path_service.h"
      9 #include "chrome/browser/importer/firefox_proxy_settings.h"
     10 #include "chrome/common/chrome_paths.h"
     11 #include "net/proxy/proxy_config.h"
     12 
     13 class FirefoxProxySettingsTest : public testing::Test {
     14 };
     15 
     16 class TestFirefoxProxySettings : public FirefoxProxySettings {
     17  public:
     18   TestFirefoxProxySettings() {}
     19 
     20   static bool TestGetSettingsFromFile(const FilePath& pref_file,
     21                                       FirefoxProxySettings* settings) {
     22     return GetSettingsFromFile(pref_file, settings);
     23   }
     24 };
     25 
     26 TEST_F(FirefoxProxySettingsTest, TestParse) {
     27   FirefoxProxySettings settings;
     28 
     29   FilePath js_pref_path;
     30   ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &js_pref_path));
     31   js_pref_path = js_pref_path.AppendASCII("firefox3_pref.js");
     32 
     33   EXPECT_TRUE(TestFirefoxProxySettings::TestGetSettingsFromFile(js_pref_path,
     34                                                                 &settings));
     35   EXPECT_EQ(FirefoxProxySettings::MANUAL, settings.config_type());
     36   EXPECT_EQ("http_proxy", settings.http_proxy());
     37   EXPECT_EQ(1111, settings.http_proxy_port());
     38   EXPECT_EQ("ssl_proxy", settings.ssl_proxy());
     39   EXPECT_EQ(2222, settings.ssl_proxy_port());
     40   EXPECT_EQ("ftp_proxy", settings.ftp_proxy());
     41   EXPECT_EQ(3333, settings.ftp_proxy_port());
     42   EXPECT_EQ("gopher_proxy", settings.gopher_proxy());
     43   EXPECT_EQ(4444, settings.gopher_proxy_port());
     44   EXPECT_EQ("socks_host", settings.socks_host());
     45   EXPECT_EQ(5555, settings.socks_port());
     46   EXPECT_EQ(FirefoxProxySettings::V4, settings.socks_version());
     47   ASSERT_EQ(3U, settings.proxy_bypass_list().size());
     48   EXPECT_EQ("localhost", settings.proxy_bypass_list()[0]);
     49   EXPECT_EQ("127.0.0.1", settings.proxy_bypass_list()[1]);
     50   EXPECT_EQ("noproxy.com", settings.proxy_bypass_list()[2]);
     51   EXPECT_EQ("", settings.autoconfig_url());
     52 
     53   // Test that ToProxyConfig() properly translates into a net::ProxyConfig.
     54   net::ProxyConfig config;
     55   EXPECT_TRUE(settings.ToProxyConfig(&config));
     56 
     57   {
     58     net::ProxyConfig expected_config;
     59     expected_config.proxy_rules().ParseFromString("http=http_proxy:1111; "
     60                                                   "https=ssl_proxy:2222; "
     61                                                   "ftp=ftp_proxy:3333; "
     62                                                   "socks=socks_host:5555");
     63     expected_config.proxy_rules().bypass_rules.ParseFromString(
     64         "*localhost,  127.0.0.1, *noproxy.com");
     65   }
     66 }
     67 
     68 TEST_F(FirefoxProxySettingsTest, TestParseAutoConfigUrl) {
     69   FirefoxProxySettings settings;
     70 
     71   FilePath js_pref_path;
     72   ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &js_pref_path));
     73   js_pref_path = js_pref_path.AppendASCII("firefox3_pref_pac_url.js");
     74 
     75   EXPECT_TRUE(TestFirefoxProxySettings::TestGetSettingsFromFile(js_pref_path,
     76                                                                 &settings));
     77   EXPECT_EQ(FirefoxProxySettings::AUTO_FROM_URL, settings.config_type());
     78 
     79   // Everything should be empty except for the autoconfig URL.
     80   EXPECT_EQ("http://custom-pac-url/", settings.autoconfig_url());
     81   EXPECT_EQ("", settings.http_proxy());
     82   EXPECT_EQ(0, settings.http_proxy_port());
     83   EXPECT_EQ("", settings.ssl_proxy());
     84   EXPECT_EQ(0, settings.ssl_proxy_port());
     85   EXPECT_EQ("", settings.ftp_proxy());
     86   EXPECT_EQ(0, settings.ftp_proxy_port());
     87   EXPECT_EQ("", settings.gopher_proxy());
     88   EXPECT_EQ(0, settings.gopher_proxy_port());
     89   EXPECT_EQ("", settings.socks_host());
     90   EXPECT_EQ(0, settings.socks_port());
     91   EXPECT_EQ(0, settings.socks_port());
     92   EXPECT_EQ(0U, settings.proxy_bypass_list().size());
     93 
     94   // Test that ToProxyConfig() properly translates into a net::ProxyConfig.
     95   net::ProxyConfig config;
     96   EXPECT_TRUE(settings.ToProxyConfig(&config));
     97 
     98   EXPECT_TRUE(config.Equals(net::ProxyConfig::CreateFromCustomPacURL(
     99       GURL("http://custom-pac-url/"))));
    100 }
    101