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.
4
5 #include "net/proxy/proxy_config_service_win.h"
6
7 #include "net/base/net_errors.h"
8 #include "net/proxy/proxy_config.h"
9 #include "net/proxy/proxy_config_service_common_unittest.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace net {
13
TEST(ProxyConfigServiceWinTest,SetFromIEConfig)14 TEST(ProxyConfigServiceWinTest, SetFromIEConfig) {
15 // Like WINHTTP_CURRENT_USER_IE_PROXY_CONFIG, but with const strings.
16 struct IEProxyConfig {
17 BOOL auto_detect;
18 const wchar_t* auto_config_url;
19 const wchar_t* proxy;
20 const wchar_t* proxy_bypass;
21 };
22 const struct {
23 // Input.
24 IEProxyConfig ie_config;
25
26 // Expected outputs (fields of the ProxyConfig).
27 bool auto_detect;
28 GURL pac_url;
29 ProxyRulesExpectation proxy_rules;
30 const char* proxy_bypass_list; // newline separated
31 } tests[] = {
32 // Auto detect.
33 {
34 { // Input.
35 TRUE, // fAutoDetect
36 NULL, // lpszAutoConfigUrl
37 NULL, // lpszProxy
38 NULL, // lpszProxyBypass
39 },
40
41 // Expected result.
42 true, // auto_detect
43 GURL(), // pac_url
44 ProxyRulesExpectation::Empty(),
45 },
46
47 // Valid PAC url
48 {
49 { // Input.
50 FALSE, // fAutoDetect
51 L"http://wpad/wpad.dat", // lpszAutoConfigUrl
52 NULL, // lpszProxy
53 NULL, // lpszProxy_bypass
54 },
55
56 // Expected result.
57 false, // auto_detect
58 GURL("http://wpad/wpad.dat"), // pac_url
59 ProxyRulesExpectation::Empty(),
60 },
61
62 // Invalid PAC url string.
63 {
64 { // Input.
65 FALSE, // fAutoDetect
66 L"wpad.dat", // lpszAutoConfigUrl
67 NULL, // lpszProxy
68 NULL, // lpszProxy_bypass
69 },
70
71 // Expected result.
72 false, // auto_detect
73 GURL(), // pac_url
74 ProxyRulesExpectation::Empty(),
75 },
76
77 // Single-host in proxy list.
78 {
79 { // Input.
80 FALSE, // fAutoDetect
81 NULL, // lpszAutoConfigUrl
82 L"www.google.com", // lpszProxy
83 NULL, // lpszProxy_bypass
84 },
85
86 // Expected result.
87 false, // auto_detect
88 GURL(), // pac_url
89 ProxyRulesExpectation::Single(
90 "www.google.com:80", // single proxy
91 ""), // bypass rules
92 },
93
94 // Per-scheme proxy rules.
95 {
96 { // Input.
97 FALSE, // fAutoDetect
98 NULL, // lpszAutoConfigUrl
99 L"http=www.google.com:80;https=www.foo.com:110", // lpszProxy
100 NULL, // lpszProxy_bypass
101 },
102
103 // Expected result.
104 false, // auto_detect
105 GURL(), // pac_url
106 ProxyRulesExpectation::PerScheme(
107 "www.google.com:80", // http
108 "www.foo.com:110", // https
109 "", // ftp
110 ""), // bypass rules
111 },
112
113 // SOCKS proxy configuration.
114 {
115 { // Input.
116 FALSE, // fAutoDetect
117 NULL, // lpszAutoConfigUrl
118 L"http=www.google.com:80;https=www.foo.com:110;"
119 L"ftp=ftpproxy:20;socks=foopy:130", // lpszProxy
120 NULL, // lpszProxy_bypass
121 },
122
123 // Expected result.
124 // Note that "socks" is interprted as meaning "socks4", since that is how
125 // Internet Explorer applies the settings. For more details on this
126 // policy, see:
127 // http://code.google.com/p/chromium/issues/detail?id=55912#c2
128 false, // auto_detect
129 GURL(), // pac_url
130 ProxyRulesExpectation::PerSchemeWithSocks(
131 "www.google.com:80", // http
132 "www.foo.com:110", // https
133 "ftpproxy:20", // ftp
134 "socks4://foopy:130", // socks
135 ""), // bypass rules
136 },
137
138 // Bypass local names.
139 {
140 { // Input.
141 TRUE, // fAutoDetect
142 NULL, // lpszAutoConfigUrl
143 NULL, // lpszProxy
144 L"<local>", // lpszProxy_bypass
145 },
146
147 true, // auto_detect
148 GURL(), // pac_url
149 ProxyRulesExpectation::EmptyWithBypass("<local>"),
150 },
151
152 // Bypass "google.com" and local names, using semicolon as delimiter
153 // (ignoring white space).
154 {
155 { // Input.
156 TRUE, // fAutoDetect
157 NULL, // lpszAutoConfigUrl
158 NULL, // lpszProxy
159 L"<local> ; google.com", // lpszProxy_bypass
160 },
161
162 // Expected result.
163 true, // auto_detect
164 GURL(), // pac_url
165 ProxyRulesExpectation::EmptyWithBypass("<local>,google.com"),
166 },
167
168 // Bypass "foo.com" and "google.com", using lines as delimiter.
169 {
170 { // Input.
171 TRUE, // fAutoDetect
172 NULL, // lpszAutoConfigUrl
173 NULL, // lpszProxy
174 L"foo.com\r\ngoogle.com", // lpszProxy_bypass
175 },
176
177 // Expected result.
178 true, // auto_detect
179 GURL(), // pac_url
180 ProxyRulesExpectation::EmptyWithBypass("foo.com,google.com"),
181 },
182
183 // Bypass "foo.com" and "google.com", using commas as delimiter.
184 {
185 { // Input.
186 TRUE, // fAutoDetect
187 NULL, // lpszAutoConfigUrl
188 NULL, // lpszProxy
189 L"foo.com, google.com", // lpszProxy_bypass
190 },
191
192 // Expected result.
193 true, // auto_detect
194 GURL(), // pac_url
195 ProxyRulesExpectation::EmptyWithBypass("foo.com,google.com"),
196 },
197 };
198
199 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
200 WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ie_config = {
201 tests[i].ie_config.auto_detect,
202 const_cast<wchar_t*>(tests[i].ie_config.auto_config_url),
203 const_cast<wchar_t*>(tests[i].ie_config.proxy),
204 const_cast<wchar_t*>(tests[i].ie_config.proxy_bypass)};
205 ProxyConfig config;
206 ProxyConfigServiceWin::SetFromIEConfig(&config, ie_config);
207
208 EXPECT_EQ(tests[i].auto_detect, config.auto_detect());
209 EXPECT_EQ(tests[i].pac_url, config.pac_url());
210 EXPECT_TRUE(tests[i].proxy_rules.Matches(config.proxy_rules()));
211 EXPECT_EQ(PROXY_CONFIG_SOURCE_SYSTEM, config.source());
212 }
213 }
214
215 } // namespace net
216