1 /*
2  *  Copyright 2010 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include <string>
12 
13 #include "webrtc/base/fileutils_mock.h"
14 #include "webrtc/base/proxydetect.h"
15 
16 namespace rtc {
17 
18 static const std::string kFirefoxProfilesIni =
19   "[Profile0]\n"
20   "Name=default\n"
21   "IsRelative=1\n"
22   "Path=Profiles/2de53ejb.default\n"
23   "Default=1\n";
24 
25 static const std::string kFirefoxHeader =
26   "# Mozilla User Preferences\n"
27   "\n"
28   "/* Some Comments\n"
29   "*\n"
30   "*/\n"
31   "\n";
32 
33 static const std::string kFirefoxCorruptHeader =
34   "iuahueqe32164";
35 
36 static const std::string kProxyAddress = "proxy.net.com";
37 
38 // Mocking out platform specific path to firefox prefs file.
39 class FirefoxPrefsFileSystem : public FakeFileSystem {
40  public:
FirefoxPrefsFileSystem(const std::vector<File> & all_files)41   explicit FirefoxPrefsFileSystem(const std::vector<File>& all_files) :
42       FakeFileSystem(all_files) {
43   }
OpenFile(const Pathname & filename,const std::string & mode)44   virtual FileStream* OpenFile(const Pathname& filename,
45                                const std::string& mode) {
46     // TODO: We could have a platform dependent check of paths here.
47     std::string name = filename.basename();
48     name.append(filename.extension());
49     EXPECT_TRUE(name.compare("prefs.js") == 0 ||
50                 name.compare("profiles.ini") == 0);
51     FileStream* stream = FakeFileSystem::OpenFile(name, mode);
52     return stream;
53   }
54 };
55 
56 class ProxyDetectTest : public testing::Test {
57 };
58 
GetProxyInfo(const std::string prefs,ProxyInfo * info)59 bool GetProxyInfo(const std::string prefs, ProxyInfo* info) {
60   std::vector<rtc::FakeFileSystem::File> files;
61   files.push_back(rtc::FakeFileSystem::File("profiles.ini",
62                                                   kFirefoxProfilesIni));
63   files.push_back(rtc::FakeFileSystem::File("prefs.js", prefs));
64   rtc::FilesystemScope fs(new rtc::FirefoxPrefsFileSystem(files));
65   return GetProxySettingsForUrl("Firefox", "www.google.com", info, false);
66 }
67 
68 // Verifies that an empty Firefox prefs file results in no proxy detected.
TEST_F(ProxyDetectTest,DISABLED_TestFirefoxEmptyPrefs)69 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxEmptyPrefs) {
70   ProxyInfo proxy_info;
71   EXPECT_TRUE(GetProxyInfo(kFirefoxHeader, &proxy_info));
72   EXPECT_EQ(PROXY_NONE, proxy_info.type);
73 }
74 
75 // Verifies that corrupted prefs file results in no proxy detected.
TEST_F(ProxyDetectTest,DISABLED_TestFirefoxCorruptedPrefs)76 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxCorruptedPrefs) {
77   ProxyInfo proxy_info;
78   EXPECT_TRUE(GetProxyInfo(kFirefoxCorruptHeader, &proxy_info));
79   EXPECT_EQ(PROXY_NONE, proxy_info.type);
80 }
81 
82 // Verifies that SOCKS5 proxy is detected if configured. SOCKS uses a
83 // handshake protocol to inform the proxy software about the
84 // connection that the client is trying to make and may be used for
85 // any form of TCP or UDP socket connection.
TEST_F(ProxyDetectTest,DISABLED_TestFirefoxProxySocks)86 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxProxySocks) {
87   ProxyInfo proxy_info;
88   SocketAddress proxy_address("proxy.socks.com", 6666);
89   std::string prefs(kFirefoxHeader);
90   prefs.append("user_pref(\"network.proxy.socks\", \"proxy.socks.com\");\n");
91   prefs.append("user_pref(\"network.proxy.socks_port\", 6666);\n");
92   prefs.append("user_pref(\"network.proxy.type\", 1);\n");
93 
94   EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
95 
96   EXPECT_EQ(PROXY_SOCKS5, proxy_info.type);
97   EXPECT_EQ(proxy_address, proxy_info.address);
98 }
99 
100 // Verified that SSL proxy is detected if configured. SSL proxy is an
101 // extention of a HTTP proxy to support secure connections.
TEST_F(ProxyDetectTest,DISABLED_TestFirefoxProxySsl)102 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxProxySsl) {
103   ProxyInfo proxy_info;
104   SocketAddress proxy_address("proxy.ssl.com", 7777);
105   std::string prefs(kFirefoxHeader);
106 
107   prefs.append("user_pref(\"network.proxy.ssl\", \"proxy.ssl.com\");\n");
108   prefs.append("user_pref(\"network.proxy.ssl_port\", 7777);\n");
109   prefs.append("user_pref(\"network.proxy.type\", 1);\n");
110 
111   EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
112 
113   EXPECT_EQ(PROXY_HTTPS, proxy_info.type);
114   EXPECT_EQ(proxy_address, proxy_info.address);
115 }
116 
117 // Verifies that a HTTP proxy is detected if configured.
TEST_F(ProxyDetectTest,DISABLED_TestFirefoxProxyHttp)118 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxProxyHttp) {
119   ProxyInfo proxy_info;
120   SocketAddress proxy_address("proxy.http.com", 8888);
121   std::string prefs(kFirefoxHeader);
122 
123   prefs.append("user_pref(\"network.proxy.http\", \"proxy.http.com\");\n");
124   prefs.append("user_pref(\"network.proxy.http_port\", 8888);\n");
125   prefs.append("user_pref(\"network.proxy.type\", 1);\n");
126 
127   EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
128 
129   EXPECT_EQ(PROXY_HTTPS, proxy_info.type);
130   EXPECT_EQ(proxy_address, proxy_info.address);
131 }
132 
133 // Verifies detection of automatic proxy detection.
TEST_F(ProxyDetectTest,DISABLED_TestFirefoxProxyAuto)134 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxProxyAuto) {
135   ProxyInfo proxy_info;
136   std::string prefs(kFirefoxHeader);
137 
138   prefs.append("user_pref(\"network.proxy.type\", 4);\n");
139 
140   EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
141 
142   EXPECT_EQ(PROXY_NONE, proxy_info.type);
143   EXPECT_TRUE(proxy_info.autodetect);
144   EXPECT_TRUE(proxy_info.autoconfig_url.empty());
145 }
146 
147 // Verifies detection of automatic proxy detection using a static url
148 // to config file.
TEST_F(ProxyDetectTest,DISABLED_TestFirefoxProxyAutoUrl)149 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxProxyAutoUrl) {
150   ProxyInfo proxy_info;
151   std::string prefs(kFirefoxHeader);
152 
153   prefs.append(
154       "user_pref(\"network.proxy.autoconfig_url\", \"http://a/b.pac\");\n");
155   prefs.append("user_pref(\"network.proxy.type\", 2);\n");
156 
157   EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
158 
159   EXPECT_FALSE(proxy_info.autodetect);
160   EXPECT_EQ(PROXY_NONE, proxy_info.type);
161   EXPECT_EQ(0, proxy_info.autoconfig_url.compare("http://a/b.pac"));
162 }
163 
164 }  // namespace rtc
165