1 // Copyright (c) 2012 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.
9 #include "base/compiler_specific.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "net/proxy/proxy_config.h"
13 #include "net/proxy/proxy_config_service_android.h"
14 #include "net/proxy/proxy_info.h"
15 #include "testing/gtest/include/gtest/gtest.h"
21 class TestObserver
: public ProxyConfigService::Observer
{
23 TestObserver() : availability_(ProxyConfigService::CONFIG_UNSET
) {}
25 // ProxyConfigService::Observer:
26 void OnProxyConfigChanged(
27 const ProxyConfig
& config
,
28 ProxyConfigService::ConfigAvailability availability
) override
{
30 availability_
= availability
;
33 ProxyConfigService::ConfigAvailability
availability() const {
37 const ProxyConfig
& config() const {
43 ProxyConfigService::ConfigAvailability availability_
;
48 typedef std::map
<std::string
, std::string
> StringMap
;
50 class ProxyConfigServiceAndroidTestBase
: public testing::Test
{
52 // Note that the current thread's message loop is initialized by the test
53 // suite (see net/test/net_test_suite.cc).
54 ProxyConfigServiceAndroidTestBase(const StringMap
& initial_configuration
)
55 : configuration_(initial_configuration
),
56 message_loop_(base::MessageLoop::current()),
57 service_(message_loop_
->task_runner(),
58 message_loop_
->task_runner(),
59 base::Bind(&ProxyConfigServiceAndroidTestBase::GetProperty
,
60 base::Unretained(this))) {}
62 ~ProxyConfigServiceAndroidTestBase() override
{}
65 void SetUp() override
{
66 message_loop_
->RunUntilIdle();
67 service_
.AddObserver(&observer_
);
70 void TearDown() override
{ service_
.RemoveObserver(&observer_
); }
72 void ClearConfiguration() {
73 configuration_
.clear();
76 void AddProperty(const std::string
& key
, const std::string
& value
) {
77 configuration_
[key
] = value
;
80 std::string
GetProperty(const std::string
& key
) {
81 StringMap::const_iterator it
= configuration_
.find(key
);
82 if (it
== configuration_
.end())
87 void ProxySettingsChanged() {
88 service_
.ProxySettingsChanged();
89 message_loop_
->RunUntilIdle();
92 void TestMapping(const std::string
& url
, const std::string
& expected
) {
93 ProxyConfigService::ConfigAvailability availability
;
94 ProxyConfig proxy_config
;
95 availability
= service_
.GetLatestProxyConfig(&proxy_config
);
96 EXPECT_EQ(ProxyConfigService::CONFIG_VALID
, availability
);
98 proxy_config
.proxy_rules().Apply(GURL(url
), &proxy_info
);
99 EXPECT_EQ(expected
, proxy_info
.ToPacString());
102 StringMap configuration_
;
103 TestObserver observer_
;
104 base::MessageLoop
* const message_loop_
;
105 ProxyConfigServiceAndroid service_
;
108 class ProxyConfigServiceAndroidTest
: public ProxyConfigServiceAndroidTestBase
{
110 ProxyConfigServiceAndroidTest()
111 : ProxyConfigServiceAndroidTestBase(StringMap()) {}
114 class ProxyConfigServiceAndroidWithInitialConfigTest
115 : public ProxyConfigServiceAndroidTestBase
{
117 ProxyConfigServiceAndroidWithInitialConfigTest()
118 : ProxyConfigServiceAndroidTestBase(MakeInitialConfiguration()) {}
121 StringMap
MakeInitialConfiguration() {
122 StringMap initial_configuration
;
123 initial_configuration
["http.proxyHost"] = "httpproxy.com";
124 initial_configuration
["http.proxyPort"] = "8080";
125 return initial_configuration
;
129 TEST_F(ProxyConfigServiceAndroidTest
, TestChangePropertiesNotification
) {
130 // Set up a non-empty configuration
131 AddProperty("http.proxyHost", "localhost");
132 ProxySettingsChanged();
133 EXPECT_EQ(ProxyConfigService::CONFIG_VALID
, observer_
.availability());
134 EXPECT_FALSE(observer_
.config().proxy_rules().empty());
136 // Set up an empty configuration
137 ClearConfiguration();
138 ProxySettingsChanged();
139 EXPECT_EQ(ProxyConfigService::CONFIG_VALID
, observer_
.availability());
140 EXPECT_TRUE(observer_
.config().proxy_rules().empty());
143 TEST_F(ProxyConfigServiceAndroidWithInitialConfigTest
, TestInitialConfig
) {
144 // Make sure that the initial config is set.
145 TestMapping("ftp://example.com/", "DIRECT");
146 TestMapping("http://example.com/", "PROXY httpproxy.com:8080");
148 // Override the initial configuration.
149 ClearConfiguration();
150 AddProperty("http.proxyHost", "httpproxy.com");
151 ProxySettingsChanged();
152 TestMapping("http://example.com/", "PROXY httpproxy.com:80");
155 // !! The following test cases are automatically generated from
156 // !! net/android/tools/proxy_test_cases.py.
157 // !! Please edit that file instead of editing the test cases below and
158 // !! update also the corresponding Java unit tests in
159 // !! AndroidProxySelectorTest.java
161 TEST_F(ProxyConfigServiceAndroidTest
, NoProxy
) {
162 // Test direct mapping when no proxy defined.
163 ProxySettingsChanged();
164 TestMapping("ftp://example.com/", "DIRECT");
165 TestMapping("http://example.com/", "DIRECT");
166 TestMapping("https://example.com/", "DIRECT");
169 TEST_F(ProxyConfigServiceAndroidTest
, HttpProxyHostAndPort
) {
170 // Test http.proxyHost and http.proxyPort works.
171 AddProperty("http.proxyHost", "httpproxy.com");
172 AddProperty("http.proxyPort", "8080");
173 ProxySettingsChanged();
174 TestMapping("ftp://example.com/", "DIRECT");
175 TestMapping("http://example.com/", "PROXY httpproxy.com:8080");
176 TestMapping("https://example.com/", "DIRECT");
179 TEST_F(ProxyConfigServiceAndroidTest
, HttpProxyHostOnly
) {
180 // We should get the default port (80) for proxied hosts.
181 AddProperty("http.proxyHost", "httpproxy.com");
182 ProxySettingsChanged();
183 TestMapping("ftp://example.com/", "DIRECT");
184 TestMapping("http://example.com/", "PROXY httpproxy.com:80");
185 TestMapping("https://example.com/", "DIRECT");
188 TEST_F(ProxyConfigServiceAndroidTest
, HttpProxyPortOnly
) {
189 // http.proxyPort only should not result in any hosts being proxied.
190 AddProperty("http.proxyPort", "8080");
191 ProxySettingsChanged();
192 TestMapping("ftp://example.com/", "DIRECT");
193 TestMapping("http://example.com/", "DIRECT");
194 TestMapping("https://example.com/", "DIRECT");
197 TEST_F(ProxyConfigServiceAndroidTest
, HttpNonProxyHosts1
) {
198 // Test that HTTP non proxy hosts are mapped correctly
199 AddProperty("http.nonProxyHosts", "slashdot.org");
200 AddProperty("http.proxyHost", "httpproxy.com");
201 AddProperty("http.proxyPort", "8080");
202 ProxySettingsChanged();
203 TestMapping("http://example.com/", "PROXY httpproxy.com:8080");
204 TestMapping("http://slashdot.org/", "DIRECT");
207 TEST_F(ProxyConfigServiceAndroidTest
, HttpNonProxyHosts2
) {
208 // Test that | pattern works.
209 AddProperty("http.nonProxyHosts", "slashdot.org|freecode.net");
210 AddProperty("http.proxyHost", "httpproxy.com");
211 AddProperty("http.proxyPort", "8080");
212 ProxySettingsChanged();
213 TestMapping("http://example.com/", "PROXY httpproxy.com:8080");
214 TestMapping("http://freecode.net/", "DIRECT");
215 TestMapping("http://slashdot.org/", "DIRECT");
218 TEST_F(ProxyConfigServiceAndroidTest
, HttpNonProxyHosts3
) {
219 // Test that * pattern works.
220 AddProperty("http.nonProxyHosts", "*example.com");
221 AddProperty("http.proxyHost", "httpproxy.com");
222 AddProperty("http.proxyPort", "8080");
223 ProxySettingsChanged();
224 TestMapping("http://example.com/", "DIRECT");
225 TestMapping("http://slashdot.org/", "PROXY httpproxy.com:8080");
226 TestMapping("http://www.example.com/", "DIRECT");
229 TEST_F(ProxyConfigServiceAndroidTest
, FtpNonProxyHosts
) {
230 // Test that FTP non proxy hosts are mapped correctly
231 AddProperty("ftp.nonProxyHosts", "slashdot.org");
232 AddProperty("ftp.proxyHost", "httpproxy.com");
233 AddProperty("ftp.proxyPort", "8080");
234 ProxySettingsChanged();
235 TestMapping("ftp://example.com/", "PROXY httpproxy.com:8080");
236 TestMapping("http://example.com/", "DIRECT");
239 TEST_F(ProxyConfigServiceAndroidTest
, FtpProxyHostAndPort
) {
240 // Test ftp.proxyHost and ftp.proxyPort works.
241 AddProperty("ftp.proxyHost", "httpproxy.com");
242 AddProperty("ftp.proxyPort", "8080");
243 ProxySettingsChanged();
244 TestMapping("ftp://example.com/", "PROXY httpproxy.com:8080");
245 TestMapping("http://example.com/", "DIRECT");
246 TestMapping("https://example.com/", "DIRECT");
249 TEST_F(ProxyConfigServiceAndroidTest
, FtpProxyHostOnly
) {
250 // Test ftp.proxyHost and default port.
251 AddProperty("ftp.proxyHost", "httpproxy.com");
252 ProxySettingsChanged();
253 TestMapping("ftp://example.com/", "PROXY httpproxy.com:80");
254 TestMapping("http://example.com/", "DIRECT");
255 TestMapping("https://example.com/", "DIRECT");
258 TEST_F(ProxyConfigServiceAndroidTest
, HttpsProxyHostAndPort
) {
259 // Test https.proxyHost and https.proxyPort works.
260 AddProperty("https.proxyHost", "httpproxy.com");
261 AddProperty("https.proxyPort", "8080");
262 ProxySettingsChanged();
263 TestMapping("ftp://example.com/", "DIRECT");
264 TestMapping("http://example.com/", "DIRECT");
265 TestMapping("https://example.com/", "PROXY httpproxy.com:8080");
268 TEST_F(ProxyConfigServiceAndroidTest
, HttpsProxyHostOnly
) {
269 // Test https.proxyHost and default port.
270 AddProperty("https.proxyHost", "httpproxy.com");
271 ProxySettingsChanged();
272 TestMapping("ftp://example.com/", "DIRECT");
273 TestMapping("http://example.com/", "DIRECT");
274 TestMapping("https://example.com/", "PROXY httpproxy.com:80");
277 TEST_F(ProxyConfigServiceAndroidTest
, HttpProxyHostIPv6
) {
278 // Test IPv6 https.proxyHost and default port.
279 AddProperty("http.proxyHost", "a:b:c::d:1");
280 ProxySettingsChanged();
281 TestMapping("ftp://example.com/", "DIRECT");
282 TestMapping("http://example.com/", "PROXY [a:b:c::d:1]:80");
285 TEST_F(ProxyConfigServiceAndroidTest
, HttpProxyHostAndPortIPv6
) {
286 // Test IPv6 http.proxyHost and http.proxyPort works.
287 AddProperty("http.proxyHost", "a:b:c::d:1");
288 AddProperty("http.proxyPort", "8080");
289 ProxySettingsChanged();
290 TestMapping("ftp://example.com/", "DIRECT");
291 TestMapping("http://example.com/", "PROXY [a:b:c::d:1]:8080");
294 TEST_F(ProxyConfigServiceAndroidTest
, HttpProxyHostAndInvalidPort
) {
295 // Test invalid http.proxyPort does not crash.
296 AddProperty("http.proxyHost", "a:b:c::d:1");
297 AddProperty("http.proxyPort", "65536");
298 ProxySettingsChanged();
299 TestMapping("ftp://example.com/", "DIRECT");
300 TestMapping("http://example.com/", "DIRECT");
303 TEST_F(ProxyConfigServiceAndroidTest
, DefaultProxyExplictPort
) {
304 // Default http proxy is used if a scheme-specific one is not found.
305 AddProperty("ftp.proxyHost", "httpproxy.com");
306 AddProperty("ftp.proxyPort", "8080");
307 AddProperty("proxyHost", "defaultproxy.com");
308 AddProperty("proxyPort", "8080");
309 ProxySettingsChanged();
310 TestMapping("ftp://example.com/", "PROXY httpproxy.com:8080");
311 TestMapping("http://example.com/", "PROXY defaultproxy.com:8080");
312 TestMapping("https://example.com/", "PROXY defaultproxy.com:8080");
315 TEST_F(ProxyConfigServiceAndroidTest
, DefaultProxyDefaultPort
) {
316 // Check that the default proxy port is as expected.
317 AddProperty("proxyHost", "defaultproxy.com");
318 ProxySettingsChanged();
319 TestMapping("http://example.com/", "PROXY defaultproxy.com:80");
320 TestMapping("https://example.com/", "PROXY defaultproxy.com:80");
323 TEST_F(ProxyConfigServiceAndroidTest
, FallbackToSocks
) {
324 // SOCKS proxy is used if scheme-specific one is not found.
325 AddProperty("http.proxyHost", "defaultproxy.com");
326 AddProperty("socksProxyHost", "socksproxy.com");
327 ProxySettingsChanged();
328 TestMapping("ftp://example.com", "SOCKS5 socksproxy.com:1080");
329 TestMapping("http://example.com/", "PROXY defaultproxy.com:80");
330 TestMapping("https://example.com/", "SOCKS5 socksproxy.com:1080");
333 TEST_F(ProxyConfigServiceAndroidTest
, SocksExplicitPort
) {
334 // SOCKS proxy port is used if specified
335 AddProperty("socksProxyHost", "socksproxy.com");
336 AddProperty("socksProxyPort", "9000");
337 ProxySettingsChanged();
338 TestMapping("http://example.com/", "SOCKS5 socksproxy.com:9000");
341 TEST_F(ProxyConfigServiceAndroidTest
, HttpProxySupercedesSocks
) {
342 // SOCKS proxy is ignored if default HTTP proxy defined.
343 AddProperty("proxyHost", "defaultproxy.com");
344 AddProperty("socksProxyHost", "socksproxy.com");
345 AddProperty("socksProxyPort", "9000");
346 ProxySettingsChanged();
347 TestMapping("http://example.com/", "PROXY defaultproxy.com:80");