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