1 // Copyright 2009 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/proxy_config_service_common_unittest.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "net/base/proxy_string_util.h"
11 #include "net/proxy_resolution/proxy_config.h"
12
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace net {
16
17 namespace {
18
19 // Helper to verify that |expected_proxy| matches the first proxy conatined in
20 // |actual_proxies|, and that |actual_proxies| contains exactly one proxy. If
21 // either condition is untrue, then |*did_fail| is set to true, and
22 // |*failure_details| is filled with a description of the failure.
MatchesProxyServerHelper(const char * failure_message,const char * expected_proxy,const ProxyList & actual_proxies,::testing::AssertionResult * failure_details,bool * did_fail)23 void MatchesProxyServerHelper(const char* failure_message,
24 const char* expected_proxy,
25 const ProxyList& actual_proxies,
26 ::testing::AssertionResult* failure_details,
27 bool* did_fail) {
28 // If |expected_proxy| is empty, then we expect |actual_proxies| to be so as
29 // well.
30 if (strlen(expected_proxy) == 0) {
31 if (!actual_proxies.IsEmpty()) {
32 *did_fail = true;
33 *failure_details
34 << failure_message << ". Was expecting no proxies but got "
35 << actual_proxies.size() << ".";
36 }
37 return;
38 }
39
40 // Otherwise we check that |actual_proxies| holds a single matching proxy.
41 if (actual_proxies.size() != 1) {
42 *did_fail = true;
43 *failure_details
44 << failure_message << ". Was expecting exactly one proxy but got "
45 << actual_proxies.size() << ".";
46 return;
47 }
48
49 ASSERT_EQ(1u, actual_proxies.First().length());
50 ProxyServer actual_proxy =
51 actual_proxies.First().GetProxyServer(/*chain_index=*/0);
52 std::string actual_proxy_string;
53 if (actual_proxy.is_valid())
54 actual_proxy_string = ProxyServerToProxyUri(actual_proxy);
55
56 if (std::string(expected_proxy) != actual_proxy_string) {
57 *failure_details
58 << failure_message << ". Was expecting: \"" << expected_proxy
59 << "\" but got: \"" << actual_proxy_string << "\"";
60 *did_fail = true;
61 }
62 }
63
FlattenProxyBypass(const ProxyBypassRules & bypass_rules)64 std::string FlattenProxyBypass(const ProxyBypassRules& bypass_rules) {
65 std::string flattened_proxy_bypass;
66 for (const auto& bypass_rule : bypass_rules.rules()) {
67 if (!flattened_proxy_bypass.empty())
68 flattened_proxy_bypass += ",";
69 flattened_proxy_bypass += bypass_rule->ToString();
70 }
71 return flattened_proxy_bypass;
72 }
73
74 } // namespace
75
ProxyRulesExpectation(ProxyConfig::ProxyRules::Type type,const char * single_proxy,const char * proxy_for_http,const char * proxy_for_https,const char * proxy_for_ftp,const char * fallback_proxy,const char * flattened_bypass_rules,bool reverse_bypass)76 ProxyRulesExpectation::ProxyRulesExpectation(
77 ProxyConfig::ProxyRules::Type type,
78 const char* single_proxy,
79 const char* proxy_for_http,
80 const char* proxy_for_https,
81 const char* proxy_for_ftp,
82 const char* fallback_proxy,
83 const char* flattened_bypass_rules,
84 bool reverse_bypass)
85 : type(type),
86 single_proxy(single_proxy),
87 proxy_for_http(proxy_for_http),
88 proxy_for_https(proxy_for_https),
89 proxy_for_ftp(proxy_for_ftp),
90 fallback_proxy(fallback_proxy),
91 flattened_bypass_rules(flattened_bypass_rules),
92 reverse_bypass(reverse_bypass) {
93 }
94
95
Matches(const ProxyConfig::ProxyRules & rules) const96 ::testing::AssertionResult ProxyRulesExpectation::Matches(
97 const ProxyConfig::ProxyRules& rules) const {
98 ::testing::AssertionResult failure_details = ::testing::AssertionFailure();
99 bool failed = false;
100
101 if (rules.type != type) {
102 failure_details << "Type mismatch. Expected: " << static_cast<int>(type)
103 << " but was: " << static_cast<int>(rules.type);
104 failed = true;
105 }
106
107 MatchesProxyServerHelper("Bad single_proxy", single_proxy,
108 rules.single_proxies, &failure_details, &failed);
109 MatchesProxyServerHelper("Bad proxy_for_http", proxy_for_http,
110 rules.proxies_for_http, &failure_details,
111 &failed);
112 MatchesProxyServerHelper("Bad proxy_for_https", proxy_for_https,
113 rules.proxies_for_https, &failure_details,
114 &failed);
115 MatchesProxyServerHelper("Bad fallback_proxy", fallback_proxy,
116 rules.fallback_proxies, &failure_details, &failed);
117
118 std::string actual_flattened_bypass = FlattenProxyBypass(rules.bypass_rules);
119 if (std::string(flattened_bypass_rules) != actual_flattened_bypass) {
120 failure_details
121 << "Bad bypass rules. Expected: \"" << flattened_bypass_rules
122 << "\" but got: \"" << actual_flattened_bypass << "\"";
123 failed = true;
124 }
125
126 if (rules.reverse_bypass != reverse_bypass) {
127 failure_details << "Bad reverse_bypass. Expected: " << reverse_bypass
128 << " but got: " << rules.reverse_bypass;
129 failed = true;
130 }
131
132 return failed ? failure_details : ::testing::AssertionSuccess();
133 }
134
135 // static
Empty()136 ProxyRulesExpectation ProxyRulesExpectation::Empty() {
137 return ProxyRulesExpectation(ProxyConfig::ProxyRules::Type::EMPTY,
138 "", "", "", "", "", "", false);
139 }
140
141 // static
EmptyWithBypass(const char * flattened_bypass_rules)142 ProxyRulesExpectation ProxyRulesExpectation::EmptyWithBypass(
143 const char* flattened_bypass_rules) {
144 return ProxyRulesExpectation(ProxyConfig::ProxyRules::Type::EMPTY,
145 "", "", "", "", "", flattened_bypass_rules,
146 false);
147 }
148
149 // static
Single(const char * single_proxy,const char * flattened_bypass_rules)150 ProxyRulesExpectation ProxyRulesExpectation::Single(
151 const char* single_proxy,
152 const char* flattened_bypass_rules) {
153 return ProxyRulesExpectation(ProxyConfig::ProxyRules::Type::PROXY_LIST,
154 single_proxy, "", "", "", "",
155 flattened_bypass_rules, false);
156 }
157
158 // static
PerScheme(const char * proxy_http,const char * proxy_https,const char * proxy_ftp,const char * flattened_bypass_rules)159 ProxyRulesExpectation ProxyRulesExpectation::PerScheme(
160 const char* proxy_http,
161 const char* proxy_https,
162 const char* proxy_ftp,
163 const char* flattened_bypass_rules) {
164 return ProxyRulesExpectation(ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME,
165 "", proxy_http, proxy_https, proxy_ftp, "",
166 flattened_bypass_rules, false);
167 }
168
169 // static
PerSchemeWithSocks(const char * proxy_http,const char * proxy_https,const char * proxy_ftp,const char * socks_proxy,const char * flattened_bypass_rules)170 ProxyRulesExpectation ProxyRulesExpectation::PerSchemeWithSocks(
171 const char* proxy_http,
172 const char* proxy_https,
173 const char* proxy_ftp,
174 const char* socks_proxy,
175 const char* flattened_bypass_rules) {
176 return ProxyRulesExpectation(ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME,
177 "", proxy_http, proxy_https, proxy_ftp,
178 socks_proxy, flattened_bypass_rules, false);
179 }
180
181 // static
PerSchemeWithBypassReversed(const char * proxy_http,const char * proxy_https,const char * proxy_ftp,const char * flattened_bypass_rules)182 ProxyRulesExpectation ProxyRulesExpectation::PerSchemeWithBypassReversed(
183 const char* proxy_http,
184 const char* proxy_https,
185 const char* proxy_ftp,
186 const char* flattened_bypass_rules) {
187 return ProxyRulesExpectation(ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME,
188 "", proxy_http, proxy_https, proxy_ftp, "",
189 flattened_bypass_rules, true);
190 }
191
192 } // namespace net
193