OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/url_request/url_request_job.h" | 5 #include "net/url_request/url_request_job.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "net/base/request_priority.h" | 10 #include "net/base/request_priority.h" |
11 #include "net/http/http_transaction_test_util.h" | 11 #include "net/http/http_transaction_test_util.h" |
12 #include "net/test/cert_test_util.h" | |
13 #include "net/test/test_data_directory.h" | |
12 #include "net/url_request/url_request.h" | 14 #include "net/url_request/url_request.h" |
13 #include "net/url_request/url_request_test_util.h" | 15 #include "net/url_request/url_request_test_util.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
15 | 17 |
16 namespace net { | 18 namespace net { |
17 | 19 |
18 namespace { | 20 namespace { |
19 | 21 |
20 // Data encoded in kBrotliHelloData. | 22 // Data encoded in kBrotliHelloData. |
21 const char kBrotliDecodedHelloData[] = "hello, world!\n"; | 23 const char kBrotliDecodedHelloData[] = "hello, world!\n"; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
54 response_data->insert(10, 64 * 1024, 'a'); | 56 response_data->insert(10, 64 * 1024, 'a'); |
55 } | 57 } |
56 | 58 |
57 void BrotliHelloServer(const HttpRequestInfo* request, | 59 void BrotliHelloServer(const HttpRequestInfo* request, |
58 std::string* response_status, | 60 std::string* response_status, |
59 std::string* response_headers, | 61 std::string* response_headers, |
60 std::string* response_data) { | 62 std::string* response_data) { |
61 response_data->assign(kBrotliHelloData, sizeof(kBrotliHelloData) - 1); | 63 response_data->assign(kBrotliHelloData, sizeof(kBrotliHelloData) - 1); |
62 } | 64 } |
63 | 65 |
66 void MakeMockReferrerPolicyTransaction(const char* original_location, | |
67 const char* referer_header, | |
68 const char* response_headers, | |
69 MockTransaction* transaction) { | |
70 transaction->url = original_location; | |
71 transaction->method = "GET"; | |
72 transaction->request_time = base::Time(); | |
73 transaction->request_headers = referer_header; | |
74 transaction->load_flags = LOAD_NORMAL; | |
75 transaction->status = "HTTP/1.1 302 Found"; | |
76 transaction->response_headers = response_headers; | |
77 transaction->response_time = base::Time(); | |
78 transaction->data = "hello"; | |
79 transaction->test_mode = TEST_MODE_NORMAL; | |
80 transaction->handler = nullptr; | |
81 if (GURL(original_location).SchemeIsCryptographic()) { | |
82 transaction->cert = | |
83 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); | |
84 } else { | |
85 transaction->cert = nullptr; | |
86 } | |
87 transaction->cert_status = 0; | |
88 transaction->ssl_connection_status = 0; | |
89 transaction->return_code = OK; | |
90 } | |
91 | |
64 const MockTransaction kGZip_Transaction = { | 92 const MockTransaction kGZip_Transaction = { |
65 "http://www.google.com/gzyp", | 93 "http://www.google.com/gzyp", |
66 "GET", | 94 "GET", |
67 base::Time(), | 95 base::Time(), |
68 "", | 96 "", |
69 LOAD_NORMAL, | 97 LOAD_NORMAL, |
70 "HTTP/1.1 200 OK", | 98 "HTTP/1.1 200 OK", |
71 "Cache-Control: max-age=10000\n" | 99 "Cache-Control: max-age=10000\n" |
72 "Content-Encoding: gzip\n" | 100 "Content-Encoding: gzip\n" |
73 "Content-Length: 30\n", // Intentionally wrong. | 101 "Content-Length: 30\n", // Intentionally wrong. |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
226 req->set_method("GET"); | 254 req->set_method("GET"); |
227 req->Start(); | 255 req->Start(); |
228 | 256 |
229 base::RunLoop().Run(); | 257 base::RunLoop().Run(); |
230 | 258 |
231 EXPECT_TRUE(network_layer.done_reading_called()); | 259 EXPECT_TRUE(network_layer.done_reading_called()); |
232 | 260 |
233 RemoveMockTransaction(&kRedirect_Transaction); | 261 RemoveMockTransaction(&kRedirect_Transaction); |
234 } | 262 } |
235 | 263 |
264 TEST(URLRequestJob, RedirectTransactionWithReferrerPolicyHeader) { | |
265 struct TestCase { | |
266 const char* original_location; | |
267 const char* original_referrer; | |
mmenke
2016/06/27 18:38:05
These two are always the same in this test, so I t
estark
2016/06/27 22:18:34
Done. (Changed a test so they're different.)
| |
268 const char* response_headers; | |
269 URLRequest::ReferrerPolicy original_referrer_policy; | |
270 URLRequest::ReferrerPolicy expected_final_referrer_policy; | |
271 const char* expected_final_referrer; | |
272 }; | |
273 | |
274 const TestCase kTests[] = { | |
275 // If a redirect serves 'Referrer-Policy: no-referrer', then the referrer | |
276 // should be cleared. | |
277 {"http://foo.test/one", "http://foo.test/one", | |
mmenke
2016/06/27 18:38:05
A bit of a pain, but I suggest comments labelling
estark
2016/06/27 22:18:34
Done. Though the formatting's a little weird :(
| |
278 "Location: http://foo.test/test\n" | |
279 "Referrer-Policy: no-referrer\n", | |
280 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, | |
281 URLRequest::NEVER_CLEAR_REFERRER, ""}, | |
282 | |
283 // Same as above but for the legacy keyword 'never'. | |
284 {"http://foo.test/one", "http://foo.test/one", | |
285 "Location: http://foo.test/test\nReferrer-Policy: never\n", | |
286 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, | |
287 URLRequest::NEVER_CLEAR_REFERRER, ""}, | |
288 | |
289 // If a redirect serves 'Referrer-Policy: | |
290 // no-referrer-when-downgrade', then the referrer should be cleared | |
291 // on downgrade, even if the original request's policy specified | |
292 // that the referrer should never be cleared. | |
293 {"https://foo.test/one", "https://foo.test/one", | |
294 "Location: http://foo.test\n" | |
295 "Referrer-Policy: no-referrer-when-downgrade\n", | |
296 URLRequest::NEVER_CLEAR_REFERRER, | |
297 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, ""}, | |
298 | |
299 // Same as above but for the legacy keyword 'default'. | |
300 {"https://foo.test/one", "https://foo.test/one", | |
301 "Location: http://foo.test\n" | |
302 "Referrer-Policy: default\n", | |
303 URLRequest::NEVER_CLEAR_REFERRER, | |
304 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, ""}, | |
305 | |
306 // If a redirect serves 'Referrer-Policy: origin', then the referrer | |
307 // should be stripped to its origin, even if the original request's | |
308 // policy specified that the referrer should never be cleared. | |
309 {"https://foo.test/one", "https://foo.test/one", | |
310 "Location: https://foo.test/two\n" | |
311 "Referrer-Policy: origin\n", | |
312 URLRequest::NEVER_CLEAR_REFERRER, URLRequest::NEVER_CLEAR_REFERRER, | |
313 "https://foo.test/"}, | |
314 | |
315 // If a redirect serves 'Referrer-Policy: origin-when-cross-origin', | |
316 // then the referrer should be untouched for a same-origin redirect... | |
317 {"https://foo.test/one", "https://foo.test/one", | |
318 "Location: https://foo.test/two\n" | |
319 "Referrer-Policy: origin-when-cross-origin\n", | |
320 URLRequest::NEVER_CLEAR_REFERRER, | |
321 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, | |
322 "https://foo.test/one"}, | |
323 | |
324 // ... but should be stripped to the origin for a cross-origin redirect. | |
325 {"https://foo.test/one", "https://foo.test/one", | |
326 "Location: https://bar.test/two\n" | |
327 "Referrer-Policy: origin-when-cross-origin\n", | |
328 URLRequest::NEVER_CLEAR_REFERRER, | |
329 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, "https://foo.test/"}, | |
330 | |
331 // If a redirect serves 'Referrer-Policy: unsafe-url', then the | |
332 // referrer should remain, even if originally set to clear on | |
333 // downgrade. | |
334 {"https://foo.test/one", "https://foo.test/one", | |
335 "Location: https://bar.test/two\n" | |
336 "Referrer-Policy: unsafe-url\n", | |
337 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, | |
338 URLRequest::NEVER_CLEAR_REFERRER, "https://foo.test/one"}, | |
339 | |
340 // Same as above but for the legacy keyword 'always'. | |
341 {"https://foo.test/one", "https://foo.test/one", | |
342 "Location: https://bar.test/two\n" | |
343 "Referrer-Policy: always\n", | |
344 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, | |
345 URLRequest::NEVER_CLEAR_REFERRER, "https://foo.test/one"}, | |
346 | |
347 // An invalid keyword should leave the policy untouched. | |
348 {"https://foo.test/one", "https://foo.test/one", | |
349 "Location: https://bar.test/two\n" | |
350 "Referrer-Policy: not-a-valid-policy\n", | |
351 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, | |
352 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, "https://foo.test/"}, | |
353 | |
354 // The last valid keyword should take precedence. | |
355 {"https://foo.test/one", "https://foo.test/one", | |
356 "Location: https://bar.test/two\n" | |
357 "Referrer-Policy: unsafe-url\n" | |
358 "Referrer-Policy: not-a-valid-policy\n", | |
359 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, | |
360 URLRequest::NEVER_CLEAR_REFERRER, "https://foo.test/one"}, | |
361 | |
362 {"https://foo.test/one", "https://foo.test/one", | |
363 "Location: https://bar.test/two\n" | |
364 "Referrer-Policy: unsafe-url\n" | |
365 "Referrer-Policy: origin\n", | |
366 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, | |
367 URLRequest::NEVER_CLEAR_REFERRER, "https://foo.test/"}, | |
368 | |
369 // An empty header should not affect the request. | |
370 {"https://foo.test/one", "https://foo.test/one", | |
371 "Location: https://bar.test/two\n" | |
372 "Referrer-Policy: \n", | |
373 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, | |
374 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, "https://foo.test/"}, | |
375 }; | |
376 | |
377 for (const auto& test : kTests) { | |
378 MockTransaction transaction; | |
379 std::string request_headers = | |
380 "Referer: " + std::string(test.original_referrer) + "\n"; | |
381 MakeMockReferrerPolicyTransaction(test.original_location, | |
382 request_headers.c_str(), | |
383 test.response_headers, &transaction); | |
384 | |
385 MockNetworkLayer network_layer; | |
386 TestURLRequestContext context; | |
387 context.set_enable_referrer_policy_header(true); | |
388 context.set_http_transaction_factory(&network_layer); | |
389 | |
390 TestDelegate d; | |
391 std::unique_ptr<URLRequest> req( | |
392 context.CreateRequest(GURL(transaction.url), DEFAULT_PRIORITY, &d)); | |
393 AddMockTransaction(&transaction); | |
394 | |
395 req->set_referrer_policy(test.original_referrer_policy); | |
396 req->SetReferrer(test.original_referrer); | |
397 | |
398 req->set_method("GET"); | |
399 req->Start(); | |
400 | |
401 base::RunLoop().Run(); | |
402 | |
403 EXPECT_TRUE(network_layer.done_reading_called()); | |
404 | |
405 RemoveMockTransaction(&transaction); | |
406 | |
407 // Test that the referrer policy and referrer were set correctly | |
408 // according to the header received during the redirect. | |
409 EXPECT_EQ(test.expected_final_referrer_policy, req->referrer_policy()); | |
410 EXPECT_EQ(test.expected_final_referrer, req->referrer()); | |
411 } | |
412 } | |
413 | |
236 TEST(URLRequestJob, TransactionNotCachedWhenNetworkDelegateRedirects) { | 414 TEST(URLRequestJob, TransactionNotCachedWhenNetworkDelegateRedirects) { |
237 MockNetworkLayer network_layer; | 415 MockNetworkLayer network_layer; |
238 TestNetworkDelegate network_delegate; | 416 TestNetworkDelegate network_delegate; |
239 network_delegate.set_redirect_on_headers_received_url(GURL("http://foo")); | 417 network_delegate.set_redirect_on_headers_received_url(GURL("http://foo")); |
240 TestURLRequestContext context; | 418 TestURLRequestContext context; |
241 context.set_http_transaction_factory(&network_layer); | 419 context.set_http_transaction_factory(&network_layer); |
242 context.set_network_delegate(&network_delegate); | 420 context.set_network_delegate(&network_delegate); |
243 | 421 |
244 TestDelegate d; | 422 TestDelegate d; |
245 std::unique_ptr<URLRequest> req( | 423 std::unique_ptr<URLRequest> req( |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
351 | 529 |
352 EXPECT_FALSE(d.request_failed()); | 530 EXPECT_FALSE(d.request_failed()); |
353 EXPECT_EQ(200, req->GetResponseCode()); | 531 EXPECT_EQ(200, req->GetResponseCode()); |
354 EXPECT_EQ(kBrotliDecodedHelloData, d.data_received()); | 532 EXPECT_EQ(kBrotliDecodedHelloData, d.data_received()); |
355 EXPECT_TRUE(network_layer.done_reading_called()); | 533 EXPECT_TRUE(network_layer.done_reading_called()); |
356 | 534 |
357 RemoveMockTransaction(&kBrotli_Slow_Transaction); | 535 RemoveMockTransaction(&kBrotli_Slow_Transaction); |
358 } | 536 } |
359 | 537 |
360 } // namespace net | 538 } // namespace net |
OLD | NEW |