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