| // Copyright 2012 The Chromium Authors |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "net/url_request/url_request_job.h" |
| |
| #include <memory> |
| |
| #include "base/run_loop.h" |
| #include "base/test/bind.h" |
| #include "base/test/scoped_feature_list.h" |
| #include "net/base/features.h" |
| #include "net/base/request_priority.h" |
| #include "net/http/http_transaction_test_util.h" |
| #include "net/test/cert_test_util.h" |
| #include "net/test/gtest_util.h" |
| #include "net/test/test_data_directory.h" |
| #include "net/test/test_with_task_environment.h" |
| #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
| #include "net/url_request/referrer_policy.h" |
| #include "net/url_request/url_request.h" |
| #include "net/url_request/url_request_context.h" |
| #include "net/url_request/url_request_context_builder.h" |
| #include "net/url_request/url_request_test_util.h" |
| #include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| #include "third_party/abseil-cpp/absl/types/optional.h" |
| #include "url/url_util.h" |
| |
| using net::test::IsError; |
| using net::test::IsOk; |
| |
| namespace net { |
| |
| namespace { |
| |
| // Data encoded in kBrotliHelloData. |
| const char kHelloData[] = "hello, world!\n"; |
| // kHelloData encoded with brotli. |
| const char kBrotliHelloData[] = |
| "\033\015\0\0\244\024\102\152\020\111\152\072\235\126\034"; |
| |
| // This is a header that signals the end of the data. |
| const char kGzipData[] = "\x1f\x08b\x08\0\0\0\0\0\0\3\3\0\0\0\0\0\0\0\0"; |
| const char kGzipDataWithName[] = |
| "\x1f\x08b\x08\x08\0\0\0\0\0\0name\0\3\0\0\0\0\0\0\0\0"; |
| // kHelloData encoded with gzip. |
| const char kGzipHelloData[] = |
| "\x1f\x8b\x08\x08\x46\x7d\x4e\x56\x00\x03\x67\x7a\x69\x70\x2e\x74\x78\x74" |
| "\x00\xcb\x48\xcd\xc9\xc9\xe7\x02\x00\x20\x30\x3a\x36\x06\x00\x00\x00"; |
| |
| void GZipServer(const HttpRequestInfo* request, |
| std::string* response_status, |
| std::string* response_headers, |
| std::string* response_data) { |
| response_data->assign(kGzipData, sizeof(kGzipData)); |
| } |
| |
| void GZipHelloServer(const HttpRequestInfo* request, |
| std::string* response_status, |
| std::string* response_headers, |
| std::string* response_data) { |
| response_data->assign(kGzipHelloData, sizeof(kGzipHelloData) - 1); |
| } |
| |
| void BigGZipServer(const HttpRequestInfo* request, |
| std::string* response_status, |
| std::string* response_headers, |
| std::string* response_data) { |
| response_data->assign(kGzipDataWithName, sizeof(kGzipDataWithName)); |
| response_data->insert(10, 64 * 1024, 'a'); |
| } |
| |
| void BrotliHelloServer(const HttpRequestInfo* request, |
| std::string* response_status, |
| std::string* response_headers, |
| std::string* response_data) { |
| response_data->assign(kBrotliHelloData, sizeof(kBrotliHelloData) - 1); |
| } |
| |
| void MakeMockReferrerPolicyTransaction(const char* original_url, |
| const char* referer_header, |
| const char* response_headers, |
| MockTransaction* transaction) { |
| transaction->url = original_url; |
| transaction->method = "GET"; |
| transaction->request_time = base::Time(); |
| transaction->request_headers = referer_header; |
| transaction->load_flags = LOAD_NORMAL; |
| transaction->status = "HTTP/1.1 302 Found"; |
| transaction->response_headers = response_headers; |
| transaction->response_time = base::Time(); |
| transaction->data = "hello"; |
| transaction->dns_aliases = {}; |
| transaction->test_mode = TEST_MODE_NORMAL; |
| transaction->handler = nullptr; |
| transaction->read_handler = nullptr; |
| if (GURL(original_url).SchemeIsCryptographic()) { |
| transaction->cert = |
| net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); |
| } else { |
| transaction->cert = nullptr; |
| } |
| transaction->cert_status = 0; |
| transaction->ssl_connection_status = 0; |
| transaction->start_return_code = OK; |
| } |
| |
| const MockTransaction kNoFilterTransaction = { |
| "http://www.google.com/gzyp", |
| "GET", |
| base::Time(), |
| "", |
| LOAD_NORMAL, |
| DefaultTransportInfo(), |
| "HTTP/1.1 200 OK", |
| "Cache-Control: max-age=10000\n" |
| "Content-Length: 30\n", // Intentionally wrong. |
| base::Time(), |
| "hello", |
| {}, |
| absl::nullopt, |
| absl::nullopt, |
| TEST_MODE_NORMAL, |
| nullptr, |
| nullptr, |
| nullptr, |
| 0, |
| OK, |
| OK, |
| }; |
| |
| const MockTransaction kNoFilterTransactionWithInvalidLength = { |
| "http://www.google.com/gzyp", |
| "GET", |
| base::Time(), |
| "", |
| LOAD_NORMAL, |
| DefaultTransportInfo(), |
| "HTTP/1.1 200 OK", |
| "Cache-Control: max-age=10000\n" |
| "Content-Length: +30\n", // Invalid |
| base::Time(), |
| "hello", |
| {}, |
| absl::nullopt, |
| absl::nullopt, |
| TEST_MODE_NORMAL, |
| nullptr, |
| nullptr, |
| nullptr, |
| 0, |
| OK, |
| OK, |
| }; |
| |
| const MockTransaction kGZipTransaction = { |
| "http://www.google.com/gzyp", |
| "GET", |
| base::Time(), |
| "", |
| LOAD_NORMAL, |
| DefaultTransportInfo(), |
| "HTTP/1.1 200 OK", |
| "Cache-Control: max-age=10000\n" |
| "Content-Encoding: gzip\n" |
| "Content-Length: 30\n", // Intentionally wrong. |
| base::Time(), |
| "", |
| {}, |
| absl::nullopt, |
| absl::nullopt, |
| TEST_MODE_NORMAL, |
| &GZipServer, |
| nullptr, |
| nullptr, |
| 0, |
| 0, |
| OK, |
| OK, |
| }; |
| |
| const MockTransaction kGzipSlowTransaction = { |
| "http://www.google.com/gzyp", |
| "GET", |
| base::Time(), |
| "", |
| LOAD_NORMAL, |
| DefaultTransportInfo(), |
| "HTTP/1.1 200 OK", |
| "Cache-Control: max-age=10000\n" |
| "Content-Encoding: gzip\n", |
| base::Time(), |
| "", |
| {}, |
| absl::nullopt, |
| absl::nullopt, |
| TEST_MODE_SLOW_READ, |
| &GZipHelloServer, |
| nullptr, |
| nullptr, |
| 0, |
| 0, |
| OK, |
| OK, |
| }; |
| |
| const MockTransaction kRedirectTransaction = { |
| "http://www.google.com/redirect", |
| "GET", |
| base::Time(), |
| "", |
| LOAD_NORMAL, |
| DefaultTransportInfo(), |
| "HTTP/1.1 302 Found", |
| "Cache-Control: max-age=10000\n" |
| "Location: http://www.google.com/destination\n" |
| "Content-Length: 5\n", |
| base::Time(), |
| "hello", |
| {}, |
| absl::nullopt, |
| absl::nullopt, |
| TEST_MODE_NORMAL, |
| nullptr, |
| nullptr, |
| nullptr, |
| 0, |
| 0, |
| OK, |
| OK, |
| }; |
| |
| const MockTransaction kEmptyBodyGzipTransaction = { |
| "http://www.google.com/empty_body", |
| "GET", |
| base::Time(), |
| "", |
| LOAD_NORMAL, |
| DefaultTransportInfo(), |
| "HTTP/1.1 200 OK", |
| "Content-Encoding: gzip\n", |
| base::Time(), |
| "", |
| {}, |
| absl::nullopt, |
| absl::nullopt, |
| TEST_MODE_NORMAL, |
| nullptr, |
| nullptr, |
| nullptr, |
| 0, |
| 0, |
| OK, |
| OK, |
| }; |
| |
| const MockTransaction kInvalidContentGZipTransaction = { |
| "http://www.google.com/gzyp", |
| "GET", |
| base::Time(), |
| "", |
| LOAD_NORMAL, |
| DefaultTransportInfo(), |
| "HTTP/1.1 200 OK", |
| "Content-Encoding: gzip\n" |
| "Content-Length: 21\n", |
| base::Time(), |
| "not a valid gzip body", |
| {}, |
| absl::nullopt, |
| absl::nullopt, |
| TEST_MODE_NORMAL, |
| nullptr, |
| nullptr, |
| nullptr, |
| 0, |
| 0, |
| OK, |
| OK, |
| }; |
| |
| const MockTransaction kBrotliSlowTransaction = { |
| "http://www.google.com/brotli", |
| "GET", |
| base::Time(), |
| "", |
| LOAD_NORMAL, |
| DefaultTransportInfo(), |
| "HTTP/1.1 200 OK", |
| "Cache-Control: max-age=10000\n" |
| "Content-Encoding: br\n" |
| "Content-Length: 230\n", // Intentionally wrong. |
| base::Time(), |
| "", |
| {}, |
| absl::nullopt, |
| absl::nullopt, |
| TEST_MODE_SLOW_READ, |
| &BrotliHelloServer, |
| nullptr, |
| nullptr, |
| 0, |
| 0, |
| OK, |
| OK, |
| }; |
| |
| } // namespace |
| |
| using URLRequestJobTest = TestWithTaskEnvironment; |
| |
| TEST_F(URLRequestJobTest, TransactionNoFilter) { |
| auto context_builder = CreateTestURLRequestContextBuilder(); |
| auto* network_layer = context_builder->SetHttpTransactionFactoryForTesting( |
| std::make_unique<MockNetworkLayer>()); |
| context_builder->DisableHttpCache(); |
| auto context = context_builder->Build(); |
| |
| TestDelegate d; |
| std::unique_ptr<URLRequest> req( |
| context->CreateRequest(GURL(kNoFilterTransaction.url), DEFAULT_PRIORITY, |
| &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| AddMockTransaction(&kNoFilterTransaction); |
| |
| req->set_method("GET"); |
| req->Start(); |
| |
| d.RunUntilComplete(); |
| |
| EXPECT_FALSE(d.request_failed()); |
| EXPECT_EQ(200, req->GetResponseCode()); |
| EXPECT_EQ("hello", d.data_received()); |
| EXPECT_TRUE(network_layer->done_reading_called()); |
| // When there's no filter and a Content-Length, expected content size should |
| // be available. |
| EXPECT_EQ(30, req->GetExpectedContentSize()); |
| |
| RemoveMockTransaction(&kNoFilterTransaction); |
| } |
| |
| TEST_F(URLRequestJobTest, TransactionNoFilterWithInvalidLength) { |
| auto context_builder = CreateTestURLRequestContextBuilder(); |
| auto* network_layer = context_builder->SetHttpTransactionFactoryForTesting( |
| std::make_unique<MockNetworkLayer>()); |
| context_builder->DisableHttpCache(); |
| auto context = context_builder->Build(); |
| |
| TestDelegate d; |
| std::unique_ptr<URLRequest> req(context->CreateRequest( |
| GURL(kNoFilterTransactionWithInvalidLength.url), DEFAULT_PRIORITY, &d, |
| TRAFFIC_ANNOTATION_FOR_TESTS)); |
| AddMockTransaction(&kNoFilterTransactionWithInvalidLength); |
| |
| req->set_method("GET"); |
| req->Start(); |
| |
| d.RunUntilComplete(); |
| |
| EXPECT_FALSE(d.request_failed()); |
| EXPECT_EQ(200, req->GetResponseCode()); |
| EXPECT_EQ("hello", d.data_received()); |
| EXPECT_TRUE(network_layer->done_reading_called()); |
| // Invalid Content-Lengths that start with a + should not be reported. |
| EXPECT_EQ(-1, req->GetExpectedContentSize()); |
| |
| RemoveMockTransaction(&kNoFilterTransactionWithInvalidLength); |
| } |
| |
| TEST_F(URLRequestJobTest, TransactionNotifiedWhenDone) { |
| auto context_builder = CreateTestURLRequestContextBuilder(); |
| auto* network_layer = context_builder->SetHttpTransactionFactoryForTesting( |
| std::make_unique<MockNetworkLayer>()); |
| context_builder->DisableHttpCache(); |
| auto context = context_builder->Build(); |
| |
| TestDelegate d; |
| std::unique_ptr<URLRequest> req( |
| context->CreateRequest(GURL(kGZipTransaction.url), DEFAULT_PRIORITY, &d, |
| TRAFFIC_ANNOTATION_FOR_TESTS)); |
| AddMockTransaction(&kGZipTransaction); |
| |
| req->set_method("GET"); |
| req->Start(); |
| |
| d.RunUntilComplete(); |
| |
| EXPECT_TRUE(d.response_completed()); |
| EXPECT_EQ(OK, d.request_status()); |
| EXPECT_EQ(200, req->GetResponseCode()); |
| EXPECT_EQ("", d.data_received()); |
| EXPECT_TRUE(network_layer->done_reading_called()); |
| // When there's a filter and a Content-Length, expected content size should |
| // not be available. |
| EXPECT_EQ(-1, req->GetExpectedContentSize()); |
| |
| RemoveMockTransaction(&kGZipTransaction); |
| } |
| |
| TEST_F(URLRequestJobTest, SyncTransactionNotifiedWhenDone) { |
| auto context_builder = CreateTestURLRequestContextBuilder(); |
| auto* network_layer = context_builder->SetHttpTransactionFactoryForTesting( |
| std::make_unique<MockNetworkLayer>()); |
| context_builder->DisableHttpCache(); |
| auto context = context_builder->Build(); |
| |
| TestDelegate d; |
| std::unique_ptr<URLRequest> req( |
| context->CreateRequest(GURL(kGZipTransaction.url), DEFAULT_PRIORITY, &d, |
| TRAFFIC_ANNOTATION_FOR_TESTS)); |
| MockTransaction transaction(kGZipTransaction); |
| transaction.test_mode = TEST_MODE_SYNC_ALL; |
| AddMockTransaction(&transaction); |
| |
| req->set_method("GET"); |
| req->Start(); |
| |
| d.RunUntilComplete(); |
| |
| EXPECT_TRUE(d.response_completed()); |
| EXPECT_EQ(OK, d.request_status()); |
| EXPECT_EQ(200, req->GetResponseCode()); |
| EXPECT_EQ("", d.data_received()); |
| EXPECT_TRUE(network_layer->done_reading_called()); |
| // When there's a filter and a Content-Length, expected content size should |
| // not be available. |
| EXPECT_EQ(-1, req->GetExpectedContentSize()); |
| |
| RemoveMockTransaction(&transaction); |
| } |
| |
| // Tests processing a large gzip header one byte at a time. |
| TEST_F(URLRequestJobTest, SyncSlowTransaction) { |
| auto context_builder = CreateTestURLRequestContextBuilder(); |
| auto* network_layer = context_builder->SetHttpTransactionFactoryForTesting( |
| std::make_unique<MockNetworkLayer>()); |
| context_builder->DisableHttpCache(); |
| auto context = context_builder->Build(); |
| |
| TestDelegate d; |
| std::unique_ptr<URLRequest> req( |
| context->CreateRequest(GURL(kGZipTransaction.url), DEFAULT_PRIORITY, &d, |
| TRAFFIC_ANNOTATION_FOR_TESTS)); |
| MockTransaction transaction(kGZipTransaction); |
| transaction.test_mode = TEST_MODE_SYNC_ALL | TEST_MODE_SLOW_READ; |
| transaction.handler = &BigGZipServer; |
| AddMockTransaction(&transaction); |
| |
| req->set_method("GET"); |
| req->Start(); |
| |
| d.RunUntilComplete(); |
| |
| EXPECT_TRUE(d.response_completed()); |
| EXPECT_EQ(OK, d.request_status()); |
| EXPECT_EQ(200, req->GetResponseCode()); |
| EXPECT_EQ("", d.data_received()); |
| EXPECT_TRUE(network_layer->done_reading_called()); |
| EXPECT_EQ(-1, req->GetExpectedContentSize()); |
| |
| RemoveMockTransaction(&transaction); |
| } |
| |
| TEST_F(URLRequestJobTest, RedirectTransactionNotifiedWhenDone) { |
| auto context_builder = CreateTestURLRequestContextBuilder(); |
| auto* network_layer = context_builder->SetHttpTransactionFactoryForTesting( |
| std::make_unique<MockNetworkLayer>()); |
| context_builder->DisableHttpCache(); |
| auto context = context_builder->Build(); |
| |
| TestDelegate d; |
| std::unique_ptr<URLRequest> req( |
| context->CreateRequest(GURL(kRedirectTransaction.url), DEFAULT_PRIORITY, |
| &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| AddMockTransaction(&kRedirectTransaction); |
| |
| req->set_method("GET"); |
| req->Start(); |
| |
| d.RunUntilComplete(); |
| |
| EXPECT_TRUE(network_layer->done_reading_called()); |
| |
| RemoveMockTransaction(&kRedirectTransaction); |
| } |
| |
| TEST_F(URLRequestJobTest, RedirectTransactionWithReferrerPolicyHeader) { |
| struct TestCase { |
| const char* original_url; |
| const char* original_referrer; |
| const char* response_headers; |
| ReferrerPolicy original_referrer_policy; |
| ReferrerPolicy expected_final_referrer_policy; |
| const char* expected_final_referrer; |
| }; |
| |
| // Note: There are more thorough test cases in RedirectInfoTest. |
| const TestCase kTests[] = { |
| // If a redirect serves 'Referrer-Policy: no-referrer', then the referrer |
| // should be cleared. |
| {"http://foo.test/one" /* original url */, |
| "http://foo.test/one" /* original referrer */, |
| "Location: http://foo.test/test\n" |
| "Referrer-Policy: no-referrer\n", |
| // original policy |
| ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| ReferrerPolicy::NO_REFERRER /* expected final policy */, |
| "" /* expected final referrer */}, |
| |
| // A redirect response without Referrer-Policy header should not affect |
| // the policy and the referrer. |
| {"http://foo.test/one" /* original url */, |
| "http://foo.test/one" /* original referrer */, |
| "Location: http://foo.test/test\n", |
| // original policy |
| ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| // expected final policy |
| ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| "http://foo.test/one" /* expected final referrer */}, |
| }; |
| |
| for (const auto& test : kTests) { |
| MockTransaction transaction; |
| std::string request_headers = |
| "Referer: " + std::string(test.original_referrer) + "\n"; |
| MakeMockReferrerPolicyTransaction(test.original_url, |
| request_headers.c_str(), |
| test.response_headers, &transaction); |
| |
| auto context_builder = CreateTestURLRequestContextBuilder(); |
| auto* network_layer = context_builder->SetHttpTransactionFactoryForTesting( |
| std::make_unique<MockNetworkLayer>()); |
| context_builder->DisableHttpCache(); |
| auto context = context_builder->Build(); |
| |
| TestDelegate d; |
| std::unique_ptr<URLRequest> req( |
| context->CreateRequest(GURL(transaction.url), DEFAULT_PRIORITY, &d, |
| TRAFFIC_ANNOTATION_FOR_TESTS)); |
| AddMockTransaction(&transaction); |
| |
| req->set_referrer_policy(test.original_referrer_policy); |
| req->SetReferrer(test.original_referrer); |
| |
| req->set_method("GET"); |
| req->Start(); |
| |
| d.RunUntilComplete(); |
| |
| EXPECT_TRUE(network_layer->done_reading_called()); |
| |
| RemoveMockTransaction(&transaction); |
| |
| // Test that the referrer policy and referrer were set correctly |
| // according to the header received during the redirect. |
| EXPECT_EQ(test.expected_final_referrer_policy, req->referrer_policy()); |
| EXPECT_EQ(test.expected_final_referrer, req->referrer()); |
| } |
| } |
| |
| TEST_F(URLRequestJobTest, TransactionNotCachedWhenNetworkDelegateRedirects) { |
| auto context_builder = CreateTestURLRequestContextBuilder(); |
| auto* network_layer = context_builder->SetHttpTransactionFactoryForTesting( |
| std::make_unique<MockNetworkLayer>()); |
| auto network_delegate = std::make_unique<TestNetworkDelegate>(); |
| network_delegate->set_redirect_on_headers_received_url(GURL("http://foo")); |
| context_builder->DisableHttpCache(); |
| context_builder->set_network_delegate(std::move(network_delegate)); |
| auto context = context_builder->Build(); |
| |
| TestDelegate d; |
| std::unique_ptr<URLRequest> req( |
| context->CreateRequest(GURL(kGZipTransaction.url), DEFAULT_PRIORITY, &d, |
| TRAFFIC_ANNOTATION_FOR_TESTS)); |
| AddMockTransaction(&kGZipTransaction); |
| |
| req->set_method("GET"); |
| req->Start(); |
| |
| d.RunUntilComplete(); |
| |
| EXPECT_TRUE(network_layer->stop_caching_called()); |
| |
| RemoveMockTransaction(&kGZipTransaction); |
| } |
| |
| // Makes sure that ReadRawDataComplete correctly updates request status before |
| // calling ReadFilteredData. |
| // Regression test for crbug.com/553300. |
| TEST_F(URLRequestJobTest, EmptyBodySkipFilter) { |
| auto context_builder = CreateTestURLRequestContextBuilder(); |
| auto* network_layer = context_builder->SetHttpTransactionFactoryForTesting( |
| std::make_unique<MockNetworkLayer>()); |
| context_builder->DisableHttpCache(); |
| auto context = context_builder->Build(); |
| |
| TestDelegate d; |
| std::unique_ptr<URLRequest> req(context->CreateRequest( |
| GURL(kEmptyBodyGzipTransaction.url), DEFAULT_PRIORITY, &d, |
| TRAFFIC_ANNOTATION_FOR_TESTS)); |
| AddMockTransaction(&kEmptyBodyGzipTransaction); |
| |
| req->set_method("GET"); |
| req->Start(); |
| |
| d.RunUntilComplete(); |
| |
| EXPECT_FALSE(d.request_failed()); |
| EXPECT_EQ(200, req->GetResponseCode()); |
| EXPECT_TRUE(d.data_received().empty()); |
| EXPECT_TRUE(network_layer->done_reading_called()); |
| |
| RemoveMockTransaction(&kEmptyBodyGzipTransaction); |
| } |
| |
| // Regression test for crbug.com/575213. |
| TEST_F(URLRequestJobTest, InvalidContentGZipTransaction) { |
| auto context_builder = CreateTestURLRequestContextBuilder(); |
| auto* network_layer = context_builder->SetHttpTransactionFactoryForTesting( |
| std::make_unique<MockNetworkLayer>()); |
| context_builder->DisableHttpCache(); |
| auto context = context_builder->Build(); |
| |
| TestDelegate d; |
| std::unique_ptr<URLRequest> req(context->CreateRequest( |
| GURL(kInvalidContentGZipTransaction.url), DEFAULT_PRIORITY, &d, |
| TRAFFIC_ANNOTATION_FOR_TESTS)); |
| AddMockTransaction(&kInvalidContentGZipTransaction); |
| |
| req->set_method("GET"); |
| req->Start(); |
| |
| d.RunUntilComplete(); |
| |
| // Request failed indicates the request failed before headers were received, |
| // so should be false. |
| EXPECT_FALSE(d.request_failed()); |
| EXPECT_EQ(200, req->GetResponseCode()); |
| EXPECT_EQ(ERR_CONTENT_DECODING_FAILED, d.request_status()); |
| EXPECT_TRUE(d.data_received().empty()); |
| EXPECT_FALSE(network_layer->done_reading_called()); |
| |
| RemoveMockTransaction(&kInvalidContentGZipTransaction); |
| } |
| |
| // Regression test for crbug.com/553300. |
| TEST_F(URLRequestJobTest, SlowFilterRead) { |
| auto context_builder = CreateTestURLRequestContextBuilder(); |
| auto* network_layer = context_builder->SetHttpTransactionFactoryForTesting( |
| std::make_unique<MockNetworkLayer>()); |
| context_builder->DisableHttpCache(); |
| auto context = context_builder->Build(); |
| |
| TestDelegate d; |
| std::unique_ptr<URLRequest> req( |
| context->CreateRequest(GURL(kGzipSlowTransaction.url), DEFAULT_PRIORITY, |
| &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| AddMockTransaction(&kGzipSlowTransaction); |
| |
| req->set_method("GET"); |
| req->Start(); |
| |
| d.RunUntilComplete(); |
| |
| EXPECT_FALSE(d.request_failed()); |
| EXPECT_EQ(200, req->GetResponseCode()); |
| EXPECT_EQ("hello\n", d.data_received()); |
| EXPECT_TRUE(network_layer->done_reading_called()); |
| |
| RemoveMockTransaction(&kGzipSlowTransaction); |
| } |
| |
| TEST_F(URLRequestJobTest, SlowBrotliRead) { |
| auto context_builder = CreateTestURLRequestContextBuilder(); |
| auto* network_layer = context_builder->SetHttpTransactionFactoryForTesting( |
| std::make_unique<MockNetworkLayer>()); |
| context_builder->DisableHttpCache(); |
| auto context = context_builder->Build(); |
| |
| TestDelegate d; |
| std::unique_ptr<URLRequest> req( |
| context->CreateRequest(GURL(kBrotliSlowTransaction.url), DEFAULT_PRIORITY, |
| &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| AddMockTransaction(&kBrotliSlowTransaction); |
| |
| req->set_method("GET"); |
| req->Start(); |
| |
| base::RunLoop().RunUntilIdle(); |
| |
| EXPECT_FALSE(d.request_failed()); |
| EXPECT_EQ(200, req->GetResponseCode()); |
| EXPECT_EQ(kHelloData, d.data_received()); |
| EXPECT_TRUE(network_layer->done_reading_called()); |
| // When there's a filter and a Content-Length, expected content size should |
| // not be available. |
| EXPECT_EQ(-1, req->GetExpectedContentSize()); |
| |
| RemoveMockTransaction(&kBrotliSlowTransaction); |
| } |
| |
| TEST(URLRequestJobComputeReferrer, SetsSameOriginForMetricsOnSameOrigin) { |
| bool same_origin = false; |
| URLRequestJob::ComputeReferrerForPolicy( |
| ReferrerPolicy(), |
| /*original_referrer=*/GURL("http://google.com"), |
| /*destination=*/GURL("http://google.com"), &same_origin); |
| EXPECT_TRUE(same_origin); |
| } |
| |
| TEST(URLRequestJobComputeReferrer, SetsSameOriginForMetricsOnCrossOrigin) { |
| bool same_origin = true; |
| URLRequestJob::ComputeReferrerForPolicy( |
| ReferrerPolicy(), |
| /*original_referrer=*/GURL("http://google.com"), |
| /*destination=*/GURL("http://boggle.com"), &same_origin); |
| EXPECT_FALSE(same_origin); |
| } |
| |
| TEST(URLRequestJobComputeReferrer, AcceptsNullptrInput) { |
| // Shouldn't segfault. |
| URLRequestJob::ComputeReferrerForPolicy(ReferrerPolicy(), GURL(), GURL(), |
| nullptr); |
| } |
| |
| TEST(URLRequestJobComputeReferrer, FilesystemDestination) { |
| EXPECT_EQ(URLRequestJob::ComputeReferrerForPolicy( |
| ReferrerPolicy::NEVER_CLEAR, GURL("https://referrer.example"), |
| GURL("filesystem:https://destination.example"), nullptr), |
| GURL("https://referrer.example")); |
| } |
| |
| TEST(URLRequestJobComputeReferrer, TruncatesLongReferrer) { |
| std::string original_spec = "https://referrer.example/"; |
| original_spec.resize(4097, 'a'); |
| const GURL kOriginalReferrer(original_spec); |
| |
| EXPECT_EQ(URLRequestJob::ComputeReferrerForPolicy(ReferrerPolicy::NEVER_CLEAR, |
| kOriginalReferrer, |
| GURL("https://google.com")), |
| GURL("https://referrer.example/")); |
| } |
| |
| TEST(URLRequestJobComputeReferrer, DoesntTruncateShortReferrer) { |
| std::string original_spec = "https://referrer.example/"; |
| original_spec.resize(4096, 'a'); |
| const GURL kOriginalReferrer(original_spec); |
| |
| EXPECT_EQ(URLRequestJob::ComputeReferrerForPolicy(ReferrerPolicy::NEVER_CLEAR, |
| kOriginalReferrer, |
| GURL("https://google.com")), |
| kOriginalReferrer); |
| } |
| |
| TEST(URLRequestJobComputeReferrer, DoesntTruncateEvenShorterReferrer) { |
| std::string original_spec = "https://referrer.example/"; |
| original_spec.resize(4095, 'a'); |
| const GURL kOriginalReferrer(original_spec); |
| |
| EXPECT_EQ(URLRequestJob::ComputeReferrerForPolicy(ReferrerPolicy::NEVER_CLEAR, |
| kOriginalReferrer, |
| GURL("https://google.com")), |
| kOriginalReferrer); |
| } |
| |
| TEST(URLRequestJobComputeReferrer, DoesntTruncateReferrerWithLongRef) { |
| // Because the "is the length greater than 4096?" check comes *after* |
| // stripping the ref in the Referrer Policy spec, a URL that is short except |
| // for having a very long ref should not be stripped to an origin by the "if |
| // the length is too long, strip to the origin" check. |
| EXPECT_EQ(URLRequestJob::ComputeReferrerForPolicy( |
| ReferrerPolicy::NEVER_CLEAR, |
| GURL(std::string("https://referrer.example/path#") + |
| std::string(5000, 'a')), |
| GURL("https://google.com")), |
| GURL("https://referrer.example/path")); |
| } |
| |
| TEST(URLRequestJobComputeReferrer, InvalidSchemeReferrer) { |
| const GURL kOriginalReferrer("about:blank"); |
| ASSERT_FALSE(url::IsReferrerScheme( |
| kOriginalReferrer.spec().data(), |
| kOriginalReferrer.parsed_for_possibly_invalid_spec().scheme)); |
| |
| EXPECT_EQ(URLRequestJob::ComputeReferrerForPolicy(ReferrerPolicy::NEVER_CLEAR, |
| kOriginalReferrer, |
| GURL("https://google.com")), |
| GURL()); |
| |
| EXPECT_EQ(URLRequestJob::ComputeReferrerForPolicy(ReferrerPolicy::ORIGIN, |
| kOriginalReferrer, |
| GURL("https://google.com")), |
| GURL()); |
| } |
| |
| TEST(URLRequestJobComputeReferrer, CapReferrerOnCrossOrigin) { |
| base::test::ScopedFeatureList feature_list; |
| feature_list.InitAndEnableFeature( |
| features::kCapReferrerToOriginOnCrossOrigin); |
| |
| const GURL kOriginalReferrer("https://boggle.com/path"); |
| |
| EXPECT_EQ(URLRequestJob::ComputeReferrerForPolicy(ReferrerPolicy::NEVER_CLEAR, |
| kOriginalReferrer, |
| GURL("https://google.com")), |
| GURL("https://boggle.com/")); |
| } |
| |
| TEST(URLRequestJobComputeReferrer, |
| CapReferrerOnCrossOriginRespectsStricterPolicy) { |
| base::test::ScopedFeatureList feature_list; |
| feature_list.InitAndEnableFeature( |
| features::kCapReferrerToOriginOnCrossOrigin); |
| |
| const GURL kOriginalReferrer("https://boggle.com/path"); |
| |
| EXPECT_EQ(URLRequestJob::ComputeReferrerForPolicy(ReferrerPolicy::NO_REFERRER, |
| kOriginalReferrer, |
| GURL("https://google.com")), |
| GURL()); |
| } |
| |
| TEST(URLRequestJobComputeReferrer, |
| CapReferrerOnCrossOriginDoesntCapOnSameOrigin) { |
| base::test::ScopedFeatureList feature_list; |
| feature_list.InitAndEnableFeature( |
| features::kCapReferrerToOriginOnCrossOrigin); |
| |
| const GURL kOriginalReferrer("https://boggle.com/path"); |
| |
| EXPECT_EQ(URLRequestJob::ComputeReferrerForPolicy(ReferrerPolicy::NEVER_CLEAR, |
| kOriginalReferrer, |
| GURL("https://boggle.com")), |
| kOriginalReferrer); |
| } |
| |
| } // namespace net |