Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / base / proxydetect_unittest.cc
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
33 namespace talk_base {
34
35 static 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
42 static const std::string kFirefoxHeader =
43   "# Mozilla User Preferences\n"
44   "\n"
45   "/* Some Comments\n"
46   "*\n"
47   "*/\n"
48   "\n";
49
50 static const std::string kFirefoxCorruptHeader =
51   "iuahueqe32164";
52
53 static const std::string kProxyAddress = "proxy.net.com";
54
55 // Mocking out platform specific path to firefox prefs file.
56 class FirefoxPrefsFileSystem : public FakeFileSystem {
57  public:
58   explicit FirefoxPrefsFileSystem(const std::vector<File>& all_files) :
59       FakeFileSystem(all_files) {
60   }
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);
69     return stream;
70   }
71 };
72
73 class ProxyDetectTest : public testing::Test {
74 };
75
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);
83 }
84
85 // Verifies that an empty Firefox prefs file results in no proxy detected.
86 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxEmptyPrefs) {
87   ProxyInfo proxy_info;
88   EXPECT_TRUE(GetProxyInfo(kFirefoxHeader, &proxy_info));
89   EXPECT_EQ(PROXY_NONE, proxy_info.type);
90 }
91
92 // Verifies that corrupted prefs file results in no proxy detected.
93 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxCorruptedPrefs) {
94   ProxyInfo proxy_info;
95   EXPECT_TRUE(GetProxyInfo(kFirefoxCorruptHeader, &proxy_info));
96   EXPECT_EQ(PROXY_NONE, proxy_info.type);
97 }
98
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");
110
111   EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
112
113   EXPECT_EQ(PROXY_SOCKS5, proxy_info.type);
114   EXPECT_EQ(proxy_address, proxy_info.address);
115 }
116
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);
123
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");
127
128   EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
129
130   EXPECT_EQ(PROXY_HTTPS, proxy_info.type);
131   EXPECT_EQ(proxy_address, proxy_info.address);
132 }
133
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);
139
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");
143
144   EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
145
146   EXPECT_EQ(PROXY_HTTPS, proxy_info.type);
147   EXPECT_EQ(proxy_address, proxy_info.address);
148 }
149
150 // Verifies detection of automatic proxy detection.
151 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxProxyAuto) {
152   ProxyInfo proxy_info;
153   std::string prefs(kFirefoxHeader);
154
155   prefs.append("user_pref(\"network.proxy.type\", 4);\n");
156
157   EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
158
159   EXPECT_EQ(PROXY_NONE, proxy_info.type);
160   EXPECT_TRUE(proxy_info.autodetect);
161   EXPECT_TRUE(proxy_info.autoconfig_url.empty());
162 }
163
164 // Verifies detection of automatic proxy detection using a static url
165 // to config file.
166 TEST_F(ProxyDetectTest, DISABLED_TestFirefoxProxyAutoUrl) {
167   ProxyInfo proxy_info;
168   std::string prefs(kFirefoxHeader);
169
170   prefs.append(
171       "user_pref(\"network.proxy.autoconfig_url\", \"http://a/b.pac\");\n");
172   prefs.append("user_pref(\"network.proxy.type\", 2);\n");
173
174   EXPECT_TRUE(GetProxyInfo(prefs, &proxy_info));
175
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"));
179 }
180
181 }  // namespace talk_base