canonical_cookie_test_helpers.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2018 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. #ifndef NET_COOKIES_CANONICAL_COOKIE_TEST_HELPERS_H_
  5. #define NET_COOKIES_CANONICAL_COOKIE_TEST_HELPERS_H_
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/strings/string_split.h"
  10. #include "net/cookies/canonical_cookie.h"
  11. #include "testing/gmock/include/gmock/gmock-matchers.h"
  12. #include "testing/gmock/include/gmock/gmock.h"
  13. namespace net {
  14. MATCHER_P(MatchesCookieLine, cookie_line, "") {
  15. std::string argument_line = CanonicalCookie::BuildCookieLine(arg);
  16. if (argument_line == cookie_line)
  17. return true;
  18. *result_listener << argument_line;
  19. return false;
  20. }
  21. // Matches a CanonicalCookie with the given name.
  22. MATCHER_P(MatchesCookieWithName, name, "") {
  23. return testing::ExplainMatchResult(name, arg.Name(), result_listener);
  24. }
  25. MATCHER_P2(MatchesCookieNameValue, name, value, "") {
  26. const CanonicalCookie& cookie = arg;
  27. return testing::ExplainMatchResult(name, cookie.Name(), result_listener) &&
  28. testing::ExplainMatchResult(value, cookie.Value(), result_listener);
  29. }
  30. MATCHER_P(MatchesCookieAccessWithName, name, "") {
  31. return testing::ExplainMatchResult(MatchesCookieWithName(name), arg.cookie,
  32. result_listener);
  33. }
  34. // Splits a string into key-value pairs, and executes the provided matcher on
  35. // the result.
  36. MATCHER_P3(WhenKVSplit, pair_delim, kv_delim, inner_matcher, "") {
  37. std::vector<std::pair<std::string, std::string>> pairs;
  38. // Return an empty vector when a cookie string (such as "None") cannot be
  39. // split into 'name=value' pairs.
  40. bool successful_split =
  41. base::SplitStringIntoKeyValuePairs(arg, kv_delim, pair_delim, &pairs);
  42. if (successful_split) {
  43. return testing::ExplainMatchResult(inner_matcher, pairs, result_listener);
  44. } else {
  45. std::vector<std::pair<std::string, std::string>> empty_pairs;
  46. return testing::ExplainMatchResult(inner_matcher, empty_pairs,
  47. result_listener);
  48. }
  49. }
  50. // Executes the inner_matcher on the Cookie string arg after it's transformed
  51. // into a vector.
  52. // If the arg is a ';'-delimited string of Cookie 'name=value' or 'name' pairs,
  53. // then the matcher will execute on a vector of <name, value> pairs.
  54. // If the arg can't be split into these pairs then the inner_matcher will
  55. // execute on an empty vector.
  56. MATCHER_P(CookieStringIs, inner_matcher, "") {
  57. return testing::ExplainMatchResult(WhenKVSplit(';', '=', inner_matcher), arg,
  58. result_listener);
  59. }
  60. MATCHER_P2(MatchesCookieWithAccessResult, cookie, access_result, "") {
  61. const CookieWithAccessResult& cwar = arg;
  62. return testing::ExplainMatchResult(cookie, cwar.cookie, result_listener) &&
  63. testing::ExplainMatchResult(access_result, cwar.access_result,
  64. result_listener);
  65. }
  66. // Helper for checking that status.IsInclude() == true.
  67. MATCHER(IsInclude, "") {
  68. const CookieInclusionStatus& status = arg;
  69. return testing::ExplainMatchResult(true, status.IsInclude(), result_listener);
  70. }
  71. // Helper for checking that status.HasDowngradeWarning() == true.
  72. MATCHER(HasDowngradeWarning, "") {
  73. CookieInclusionStatus status = arg;
  74. return testing::ExplainMatchResult(true, status.HasDowngradeWarning(),
  75. result_listener);
  76. }
  77. // Helper for checking that status.HasWarningReason(reason) == true.
  78. MATCHER_P(HasWarningReason, reason, "") {
  79. CookieInclusionStatus status = arg;
  80. return testing::ExplainMatchResult(true, status.HasWarningReason(reason),
  81. result_listener);
  82. }
  83. // Helper for checking that status.HasExclusionReason(reason) == true.
  84. MATCHER_P(HasExclusionReason, reason, "") {
  85. CookieInclusionStatus status = arg;
  86. return testing::ExplainMatchResult(true, status.HasExclusionReason(reason),
  87. result_listener);
  88. }
  89. // Helper for checking that status.HasExactlyExclusionReasonsForTesting(reasons)
  90. // == true.
  91. MATCHER_P(HasExactlyExclusionReasonsForTesting, reasons, "") {
  92. const CookieInclusionStatus status = arg;
  93. return testing::ExplainMatchResult(
  94. true, status.HasExactlyExclusionReasonsForTesting(reasons),
  95. result_listener);
  96. }
  97. // Helper for checking that status.HasExactlyWarningReasonsForTesting(reasons)
  98. // == true.
  99. MATCHER_P(HasExactlyWarningReasonsForTesting, reasons, "") {
  100. const CookieInclusionStatus status = arg;
  101. return testing::ExplainMatchResult(
  102. true, status.HasExactlyWarningReasonsForTesting(reasons),
  103. result_listener);
  104. }
  105. MATCHER(ShouldWarn, "") {
  106. net::CookieInclusionStatus status = arg;
  107. return testing::ExplainMatchResult(true, status.ShouldWarn(),
  108. result_listener);
  109. }
  110. // Helper for checking CookieAccessResults. Should be called with matchers (or
  111. // values) for each of the fields of a CookieAccessResult.
  112. MATCHER_P4(MatchesCookieAccessResult,
  113. status,
  114. effective_same_site,
  115. access_semantics,
  116. is_allowed_to_access_secure_cookies,
  117. "") {
  118. const CookieAccessResult& car = arg;
  119. return testing::ExplainMatchResult(status, car.status, result_listener) &&
  120. testing::ExplainMatchResult(
  121. effective_same_site, car.effective_same_site, result_listener) &&
  122. testing::ExplainMatchResult(access_semantics, car.access_semantics,
  123. result_listener) &&
  124. testing::ExplainMatchResult(is_allowed_to_access_secure_cookies,
  125. car.is_allowed_to_access_secure_cookies,
  126. result_listener);
  127. }
  128. MATCHER_P3(MatchesCookieAndLineWithAccessResult,
  129. cookie,
  130. line,
  131. access_result,
  132. "") {
  133. const CookieAndLineWithAccessResult& cookie_and_line_with_access_result = arg;
  134. return testing::ExplainMatchResult(cookie,
  135. cookie_and_line_with_access_result.cookie,
  136. result_listener) &&
  137. testing::ExplainMatchResult(
  138. line, cookie_and_line_with_access_result.cookie_string,
  139. result_listener) &&
  140. testing::ExplainMatchResult(
  141. access_result, cookie_and_line_with_access_result.access_result,
  142. result_listener);
  143. }
  144. MATCHER(NameIs, "") {
  145. const std::pair<std::string, std::string>& actual = testing::get<0>(arg);
  146. const std::string& expected_name = testing::get<1>(arg);
  147. return testing::ExplainMatchResult(actual.first, expected_name,
  148. result_listener);
  149. }
  150. MATCHER(CanonicalCookieNameIs, "") {
  151. const net::CanonicalCookie& actual = testing::get<0>(arg);
  152. const std::string& expected_name = testing::get<1>(arg);
  153. return testing::ExplainMatchResult(actual.Name(), expected_name,
  154. result_listener);
  155. }
  156. } // namespace net
  157. #endif // NET_COOKIES_CANONICAL_COOKIE_TEST_HELPERS_H_