Probably broke Win7 Tests (dbg)(6). http://build.chromium.org/p/chromium.win/builders...
[chromium-blink-merge.git] / net / proxy / proxy_config_service_common_unittest.cc
blob3835bb1ad201f745943395552d625f1c9d033938
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.
5 #include "net/proxy/proxy_config_service_common_unittest.h"
7 #include <string>
8 #include <vector>
10 #include "net/proxy/proxy_config.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace net {
16 namespace {
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.
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() << ".";
36 return;
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;
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();
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;
61 std::string FlattenProxyBypass(const ProxyBypassRules& bypass_rules) {
62 std::string flattened_proxy_bypass;
63 for (ProxyBypassRules::RuleList::const_iterator it =
64 bypass_rules.rules().begin();
65 it != bypass_rules.rules().end(); ++it) {
66 if (!flattened_proxy_bypass.empty())
67 flattened_proxy_bypass += ",";
68 flattened_proxy_bypass += (*it)->ToString();
70 return flattened_proxy_bypass;
73 } // namespace
75 ProxyRulesExpectation::ProxyRulesExpectation(
76 ProxyConfig::ProxyRules::Type type,
77 const char* single_proxy,
78 const char* proxy_for_http,
79 const char* proxy_for_https,
80 const char* proxy_for_ftp,
81 const char* fallback_proxy,
82 const char* flattened_bypass_rules,
83 bool reverse_bypass)
84 : type(type),
85 single_proxy(single_proxy),
86 proxy_for_http(proxy_for_http),
87 proxy_for_https(proxy_for_https),
88 proxy_for_ftp(proxy_for_ftp),
89 fallback_proxy(fallback_proxy),
90 flattened_bypass_rules(flattened_bypass_rules),
91 reverse_bypass(reverse_bypass) {
95 ::testing::AssertionResult ProxyRulesExpectation::Matches(
96 const ProxyConfig::ProxyRules& rules) const {
97 ::testing::AssertionResult failure_details = ::testing::AssertionFailure();
98 bool failed = false;
100 if (rules.type != type) {
101 failure_details << "Type mismatch. Expected: "
102 << type << " but was: " << rules.type;
103 failed = true;
106 MatchesProxyServerHelper("Bad single_proxy", single_proxy,
107 rules.single_proxies, &failure_details, &failed);
108 MatchesProxyServerHelper("Bad proxy_for_http", proxy_for_http,
109 rules.proxies_for_http, &failure_details,
110 &failed);
111 MatchesProxyServerHelper("Bad proxy_for_https", proxy_for_https,
112 rules.proxies_for_https, &failure_details,
113 &failed);
114 MatchesProxyServerHelper("Bad fallback_proxy", fallback_proxy,
115 rules.fallback_proxies, &failure_details, &failed);
117 std::string actual_flattened_bypass = FlattenProxyBypass(rules.bypass_rules);
118 if (std::string(flattened_bypass_rules) != actual_flattened_bypass) {
119 failure_details
120 << "Bad bypass rules. Expected: \"" << flattened_bypass_rules
121 << "\" but got: \"" << actual_flattened_bypass << "\"";
122 failed = true;
125 if (rules.reverse_bypass != reverse_bypass) {
126 failure_details << "Bad reverse_bypass. Expected: " << reverse_bypass
127 << " but got: " << rules.reverse_bypass;
128 failed = true;
131 return failed ? failure_details : ::testing::AssertionSuccess();
134 // static
135 ProxyRulesExpectation ProxyRulesExpectation::Empty() {
136 return ProxyRulesExpectation(ProxyConfig::ProxyRules::TYPE_NO_RULES,
137 "", "", "", "", "", "", false);
140 // static
141 ProxyRulesExpectation ProxyRulesExpectation::EmptyWithBypass(
142 const char* flattened_bypass_rules) {
143 return ProxyRulesExpectation(ProxyConfig::ProxyRules::TYPE_NO_RULES,
144 "", "", "", "", "", flattened_bypass_rules,
145 false);
148 // static
149 ProxyRulesExpectation ProxyRulesExpectation::Single(
150 const char* single_proxy,
151 const char* flattened_bypass_rules) {
152 return ProxyRulesExpectation(ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY,
153 single_proxy, "", "", "", "",
154 flattened_bypass_rules, false);
157 // static
158 ProxyRulesExpectation ProxyRulesExpectation::PerScheme(
159 const char* proxy_http,
160 const char* proxy_https,
161 const char* proxy_ftp,
162 const char* flattened_bypass_rules) {
163 return ProxyRulesExpectation(ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
164 "", proxy_http, proxy_https, proxy_ftp, "",
165 flattened_bypass_rules, false);
168 // static
169 ProxyRulesExpectation ProxyRulesExpectation::PerSchemeWithSocks(
170 const char* proxy_http,
171 const char* proxy_https,
172 const char* proxy_ftp,
173 const char* socks_proxy,
174 const char* flattened_bypass_rules) {
175 return ProxyRulesExpectation(ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
176 "", proxy_http, proxy_https, proxy_ftp,
177 socks_proxy, flattened_bypass_rules, false);
180 // static
181 ProxyRulesExpectation ProxyRulesExpectation::PerSchemeWithBypassReversed(
182 const char* proxy_http,
183 const char* proxy_https,
184 const char* proxy_ftp,
185 const char* flattened_bypass_rules) {
186 return ProxyRulesExpectation(ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
187 "", proxy_http, proxy_https, proxy_ftp, "",
188 flattened_bypass_rules, true);
191 } // namespace net