3 * Copyright 2010, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "talk/base/fileutils_mock.h"
31 #include "talk/base/proxydetect.h"
35 static const std::string kFirefoxProfilesIni =
39 "Path=Profiles/2de53ejb.default\n"
42 static const std::string kFirefoxHeader =
43 "# Mozilla User Preferences\n"
50 static const std::string kFirefoxCorruptHeader =
53 static const std::string kProxyAddress = "proxy.net.com";
54 static const int kProxyPort = 9999;
56 // Mocking out platform specific path to firefox prefs file.
57 class FirefoxPrefsFileSystem : public FakeFileSystem {
59 explicit FirefoxPrefsFileSystem(const std::vector<File>& all_files) :
60 FakeFileSystem(all_files) {
62 virtual FileStream* OpenFile(const Pathname& filename,
63 const std::string& mode) {
64 // TODO: We could have a platform dependent check of paths here.
65 std::string name = filename.basename();
66 name.append(filename.extension());
67 EXPECT_TRUE(name.compare("prefs.js") == 0 ||
68 name.compare("profiles.ini") == 0);
69 FileStream* stream = FakeFileSystem::OpenFile(name, mode);
74 class ProxyDetectTest : public testing::Test {
77 bool GetProxyInfo(const std::string prefs, ProxyInfo* info) {
78 std::vector<talk_base::FakeFileSystem::File> files;
79 files.push_back(talk_base::FakeFileSystem::File("profiles.ini",
80 kFirefoxProfilesIni));
81 files.push_back(talk_base::FakeFileSystem::File("prefs.js", prefs));
82 talk_base::FilesystemScope fs(new talk_base::FirefoxPrefsFileSystem(files));
83 return GetProxySettingsForUrl("Firefox", "www.google.com", info, false);
86 // Verifies that an empty Firefox prefs file results in no proxy detected.
87 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxEmptyPrefs) {
89 EXPECT_TRUE(GetProxyInfo(kFirefoxHeader, &proxy_info));
90 EXPECT_EQ(PROXY_NONE, proxy_info.type);
93 // Verifies that corrupted prefs file results in no proxy detected.
94 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxCorruptedPrefs) {
96 EXPECT_TRUE(GetProxyInfo(kFirefoxCorruptHeader, &proxy_info));
97 EXPECT_EQ(PROXY_NONE, proxy_info.type);
100 // Verifies that SOCKS5 proxy is detected if configured. SOCKS uses a
101 // handshake protocol to inform the proxy software about the
102 // connection that the client is trying to make and may be used for
103 // any form of TCP or UDP socket connection.
104 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxProxySocks) {
105 ProxyInfo proxy_info;
106 SocketAddress proxy_address("proxy.socks.com", 6666);
107 std::string prefs(kFirefoxHeader);
108 prefs.append("user_pref(\"network.proxy.socks\", \"proxy.socks.com\");\n");
109 prefs.append("user_pref(\"network.proxy.socks_port\", 6666);\n");
110 prefs.append("user_pref(\"network.proxy.type\", 1);\n");
112 EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
114 EXPECT_EQ(PROXY_SOCKS5, proxy_info.type);
115 EXPECT_EQ(proxy_address, proxy_info.address);
118 // Verified that SSL proxy is detected if configured. SSL proxy is an
119 // extention of a HTTP proxy to support secure connections.
120 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxProxySsl) {
121 ProxyInfo proxy_info;
122 SocketAddress proxy_address("proxy.ssl.com", 7777);
123 std::string prefs(kFirefoxHeader);
125 prefs.append("user_pref(\"network.proxy.ssl\", \"proxy.ssl.com\");\n");
126 prefs.append("user_pref(\"network.proxy.ssl_port\", 7777);\n");
127 prefs.append("user_pref(\"network.proxy.type\", 1);\n");
129 EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
131 EXPECT_EQ(PROXY_HTTPS, proxy_info.type);
132 EXPECT_EQ(proxy_address, proxy_info.address);
135 // Verifies that a HTTP proxy is detected if configured.
136 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxProxyHttp) {
137 ProxyInfo proxy_info;
138 SocketAddress proxy_address("proxy.http.com", 8888);
139 std::string prefs(kFirefoxHeader);
141 prefs.append("user_pref(\"network.proxy.http\", \"proxy.http.com\");\n");
142 prefs.append("user_pref(\"network.proxy.http_port\", 8888);\n");
143 prefs.append("user_pref(\"network.proxy.type\", 1);\n");
145 EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
147 EXPECT_EQ(PROXY_HTTPS, proxy_info.type);
148 EXPECT_EQ(proxy_address, proxy_info.address);
151 // Verifies detection of automatic proxy detection.
152 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxProxyAuto) {
153 ProxyInfo proxy_info;
154 std::string prefs(kFirefoxHeader);
156 prefs.append("user_pref(\"network.proxy.type\", 4);\n");
158 EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
160 EXPECT_EQ(PROXY_NONE, proxy_info.type);
161 EXPECT_TRUE(proxy_info.autodetect);
162 EXPECT_TRUE(proxy_info.autoconfig_url.empty());
165 // Verifies detection of automatic proxy detection using a static url
167 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxProxyAutoUrl) {
168 ProxyInfo proxy_info;
169 std::string prefs(kFirefoxHeader);
172 "user_pref(\"network.proxy.autoconfig_url\", \"http://a/b.pac\");\n");
173 prefs.append("user_pref(\"network.proxy.type\", 2);\n");
175 EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
177 EXPECT_FALSE(proxy_info.autodetect);
178 EXPECT_EQ(PROXY_NONE, proxy_info.type);
179 EXPECT_EQ(0, proxy_info.autoconfig_url.compare("http://a/b.pac"));
182 } // namespace talk_base