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_P2(MatchesCookieWithNameSourceType, name, source_type, "") { 40 return testing::ExplainMatchResult(name, arg.Name(), result_listener) && 41 testing::ExplainMatchResult(source_type, arg.SourceType(), 42 result_listener); 43 } 44 45 MATCHER_P(MatchesCookieAccessWithName, name, "") { 46 return testing::ExplainMatchResult(MatchesCookieWithName(name), arg.cookie, 47 result_listener); 48 } 49 50 // Splits a string into key-value pairs, and executes the provided matcher on 51 // the result. 52 MATCHER_P3(WhenKVSplit, pair_delim, kv_delim, inner_matcher, "") { 53 std::vector<std::pair<std::string, std::string>> pairs; 54 // Return an empty vector when a cookie string (such as "None") cannot be 55 // split into 'name=value' pairs. 56 bool successful_split = 57 base::SplitStringIntoKeyValuePairs(arg, kv_delim, pair_delim, &pairs); 58 if (successful_split) { 59 return testing::ExplainMatchResult(inner_matcher, pairs, result_listener); 60 } else { 61 std::vector<std::pair<std::string, std::string>> empty_pairs; 62 return testing::ExplainMatchResult(inner_matcher, empty_pairs, 63 result_listener); 64 } 65 } 66 67 // Executes the inner_matcher on the Cookie string arg after it's transformed 68 // into a vector. 69 // If the arg is a ';'-delimited string of Cookie 'name=value' or 'name' pairs, 70 // then the matcher will execute on a vector of <name, value> pairs. 71 // If the arg can't be split into these pairs then the inner_matcher will 72 // execute on an empty vector. 73 MATCHER_P(CookieStringIs, inner_matcher, "") { 74 return testing::ExplainMatchResult(WhenKVSplit(';', '=', inner_matcher), arg, 75 result_listener); 76 } 77 78 MATCHER_P2(MatchesCookieWithAccessResult, cookie, access_result, "") { 79 const CookieWithAccessResult& cwar = arg; 80 return testing::ExplainMatchResult(cookie, cwar.cookie, result_listener) && 81 testing::ExplainMatchResult(access_result, cwar.access_result, 82 result_listener); 83 } 84 85 // Helper for checking that status.IsInclude() == true. 86 MATCHER(IsInclude, "") { 87 const CookieInclusionStatus& status = arg; 88 return testing::ExplainMatchResult(true, status.IsInclude(), result_listener); 89 } 90 91 // Helper for checking that status.HasSchemefulDowngradeWarning() == true. 92 MATCHER(HasSchemefulDowngradeWarning, "") { 93 CookieInclusionStatus status = arg; 94 return testing::ExplainMatchResult( 95 true, status.HasSchemefulDowngradeWarning(), result_listener); 96 } 97 98 // Helper for checking that status.HasWarningReason(reason) == true. 99 MATCHER_P(HasWarningReason, reason, "") { 100 CookieInclusionStatus status = arg; 101 return testing::ExplainMatchResult(true, status.HasWarningReason(reason), 102 result_listener); 103 } 104 105 // Helper for checking that status.HasExclusionReason(reason) == true. 106 MATCHER_P(HasExclusionReason, reason, "") { 107 CookieInclusionStatus status = arg; 108 return testing::ExplainMatchResult(true, status.HasExclusionReason(reason), 109 result_listener); 110 } 111 112 // Helper for checking that status.exemption_reason() == reason. 113 MATCHER_P(HasExactlyExemptionReason, reason, "") { 114 CookieInclusionStatus status = arg; 115 return testing::ExplainMatchResult(true, status.exemption_reason() == reason, 116 result_listener); 117 } 118 119 // Helper for checking that status.HasExactlyExclusionReasonsForTesting(reasons) 120 // == true. 121 MATCHER_P(HasExactlyExclusionReasonsForTesting, reasons, "") { 122 const CookieInclusionStatus status = arg; 123 return testing::ExplainMatchResult( 124 true, status.HasExactlyExclusionReasonsForTesting(reasons), 125 result_listener); 126 } 127 128 // Helper for checking that status.HasExactlyWarningReasonsForTesting(reasons) 129 // == true. 130 MATCHER_P(HasExactlyWarningReasonsForTesting, reasons, "") { 131 const CookieInclusionStatus status = arg; 132 return testing::ExplainMatchResult( 133 true, status.HasExactlyWarningReasonsForTesting(reasons), 134 result_listener); 135 } 136 137 MATCHER(ShouldWarn, "") { 138 net::CookieInclusionStatus status = arg; 139 return testing::ExplainMatchResult(true, status.ShouldWarn(), 140 result_listener); 141 } 142 143 // Helper for checking CookieAccessResults. Should be called with matchers (or 144 // values) for each of the fields of a CookieAccessResult. 145 MATCHER_P4(MatchesCookieAccessResult, 146 status, 147 effective_same_site, 148 access_semantics, 149 is_allowed_to_access_secure_cookies, 150 "") { 151 const CookieAccessResult& car = arg; 152 return testing::ExplainMatchResult(status, car.status, result_listener) && 153 testing::ExplainMatchResult( 154 effective_same_site, car.effective_same_site, result_listener) && 155 testing::ExplainMatchResult(access_semantics, car.access_semantics, 156 result_listener) && 157 testing::ExplainMatchResult(is_allowed_to_access_secure_cookies, 158 car.is_allowed_to_access_secure_cookies, 159 result_listener); 160 } 161 162 MATCHER_P3(MatchesCookieAndLineWithAccessResult, 163 cookie, 164 line, 165 access_result, 166 "") { 167 const CookieAndLineWithAccessResult& cookie_and_line_with_access_result = arg; 168 return testing::ExplainMatchResult(cookie, 169 cookie_and_line_with_access_result.cookie, 170 result_listener) && 171 testing::ExplainMatchResult( 172 line, cookie_and_line_with_access_result.cookie_string, 173 result_listener) && 174 testing::ExplainMatchResult( 175 access_result, cookie_and_line_with_access_result.access_result, 176 result_listener); 177 } 178 179 MATCHER(NameIs, "") { 180 const std::pair<std::string, std::string>& actual = testing::get<0>(arg); 181 const std::string& expected_name = testing::get<1>(arg); 182 return testing::ExplainMatchResult(actual.first, expected_name, 183 result_listener); 184 } 185 186 MATCHER(CanonicalCookieNameIs, "") { 187 const net::CanonicalCookie& actual = testing::get<0>(arg); 188 const std::string& expected_name = testing::get<1>(arg); 189 return testing::ExplainMatchResult(actual.Name(), expected_name, 190 result_listener); 191 } 192 193 } // namespace net 194 195 #endif // NET_COOKIES_CANONICAL_COOKIE_TEST_HELPERS_H_ 196