Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: ios/web/public/referrer_util_unittest.cc

Issue 2918313002: Implement new referrer policies (Closed)
Patch Set: update public/platform/OWNERS per presubmit Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ios/web/public/referrer_util.cc ('k') | net/url_request/url_request.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ios/web/public/referrer_util.h" 5 #include "ios/web/public/referrer_util.h"
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "ios/web/public/referrer.h" 8 #include "ios/web/public/referrer.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "url/gurl.h" 10 #include "url/gurl.h"
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 // Full URL for the same origin, and origin for all other cases (even 121 // Full URL for the same origin, and origin for all other cases (even
122 // secure->insecure). 122 // secure->insecure).
123 if (source_url.GetOrigin() == dest_url.GetOrigin()) 123 if (source_url.GetOrigin() == dest_url.GetOrigin())
124 EXPECT_EQ(source_url.GetAsReferrer().spec(), value); 124 EXPECT_EQ(source_url.GetAsReferrer().spec(), value);
125 else 125 else
126 EXPECT_EQ(source_url.GetOrigin().spec(), value); 126 EXPECT_EQ(source_url.GetOrigin().spec(), value);
127 } 127 }
128 } 128 }
129 } 129 }
130 130
131 // Tests that the same-origin policy works as expected.
132 TEST(ReferrerUtilTest, SameOriginPolicy) {
133 for (unsigned int source = 0; source < arraysize(kTestUrls); ++source) {
134 for (unsigned int dest = 1; dest < arraysize(kTestUrls); ++dest) {
135 GURL source_url(kTestUrls[source]);
136 GURL dest_url(kTestUrls[dest]);
137 Referrer referrer(source_url, ReferrerPolicySameOrigin);
138 std::string value = ReferrerHeaderValueForNavigation(dest_url, referrer);
139
140 // Full URL for the same origin, and nothing for all other cases.
141 if (source_url.GetOrigin() == dest_url.GetOrigin())
142 EXPECT_EQ(source_url.GetAsReferrer().spec(), value);
143 else
144 EXPECT_EQ(std::string(), value);
145 }
146 }
147 }
148
149 // Tests that the strict-origin policy works as expected.
150 TEST(ReferrerUtilTest, StrictOriginPolicy) {
151 for (unsigned int source = 0; source < arraysize(kTestUrls); ++source) {
152 for (unsigned int dest = 1; dest < arraysize(kTestUrls); ++dest) {
153 GURL source_url(kTestUrls[source]);
154 GURL dest_url(kTestUrls[dest]);
155 Referrer referrer(source_url, ReferrerPolicyStrictOrigin);
156 std::string value = ReferrerHeaderValueForNavigation(dest_url, referrer);
157
158 // No referrer when downgrading, and origin otherwise.
159 if (source_url.SchemeIsCryptographic() &&
160 !dest_url.SchemeIsCryptographic())
161 EXPECT_EQ("", value);
162 else
163 EXPECT_EQ(source_url.GetOrigin().spec(), value);
164 }
165 }
166 }
167
168 // Tests that the strict-origin-when-cross-origin policy works as expected.
169 TEST(ReferrerUtilTest, StrictOriginWhenCrossOriginPolicy) {
170 for (unsigned int source = 0; source < arraysize(kTestUrls); ++source) {
171 for (unsigned int dest = 1; dest < arraysize(kTestUrls); ++dest) {
172 GURL source_url(kTestUrls[source]);
173 GURL dest_url(kTestUrls[dest]);
174 Referrer referrer(source_url, ReferrerPolicyStrictOriginWhenCrossOrigin);
175 std::string value = ReferrerHeaderValueForNavigation(dest_url, referrer);
176
177 // No referrer when downgrading, origin when cross-origin but not
178 // downgrading, and full referrer otherwise.
179 if (source_url.SchemeIsCryptographic() &&
180 !dest_url.SchemeIsCryptographic())
181 EXPECT_EQ("", value);
182 else if (source_url.GetOrigin() == dest_url.GetOrigin())
183 EXPECT_EQ(source_url.GetAsReferrer().spec(), value);
184 else
185 EXPECT_EQ(source_url.GetOrigin().spec(), value);
186 }
187 }
188 }
189
131 // Tests that PolicyForNavigation gives the right values. 190 // Tests that PolicyForNavigation gives the right values.
132 TEST(ReferrerUtilTest, PolicyForNavigation) { 191 TEST(ReferrerUtilTest, PolicyForNavigation) {
133 // The request and destination URLs are unused in the current implementation, 192 // The request and destination URLs are unused in the current implementation,
134 // so use a dummy value. 193 // so use a dummy value.
135 GURL dummy_url; 194 GURL dummy_url;
136 for (unsigned int policy = 0; policy <= ReferrerPolicyLast; ++policy) { 195 for (unsigned int policy = 0; policy <= ReferrerPolicyLast; ++policy) {
137 Referrer referrer(dummy_url, static_cast<ReferrerPolicy>(policy)); 196 Referrer referrer(dummy_url, static_cast<ReferrerPolicy>(policy));
138 net::URLRequest::ReferrerPolicy net_request_policy = 197 net::URLRequest::ReferrerPolicy net_request_policy =
139 PolicyForNavigation(dummy_url, referrer); 198 PolicyForNavigation(dummy_url, referrer);
140 // The test here is deliberately backward from the way the test would 199 // The test here is deliberately backward from the way the test would
141 // intuitively work so that it's structured differently from the code it's 200 // intuitively work so that it's structured differently from the code it's
142 // testing, and thus less likely to have a copy/paste bug that passes 201 // testing, and thus less likely to have a copy/paste bug that passes
143 // incorrect mappings. 202 // incorrect mappings.
144 switch (net_request_policy) { 203 switch (net_request_policy) {
145 case net::URLRequest:: 204 case net::URLRequest::
146 REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN:
147 // Nothing currently maps to this policy on iOS.
148 FAIL();
149 break;
150 case net::URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN:
151 EXPECT_EQ(ReferrerPolicyOriginWhenCrossOrigin, policy);
152 break;
153 case net::URLRequest::NEVER_CLEAR_REFERRER:
154 // This request policy should be used when the referrer policy is always
155 // the same regardless of source and destination.
156 EXPECT_TRUE(policy == ReferrerPolicyAlways ||
157 policy == ReferrerPolicyNever ||
158 policy == ReferrerPolicyOrigin);
159 break;
160 case net::URLRequest::
161 CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE: 205 CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE:
162 // This corresponds directly to ReferrerPolicyNoReferrerWhenDowngrade, 206 // This corresponds directly to ReferrerPolicyNoReferrerWhenDowngrade,
163 // which is also how Default works on iOS. 207 // which is also how Default works on iOS.
164 EXPECT_TRUE(policy == ReferrerPolicyDefault || 208 EXPECT_TRUE(policy == ReferrerPolicyDefault ||
165 policy == ReferrerPolicyNoReferrerWhenDowngrade); 209 policy == ReferrerPolicyNoReferrerWhenDowngrade);
166 break; 210 break;
211 case net::URLRequest::
212 REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN:
213 EXPECT_EQ(ReferrerPolicyStrictOriginWhenCrossOrigin, policy);
214 break;
215 case net::URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN:
216 EXPECT_EQ(ReferrerPolicyOriginWhenCrossOrigin, policy);
217 break;
218 case net::URLRequest::NEVER_CLEAR_REFERRER:
219 EXPECT_EQ(ReferrerPolicyAlways, policy);
220 break;
167 case net::URLRequest::ORIGIN: 221 case net::URLRequest::ORIGIN:
168 EXPECT_TRUE(policy == ReferrerPolicyOrigin); 222 EXPECT_EQ(ReferrerPolicyOrigin, policy);
223 break;
224 case net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN:
225 EXPECT_EQ(ReferrerPolicySameOrigin, policy);
226 break;
227 case net::URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE:
228 EXPECT_EQ(ReferrerPolicyStrictOrigin, policy);
169 break; 229 break;
170 case net::URLRequest::NO_REFERRER: 230 case net::URLRequest::NO_REFERRER:
171 EXPECT_TRUE(policy == ReferrerPolicyNever); 231 EXPECT_EQ(ReferrerPolicyNever, policy);
172 break; 232 break;
173 case net::URLRequest::MAX_REFERRER_POLICY: 233 case net::URLRequest::MAX_REFERRER_POLICY:
174 FAIL(); 234 FAIL();
175 break; 235 break;
176 } 236 }
177 } 237 }
178 } 238 }
179 239
180 // Tests that all the strings corresponding to web::ReferrerPolicy values are 240 // Tests that all the strings corresponding to web::ReferrerPolicy values are
181 // correctly handled. 241 // correctly handled.
182 TEST(ReferrerUtilTest, PolicyFromString) { 242 TEST(ReferrerUtilTest, PolicyFromString) {
183 // The ordering here must match web::ReferrerPolicy; this makes the test 243 // The ordering here must match web::ReferrerPolicy; this makes the test
184 // simpler, at the cost of needing to re-order if the enum is re-ordered. 244 // simpler, at the cost of needing to re-order if the enum is re-ordered.
185 const char* const kPolicyStrings[] = { 245 const char* const kPolicyStrings[] = {
186 "unsafe-url", 246 "unsafe-url",
187 nullptr, // Default is skipped, because no string maps to Default. 247 nullptr, // Default is skipped, because no string maps to Default.
188 "no-referrer-when-downgrade", 248 "no-referrer-when-downgrade",
189 "no-referrer", 249 "no-referrer",
190 "origin", 250 "origin",
191 "origin-when-cross-origin", 251 "origin-when-cross-origin",
252 "same-origin",
253 "strict-origin",
254 "strict-origin-when-cross-origin",
192 }; 255 };
193 // Test that all the values are supported. 256 // Test that all the values are supported.
194 for (int i = 0; i < ReferrerPolicyLast; ++i) { 257 for (int i = 0; i < ReferrerPolicyLast; ++i) {
195 if (!kPolicyStrings[i]) 258 if (!kPolicyStrings[i])
196 continue; 259 continue;
197 EXPECT_EQ(i, ReferrerPolicyFromString(kPolicyStrings[i])); 260 EXPECT_EQ(i, ReferrerPolicyFromString(kPolicyStrings[i]));
198 } 261 }
199 262
200 // Verify that if something is added to the enum, its string value gets added 263 // Verify that if something is added to the enum, its string value gets added
201 // to the mapping function. 264 // to the mapping function.
202 EXPECT_EQ(ReferrerPolicyLast + 1, 265 EXPECT_EQ(ReferrerPolicyLast + 1,
203 static_cast<int>(arraysize(kPolicyStrings))); 266 static_cast<int>(arraysize(kPolicyStrings)));
204 267
205 // Test the legacy policy names. 268 // Test the legacy policy names.
206 EXPECT_EQ(ReferrerPolicyNever, ReferrerPolicyFromString("never")); 269 EXPECT_EQ(ReferrerPolicyNever, ReferrerPolicyFromString("never"));
207 // Note that per the spec, "default" maps to NoReferrerWhenDowngrade; the 270 // Note that per the spec, "default" maps to NoReferrerWhenDowngrade; the
208 // Default enum value is not actually a spec'd value. 271 // Default enum value is not actually a spec'd value.
209 EXPECT_EQ(ReferrerPolicyNoReferrerWhenDowngrade, 272 EXPECT_EQ(ReferrerPolicyNoReferrerWhenDowngrade,
210 ReferrerPolicyFromString("default")); 273 ReferrerPolicyFromString("default"));
211 EXPECT_EQ(ReferrerPolicyAlways, ReferrerPolicyFromString("always")); 274 EXPECT_EQ(ReferrerPolicyAlways, ReferrerPolicyFromString("always"));
212 275
213 // Test that invalid values map to Never. 276 // Test that invalid values map to Default.
214 EXPECT_EQ(ReferrerPolicyNever, ReferrerPolicyFromString("")); 277 EXPECT_EQ(ReferrerPolicyDefault, ReferrerPolicyFromString(""));
215 EXPECT_EQ(ReferrerPolicyNever, ReferrerPolicyFromString("made-up")); 278 EXPECT_EQ(ReferrerPolicyDefault, ReferrerPolicyFromString("made-up"));
216 } 279 }
217 280
218 } // namespace web 281 } // namespace web
OLDNEW
« no previous file with comments | « ios/web/public/referrer_util.cc ('k') | net/url_request/url_request.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698