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/proxy_config_service_common_unittest.h"
6
7#include <string>
8#include <vector>
9
10#include "net/proxy/proxy_config.h"
11
12#include "testing/gtest/include/gtest/gtest.h"
13
14namespace net {
15
16namespace {
17
18// Helper to verify that |expected_proxy| matches |actual_proxy|. If it does
19// not, then |*did_fail| is set to true, and |*failure_details| is filled with
20// a description of the failure.
21void MatchesProxyServerHelper(const char* failure_message,
22                              const char* expected_proxy,
23                              const ProxyServer& actual_proxy,
24                              ::testing::AssertionResult* failure_details,
25                              bool* did_fail) {
26  std::string actual_proxy_string;
27  if (actual_proxy.is_valid())
28    actual_proxy_string = actual_proxy.ToURI();
29
30  if (std::string(expected_proxy) != actual_proxy_string) {
31    *failure_details
32        << failure_message << ". Was expecting: \"" << expected_proxy
33        << "\" but got: \"" << actual_proxy_string << "\"";
34    *did_fail = true;
35  }
36}
37
38std::string FlattenProxyBypass(const ProxyBypassRules& bypass_rules) {
39  std::string flattened_proxy_bypass;
40  for (ProxyBypassRules::RuleList::const_iterator it =
41       bypass_rules.rules().begin();
42       it != bypass_rules.rules().end(); ++it) {
43    if (!flattened_proxy_bypass.empty())
44      flattened_proxy_bypass += ",";
45    flattened_proxy_bypass += (*it)->ToString();
46  }
47  return flattened_proxy_bypass;
48}
49
50}  // namespace
51
52ProxyRulesExpectation::ProxyRulesExpectation(
53    ProxyConfig::ProxyRules::Type type,
54    const char* single_proxy,
55    const char* proxy_for_http,
56    const char* proxy_for_https,
57    const char* proxy_for_ftp,
58    const char* fallback_proxy,
59    const char* flattened_bypass_rules,
60    bool reverse_bypass)
61    : type(type),
62      single_proxy(single_proxy),
63      proxy_for_http(proxy_for_http),
64      proxy_for_https(proxy_for_https),
65      proxy_for_ftp(proxy_for_ftp),
66      fallback_proxy(fallback_proxy),
67      flattened_bypass_rules(flattened_bypass_rules),
68      reverse_bypass(reverse_bypass) {
69}
70
71
72::testing::AssertionResult ProxyRulesExpectation::Matches(
73    const ProxyConfig::ProxyRules& rules) const {
74  ::testing::AssertionResult failure_details = ::testing::AssertionFailure();
75  bool failed = false;
76
77  if (rules.type != type) {
78    failure_details << "Type mismatch. Expected: "
79                    << type << " but was: " << rules.type;
80    failed = true;
81  }
82
83  MatchesProxyServerHelper("Bad single_proxy", single_proxy,
84                           rules.single_proxy, &failure_details, &failed);
85  MatchesProxyServerHelper("Bad proxy_for_http", proxy_for_http,
86                           rules.proxy_for_http, &failure_details, &failed);
87  MatchesProxyServerHelper("Bad proxy_for_https", proxy_for_https,
88                           rules.proxy_for_https, &failure_details, &failed);
89  MatchesProxyServerHelper("Bad fallback_proxy", fallback_proxy,
90                           rules.fallback_proxy, &failure_details, &failed);
91
92  std::string actual_flattened_bypass = FlattenProxyBypass(rules.bypass_rules);
93  if (std::string(flattened_bypass_rules) != actual_flattened_bypass) {
94    failure_details
95        << "Bad bypass rules. Expected: \"" << flattened_bypass_rules
96        << "\" but got: \"" << actual_flattened_bypass << "\"";
97    failed = true;
98  }
99
100  if (rules.reverse_bypass != reverse_bypass) {
101    failure_details << "Bad reverse_bypass. Expected: " << reverse_bypass
102                    << " but got: " << rules.reverse_bypass;
103    failed = true;
104  }
105
106  return failed ? failure_details : ::testing::AssertionSuccess();
107}
108
109// static
110ProxyRulesExpectation ProxyRulesExpectation::Empty() {
111  return ProxyRulesExpectation(ProxyConfig::ProxyRules::TYPE_NO_RULES,
112                               "", "", "", "", "", "", false);
113}
114
115// static
116ProxyRulesExpectation ProxyRulesExpectation::EmptyWithBypass(
117    const char* flattened_bypass_rules) {
118  return ProxyRulesExpectation(ProxyConfig::ProxyRules::TYPE_NO_RULES,
119                               "", "", "", "", "", flattened_bypass_rules,
120                               false);
121}
122
123// static
124ProxyRulesExpectation ProxyRulesExpectation::Single(
125    const char* single_proxy,
126    const char* flattened_bypass_rules) {
127  return ProxyRulesExpectation(ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY,
128                               single_proxy, "", "", "", "",
129                               flattened_bypass_rules, false);
130}
131
132// static
133ProxyRulesExpectation ProxyRulesExpectation::PerScheme(
134    const char* proxy_http,
135    const char* proxy_https,
136    const char* proxy_ftp,
137    const char* flattened_bypass_rules) {
138  return ProxyRulesExpectation(ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
139                               "", proxy_http, proxy_https, proxy_ftp, "",
140                               flattened_bypass_rules, false);
141}
142
143// static
144ProxyRulesExpectation ProxyRulesExpectation::PerSchemeWithSocks(
145    const char* proxy_http,
146    const char* proxy_https,
147    const char* proxy_ftp,
148    const char* socks_proxy,
149    const char* flattened_bypass_rules) {
150  return ProxyRulesExpectation(ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
151                               "", proxy_http, proxy_https, proxy_ftp,
152                               socks_proxy, flattened_bypass_rules, false);
153}
154
155// static
156ProxyRulesExpectation ProxyRulesExpectation::PerSchemeWithBypassReversed(
157    const char* proxy_http,
158    const char* proxy_https,
159    const char* proxy_ftp,
160    const char* flattened_bypass_rules) {
161  return ProxyRulesExpectation(ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
162                               "", proxy_http, proxy_https, proxy_ftp, "",
163                               flattened_bypass_rules, true);
164}
165
166}  // namespace net
167