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