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