1/*
2 * libjingle
3 * Copyright 2010, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
15 *
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.
26 */
27
28#include <string>
29
30#include "talk/base/fileutils_mock.h"
31#include "talk/base/proxydetect.h"
32
33namespace talk_base {
34
35static const std::string kFirefoxProfilesIni =
36  "[Profile0]\n"
37  "Name=default\n"
38  "IsRelative=1\n"
39  "Path=Profiles/2de53ejb.default\n"
40  "Default=1\n";
41
42static const std::string kFirefoxHeader =
43  "# Mozilla User Preferences\n"
44  "\n"
45  "/* Some Comments\n"
46  "*\n"
47  "*/\n"
48  "\n";
49
50static const std::string kFirefoxCorruptHeader =
51  "iuahueqe32164";
52
53static const std::string kProxyAddress = "proxy.net.com";
54static const int kProxyPort = 9999;
55
56// Mocking out platform specific path to firefox prefs file.
57class FirefoxPrefsFileSystem : public FakeFileSystem {
58 public:
59  explicit FirefoxPrefsFileSystem(const std::vector<File>& all_files) :
60      FakeFileSystem(all_files) {
61  }
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);
70    return stream;
71  }
72};
73
74class ProxyDetectTest : public testing::Test {
75};
76
77bool 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);
84}
85
86// Verifies that an empty Firefox prefs file results in no proxy detected.
87TEST_F(ProxyDetectTest, DISABLED_TestFirefoxEmptyPrefs) {
88  ProxyInfo proxy_info;
89  EXPECT_TRUE(GetProxyInfo(kFirefoxHeader, &proxy_info));
90  EXPECT_EQ(PROXY_NONE, proxy_info.type);
91}
92
93// Verifies that corrupted prefs file results in no proxy detected.
94TEST_F(ProxyDetectTest, DISABLED_TestFirefoxCorruptedPrefs) {
95  ProxyInfo proxy_info;
96  EXPECT_TRUE(GetProxyInfo(kFirefoxCorruptHeader, &proxy_info));
97  EXPECT_EQ(PROXY_NONE, proxy_info.type);
98}
99
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.
104TEST_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");
111
112  EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
113
114  EXPECT_EQ(PROXY_SOCKS5, proxy_info.type);
115  EXPECT_EQ(proxy_address, proxy_info.address);
116}
117
118// Verified that SSL proxy is detected if configured. SSL proxy is an
119// extention of a HTTP proxy to support secure connections.
120TEST_F(ProxyDetectTest, DISABLED_TestFirefoxProxySsl) {
121  ProxyInfo proxy_info;
122  SocketAddress proxy_address("proxy.ssl.com", 7777);
123  std::string prefs(kFirefoxHeader);
124
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");
128
129  EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
130
131  EXPECT_EQ(PROXY_HTTPS, proxy_info.type);
132  EXPECT_EQ(proxy_address, proxy_info.address);
133}
134
135// Verifies that a HTTP proxy is detected if configured.
136TEST_F(ProxyDetectTest, DISABLED_TestFirefoxProxyHttp) {
137  ProxyInfo proxy_info;
138  SocketAddress proxy_address("proxy.http.com", 8888);
139  std::string prefs(kFirefoxHeader);
140
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");
144
145  EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
146
147  EXPECT_EQ(PROXY_HTTPS, proxy_info.type);
148  EXPECT_EQ(proxy_address, proxy_info.address);
149}
150
151// Verifies detection of automatic proxy detection.
152TEST_F(ProxyDetectTest, DISABLED_TestFirefoxProxyAuto) {
153  ProxyInfo proxy_info;
154  std::string prefs(kFirefoxHeader);
155
156  prefs.append("user_pref(\"network.proxy.type\", 4);\n");
157
158  EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
159
160  EXPECT_EQ(PROXY_NONE, proxy_info.type);
161  EXPECT_TRUE(proxy_info.autodetect);
162  EXPECT_TRUE(proxy_info.autoconfig_url.empty());
163}
164
165// Verifies detection of automatic proxy detection using a static url
166// to config file.
167TEST_F(ProxyDetectTest, DISABLED_TestFirefoxProxyAutoUrl) {
168  ProxyInfo proxy_info;
169  std::string prefs(kFirefoxHeader);
170
171  prefs.append(
172      "user_pref(\"network.proxy.autoconfig_url\", \"http://a/b.pac\");\n");
173  prefs.append("user_pref(\"network.proxy.type\", 2);\n");
174
175  EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
176
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"));
180}
181
182}  // namespace talk_base
183