1 // Copyright 2013 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 #include "net/url_request/url_request_http_job.h"
6
7 #include <stdint.h>
8
9 #include <cstddef>
10 #include <memory>
11 #include <utility>
12 #include <vector>
13
14 #include "base/compiler_specific.h"
15 #include "base/memory/ptr_util.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/run_loop.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_split.h"
20 #include "base/test/bind.h"
21 #include "base/test/metrics/histogram_tester.h"
22 #include "base/test/scoped_feature_list.h"
23 #include "base/test/task_environment.h"
24 #include "build/build_config.h"
25 #include "net/base/auth.h"
26 #include "net/base/features.h"
27 #include "net/base/isolation_info.h"
28 #include "net/base/load_flags.h"
29 #include "net/base/proxy_chain.h"
30 #include "net/base/proxy_server.h"
31 #include "net/base/proxy_string_util.h"
32 #include "net/base/request_priority.h"
33 #include "net/cert/ct_policy_status.h"
34 #include "net/cookies/canonical_cookie_test_helpers.h"
35 #include "net/cookies/cookie_monster.h"
36 #include "net/cookies/cookie_store_test_callbacks.h"
37 #include "net/cookies/cookie_store_test_helpers.h"
38 #include "net/cookies/test_cookie_access_delegate.h"
39 #include "net/http/http_transaction_factory.h"
40 #include "net/http/http_transaction_test_util.h"
41 #include "net/http/transport_security_state.h"
42 #include "net/log/net_log_event_type.h"
43 #include "net/log/test_net_log.h"
44 #include "net/log/test_net_log_util.h"
45 #include "net/net_buildflags.h"
46 #include "net/proxy_resolution/configured_proxy_resolution_service.h"
47 #include "net/socket/next_proto.h"
48 #include "net/socket/socket_test_util.h"
49 #include "net/test/cert_test_util.h"
50 #include "net/test/embedded_test_server/default_handlers.h"
51 #include "net/test/gtest_util.h"
52 #include "net/test/test_data_directory.h"
53 #include "net/test/test_with_task_environment.h"
54 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
55 #include "net/url_request/url_request.h"
56 #include "net/url_request/url_request_context.h"
57 #include "net/url_request/url_request_context_builder.h"
58 #include "net/url_request/url_request_test_util.h"
59 #include "net/url_request/websocket_handshake_userdata_key.h"
60 #include "net/websockets/websocket_test_util.h"
61 #include "testing/gmock/include/gmock/gmock.h"
62 #include "testing/gtest/include/gtest/gtest.h"
63 #include "url/gurl.h"
64 #include "url/url_constants.h"
65
66 #if BUILDFLAG(IS_ANDROID)
67 #include "base/android/jni_android.h"
68 #include "net/android/net_test_support_jni/AndroidNetworkLibraryTestUtil_jni.h"
69 #endif
70
71 #if BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS)
72 #include "net/device_bound_sessions/device_bound_session_service.h"
73 #include "net/device_bound_sessions/test_util.h"
74 #endif
75
76 using net::test::IsError;
77 using net::test::IsOk;
78
79 namespace net {
80
81 namespace {
82
83 using ::testing::_;
84 using ::testing::Return;
85 using ::testing::UnorderedElementsAre;
86
87 const char kSimpleGetMockWrite[] =
88 "GET / HTTP/1.1\r\n"
89 "Host: www.example.com\r\n"
90 "Connection: keep-alive\r\n"
91 "User-Agent: \r\n"
92 "Accept-Encoding: gzip, deflate\r\n"
93 "Accept-Language: en-us,fr\r\n\r\n";
94
95 const char kSimpleHeadMockWrite[] =
96 "HEAD / HTTP/1.1\r\n"
97 "Host: www.example.com\r\n"
98 "Connection: keep-alive\r\n"
99 "User-Agent: \r\n"
100 "Accept-Encoding: gzip, deflate\r\n"
101 "Accept-Language: en-us,fr\r\n\r\n";
102
103 const char kTrustAnchorRequestHistogram[] =
104 "Net.Certificate.TrustAnchor.Request";
105
106 // Inherit from URLRequestHttpJob to expose the priority and some
107 // other hidden functions.
108 class TestURLRequestHttpJob : public URLRequestHttpJob {
109 public:
TestURLRequestHttpJob(URLRequest * request)110 explicit TestURLRequestHttpJob(URLRequest* request)
111 : URLRequestHttpJob(request,
112 request->context()->http_user_agent_settings()) {}
113
114 TestURLRequestHttpJob(const TestURLRequestHttpJob&) = delete;
115 TestURLRequestHttpJob& operator=(const TestURLRequestHttpJob&) = delete;
116
117 ~TestURLRequestHttpJob() override = default;
118
119 // URLRequestJob implementation:
SetUpSourceStream()120 std::unique_ptr<SourceStream> SetUpSourceStream() override {
121 if (use_null_source_stream_)
122 return nullptr;
123 return URLRequestHttpJob::SetUpSourceStream();
124 }
125
set_use_null_source_stream(bool use_null_source_stream)126 void set_use_null_source_stream(bool use_null_source_stream) {
127 use_null_source_stream_ = use_null_source_stream;
128 }
129
130 using URLRequestHttpJob::SetPriority;
131 using URLRequestHttpJob::Start;
132 using URLRequestHttpJob::Kill;
133 using URLRequestHttpJob::priority;
134
135 private:
136 bool use_null_source_stream_ = false;
137 };
138
139 class URLRequestHttpJobSetUpSourceTest : public TestWithTaskEnvironment {
140 public:
URLRequestHttpJobSetUpSourceTest()141 URLRequestHttpJobSetUpSourceTest() {
142 auto context_builder = CreateTestURLRequestContextBuilder();
143 context_builder->set_client_socket_factory_for_testing(&socket_factory_);
144 context_ = context_builder->Build();
145 }
146
147 protected:
148 MockClientSocketFactory socket_factory_;
149
150 std::unique_ptr<URLRequestContext> context_;
151 TestDelegate delegate_;
152 };
153
154 // Tests that if SetUpSourceStream() returns nullptr, the request fails.
TEST_F(URLRequestHttpJobSetUpSourceTest,SetUpSourceFails)155 TEST_F(URLRequestHttpJobSetUpSourceTest, SetUpSourceFails) {
156 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
157 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
158 "Content-Length: 12\r\n\r\n"),
159 MockRead("Test Content")};
160
161 StaticSocketDataProvider socket_data(reads, writes);
162 socket_factory_.AddSocketDataProvider(&socket_data);
163
164 std::unique_ptr<URLRequest> request =
165 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
166 &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS);
167 auto job = std::make_unique<TestURLRequestHttpJob>(request.get());
168 job->set_use_null_source_stream(true);
169 TestScopedURLInterceptor interceptor(request->url(), std::move(job));
170 request->Start();
171
172 delegate_.RunUntilComplete();
173 EXPECT_EQ(ERR_CONTENT_DECODING_INIT_FAILED, delegate_.request_status());
174 }
175
176 // Tests that if there is an unknown content-encoding type, the raw response
177 // body is passed through.
TEST_F(URLRequestHttpJobSetUpSourceTest,UnknownEncoding)178 TEST_F(URLRequestHttpJobSetUpSourceTest, UnknownEncoding) {
179 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
180 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
181 "Content-Encoding: foo, gzip\r\n"
182 "Content-Length: 12\r\n\r\n"),
183 MockRead("Test Content")};
184
185 StaticSocketDataProvider socket_data(reads, writes);
186 socket_factory_.AddSocketDataProvider(&socket_data);
187
188 std::unique_ptr<URLRequest> request =
189 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
190 &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS);
191 auto job = std::make_unique<TestURLRequestHttpJob>(request.get());
192 TestScopedURLInterceptor interceptor(request->url(), std::move(job));
193 request->Start();
194
195 delegate_.RunUntilComplete();
196 EXPECT_EQ(OK, delegate_.request_status());
197 EXPECT_EQ("Test Content", delegate_.data_received());
198 }
199
200 // TaskEnvironment is required to instantiate a
201 // net::ConfiguredProxyResolutionService, which registers itself as an IP
202 // Address Observer with the NetworkChangeNotifier.
203 using URLRequestHttpJobWithProxyTest = TestWithTaskEnvironment;
204
205 class URLRequestHttpJobWithProxy {
206 public:
URLRequestHttpJobWithProxy(std::unique_ptr<ProxyResolutionService> proxy_resolution_service)207 explicit URLRequestHttpJobWithProxy(
208 std::unique_ptr<ProxyResolutionService> proxy_resolution_service) {
209 auto context_builder = CreateTestURLRequestContextBuilder();
210 context_builder->set_client_socket_factory_for_testing(&socket_factory_);
211 if (proxy_resolution_service) {
212 context_builder->set_proxy_resolution_service(
213 std::move(proxy_resolution_service));
214 }
215 context_ = context_builder->Build();
216 }
217
218 URLRequestHttpJobWithProxy(const URLRequestHttpJobWithProxy&) = delete;
219 URLRequestHttpJobWithProxy& operator=(const URLRequestHttpJobWithProxy&) =
220 delete;
221
222 MockClientSocketFactory socket_factory_;
223 std::unique_ptr<URLRequestContext> context_;
224 };
225
226 // Tests that when a proxy is not used, the proxy chain is set correctly on the
227 // URLRequest.
TEST_F(URLRequestHttpJobWithProxyTest,TestFailureWithoutProxy)228 TEST_F(URLRequestHttpJobWithProxyTest, TestFailureWithoutProxy) {
229 URLRequestHttpJobWithProxy http_job_with_proxy(nullptr);
230
231 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
232 MockRead reads[] = {MockRead(SYNCHRONOUS, ERR_CONNECTION_RESET)};
233
234 StaticSocketDataProvider socket_data(reads, writes);
235 http_job_with_proxy.socket_factory_.AddSocketDataProvider(&socket_data);
236
237 TestDelegate delegate;
238 std::unique_ptr<URLRequest> request =
239 http_job_with_proxy.context_->CreateRequest(
240 GURL("http://www.example.com"), DEFAULT_PRIORITY, &delegate,
241 TRAFFIC_ANNOTATION_FOR_TESTS);
242
243 request->Start();
244 ASSERT_TRUE(request->is_pending());
245 delegate.RunUntilComplete();
246
247 EXPECT_THAT(delegate.request_status(), IsError(ERR_CONNECTION_RESET));
248 EXPECT_EQ(ProxyChain::Direct(), request->proxy_chain());
249 EXPECT_EQ(0, request->received_response_content_length());
250 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
251 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
252 }
253
254 // Tests that when one proxy chain is in use and the connection to a proxy
255 // server in the proxy chain fails, the proxy chain is still set correctly on
256 // the URLRequest.
TEST_F(URLRequestHttpJobWithProxyTest,TestSuccessfulWithOneProxy)257 TEST_F(URLRequestHttpJobWithProxyTest, TestSuccessfulWithOneProxy) {
258 const char kSimpleProxyGetMockWrite[] =
259 "GET http://www.example.com/ HTTP/1.1\r\n"
260 "Host: www.example.com\r\n"
261 "Proxy-Connection: keep-alive\r\n"
262 "User-Agent: \r\n"
263 "Accept-Encoding: gzip, deflate\r\n"
264 "Accept-Language: en-us,fr\r\n\r\n";
265
266 const ProxyChain proxy_chain =
267 ProxyUriToProxyChain("http://origin.net:80", ProxyServer::SCHEME_HTTP);
268
269 std::unique_ptr<ProxyResolutionService> proxy_resolution_service =
270 ConfiguredProxyResolutionService::CreateFixedFromPacResultForTest(
271 ProxyServerToPacResultElement(proxy_chain.First()),
272 TRAFFIC_ANNOTATION_FOR_TESTS);
273
274 MockWrite writes[] = {MockWrite(kSimpleProxyGetMockWrite)};
275 MockRead reads[] = {MockRead(SYNCHRONOUS, ERR_CONNECTION_RESET)};
276
277 StaticSocketDataProvider socket_data(reads, writes);
278
279 URLRequestHttpJobWithProxy http_job_with_proxy(
280 std::move(proxy_resolution_service));
281 http_job_with_proxy.socket_factory_.AddSocketDataProvider(&socket_data);
282
283 TestDelegate delegate;
284 std::unique_ptr<URLRequest> request =
285 http_job_with_proxy.context_->CreateRequest(
286 GURL("http://www.example.com"), DEFAULT_PRIORITY, &delegate,
287 TRAFFIC_ANNOTATION_FOR_TESTS);
288
289 request->Start();
290 ASSERT_TRUE(request->is_pending());
291 delegate.RunUntilComplete();
292
293 EXPECT_THAT(delegate.request_status(), IsError(ERR_CONNECTION_RESET));
294 // When request fails due to proxy connection errors, the proxy chain should
295 // still be set on the `request`.
296 EXPECT_EQ(proxy_chain, request->proxy_chain());
297 EXPECT_EQ(0, request->received_response_content_length());
298 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
299 EXPECT_EQ(0, request->GetTotalReceivedBytes());
300 }
301
302 // Tests that when two proxy chains are in use and the connection to a proxy
303 // server in the first proxy chain fails, the proxy chain is set correctly on
304 // the URLRequest.
TEST_F(URLRequestHttpJobWithProxyTest,TestContentLengthSuccessfulRequestWithTwoProxies)305 TEST_F(URLRequestHttpJobWithProxyTest,
306 TestContentLengthSuccessfulRequestWithTwoProxies) {
307 const ProxyChain proxy_chain =
308 ProxyUriToProxyChain("http://origin.net:80", ProxyServer::SCHEME_HTTP);
309
310 // Connection to `proxy_chain` would fail. Request should be fetched over
311 // DIRECT.
312 std::unique_ptr<ProxyResolutionService> proxy_resolution_service =
313 ConfiguredProxyResolutionService::CreateFixedFromPacResultForTest(
314 ProxyServerToPacResultElement(proxy_chain.First()) + "; DIRECT",
315 TRAFFIC_ANNOTATION_FOR_TESTS);
316
317 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
318 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
319 "Content-Length: 12\r\n\r\n"),
320 MockRead("Test Content"), MockRead(ASYNC, OK)};
321
322 MockConnect mock_connect_1(SYNCHRONOUS, ERR_CONNECTION_RESET);
323 StaticSocketDataProvider connect_data_1;
324 connect_data_1.set_connect_data(mock_connect_1);
325
326 StaticSocketDataProvider socket_data(reads, writes);
327
328 URLRequestHttpJobWithProxy http_job_with_proxy(
329 std::move(proxy_resolution_service));
330 http_job_with_proxy.socket_factory_.AddSocketDataProvider(&connect_data_1);
331 http_job_with_proxy.socket_factory_.AddSocketDataProvider(&socket_data);
332
333 TestDelegate delegate;
334 std::unique_ptr<URLRequest> request =
335 http_job_with_proxy.context_->CreateRequest(
336 GURL("http://www.example.com"), DEFAULT_PRIORITY, &delegate,
337 TRAFFIC_ANNOTATION_FOR_TESTS);
338
339 request->Start();
340 ASSERT_TRUE(request->is_pending());
341 delegate.RunUntilComplete();
342
343 EXPECT_THAT(delegate.request_status(), IsOk());
344 EXPECT_EQ(ProxyChain::Direct(), request->proxy_chain());
345 EXPECT_EQ(12, request->received_response_content_length());
346 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
347 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
348 }
349
350 class URLRequestHttpJobTest : public TestWithTaskEnvironment {
351 protected:
URLRequestHttpJobTest()352 URLRequestHttpJobTest() {
353 auto context_builder = CreateTestURLRequestContextBuilder();
354 context_builder->SetHttpTransactionFactoryForTesting(
355 std::make_unique<MockNetworkLayer>());
356 context_builder->DisableHttpCache();
357 context_builder->set_net_log(NetLog::Get());
358 context_ = context_builder->Build();
359
360 req_ = context_->CreateRequest(GURL("http://www.example.com"),
361 DEFAULT_PRIORITY, &delegate_,
362 TRAFFIC_ANNOTATION_FOR_TESTS);
363 }
364
network_layer()365 MockNetworkLayer& network_layer() {
366 // This cast is safe because we set a MockNetworkLayer in the constructor.
367 return *static_cast<MockNetworkLayer*>(
368 context_->http_transaction_factory());
369 }
370
CreateFirstPartyRequest(const URLRequestContext & context,const GURL & url,URLRequest::Delegate * delegate)371 std::unique_ptr<URLRequest> CreateFirstPartyRequest(
372 const URLRequestContext& context,
373 const GURL& url,
374 URLRequest::Delegate* delegate) {
375 auto req = context.CreateRequest(url, DEFAULT_PRIORITY, delegate,
376 TRAFFIC_ANNOTATION_FOR_TESTS);
377 req->set_initiator(url::Origin::Create(url));
378 req->set_site_for_cookies(SiteForCookies::FromUrl(url));
379 return req;
380 }
381
382 std::unique_ptr<URLRequestContext> context_;
383 TestDelegate delegate_;
384 RecordingNetLogObserver net_log_observer_;
385 std::unique_ptr<URLRequest> req_;
386 };
387
388 class URLRequestHttpJobWithMockSocketsTest : public TestWithTaskEnvironment {
389 protected:
URLRequestHttpJobWithMockSocketsTest()390 URLRequestHttpJobWithMockSocketsTest() {
391 auto context_builder = CreateTestURLRequestContextBuilder();
392 context_builder->set_client_socket_factory_for_testing(&socket_factory_);
393 context_ = context_builder->Build();
394 }
395
396 MockClientSocketFactory socket_factory_;
397 std::unique_ptr<URLRequestContext> context_;
398 };
399
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestContentLengthSuccessfulRequest)400 TEST_F(URLRequestHttpJobWithMockSocketsTest,
401 TestContentLengthSuccessfulRequest) {
402 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
403 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
404 "Content-Length: 12\r\n\r\n"),
405 MockRead("Test Content")};
406
407 StaticSocketDataProvider socket_data(reads, writes);
408 socket_factory_.AddSocketDataProvider(&socket_data);
409
410 TestDelegate delegate;
411 std::unique_ptr<URLRequest> request =
412 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
413 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
414
415 request->Start();
416 ASSERT_TRUE(request->is_pending());
417 delegate.RunUntilComplete();
418
419 EXPECT_THAT(delegate.request_status(), IsOk());
420 EXPECT_EQ(12, request->received_response_content_length());
421 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
422 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
423 }
424
425 // Tests a successful HEAD request.
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestSuccessfulHead)426 TEST_F(URLRequestHttpJobWithMockSocketsTest, TestSuccessfulHead) {
427 MockWrite writes[] = {MockWrite(kSimpleHeadMockWrite)};
428 MockRead reads[] = {
429 MockRead("HTTP/1.1 200 OK\r\n"
430 "Content-Length: 0\r\n\r\n")};
431
432 StaticSocketDataProvider socket_data(reads, writes);
433 socket_factory_.AddSocketDataProvider(&socket_data);
434
435 TestDelegate delegate;
436 std::unique_ptr<URLRequest> request =
437 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
438 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
439
440 request->set_method("HEAD");
441 request->Start();
442 ASSERT_TRUE(request->is_pending());
443 delegate.RunUntilComplete();
444
445 EXPECT_THAT(delegate.request_status(), IsOk());
446 EXPECT_EQ(0, request->received_response_content_length());
447 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
448 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
449 }
450
451 // Similar to above test but tests that even if response body is there in the
452 // HEAD response stream, it should not be read due to HttpStreamParser's logic.
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestSuccessfulHeadWithContent)453 TEST_F(URLRequestHttpJobWithMockSocketsTest, TestSuccessfulHeadWithContent) {
454 MockWrite writes[] = {MockWrite(kSimpleHeadMockWrite)};
455 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
456 "Content-Length: 12\r\n\r\n"),
457 MockRead("Test Content")};
458
459 StaticSocketDataProvider socket_data(reads, writes);
460 socket_factory_.AddSocketDataProvider(&socket_data);
461
462 TestDelegate delegate;
463 std::unique_ptr<URLRequest> request =
464 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
465 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
466
467 request->set_method("HEAD");
468 request->Start();
469 ASSERT_TRUE(request->is_pending());
470 delegate.RunUntilComplete();
471
472 EXPECT_THAT(delegate.request_status(), IsOk());
473 EXPECT_EQ(0, request->received_response_content_length());
474 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
475 EXPECT_EQ(CountReadBytes(reads) - 12, request->GetTotalReceivedBytes());
476 }
477
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestSuccessfulCachedHeadRequest)478 TEST_F(URLRequestHttpJobWithMockSocketsTest, TestSuccessfulCachedHeadRequest) {
479 const url::Origin kOrigin1 =
480 url::Origin::Create(GURL("http://www.example.com"));
481 const IsolationInfo kTestIsolationInfo =
482 IsolationInfo::CreateForInternalRequest(kOrigin1);
483
484 // Cache the response.
485 {
486 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
487 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
488 "Content-Length: 12\r\n\r\n"),
489 MockRead("Test Content")};
490
491 StaticSocketDataProvider socket_data(reads, writes);
492 socket_factory_.AddSocketDataProvider(&socket_data);
493
494 TestDelegate delegate;
495 std::unique_ptr<URLRequest> request = context_->CreateRequest(
496 GURL("http://www.example.com"), DEFAULT_PRIORITY, &delegate,
497 TRAFFIC_ANNOTATION_FOR_TESTS);
498
499 request->set_isolation_info(kTestIsolationInfo);
500 request->Start();
501 ASSERT_TRUE(request->is_pending());
502 delegate.RunUntilComplete();
503
504 EXPECT_THAT(delegate.request_status(), IsOk());
505 EXPECT_EQ(12, request->received_response_content_length());
506 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
507 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
508 }
509
510 // Send a HEAD request for the cached response.
511 {
512 MockWrite writes[] = {MockWrite(kSimpleHeadMockWrite)};
513 MockRead reads[] = {
514 MockRead("HTTP/1.1 200 OK\r\n"
515 "Content-Length: 0\r\n\r\n")};
516
517 StaticSocketDataProvider socket_data(reads, writes);
518 socket_factory_.AddSocketDataProvider(&socket_data);
519
520 TestDelegate delegate;
521 std::unique_ptr<URLRequest> request = context_->CreateRequest(
522 GURL("http://www.example.com"), DEFAULT_PRIORITY, &delegate,
523 TRAFFIC_ANNOTATION_FOR_TESTS);
524
525 // Use the cached version.
526 request->SetLoadFlags(LOAD_SKIP_CACHE_VALIDATION);
527 request->set_method("HEAD");
528 request->set_isolation_info(kTestIsolationInfo);
529 request->Start();
530 ASSERT_TRUE(request->is_pending());
531 delegate.RunUntilComplete();
532
533 EXPECT_THAT(delegate.request_status(), IsOk());
534 EXPECT_EQ(0, request->received_response_content_length());
535 EXPECT_EQ(0, request->GetTotalSentBytes());
536 EXPECT_EQ(0, request->GetTotalReceivedBytes());
537 }
538 }
539
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestContentLengthSuccessfulHttp09Request)540 TEST_F(URLRequestHttpJobWithMockSocketsTest,
541 TestContentLengthSuccessfulHttp09Request) {
542 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
543 MockRead reads[] = {MockRead("Test Content"),
544 MockRead(net::SYNCHRONOUS, net::OK)};
545
546 StaticSocketDataProvider socket_data(reads, base::span<MockWrite>());
547 socket_factory_.AddSocketDataProvider(&socket_data);
548
549 TestDelegate delegate;
550 std::unique_ptr<URLRequest> request =
551 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
552 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
553
554 request->Start();
555 ASSERT_TRUE(request->is_pending());
556 delegate.RunUntilComplete();
557
558 EXPECT_THAT(delegate.request_status(), IsOk());
559 EXPECT_EQ(12, request->received_response_content_length());
560 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
561 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
562 }
563
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestContentLengthFailedRequest)564 TEST_F(URLRequestHttpJobWithMockSocketsTest, TestContentLengthFailedRequest) {
565 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
566 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
567 "Content-Length: 20\r\n\r\n"),
568 MockRead("Test Content"),
569 MockRead(net::SYNCHRONOUS, net::ERR_FAILED)};
570
571 StaticSocketDataProvider socket_data(reads, writes);
572 socket_factory_.AddSocketDataProvider(&socket_data);
573
574 TestDelegate delegate;
575 std::unique_ptr<URLRequest> request =
576 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
577 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
578
579 request->Start();
580 ASSERT_TRUE(request->is_pending());
581 delegate.RunUntilComplete();
582
583 EXPECT_THAT(delegate.request_status(), IsError(ERR_FAILED));
584 EXPECT_EQ(12, request->received_response_content_length());
585 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
586 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
587 }
588
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestContentLengthCancelledRequest)589 TEST_F(URLRequestHttpJobWithMockSocketsTest,
590 TestContentLengthCancelledRequest) {
591 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
592 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
593 "Content-Length: 20\r\n\r\n"),
594 MockRead("Test Content"),
595 MockRead(net::SYNCHRONOUS, net::ERR_IO_PENDING)};
596
597 StaticSocketDataProvider socket_data(reads, writes);
598 socket_factory_.AddSocketDataProvider(&socket_data);
599
600 TestDelegate delegate;
601 std::unique_ptr<URLRequest> request =
602 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
603 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
604
605 delegate.set_cancel_in_received_data(true);
606 request->Start();
607 delegate.RunUntilComplete();
608
609 EXPECT_THAT(delegate.request_status(), IsError(ERR_ABORTED));
610 EXPECT_EQ(12, request->received_response_content_length());
611 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
612 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
613 }
614
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestNetworkBytesRedirectedRequest)615 TEST_F(URLRequestHttpJobWithMockSocketsTest,
616 TestNetworkBytesRedirectedRequest) {
617 MockWrite redirect_writes[] = {
618 MockWrite("GET / HTTP/1.1\r\n"
619 "Host: www.redirect.com\r\n"
620 "Connection: keep-alive\r\n"
621 "User-Agent: \r\n"
622 "Accept-Encoding: gzip, deflate\r\n"
623 "Accept-Language: en-us,fr\r\n\r\n")};
624
625 MockRead redirect_reads[] = {
626 MockRead("HTTP/1.1 302 Found\r\n"
627 "Location: http://www.example.com\r\n\r\n"),
628 };
629 StaticSocketDataProvider redirect_socket_data(redirect_reads,
630 redirect_writes);
631 socket_factory_.AddSocketDataProvider(&redirect_socket_data);
632
633 MockWrite final_writes[] = {MockWrite(kSimpleGetMockWrite)};
634 MockRead final_reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
635 "Content-Length: 12\r\n\r\n"),
636 MockRead("Test Content")};
637 StaticSocketDataProvider final_socket_data(final_reads, final_writes);
638 socket_factory_.AddSocketDataProvider(&final_socket_data);
639
640 TestDelegate delegate;
641 std::unique_ptr<URLRequest> request =
642 context_->CreateRequest(GURL("http://www.redirect.com"), DEFAULT_PRIORITY,
643 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
644
645 request->Start();
646 ASSERT_TRUE(request->is_pending());
647 delegate.RunUntilComplete();
648
649 EXPECT_THAT(delegate.request_status(), IsOk());
650 EXPECT_EQ(12, request->received_response_content_length());
651 // Should not include the redirect.
652 EXPECT_EQ(CountWriteBytes(final_writes), request->GetTotalSentBytes());
653 EXPECT_EQ(CountReadBytes(final_reads), request->GetTotalReceivedBytes());
654 }
655
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestNetworkBytesCancelledAfterHeaders)656 TEST_F(URLRequestHttpJobWithMockSocketsTest,
657 TestNetworkBytesCancelledAfterHeaders) {
658 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
659 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n\r\n")};
660 StaticSocketDataProvider socket_data(reads, writes);
661 socket_factory_.AddSocketDataProvider(&socket_data);
662
663 TestDelegate delegate;
664 std::unique_ptr<URLRequest> request =
665 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
666 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
667
668 delegate.set_cancel_in_response_started(true);
669 request->Start();
670 delegate.RunUntilComplete();
671
672 EXPECT_THAT(delegate.request_status(), IsError(ERR_ABORTED));
673 EXPECT_EQ(0, request->received_response_content_length());
674 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
675 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
676 }
677
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestNetworkBytesCancelledImmediately)678 TEST_F(URLRequestHttpJobWithMockSocketsTest,
679 TestNetworkBytesCancelledImmediately) {
680 StaticSocketDataProvider socket_data;
681 socket_factory_.AddSocketDataProvider(&socket_data);
682
683 TestDelegate delegate;
684 std::unique_ptr<URLRequest> request =
685 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
686 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
687
688 request->Start();
689 request->Cancel();
690 delegate.RunUntilComplete();
691
692 EXPECT_THAT(delegate.request_status(), IsError(ERR_ABORTED));
693 EXPECT_EQ(0, request->received_response_content_length());
694 EXPECT_EQ(0, request->GetTotalSentBytes());
695 EXPECT_EQ(0, request->GetTotalReceivedBytes());
696 }
697
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestHttpTimeToFirstByte)698 TEST_F(URLRequestHttpJobWithMockSocketsTest, TestHttpTimeToFirstByte) {
699 base::HistogramTester histograms;
700 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
701 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
702 "Content-Length: 12\r\n\r\n"),
703 MockRead("Test Content")};
704
705 StaticSocketDataProvider socket_data(reads, writes);
706 socket_factory_.AddSocketDataProvider(&socket_data);
707
708 TestDelegate delegate;
709 std::unique_ptr<URLRequest> request =
710 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
711 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
712 histograms.ExpectTotalCount("Net.HttpTimeToFirstByte", 0);
713
714 request->Start();
715 delegate.RunUntilComplete();
716
717 EXPECT_THAT(delegate.request_status(), IsOk());
718 histograms.ExpectTotalCount("Net.HttpTimeToFirstByte", 1);
719 }
720
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestHttpTimeToFirstByteForCancelledTask)721 TEST_F(URLRequestHttpJobWithMockSocketsTest,
722 TestHttpTimeToFirstByteForCancelledTask) {
723 base::HistogramTester histograms;
724 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
725 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
726 "Content-Length: 12\r\n\r\n"),
727 MockRead("Test Content")};
728
729 StaticSocketDataProvider socket_data(reads, writes);
730 socket_factory_.AddSocketDataProvider(&socket_data);
731
732 TestDelegate delegate;
733 std::unique_ptr<URLRequest> request =
734 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
735 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
736
737 request->Start();
738 request->Cancel();
739 delegate.RunUntilComplete();
740
741 EXPECT_THAT(delegate.request_status(), IsError(ERR_ABORTED));
742 histograms.ExpectTotalCount("Net.HttpTimeToFirstByte", 0);
743 }
744
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestHttpJobSuccessPriorityKeyedTotalTime)745 TEST_F(URLRequestHttpJobWithMockSocketsTest,
746 TestHttpJobSuccessPriorityKeyedTotalTime) {
747 base::HistogramTester histograms;
748
749 for (int priority = 0; priority < net::NUM_PRIORITIES; ++priority) {
750 for (int request_index = 0; request_index <= priority; ++request_index) {
751 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
752 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
753 "Content-Length: 12\r\n\r\n"),
754 MockRead("Test Content")};
755
756 StaticSocketDataProvider socket_data(reads, writes);
757 socket_factory_.AddSocketDataProvider(&socket_data);
758
759 TestDelegate delegate;
760 std::unique_ptr<URLRequest> request =
761 context_->CreateRequest(GURL("http://www.example.com/"),
762 static_cast<net::RequestPriority>(priority),
763 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
764
765 request->Start();
766 delegate.RunUntilComplete();
767 EXPECT_THAT(delegate.request_status(), IsOk());
768 }
769 }
770
771 for (int priority = 0; priority < net::NUM_PRIORITIES; ++priority) {
772 histograms.ExpectTotalCount("Net.HttpJob.TotalTimeSuccess.Priority" +
773 base::NumberToString(priority),
774 priority + 1);
775 }
776 }
777
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestHttpJobRecordsTrustAnchorHistograms)778 TEST_F(URLRequestHttpJobWithMockSocketsTest,
779 TestHttpJobRecordsTrustAnchorHistograms) {
780 SSLSocketDataProvider ssl_socket_data(net::ASYNC, net::OK);
781 ssl_socket_data.ssl_info.cert =
782 ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
783 // Simulate a certificate chain issued by "C=US, O=Google Trust Services LLC,
784 // CN=GTS Root R4". This publicly-trusted root was chosen as it was included
785 // in 2017 and is not anticipated to be removed from all supported platforms
786 // for a few decades.
787 // Note: The actual cert in |cert| does not matter for this testing.
788 SHA256HashValue leaf_hash = {{0}};
789 SHA256HashValue intermediate_hash = {{1}};
790 SHA256HashValue root_hash = {
791 {0x98, 0x47, 0xe5, 0x65, 0x3e, 0x5e, 0x9e, 0x84, 0x75, 0x16, 0xe5,
792 0xcb, 0x81, 0x86, 0x06, 0xaa, 0x75, 0x44, 0xa1, 0x9b, 0xe6, 0x7f,
793 0xd7, 0x36, 0x6d, 0x50, 0x69, 0x88, 0xe8, 0xd8, 0x43, 0x47}};
794 ssl_socket_data.ssl_info.public_key_hashes.push_back(HashValue(leaf_hash));
795 ssl_socket_data.ssl_info.public_key_hashes.push_back(
796 HashValue(intermediate_hash));
797 ssl_socket_data.ssl_info.public_key_hashes.push_back(HashValue(root_hash));
798
799 const base::HistogramBase::Sample kGTSRootR4HistogramID = 486;
800
801 socket_factory_.AddSSLSocketDataProvider(&ssl_socket_data);
802
803 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
804 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
805 "Content-Length: 12\r\n\r\n"),
806 MockRead("Test Content")};
807 StaticSocketDataProvider socket_data(reads, writes);
808 socket_factory_.AddSocketDataProvider(&socket_data);
809
810 base::HistogramTester histograms;
811 histograms.ExpectTotalCount(kTrustAnchorRequestHistogram, 0);
812
813 TestDelegate delegate;
814 std::unique_ptr<URLRequest> request = context_->CreateRequest(
815 GURL("https://www.example.com/"), DEFAULT_PRIORITY, &delegate,
816 TRAFFIC_ANNOTATION_FOR_TESTS);
817 request->Start();
818 delegate.RunUntilComplete();
819 EXPECT_THAT(delegate.request_status(), IsOk());
820
821 histograms.ExpectTotalCount(kTrustAnchorRequestHistogram, 1);
822 histograms.ExpectUniqueSample(kTrustAnchorRequestHistogram,
823 kGTSRootR4HistogramID, 1);
824 }
825
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestHttpJobDoesNotRecordTrustAnchorHistogramsWhenNoNetworkLoad)826 TEST_F(URLRequestHttpJobWithMockSocketsTest,
827 TestHttpJobDoesNotRecordTrustAnchorHistogramsWhenNoNetworkLoad) {
828 SSLSocketDataProvider ssl_socket_data(net::ASYNC, net::OK);
829 ssl_socket_data.ssl_info.cert =
830 ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
831 // Simulate a request loaded from a non-network source, such as a disk
832 // cache.
833 ssl_socket_data.ssl_info.public_key_hashes.clear();
834
835 socket_factory_.AddSSLSocketDataProvider(&ssl_socket_data);
836
837 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
838 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
839 "Content-Length: 12\r\n\r\n"),
840 MockRead("Test Content")};
841 StaticSocketDataProvider socket_data(reads, writes);
842 socket_factory_.AddSocketDataProvider(&socket_data);
843
844 base::HistogramTester histograms;
845 histograms.ExpectTotalCount(kTrustAnchorRequestHistogram, 0);
846
847 TestDelegate delegate;
848 std::unique_ptr<URLRequest> request = context_->CreateRequest(
849 GURL("https://www.example.com/"), DEFAULT_PRIORITY, &delegate,
850 TRAFFIC_ANNOTATION_FOR_TESTS);
851 request->Start();
852 delegate.RunUntilComplete();
853 EXPECT_THAT(delegate.request_status(), IsOk());
854
855 histograms.ExpectTotalCount(kTrustAnchorRequestHistogram, 0);
856 }
857
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestHttpJobRecordsMostSpecificTrustAnchorHistograms)858 TEST_F(URLRequestHttpJobWithMockSocketsTest,
859 TestHttpJobRecordsMostSpecificTrustAnchorHistograms) {
860 SSLSocketDataProvider ssl_socket_data(net::ASYNC, net::OK);
861 ssl_socket_data.ssl_info.cert =
862 ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
863 // Simulate a certificate chain issued by "C=US, O=Google Trust Services LLC,
864 // CN=GTS Root R4". This publicly-trusted root was chosen as it was included
865 // in 2017 and is not anticipated to be removed from all supported platforms
866 // for a few decades.
867 // Note: The actual cert in |cert| does not matter for this testing.
868 SHA256HashValue leaf_hash = {{0}};
869 SHA256HashValue intermediate_hash = {{1}};
870 SHA256HashValue gts_root_r3_hash = {
871 {0x41, 0x79, 0xed, 0xd9, 0x81, 0xef, 0x74, 0x74, 0x77, 0xb4, 0x96,
872 0x26, 0x40, 0x8a, 0xf4, 0x3d, 0xaa, 0x2c, 0xa7, 0xab, 0x7f, 0x9e,
873 0x08, 0x2c, 0x10, 0x60, 0xf8, 0x40, 0x96, 0x77, 0x43, 0x48}};
874 SHA256HashValue gts_root_r4_hash = {
875 {0x98, 0x47, 0xe5, 0x65, 0x3e, 0x5e, 0x9e, 0x84, 0x75, 0x16, 0xe5,
876 0xcb, 0x81, 0x86, 0x06, 0xaa, 0x75, 0x44, 0xa1, 0x9b, 0xe6, 0x7f,
877 0xd7, 0x36, 0x6d, 0x50, 0x69, 0x88, 0xe8, 0xd8, 0x43, 0x47}};
878 ssl_socket_data.ssl_info.public_key_hashes.push_back(HashValue(leaf_hash));
879 ssl_socket_data.ssl_info.public_key_hashes.push_back(
880 HashValue(intermediate_hash));
881 ssl_socket_data.ssl_info.public_key_hashes.push_back(
882 HashValue(gts_root_r3_hash));
883 ssl_socket_data.ssl_info.public_key_hashes.push_back(
884 HashValue(gts_root_r4_hash));
885
886 const base::HistogramBase::Sample kGTSRootR3HistogramID = 485;
887
888 socket_factory_.AddSSLSocketDataProvider(&ssl_socket_data);
889
890 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
891 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
892 "Content-Length: 12\r\n\r\n"),
893 MockRead("Test Content")};
894 StaticSocketDataProvider socket_data(reads, writes);
895 socket_factory_.AddSocketDataProvider(&socket_data);
896
897 base::HistogramTester histograms;
898 histograms.ExpectTotalCount(kTrustAnchorRequestHistogram, 0);
899
900 TestDelegate delegate;
901 std::unique_ptr<URLRequest> request = context_->CreateRequest(
902 GURL("https://www.example.com/"), DEFAULT_PRIORITY, &delegate,
903 TRAFFIC_ANNOTATION_FOR_TESTS);
904 request->Start();
905 delegate.RunUntilComplete();
906 EXPECT_THAT(delegate.request_status(), IsOk());
907
908 histograms.ExpectTotalCount(kTrustAnchorRequestHistogram, 1);
909 histograms.ExpectUniqueSample(kTrustAnchorRequestHistogram,
910 kGTSRootR3HistogramID, 1);
911 }
912
TEST_F(URLRequestHttpJobWithMockSocketsTest,EncodingAdvertisementOnRange)913 TEST_F(URLRequestHttpJobWithMockSocketsTest, EncodingAdvertisementOnRange) {
914 MockWrite writes[] = {
915 MockWrite("GET / HTTP/1.1\r\n"
916 "Host: www.example.com\r\n"
917 "Connection: keep-alive\r\n"
918 "User-Agent: \r\n"
919 "Accept-Encoding: identity\r\n"
920 "Accept-Language: en-us,fr\r\n"
921 "Range: bytes=0-1023\r\n\r\n")};
922
923 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
924 "Accept-Ranges: bytes\r\n"
925 "Content-Length: 12\r\n\r\n"),
926 MockRead("Test Content")};
927
928 StaticSocketDataProvider socket_data(reads, writes);
929 socket_factory_.AddSocketDataProvider(&socket_data);
930
931 TestDelegate delegate;
932 std::unique_ptr<URLRequest> request =
933 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
934 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
935
936 // Make the extra header to trigger the change in "Accepted-Encoding"
937 HttpRequestHeaders headers;
938 headers.SetHeader("Range", "bytes=0-1023");
939 request->SetExtraRequestHeaders(headers);
940
941 request->Start();
942 delegate.RunUntilComplete();
943
944 EXPECT_THAT(delegate.request_status(), IsOk());
945 EXPECT_EQ(12, request->received_response_content_length());
946 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
947 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
948 }
949
TEST_F(URLRequestHttpJobWithMockSocketsTest,RangeRequestOverrideEncoding)950 TEST_F(URLRequestHttpJobWithMockSocketsTest, RangeRequestOverrideEncoding) {
951 MockWrite writes[] = {
952 MockWrite("GET / HTTP/1.1\r\n"
953 "Host: www.example.com\r\n"
954 "Connection: keep-alive\r\n"
955 "Accept-Encoding: gzip, deflate\r\n"
956 "User-Agent: \r\n"
957 "Accept-Language: en-us,fr\r\n"
958 "Range: bytes=0-1023\r\n\r\n")};
959
960 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
961 "Accept-Ranges: bytes\r\n"
962 "Content-Length: 12\r\n\r\n"),
963 MockRead("Test Content")};
964
965 StaticSocketDataProvider socket_data(reads, writes);
966 socket_factory_.AddSocketDataProvider(&socket_data);
967
968 TestDelegate delegate;
969 std::unique_ptr<URLRequest> request =
970 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
971 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
972
973 // Explicitly set "Accept-Encoding" to make sure it's not overridden by
974 // AddExtraHeaders
975 HttpRequestHeaders headers;
976 headers.SetHeader("Accept-Encoding", "gzip, deflate");
977 headers.SetHeader("Range", "bytes=0-1023");
978 request->SetExtraRequestHeaders(headers);
979
980 request->Start();
981 delegate.RunUntilComplete();
982
983 EXPECT_THAT(delegate.request_status(), IsOk());
984 EXPECT_EQ(12, request->received_response_content_length());
985 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
986 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
987 }
988
TEST_F(URLRequestHttpJobTest,TestCancelWhileReadingCookies)989 TEST_F(URLRequestHttpJobTest, TestCancelWhileReadingCookies) {
990 auto context_builder = CreateTestURLRequestContextBuilder();
991 context_builder->SetCookieStore(std::make_unique<DelayedCookieMonster>());
992 auto context = context_builder->Build();
993
994 TestDelegate delegate;
995 std::unique_ptr<URLRequest> request =
996 context->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
997 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
998
999 request->Start();
1000 request->Cancel();
1001 delegate.RunUntilComplete();
1002
1003 EXPECT_THAT(delegate.request_status(), IsError(ERR_ABORTED));
1004 }
1005
1006 // Make sure that SetPriority actually sets the URLRequestHttpJob's
1007 // priority, before start. Other tests handle the after start case.
TEST_F(URLRequestHttpJobTest,SetPriorityBasic)1008 TEST_F(URLRequestHttpJobTest, SetPriorityBasic) {
1009 auto job = std::make_unique<TestURLRequestHttpJob>(req_.get());
1010 EXPECT_EQ(DEFAULT_PRIORITY, job->priority());
1011
1012 job->SetPriority(LOWEST);
1013 EXPECT_EQ(LOWEST, job->priority());
1014
1015 job->SetPriority(LOW);
1016 EXPECT_EQ(LOW, job->priority());
1017 }
1018
1019 // Make sure that URLRequestHttpJob passes on its priority to its
1020 // transaction on start.
TEST_F(URLRequestHttpJobTest,SetTransactionPriorityOnStart)1021 TEST_F(URLRequestHttpJobTest, SetTransactionPriorityOnStart) {
1022 TestScopedURLInterceptor interceptor(
1023 req_->url(), std::make_unique<TestURLRequestHttpJob>(req_.get()));
1024 req_->SetPriority(LOW);
1025
1026 EXPECT_FALSE(network_layer().last_transaction());
1027
1028 req_->Start();
1029
1030 ASSERT_TRUE(network_layer().last_transaction());
1031 EXPECT_EQ(LOW, network_layer().last_transaction()->priority());
1032 }
1033
1034 // Make sure that URLRequestHttpJob passes on its priority updates to
1035 // its transaction.
TEST_F(URLRequestHttpJobTest,SetTransactionPriority)1036 TEST_F(URLRequestHttpJobTest, SetTransactionPriority) {
1037 TestScopedURLInterceptor interceptor(
1038 req_->url(), std::make_unique<TestURLRequestHttpJob>(req_.get()));
1039 req_->SetPriority(LOW);
1040 req_->Start();
1041 ASSERT_TRUE(network_layer().last_transaction());
1042 EXPECT_EQ(LOW, network_layer().last_transaction()->priority());
1043
1044 req_->SetPriority(HIGHEST);
1045 EXPECT_EQ(HIGHEST, network_layer().last_transaction()->priority());
1046 }
1047
TEST_F(URLRequestHttpJobTest,HSTSInternalRedirectTest)1048 TEST_F(URLRequestHttpJobTest, HSTSInternalRedirectTest) {
1049 // Setup HSTS state.
1050 context_->transport_security_state()->AddHSTS(
1051 "upgrade.test", base::Time::Now() + base::Seconds(10), true);
1052 ASSERT_TRUE(
1053 context_->transport_security_state()->ShouldUpgradeToSSL("upgrade.test"));
1054 ASSERT_FALSE(context_->transport_security_state()->ShouldUpgradeToSSL(
1055 "no-upgrade.test"));
1056
1057 struct TestCase {
1058 const char* url;
1059 bool upgrade_expected;
1060 const char* url_expected;
1061 } cases[] = {
1062 {"http://upgrade.test/", true, "https://upgrade.test/"},
1063 {"http://upgrade.test:123/", true, "https://upgrade.test:123/"},
1064 {"http://no-upgrade.test/", false, "http://no-upgrade.test/"},
1065 {"http://no-upgrade.test:123/", false, "http://no-upgrade.test:123/"},
1066 #if BUILDFLAG(ENABLE_WEBSOCKETS)
1067 {"ws://upgrade.test/", true, "wss://upgrade.test/"},
1068 {"ws://upgrade.test:123/", true, "wss://upgrade.test:123/"},
1069 {"ws://no-upgrade.test/", false, "ws://no-upgrade.test/"},
1070 {"ws://no-upgrade.test:123/", false, "ws://no-upgrade.test:123/"},
1071 #endif // BUILDFLAG(ENABLE_WEBSOCKETS)
1072 };
1073
1074 for (const auto& test : cases) {
1075 SCOPED_TRACE(test.url);
1076
1077 GURL url = GURL(test.url);
1078 // This is needed to bypass logic that rejects using URLRequests directly
1079 // for WebSocket requests.
1080 bool is_for_websockets = url.SchemeIsWSOrWSS();
1081
1082 TestDelegate d;
1083 TestNetworkDelegate network_delegate;
1084 std::unique_ptr<URLRequest> r(context_->CreateRequest(
1085 url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS,
1086 is_for_websockets));
1087
1088 net_log_observer_.Clear();
1089 r->Start();
1090 d.RunUntilComplete();
1091
1092 if (test.upgrade_expected) {
1093 auto entries = net_log_observer_.GetEntriesWithType(
1094 net::NetLogEventType::URL_REQUEST_REDIRECT_JOB);
1095 int redirects = entries.size();
1096 for (const auto& entry : entries) {
1097 EXPECT_EQ("HSTS", GetStringValueFromParams(entry, "reason"));
1098 }
1099 EXPECT_EQ(1, redirects);
1100 EXPECT_EQ(1, d.received_redirect_count());
1101 EXPECT_EQ(2u, r->url_chain().size());
1102 } else {
1103 EXPECT_EQ(0, d.received_redirect_count());
1104 EXPECT_EQ(1u, r->url_chain().size());
1105 }
1106 EXPECT_EQ(GURL(test.url_expected), r->url());
1107 }
1108 }
1109
TEST_F(URLRequestHttpJobTest,ShouldBypassHSTS)1110 TEST_F(URLRequestHttpJobTest, ShouldBypassHSTS) {
1111 // Setup HSTS state.
1112 context_->transport_security_state()->AddHSTS(
1113 "upgrade.test", base::Time::Now() + base::Seconds(30), true);
1114 ASSERT_TRUE(
1115 context_->transport_security_state()->ShouldUpgradeToSSL("upgrade.test"));
1116
1117 struct TestCase {
1118 const char* url;
1119 bool bypass_hsts;
1120 const char* url_expected;
1121 } cases[] = {
1122 {"http://upgrade.test/example.crl", true,
1123 "http://upgrade.test/example.crl"},
1124 // This test ensures that the HSTS check and upgrade happens prior to cache
1125 // and socket pool checks
1126 {"http://upgrade.test/example.crl", false,
1127 "https://upgrade.test/example.crl"},
1128 {"http://upgrade.test", false, "https://upgrade.test"},
1129 {"http://upgrade.test:1080", false, "https://upgrade.test:1080"},
1130 #if BUILDFLAG(ENABLE_WEBSOCKETS)
1131 {"ws://upgrade.test/example.crl", true, "ws://upgrade.test/example.crl"},
1132 {"ws://upgrade.test/example.crl", false, "wss://upgrade.test/example.crl"},
1133 {"ws://upgrade.test", false, "wss://upgrade.test"},
1134 {"ws://upgrade.test:1080", false, "wss://upgrade.test:1080"},
1135 #endif // BUILDFLAG(ENABLE_WEBSOCKETS)
1136 };
1137
1138 for (const auto& test : cases) {
1139 SCOPED_TRACE(test.url);
1140
1141 GURL url = GURL(test.url);
1142 // This is needed to bypass logic that rejects using URLRequests directly
1143 // for WebSocket requests.
1144 bool is_for_websockets = url.SchemeIsWSOrWSS();
1145
1146 TestDelegate d;
1147 TestNetworkDelegate network_delegate;
1148 std::unique_ptr<URLRequest> r(context_->CreateRequest(
1149 url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS,
1150 is_for_websockets));
1151 if (test.bypass_hsts) {
1152 r->SetLoadFlags(net::LOAD_SHOULD_BYPASS_HSTS);
1153 r->set_allow_credentials(false);
1154 }
1155
1156 net_log_observer_.Clear();
1157 r->Start();
1158 d.RunUntilComplete();
1159
1160 if (test.bypass_hsts) {
1161 EXPECT_EQ(0, d.received_redirect_count());
1162 EXPECT_EQ(1u, r->url_chain().size());
1163 } else {
1164 auto entries = net_log_observer_.GetEntriesWithType(
1165 net::NetLogEventType::URL_REQUEST_REDIRECT_JOB);
1166 int redirects = entries.size();
1167 for (const auto& entry : entries) {
1168 EXPECT_EQ("HSTS", GetStringValueFromParams(entry, "reason"));
1169 }
1170 EXPECT_EQ(1, redirects);
1171 EXPECT_EQ(1, d.received_redirect_count());
1172 EXPECT_EQ(2u, r->url_chain().size());
1173 }
1174 EXPECT_EQ(GURL(test.url_expected), r->url());
1175 }
1176 }
1177
1178 #if BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS)
1179
1180 class URLRequestHttpJobWithMockSocketsDeviceBoundSessionServiceTest
1181 : public TestWithTaskEnvironment {
1182 protected:
URLRequestHttpJobWithMockSocketsDeviceBoundSessionServiceTest()1183 URLRequestHttpJobWithMockSocketsDeviceBoundSessionServiceTest() {
1184 auto context_builder = CreateTestURLRequestContextBuilder();
1185 context_builder->set_client_socket_factory_for_testing(&socket_factory_);
1186 context_builder->set_device_bound_session_service(
1187 std::make_unique<testing::StrictMock<DeviceBoundSessionServiceMock>>());
1188 context_ = context_builder->Build();
1189 request_ = context_->CreateRequest(GURL("http://www.example.com"),
1190 DEFAULT_PRIORITY, &delegate_,
1191 TRAFFIC_ANNOTATION_FOR_TESTS);
1192 }
1193
GetMockService()1194 DeviceBoundSessionServiceMock& GetMockService() {
1195 return *static_cast<DeviceBoundSessionServiceMock*>(
1196 context_->device_bound_session_service());
1197 }
1198
1199 MockClientSocketFactory socket_factory_;
1200 std::unique_ptr<URLRequestContext> context_;
1201 TestDelegate delegate_;
1202 std::unique_ptr<URLRequest> request_;
1203 };
1204
TEST_F(URLRequestHttpJobWithMockSocketsDeviceBoundSessionServiceTest,ShouldRespondToDeviceBoundSessionHeader)1205 TEST_F(URLRequestHttpJobWithMockSocketsDeviceBoundSessionServiceTest,
1206 ShouldRespondToDeviceBoundSessionHeader) {
1207 const MockWrite writes[] = {
1208 MockWrite("GET / HTTP/1.1\r\n"
1209 "Host: www.example.com\r\n"
1210 "Connection: keep-alive\r\n"
1211 "User-Agent: \r\n"
1212 "Accept-Encoding: gzip, deflate\r\n"
1213 "Accept-Language: en-us,fr\r\n\r\n")};
1214
1215 const MockRead reads[] = {
1216 MockRead(
1217 "HTTP/1.1 200 OK\r\n"
1218 "Accept-Ranges: bytes\r\n"
1219 "Sec-Session-Registration: \"new\";challenge=:Y29kZWQ=:;es256\r\n"
1220 "Content-Length: 12\r\n\r\n"),
1221 MockRead("Test Content")};
1222
1223 StaticSocketDataProvider socket_data(reads, writes);
1224 socket_factory_.AddSocketDataProvider(&socket_data);
1225
1226 request_->Start();
1227 EXPECT_CALL(GetMockService(), RegisterBoundSession).Times(1);
1228 delegate_.RunUntilComplete();
1229 EXPECT_THAT(delegate_.request_status(), IsOk());
1230 }
1231
TEST_F(URLRequestHttpJobWithMockSocketsDeviceBoundSessionServiceTest,ShouldNotRespondWithoutDeviceBoundSessionHeader)1232 TEST_F(URLRequestHttpJobWithMockSocketsDeviceBoundSessionServiceTest,
1233 ShouldNotRespondWithoutDeviceBoundSessionHeader) {
1234 const MockWrite writes[] = {
1235 MockWrite("GET / HTTP/1.1\r\n"
1236 "Host: www.example.com\r\n"
1237 "Connection: keep-alive\r\n"
1238 "User-Agent: \r\n"
1239 "Accept-Encoding: gzip, deflate\r\n"
1240 "Accept-Language: en-us,fr\r\n\r\n")};
1241
1242 const MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
1243 "Accept-Ranges: bytes\r\n"
1244 "Content-Length: 12\r\n\r\n"),
1245 MockRead("Test Content")};
1246
1247 StaticSocketDataProvider socket_data(reads, writes);
1248 socket_factory_.AddSocketDataProvider(&socket_data);
1249
1250 request_->Start();
1251 EXPECT_CALL(GetMockService(), RegisterBoundSession).Times(0);
1252 delegate_.RunUntilComplete();
1253 EXPECT_THAT(delegate_.request_status(), IsOk());
1254 }
1255
1256 #endif // BUILDFLAG(ENABLE_DEVICE_BOUND_SESSIONS)
1257
1258 namespace {
HandleRequest(const std::string_view & content,const test_server::HttpRequest & request)1259 std::unique_ptr<test_server::HttpResponse> HandleRequest(
1260 const std::string_view& content,
1261 const test_server::HttpRequest& request) {
1262 auto response = std::make_unique<test_server::BasicHttpResponse>();
1263 response->set_content(content);
1264 return std::move(response);
1265 }
1266 } // namespace
1267
1268 // This test checks that if an HTTP connection was made for a request that has
1269 // the should_bypass_hsts flag set to true, subsequent calls to the exact same
1270 // URL WITHOUT should_bypass_hsts=true will be upgraded to HTTPS early
1271 // enough in the process such that the HTTP socket connection is not re-used,
1272 // and the request does not have a hit in the cache.
TEST_F(URLRequestHttpJobTest,ShouldBypassHSTSResponseAndConnectionNotReused)1273 TEST_F(URLRequestHttpJobTest, ShouldBypassHSTSResponseAndConnectionNotReused) {
1274 constexpr std::string_view kSecureContent = "Secure: Okay Content";
1275 constexpr std::string_view kInsecureContent = "Insecure: Bad Content";
1276
1277 auto context_builder = CreateTestURLRequestContextBuilder();
1278 auto context = context_builder->Build();
1279
1280 // The host of all EmbeddedTestServer URLs is 127.0.0.1.
1281 context->transport_security_state()->AddHSTS(
1282 "127.0.0.1", base::Time::Now() + base::Seconds(30), true);
1283 ASSERT_TRUE(
1284 context->transport_security_state()->ShouldUpgradeToSSL("127.0.0.1"));
1285
1286 GURL::Replacements replace_scheme;
1287 replace_scheme.SetSchemeStr("https");
1288 GURL insecure_url;
1289 GURL secure_url;
1290
1291 int common_port = 0;
1292
1293 // Create an HTTP request that is not upgraded to the should_bypass_hsts flag,
1294 // and ensure that the response is stored in the cache.
1295 {
1296 EmbeddedTestServer http_server(EmbeddedTestServer::TYPE_HTTP);
1297 http_server.AddDefaultHandlers(base::FilePath());
1298 http_server.RegisterRequestHandler(
1299 base::BindRepeating(&HandleRequest, kInsecureContent));
1300 ASSERT_TRUE(http_server.Start());
1301 common_port = http_server.port();
1302
1303 insecure_url = http_server.base_url();
1304 ASSERT_TRUE(insecure_url.SchemeIs("http"));
1305 secure_url = insecure_url.ReplaceComponents(replace_scheme);
1306 ASSERT_TRUE(secure_url.SchemeIs("https"));
1307
1308 net_log_observer_.Clear();
1309 TestDelegate delegate;
1310 std::unique_ptr<URLRequest> req(
1311 context->CreateRequest(insecure_url, DEFAULT_PRIORITY, &delegate,
1312 TRAFFIC_ANNOTATION_FOR_TESTS));
1313 req->SetLoadFlags(net::LOAD_SHOULD_BYPASS_HSTS);
1314 req->set_allow_credentials(false);
1315 req->Start();
1316 delegate.RunUntilComplete();
1317 EXPECT_EQ(kInsecureContent, delegate.data_received());
1318 // There should be 2 cache event entries, one for beginning the read and one
1319 // for finishing the read.
1320 EXPECT_EQ(2u, net_log_observer_
1321 .GetEntriesWithType(
1322 net::NetLogEventType::HTTP_CACHE_ADD_TO_ENTRY)
1323 .size());
1324 ASSERT_TRUE(http_server.ShutdownAndWaitUntilComplete());
1325 }
1326 // Test that a request with the same URL will be upgraded as long as
1327 // should_bypass_hsts flag is not set, and doesn't have an cache hit or
1328 // re-use an existing socket connection.
1329 {
1330 EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS);
1331 https_server.AddDefaultHandlers(base::FilePath());
1332 https_server.RegisterRequestHandler(
1333 base::BindRepeating(&HandleRequest, kSecureContent));
1334 ASSERT_TRUE(https_server.Start(common_port));
1335
1336 TestDelegate delegate;
1337 std::unique_ptr<URLRequest> req(
1338 context->CreateRequest(insecure_url, DEFAULT_PRIORITY, &delegate,
1339 TRAFFIC_ANNOTATION_FOR_TESTS));
1340 req->set_allow_credentials(false);
1341 req->Start();
1342 delegate.RunUntilRedirect();
1343 // Ensure that the new URL has an upgraded protocol. This ensures that when
1344 // the redirect request continues, the HTTP socket connection from before
1345 // will not be re-used, given that "protocol" is one of the fields used to
1346 // create a socket connection. Documentation here:
1347 // https://chromium.googlesource.com/chromium/src/+/HEAD/net/docs/life-of-a-url-request.md
1348 // under "Socket Pools" section.
1349 EXPECT_EQ(delegate.redirect_info().new_url, secure_url);
1350 EXPECT_TRUE(delegate.redirect_info().new_url.SchemeIs("https"));
1351 EXPECT_THAT(delegate.request_status(), net::ERR_IO_PENDING);
1352
1353 req->FollowDeferredRedirect(std::nullopt /* removed_headers */,
1354 std::nullopt /* modified_headers */);
1355 delegate.RunUntilComplete();
1356 EXPECT_EQ(kSecureContent, delegate.data_received());
1357 EXPECT_FALSE(req->was_cached());
1358 ASSERT_TRUE(https_server.ShutdownAndWaitUntilComplete());
1359 }
1360 }
1361
TEST_F(URLRequestHttpJobTest,HSTSInternalRedirectCallback)1362 TEST_F(URLRequestHttpJobTest, HSTSInternalRedirectCallback) {
1363 EmbeddedTestServer https_test(EmbeddedTestServer::TYPE_HTTPS);
1364 https_test.AddDefaultHandlers(base::FilePath());
1365 ASSERT_TRUE(https_test.Start());
1366
1367 auto context = CreateTestURLRequestContextBuilder()->Build();
1368 context->transport_security_state()->AddHSTS(
1369 "127.0.0.1", base::Time::Now() + base::Seconds(10), true);
1370 ASSERT_TRUE(
1371 context->transport_security_state()->ShouldUpgradeToSSL("127.0.0.1"));
1372
1373 GURL::Replacements replace_scheme;
1374 replace_scheme.SetSchemeStr("http");
1375
1376 {
1377 GURL url(
1378 https_test.GetURL("/echoheader").ReplaceComponents(replace_scheme));
1379 TestDelegate delegate;
1380 HttpRequestHeaders extra_headers;
1381 extra_headers.SetHeader("X-HSTS-Test", "1");
1382
1383 HttpRawRequestHeaders raw_req_headers;
1384
1385 std::unique_ptr<URLRequest> r(context->CreateRequest(
1386 url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
1387 r->SetExtraRequestHeaders(extra_headers);
1388 r->SetRequestHeadersCallback(base::BindRepeating(
1389 &HttpRawRequestHeaders::Assign, base::Unretained(&raw_req_headers)));
1390
1391 r->Start();
1392 delegate.RunUntilRedirect();
1393
1394 EXPECT_FALSE(raw_req_headers.headers().empty());
1395 std::string value;
1396 EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-HSTS-Test", &value));
1397 EXPECT_EQ("1", value);
1398 EXPECT_EQ("GET /echoheader HTTP/1.1\r\n", raw_req_headers.request_line());
1399
1400 raw_req_headers = HttpRawRequestHeaders();
1401
1402 r->FollowDeferredRedirect(std::nullopt /* removed_headers */,
1403 std::nullopt /* modified_headers */);
1404 delegate.RunUntilComplete();
1405
1406 EXPECT_FALSE(raw_req_headers.headers().empty());
1407 }
1408
1409 {
1410 GURL url(https_test.GetURL("/echoheader?foo=bar")
1411 .ReplaceComponents(replace_scheme));
1412 TestDelegate delegate;
1413
1414 HttpRawRequestHeaders raw_req_headers;
1415
1416 std::unique_ptr<URLRequest> r(context->CreateRequest(
1417 url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
1418 r->SetRequestHeadersCallback(base::BindRepeating(
1419 &HttpRawRequestHeaders::Assign, base::Unretained(&raw_req_headers)));
1420
1421 r->Start();
1422 delegate.RunUntilRedirect();
1423
1424 EXPECT_EQ("GET /echoheader?foo=bar HTTP/1.1\r\n",
1425 raw_req_headers.request_line());
1426 }
1427
1428 {
1429 GURL url(
1430 https_test.GetURL("/echoheader#foo").ReplaceComponents(replace_scheme));
1431 TestDelegate delegate;
1432
1433 HttpRawRequestHeaders raw_req_headers;
1434
1435 std::unique_ptr<URLRequest> r(context->CreateRequest(
1436 url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
1437 r->SetRequestHeadersCallback(base::BindRepeating(
1438 &HttpRawRequestHeaders::Assign, base::Unretained(&raw_req_headers)));
1439
1440 r->Start();
1441 delegate.RunUntilRedirect();
1442
1443 EXPECT_EQ("GET /echoheader HTTP/1.1\r\n", raw_req_headers.request_line());
1444 }
1445 }
1446
1447 class URLRequestHttpJobWithBrotliSupportTest : public TestWithTaskEnvironment {
1448 protected:
URLRequestHttpJobWithBrotliSupportTest()1449 URLRequestHttpJobWithBrotliSupportTest() {
1450 HttpNetworkSessionParams params;
1451 auto context_builder = CreateTestURLRequestContextBuilder();
1452 context_builder->set_enable_brotli(true);
1453 context_builder->set_http_network_session_params(params);
1454 context_builder->set_client_socket_factory_for_testing(&socket_factory_);
1455 context_ = context_builder->Build();
1456 }
1457
1458 MockClientSocketFactory socket_factory_;
1459 std::unique_ptr<URLRequestContext> context_;
1460 };
1461
TEST_F(URLRequestHttpJobWithBrotliSupportTest,NoBrotliAdvertisementOverHttp)1462 TEST_F(URLRequestHttpJobWithBrotliSupportTest, NoBrotliAdvertisementOverHttp) {
1463 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
1464 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
1465 "Content-Length: 12\r\n\r\n"),
1466 MockRead("Test Content")};
1467 StaticSocketDataProvider socket_data(reads, writes);
1468 socket_factory_.AddSocketDataProvider(&socket_data);
1469
1470 TestDelegate delegate;
1471 std::unique_ptr<URLRequest> request =
1472 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
1473 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
1474 request->Start();
1475 delegate.RunUntilComplete();
1476
1477 EXPECT_THAT(delegate.request_status(), IsOk());
1478 EXPECT_EQ(12, request->received_response_content_length());
1479 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
1480 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
1481 }
1482
TEST_F(URLRequestHttpJobWithBrotliSupportTest,BrotliAdvertisement)1483 TEST_F(URLRequestHttpJobWithBrotliSupportTest, BrotliAdvertisement) {
1484 net::SSLSocketDataProvider ssl_socket_data_provider(net::ASYNC, net::OK);
1485 ssl_socket_data_provider.next_proto = kProtoHTTP11;
1486 ssl_socket_data_provider.ssl_info.cert =
1487 ImportCertFromFile(GetTestCertsDirectory(), "unittest.selfsigned.der");
1488 ASSERT_TRUE(ssl_socket_data_provider.ssl_info.cert);
1489 socket_factory_.AddSSLSocketDataProvider(&ssl_socket_data_provider);
1490
1491 MockWrite writes[] = {
1492 MockWrite("GET / HTTP/1.1\r\n"
1493 "Host: www.example.com\r\n"
1494 "Connection: keep-alive\r\n"
1495 "User-Agent: \r\n"
1496 "Accept-Encoding: gzip, deflate, br\r\n"
1497 "Accept-Language: en-us,fr\r\n\r\n")};
1498 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
1499 "Content-Length: 12\r\n\r\n"),
1500 MockRead("Test Content")};
1501 StaticSocketDataProvider socket_data(reads, writes);
1502 socket_factory_.AddSocketDataProvider(&socket_data);
1503
1504 TestDelegate delegate;
1505 std::unique_ptr<URLRequest> request =
1506 context_->CreateRequest(GURL("https://www.example.com"), DEFAULT_PRIORITY,
1507 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
1508 request->Start();
1509 delegate.RunUntilComplete();
1510
1511 EXPECT_THAT(delegate.request_status(), IsOk());
1512 EXPECT_EQ(12, request->received_response_content_length());
1513 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
1514 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
1515 }
1516
TEST_F(URLRequestHttpJobWithBrotliSupportTest,DefaultAcceptEncodingOverriden)1517 TEST_F(URLRequestHttpJobWithBrotliSupportTest, DefaultAcceptEncodingOverriden) {
1518 struct {
1519 base::flat_set<net::SourceStream::SourceType> accepted_types;
1520 const char* expected_request_headers;
1521 } kTestCases[] = {{{net::SourceStream::SourceType::TYPE_DEFLATE},
1522 "GET / HTTP/1.1\r\n"
1523 "Host: www.example.com\r\n"
1524 "Connection: keep-alive\r\n"
1525 "User-Agent: \r\n"
1526 "Accept-Encoding: deflate\r\n"
1527 "Accept-Language: en-us,fr\r\n\r\n"},
1528 {{},
1529 "GET / HTTP/1.1\r\n"
1530 "Host: www.example.com\r\n"
1531 "Connection: keep-alive\r\n"
1532 "User-Agent: \r\n"
1533 "Accept-Language: en-us,fr\r\n\r\n"},
1534 {{net::SourceStream::SourceType::TYPE_GZIP},
1535 "GET / HTTP/1.1\r\n"
1536 "Host: www.example.com\r\n"
1537 "Connection: keep-alive\r\n"
1538 "User-Agent: \r\n"
1539 "Accept-Encoding: gzip\r\n"
1540 "Accept-Language: en-us,fr\r\n\r\n"},
1541 {{net::SourceStream::SourceType::TYPE_GZIP,
1542 net::SourceStream::SourceType::TYPE_DEFLATE},
1543 "GET / HTTP/1.1\r\n"
1544 "Host: www.example.com\r\n"
1545 "Connection: keep-alive\r\n"
1546 "User-Agent: \r\n"
1547 "Accept-Encoding: gzip, deflate\r\n"
1548 "Accept-Language: en-us,fr\r\n\r\n"},
1549 {{net::SourceStream::SourceType::TYPE_BROTLI},
1550 "GET / HTTP/1.1\r\n"
1551 "Host: www.example.com\r\n"
1552 "Connection: keep-alive\r\n"
1553 "User-Agent: \r\n"
1554 "Accept-Encoding: br\r\n"
1555 "Accept-Language: en-us,fr\r\n\r\n"},
1556 {{net::SourceStream::SourceType::TYPE_BROTLI,
1557 net::SourceStream::SourceType::TYPE_GZIP,
1558 net::SourceStream::SourceType::TYPE_DEFLATE},
1559 "GET / HTTP/1.1\r\n"
1560 "Host: www.example.com\r\n"
1561 "Connection: keep-alive\r\n"
1562 "User-Agent: \r\n"
1563 "Accept-Encoding: gzip, deflate, br\r\n"
1564 "Accept-Language: en-us,fr\r\n\r\n"}};
1565
1566 for (auto test : kTestCases) {
1567 net::SSLSocketDataProvider ssl_socket_data_provider(net::ASYNC, net::OK);
1568 ssl_socket_data_provider.next_proto = kProtoHTTP11;
1569 ssl_socket_data_provider.ssl_info.cert =
1570 ImportCertFromFile(GetTestCertsDirectory(), "unittest.selfsigned.der");
1571 ASSERT_TRUE(ssl_socket_data_provider.ssl_info.cert);
1572 socket_factory_.AddSSLSocketDataProvider(&ssl_socket_data_provider);
1573
1574 MockWrite writes[] = {MockWrite(test.expected_request_headers)};
1575 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
1576 "Content-Length: 12\r\n\r\n"),
1577 MockRead("Test Content")};
1578 StaticSocketDataProvider socket_data(reads, writes);
1579 socket_factory_.AddSocketDataProvider(&socket_data);
1580
1581 TestDelegate delegate;
1582 std::unique_ptr<URLRequest> request = context_->CreateRequest(
1583 GURL("https://www.example.com"), DEFAULT_PRIORITY, &delegate,
1584 TRAFFIC_ANNOTATION_FOR_TESTS);
1585 request->set_accepted_stream_types(test.accepted_types);
1586 request->Start();
1587 delegate.RunUntilComplete();
1588 EXPECT_THAT(delegate.request_status(), IsOk());
1589 socket_factory_.ResetNextMockIndexes();
1590 }
1591 }
1592
1593 #if BUILDFLAG(IS_ANDROID)
1594 class URLRequestHttpJobWithCheckClearTextPermittedTest
1595 : public TestWithTaskEnvironment {
1596 protected:
URLRequestHttpJobWithCheckClearTextPermittedTest()1597 URLRequestHttpJobWithCheckClearTextPermittedTest() {
1598 auto context_builder = CreateTestURLRequestContextBuilder();
1599 context_builder->SetHttpTransactionFactoryForTesting(
1600 std::make_unique<MockNetworkLayer>());
1601 context_builder->set_check_cleartext_permitted(true);
1602 context_builder->set_client_socket_factory_for_testing(&socket_factory_);
1603 context_ = context_builder->Build();
1604 }
1605
1606 MockClientSocketFactory socket_factory_;
1607 std::unique_ptr<URLRequestContext> context_;
1608 };
1609
TEST_F(URLRequestHttpJobWithCheckClearTextPermittedTest,AndroidCleartextPermittedTest)1610 TEST_F(URLRequestHttpJobWithCheckClearTextPermittedTest,
1611 AndroidCleartextPermittedTest) {
1612 static constexpr struct TestCase {
1613 const char* url;
1614 bool cleartext_permitted;
1615 bool should_block;
1616 int expected_per_host_call_count;
1617 int expected_default_call_count;
1618 } kTestCases[] = {
1619 {"http://unblocked.test/", true, false, 1, 0},
1620 {"https://unblocked.test/", true, false, 0, 0},
1621 {"http://blocked.test/", false, true, 1, 0},
1622 {"https://blocked.test/", false, false, 0, 0},
1623 // If determining the per-host cleartext policy causes an
1624 // IllegalArgumentException (because the hostname is invalid),
1625 // the default configuration should be applied, and the
1626 // exception should not cause a JNI error.
1627 {"http://./", false, true, 1, 1},
1628 {"http://./", true, false, 1, 1},
1629 // Even if the host name would be considered invalid, https
1630 // schemes should not trigger cleartext policy checks.
1631 {"https://./", false, false, 0, 0},
1632 };
1633
1634 JNIEnv* env = base::android::AttachCurrentThread();
1635 for (const TestCase& test : kTestCases) {
1636 Java_AndroidNetworkLibraryTestUtil_setUpSecurityPolicyForTesting(
1637 env, test.cleartext_permitted);
1638
1639 TestDelegate delegate;
1640 std::unique_ptr<URLRequest> request =
1641 context_->CreateRequest(GURL(test.url), DEFAULT_PRIORITY, &delegate,
1642 TRAFFIC_ANNOTATION_FOR_TESTS);
1643 request->Start();
1644 delegate.RunUntilComplete();
1645
1646 if (test.should_block) {
1647 EXPECT_THAT(delegate.request_status(),
1648 IsError(ERR_CLEARTEXT_NOT_PERMITTED));
1649 } else {
1650 // Should fail since there's no test server running
1651 EXPECT_THAT(delegate.request_status(), IsError(ERR_FAILED));
1652 }
1653 EXPECT_EQ(
1654 Java_AndroidNetworkLibraryTestUtil_getPerHostCleartextCheckCount(env),
1655 test.expected_per_host_call_count);
1656 EXPECT_EQ(
1657 Java_AndroidNetworkLibraryTestUtil_getDefaultCleartextCheckCount(env),
1658 test.expected_default_call_count);
1659 }
1660 }
1661 #endif
1662
1663 #if BUILDFLAG(ENABLE_WEBSOCKETS)
1664
1665 class URLRequestHttpJobWebSocketTest : public TestWithTaskEnvironment {
1666 protected:
URLRequestHttpJobWebSocketTest()1667 URLRequestHttpJobWebSocketTest() {
1668 auto context_builder = CreateTestURLRequestContextBuilder();
1669 context_builder->set_client_socket_factory_for_testing(&socket_factory_);
1670 context_ = context_builder->Build();
1671 req_ =
1672 context_->CreateRequest(GURL("ws://www.example.org"), DEFAULT_PRIORITY,
1673 &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS,
1674 /*is_for_websockets=*/true);
1675 }
1676
1677 std::unique_ptr<URLRequestContext> context_;
1678 MockClientSocketFactory socket_factory_;
1679 TestDelegate delegate_;
1680 std::unique_ptr<URLRequest> req_;
1681 };
1682
TEST_F(URLRequestHttpJobWebSocketTest,RejectedWithoutCreateHelper)1683 TEST_F(URLRequestHttpJobWebSocketTest, RejectedWithoutCreateHelper) {
1684 req_->Start();
1685 delegate_.RunUntilComplete();
1686 EXPECT_THAT(delegate_.request_status(), IsError(ERR_DISALLOWED_URL_SCHEME));
1687 }
1688
TEST_F(URLRequestHttpJobWebSocketTest,CreateHelperPassedThrough)1689 TEST_F(URLRequestHttpJobWebSocketTest, CreateHelperPassedThrough) {
1690 HttpRequestHeaders headers;
1691 headers.SetHeader("Connection", "Upgrade");
1692 headers.SetHeader("Upgrade", "websocket");
1693 headers.SetHeader("Origin", "http://www.example.org");
1694 headers.SetHeader("Sec-WebSocket-Version", "13");
1695 req_->SetExtraRequestHeaders(headers);
1696
1697 MockWrite writes[] = {
1698 MockWrite("GET / HTTP/1.1\r\n"
1699 "Host: www.example.org\r\n"
1700 "Connection: Upgrade\r\n"
1701 "Upgrade: websocket\r\n"
1702 "Origin: http://www.example.org\r\n"
1703 "Sec-WebSocket-Version: 13\r\n"
1704 "User-Agent: \r\n"
1705 "Accept-Encoding: gzip, deflate\r\n"
1706 "Accept-Language: en-us,fr\r\n"
1707 "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
1708 "Sec-WebSocket-Extensions: permessage-deflate; "
1709 "client_max_window_bits\r\n\r\n")};
1710
1711 MockRead reads[] = {
1712 MockRead("HTTP/1.1 101 Switching Protocols\r\n"
1713 "Upgrade: websocket\r\n"
1714 "Connection: Upgrade\r\n"
1715 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n"),
1716 MockRead(ASYNC, 0)};
1717
1718 StaticSocketDataProvider data(reads, writes);
1719 socket_factory_.AddSocketDataProvider(&data);
1720
1721 auto websocket_stream_create_helper =
1722 std::make_unique<TestWebSocketHandshakeStreamCreateHelper>();
1723
1724 req_->SetUserData(kWebSocketHandshakeUserDataKey,
1725 std::move(websocket_stream_create_helper));
1726 req_->SetLoadFlags(LOAD_DISABLE_CACHE);
1727 req_->Start();
1728 delegate_.RunUntilComplete();
1729 EXPECT_THAT(delegate_.request_status(), IsOk());
1730 EXPECT_TRUE(delegate_.response_completed());
1731
1732 EXPECT_TRUE(data.AllWriteDataConsumed());
1733 EXPECT_TRUE(data.AllReadDataConsumed());
1734 }
1735
1736 #endif // BUILDFLAG(ENABLE_WEBSOCKETS)
1737
SetAllCookies(CookieMonster * cm,const CookieList & list)1738 bool SetAllCookies(CookieMonster* cm, const CookieList& list) {
1739 DCHECK(cm);
1740 ResultSavingCookieCallback<CookieAccessResult> callback;
1741 cm->SetAllCookiesAsync(list, callback.MakeCallback());
1742 callback.WaitUntilDone();
1743 return callback.result().status.IsInclude();
1744 }
1745
CreateAndSetCookie(CookieStore * cs,const GURL & url,const std::string & cookie_line)1746 bool CreateAndSetCookie(CookieStore* cs,
1747 const GURL& url,
1748 const std::string& cookie_line) {
1749 auto cookie =
1750 CanonicalCookie::CreateForTesting(url, cookie_line, base::Time::Now());
1751 if (!cookie)
1752 return false;
1753 DCHECK(cs);
1754 ResultSavingCookieCallback<CookieAccessResult> callback;
1755 cs->SetCanonicalCookieAsync(std::move(cookie), url,
1756 CookieOptions::MakeAllInclusive(),
1757 callback.MakeCallback());
1758 callback.WaitUntilDone();
1759 return callback.result().status.IsInclude();
1760 }
1761
RunRequest(URLRequestContext * context,const GURL & url)1762 void RunRequest(URLRequestContext* context, const GURL& url) {
1763 TestDelegate delegate;
1764 std::unique_ptr<URLRequest> request = context->CreateRequest(
1765 url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
1766
1767 // Make this a laxly same-site context to allow setting
1768 // SameSite=Lax-by-default cookies.
1769 request->set_site_for_cookies(SiteForCookies::FromUrl(url));
1770 request->Start();
1771 delegate.RunUntilComplete();
1772 }
1773
1774 } // namespace
1775
TEST_F(URLRequestHttpJobTest,CookieSchemeRequestSchemeHistogram)1776 TEST_F(URLRequestHttpJobTest, CookieSchemeRequestSchemeHistogram) {
1777 base::HistogramTester histograms;
1778 const std::string test_histogram = "Cookie.CookieSchemeRequestScheme";
1779
1780 auto context_builder = CreateTestURLRequestContextBuilder();
1781 context_builder->SetCookieStore(std::make_unique<CookieMonster>(
1782 /*store=*/nullptr, /*net_log=*/nullptr));
1783 auto context = context_builder->Build();
1784
1785 auto* cookie_store = static_cast<CookieMonster*>(context->cookie_store());
1786
1787 // Secure set cookie marked as Unset source scheme.
1788 // Using port 7 because it fails the transaction without sending a request and
1789 // prevents a timeout due to the fake addresses. Because we only need the
1790 // headers to be generated (and thus the histogram filled) and not actually
1791 // sent this is acceptable.
1792 GURL nonsecure_url_for_unset1("http://unset1.example:7");
1793 GURL secure_url_for_unset1("https://unset1.example:7");
1794
1795 // Normally the source scheme would be set by
1796 // CookieMonster::SetCanonicalCookie(), however we're using SetAllCookies() to
1797 // bypass the source scheme check in order to test the kUnset state which
1798 // would normally only happen during an existing cookie DB version upgrade.
1799 std::unique_ptr<CanonicalCookie> unset_cookie1 =
1800 CanonicalCookie::CreateForTesting(
1801 secure_url_for_unset1, "NoSourceSchemeHttps=val", base::Time::Now());
1802 unset_cookie1->SetSourceScheme(net::CookieSourceScheme::kUnset);
1803
1804 CookieList list1 = {*unset_cookie1};
1805 EXPECT_TRUE(SetAllCookies(cookie_store, list1));
1806 RunRequest(context.get(), nonsecure_url_for_unset1);
1807 histograms.ExpectBucketCount(
1808 test_histogram,
1809 URLRequestHttpJob::CookieRequestScheme::kUnsetCookieScheme, 1);
1810 RunRequest(context.get(), secure_url_for_unset1);
1811 histograms.ExpectBucketCount(
1812 test_histogram,
1813 URLRequestHttpJob::CookieRequestScheme::kUnsetCookieScheme, 2);
1814
1815 // Nonsecure set cookie marked as unset source scheme.
1816 GURL nonsecure_url_for_unset2("http://unset2.example:7");
1817 GURL secure_url_for_unset2("https://unset2.example:7");
1818
1819 std::unique_ptr<CanonicalCookie> unset_cookie2 =
1820 CanonicalCookie::CreateForTesting(nonsecure_url_for_unset2,
1821 "NoSourceSchemeHttp=val",
1822 base::Time::Now());
1823 unset_cookie2->SetSourceScheme(net::CookieSourceScheme::kUnset);
1824
1825 CookieList list2 = {*unset_cookie2};
1826 EXPECT_TRUE(SetAllCookies(cookie_store, list2));
1827 RunRequest(context.get(), nonsecure_url_for_unset2);
1828 histograms.ExpectBucketCount(
1829 test_histogram,
1830 URLRequestHttpJob::CookieRequestScheme::kUnsetCookieScheme, 3);
1831 RunRequest(context.get(), secure_url_for_unset2);
1832 histograms.ExpectBucketCount(
1833 test_histogram,
1834 URLRequestHttpJob::CookieRequestScheme::kUnsetCookieScheme, 4);
1835
1836 // Secure set cookie with source scheme marked appropriately.
1837 GURL nonsecure_url_for_secure_set("http://secureset.example:7");
1838 GURL secure_url_for_secure_set("https://secureset.example:7");
1839
1840 EXPECT_TRUE(CreateAndSetCookie(cookie_store, secure_url_for_secure_set,
1841 "SecureScheme=val"));
1842 RunRequest(context.get(), nonsecure_url_for_secure_set);
1843 histograms.ExpectBucketCount(
1844 test_histogram,
1845 URLRequestHttpJob::CookieRequestScheme::kSecureSetNonsecureRequest, 1);
1846 RunRequest(context.get(), secure_url_for_secure_set);
1847 histograms.ExpectBucketCount(
1848 test_histogram,
1849 URLRequestHttpJob::CookieRequestScheme::kSecureSetSecureRequest, 1);
1850
1851 // Nonsecure set cookie with source scheme marked appropriately.
1852 GURL nonsecure_url_for_nonsecure_set("http://nonsecureset.example:7");
1853 GURL secure_url_for_nonsecure_set("https://nonsecureset.example:7");
1854
1855 EXPECT_TRUE(CreateAndSetCookie(cookie_store, nonsecure_url_for_nonsecure_set,
1856 "NonSecureScheme=val"));
1857 RunRequest(context.get(), nonsecure_url_for_nonsecure_set);
1858 histograms.ExpectBucketCount(
1859 test_histogram,
1860 URLRequestHttpJob::CookieRequestScheme::kNonsecureSetNonsecureRequest, 1);
1861 RunRequest(context.get(), secure_url_for_nonsecure_set);
1862 histograms.ExpectBucketCount(
1863 test_histogram,
1864 URLRequestHttpJob::CookieRequestScheme::kNonsecureSetSecureRequest, 1);
1865 }
1866
1867 // Test that cookies are annotated with the appropriate exclusion reason when
1868 // privacy mode is enabled.
TEST_F(URLRequestHttpJobTest,PrivacyMode_ExclusionReason)1869 TEST_F(URLRequestHttpJobTest, PrivacyMode_ExclusionReason) {
1870 HttpTestServer test_server;
1871 ASSERT_TRUE(test_server.Start());
1872
1873 auto context_builder = CreateTestURLRequestContextBuilder();
1874 context_builder->SetCookieStore(std::make_unique<CookieMonster>(
1875 /*store=*/nullptr, /*net_log=*/nullptr));
1876 auto& network_delegate = *context_builder->set_network_delegate(
1877 std::make_unique<FilteringTestNetworkDelegate>());
1878 auto context = context_builder->Build();
1879
1880 // Set cookies.
1881 {
1882 TestDelegate d;
1883 GURL test_url = test_server.GetURL(
1884 "/set-cookie?one=1&"
1885 "two=2&"
1886 "three=3");
1887 std::unique_ptr<URLRequest> req =
1888 CreateFirstPartyRequest(*context, test_url, &d);
1889 req->Start();
1890 d.RunUntilComplete();
1891 }
1892
1893 // Get cookies.
1894 network_delegate.ResetAnnotateCookiesCalledCount();
1895 ASSERT_EQ(0, network_delegate.annotate_cookies_called_count());
1896 // We want to fetch cookies from the cookie store, so we use the
1897 // NetworkDelegate to override the privacy mode (rather than setting it via
1898 // `allow_credentials`, since that skips querying the cookie store).
1899 network_delegate.set_force_privacy_mode(true);
1900 TestDelegate d;
1901 std::unique_ptr<URLRequest> req = CreateFirstPartyRequest(
1902 *context, test_server.GetURL("/echoheader?Cookie"), &d);
1903 req->Start();
1904 d.RunUntilComplete();
1905
1906 EXPECT_EQ("None", d.data_received());
1907 EXPECT_THAT(
1908 req->maybe_sent_cookies(),
1909 UnorderedElementsAre(
1910 MatchesCookieWithAccessResult(
1911 MatchesCookieWithNameSourceType("one", CookieSourceType::kHTTP),
1912 MatchesCookieAccessResult(
1913 HasExactlyExclusionReasonsForTesting(
1914 std::vector<CookieInclusionStatus::ExclusionReason>{
1915 CookieInclusionStatus::EXCLUDE_USER_PREFERENCES}),
1916 _, _, _)),
1917 MatchesCookieWithAccessResult(
1918 MatchesCookieWithNameSourceType("two", CookieSourceType::kHTTP),
1919 MatchesCookieAccessResult(
1920 HasExactlyExclusionReasonsForTesting(
1921 std::vector<CookieInclusionStatus::ExclusionReason>{
1922 CookieInclusionStatus::EXCLUDE_USER_PREFERENCES}),
1923 _, _, _)),
1924 MatchesCookieWithAccessResult(
1925 MatchesCookieWithNameSourceType("three", CookieSourceType::kHTTP),
1926 MatchesCookieAccessResult(
1927 HasExactlyExclusionReasonsForTesting(
1928 std::vector<CookieInclusionStatus::ExclusionReason>{
1929 CookieInclusionStatus::EXCLUDE_USER_PREFERENCES}),
1930 _, _, _))));
1931
1932 EXPECT_EQ(0, network_delegate.annotate_cookies_called_count());
1933 }
1934
1935 // Test that cookies are allowed to be selectively blocked by the network
1936 // delegate.
TEST_F(URLRequestHttpJobTest,IndividuallyBlockedCookies)1937 TEST_F(URLRequestHttpJobTest, IndividuallyBlockedCookies) {
1938 HttpTestServer test_server;
1939 ASSERT_TRUE(test_server.Start());
1940
1941 auto network_delegate = std::make_unique<FilteringTestNetworkDelegate>();
1942 network_delegate->set_block_get_cookies_by_name(true);
1943 network_delegate->SetCookieFilter("blocked_");
1944 auto context_builder = CreateTestURLRequestContextBuilder();
1945 context_builder->SetCookieStore(std::make_unique<CookieMonster>(
1946 /*store=*/nullptr, /*net_log=*/nullptr));
1947 context_builder->set_network_delegate(std::move(network_delegate));
1948 auto context = context_builder->Build();
1949
1950 // Set cookies.
1951 {
1952 TestDelegate d;
1953 GURL test_url = test_server.GetURL(
1954 "/set-cookie?blocked_one=1;SameSite=Lax;Secure&"
1955 "blocked_two=1;SameSite=Lax;Secure&"
1956 "allowed=1;SameSite=Lax;Secure");
1957 std::unique_ptr<URLRequest> req =
1958 CreateFirstPartyRequest(*context, test_url, &d);
1959 req->Start();
1960 d.RunUntilComplete();
1961 }
1962
1963 // Get cookies.
1964 TestDelegate d;
1965 std::unique_ptr<URLRequest> req = CreateFirstPartyRequest(
1966 *context, test_server.GetURL("/echoheader?Cookie"), &d);
1967 req->Start();
1968 d.RunUntilComplete();
1969
1970 EXPECT_EQ("allowed=1", d.data_received());
1971 EXPECT_THAT(
1972 req->maybe_sent_cookies(),
1973 UnorderedElementsAre(
1974 MatchesCookieWithAccessResult(
1975 MatchesCookieWithNameSourceType("blocked_one",
1976 CookieSourceType::kHTTP),
1977 MatchesCookieAccessResult(
1978 HasExactlyExclusionReasonsForTesting(
1979 std::vector<CookieInclusionStatus::ExclusionReason>{
1980 CookieInclusionStatus::EXCLUDE_USER_PREFERENCES}),
1981 _, _, _)),
1982 MatchesCookieWithAccessResult(
1983 MatchesCookieWithNameSourceType("blocked_two",
1984 CookieSourceType::kHTTP),
1985 MatchesCookieAccessResult(
1986 HasExactlyExclusionReasonsForTesting(
1987 std::vector<CookieInclusionStatus::ExclusionReason>{
1988 CookieInclusionStatus::EXCLUDE_USER_PREFERENCES}),
1989 _, _, _)),
1990 MatchesCookieWithAccessResult(
1991 MatchesCookieWithNameSourceType("allowed",
1992 CookieSourceType::kHTTP),
1993 MatchesCookieAccessResult(IsInclude(), _, _, _))));
1994 }
1995
1996 namespace {
1997
1998 int content_count = 0;
IncreaseOnRequest(const test_server::HttpRequest & request)1999 std::unique_ptr<test_server::HttpResponse> IncreaseOnRequest(
2000 const test_server::HttpRequest& request) {
2001 auto http_response = std::make_unique<test_server::BasicHttpResponse>();
2002 http_response->set_content(base::NumberToString(content_count));
2003 content_count++;
2004 return std::move(http_response);
2005 }
2006
ResetContentCount()2007 void ResetContentCount() {
2008 content_count = 0;
2009 }
2010
2011 } // namespace
2012
TEST_F(URLRequestHttpJobTest,GetFirstPartySetsCacheFilterMatchInfo)2013 TEST_F(URLRequestHttpJobTest, GetFirstPartySetsCacheFilterMatchInfo) {
2014 EmbeddedTestServer https_test(EmbeddedTestServer::TYPE_HTTPS);
2015 https_test.AddDefaultHandlers(base::FilePath());
2016 https_test.RegisterRequestHandler(base::BindRepeating(&IncreaseOnRequest));
2017 ASSERT_TRUE(https_test.Start());
2018
2019 auto context_builder = CreateTestURLRequestContextBuilder();
2020 auto cookie_access_delegate = std::make_unique<TestCookieAccessDelegate>();
2021 TestCookieAccessDelegate* raw_cookie_access_delegate =
2022 cookie_access_delegate.get();
2023 auto cm = std::make_unique<CookieMonster>(nullptr, nullptr);
2024 cm->SetCookieAccessDelegate(std::move(cookie_access_delegate));
2025 context_builder->SetCookieStore(std::move(cm));
2026 auto context = context_builder->Build();
2027
2028 const GURL kTestUrl = https_test.GetURL("/");
2029 const IsolationInfo kTestIsolationInfo =
2030 IsolationInfo::CreateForInternalRequest(url::Origin::Create(kTestUrl));
2031 {
2032 TestDelegate delegate;
2033 std::unique_ptr<URLRequest> req(context->CreateRequest(
2034 kTestUrl, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
2035 req->set_isolation_info(kTestIsolationInfo);
2036 req->set_allow_credentials(false);
2037 req->Start();
2038 delegate.RunUntilComplete();
2039 EXPECT_EQ("0", delegate.data_received());
2040 }
2041 { // Test using the cached response.
2042 TestDelegate delegate;
2043 std::unique_ptr<URLRequest> req(context->CreateRequest(
2044 kTestUrl, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
2045 req->SetLoadFlags(LOAD_SKIP_CACHE_VALIDATION);
2046 req->set_allow_credentials(false);
2047 req->set_isolation_info(kTestIsolationInfo);
2048 req->Start();
2049 delegate.RunUntilComplete();
2050 EXPECT_EQ("0", delegate.data_received());
2051 }
2052
2053 // Set cache filter and test cache is bypassed because the request site has a
2054 // matched entry in the filter and its response cache was stored before being
2055 // marked to clear.
2056 const int64_t kClearAtRunId = 3;
2057 const int64_t kBrowserRunId = 3;
2058 FirstPartySetsCacheFilter cache_filter(
2059 {{SchemefulSite(kTestUrl), kClearAtRunId}}, kBrowserRunId);
2060 raw_cookie_access_delegate->set_first_party_sets_cache_filter(
2061 std::move(cache_filter));
2062 {
2063 TestDelegate delegate;
2064 std::unique_ptr<URLRequest> req(context->CreateRequest(
2065 kTestUrl, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
2066 req->SetLoadFlags(LOAD_SKIP_CACHE_VALIDATION);
2067 req->set_allow_credentials(false);
2068 req->set_isolation_info(kTestIsolationInfo);
2069 req->Start();
2070 delegate.RunUntilComplete();
2071 EXPECT_EQ("1", delegate.data_received());
2072 }
2073
2074 ResetContentCount();
2075 }
2076
TEST_F(URLRequestHttpJobTest,SetPartitionedCookie)2077 TEST_F(URLRequestHttpJobTest, SetPartitionedCookie) {
2078 EmbeddedTestServer https_test(EmbeddedTestServer::TYPE_HTTPS);
2079 https_test.AddDefaultHandlers(base::FilePath());
2080 ASSERT_TRUE(https_test.Start());
2081
2082 auto context_builder = CreateTestURLRequestContextBuilder();
2083 context_builder->SetCookieStore(std::make_unique<CookieMonster>(
2084 /*store=*/nullptr, /*net_log=*/nullptr));
2085 auto context = context_builder->Build();
2086
2087 const url::Origin kTopFrameOrigin =
2088 url::Origin::Create(GURL("https://www.toplevelsite.com"));
2089 const IsolationInfo kTestIsolationInfo =
2090 IsolationInfo::CreateForInternalRequest(kTopFrameOrigin);
2091
2092 {
2093 TestDelegate delegate;
2094 std::unique_ptr<URLRequest> req(context->CreateRequest(
2095 https_test.GetURL(
2096 "/set-cookie?__Host-foo=bar;SameSite=None;Secure;Path=/"
2097 ";Partitioned;"),
2098 DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
2099
2100 req->set_isolation_info(kTestIsolationInfo);
2101 req->Start();
2102 ASSERT_TRUE(req->is_pending());
2103 delegate.RunUntilComplete();
2104 }
2105
2106 { // Test request from the same top-level site.
2107 TestDelegate delegate;
2108 std::unique_ptr<URLRequest> req(context->CreateRequest(
2109 https_test.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &delegate,
2110 TRAFFIC_ANNOTATION_FOR_TESTS));
2111 req->set_isolation_info(kTestIsolationInfo);
2112 req->Start();
2113 delegate.RunUntilComplete();
2114 EXPECT_EQ("__Host-foo=bar", delegate.data_received());
2115 }
2116
2117 { // Test request from a different top-level site.
2118 const url::Origin kOtherTopFrameOrigin =
2119 url::Origin::Create(GURL("https://www.anothertoplevelsite.com"));
2120 const IsolationInfo kOtherTestIsolationInfo =
2121 IsolationInfo::CreateForInternalRequest(kOtherTopFrameOrigin);
2122
2123 TestDelegate delegate;
2124 std::unique_ptr<URLRequest> req(context->CreateRequest(
2125 https_test.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &delegate,
2126 TRAFFIC_ANNOTATION_FOR_TESTS));
2127 req->set_isolation_info(kOtherTestIsolationInfo);
2128 req->Start();
2129 delegate.RunUntilComplete();
2130 EXPECT_EQ("None", delegate.data_received());
2131 }
2132
2133 { // Test request from same top-level eTLD+1 but different scheme. Note that
2134 // although the top-level site is insecure, the endpoint setting/receiving
2135 // the cookie is always secure.
2136 const url::Origin kHttpTopFrameOrigin =
2137 url::Origin::Create(GURL("http://www.toplevelsite.com"));
2138 const IsolationInfo kHttpTestIsolationInfo =
2139 IsolationInfo::CreateForInternalRequest(kHttpTopFrameOrigin);
2140
2141 TestDelegate delegate;
2142 std::unique_ptr<URLRequest> req(context->CreateRequest(
2143 https_test.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &delegate,
2144 TRAFFIC_ANNOTATION_FOR_TESTS));
2145 req->set_isolation_info(kHttpTestIsolationInfo);
2146 req->Start();
2147 delegate.RunUntilComplete();
2148 EXPECT_EQ("None", delegate.data_received());
2149 }
2150 }
2151
TEST_F(URLRequestHttpJobTest,PartitionedCookiePrivacyMode)2152 TEST_F(URLRequestHttpJobTest, PartitionedCookiePrivacyMode) {
2153 EmbeddedTestServer https_test(EmbeddedTestServer::TYPE_HTTPS);
2154 https_test.AddDefaultHandlers(base::FilePath());
2155 ASSERT_TRUE(https_test.Start());
2156
2157 auto context_builder = CreateTestURLRequestContextBuilder();
2158 context_builder->SetCookieStore(
2159 std::make_unique<CookieMonster>(/*store=*/nullptr, /*net_log=*/nullptr));
2160 auto& network_delegate = *context_builder->set_network_delegate(
2161 std::make_unique<FilteringTestNetworkDelegate>());
2162 auto context = context_builder->Build();
2163
2164 const url::Origin kTopFrameOrigin =
2165 url::Origin::Create(GURL("https://www.toplevelsite.com"));
2166 const IsolationInfo kTestIsolationInfo =
2167 IsolationInfo::CreateForInternalRequest(kTopFrameOrigin);
2168
2169 {
2170 // Set an unpartitioned and partitioned cookie.
2171 TestDelegate delegate;
2172 std::unique_ptr<URLRequest> req(context->CreateRequest(
2173 https_test.GetURL(
2174 "/set-cookie?__Host-partitioned=0;SameSite=None;Secure;Path=/"
2175 ";Partitioned;&__Host-unpartitioned=1;SameSite=None;Secure;Path=/"),
2176 DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
2177 req->set_isolation_info(kTestIsolationInfo);
2178 req->Start();
2179 ASSERT_TRUE(req->is_pending());
2180 delegate.RunUntilComplete();
2181 }
2182
2183 { // Get both cookies when privacy mode is disabled.
2184 TestDelegate delegate;
2185 std::unique_ptr<URLRequest> req(context->CreateRequest(
2186 https_test.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &delegate,
2187 TRAFFIC_ANNOTATION_FOR_TESTS));
2188 req->set_isolation_info(kTestIsolationInfo);
2189 req->Start();
2190 delegate.RunUntilComplete();
2191 EXPECT_EQ("__Host-partitioned=0; __Host-unpartitioned=1",
2192 delegate.data_received());
2193 }
2194
2195 { // Get cookies with privacy mode enabled and partitioned state allowed.
2196 network_delegate.set_force_privacy_mode(true);
2197 network_delegate.set_partitioned_state_allowed(true);
2198 network_delegate.SetCookieFilter("unpartitioned");
2199 network_delegate.set_block_get_cookies_by_name(true);
2200 TestDelegate delegate;
2201 std::unique_ptr<URLRequest> req(context->CreateRequest(
2202 https_test.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &delegate,
2203 TRAFFIC_ANNOTATION_FOR_TESTS));
2204 req->set_isolation_info(kTestIsolationInfo);
2205 req->Start();
2206 delegate.RunUntilComplete();
2207 EXPECT_EQ("__Host-partitioned=0", delegate.data_received());
2208 auto want_exclusion_reasons =
2209 std::vector<CookieInclusionStatus::ExclusionReason>{};
2210
2211 EXPECT_THAT(
2212 req->maybe_sent_cookies(),
2213 UnorderedElementsAre(
2214 MatchesCookieWithAccessResult(
2215 MatchesCookieWithNameSourceType("__Host-partitioned",
2216 CookieSourceType::kHTTP),
2217 MatchesCookieAccessResult(HasExactlyExclusionReasonsForTesting(
2218 want_exclusion_reasons),
2219 _, _, _)),
2220 MatchesCookieWithAccessResult(
2221 MatchesCookieWithNameSourceType("__Host-unpartitioned",
2222 CookieSourceType::kHTTP),
2223 MatchesCookieAccessResult(
2224 HasExactlyExclusionReasonsForTesting(
2225 std::vector<CookieInclusionStatus::ExclusionReason>{
2226 CookieInclusionStatus::EXCLUDE_USER_PREFERENCES}),
2227 _, _, _))));
2228 }
2229
2230 { // Get cookies with privacy mode enabled and partitioned state is not
2231 // allowed.
2232 network_delegate.set_force_privacy_mode(true);
2233 network_delegate.set_partitioned_state_allowed(false);
2234 TestDelegate delegate;
2235 std::unique_ptr<URLRequest> req(context->CreateRequest(
2236 https_test.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &delegate,
2237 TRAFFIC_ANNOTATION_FOR_TESTS));
2238 req->set_isolation_info(kTestIsolationInfo);
2239 req->Start();
2240 delegate.RunUntilComplete();
2241 EXPECT_EQ("None", delegate.data_received());
2242 EXPECT_THAT(
2243 req->maybe_sent_cookies(),
2244 UnorderedElementsAre(
2245 MatchesCookieWithAccessResult(
2246 MatchesCookieWithNameSourceType("__Host-partitioned",
2247 CookieSourceType::kHTTP),
2248 MatchesCookieAccessResult(
2249 HasExactlyExclusionReasonsForTesting(
2250 std::vector<CookieInclusionStatus::ExclusionReason>{
2251 CookieInclusionStatus::EXCLUDE_USER_PREFERENCES}),
2252 _, _, _)),
2253 MatchesCookieWithAccessResult(
2254 MatchesCookieWithNameSourceType("__Host-unpartitioned",
2255 CookieSourceType::kHTTP),
2256 MatchesCookieAccessResult(
2257 HasExactlyExclusionReasonsForTesting(
2258 std::vector<CookieInclusionStatus::ExclusionReason>{
2259 CookieInclusionStatus::EXCLUDE_USER_PREFERENCES}),
2260 _, _, _))));
2261 }
2262 }
2263
2264 } // namespace net
2265