proxy_config_service_common_unittest.cc 6.7 KB

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