1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/proxy/proxy_list.h"
7 #include "net/base/net_errors.h"
8 #include "net/base/net_log.h"
9 #include "net/proxy/proxy_retry_info.h"
10 #include "net/proxy/proxy_server.h"
11 #include "testing/gtest/include/gtest/gtest.h"
17 // Test parsing from a PAC string.
18 TEST(ProxyListTest
, SetFromPacString
) {
20 const char* pac_input
;
21 const char* pac_output
;
27 { " DIRECT", // leading space.
30 { "PROXY foopy1 ; proxy foopy2;\t DIRECT",
31 "PROXY foopy1:80;PROXY foopy2:80;DIRECT",
33 { "proxy foopy1 ; SOCKS foopy2",
34 "PROXY foopy1:80;SOCKS foopy2:1080",
36 // Try putting DIRECT first.
37 { "DIRECT ; proxy foopy1 ; DIRECT ; SOCKS5 foopy2;DIRECT ",
38 "DIRECT;PROXY foopy1:80;DIRECT;SOCKS5 foopy2:1080;DIRECT",
40 // Try putting DIRECT consecutively.
41 { "DIRECT ; proxy foopy1:80; DIRECT ; DIRECT",
42 "DIRECT;PROXY foopy1:80;DIRECT;DIRECT",
45 // Invalid inputs (parts which aren't understood get
46 // silently discarded):
48 // If the proxy list string parsed to empty, automatically fall-back to
56 { "PROXY foopy1 ; JUNK ; JUNK ; SOCKS5 foopy2 ; ;",
57 "PROXY foopy1:80;SOCKS5 foopy2:1080",
61 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
63 list
.SetFromPacString(tests
[i
].pac_input
);
64 EXPECT_EQ(tests
[i
].pac_output
, list
.ToPacString());
65 EXPECT_FALSE(list
.IsEmpty());
69 TEST(ProxyListTest
, RemoveProxiesWithoutScheme
) {
71 const char* pac_input
;
73 const char* filtered_pac_output
;
75 { "PROXY foopy:10 ; SOCKS5 foopy2 ; SOCKS foopy11 ; PROXY foopy3 ; DIRECT",
76 // Remove anything that isn't HTTP or DIRECT.
77 ProxyServer::SCHEME_DIRECT
| ProxyServer::SCHEME_HTTP
,
78 "PROXY foopy:10;PROXY foopy3:80;DIRECT",
80 { "PROXY foopy:10 ; SOCKS5 foopy2",
81 // Remove anything that isn't HTTP or SOCKS5.
82 ProxyServer::SCHEME_DIRECT
| ProxyServer::SCHEME_SOCKS4
,
87 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
89 list
.SetFromPacString(tests
[i
].pac_input
);
90 list
.RemoveProxiesWithoutScheme(tests
[i
].filter
);
91 EXPECT_EQ(tests
[i
].filtered_pac_output
, list
.ToPacString());
95 TEST(ProxyListTest
, DeprioritizeBadProxies
) {
96 // Retry info that marks a proxy as being bad for a *very* long time (to avoid
97 // the test depending on the current time.)
98 ProxyRetryInfo proxy_retry_info
;
99 proxy_retry_info
.bad_until
=
100 base::TimeTicks::Now() + base::TimeDelta::FromDays(1);
102 // Call DeprioritizeBadProxies with an empty map -- should have no effect.
105 list
.SetFromPacString("PROXY foopy1:80;PROXY foopy2:80;PROXY foopy3:80");
107 ProxyRetryInfoMap retry_info_map
;
108 list
.DeprioritizeBadProxies(retry_info_map
);
109 EXPECT_EQ("PROXY foopy1:80;PROXY foopy2:80;PROXY foopy3:80",
113 // Call DeprioritizeBadProxies with 2 of the three proxies marked as bad.
114 // These proxies should be retried last.
117 list
.SetFromPacString("PROXY foopy1:80;PROXY foopy2:80;PROXY foopy3:80");
119 ProxyRetryInfoMap retry_info_map
;
120 retry_info_map
["foopy1:80"] = proxy_retry_info
;
121 retry_info_map
["foopy3:80"] = proxy_retry_info
;
122 retry_info_map
["socks5://localhost:1080"] = proxy_retry_info
;
124 list
.DeprioritizeBadProxies(retry_info_map
);
126 EXPECT_EQ("PROXY foopy2:80;PROXY foopy1:80;PROXY foopy3:80",
130 // Call DeprioritizeBadProxies where ALL of the proxies are marked as bad.
131 // This should have no effect on the order.
134 list
.SetFromPacString("PROXY foopy1:80;PROXY foopy2:80;PROXY foopy3:80");
136 ProxyRetryInfoMap retry_info_map
;
137 retry_info_map
["foopy1:80"] = proxy_retry_info
;
138 retry_info_map
["foopy2:80"] = proxy_retry_info
;
139 retry_info_map
["foopy3:80"] = proxy_retry_info
;
141 list
.DeprioritizeBadProxies(retry_info_map
);
143 EXPECT_EQ("PROXY foopy1:80;PROXY foopy2:80;PROXY foopy3:80",
147 // Call DeprioritizeBadProxies with 2 of the three proxies marked as bad. Of
148 // the 2 bad proxies, one is to be reconsidered and should be retried last.
149 // The other is not to be reconsidered and should be removed from the list.
152 list
.SetFromPacString("PROXY foopy1:80;PROXY foopy2:80;PROXY foopy3:80");
154 ProxyRetryInfoMap retry_info_map
;
155 // |proxy_retry_info.reconsider defaults to true.
156 retry_info_map
["foopy1:80"] = proxy_retry_info
;
157 proxy_retry_info
.try_while_bad
= false;
158 retry_info_map
["foopy3:80"] = proxy_retry_info
;
159 proxy_retry_info
.try_while_bad
= true;
160 retry_info_map
["socks5://localhost:1080"] = proxy_retry_info
;
162 list
.DeprioritizeBadProxies(retry_info_map
);
164 EXPECT_EQ("PROXY foopy2:80;PROXY foopy1:80",
169 TEST(ProxyListTest
, UpdateRetryInfoOnFallback
) {
170 ProxyRetryInfo proxy_retry_info
;
171 // Retrying should put the first proxy on the retry list.
174 ProxyRetryInfoMap retry_info_map
;
176 ProxyServer
proxy_server(
177 ProxyServer::FromURI("foopy1:80", ProxyServer::SCHEME_HTTP
));
178 list
.SetFromPacString("PROXY foopy1:80;PROXY foopy2:80;PROXY foopy3:80");
179 list
.UpdateRetryInfoOnFallback(&retry_info_map
,
180 base::TimeDelta::FromSeconds(60),
183 ERR_PROXY_CONNECTION_FAILED
,
185 EXPECT_TRUE(retry_info_map
.end() != retry_info_map
.find("foopy1:80"));
186 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED
,
187 retry_info_map
[proxy_server
.ToURI()].net_error
);
188 EXPECT_TRUE(retry_info_map
.end() == retry_info_map
.find("foopy2:80"));
189 EXPECT_TRUE(retry_info_map
.end() == retry_info_map
.find("foopy3:80"));
191 // Retrying should put the first proxy on the retry list, even if there
192 // was no network error.
195 ProxyRetryInfoMap retry_info_map
;
197 ProxyServer
proxy_server(
198 ProxyServer::FromURI("foopy1:80", ProxyServer::SCHEME_HTTP
));
199 list
.SetFromPacString("PROXY foopy1:80;PROXY foopy2:80;PROXY foopy3:80");
200 list
.UpdateRetryInfoOnFallback(&retry_info_map
,
201 base::TimeDelta::FromSeconds(60),
206 EXPECT_TRUE(retry_info_map
.end() != retry_info_map
.find("foopy1:80"));
207 EXPECT_EQ(OK
, retry_info_map
[proxy_server
.ToURI()].net_error
);
208 EXPECT_TRUE(retry_info_map
.end() == retry_info_map
.find("foopy2:80"));
209 EXPECT_TRUE(retry_info_map
.end() == retry_info_map
.find("foopy3:80"));
211 // Including another bad proxy should put both the first and the specified
212 // proxy on the retry list.
215 ProxyRetryInfoMap retry_info_map
;
217 ProxyServer proxy_server
= ProxyServer::FromURI("foopy3:80",
218 ProxyServer::SCHEME_HTTP
);
219 list
.SetFromPacString("PROXY foopy1:80;PROXY foopy2:80;PROXY foopy3:80");
220 list
.UpdateRetryInfoOnFallback(&retry_info_map
,
221 base::TimeDelta::FromSeconds(60),
224 ERR_NAME_RESOLUTION_FAILED
,
226 EXPECT_TRUE(retry_info_map
.end() != retry_info_map
.find("foopy1:80"));
227 EXPECT_EQ(ERR_NAME_RESOLUTION_FAILED
,
228 retry_info_map
[proxy_server
.ToURI()].net_error
);
229 EXPECT_TRUE(retry_info_map
.end() == retry_info_map
.find("foopy2:80"));
230 EXPECT_TRUE(retry_info_map
.end() != retry_info_map
.find("foopy3:80"));
232 // If the first proxy is DIRECT, nothing is added to the retry list, even
233 // if another bad proxy is specified.
236 ProxyRetryInfoMap retry_info_map
;
238 ProxyServer proxy_server
= ProxyServer::FromURI("foopy2:80",
239 ProxyServer::SCHEME_HTTP
);
240 list
.SetFromPacString("DIRECT;PROXY foopy2:80;PROXY foopy3:80");
241 list
.UpdateRetryInfoOnFallback(&retry_info_map
,
242 base::TimeDelta::FromSeconds(60),
247 EXPECT_TRUE(retry_info_map
.end() == retry_info_map
.find("foopy2:80"));
248 EXPECT_TRUE(retry_info_map
.end() == retry_info_map
.find("foopy3:80"));