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