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";
55 // Mocking out platform specific path to firefox prefs file.
56 class FirefoxPrefsFileSystem : public FakeFileSystem {
58 explicit FirefoxPrefsFileSystem(const std::vector<File>& all_files) :
59 FakeFileSystem(all_files) {
61 virtual FileStream* OpenFile(const Pathname& filename,
62 const std::string& mode) {
63 // TODO: We could have a platform dependent check of paths here.
64 std::string name = filename.basename();
65 name.append(filename.extension());
66 EXPECT_TRUE(name.compare("prefs.js") == 0 ||
67 name.compare("profiles.ini") == 0);
68 FileStream* stream = FakeFileSystem::OpenFile(name, mode);
73 class ProxyDetectTest : public testing::Test {
76 bool GetProxyInfo(const std::string prefs, ProxyInfo* info) {
77 std::vector<talk_base::FakeFileSystem::File> files;
78 files.push_back(talk_base::FakeFileSystem::File("profiles.ini",
79 kFirefoxProfilesIni));
80 files.push_back(talk_base::FakeFileSystem::File("prefs.js", prefs));
81 talk_base::FilesystemScope fs(new talk_base::FirefoxPrefsFileSystem(files));
82 return GetProxySettingsForUrl("Firefox", "www.google.com", info, false);
85 // Verifies that an empty Firefox prefs file results in no proxy detected.
86 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxEmptyPrefs) {
88 EXPECT_TRUE(GetProxyInfo(kFirefoxHeader, &proxy_info));
89 EXPECT_EQ(PROXY_NONE, proxy_info.type);
92 // Verifies that corrupted prefs file results in no proxy detected.
93 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxCorruptedPrefs) {
95 EXPECT_TRUE(GetProxyInfo(kFirefoxCorruptHeader, &proxy_info));
96 EXPECT_EQ(PROXY_NONE, proxy_info.type);
99 // Verifies that SOCKS5 proxy is detected if configured. SOCKS uses a
100 // handshake protocol to inform the proxy software about the
101 // connection that the client is trying to make and may be used for
102 // any form of TCP or UDP socket connection.
103 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxProxySocks) {
104 ProxyInfo proxy_info;
105 SocketAddress proxy_address("proxy.socks.com", 6666);
106 std::string prefs(kFirefoxHeader);
107 prefs.append("user_pref(\"network.proxy.socks\", \"proxy.socks.com\");\n");
108 prefs.append("user_pref(\"network.proxy.socks_port\", 6666);\n");
109 prefs.append("user_pref(\"network.proxy.type\", 1);\n");
111 EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
113 EXPECT_EQ(PROXY_SOCKS5, proxy_info.type);
114 EXPECT_EQ(proxy_address, proxy_info.address);
117 // Verified that SSL proxy is detected if configured. SSL proxy is an
118 // extention of a HTTP proxy to support secure connections.
119 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxProxySsl) {
120 ProxyInfo proxy_info;
121 SocketAddress proxy_address("proxy.ssl.com", 7777);
122 std::string prefs(kFirefoxHeader);
124 prefs.append("user_pref(\"network.proxy.ssl\", \"proxy.ssl.com\");\n");
125 prefs.append("user_pref(\"network.proxy.ssl_port\", 7777);\n");
126 prefs.append("user_pref(\"network.proxy.type\", 1);\n");
128 EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
130 EXPECT_EQ(PROXY_HTTPS, proxy_info.type);
131 EXPECT_EQ(proxy_address, proxy_info.address);
134 // Verifies that a HTTP proxy is detected if configured.
135 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxProxyHttp) {
136 ProxyInfo proxy_info;
137 SocketAddress proxy_address("proxy.http.com", 8888);
138 std::string prefs(kFirefoxHeader);
140 prefs.append("user_pref(\"network.proxy.http\", \"proxy.http.com\");\n");
141 prefs.append("user_pref(\"network.proxy.http_port\", 8888);\n");
142 prefs.append("user_pref(\"network.proxy.type\", 1);\n");
144 EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
146 EXPECT_EQ(PROXY_HTTPS, proxy_info.type);
147 EXPECT_EQ(proxy_address, proxy_info.address);
150 // Verifies detection of automatic proxy detection.
151 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxProxyAuto) {
152 ProxyInfo proxy_info;
153 std::string prefs(kFirefoxHeader);
155 prefs.append("user_pref(\"network.proxy.type\", 4);\n");
157 EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
159 EXPECT_EQ(PROXY_NONE, proxy_info.type);
160 EXPECT_TRUE(proxy_info.autodetect);
161 EXPECT_TRUE(proxy_info.autoconfig_url.empty());
164 // Verifies detection of automatic proxy detection using a static url
166 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxProxyAutoUrl) {
167 ProxyInfo proxy_info;
168 std::string prefs(kFirefoxHeader);
171 "user_pref(\"network.proxy.autoconfig_url\", \"http://a/b.pac\");\n");
172 prefs.append("user_pref(\"network.proxy.type\", 2);\n");
174 EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
176 EXPECT_FALSE(proxy_info.autodetect);
177 EXPECT_EQ(PROXY_NONE, proxy_info.type);
178 EXPECT_EQ(0, proxy_info.autoconfig_url.compare("http://a/b.pac"));
181 } // namespace talk_base