1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
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/macros.h"
16 #include "base/memory/ptr_util.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/run_loop.h"
19 #include "base/strings/string_number_conversions.h"
20 #include "base/strings/string_split.h"
21 #include "base/test/bind_test_util.h"
22 #include "base/test/metrics/histogram_tester.h"
23 #include "net/base/auth.h"
24 #include "net/base/isolation_info.h"
25 #include "net/base/request_priority.h"
26 #include "net/cert/ct_policy_status.h"
27 #include "net/cookies/cookie_monster.h"
28 #include "net/cookies/cookie_store_test_callbacks.h"
29 #include "net/cookies/cookie_store_test_helpers.h"
30 #include "net/http/http_transaction_factory.h"
31 #include "net/http/http_transaction_test_util.h"
32 #include "net/log/net_log_event_type.h"
33 #include "net/log/test_net_log.h"
34 #include "net/log/test_net_log_util.h"
35 #include "net/net_buildflags.h"
36 #include "net/proxy_resolution/configured_proxy_resolution_service.h"
37 #include "net/socket/next_proto.h"
38 #include "net/socket/socket_test_util.h"
39 #include "net/test/cert_test_util.h"
40 #include "net/test/embedded_test_server/default_handlers.h"
41 #include "net/test/gtest_util.h"
42 #include "net/test/test_data_directory.h"
43 #include "net/test/test_with_task_environment.h"
44 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
45 #include "net/url_request/url_request.h"
46 #include "net/url_request/url_request_job_factory_impl.h"
47 #include "net/url_request/url_request_status.h"
48 #include "net/url_request/url_request_test_util.h"
49 #include "net/url_request/websocket_handshake_userdata_key.h"
50 #include "net/websockets/websocket_test_util.h"
51 #include "testing/gmock/include/gmock/gmock.h"
52 #include "testing/gtest/include/gtest/gtest.h"
53 #include "url/gurl.h"
54 #include "url/url_constants.h"
55
56 #if defined(OS_ANDROID)
57 #include "base/android/jni_android.h"
58 #include "net/net_test_jni_headers/AndroidNetworkLibraryTestUtil_jni.h"
59 #endif
60
61 using net::test::IsError;
62 using net::test::IsOk;
63
64 namespace net {
65
66 namespace {
67
68 using ::testing::Return;
69
70 const char kSimpleGetMockWrite[] =
71 "GET / HTTP/1.1\r\n"
72 "Host: www.example.com\r\n"
73 "Connection: keep-alive\r\n"
74 "User-Agent: \r\n"
75 "Accept-Encoding: gzip, deflate\r\n"
76 "Accept-Language: en-us,fr\r\n\r\n";
77
78 const char kSimpleHeadMockWrite[] =
79 "HEAD / HTTP/1.1\r\n"
80 "Host: www.example.com\r\n"
81 "Connection: keep-alive\r\n"
82 "User-Agent: \r\n"
83 "Accept-Encoding: gzip, deflate\r\n"
84 "Accept-Language: en-us,fr\r\n\r\n";
85
86 const char kTrustAnchorRequestHistogram[] =
87 "Net.Certificate.TrustAnchor.Request";
88
89 const char kCTComplianceHistogramName[] =
90 "Net.CertificateTransparency.RequestComplianceStatus";
91 const char kCTRequiredHistogramName[] =
92 "Net.CertificateTransparency.CTRequiredRequestComplianceStatus";
93
94 // Inherit from URLRequestHttpJob to expose the priority and some
95 // other hidden functions.
96 class TestURLRequestHttpJob : public URLRequestHttpJob {
97 public:
TestURLRequestHttpJob(URLRequest * request)98 explicit TestURLRequestHttpJob(URLRequest* request)
99 : URLRequestHttpJob(request,
100 request->context()->network_delegate(),
101 request->context()->http_user_agent_settings()),
102 use_null_source_stream_(false) {}
103
104 ~TestURLRequestHttpJob() override = default;
105
106 // URLRequestJob implementation:
SetUpSourceStream()107 std::unique_ptr<SourceStream> SetUpSourceStream() override {
108 if (use_null_source_stream_)
109 return nullptr;
110 return URLRequestHttpJob::SetUpSourceStream();
111 }
112
set_use_null_source_stream(bool use_null_source_stream)113 void set_use_null_source_stream(bool use_null_source_stream) {
114 use_null_source_stream_ = use_null_source_stream;
115 }
116
117 using URLRequestHttpJob::SetPriority;
118 using URLRequestHttpJob::Start;
119 using URLRequestHttpJob::Kill;
120 using URLRequestHttpJob::priority;
121
122 private:
123 bool use_null_source_stream_;
124
125 DISALLOW_COPY_AND_ASSIGN(TestURLRequestHttpJob);
126 };
127
128 class URLRequestHttpJobSetUpSourceTest : public TestWithTaskEnvironment {
129 public:
URLRequestHttpJobSetUpSourceTest()130 URLRequestHttpJobSetUpSourceTest() : context_(true) {
131 test_job_interceptor_ = new TestJobInterceptor();
132 EXPECT_TRUE(test_job_factory_.SetProtocolHandler(
133 url::kHttpScheme, base::WrapUnique(test_job_interceptor_)));
134 context_.set_job_factory(&test_job_factory_);
135 context_.set_client_socket_factory(&socket_factory_);
136 context_.Init();
137 }
138
139 protected:
140 MockClientSocketFactory socket_factory_;
141 // |test_job_interceptor_| is owned by |test_job_factory_|.
142 TestJobInterceptor* test_job_interceptor_;
143 URLRequestJobFactoryImpl test_job_factory_;
144
145 TestURLRequestContext context_;
146 TestDelegate delegate_;
147 };
148
149 // Tests that if SetUpSourceStream() returns nullptr, the request fails.
TEST_F(URLRequestHttpJobSetUpSourceTest,SetUpSourceFails)150 TEST_F(URLRequestHttpJobSetUpSourceTest, SetUpSourceFails) {
151 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
152 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
153 "Content-Length: 12\r\n\r\n"),
154 MockRead("Test Content")};
155
156 StaticSocketDataProvider socket_data(reads, writes);
157 socket_factory_.AddSocketDataProvider(&socket_data);
158
159 std::unique_ptr<URLRequest> request =
160 context_.CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
161 &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS);
162 auto job = std::make_unique<TestURLRequestHttpJob>(request.get());
163 job->set_use_null_source_stream(true);
164 test_job_interceptor_->set_main_intercept_job(std::move(job));
165 request->Start();
166
167 delegate_.RunUntilComplete();
168 EXPECT_EQ(ERR_CONTENT_DECODING_INIT_FAILED, delegate_.request_status());
169 }
170
171 // Tests that if there is an unknown content-encoding type, the raw response
172 // body is passed through.
TEST_F(URLRequestHttpJobSetUpSourceTest,UnknownEncoding)173 TEST_F(URLRequestHttpJobSetUpSourceTest, UnknownEncoding) {
174 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
175 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
176 "Content-Encoding: foo, gzip\r\n"
177 "Content-Length: 12\r\n\r\n"),
178 MockRead("Test Content")};
179
180 StaticSocketDataProvider socket_data(reads, writes);
181 socket_factory_.AddSocketDataProvider(&socket_data);
182
183 std::unique_ptr<URLRequest> request =
184 context_.CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
185 &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS);
186 auto job = std::make_unique<TestURLRequestHttpJob>(request.get());
187 test_job_interceptor_->set_main_intercept_job(std::move(job));
188 request->Start();
189
190 delegate_.RunUntilComplete();
191 EXPECT_EQ(OK, delegate_.request_status());
192 EXPECT_EQ("Test Content", delegate_.data_received());
193 }
194
195 class URLRequestHttpJobWithProxy : public WithTaskEnvironment {
196 public:
URLRequestHttpJobWithProxy(std::unique_ptr<ProxyResolutionService> proxy_resolution_service)197 explicit URLRequestHttpJobWithProxy(
198 std::unique_ptr<ProxyResolutionService> proxy_resolution_service)
199 : proxy_resolution_service_(std::move(proxy_resolution_service)),
200 context_(new TestURLRequestContext(true)) {
201 context_->set_client_socket_factory(&socket_factory_);
202 context_->set_network_delegate(&network_delegate_);
203 context_->set_proxy_resolution_service(proxy_resolution_service_.get());
204 context_->Init();
205 }
206
207 MockClientSocketFactory socket_factory_;
208 TestNetworkDelegate network_delegate_;
209 std::unique_ptr<ProxyResolutionService> proxy_resolution_service_;
210 std::unique_ptr<TestURLRequestContext> context_;
211
212 private:
213 DISALLOW_COPY_AND_ASSIGN(URLRequestHttpJobWithProxy);
214 };
215
216 // Tests that when proxy is not used, the proxy server is set correctly on the
217 // URLRequest.
TEST(URLRequestHttpJobWithProxy,TestFailureWithoutProxy)218 TEST(URLRequestHttpJobWithProxy, TestFailureWithoutProxy) {
219 URLRequestHttpJobWithProxy http_job_with_proxy(nullptr);
220
221 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
222 MockRead reads[] = {MockRead(SYNCHRONOUS, ERR_CONNECTION_RESET)};
223
224 StaticSocketDataProvider socket_data(reads, writes);
225 http_job_with_proxy.socket_factory_.AddSocketDataProvider(&socket_data);
226
227 TestDelegate delegate;
228 std::unique_ptr<URLRequest> request =
229 http_job_with_proxy.context_->CreateRequest(
230 GURL("http://www.example.com"), DEFAULT_PRIORITY, &delegate,
231 TRAFFIC_ANNOTATION_FOR_TESTS);
232
233 request->Start();
234 ASSERT_TRUE(request->is_pending());
235 delegate.RunUntilComplete();
236
237 EXPECT_THAT(delegate.request_status(), IsError(ERR_CONNECTION_RESET));
238 EXPECT_EQ(ProxyServer::Direct(), request->proxy_server());
239 EXPECT_EQ(0, request->received_response_content_length());
240 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
241 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
242 }
243
244 // Tests that when one proxy is in use and the connection to the proxy server
245 // fails, the proxy server is still set correctly on the URLRequest.
TEST(URLRequestHttpJobWithProxy,TestSuccessfulWithOneProxy)246 TEST(URLRequestHttpJobWithProxy, TestSuccessfulWithOneProxy) {
247 const char kSimpleProxyGetMockWrite[] =
248 "GET http://www.example.com/ HTTP/1.1\r\n"
249 "Host: www.example.com\r\n"
250 "Proxy-Connection: keep-alive\r\n"
251 "User-Agent: \r\n"
252 "Accept-Encoding: gzip, deflate\r\n"
253 "Accept-Language: en-us,fr\r\n\r\n";
254
255 const ProxyServer proxy_server =
256 ProxyServer::FromURI("http://origin.net:80", ProxyServer::SCHEME_HTTP);
257
258 std::unique_ptr<ProxyResolutionService> proxy_resolution_service =
259 ConfiguredProxyResolutionService::CreateFixedFromPacResult(
260 proxy_server.ToPacString(), TRAFFIC_ANNOTATION_FOR_TESTS);
261
262 MockWrite writes[] = {MockWrite(kSimpleProxyGetMockWrite)};
263 MockRead reads[] = {MockRead(SYNCHRONOUS, ERR_CONNECTION_RESET)};
264
265 StaticSocketDataProvider socket_data(reads, writes);
266
267 URLRequestHttpJobWithProxy http_job_with_proxy(
268 std::move(proxy_resolution_service));
269 http_job_with_proxy.socket_factory_.AddSocketDataProvider(&socket_data);
270
271 TestDelegate delegate;
272 std::unique_ptr<URLRequest> request =
273 http_job_with_proxy.context_->CreateRequest(
274 GURL("http://www.example.com"), DEFAULT_PRIORITY, &delegate,
275 TRAFFIC_ANNOTATION_FOR_TESTS);
276
277 request->Start();
278 ASSERT_TRUE(request->is_pending());
279 delegate.RunUntilComplete();
280
281 EXPECT_THAT(delegate.request_status(), IsError(ERR_CONNECTION_RESET));
282 // When request fails due to proxy connection errors, the proxy server should
283 // still be set on the |request|.
284 EXPECT_EQ(proxy_server, request->proxy_server());
285 EXPECT_EQ(0, request->received_response_content_length());
286 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
287 EXPECT_EQ(0, request->GetTotalReceivedBytes());
288 }
289
290 // Tests that when two proxies are in use and the connection to the first proxy
291 // server fails, the proxy server is set correctly on the URLRequest.
TEST(URLRequestHttpJobWithProxy,TestContentLengthSuccessfulRequestWithTwoProxies)292 TEST(URLRequestHttpJobWithProxy,
293 TestContentLengthSuccessfulRequestWithTwoProxies) {
294 const ProxyServer proxy_server =
295 ProxyServer::FromURI("http://origin.net:80", ProxyServer::SCHEME_HTTP);
296
297 // Connection to |proxy_server| would fail. Request should be fetched over
298 // DIRECT.
299 std::unique_ptr<ProxyResolutionService> proxy_resolution_service =
300 ConfiguredProxyResolutionService::CreateFixedFromPacResult(
301 proxy_server.ToPacString() + "; " +
302 ProxyServer::Direct().ToPacString(),
303 TRAFFIC_ANNOTATION_FOR_TESTS);
304
305 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
306 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
307 "Content-Length: 12\r\n\r\n"),
308 MockRead("Test Content"), MockRead(ASYNC, OK)};
309
310 MockConnect mock_connect_1(SYNCHRONOUS, ERR_CONNECTION_RESET);
311 StaticSocketDataProvider connect_data_1;
312 connect_data_1.set_connect_data(mock_connect_1);
313
314 StaticSocketDataProvider socket_data(reads, writes);
315
316 URLRequestHttpJobWithProxy http_job_with_proxy(
317 std::move(proxy_resolution_service));
318 http_job_with_proxy.socket_factory_.AddSocketDataProvider(&connect_data_1);
319 http_job_with_proxy.socket_factory_.AddSocketDataProvider(&socket_data);
320
321 TestDelegate delegate;
322 std::unique_ptr<URLRequest> request =
323 http_job_with_proxy.context_->CreateRequest(
324 GURL("http://www.example.com"), DEFAULT_PRIORITY, &delegate,
325 TRAFFIC_ANNOTATION_FOR_TESTS);
326
327 request->Start();
328 ASSERT_TRUE(request->is_pending());
329 base::RunLoop().RunUntilIdle();
330
331 EXPECT_THAT(delegate.request_status(), IsOk());
332 EXPECT_EQ(ProxyServer::Direct(), request->proxy_server());
333 EXPECT_EQ(12, request->received_response_content_length());
334 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
335 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
336 }
337
338 class URLRequestHttpJobTest : public TestWithTaskEnvironment {
339 protected:
URLRequestHttpJobTest()340 URLRequestHttpJobTest() : context_(true) {
341 context_.set_http_transaction_factory(&network_layer_);
342
343 // The |test_job_factory_| takes ownership of the interceptor.
344 test_job_interceptor_ = new TestJobInterceptor();
345 EXPECT_TRUE(test_job_factory_.SetProtocolHandler(
346 url::kHttpScheme, base::WrapUnique(test_job_interceptor_)));
347 context_.set_job_factory(&test_job_factory_);
348 context_.set_net_log(&net_log_);
349 context_.Init();
350
351 req_ =
352 context_.CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
353 &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS);
354 }
355
356 MockNetworkLayer network_layer_;
357
358 // |test_job_interceptor_| is owned by |test_job_factory_|.
359 TestJobInterceptor* test_job_interceptor_;
360 URLRequestJobFactoryImpl test_job_factory_;
361
362 TestURLRequestContext context_;
363 TestDelegate delegate_;
364 RecordingTestNetLog net_log_;
365 std::unique_ptr<URLRequest> req_;
366 };
367
368 class URLRequestHttpJobWithMockSocketsTest : public TestWithTaskEnvironment {
369 protected:
URLRequestHttpJobWithMockSocketsTest()370 URLRequestHttpJobWithMockSocketsTest()
371 : context_(new TestURLRequestContext(true)) {
372 context_->set_client_socket_factory(&socket_factory_);
373 context_->set_network_delegate(&network_delegate_);
374 context_->Init();
375 }
376
377 MockClientSocketFactory socket_factory_;
378 TestNetworkDelegate network_delegate_;
379 std::unique_ptr<TestURLRequestContext> context_;
380 };
381
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestContentLengthSuccessfulRequest)382 TEST_F(URLRequestHttpJobWithMockSocketsTest,
383 TestContentLengthSuccessfulRequest) {
384 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
385 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
386 "Content-Length: 12\r\n\r\n"),
387 MockRead("Test Content")};
388
389 StaticSocketDataProvider socket_data(reads, writes);
390 socket_factory_.AddSocketDataProvider(&socket_data);
391
392 TestDelegate delegate;
393 std::unique_ptr<URLRequest> request =
394 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
395 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
396
397 request->Start();
398 ASSERT_TRUE(request->is_pending());
399 delegate.RunUntilComplete();
400
401 EXPECT_THAT(delegate.request_status(), IsOk());
402 EXPECT_EQ(12, request->received_response_content_length());
403 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
404 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
405 }
406
407 // Tests a successful HEAD request.
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestSuccessfulHead)408 TEST_F(URLRequestHttpJobWithMockSocketsTest, TestSuccessfulHead) {
409 MockWrite writes[] = {MockWrite(kSimpleHeadMockWrite)};
410 MockRead reads[] = {
411 MockRead("HTTP/1.1 200 OK\r\n"
412 "Content-Length: 0\r\n\r\n")};
413
414 StaticSocketDataProvider socket_data(reads, writes);
415 socket_factory_.AddSocketDataProvider(&socket_data);
416
417 TestDelegate delegate;
418 std::unique_ptr<URLRequest> request =
419 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
420 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
421
422 request->set_method("HEAD");
423 request->Start();
424 ASSERT_TRUE(request->is_pending());
425 delegate.RunUntilComplete();
426
427 EXPECT_THAT(delegate.request_status(), IsOk());
428 EXPECT_EQ(0, request->received_response_content_length());
429 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
430 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
431 }
432
433 // Similar to above test but tests that even if response body is there in the
434 // HEAD response stream, it should not be read due to HttpStreamParser's logic.
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestSuccessfulHeadWithContent)435 TEST_F(URLRequestHttpJobWithMockSocketsTest, TestSuccessfulHeadWithContent) {
436 MockWrite writes[] = {MockWrite(kSimpleHeadMockWrite)};
437 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
438 "Content-Length: 12\r\n\r\n"),
439 MockRead("Test Content")};
440
441 StaticSocketDataProvider socket_data(reads, writes);
442 socket_factory_.AddSocketDataProvider(&socket_data);
443
444 TestDelegate delegate;
445 std::unique_ptr<URLRequest> request =
446 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
447 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
448
449 request->set_method("HEAD");
450 request->Start();
451 ASSERT_TRUE(request->is_pending());
452 delegate.RunUntilComplete();
453
454 EXPECT_THAT(delegate.request_status(), IsOk());
455 EXPECT_EQ(0, request->received_response_content_length());
456 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
457 EXPECT_EQ(CountReadBytes(reads) - 12, request->GetTotalReceivedBytes());
458 }
459
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestSuccessfulCachedHeadRequest)460 TEST_F(URLRequestHttpJobWithMockSocketsTest, TestSuccessfulCachedHeadRequest) {
461 const url::Origin kOrigin1 =
462 url::Origin::Create(GURL("http://www.example.com"));
463 const IsolationInfo kTestIsolationInfo =
464 IsolationInfo::CreateForInternalRequest(kOrigin1);
465
466 // Cache the response.
467 {
468 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
469 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
470 "Content-Length: 12\r\n\r\n"),
471 MockRead("Test Content")};
472
473 StaticSocketDataProvider socket_data(reads, writes);
474 socket_factory_.AddSocketDataProvider(&socket_data);
475
476 TestDelegate delegate;
477 std::unique_ptr<URLRequest> request = context_->CreateRequest(
478 GURL("http://www.example.com"), DEFAULT_PRIORITY, &delegate,
479 TRAFFIC_ANNOTATION_FOR_TESTS);
480
481 request->set_isolation_info(kTestIsolationInfo);
482 request->Start();
483 ASSERT_TRUE(request->is_pending());
484 delegate.RunUntilComplete();
485
486 EXPECT_THAT(delegate.request_status(), IsOk());
487 EXPECT_EQ(12, request->received_response_content_length());
488 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
489 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
490 }
491
492 // Send a HEAD request for the cached response.
493 {
494 MockWrite writes[] = {MockWrite(kSimpleHeadMockWrite)};
495 MockRead reads[] = {
496 MockRead("HTTP/1.1 200 OK\r\n"
497 "Content-Length: 0\r\n\r\n")};
498
499 StaticSocketDataProvider socket_data(reads, writes);
500 socket_factory_.AddSocketDataProvider(&socket_data);
501
502 TestDelegate delegate;
503 std::unique_ptr<URLRequest> request = context_->CreateRequest(
504 GURL("http://www.example.com"), DEFAULT_PRIORITY, &delegate,
505 TRAFFIC_ANNOTATION_FOR_TESTS);
506
507 // Use the cached version.
508 request->SetLoadFlags(LOAD_SKIP_CACHE_VALIDATION);
509 request->set_method("HEAD");
510 request->set_isolation_info(kTestIsolationInfo);
511 request->Start();
512 ASSERT_TRUE(request->is_pending());
513 delegate.RunUntilComplete();
514
515 EXPECT_THAT(delegate.request_status(), IsOk());
516 EXPECT_EQ(0, request->received_response_content_length());
517 EXPECT_EQ(0, request->GetTotalSentBytes());
518 EXPECT_EQ(0, request->GetTotalReceivedBytes());
519 }
520 }
521
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestContentLengthSuccessfulHttp09Request)522 TEST_F(URLRequestHttpJobWithMockSocketsTest,
523 TestContentLengthSuccessfulHttp09Request) {
524 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
525 MockRead reads[] = {MockRead("Test Content"),
526 MockRead(net::SYNCHRONOUS, net::OK)};
527
528 StaticSocketDataProvider socket_data(reads, base::span<MockWrite>());
529 socket_factory_.AddSocketDataProvider(&socket_data);
530
531 TestDelegate delegate;
532 std::unique_ptr<URLRequest> request =
533 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
534 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
535
536 request->Start();
537 ASSERT_TRUE(request->is_pending());
538 delegate.RunUntilComplete();
539
540 EXPECT_THAT(delegate.request_status(), IsOk());
541 EXPECT_EQ(12, request->received_response_content_length());
542 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
543 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
544 }
545
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestContentLengthFailedRequest)546 TEST_F(URLRequestHttpJobWithMockSocketsTest, TestContentLengthFailedRequest) {
547 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
548 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
549 "Content-Length: 20\r\n\r\n"),
550 MockRead("Test Content"),
551 MockRead(net::SYNCHRONOUS, net::ERR_FAILED)};
552
553 StaticSocketDataProvider socket_data(reads, writes);
554 socket_factory_.AddSocketDataProvider(&socket_data);
555
556 TestDelegate delegate;
557 std::unique_ptr<URLRequest> request =
558 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
559 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
560
561 request->Start();
562 ASSERT_TRUE(request->is_pending());
563 delegate.RunUntilComplete();
564
565 EXPECT_THAT(delegate.request_status(), IsError(ERR_FAILED));
566 EXPECT_EQ(12, request->received_response_content_length());
567 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
568 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
569 }
570
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestContentLengthCancelledRequest)571 TEST_F(URLRequestHttpJobWithMockSocketsTest,
572 TestContentLengthCancelledRequest) {
573 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
574 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
575 "Content-Length: 20\r\n\r\n"),
576 MockRead("Test Content"),
577 MockRead(net::SYNCHRONOUS, net::ERR_IO_PENDING)};
578
579 StaticSocketDataProvider socket_data(reads, writes);
580 socket_factory_.AddSocketDataProvider(&socket_data);
581
582 TestDelegate delegate;
583 std::unique_ptr<URLRequest> request =
584 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
585 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
586
587 delegate.set_cancel_in_received_data(true);
588 request->Start();
589 base::RunLoop().RunUntilIdle();
590
591 EXPECT_THAT(delegate.request_status(), IsError(ERR_ABORTED));
592 EXPECT_EQ(12, request->received_response_content_length());
593 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
594 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
595 }
596
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestRawHeaderSizeSuccessfullRequest)597 TEST_F(URLRequestHttpJobWithMockSocketsTest,
598 TestRawHeaderSizeSuccessfullRequest) {
599 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
600
601 const std::string& response_header =
602 "HTTP/1.1 200 OK\r\n"
603 "Content-Length: 12\r\n\r\n";
604 const std::string& content_data = "Test Content";
605
606 MockRead reads[] = {MockRead(response_header.c_str()),
607 MockRead(content_data.c_str()),
608 MockRead(net::SYNCHRONOUS, net::OK)};
609
610 StaticSocketDataProvider socket_data(reads, writes);
611 socket_factory_.AddSocketDataProvider(&socket_data);
612
613 TestDelegate delegate;
614 std::unique_ptr<URLRequest> request =
615 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
616 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
617
618 request->Start();
619 ASSERT_TRUE(request->is_pending());
620 delegate.RunUntilComplete();
621
622 EXPECT_EQ(OK, delegate.request_status());
623 EXPECT_EQ(static_cast<int>(content_data.size()),
624 request->received_response_content_length());
625 EXPECT_EQ(static_cast<int>(response_header.size()),
626 request->raw_header_size());
627 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
628 }
629
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestRawHeaderSizeSuccessfull100ContinueRequest)630 TEST_F(URLRequestHttpJobWithMockSocketsTest,
631 TestRawHeaderSizeSuccessfull100ContinueRequest) {
632 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
633
634 const std::string& continue_header = "HTTP/1.1 100 Continue\r\n\r\n";
635 const std::string& response_header =
636 "HTTP/1.1 200 OK\r\n"
637 "Content-Length: 12\r\n\r\n";
638 const std::string& content_data = "Test Content";
639
640 MockRead reads[] = {
641 MockRead(continue_header.c_str()), MockRead(response_header.c_str()),
642 MockRead(content_data.c_str()), MockRead(net::SYNCHRONOUS, net::OK)};
643
644 StaticSocketDataProvider socket_data(reads, writes);
645 socket_factory_.AddSocketDataProvider(&socket_data);
646
647 TestDelegate delegate;
648 std::unique_ptr<URLRequest> request =
649 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
650 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
651
652 request->Start();
653 ASSERT_TRUE(request->is_pending());
654 delegate.RunUntilComplete();
655
656 EXPECT_EQ(OK, delegate.request_status());
657 EXPECT_EQ(static_cast<int>(content_data.size()),
658 request->received_response_content_length());
659 EXPECT_EQ(static_cast<int>(continue_header.size() + response_header.size()),
660 request->raw_header_size());
661 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
662 }
663
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestRawHeaderSizeFailureTruncatedHeaders)664 TEST_F(URLRequestHttpJobWithMockSocketsTest,
665 TestRawHeaderSizeFailureTruncatedHeaders) {
666 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
667 MockRead reads[] = {MockRead("HTTP/1.0 200 OK\r\n"
668 "Content-Len"),
669 MockRead(net::SYNCHRONOUS, net::OK)};
670
671 StaticSocketDataProvider socket_data(reads, writes);
672 socket_factory_.AddSocketDataProvider(&socket_data);
673
674 TestDelegate delegate;
675 std::unique_ptr<URLRequest> request =
676 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
677 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
678
679 delegate.set_cancel_in_response_started(true);
680 request->Start();
681 base::RunLoop().RunUntilIdle();
682
683 EXPECT_EQ(ERR_ABORTED, delegate.request_status());
684 EXPECT_EQ(0, request->received_response_content_length());
685 EXPECT_EQ(28, request->raw_header_size());
686 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
687 }
688
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestRawHeaderSizeSuccessfullContinuiousRead)689 TEST_F(URLRequestHttpJobWithMockSocketsTest,
690 TestRawHeaderSizeSuccessfullContinuiousRead) {
691 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
692 const std::string& header_data =
693 "HTTP/1.1 200 OK\r\n"
694 "Content-Length: 12\r\n\r\n";
695 const std::string& content_data = "Test Content";
696 std::string single_read_content = header_data;
697 single_read_content.append(content_data);
698 MockRead reads[] = {MockRead(single_read_content.c_str())};
699
700 StaticSocketDataProvider socket_data(reads, writes);
701 socket_factory_.AddSocketDataProvider(&socket_data);
702
703 TestDelegate delegate;
704 std::unique_ptr<URLRequest> request =
705 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
706 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
707
708 request->Start();
709 delegate.RunUntilComplete();
710
711 EXPECT_EQ(OK, delegate.request_status());
712 EXPECT_EQ(static_cast<int>(content_data.size()),
713 request->received_response_content_length());
714 EXPECT_EQ(static_cast<int>(header_data.size()), request->raw_header_size());
715 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
716 }
717
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestNetworkBytesRedirectedRequest)718 TEST_F(URLRequestHttpJobWithMockSocketsTest,
719 TestNetworkBytesRedirectedRequest) {
720 MockWrite redirect_writes[] = {
721 MockWrite("GET / HTTP/1.1\r\n"
722 "Host: www.redirect.com\r\n"
723 "Connection: keep-alive\r\n"
724 "User-Agent: \r\n"
725 "Accept-Encoding: gzip, deflate\r\n"
726 "Accept-Language: en-us,fr\r\n\r\n")};
727
728 MockRead redirect_reads[] = {
729 MockRead("HTTP/1.1 302 Found\r\n"
730 "Location: http://www.example.com\r\n\r\n"),
731 };
732 StaticSocketDataProvider redirect_socket_data(redirect_reads,
733 redirect_writes);
734 socket_factory_.AddSocketDataProvider(&redirect_socket_data);
735
736 MockWrite final_writes[] = {MockWrite(kSimpleGetMockWrite)};
737 MockRead final_reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
738 "Content-Length: 12\r\n\r\n"),
739 MockRead("Test Content")};
740 StaticSocketDataProvider final_socket_data(final_reads, final_writes);
741 socket_factory_.AddSocketDataProvider(&final_socket_data);
742
743 TestDelegate delegate;
744 std::unique_ptr<URLRequest> request =
745 context_->CreateRequest(GURL("http://www.redirect.com"), DEFAULT_PRIORITY,
746 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
747
748 request->Start();
749 ASSERT_TRUE(request->is_pending());
750 base::RunLoop().RunUntilIdle();
751
752 EXPECT_THAT(delegate.request_status(), IsOk());
753 EXPECT_EQ(12, request->received_response_content_length());
754 // Should not include the redirect.
755 EXPECT_EQ(CountWriteBytes(final_writes), request->GetTotalSentBytes());
756 EXPECT_EQ(CountReadBytes(final_reads), request->GetTotalReceivedBytes());
757 }
758
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestNetworkBytesCancelledAfterHeaders)759 TEST_F(URLRequestHttpJobWithMockSocketsTest,
760 TestNetworkBytesCancelledAfterHeaders) {
761 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
762 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n\r\n")};
763 StaticSocketDataProvider socket_data(reads, writes);
764 socket_factory_.AddSocketDataProvider(&socket_data);
765
766 TestDelegate delegate;
767 std::unique_ptr<URLRequest> request =
768 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
769 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
770
771 delegate.set_cancel_in_response_started(true);
772 request->Start();
773 base::RunLoop().RunUntilIdle();
774
775 EXPECT_THAT(delegate.request_status(), IsError(ERR_ABORTED));
776 EXPECT_EQ(0, request->received_response_content_length());
777 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
778 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
779 }
780
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestNetworkBytesCancelledImmediately)781 TEST_F(URLRequestHttpJobWithMockSocketsTest,
782 TestNetworkBytesCancelledImmediately) {
783 StaticSocketDataProvider socket_data;
784 socket_factory_.AddSocketDataProvider(&socket_data);
785
786 TestDelegate delegate;
787 std::unique_ptr<URLRequest> request =
788 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
789 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
790
791 request->Start();
792 request->Cancel();
793 base::RunLoop().RunUntilIdle();
794
795 EXPECT_THAT(delegate.request_status(), IsError(ERR_ABORTED));
796 EXPECT_EQ(0, request->received_response_content_length());
797 EXPECT_EQ(0, request->GetTotalSentBytes());
798 EXPECT_EQ(0, request->GetTotalReceivedBytes());
799 }
800
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestHttpTimeToFirstByte)801 TEST_F(URLRequestHttpJobWithMockSocketsTest, TestHttpTimeToFirstByte) {
802 base::HistogramTester histograms;
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
808 StaticSocketDataProvider socket_data(reads, writes);
809 socket_factory_.AddSocketDataProvider(&socket_data);
810
811 TestDelegate delegate;
812 std::unique_ptr<URLRequest> request =
813 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
814 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
815 histograms.ExpectTotalCount("Net.HttpTimeToFirstByte", 0);
816
817 request->Start();
818 delegate.RunUntilComplete();
819
820 EXPECT_THAT(delegate.request_status(), IsOk());
821 histograms.ExpectTotalCount("Net.HttpTimeToFirstByte", 1);
822 }
823
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestHttpTimeToFirstByteForCancelledTask)824 TEST_F(URLRequestHttpJobWithMockSocketsTest,
825 TestHttpTimeToFirstByteForCancelledTask) {
826 base::HistogramTester histograms;
827 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
828 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
829 "Content-Length: 12\r\n\r\n"),
830 MockRead("Test Content")};
831
832 StaticSocketDataProvider socket_data(reads, writes);
833 socket_factory_.AddSocketDataProvider(&socket_data);
834
835 TestDelegate delegate;
836 std::unique_ptr<URLRequest> request =
837 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
838 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
839
840 request->Start();
841 request->Cancel();
842 delegate.RunUntilComplete();
843
844 EXPECT_THAT(delegate.request_status(), IsError(ERR_ABORTED));
845 histograms.ExpectTotalCount("Net.HttpTimeToFirstByte", 0);
846 }
847
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestHttpJobSuccessPriorityKeyedTotalTime)848 TEST_F(URLRequestHttpJobWithMockSocketsTest,
849 TestHttpJobSuccessPriorityKeyedTotalTime) {
850 base::HistogramTester histograms;
851
852 for (int priority = 0; priority < net::NUM_PRIORITIES; ++priority) {
853 for (int request_index = 0; request_index <= priority; ++request_index) {
854 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
855 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
856 "Content-Length: 12\r\n\r\n"),
857 MockRead("Test Content")};
858
859 StaticSocketDataProvider socket_data(reads, writes);
860 socket_factory_.AddSocketDataProvider(&socket_data);
861
862 TestDelegate delegate;
863 std::unique_ptr<URLRequest> request =
864 context_->CreateRequest(GURL("http://www.example.com/"),
865 static_cast<net::RequestPriority>(priority),
866 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
867
868 request->Start();
869 delegate.RunUntilComplete();
870 EXPECT_THAT(delegate.request_status(), IsOk());
871 }
872 }
873
874 for (int priority = 0; priority < net::NUM_PRIORITIES; ++priority) {
875 histograms.ExpectTotalCount("Net.HttpJob.TotalTimeSuccess.Priority" +
876 base::NumberToString(priority),
877 priority + 1);
878 }
879 }
880
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestHttpJobRecordsTrustAnchorHistograms)881 TEST_F(URLRequestHttpJobWithMockSocketsTest,
882 TestHttpJobRecordsTrustAnchorHistograms) {
883 SSLSocketDataProvider ssl_socket_data(net::ASYNC, net::OK);
884 ssl_socket_data.ssl_info.cert =
885 ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
886 // Simulate a certificate chain issued by "C=US, O=Google Trust Services LLC,
887 // CN=GTS Root R4". This publicly-trusted root was chosen as it was included
888 // in 2017 and is not anticipated to be removed from all supported platforms
889 // for a few decades.
890 // Note: The actual cert in |cert| does not matter for this testing.
891 SHA256HashValue leaf_hash = {{0}};
892 SHA256HashValue intermediate_hash = {{1}};
893 SHA256HashValue root_hash = {
894 {0x98, 0x47, 0xe5, 0x65, 0x3e, 0x5e, 0x9e, 0x84, 0x75, 0x16, 0xe5,
895 0xcb, 0x81, 0x86, 0x06, 0xaa, 0x75, 0x44, 0xa1, 0x9b, 0xe6, 0x7f,
896 0xd7, 0x36, 0x6d, 0x50, 0x69, 0x88, 0xe8, 0xd8, 0x43, 0x47}};
897 ssl_socket_data.ssl_info.public_key_hashes.push_back(HashValue(leaf_hash));
898 ssl_socket_data.ssl_info.public_key_hashes.push_back(
899 HashValue(intermediate_hash));
900 ssl_socket_data.ssl_info.public_key_hashes.push_back(HashValue(root_hash));
901
902 const base::HistogramBase::Sample kGTSRootR4HistogramID = 486;
903
904 socket_factory_.AddSSLSocketDataProvider(&ssl_socket_data);
905
906 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
907 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
908 "Content-Length: 12\r\n\r\n"),
909 MockRead("Test Content")};
910 StaticSocketDataProvider socket_data(reads, writes);
911 socket_factory_.AddSocketDataProvider(&socket_data);
912
913 base::HistogramTester histograms;
914 histograms.ExpectTotalCount(kTrustAnchorRequestHistogram, 0);
915
916 TestDelegate delegate;
917 std::unique_ptr<URLRequest> request = context_->CreateRequest(
918 GURL("https://www.example.com/"), DEFAULT_PRIORITY, &delegate,
919 TRAFFIC_ANNOTATION_FOR_TESTS);
920 request->Start();
921 delegate.RunUntilComplete();
922 EXPECT_THAT(delegate.request_status(), IsOk());
923
924 histograms.ExpectTotalCount(kTrustAnchorRequestHistogram, 1);
925 histograms.ExpectUniqueSample(kTrustAnchorRequestHistogram,
926 kGTSRootR4HistogramID, 1);
927 }
928
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestHttpJobDoesNotRecordTrustAnchorHistogramsWhenNoNetworkLoad)929 TEST_F(URLRequestHttpJobWithMockSocketsTest,
930 TestHttpJobDoesNotRecordTrustAnchorHistogramsWhenNoNetworkLoad) {
931 SSLSocketDataProvider ssl_socket_data(net::ASYNC, net::OK);
932 ssl_socket_data.ssl_info.cert =
933 ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
934 // Simulate a request loaded from a non-network source, such as a disk
935 // cache.
936 ssl_socket_data.ssl_info.public_key_hashes.clear();
937
938 socket_factory_.AddSSLSocketDataProvider(&ssl_socket_data);
939
940 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
941 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
942 "Content-Length: 12\r\n\r\n"),
943 MockRead("Test Content")};
944 StaticSocketDataProvider socket_data(reads, writes);
945 socket_factory_.AddSocketDataProvider(&socket_data);
946
947 base::HistogramTester histograms;
948 histograms.ExpectTotalCount(kTrustAnchorRequestHistogram, 0);
949
950 TestDelegate delegate;
951 std::unique_ptr<URLRequest> request = context_->CreateRequest(
952 GURL("https://www.example.com/"), DEFAULT_PRIORITY, &delegate,
953 TRAFFIC_ANNOTATION_FOR_TESTS);
954 request->Start();
955 delegate.RunUntilComplete();
956 EXPECT_THAT(delegate.request_status(), IsOk());
957
958 histograms.ExpectTotalCount(kTrustAnchorRequestHistogram, 0);
959 }
960
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestHttpJobRecordsMostSpecificTrustAnchorHistograms)961 TEST_F(URLRequestHttpJobWithMockSocketsTest,
962 TestHttpJobRecordsMostSpecificTrustAnchorHistograms) {
963 SSLSocketDataProvider ssl_socket_data(net::ASYNC, net::OK);
964 ssl_socket_data.ssl_info.cert =
965 ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
966 // Simulate a certificate chain issued by "C=US, O=Google Trust Services LLC,
967 // CN=GTS Root R4". This publicly-trusted root was chosen as it was included
968 // in 2017 and is not anticipated to be removed from all supported platforms
969 // for a few decades.
970 // Note: The actual cert in |cert| does not matter for this testing.
971 SHA256HashValue leaf_hash = {{0}};
972 SHA256HashValue intermediate_hash = {{1}};
973 SHA256HashValue gts_root_r3_hash = {
974 {0x41, 0x79, 0xed, 0xd9, 0x81, 0xef, 0x74, 0x74, 0x77, 0xb4, 0x96,
975 0x26, 0x40, 0x8a, 0xf4, 0x3d, 0xaa, 0x2c, 0xa7, 0xab, 0x7f, 0x9e,
976 0x08, 0x2c, 0x10, 0x60, 0xf8, 0x40, 0x96, 0x77, 0x43, 0x48}};
977 SHA256HashValue gts_root_r4_hash = {
978 {0x98, 0x47, 0xe5, 0x65, 0x3e, 0x5e, 0x9e, 0x84, 0x75, 0x16, 0xe5,
979 0xcb, 0x81, 0x86, 0x06, 0xaa, 0x75, 0x44, 0xa1, 0x9b, 0xe6, 0x7f,
980 0xd7, 0x36, 0x6d, 0x50, 0x69, 0x88, 0xe8, 0xd8, 0x43, 0x47}};
981 ssl_socket_data.ssl_info.public_key_hashes.push_back(HashValue(leaf_hash));
982 ssl_socket_data.ssl_info.public_key_hashes.push_back(
983 HashValue(intermediate_hash));
984 ssl_socket_data.ssl_info.public_key_hashes.push_back(
985 HashValue(gts_root_r3_hash));
986 ssl_socket_data.ssl_info.public_key_hashes.push_back(
987 HashValue(gts_root_r4_hash));
988
989 const base::HistogramBase::Sample kGTSRootR3HistogramID = 485;
990
991 socket_factory_.AddSSLSocketDataProvider(&ssl_socket_data);
992
993 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
994 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
995 "Content-Length: 12\r\n\r\n"),
996 MockRead("Test Content")};
997 StaticSocketDataProvider socket_data(reads, writes);
998 socket_factory_.AddSocketDataProvider(&socket_data);
999
1000 base::HistogramTester histograms;
1001 histograms.ExpectTotalCount(kTrustAnchorRequestHistogram, 0);
1002
1003 TestDelegate delegate;
1004 std::unique_ptr<URLRequest> request = context_->CreateRequest(
1005 GURL("https://www.example.com/"), DEFAULT_PRIORITY, &delegate,
1006 TRAFFIC_ANNOTATION_FOR_TESTS);
1007 request->Start();
1008 delegate.RunUntilComplete();
1009 EXPECT_THAT(delegate.request_status(), IsOk());
1010
1011 histograms.ExpectTotalCount(kTrustAnchorRequestHistogram, 1);
1012 histograms.ExpectUniqueSample(kTrustAnchorRequestHistogram,
1013 kGTSRootR3HistogramID, 1);
1014 }
1015
1016 // Tests that the CT compliance histogram is recorded, even if CT is not
1017 // required.
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestHttpJobRecordsCTComplianceHistograms)1018 TEST_F(URLRequestHttpJobWithMockSocketsTest,
1019 TestHttpJobRecordsCTComplianceHistograms) {
1020 SSLSocketDataProvider ssl_socket_data(net::ASYNC, net::OK);
1021 ssl_socket_data.ssl_info.cert =
1022 ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
1023 ssl_socket_data.ssl_info.is_issued_by_known_root = true;
1024 ssl_socket_data.ssl_info.ct_policy_compliance_required = false;
1025 ssl_socket_data.ssl_info.ct_policy_compliance =
1026 ct::CTPolicyCompliance::CT_POLICY_NOT_DIVERSE_SCTS;
1027
1028 socket_factory_.AddSSLSocketDataProvider(&ssl_socket_data);
1029
1030 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
1031 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
1032 "Content-Length: 12\r\n\r\n"),
1033 MockRead("Test Content")};
1034 StaticSocketDataProvider socket_data(reads, writes);
1035 socket_factory_.AddSocketDataProvider(&socket_data);
1036
1037 base::HistogramTester histograms;
1038
1039 TestDelegate delegate;
1040 std::unique_ptr<URLRequest> request = context_->CreateRequest(
1041 GURL("https://www.example.com/"), DEFAULT_PRIORITY, &delegate,
1042 TRAFFIC_ANNOTATION_FOR_TESTS);
1043 request->Start();
1044 delegate.RunUntilComplete();
1045 EXPECT_THAT(delegate.request_status(), IsOk());
1046
1047 histograms.ExpectUniqueSample(
1048 kCTComplianceHistogramName,
1049 static_cast<int32_t>(ct::CTPolicyCompliance::CT_POLICY_NOT_DIVERSE_SCTS),
1050 1);
1051 // CTRequiredRequestComplianceStatus should *not* have been recorded because
1052 // it is only recorded for requests which are required to be compliant.
1053 histograms.ExpectTotalCount(kCTRequiredHistogramName, 0);
1054 }
1055
1056 // Tests that the CT compliance histograms are not recorded for
1057 // locally-installed trust anchors.
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestHttpJobDoesNotRecordCTComplianceHistogramsForLocalRoot)1058 TEST_F(URLRequestHttpJobWithMockSocketsTest,
1059 TestHttpJobDoesNotRecordCTComplianceHistogramsForLocalRoot) {
1060 SSLSocketDataProvider ssl_socket_data(net::ASYNC, net::OK);
1061 ssl_socket_data.ssl_info.cert =
1062 ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
1063 ssl_socket_data.ssl_info.is_issued_by_known_root = false;
1064 ssl_socket_data.ssl_info.ct_policy_compliance_required = false;
1065 ssl_socket_data.ssl_info.ct_policy_compliance =
1066 ct::CTPolicyCompliance::CT_POLICY_NOT_DIVERSE_SCTS;
1067
1068 socket_factory_.AddSSLSocketDataProvider(&ssl_socket_data);
1069
1070 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
1071 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
1072 "Content-Length: 12\r\n\r\n"),
1073 MockRead("Test Content")};
1074 StaticSocketDataProvider socket_data(reads, writes);
1075 socket_factory_.AddSocketDataProvider(&socket_data);
1076
1077 base::HistogramTester histograms;
1078
1079 TestDelegate delegate;
1080 std::unique_ptr<URLRequest> request = context_->CreateRequest(
1081 GURL("https://www.example.com/"), DEFAULT_PRIORITY, &delegate,
1082 TRAFFIC_ANNOTATION_FOR_TESTS);
1083 request->Start();
1084 delegate.RunUntilComplete();
1085 EXPECT_THAT(delegate.request_status(), IsOk());
1086
1087 histograms.ExpectTotalCount(kCTComplianceHistogramName, 0);
1088 histograms.ExpectTotalCount(kCTRequiredHistogramName, 0);
1089 }
1090
1091 // Tests that the CT compliance histogram is recorded when CT is required but
1092 // not compliant.
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestHttpJobRecordsCTRequiredHistogram)1093 TEST_F(URLRequestHttpJobWithMockSocketsTest,
1094 TestHttpJobRecordsCTRequiredHistogram) {
1095 SSLSocketDataProvider ssl_socket_data(net::ASYNC, net::OK);
1096 ssl_socket_data.ssl_info.cert =
1097 ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
1098 ssl_socket_data.ssl_info.is_issued_by_known_root = true;
1099 ssl_socket_data.ssl_info.ct_policy_compliance_required = true;
1100 ssl_socket_data.ssl_info.ct_policy_compliance =
1101 ct::CTPolicyCompliance::CT_POLICY_NOT_DIVERSE_SCTS;
1102
1103 socket_factory_.AddSSLSocketDataProvider(&ssl_socket_data);
1104
1105 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
1106 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
1107 "Content-Length: 12\r\n\r\n"),
1108 MockRead("Test Content")};
1109 StaticSocketDataProvider socket_data(reads, writes);
1110 socket_factory_.AddSocketDataProvider(&socket_data);
1111
1112 base::HistogramTester histograms;
1113
1114 TestDelegate delegate;
1115 std::unique_ptr<URLRequest> request = context_->CreateRequest(
1116 GURL("https://www.example.com/"), DEFAULT_PRIORITY, &delegate,
1117 TRAFFIC_ANNOTATION_FOR_TESTS);
1118 request->Start();
1119 delegate.RunUntilComplete();
1120 EXPECT_THAT(delegate.request_status(), IsOk());
1121
1122 histograms.ExpectUniqueSample(
1123 kCTComplianceHistogramName,
1124 static_cast<int32_t>(ct::CTPolicyCompliance::CT_POLICY_NOT_DIVERSE_SCTS),
1125 1);
1126 histograms.ExpectUniqueSample(
1127 kCTRequiredHistogramName,
1128 static_cast<int32_t>(ct::CTPolicyCompliance::CT_POLICY_NOT_DIVERSE_SCTS),
1129 1);
1130 }
1131
1132 // Tests that the CT compliance histograms are not recorded when there is an
1133 // unrelated certificate error.
TEST_F(URLRequestHttpJobWithMockSocketsTest,TestHttpJobDoesNotRecordCTHistogramWithCertError)1134 TEST_F(URLRequestHttpJobWithMockSocketsTest,
1135 TestHttpJobDoesNotRecordCTHistogramWithCertError) {
1136 SSLSocketDataProvider ssl_socket_data(net::ASYNC, net::OK);
1137 ssl_socket_data.ssl_info.cert =
1138 ImportCertFromFile(GetTestCertsDirectory(), "ok_cert.pem");
1139 ssl_socket_data.ssl_info.is_issued_by_known_root = true;
1140 ssl_socket_data.ssl_info.ct_policy_compliance_required = true;
1141 ssl_socket_data.ssl_info.ct_policy_compliance =
1142 ct::CTPolicyCompliance::CT_POLICY_NOT_DIVERSE_SCTS;
1143 ssl_socket_data.ssl_info.cert_status = net::CERT_STATUS_DATE_INVALID;
1144
1145 socket_factory_.AddSSLSocketDataProvider(&ssl_socket_data);
1146
1147 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
1148 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
1149 "Content-Length: 12\r\n\r\n"),
1150 MockRead("Test Content")};
1151 StaticSocketDataProvider socket_data(reads, writes);
1152 socket_factory_.AddSocketDataProvider(&socket_data);
1153
1154 base::HistogramTester histograms;
1155
1156 TestDelegate delegate;
1157 std::unique_ptr<URLRequest> request = context_->CreateRequest(
1158 GURL("https://www.example.com/"), DEFAULT_PRIORITY, &delegate,
1159 TRAFFIC_ANNOTATION_FOR_TESTS);
1160 request->Start();
1161 delegate.RunUntilComplete();
1162 EXPECT_THAT(delegate.request_status(), IsOk());
1163
1164 histograms.ExpectTotalCount(kCTComplianceHistogramName, 0);
1165 histograms.ExpectTotalCount(kCTRequiredHistogramName, 0);
1166 }
1167
TEST_F(URLRequestHttpJobWithMockSocketsTest,EncodingAdvertisementOnRange)1168 TEST_F(URLRequestHttpJobWithMockSocketsTest, EncodingAdvertisementOnRange) {
1169 MockWrite writes[] = {
1170 MockWrite("GET / HTTP/1.1\r\n"
1171 "Host: www.example.com\r\n"
1172 "Connection: keep-alive\r\n"
1173 "User-Agent: \r\n"
1174 "Accept-Encoding: identity\r\n"
1175 "Accept-Language: en-us,fr\r\n"
1176 "Range: bytes=0-1023\r\n\r\n")};
1177
1178 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
1179 "Accept-Ranges: bytes\r\n"
1180 "Content-Length: 12\r\n\r\n"),
1181 MockRead("Test Content")};
1182
1183 StaticSocketDataProvider socket_data(reads, writes);
1184 socket_factory_.AddSocketDataProvider(&socket_data);
1185
1186 TestDelegate delegate;
1187 std::unique_ptr<URLRequest> request =
1188 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
1189 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
1190
1191 // Make the extra header to trigger the change in "Accepted-Encoding"
1192 HttpRequestHeaders headers;
1193 headers.SetHeader("Range", "bytes=0-1023");
1194 request->SetExtraRequestHeaders(headers);
1195
1196 request->Start();
1197 base::RunLoop().RunUntilIdle();
1198
1199 EXPECT_THAT(delegate.request_status(), IsOk());
1200 EXPECT_EQ(12, request->received_response_content_length());
1201 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
1202 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
1203 }
1204
TEST_F(URLRequestHttpJobWithMockSocketsTest,RangeRequestOverrideEncoding)1205 TEST_F(URLRequestHttpJobWithMockSocketsTest, RangeRequestOverrideEncoding) {
1206 MockWrite writes[] = {
1207 MockWrite("GET / HTTP/1.1\r\n"
1208 "Host: www.example.com\r\n"
1209 "Connection: keep-alive\r\n"
1210 "Accept-Encoding: gzip, deflate\r\n"
1211 "User-Agent: \r\n"
1212 "Accept-Language: en-us,fr\r\n"
1213 "Range: bytes=0-1023\r\n\r\n")};
1214
1215 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
1216 "Accept-Ranges: bytes\r\n"
1217 "Content-Length: 12\r\n\r\n"),
1218 MockRead("Test Content")};
1219
1220 StaticSocketDataProvider socket_data(reads, writes);
1221 socket_factory_.AddSocketDataProvider(&socket_data);
1222
1223 TestDelegate delegate;
1224 std::unique_ptr<URLRequest> request =
1225 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
1226 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
1227
1228 // Explicitly set "Accept-Encoding" to make sure it's not overridden by
1229 // AddExtraHeaders
1230 HttpRequestHeaders headers;
1231 headers.SetHeader("Accept-Encoding", "gzip, deflate");
1232 headers.SetHeader("Range", "bytes=0-1023");
1233 request->SetExtraRequestHeaders(headers);
1234
1235 request->Start();
1236 base::RunLoop().RunUntilIdle();
1237
1238 EXPECT_THAT(delegate.request_status(), IsOk());
1239 EXPECT_EQ(12, request->received_response_content_length());
1240 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
1241 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
1242 }
1243
TEST_F(URLRequestHttpJobTest,TestCancelWhileReadingCookies)1244 TEST_F(URLRequestHttpJobTest, TestCancelWhileReadingCookies) {
1245 DelayedCookieMonster cookie_monster;
1246 TestURLRequestContext context(true);
1247 context.set_cookie_store(&cookie_monster);
1248 context.Init();
1249
1250 TestDelegate delegate;
1251 std::unique_ptr<URLRequest> request =
1252 context.CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
1253 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
1254
1255 request->Start();
1256 request->Cancel();
1257 delegate.RunUntilComplete();
1258
1259 EXPECT_THAT(delegate.request_status(), IsError(ERR_ABORTED));
1260 }
1261
1262 // Make sure that SetPriority actually sets the URLRequestHttpJob's
1263 // priority, before start. Other tests handle the after start case.
TEST_F(URLRequestHttpJobTest,SetPriorityBasic)1264 TEST_F(URLRequestHttpJobTest, SetPriorityBasic) {
1265 auto job = std::make_unique<TestURLRequestHttpJob>(req_.get());
1266 EXPECT_EQ(DEFAULT_PRIORITY, job->priority());
1267
1268 job->SetPriority(LOWEST);
1269 EXPECT_EQ(LOWEST, job->priority());
1270
1271 job->SetPriority(LOW);
1272 EXPECT_EQ(LOW, job->priority());
1273 }
1274
1275 // Make sure that URLRequestHttpJob passes on its priority to its
1276 // transaction on start.
TEST_F(URLRequestHttpJobTest,SetTransactionPriorityOnStart)1277 TEST_F(URLRequestHttpJobTest, SetTransactionPriorityOnStart) {
1278 test_job_interceptor_->set_main_intercept_job(
1279 std::make_unique<TestURLRequestHttpJob>(req_.get()));
1280 req_->SetPriority(LOW);
1281
1282 EXPECT_FALSE(network_layer_.last_transaction());
1283
1284 req_->Start();
1285
1286 ASSERT_TRUE(network_layer_.last_transaction());
1287 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority());
1288 }
1289
1290 // Make sure that URLRequestHttpJob passes on its priority updates to
1291 // its transaction.
TEST_F(URLRequestHttpJobTest,SetTransactionPriority)1292 TEST_F(URLRequestHttpJobTest, SetTransactionPriority) {
1293 test_job_interceptor_->set_main_intercept_job(
1294 std::make_unique<TestURLRequestHttpJob>(req_.get()));
1295 req_->SetPriority(LOW);
1296 req_->Start();
1297 ASSERT_TRUE(network_layer_.last_transaction());
1298 EXPECT_EQ(LOW, network_layer_.last_transaction()->priority());
1299
1300 req_->SetPriority(HIGHEST);
1301 EXPECT_EQ(HIGHEST, network_layer_.last_transaction()->priority());
1302 }
1303
TEST_F(URLRequestHttpJobTest,HSTSInternalRedirectTest)1304 TEST_F(URLRequestHttpJobTest, HSTSInternalRedirectTest) {
1305 // Setup HSTS state.
1306 context_.transport_security_state()->AddHSTS(
1307 "upgrade.test", base::Time::Now() + base::TimeDelta::FromSeconds(10),
1308 true);
1309 ASSERT_TRUE(
1310 context_.transport_security_state()->ShouldUpgradeToSSL("upgrade.test"));
1311 ASSERT_FALSE(context_.transport_security_state()->ShouldUpgradeToSSL(
1312 "no-upgrade.test"));
1313
1314 struct TestCase {
1315 const char* url;
1316 bool upgrade_expected;
1317 const char* url_expected;
1318 } cases[] = {
1319 {"http://upgrade.test/", true, "https://upgrade.test/"},
1320 {"http://upgrade.test:123/", true, "https://upgrade.test:123/"},
1321 {"http://no-upgrade.test/", false, "http://no-upgrade.test/"},
1322 {"http://no-upgrade.test:123/", false, "http://no-upgrade.test:123/"},
1323 #if BUILDFLAG(ENABLE_WEBSOCKETS)
1324 {"ws://upgrade.test/", true, "wss://upgrade.test/"},
1325 {"ws://upgrade.test:123/", true, "wss://upgrade.test:123/"},
1326 {"ws://no-upgrade.test/", false, "ws://no-upgrade.test/"},
1327 {"ws://no-upgrade.test:123/", false, "ws://no-upgrade.test:123/"},
1328 #endif // BUILDFLAG(ENABLE_WEBSOCKETS)
1329 };
1330
1331 for (const auto& test : cases) {
1332 SCOPED_TRACE(test.url);
1333 TestDelegate d;
1334 TestNetworkDelegate network_delegate;
1335 std::unique_ptr<URLRequest> r(context_.CreateRequest(
1336 GURL(test.url), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS));
1337
1338 net_log_.Clear();
1339 r->Start();
1340 d.RunUntilComplete();
1341
1342 if (test.upgrade_expected) {
1343 auto entries = net_log_.GetEntriesWithType(
1344 net::NetLogEventType::URL_REQUEST_REDIRECT_JOB);
1345 int redirects = entries.size();
1346 for (const auto& entry : entries) {
1347 EXPECT_EQ("HSTS", GetStringValueFromParams(entry, "reason"));
1348 }
1349 EXPECT_EQ(1, redirects);
1350 EXPECT_EQ(1, d.received_redirect_count());
1351 EXPECT_EQ(2u, r->url_chain().size());
1352 } else {
1353 EXPECT_EQ(0, d.received_redirect_count());
1354 EXPECT_EQ(1u, r->url_chain().size());
1355 }
1356 EXPECT_EQ(GURL(test.url_expected), r->url());
1357 }
1358 }
1359
TEST_F(URLRequestHttpJobTest,HSTSInternalRedirectCallback)1360 TEST_F(URLRequestHttpJobTest, HSTSInternalRedirectCallback) {
1361 EmbeddedTestServer https_test(EmbeddedTestServer::TYPE_HTTPS);
1362 https_test.AddDefaultHandlers(base::FilePath());
1363 ASSERT_TRUE(https_test.Start());
1364
1365 TestURLRequestContext context;
1366 context.transport_security_state()->AddHSTS(
1367 "127.0.0.1", base::Time::Now() + base::TimeDelta::FromSeconds(10), true);
1368 ASSERT_TRUE(
1369 context.transport_security_state()->ShouldUpgradeToSSL("127.0.0.1"));
1370
1371 GURL::Replacements replace_scheme;
1372 replace_scheme.SetSchemeStr("http");
1373
1374 {
1375 GURL url(
1376 https_test.GetURL("/echoheader").ReplaceComponents(replace_scheme));
1377 TestDelegate delegate;
1378 HttpRequestHeaders extra_headers;
1379 extra_headers.SetHeader("X-HSTS-Test", "1");
1380
1381 HttpRawRequestHeaders raw_req_headers;
1382
1383 std::unique_ptr<URLRequest> r(context.CreateRequest(
1384 url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
1385 r->SetExtraRequestHeaders(extra_headers);
1386 r->SetRequestHeadersCallback(base::BindRepeating(
1387 &HttpRawRequestHeaders::Assign, base::Unretained(&raw_req_headers)));
1388
1389 r->Start();
1390 delegate.RunUntilRedirect();
1391
1392 EXPECT_FALSE(raw_req_headers.headers().empty());
1393 std::string value;
1394 EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-HSTS-Test", &value));
1395 EXPECT_EQ("1", value);
1396 EXPECT_EQ("GET /echoheader HTTP/1.1\r\n", raw_req_headers.request_line());
1397
1398 raw_req_headers = HttpRawRequestHeaders();
1399
1400 r->FollowDeferredRedirect(base::nullopt /* removed_headers */,
1401 base::nullopt /* modified_headers */);
1402 delegate.RunUntilComplete();
1403
1404 EXPECT_FALSE(raw_req_headers.headers().empty());
1405 }
1406
1407 {
1408 GURL url(https_test.GetURL("/echoheader?foo=bar")
1409 .ReplaceComponents(replace_scheme));
1410 TestDelegate delegate;
1411
1412 HttpRawRequestHeaders raw_req_headers;
1413
1414 std::unique_ptr<URLRequest> r(context.CreateRequest(
1415 url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
1416 r->SetRequestHeadersCallback(base::BindRepeating(
1417 &HttpRawRequestHeaders::Assign, base::Unretained(&raw_req_headers)));
1418
1419 r->Start();
1420 delegate.RunUntilRedirect();
1421
1422 EXPECT_EQ("GET /echoheader?foo=bar HTTP/1.1\r\n",
1423 raw_req_headers.request_line());
1424 }
1425
1426 {
1427 GURL url(
1428 https_test.GetURL("/echoheader#foo").ReplaceComponents(replace_scheme));
1429 TestDelegate delegate;
1430
1431 HttpRawRequestHeaders raw_req_headers;
1432
1433 std::unique_ptr<URLRequest> r(context.CreateRequest(
1434 url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS));
1435 r->SetRequestHeadersCallback(base::BindRepeating(
1436 &HttpRawRequestHeaders::Assign, base::Unretained(&raw_req_headers)));
1437
1438 r->Start();
1439 delegate.RunUntilRedirect();
1440
1441 EXPECT_EQ("GET /echoheader HTTP/1.1\r\n", raw_req_headers.request_line());
1442 }
1443 }
1444
1445 class URLRequestHttpJobWithBrotliSupportTest : public TestWithTaskEnvironment {
1446 protected:
URLRequestHttpJobWithBrotliSupportTest()1447 URLRequestHttpJobWithBrotliSupportTest()
1448 : context_(new TestURLRequestContext(true)) {
1449 auto params = std::make_unique<HttpNetworkSession::Params>();
1450 context_->set_enable_brotli(true);
1451 context_->set_http_network_session_params(std::move(params));
1452 context_->set_client_socket_factory(&socket_factory_);
1453 context_->Init();
1454 }
1455
1456 MockClientSocketFactory socket_factory_;
1457 std::unique_ptr<TestURLRequestContext> context_;
1458 };
1459
TEST_F(URLRequestHttpJobWithBrotliSupportTest,NoBrotliAdvertisementOverHttp)1460 TEST_F(URLRequestHttpJobWithBrotliSupportTest, NoBrotliAdvertisementOverHttp) {
1461 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)};
1462 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
1463 "Content-Length: 12\r\n\r\n"),
1464 MockRead("Test Content")};
1465 StaticSocketDataProvider socket_data(reads, writes);
1466 socket_factory_.AddSocketDataProvider(&socket_data);
1467
1468 TestDelegate delegate;
1469 std::unique_ptr<URLRequest> request =
1470 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
1471 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
1472 request->Start();
1473 base::RunLoop().RunUntilIdle();
1474
1475 EXPECT_THAT(delegate.request_status(), IsOk());
1476 EXPECT_EQ(12, request->received_response_content_length());
1477 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
1478 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
1479 }
1480
TEST_F(URLRequestHttpJobWithBrotliSupportTest,BrotliAdvertisement)1481 TEST_F(URLRequestHttpJobWithBrotliSupportTest, BrotliAdvertisement) {
1482 net::SSLSocketDataProvider ssl_socket_data_provider(net::ASYNC, net::OK);
1483 ssl_socket_data_provider.next_proto = kProtoHTTP11;
1484 ssl_socket_data_provider.ssl_info.cert =
1485 ImportCertFromFile(GetTestCertsDirectory(), "unittest.selfsigned.der");
1486 ASSERT_TRUE(ssl_socket_data_provider.ssl_info.cert);
1487 socket_factory_.AddSSLSocketDataProvider(&ssl_socket_data_provider);
1488
1489 MockWrite writes[] = {
1490 MockWrite("GET / HTTP/1.1\r\n"
1491 "Host: www.example.com\r\n"
1492 "Connection: keep-alive\r\n"
1493 "User-Agent: \r\n"
1494 "Accept-Encoding: gzip, deflate, br\r\n"
1495 "Accept-Language: en-us,fr\r\n\r\n")};
1496 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
1497 "Content-Length: 12\r\n\r\n"),
1498 MockRead("Test Content")};
1499 StaticSocketDataProvider socket_data(reads, writes);
1500 socket_factory_.AddSocketDataProvider(&socket_data);
1501
1502 TestDelegate delegate;
1503 std::unique_ptr<URLRequest> request =
1504 context_->CreateRequest(GURL("https://www.example.com"), DEFAULT_PRIORITY,
1505 &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
1506 request->Start();
1507 base::RunLoop().RunUntilIdle();
1508
1509 EXPECT_THAT(delegate.request_status(), IsOk());
1510 EXPECT_EQ(12, request->received_response_content_length());
1511 EXPECT_EQ(CountWriteBytes(writes), request->GetTotalSentBytes());
1512 EXPECT_EQ(CountReadBytes(reads), request->GetTotalReceivedBytes());
1513 }
1514
1515 #if defined(OS_ANDROID)
TEST_F(URLRequestHttpJobTest,AndroidCleartextPermittedTest)1516 TEST_F(URLRequestHttpJobTest, AndroidCleartextPermittedTest) {
1517 context_.set_check_cleartext_permitted(true);
1518
1519 static constexpr struct TestCase {
1520 const char* url;
1521 bool cleartext_permitted;
1522 bool should_block;
1523 int expected_per_host_call_count;
1524 int expected_default_call_count;
1525 } kTestCases[] = {
1526 {"http://unblocked.test/", true, false, 1, 0},
1527 {"https://unblocked.test/", true, false, 0, 0},
1528 {"http://blocked.test/", false, true, 1, 0},
1529 {"https://blocked.test/", false, false, 0, 0},
1530 // If determining the per-host cleartext policy causes an
1531 // IllegalArgumentException (because the hostname is invalid),
1532 // the default configuration should be applied, and the
1533 // exception should not cause a JNI error.
1534 {"http://./", false, true, 1, 1},
1535 {"http://./", true, false, 1, 1},
1536 // Even if the host name would be considered invalid, https
1537 // schemes should not trigger cleartext policy checks.
1538 {"https://./", false, false, 0, 0},
1539 };
1540
1541 JNIEnv* env = base::android::AttachCurrentThread();
1542 for (const TestCase& test : kTestCases) {
1543 Java_AndroidNetworkLibraryTestUtil_setUpSecurityPolicyForTesting(
1544 env, test.cleartext_permitted);
1545
1546 TestDelegate delegate;
1547 std::unique_ptr<URLRequest> request =
1548 context_.CreateRequest(GURL(test.url), DEFAULT_PRIORITY, &delegate,
1549 TRAFFIC_ANNOTATION_FOR_TESTS);
1550 request->Start();
1551 delegate.RunUntilComplete();
1552
1553 if (test.should_block) {
1554 EXPECT_THAT(delegate.request_status(),
1555 IsError(ERR_CLEARTEXT_NOT_PERMITTED));
1556 } else {
1557 // Should fail since there's no test server running
1558 EXPECT_THAT(delegate.request_status(), IsError(ERR_FAILED));
1559 }
1560 EXPECT_EQ(
1561 Java_AndroidNetworkLibraryTestUtil_getPerHostCleartextCheckCount(env),
1562 test.expected_per_host_call_count);
1563 EXPECT_EQ(
1564 Java_AndroidNetworkLibraryTestUtil_getDefaultCleartextCheckCount(env),
1565 test.expected_default_call_count);
1566 }
1567 }
1568 #endif
1569
1570 #if BUILDFLAG(ENABLE_WEBSOCKETS)
1571
1572 class URLRequestHttpJobWebSocketTest : public TestWithTaskEnvironment {
1573 protected:
URLRequestHttpJobWebSocketTest()1574 URLRequestHttpJobWebSocketTest() : context_(true) {
1575 context_.set_network_delegate(&network_delegate_);
1576 context_.set_client_socket_factory(&socket_factory_);
1577 context_.Init();
1578 req_ =
1579 context_.CreateRequest(GURL("ws://www.example.org"), DEFAULT_PRIORITY,
1580 &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS);
1581 }
1582
1583 // A Network Delegate is required for the WebSocketHandshakeStreamBase
1584 // object to be passed on to the HttpNetworkTransaction.
1585 TestNetworkDelegate network_delegate_;
1586
1587 TestURLRequestContext context_;
1588 MockClientSocketFactory socket_factory_;
1589 TestDelegate delegate_;
1590 std::unique_ptr<URLRequest> req_;
1591 };
1592
TEST_F(URLRequestHttpJobWebSocketTest,RejectedWithoutCreateHelper)1593 TEST_F(URLRequestHttpJobWebSocketTest, RejectedWithoutCreateHelper) {
1594 req_->Start();
1595 base::RunLoop().RunUntilIdle();
1596 EXPECT_THAT(delegate_.request_status(), IsError(ERR_DISALLOWED_URL_SCHEME));
1597 }
1598
TEST_F(URLRequestHttpJobWebSocketTest,CreateHelperPassedThrough)1599 TEST_F(URLRequestHttpJobWebSocketTest, CreateHelperPassedThrough) {
1600 HttpRequestHeaders headers;
1601 headers.SetHeader("Connection", "Upgrade");
1602 headers.SetHeader("Upgrade", "websocket");
1603 headers.SetHeader("Origin", "http://www.example.org");
1604 headers.SetHeader("Sec-WebSocket-Version", "13");
1605 req_->SetExtraRequestHeaders(headers);
1606
1607 MockWrite writes[] = {
1608 MockWrite("GET / HTTP/1.1\r\n"
1609 "Host: www.example.org\r\n"
1610 "Connection: Upgrade\r\n"
1611 "Upgrade: websocket\r\n"
1612 "Origin: http://www.example.org\r\n"
1613 "Sec-WebSocket-Version: 13\r\n"
1614 "User-Agent: \r\n"
1615 "Accept-Encoding: gzip, deflate\r\n"
1616 "Accept-Language: en-us,fr\r\n"
1617 "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
1618 "Sec-WebSocket-Extensions: permessage-deflate; "
1619 "client_max_window_bits\r\n\r\n")};
1620
1621 MockRead reads[] = {
1622 MockRead("HTTP/1.1 101 Switching Protocols\r\n"
1623 "Upgrade: websocket\r\n"
1624 "Connection: Upgrade\r\n"
1625 "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n"),
1626 MockRead(ASYNC, 0)};
1627
1628 StaticSocketDataProvider data(reads, writes);
1629 socket_factory_.AddSocketDataProvider(&data);
1630
1631 auto websocket_stream_create_helper =
1632 std::make_unique<TestWebSocketHandshakeStreamCreateHelper>();
1633
1634 req_->SetUserData(kWebSocketHandshakeUserDataKey,
1635 std::move(websocket_stream_create_helper));
1636 req_->SetLoadFlags(LOAD_DISABLE_CACHE);
1637 req_->Start();
1638 base::RunLoop().RunUntilIdle();
1639 EXPECT_THAT(delegate_.request_status(), IsOk());
1640 EXPECT_TRUE(delegate_.response_completed());
1641
1642 EXPECT_TRUE(data.AllWriteDataConsumed());
1643 EXPECT_TRUE(data.AllReadDataConsumed());
1644 }
1645
1646 #endif // BUILDFLAG(ENABLE_WEBSOCKETS)
1647
SetAllCookies(CookieMonster * cm,const CookieList & list)1648 bool SetAllCookies(CookieMonster* cm, const CookieList& list) {
1649 DCHECK(cm);
1650 ResultSavingCookieCallback<CanonicalCookie::CookieInclusionStatus> callback;
1651 cm->SetAllCookiesAsync(list, callback.MakeCallback());
1652 callback.WaitUntilDone();
1653 return callback.result().IsInclude();
1654 }
1655
CreateAndSetCookie(CookieStore * cs,const GURL & url,const std::string & cookie_line)1656 bool CreateAndSetCookie(CookieStore* cs,
1657 const GURL& url,
1658 const std::string& cookie_line) {
1659 auto cookie = CanonicalCookie::Create(url, cookie_line, base::Time::Now(),
1660 base::nullopt);
1661 if (!cookie)
1662 return false;
1663 DCHECK(cs);
1664 ResultSavingCookieCallback<CanonicalCookie::CookieInclusionStatus> callback;
1665 cs->SetCanonicalCookieAsync(std::move(cookie), url.scheme(),
1666 CookieOptions::MakeAllInclusive(),
1667 callback.MakeCallback());
1668 callback.WaitUntilDone();
1669 return callback.result().IsInclude();
1670 }
1671
RunRequest(TestURLRequestContext * context,const GURL & url)1672 void RunRequest(TestURLRequestContext* context, const GURL& url) {
1673 TestDelegate delegate;
1674 std::unique_ptr<URLRequest> request = context->CreateRequest(
1675 url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS);
1676
1677 // Make this a laxly same-site context to allow setting
1678 // SameSite=Lax-by-default cookies.
1679 request->set_site_for_cookies(SiteForCookies::FromUrl(url));
1680 request->Start();
1681 delegate.RunUntilComplete();
1682 }
1683
1684 } // namespace
1685
TEST_F(URLRequestHttpJobTest,CookieSchemeRequestSchemeHistogram)1686 TEST_F(URLRequestHttpJobTest, CookieSchemeRequestSchemeHistogram) {
1687 base::HistogramTester histograms;
1688 const std::string test_histogram = "Cookie.CookieSchemeRequestScheme";
1689
1690 CookieMonster cm(nullptr, nullptr);
1691 TestURLRequestContext context(true);
1692 context.set_cookie_store(&cm);
1693 context.Init();
1694
1695 // Secure set cookie marked as Unset source scheme.
1696 // Using port 7 because it fails the transaction without sending a request and
1697 // prevents a timeout due to the fake addresses. Because we only need the
1698 // headers to be generated (and thus the histogram filled) and not actually
1699 // sent this is acceptable.
1700 GURL nonsecure_url_for_unset1("http://unset1.example:7");
1701 GURL secure_url_for_unset1("https://unset1.example:7");
1702
1703 // Normally the source scheme would be set by
1704 // CookieMonster::SetCanonicalCookie(), however we're using SetAllCookies() to
1705 // bypass the source scheme check in order to test the kUnset state which
1706 // would normally only happen during an existing cookie DB version upgrade.
1707 std::unique_ptr<CanonicalCookie> unset_cookie1 = CanonicalCookie::Create(
1708 secure_url_for_unset1, "NoSourceSchemeHttps=val", base::Time::Now(),
1709 base::nullopt /* server_time */);
1710 unset_cookie1->SetSourceScheme(net::CookieSourceScheme::kUnset);
1711
1712 CookieList list1 = {*unset_cookie1};
1713 EXPECT_TRUE(SetAllCookies(&cm, list1));
1714 RunRequest(&context, nonsecure_url_for_unset1);
1715 histograms.ExpectBucketCount(
1716 test_histogram,
1717 URLRequestHttpJob::CookieRequestScheme::kUnsetCookieScheme, 1);
1718 RunRequest(&context, secure_url_for_unset1);
1719 histograms.ExpectBucketCount(
1720 test_histogram,
1721 URLRequestHttpJob::CookieRequestScheme::kUnsetCookieScheme, 2);
1722
1723 // Nonsecure set cookie marked as unset source scheme.
1724 GURL nonsecure_url_for_unset2("http://unset2.example:7");
1725 GURL secure_url_for_unset2("https://unset2.example:7");
1726
1727 std::unique_ptr<CanonicalCookie> unset_cookie2 = CanonicalCookie::Create(
1728 nonsecure_url_for_unset2, "NoSourceSchemeHttp=val", base::Time::Now(),
1729 base::nullopt /* server_time */);
1730 unset_cookie2->SetSourceScheme(net::CookieSourceScheme::kUnset);
1731
1732 CookieList list2 = {*unset_cookie2};
1733 EXPECT_TRUE(SetAllCookies(&cm, list2));
1734 RunRequest(&context, nonsecure_url_for_unset2);
1735 histograms.ExpectBucketCount(
1736 test_histogram,
1737 URLRequestHttpJob::CookieRequestScheme::kUnsetCookieScheme, 3);
1738 RunRequest(&context, secure_url_for_unset2);
1739 histograms.ExpectBucketCount(
1740 test_histogram,
1741 URLRequestHttpJob::CookieRequestScheme::kUnsetCookieScheme, 4);
1742
1743 // Secure set cookie with source scheme marked appropriately.
1744 GURL nonsecure_url_for_secure_set("http://secureset.example:7");
1745 GURL secure_url_for_secure_set("https://secureset.example:7");
1746
1747 EXPECT_TRUE(
1748 CreateAndSetCookie(&cm, secure_url_for_secure_set, "SecureScheme=val"));
1749 RunRequest(&context, nonsecure_url_for_secure_set);
1750 histograms.ExpectBucketCount(
1751 test_histogram,
1752 URLRequestHttpJob::CookieRequestScheme::kSecureSetNonsecureRequest, 1);
1753 RunRequest(&context, secure_url_for_secure_set);
1754 histograms.ExpectBucketCount(
1755 test_histogram,
1756 URLRequestHttpJob::CookieRequestScheme::kSecureSetSecureRequest, 1);
1757
1758 // Nonsecure set cookie with source scheme marked appropriately.
1759 GURL nonsecure_url_for_nonsecure_set("http://nonsecureset.example:7");
1760 GURL secure_url_for_nonsecure_set("https://nonsecureset.example:7");
1761
1762 EXPECT_TRUE(CreateAndSetCookie(&cm, nonsecure_url_for_nonsecure_set,
1763 "NonSecureScheme=val"));
1764 RunRequest(&context, nonsecure_url_for_nonsecure_set);
1765 histograms.ExpectBucketCount(
1766 test_histogram,
1767 URLRequestHttpJob::CookieRequestScheme::kNonsecureSetNonsecureRequest, 1);
1768 RunRequest(&context, secure_url_for_nonsecure_set);
1769 histograms.ExpectBucketCount(
1770 test_histogram,
1771 URLRequestHttpJob::CookieRequestScheme::kNonsecureSetSecureRequest, 1);
1772 }
1773
1774 } // namespace net
1775