1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "googleurl/src/gurl.h"
#include "net/base/net_errors.h"
#include "net/http/http_proxy_service.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class MockProxyResolver : public net::HttpProxyResolver {
public:
MockProxyResolver() : fail_get_proxy_for_url(false) {
}
virtual int GetProxyConfig(net::HttpProxyConfig* results) {
*results = config;
return net::OK;
}
virtual int GetProxyForURL(const std::wstring& query_url,
const std::wstring& pac_url,
net::HttpProxyInfo* results) {
if (pac_url != config.pac_url)
return net::ERR_INVALID_ARGUMENT;
if (fail_get_proxy_for_url)
return net::ERR_FAILED;
if (GURL(query_url).host() == info_predicate_query_host) {
results->Use(info);
} else {
results->UseDirect();
}
return net::OK;
}
net::HttpProxyConfig config;
net::HttpProxyInfo info;
// info is only returned if query_url in GetProxyForURL matches this:
std::string info_predicate_query_host;
// If true, then GetProxyForURL will fail, which simulates failure to
// download or execute the PAC file.
bool fail_get_proxy_for_url;
};
} // namespace
TEST(HttpProxyServiceTest, Direct) {
MockProxyResolver resolver;
net::HttpProxyService service(&resolver);
GURL url("http://www.google.com/");
net::HttpProxyInfo info;
int rv = service.ResolveProxy(url, &info, NULL, NULL);
EXPECT_EQ(rv, net::OK);
EXPECT_TRUE(info.is_direct());
}
TEST(HttpProxyServiceTest, PAC) {
MockProxyResolver resolver;
resolver.config.pac_url = L"http://foopy/proxy.pac";
resolver.info.UseNamedProxy(L"foopy");
resolver.info_predicate_query_host = "www.google.com";
net::HttpProxyService service(&resolver);
GURL url("http://www.google.com/");
net::HttpProxyInfo info;
int rv = service.ResolveProxy(url, &info, NULL, NULL);
EXPECT_EQ(rv, net::OK);
EXPECT_FALSE(info.is_direct());
EXPECT_EQ(info.proxy_server(), L"foopy");
}
TEST(HttpProxyServiceTest, PAC_FailoverToDirect) {
MockProxyResolver resolver;
resolver.config.pac_url = L"http://foopy/proxy.pac";
resolver.info.UseNamedProxy(L"foopy:8080");
resolver.info_predicate_query_host = "www.google.com";
net::HttpProxyService service(&resolver);
GURL url("http://www.google.com/");
net::HttpProxyInfo info;
int rv = service.ResolveProxy(url, &info, NULL, NULL);
EXPECT_EQ(rv, net::OK);
EXPECT_FALSE(info.is_direct());
EXPECT_EQ(info.proxy_server(), L"foopy:8080");
// Now, imagine that connecting to foopy:8080 fails.
rv = service.ReconsiderProxyAfterError(url, &info, NULL, NULL);
EXPECT_EQ(rv, net::OK);
EXPECT_TRUE(info.is_direct());
}
TEST(HttpProxyServiceTest, PAC_FailsToDownload) {
// Test what happens when we fail to download the PAC URL.
MockProxyResolver resolver;
resolver.config.pac_url = L"http://foopy/proxy.pac";
resolver.info.UseNamedProxy(L"foopy:8080");
resolver.info_predicate_query_host = "www.google.com";
resolver.fail_get_proxy_for_url = true;
net::HttpProxyService service(&resolver);
GURL url("http://www.google.com/");
net::HttpProxyInfo info;
int rv = service.ResolveProxy(url, &info, NULL, NULL);
EXPECT_EQ(rv, net::OK);
EXPECT_TRUE(info.is_direct());
rv = service.ResolveProxy(url, &info, NULL, NULL);
EXPECT_EQ(rv, net::OK);
EXPECT_TRUE(info.is_direct());
resolver.fail_get_proxy_for_url = false;
resolver.info.UseNamedProxy(L"foopy_valid:8080");
// But, if that fails, then we should give the proxy config another shot
// since we have never tried it with this URL before.
rv = service.ReconsiderProxyAfterError(url, &info, NULL, NULL);
EXPECT_EQ(rv, net::OK);
EXPECT_FALSE(info.is_direct());
EXPECT_EQ(info.proxy_server(), L"foopy_valid:8080");
}
TEST(HttpProxyServiceTest, ProxyFallback) {
// Test what happens when we specify multiple proxy servers and some of them
// are bad.
MockProxyResolver resolver;
resolver.config.pac_url = L"http://foopy/proxy.pac";
resolver.info.UseNamedProxy(L"foopy1:8080;foopy2:9090");
resolver.info_predicate_query_host = "www.google.com";
resolver.fail_get_proxy_for_url = false;
net::HttpProxyService service(&resolver);
GURL url("http://www.google.com/");
// Get the proxy information.
net::HttpProxyInfo info;
int rv = service.ResolveProxy(url, &info, NULL, NULL);
EXPECT_EQ(rv, net::OK);
EXPECT_FALSE(info.is_direct());
// The first item is valid.
EXPECT_EQ(info.proxy_server(), L"foopy1:8080");
// Fake an error on the proxy.
rv = service.ReconsiderProxyAfterError(url, &info, NULL, NULL);
EXPECT_EQ(rv, net::OK);
// The second proxy should be specified.
EXPECT_EQ(info.proxy_server(), L"foopy2:9090");
// Create a new resolver that returns 3 proxies. The second one is already
// known to be bad.
resolver.config.pac_url = L"http://foopy/proxy.pac";
resolver.info.UseNamedProxy(L"foopy3:7070;foopy1:8080;foopy2:9090");
resolver.info_predicate_query_host = "www.google.com";
resolver.fail_get_proxy_for_url = false;
rv = service.ResolveProxy(url, &info, NULL, NULL);
EXPECT_EQ(rv, net::OK);
EXPECT_FALSE(info.is_direct());
EXPECT_EQ(info.proxy_server(), L"foopy3:7070");
// We fake another error. It should now try the third one.
rv = service.ReconsiderProxyAfterError(url, &info, NULL, NULL);
EXPECT_EQ(rv, net::OK);
EXPECT_EQ(info.proxy_server(), L"foopy2:9090");
// Fake another error, the last proxy is gone, the list should now be empty.
rv = service.ReconsiderProxyAfterError(url, &info, NULL, NULL);
EXPECT_EQ(rv, net::OK); // We try direct.
EXPECT_TRUE(info.is_direct());
// If it fails again, we don't have anything else to try.
rv = service.ReconsiderProxyAfterError(url, &info, NULL, NULL);
EXPECT_EQ(rv, net::ERR_FAILED); // We try direct.
// TODO(nsylvain): Test that the proxy can be retried after the delay.
}
TEST(HttpProxyServiceTest, ProxyBypassList) {
// Test what happens when a proxy bypass list is specified.
MockProxyResolver resolver;
resolver.config.proxy_server = L"foopy1:8080;foopy2:9090";
resolver.config.auto_detect = false;
resolver.config.proxy_bypass = L"<local>";
net::HttpProxyService service(&resolver);
GURL url("http://www.google.com/");
// Get the proxy information.
net::HttpProxyInfo info;
int rv = service.ResolveProxy(url, &info, NULL, NULL);
EXPECT_EQ(rv, net::OK);
EXPECT_FALSE(info.is_direct());
net::HttpProxyService service1(&resolver);
GURL test_url1("local");
net::HttpProxyInfo info1;
rv = service1.ResolveProxy(test_url1, &info1, NULL, NULL);
EXPECT_EQ(rv, net::OK);
EXPECT_TRUE(info1.is_direct());
resolver.config.proxy_bypass = L"<local>;*.org";
net::HttpProxyService service2(&resolver);
GURL test_url2("http://www.webkit.org");
net::HttpProxyInfo info2;
rv = service2.ResolveProxy(test_url2, &info2, NULL, NULL);
EXPECT_EQ(rv, net::OK);
EXPECT_TRUE(info2.is_direct());
resolver.config.proxy_bypass = L"<local>;*.org;7*";
net::HttpProxyService service3(&resolver);
GURL test_url3("http://74.125.19.147");
net::HttpProxyInfo info3;
rv = service3.ResolveProxy(test_url3, &info3, NULL, NULL);
EXPECT_EQ(rv, net::OK);
EXPECT_TRUE(info3.is_direct());
resolver.config.proxy_bypass = L"<local>;*.org;";
net::HttpProxyService service4(&resolver);
GURL test_url4("http://www.msn.com");
net::HttpProxyInfo info4;
rv = service4.ResolveProxy(test_url4, &info4, NULL, NULL);
EXPECT_EQ(rv, net::OK);
EXPECT_FALSE(info4.is_direct());
}
TEST(HttpProxyServiceTest, PerProtocolProxyTests) {
MockProxyResolver resolver;
resolver.config.proxy_server = L"http=foopy1:8080;https=foopy2:8080";
resolver.config.auto_detect = false;
net::HttpProxyService service1(&resolver);
GURL test_url1("http://www.msn.com");
net::HttpProxyInfo info1;
int rv = service1.ResolveProxy(test_url1, &info1, NULL, NULL);
EXPECT_EQ(rv, net::OK);
EXPECT_FALSE(info1.is_direct());
EXPECT_TRUE(info1.proxy_server() == L"foopy1:8080");
net::HttpProxyService service2(&resolver);
GURL test_url2("ftp://ftp.google.com");
net::HttpProxyInfo info2;
rv = service2.ResolveProxy(test_url2, &info2, NULL, NULL);
EXPECT_EQ(rv, net::OK);
EXPECT_TRUE(info2.is_direct());
EXPECT_TRUE(info2.proxy_server() == L"");
net::HttpProxyService service3(&resolver);
GURL test_url3("https://webbranch.techcu.com");
net::HttpProxyInfo info3;
rv = service3.ResolveProxy(test_url3, &info3, NULL, NULL);
EXPECT_EQ(rv, net::OK);
EXPECT_FALSE(info3.is_direct());
EXPECT_TRUE(info3.proxy_server() == L"foopy2:8080");
resolver.config.proxy_server = L"foopy1:8080";
net::HttpProxyService service4(&resolver);
GURL test_url4("www.microsoft.com");
net::HttpProxyInfo info4;
rv = service4.ResolveProxy(test_url4, &info4, NULL, NULL);
EXPECT_EQ(rv, net::OK);
EXPECT_FALSE(info4.is_direct());
EXPECT_TRUE(info4.proxy_server() == L"foopy1:8080");
}
|