[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
Douglas Creager | 4d1ef84 | 2018-07-16 23:42:25 | [diff] [blame] | 5 | #include <algorithm> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 6 | #include <utility> |
| 7 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 8 | // This must be before Windows headers |
Sebastien Marchand | 17fa278 | 2019-01-25 19:28:10 | [diff] [blame] | 9 | #include "base/bind_helpers.h" |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10 | #include "build/build_config.h" |
| 11 | |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 12 | #if defined(OS_WIN) |
robliao | eb9bfd64 | 2017-05-18 17:35:16 | [diff] [blame] | 13 | #include <objbase.h> |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 14 | #include <shlobj.h> |
Fan Yang | 5a88d5d | 2017-10-24 01:14:29 | [diff] [blame] | 15 | #include <windows.h> |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 16 | #include <wrl/client.h> |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 17 | #endif |
| 18 | |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 21 | #include <algorithm> |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 22 | #include <limits> |
Devlin Cronin | e4bcb40e | 2018-06-05 18:02:47 | [diff] [blame] | 23 | #include <memory> |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 24 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 25 | #include "base/base64url.h" |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 26 | #include "base/bind.h" |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 27 | #include "base/compiler_specific.h" |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 28 | #include "base/files/file_path.h" |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame] | 29 | #include "base/files/file_util.h" |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 30 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 31 | #include "base/format_macros.h" |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 32 | #include "base/json/json_reader.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 33 | #include "base/location.h" |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 34 | #include "base/memory/weak_ptr.h" |
Alexander Timin | 4f9c35c | 2018-11-01 20:15:20 | [diff] [blame] | 35 | #include "base/message_loop/message_loop.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 36 | #include "base/path_service.h" |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 37 | #include "base/run_loop.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 38 | #include "base/single_thread_task_runner.h" |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 39 | #include "base/stl_util.h" |
[email protected] | 4dc3ad4f | 2013-06-11 07:15:50 | [diff] [blame] | 40 | #include "base/strings/string_number_conversions.h" |
[email protected] | d069c11a | 2013-04-13 00:01:55 | [diff] [blame] | 41 | #include "base/strings/string_piece.h" |
[email protected] | d778e042 | 2013-03-06 18:10:22 | [diff] [blame] | 42 | #include "base/strings/string_split.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 43 | #include "base/strings/string_util.h" |
| 44 | #include "base/strings/stringprintf.h" |
[email protected] | 750b2f3c | 2013-06-07 18:41:05 | [diff] [blame] | 45 | #include "base/strings/utf_string_conversions.h" |
Matt Menke | 0743e34 | 2019-06-11 20:48:10 | [diff] [blame] | 46 | #include "base/test/bind_test_util.h" |
Devlin Cronin | e4bcb40e | 2018-06-05 18:02:47 | [diff] [blame] | 47 | #include "base/test/metrics/histogram_tester.h" |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 48 | #include "base/test/scoped_feature_list.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 49 | #include "base/threading/thread_task_runner_handle.h" |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 50 | #include "base/values.h" |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 51 | #include "build/buildflag.h" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 52 | #include "net/base/chunked_upload_data_stream.h" |
tfarina | 43a416b | 2016-01-06 21:48:07 | [diff] [blame] | 53 | #include "net/base/directory_listing.h" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 54 | #include "net/base/elements_upload_data_stream.h" |
David Benjamin | a7fde61 | 2019-03-15 14:20:58 | [diff] [blame] | 55 | #include "net/base/escape.h" |
Mike West | fbd4411 | 2019-05-08 18:23:37 | [diff] [blame] | 56 | #include "net/base/features.h" |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 57 | #include "net/base/layered_network_delegate.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 58 | #include "net/base/load_flags.h" |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 59 | #include "net/base/load_timing_info.h" |
| 60 | #include "net/base/load_timing_info_test_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 61 | #include "net/base/net_errors.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 62 | #include "net/base/net_module.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 63 | #include "net/base/proxy_server.h" |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 64 | #include "net/base/request_priority.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 65 | #include "net/base/test_completion_callback.h" |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 66 | #include "net/base/upload_bytes_element_reader.h" |
| 67 | #include "net/base/upload_data_stream.h" |
| 68 | #include "net/base/upload_file_element_reader.h" |
tfarina | 7a4a7fd | 2016-01-20 14:23:44 | [diff] [blame] | 69 | #include "net/base/url_util.h" |
Matt Mueller | a4aaa9d | 2019-04-24 21:09:14 | [diff] [blame] | 70 | #include "net/cert/cert_net_fetcher.h" |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 71 | #include "net/cert/crl_set.h" |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 72 | #include "net/cert/ct_policy_enforcer.h" |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 73 | #include "net/cert/ct_policy_status.h" |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 74 | #include "net/cert/do_nothing_ct_verifier.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 75 | #include "net/cert/ev_root_ca_metadata.h" |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 76 | #include "net/cert/mock_cert_verifier.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 77 | #include "net/cert/multi_log_ct_verifier.h" |
eranm | dcec963 | 2016-10-10 14:16:10 | [diff] [blame] | 78 | #include "net/cert/signed_certificate_timestamp_and_status.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 79 | #include "net/cert/test_root_certs.h" |
Matt Mueller | a4aaa9d | 2019-04-24 21:09:14 | [diff] [blame] | 80 | #include "net/cert_net/cert_net_fetcher_impl.h" |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 81 | #include "net/cookies/canonical_cookie_test_helpers.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 82 | #include "net/cookies/cookie_monster.h" |
| 83 | #include "net/cookies/cookie_store_test_helpers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 84 | #include "net/disk_cache/disk_cache.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 85 | #include "net/dns/mock_host_resolver.h" |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 86 | #include "net/http/http_byte_range.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 87 | #include "net/http/http_cache.h" |
| 88 | #include "net/http/http_network_layer.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 89 | #include "net/http/http_network_session.h" |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 90 | #include "net/http/http_request_headers.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 91 | #include "net/http/http_response_headers.h" |
Matt Menke | 60916074 | 2019-08-02 18:47:26 | [diff] [blame] | 92 | #include "net/http/http_server_properties.h" |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 93 | #include "net/http/http_util.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 94 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 95 | #include "net/log/net_log_source.h" |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 96 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 97 | #include "net/log/test_net_log_util.h" |
Scott Violet | 0caaaf43 | 2018-03-24 00:43:59 | [diff] [blame] | 98 | #include "net/net_buildflags.h" |
Lily Houghton | ffe89daa0 | 2018-03-09 18:30:03 | [diff] [blame] | 99 | #include "net/proxy_resolution/proxy_resolution_service.h" |
Ryan Hamilton | a3ee93a7 | 2018-08-01 22:03:08 | [diff] [blame] | 100 | #include "net/quic/mock_crypto_client_stream_factory.h" |
| 101 | #include "net/quic/quic_server_info.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 102 | #include "net/socket/socket_test_util.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 103 | #include "net/socket/ssl_client_socket.h" |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 104 | #include "net/ssl/client_cert_identity_test_util.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 105 | #include "net/ssl/ssl_connection_status_flags.h" |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 106 | #include "net/ssl/ssl_private_key.h" |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 107 | #include "net/ssl/ssl_server_config.h" |
David Benjamin | 151ec6b | 2019-08-02 19:38:52 | [diff] [blame] | 108 | #include "net/ssl/test_ssl_config_service.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 109 | #include "net/test/cert_test_util.h" |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 110 | #include "net/test/embedded_test_server/embedded_test_server.h" |
| 111 | #include "net/test/embedded_test_server/http_request.h" |
| 112 | #include "net/test/embedded_test_server/http_response.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 113 | #include "net/test/gtest_util.h" |
[email protected] | 89b3252 | 2013-05-07 20:04:21 | [diff] [blame] | 114 | #include "net/test/spawned_test_server/spawned_test_server.h" |
rsleevi | a69c79a | 2016-06-22 03:28:43 | [diff] [blame] | 115 | #include "net/test/test_data_directory.h" |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 116 | #include "net/test/test_with_scoped_task_environment.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 117 | #include "net/test/url_request/url_request_failed_job.h" |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 118 | #include "net/test/url_request/url_request_mock_http_job.h" |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 119 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 120 | #include "net/url_request/data_protocol_handler.h" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 121 | #include "net/url_request/static_http_user_agent_settings.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 122 | #include "net/url_request/url_request.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 123 | #include "net/url_request/url_request_filter.h" |
[email protected] | bcb84f8b | 2009-08-31 16:20:14 | [diff] [blame] | 124 | #include "net/url_request/url_request_http_job.h" |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 125 | #include "net/url_request/url_request_http_job_histogram.h" |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 126 | #include "net/url_request/url_request_intercepting_job_factory.h" |
| 127 | #include "net/url_request/url_request_interceptor.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 128 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 129 | #include "net/url_request/url_request_redirect_job.h" |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 130 | #include "net/url_request/url_request_test_job.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 131 | #include "net/url_request/url_request_test_util.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 132 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 133 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 134 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 135 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 136 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 137 | #include "net/base/filename_util.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 138 | #include "net/url_request/file_protocol_handler.h" |
| 139 | #include "net/url_request/url_request_file_dir_job.h" |
| 140 | #endif |
| 141 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 142 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
Emily Stark | 0122798 | 2019-06-06 18:45:56 | [diff] [blame] | 143 | #include "net/ftp/ftp_auth_cache.h" |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 144 | #include "net/ftp/ftp_network_layer.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 145 | #include "net/url_request/ftp_protocol_handler.h" |
| 146 | #endif |
| 147 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 148 | #if defined(OS_WIN) |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 149 | #include "base/win/scoped_com_initializer.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 150 | #endif |
| 151 | |
Matt Mueller | 7f60dc4 | 2019-04-26 22:47:10 | [diff] [blame] | 152 | #if defined(OS_MACOSX) |
| 153 | #include "base/mac/mac_util.h" |
| 154 | #endif |
| 155 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 156 | #if BUILDFLAG(ENABLE_REPORTING) |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 157 | #include "net/network_error_logging/network_error_logging_service.h" |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 158 | #include "net/network_error_logging/network_error_logging_test_util.h" |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 159 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 160 | |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 161 | #if defined(USE_NSS_CERTS) |
| 162 | #include "net/cert_net/nss_ocsp.h" |
| 163 | #endif |
| 164 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 165 | using net::test::IsError; |
| 166 | using net::test::IsOk; |
| 167 | |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 168 | using base::ASCIIToUTF16; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 169 | using base::Time; |
halton.huo | e4e4574 | 2014-12-08 07:55:46 | [diff] [blame] | 170 | using std::string; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 171 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 172 | namespace net { |
| 173 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 174 | namespace { |
| 175 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 176 | namespace test_default { |
| 177 | #include "net/http/transport_security_state_static_unittest_default.h" |
xunjieli | 815ad5b | 2017-07-18 15:51:35 | [diff] [blame] | 178 | } |
| 179 | |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 180 | const base::string16 kChrome(ASCIIToUTF16("chrome")); |
| 181 | const base::string16 kSecret(ASCIIToUTF16("secret")); |
| 182 | const base::string16 kUser(ASCIIToUTF16("user")); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 183 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 184 | const base::FilePath::CharType kTestFilePath[] = |
| 185 | FILE_PATH_LITERAL("net/data/url_request_unittest"); |
| 186 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 187 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 188 | !defined(OS_FUCHSIA) |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 189 | // Test file used in most FTP tests. |
| 190 | const char kFtpTestFile[] = "BullRunSpeech.txt"; |
| 191 | #endif |
| 192 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 193 | // Tests load timing information in the case a fresh connection was used, with |
| 194 | // no proxy. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 195 | void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info, |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 196 | int connect_timing_flags) { |
| 197 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 198 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 199 | |
| 200 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 201 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 202 | |
| 203 | EXPECT_LE(load_timing_info.request_start, |
| 204 | load_timing_info.connect_timing.connect_start); |
| 205 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 206 | connect_timing_flags); |
| 207 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 208 | load_timing_info.send_start); |
| 209 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 210 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_start); |
| 211 | EXPECT_LE(load_timing_info.receive_headers_start, |
| 212 | load_timing_info.receive_headers_end); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 213 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 214 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 215 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 216 | } |
| 217 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 218 | // Same as above, but with proxy times. |
| 219 | void TestLoadTimingNotReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 220 | const LoadTimingInfo& load_timing_info, |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 221 | int connect_timing_flags) { |
| 222 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 223 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 224 | |
| 225 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 226 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 227 | |
| 228 | EXPECT_LE(load_timing_info.request_start, |
| 229 | load_timing_info.proxy_resolve_start); |
| 230 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 231 | load_timing_info.proxy_resolve_end); |
| 232 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 233 | load_timing_info.connect_timing.connect_start); |
| 234 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 235 | connect_timing_flags); |
| 236 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 237 | load_timing_info.send_start); |
| 238 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 239 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_start); |
| 240 | EXPECT_LE(load_timing_info.receive_headers_start, |
| 241 | load_timing_info.receive_headers_end); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | // Same as above, but with a reused socket and proxy times. |
| 245 | void TestLoadTimingReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 246 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 247 | EXPECT_TRUE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 248 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 249 | |
| 250 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 251 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 252 | |
| 253 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 254 | |
| 255 | EXPECT_LE(load_timing_info.request_start, |
| 256 | load_timing_info.proxy_resolve_start); |
| 257 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 258 | load_timing_info.proxy_resolve_end); |
| 259 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 260 | load_timing_info.send_start); |
| 261 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 262 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_start); |
| 263 | EXPECT_LE(load_timing_info.receive_headers_start, |
| 264 | load_timing_info.receive_headers_end); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 265 | } |
| 266 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 267 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 268 | // Tests load timing information in the case of a cache hit, when no cache |
| 269 | // validation request was sent over the wire. |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 270 | base::StringPiece TestNetResourceProvider(int key) { |
| 271 | return "header"; |
| 272 | } |
| 273 | |
| 274 | void FillBuffer(char* buffer, size_t len) { |
| 275 | static bool called = false; |
| 276 | if (!called) { |
| 277 | called = true; |
| 278 | int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 279 | srand(seed); |
| 280 | } |
| 281 | |
| 282 | for (size_t i = 0; i < len; i++) { |
| 283 | buffer[i] = static_cast<char>(rand()); |
| 284 | if (!buffer[i]) |
| 285 | buffer[i] = 'g'; |
| 286 | } |
| 287 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 288 | #endif |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 289 | |
Matt Menke | 0743e34 | 2019-06-11 20:48:10 | [diff] [blame] | 290 | CookieList GetAllCookies(URLRequestContext* request_context) { |
| 291 | CookieList cookie_list; |
| 292 | base::RunLoop run_loop; |
| 293 | request_context->cookie_store()->GetAllCookiesAsync( |
| 294 | base::BindLambdaForTesting([&](const CookieList& cookies, |
| 295 | const CookieStatusList& excluded_list) { |
| 296 | cookie_list = cookies; |
| 297 | run_loop.Quit(); |
| 298 | })); |
| 299 | run_loop.Run(); |
| 300 | return cookie_list; |
| 301 | } |
| 302 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 303 | void TestLoadTimingCacheHitNoNetwork( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 304 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 305 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 306 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 307 | |
| 308 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 309 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 310 | |
| 311 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 312 | EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start); |
| 313 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 314 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_start); |
| 315 | EXPECT_LE(load_timing_info.receive_headers_start, |
| 316 | load_timing_info.receive_headers_end); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 317 | |
| 318 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 319 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 320 | } |
| 321 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 322 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 323 | !defined(OS_FUCHSIA) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 324 | // Tests load timing in the case that there is no HTTP response. This can be |
| 325 | // used to test in the case of errors or non-HTTP requests. |
| 326 | void TestLoadTimingNoHttpResponse( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 327 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 328 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 329 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 330 | |
| 331 | // Only the request times should be non-null. |
| 332 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 333 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 334 | |
| 335 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 336 | |
| 337 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 338 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 339 | EXPECT_TRUE(load_timing_info.send_start.is_null()); |
| 340 | EXPECT_TRUE(load_timing_info.send_end.is_null()); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 341 | EXPECT_TRUE(load_timing_info.receive_headers_start.is_null()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 342 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 343 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 344 | #endif |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 345 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 346 | // Job that allows monitoring of its priority. |
| 347 | class PriorityMonitoringURLRequestJob : public URLRequestTestJob { |
| 348 | public: |
| 349 | // The latest priority of the job is always written to |request_priority_|. |
| 350 | PriorityMonitoringURLRequestJob(URLRequest* request, |
| 351 | NetworkDelegate* network_delegate, |
| 352 | RequestPriority* request_priority) |
| 353 | : URLRequestTestJob(request, network_delegate), |
| 354 | request_priority_(request_priority) { |
| 355 | *request_priority_ = DEFAULT_PRIORITY; |
| 356 | } |
| 357 | |
| 358 | void SetPriority(RequestPriority priority) override { |
| 359 | *request_priority_ = priority; |
| 360 | URLRequestTestJob::SetPriority(priority); |
| 361 | } |
| 362 | |
| 363 | private: |
| 364 | RequestPriority* const request_priority_; |
| 365 | }; |
| 366 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 367 | // Do a case-insensitive search through |haystack| for |needle|. |
| 368 | bool ContainsString(const std::string& haystack, const char* needle) { |
brettw | a2027fb | 2015-07-14 02:24:50 | [diff] [blame] | 369 | std::string::const_iterator it = std::search( |
| 370 | haystack.begin(), haystack.end(), needle, needle + strlen(needle), |
| 371 | base::CaseInsensitiveCompareASCII<char>()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 372 | return it != haystack.end(); |
| 373 | } |
| 374 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 375 | std::unique_ptr<UploadDataStream> CreateSimpleUploadData(const char* data) { |
| 376 | std::unique_ptr<UploadElementReader> reader( |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 377 | new UploadBytesElementReader(data, strlen(data))); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 378 | return ElementsUploadDataStream::CreateWithReader(std::move(reader), 0); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 379 | } |
| 380 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 381 | // Verify that the SSLInfo of a successful SSL connection has valid values. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 382 | void CheckSSLInfo(const SSLInfo& ssl_info) { |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 383 | // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated. |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 384 | uint16_t cipher_suite = |
| 385 | SSLConnectionStatusToCipherSuite(ssl_info.connection_status); |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 386 | EXPECT_NE(0U, cipher_suite); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 387 | } |
| 388 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 389 | void CheckFullRequestHeaders(const HttpRequestHeaders& headers, |
| 390 | const GURL& host_url) { |
| 391 | std::string sent_value; |
| 392 | |
| 393 | EXPECT_TRUE(headers.GetHeader("Host", &sent_value)); |
| 394 | EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value); |
| 395 | |
| 396 | EXPECT_TRUE(headers.GetHeader("Connection", &sent_value)); |
| 397 | EXPECT_EQ("keep-alive", sent_value); |
| 398 | } |
| 399 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 400 | // A network delegate that allows the user to choose a subset of request stages |
| 401 | // to block in. When blocking, the delegate can do one of the following: |
| 402 | // * synchronously return a pre-specified error code, or |
| 403 | // * asynchronously return that value via an automatically called callback, |
| 404 | // or |
| 405 | // * block and wait for the user to do a callback. |
| 406 | // Additionally, the user may also specify a redirect URL -- then each request |
| 407 | // with the current URL different from the redirect target will be redirected |
| 408 | // to that target, in the on-before-URL-request stage, independent of whether |
| 409 | // the delegate blocks in ON_BEFORE_URL_REQUEST or not. |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 410 | class BlockingNetworkDelegate : public TestNetworkDelegate { |
| 411 | public: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 412 | // Stages in which the delegate can block. |
| 413 | enum Stage { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 414 | NOT_BLOCKED = 0, |
| 415 | ON_BEFORE_URL_REQUEST = 1 << 0, |
| 416 | ON_BEFORE_SEND_HEADERS = 1 << 1, |
| 417 | ON_HEADERS_RECEIVED = 1 << 2, |
| 418 | ON_AUTH_REQUIRED = 1 << 3 |
| 419 | }; |
| 420 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 421 | // Behavior during blocked stages. During other stages, just |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 422 | // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 423 | enum BlockMode { |
| 424 | SYNCHRONOUS, // No callback, returns specified return values. |
| 425 | AUTO_CALLBACK, // |this| posts a task to run the callback using the |
| 426 | // specified return codes. |
| 427 | USER_CALLBACK, // User takes care of doing a callback. |retval_| and |
| 428 | // |auth_retval_| are ignored. In every blocking stage the |
| 429 | // message loop is quit. |
| 430 | }; |
| 431 | |
| 432 | // Creates a delegate which does not block at all. |
| 433 | explicit BlockingNetworkDelegate(BlockMode block_mode); |
| 434 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 435 | // Runs the message loop until the delegate blocks. |
| 436 | void RunUntilBlocked(); |
| 437 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 438 | // For users to trigger a callback returning |response|. |
| 439 | // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks. |
| 440 | // Only call if |block_mode_| == USER_CALLBACK. |
| 441 | void DoCallback(int response); |
| 442 | void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response); |
| 443 | |
| 444 | // Setters. |
| 445 | void set_retval(int retval) { |
| 446 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 447 | ASSERT_NE(ERR_IO_PENDING, retval); |
| 448 | ASSERT_NE(OK, retval); |
| 449 | retval_ = retval; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 450 | } |
| 451 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 452 | // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then |
| 453 | // |auth_credentials_| will be passed with the response. |
| 454 | void set_auth_retval(AuthRequiredResponse auth_retval) { |
| 455 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 456 | ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval); |
| 457 | auth_retval_ = auth_retval; |
| 458 | } |
| 459 | void set_auth_credentials(const AuthCredentials& auth_credentials) { |
| 460 | auth_credentials_ = auth_credentials; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 461 | } |
| 462 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 463 | void set_redirect_url(const GURL& url) { |
| 464 | redirect_url_ = url; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 465 | } |
| 466 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 467 | void set_block_on(int block_on) { |
| 468 | block_on_ = block_on; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 469 | } |
| 470 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 471 | // Allows the user to check in which state did we block. |
| 472 | Stage stage_blocked_for_callback() const { |
| 473 | EXPECT_EQ(USER_CALLBACK, block_mode_); |
| 474 | return stage_blocked_for_callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | private: |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 478 | void OnBlocked(); |
| 479 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 480 | void RunCallback(int response, CompletionOnceCallback callback); |
| 481 | void RunAuthCallback(AuthRequiredResponse response, AuthCallback callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 482 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 483 | // TestNetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 484 | int OnBeforeURLRequest(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 485 | CompletionOnceCallback callback, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 486 | GURL* new_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 487 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 488 | int OnBeforeStartTransaction(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 489 | CompletionOnceCallback callback, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 490 | HttpRequestHeaders* headers) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 491 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 492 | int OnHeadersReceived( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 493 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 494 | CompletionOnceCallback callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 495 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 496 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 497 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 498 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 499 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 500 | URLRequest* request, |
| 501 | const AuthChallengeInfo& auth_info, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 502 | AuthCallback callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 503 | AuthCredentials* credentials) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 504 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 505 | // Resets the callbacks and |stage_blocked_for_callback_|. |
| 506 | void Reset(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 507 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 508 | // Checks whether we should block in |stage|. If yes, returns an error code |
| 509 | // and optionally sets up callback based on |block_mode_|. If no, returns OK. |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 510 | int MaybeBlockStage(Stage stage, CompletionOnceCallback callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 511 | |
| 512 | // Configuration parameters, can be adjusted by public methods: |
| 513 | const BlockMode block_mode_; |
| 514 | |
| 515 | // Values returned on blocking stages when mode is SYNCHRONOUS or |
| 516 | // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING. |
| 517 | int retval_; // To be returned in non-auth stages. |
| 518 | AuthRequiredResponse auth_retval_; |
| 519 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 520 | GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 521 | int block_on_; // Bit mask: in which stages to block. |
| 522 | |
| 523 | // |auth_credentials_| will be copied to |*target_auth_credential_| on |
| 524 | // callback. |
| 525 | AuthCredentials auth_credentials_; |
| 526 | AuthCredentials* target_auth_credentials_; |
| 527 | |
| 528 | // Internal variables, not set by not the user: |
| 529 | // Last blocked stage waiting for user callback (unused if |block_mode_| != |
| 530 | // USER_CALLBACK). |
| 531 | Stage stage_blocked_for_callback_; |
| 532 | |
| 533 | // Callback objects stored during blocking stages. |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 534 | CompletionOnceCallback callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 535 | AuthCallback auth_callback_; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 536 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 537 | // Closure to run to exit RunUntilBlocked(). |
| 538 | base::OnceClosure on_blocked_; |
| 539 | |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 540 | base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_{this}; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 541 | |
| 542 | DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 543 | }; |
| 544 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 545 | BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode) |
| 546 | : block_mode_(block_mode), |
| 547 | retval_(OK), |
| 548 | auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION), |
| 549 | block_on_(0), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 550 | target_auth_credentials_(nullptr), |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 551 | stage_blocked_for_callback_(NOT_BLOCKED) {} |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 552 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 553 | void BlockingNetworkDelegate::RunUntilBlocked() { |
| 554 | base::RunLoop run_loop; |
| 555 | on_blocked_ = run_loop.QuitClosure(); |
| 556 | run_loop.Run(); |
| 557 | } |
| 558 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 559 | void BlockingNetworkDelegate::DoCallback(int response) { |
| 560 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 561 | ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 562 | ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 563 | CompletionOnceCallback callback = std::move(callback_); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 564 | Reset(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 565 | |
| 566 | // |callback| may trigger completion of a request, so post it as a task, so |
| 567 | // it will run under a subsequent TestDelegate::RunUntilComplete() loop. |
| 568 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 569 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::RunCallback, |
| 570 | weak_factory_.GetWeakPtr(), response, |
| 571 | std::move(callback))); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | void BlockingNetworkDelegate::DoAuthCallback( |
| 575 | NetworkDelegate::AuthRequiredResponse response) { |
| 576 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 577 | ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 578 | AuthCallback auth_callback = std::move(auth_callback_); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 579 | Reset(); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 580 | RunAuthCallback(response, std::move(auth_callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 581 | } |
| 582 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 583 | void BlockingNetworkDelegate::OnBlocked() { |
| 584 | // If this fails due to |on_blocked_| being null then OnBlocked() was run by |
| 585 | // a RunLoop other than RunUntilBlocked(), indicating a bug in the calling |
| 586 | // test. |
| 587 | std::move(on_blocked_).Run(); |
| 588 | } |
| 589 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 590 | void BlockingNetworkDelegate::RunCallback(int response, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 591 | CompletionOnceCallback callback) { |
| 592 | std::move(callback).Run(response); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 596 | AuthCallback callback) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 597 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 598 | ASSERT_TRUE(target_auth_credentials_ != nullptr); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 599 | *target_auth_credentials_ = auth_credentials_; |
| 600 | } |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 601 | std::move(callback).Run(response); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 602 | } |
| 603 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 604 | int BlockingNetworkDelegate::OnBeforeURLRequest(URLRequest* request, |
| 605 | CompletionOnceCallback callback, |
| 606 | GURL* new_url) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 607 | if (redirect_url_ == request->url()) |
| 608 | return OK; // We've already seen this request and redirected elsewhere. |
| 609 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 610 | // TestNetworkDelegate always completes synchronously. |
| 611 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeURLRequest( |
| 612 | request, base::NullCallback(), new_url)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 613 | |
| 614 | if (!redirect_url_.is_empty()) |
| 615 | *new_url = redirect_url_; |
| 616 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 617 | return MaybeBlockStage(ON_BEFORE_URL_REQUEST, std::move(callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 618 | } |
| 619 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 620 | int BlockingNetworkDelegate::OnBeforeStartTransaction( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 621 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 622 | CompletionOnceCallback callback, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 623 | HttpRequestHeaders* headers) { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 624 | // TestNetworkDelegate always completes synchronously. |
| 625 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeStartTransaction( |
| 626 | request, base::NullCallback(), headers)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 627 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 628 | return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, std::move(callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | int BlockingNetworkDelegate::OnHeadersReceived( |
| 632 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 633 | CompletionOnceCallback callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 634 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 635 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
| 636 | GURL* allowed_unsafe_redirect_url) { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 637 | // TestNetworkDelegate always completes synchronously. |
| 638 | CHECK_NE(ERR_IO_PENDING, |
| 639 | TestNetworkDelegate::OnHeadersReceived( |
| 640 | request, base::NullCallback(), original_response_headers, |
| 641 | override_response_headers, allowed_unsafe_redirect_url)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 642 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 643 | return MaybeBlockStage(ON_HEADERS_RECEIVED, std::move(callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired( |
| 647 | URLRequest* request, |
| 648 | const AuthChallengeInfo& auth_info, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 649 | AuthCallback callback, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 650 | AuthCredentials* credentials) { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 651 | // TestNetworkDelegate always completes synchronously. |
| 652 | CHECK_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, |
| 653 | TestNetworkDelegate::OnAuthRequired( |
| 654 | request, auth_info, base::NullCallback(), credentials)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 655 | // Check that the user has provided callback for the previous blocked stage. |
| 656 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 657 | |
| 658 | if ((block_on_ & ON_AUTH_REQUIRED) == 0) { |
| 659 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 660 | } |
| 661 | |
| 662 | target_auth_credentials_ = credentials; |
| 663 | |
| 664 | switch (block_mode_) { |
| 665 | case SYNCHRONOUS: |
| 666 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) |
| 667 | *target_auth_credentials_ = auth_credentials_; |
| 668 | return auth_retval_; |
| 669 | |
| 670 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 671 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 672 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::RunAuthCallback, |
| 673 | weak_factory_.GetWeakPtr(), auth_retval_, |
| 674 | std::move(callback))); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 675 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 676 | |
| 677 | case USER_CALLBACK: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 678 | auth_callback_ = std::move(callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 679 | stage_blocked_for_callback_ = ON_AUTH_REQUIRED; |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 680 | // We may reach here via a callback prior to RunUntilBlocked(), so post |
| 681 | // a task to fetch and run the |on_blocked_| closure. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 682 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 683 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::OnBlocked, |
| 684 | weak_factory_.GetWeakPtr())); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 685 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 686 | } |
| 687 | NOTREACHED(); |
| 688 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value. |
| 689 | } |
| 690 | |
| 691 | void BlockingNetworkDelegate::Reset() { |
| 692 | EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 693 | stage_blocked_for_callback_ = NOT_BLOCKED; |
| 694 | callback_.Reset(); |
| 695 | auth_callback_.Reset(); |
| 696 | } |
| 697 | |
| 698 | int BlockingNetworkDelegate::MaybeBlockStage( |
| 699 | BlockingNetworkDelegate::Stage stage, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 700 | CompletionOnceCallback callback) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 701 | // Check that the user has provided callback for the previous blocked stage. |
| 702 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 703 | |
| 704 | if ((block_on_ & stage) == 0) { |
| 705 | return OK; |
| 706 | } |
| 707 | |
| 708 | switch (block_mode_) { |
| 709 | case SYNCHRONOUS: |
| 710 | EXPECT_NE(OK, retval_); |
| 711 | return retval_; |
| 712 | |
| 713 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 714 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 715 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::RunCallback, |
| 716 | weak_factory_.GetWeakPtr(), retval_, |
| 717 | std::move(callback))); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 718 | return ERR_IO_PENDING; |
| 719 | |
| 720 | case USER_CALLBACK: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 721 | callback_ = std::move(callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 722 | stage_blocked_for_callback_ = stage; |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 723 | // We may reach here via a callback prior to RunUntilBlocked(), so post |
| 724 | // a task to fetch and run the |on_blocked_| closure. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 725 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 726 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::OnBlocked, |
| 727 | weak_factory_.GetWeakPtr())); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 728 | return ERR_IO_PENDING; |
| 729 | } |
| 730 | NOTREACHED(); |
| 731 | return 0; |
| 732 | } |
| 733 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 734 | class TestURLRequestContextWithProxy : public TestURLRequestContext { |
| 735 | public: |
| 736 | // Does not own |delegate|. |
| 737 | TestURLRequestContextWithProxy(const std::string& proxy, |
Matt Menke | 39e6de6 | 2019-06-27 20:11:39 | [diff] [blame] | 738 | NetworkDelegate* delegate, |
| 739 | bool delay_initialization = false) |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 740 | : TestURLRequestContext(true) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 741 | context_storage_.set_proxy_resolution_service( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 742 | ProxyResolutionService::CreateFixed(proxy, |
| 743 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 744 | set_network_delegate(delegate); |
Matt Menke | 39e6de6 | 2019-06-27 20:11:39 | [diff] [blame] | 745 | if (!delay_initialization) |
| 746 | Init(); |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 747 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 748 | ~TestURLRequestContextWithProxy() override = default; |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 749 | }; |
| 750 | |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 751 | // A mock ReportSenderInterface that just remembers the latest report |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 752 | // URI and report to be sent. |
| 753 | class MockCertificateReportSender |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 754 | : public TransportSecurityState::ReportSenderInterface { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 755 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 756 | MockCertificateReportSender() = default; |
| 757 | ~MockCertificateReportSender() override = default; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 758 | |
meacer | 5d4dc5a | 2017-04-27 20:37:48 | [diff] [blame] | 759 | void Send(const GURL& report_uri, |
| 760 | base::StringPiece content_type, |
| 761 | base::StringPiece report, |
| 762 | const base::Callback<void()>& success_callback, |
| 763 | const base::Callback<void(const GURL&, int, int)>& error_callback) |
| 764 | override { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 765 | latest_report_uri_ = report_uri; |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 766 | report.CopyToString(&latest_report_); |
| 767 | content_type.CopyToString(&latest_content_type_); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 768 | } |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 769 | const GURL& latest_report_uri() { return latest_report_uri_; } |
| 770 | const std::string& latest_report() { return latest_report_; } |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 771 | const std::string& latest_content_type() { return latest_content_type_; } |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 772 | |
| 773 | private: |
| 774 | GURL latest_report_uri_; |
| 775 | std::string latest_report_; |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 776 | std::string latest_content_type_; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 777 | }; |
| 778 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 779 | // OCSPErrorTestDelegate caches the SSLInfo passed to OnSSLCertificateError. |
| 780 | // This is needed because after the certificate failure, the URLRequest will |
| 781 | // retry the connection, and return a partial SSLInfo with a cached cert status. |
| 782 | // The partial SSLInfo does not have the OCSP information filled out. |
| 783 | class OCSPErrorTestDelegate : public TestDelegate { |
| 784 | public: |
| 785 | void OnSSLCertificateError(URLRequest* request, |
Emily Stark | 79fba584 | 2019-04-25 04:59:36 | [diff] [blame] | 786 | int net_error, |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 787 | const SSLInfo& ssl_info, |
| 788 | bool fatal) override { |
| 789 | ssl_info_ = ssl_info; |
| 790 | on_ssl_certificate_error_called_ = true; |
Emily Stark | 79fba584 | 2019-04-25 04:59:36 | [diff] [blame] | 791 | TestDelegate::OnSSLCertificateError(request, net_error, ssl_info, fatal); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | bool on_ssl_certificate_error_called() { |
| 795 | return on_ssl_certificate_error_called_; |
| 796 | } |
| 797 | |
| 798 | SSLInfo ssl_info() { return ssl_info_; } |
| 799 | |
| 800 | private: |
| 801 | bool on_ssl_certificate_error_called_ = false; |
| 802 | SSLInfo ssl_info_; |
| 803 | }; |
| 804 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 805 | } // namespace |
| 806 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 807 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame^] | 808 | class URLRequestTest : public PlatformTest, public WithTaskEnvironment { |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 809 | public: |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 810 | URLRequestTest() |
| 811 | : default_context_(std::make_unique<TestURLRequestContext>(true)) { |
| 812 | default_context_->set_network_delegate(&default_network_delegate_); |
| 813 | default_context_->set_net_log(&net_log_); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 814 | job_factory_impl_ = new URLRequestJobFactoryImpl(); |
| 815 | job_factory_.reset(job_factory_impl_); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 816 | } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 817 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 818 | ~URLRequestTest() override { |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 819 | // URLRequestJobs may post clean-up tasks on destruction. |
| 820 | base::RunLoop().RunUntilIdle(); |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 821 | |
| 822 | SetTransportSecurityStateSourceForTesting(nullptr); |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 823 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 824 | |
dcheng | 2339883c | 2014-12-23 00:23:05 | [diff] [blame] | 825 | void SetUp() override { |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 826 | SetUpFactory(); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 827 | default_context_->set_job_factory(job_factory_.get()); |
| 828 | default_context_->Init(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 829 | PlatformTest::SetUp(); |
| 830 | } |
| 831 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 832 | void TearDown() override { default_context_.reset(); } |
| 833 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 834 | virtual void SetUpFactory() { |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 835 | job_factory_impl_->SetProtocolHandler( |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 836 | "data", std::make_unique<DataProtocolHandler>()); |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 837 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 838 | job_factory_impl_->SetProtocolHandler( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 839 | "file", std::make_unique<FileProtocolHandler>( |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 840 | base::ThreadTaskRunnerHandle::Get())); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 841 | #endif |
| 842 | } |
| 843 | |
| 844 | TestNetworkDelegate* default_network_delegate() { |
| 845 | return &default_network_delegate_; |
| 846 | } |
| 847 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 848 | TestURLRequestContext& default_context() const { return *default_context_; } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 849 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 850 | // Adds the TestJobInterceptor to the default context. |
| 851 | TestJobInterceptor* AddTestInterceptor() { |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 852 | TestJobInterceptor* protocol_handler_ = new TestJobInterceptor(); |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 853 | job_factory_impl_->SetProtocolHandler("http", nullptr); |
| 854 | job_factory_impl_->SetProtocolHandler("http", |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 855 | base::WrapUnique(protocol_handler_)); |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 856 | return protocol_handler_; |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 857 | } |
| 858 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 859 | // Creates a temp test file and writes |data| to the file. The file will be |
| 860 | // deleted after the test completes. |
| 861 | void CreateTestFile(const char* data, |
| 862 | size_t data_size, |
| 863 | base::FilePath* test_file) { |
| 864 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 865 | // Get an absolute path since |temp_dir| can contain a symbolic link. As of |
| 866 | // now, Mac and Android bots return a path with a symbolic link. |
| 867 | base::FilePath absolute_temp_dir = |
| 868 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
| 869 | |
| 870 | ASSERT_TRUE(base::CreateTemporaryFileInDir(absolute_temp_dir, test_file)); |
| 871 | ASSERT_EQ(static_cast<int>(data_size), |
| 872 | base::WriteFile(*test_file, data, data_size)); |
| 873 | } |
| 874 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 875 | protected: |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 876 | TestNetLog net_log_; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 877 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 878 | URLRequestJobFactoryImpl* job_factory_impl_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 879 | std::unique_ptr<URLRequestJobFactory> job_factory_; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 880 | std::unique_ptr<TestURLRequestContext> default_context_; |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 881 | base::ScopedTempDir temp_dir_; |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 882 | }; |
| 883 | |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 884 | // This NetworkDelegate is picky about what files are accessible. Only |
Ryan Sleevi | a9d6aa6 | 2019-07-26 13:32:18 | [diff] [blame] | 885 | // allowlisted files are allowed. |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 886 | class CookieBlockingNetworkDelegate : public TestNetworkDelegate { |
| 887 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 888 | CookieBlockingNetworkDelegate() = default; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 889 | |
Ryan Sleevi | a9d6aa6 | 2019-07-26 13:32:18 | [diff] [blame] | 890 | // Adds |directory| to the access allow list. |
| 891 | void AddToAllowlist(const base::FilePath& directory) { |
| 892 | allowlist_.insert(directory); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | private: |
Ryan Sleevi | a9d6aa6 | 2019-07-26 13:32:18 | [diff] [blame] | 896 | // Returns true if |path| matches the allow list. |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 897 | bool OnCanAccessFileInternal(const base::FilePath& path) const { |
Ryan Sleevi | a9d6aa6 | 2019-07-26 13:32:18 | [diff] [blame] | 898 | for (const auto& directory : allowlist_) { |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 899 | if (directory == path || directory.IsParent(path)) |
| 900 | return true; |
| 901 | } |
| 902 | return false; |
| 903 | } |
| 904 | |
| 905 | // Returns true only if both |original_path| and |absolute_path| match the |
Ryan Sleevi | a9d6aa6 | 2019-07-26 13:32:18 | [diff] [blame] | 906 | // allow list. |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 907 | bool OnCanAccessFile(const URLRequest& request, |
| 908 | const base::FilePath& original_path, |
| 909 | const base::FilePath& absolute_path) const override { |
| 910 | return (OnCanAccessFileInternal(original_path) && |
| 911 | OnCanAccessFileInternal(absolute_path)); |
| 912 | } |
| 913 | |
Ryan Sleevi | a9d6aa6 | 2019-07-26 13:32:18 | [diff] [blame] | 914 | std::set<base::FilePath> allowlist_; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 915 | |
| 916 | DISALLOW_COPY_AND_ASSIGN(CookieBlockingNetworkDelegate); |
| 917 | }; |
| 918 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 919 | TEST_F(URLRequestTest, AboutBlankTest) { |
| 920 | TestDelegate d; |
| 921 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 922 | std::unique_ptr<URLRequest> r( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 923 | default_context().CreateRequest(GURL("about:blank"), DEFAULT_PRIORITY, |
| 924 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 925 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 926 | r->Start(); |
| 927 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 928 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 929 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 930 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 931 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 932 | EXPECT_FALSE(d.received_data_before_response()); |
| 933 | EXPECT_EQ(d.bytes_received(), 0); |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 934 | EXPECT_TRUE(r->GetResponseRemoteEndpoint().address().empty()); |
| 935 | EXPECT_EQ(0, r->GetResponseRemoteEndpoint().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 936 | |
| 937 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 938 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | |
| 942 | TEST_F(URLRequestTest, DataURLImageTest) { |
| 943 | TestDelegate d; |
| 944 | { |
| 945 | // Use our nice little Chrome logo. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 946 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 947 | GURL("data:image/png;base64," |
| 948 | "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUB" |
| 949 | "BjG3w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQ" |
| 950 | "xIEVxitD5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI" |
| 951 | "5MpCVdW0gO7tvNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKz" |
| 952 | "Mx1+fg9bAgK6zHq9cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7Ck" |
| 953 | "IMDxQpF7r/MWq12UctI1dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0" |
| 954 | "WYUqt57yWf2McHTObYPbVD+ZwbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2H" |
| 955 | "QQcm43XwmtoYM6vVKleh0uoWvnzW3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nk" |
| 956 | "DFHF9ZS+uYVjRUasMeHUmyLYtcklTvzWGFZnNOXczThvpKIzjcahSqIzkvDLayDq6" |
| 957 | "D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTbxXX1T5xsV9tm9r4TQwHLiZw/pdDZJ" |
| 958 | "ea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5MrVvMzNaI3+ERHfrFzPKQukrQ" |
| 959 | "GI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gVMd6q5c8GdosynKmSeRuGz" |
| 960 | "pjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq/uxxZKzNiZFGD0wRC" |
| 961 | "3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7FUx1qLnV7MGF40" |
| 962 | "smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufBJbsysjjW4" |
| 963 | "kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJMnlm2O" |
| 964 | "34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfmm" |
| 965 | "oRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV" |
| 966 | "5EnhORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 967 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 968 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 969 | r->Start(); |
| 970 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 971 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 972 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 973 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 974 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 975 | EXPECT_FALSE(d.received_data_before_response()); |
| 976 | EXPECT_EQ(d.bytes_received(), 911); |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 977 | EXPECT_TRUE(r->GetResponseRemoteEndpoint().address().empty()); |
| 978 | EXPECT_EQ(0, r->GetResponseRemoteEndpoint().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 979 | |
| 980 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 981 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 982 | } |
| 983 | } |
| 984 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 985 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 986 | TEST_F(URLRequestTest, FileTest) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 987 | const char kTestFileContent[] = "Hello"; |
| 988 | base::FilePath test_file; |
| 989 | ASSERT_NO_FATAL_FAILURE( |
| 990 | CreateTestFile(kTestFileContent, sizeof(kTestFileContent), &test_file)); |
kraush | 5a64582 | 2016-04-07 18:35:04 | [diff] [blame] | 991 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 992 | GURL test_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 993 | |
| 994 | TestDelegate d; |
| 995 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 996 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 997 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 998 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 999 | r->Start(); |
| 1000 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1001 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1002 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1003 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1004 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1005 | EXPECT_EQ(1, d.response_started_count()); |
| 1006 | EXPECT_FALSE(d.received_data_before_response()); |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1007 | EXPECT_EQ(d.bytes_received(), static_cast<int>(sizeof(kTestFileContent))); |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 1008 | EXPECT_TRUE(r->GetResponseRemoteEndpoint().address().empty()); |
| 1009 | EXPECT_EQ(0, r->GetResponseRemoteEndpoint().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 1010 | |
| 1011 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1012 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1013 | } |
| 1014 | } |
| 1015 | |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1016 | TEST_F(URLRequestTest, FileTestCancel) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1017 | const char kTestFileContent[] = "Hello"; |
| 1018 | base::FilePath test_file; |
| 1019 | ASSERT_NO_FATAL_FAILURE( |
| 1020 | CreateTestFile(kTestFileContent, sizeof(kTestFileContent), &test_file)); |
| 1021 | |
| 1022 | GURL test_url = FilePathToFileURL(test_file); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1023 | |
| 1024 | TestDelegate d; |
| 1025 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1026 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1027 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1028 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1029 | r->Start(); |
| 1030 | EXPECT_TRUE(r->is_pending()); |
| 1031 | r->Cancel(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1032 | } |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 1033 | // Async cancellation should be safe even when URLRequest has been already |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1034 | // destroyed. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1035 | base::RunLoop().RunUntilIdle(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1036 | } |
| 1037 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1038 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 1039 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1040 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1041 | FillBuffer(buffer.get(), buffer_size); |
| 1042 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1043 | base::FilePath test_file; |
| 1044 | ASSERT_NO_FATAL_FAILURE( |
| 1045 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1046 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1047 | |
| 1048 | const size_t first_byte_position = 500; |
| 1049 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 1050 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 1051 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 1052 | buffer.get() + last_byte_position + 1); |
| 1053 | |
| 1054 | TestDelegate d; |
| 1055 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1056 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1057 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1058 | |
| 1059 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1060 | headers.SetHeader( |
| 1061 | HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1062 | HttpByteRange::Bounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1063 | first_byte_position, last_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1064 | r->SetExtraRequestHeaders(headers); |
| 1065 | r->Start(); |
| 1066 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1067 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1068 | d.RunUntilComplete(); |
| 1069 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1070 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1071 | EXPECT_EQ(1, d.response_started_count()); |
| 1072 | EXPECT_FALSE(d.received_data_before_response()); |
| 1073 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 1074 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 1075 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 1076 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 1080 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1081 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1082 | FillBuffer(buffer.get(), buffer_size); |
| 1083 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1084 | base::FilePath test_file; |
| 1085 | ASSERT_NO_FATAL_FAILURE( |
| 1086 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1087 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1088 | |
| 1089 | const size_t first_byte_position = 500; |
| 1090 | const size_t last_byte_position = buffer_size - 1; |
| 1091 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 1092 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 1093 | buffer.get() + last_byte_position + 1); |
| 1094 | |
| 1095 | TestDelegate d; |
| 1096 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1097 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1098 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1099 | |
| 1100 | HttpRequestHeaders headers; |
| 1101 | headers.SetHeader(HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1102 | HttpByteRange::RightUnbounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1103 | first_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1104 | r->SetExtraRequestHeaders(headers); |
| 1105 | r->Start(); |
| 1106 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1107 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1108 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1109 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1110 | EXPECT_EQ(1, d.response_started_count()); |
| 1111 | EXPECT_FALSE(d.received_data_before_response()); |
| 1112 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 1113 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 1114 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 1115 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 1119 | const size_t buffer_size = 400000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1120 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1121 | FillBuffer(buffer.get(), buffer_size); |
| 1122 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1123 | base::FilePath test_file; |
| 1124 | ASSERT_NO_FATAL_FAILURE( |
| 1125 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1126 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1127 | |
| 1128 | TestDelegate d; |
| 1129 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1130 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1131 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1132 | |
| 1133 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1134 | headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1135 | r->SetExtraRequestHeaders(headers); |
| 1136 | r->Start(); |
| 1137 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1138 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1139 | d.RunUntilComplete(); |
| 1140 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1141 | EXPECT_TRUE(d.request_failed()); |
| 1142 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1143 | } |
| 1144 | |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1145 | TEST_F(URLRequestTest, AllowFileURLs) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1146 | std::string test_data("monkey"); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1147 | base::FilePath test_file; |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1148 | ASSERT_NO_FATAL_FAILURE( |
| 1149 | CreateTestFile(test_data.data(), test_data.size(), &test_file)); |
| 1150 | |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1151 | // The directory part of the path returned from CreateTemporaryFileInDir() |
| 1152 | // can be slightly different from |absolute_temp_dir| on Windows. |
| 1153 | // Example: C:\\Users\\CHROME~2 -> C:\\Users\\chrome-bot |
| 1154 | // Hence the test should use the directory name of |test_file|, rather than |
Ryan Sleevi | a9d6aa6 | 2019-07-26 13:32:18 | [diff] [blame] | 1155 | // |absolute_temp_dir|, for allowlisting. |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1156 | base::FilePath real_temp_dir = test_file.DirName(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1157 | GURL test_file_url = FilePathToFileURL(test_file); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1158 | { |
| 1159 | TestDelegate d; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1160 | CookieBlockingNetworkDelegate network_delegate; |
Ryan Sleevi | a9d6aa6 | 2019-07-26 13:32:18 | [diff] [blame] | 1161 | network_delegate.AddToAllowlist(real_temp_dir); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1162 | default_context().set_network_delegate(&network_delegate); |
| 1163 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1164 | test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1165 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1166 | d.RunUntilComplete(); |
Ryan Sleevi | a9d6aa6 | 2019-07-26 13:32:18 | [diff] [blame] | 1167 | // This should be allowed as the file path is allowlisted. |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1168 | EXPECT_FALSE(d.request_failed()); |
| 1169 | EXPECT_EQ(test_data, d.data_received()); |
| 1170 | } |
| 1171 | |
| 1172 | { |
| 1173 | TestDelegate d; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1174 | CookieBlockingNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1175 | default_context().set_network_delegate(&network_delegate); |
| 1176 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1177 | test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1178 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1179 | d.RunUntilComplete(); |
Ryan Sleevi | a9d6aa6 | 2019-07-26 13:32:18 | [diff] [blame] | 1180 | // This should be rejected as the file path is not allowlisted. |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1181 | EXPECT_TRUE(d.request_failed()); |
| 1182 | EXPECT_EQ("", d.data_received()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1183 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1184 | } |
| 1185 | } |
| 1186 | |
Wez | 3553913 | 2018-07-17 11:26:05 | [diff] [blame] | 1187 | #if defined(OS_POSIX) // Because of symbolic links. |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1188 | |
| 1189 | TEST_F(URLRequestTest, SymlinksToFiles) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1190 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1191 | // Get an absolute path since temp_dir can contain a symbolic link. |
| 1192 | base::FilePath absolute_temp_dir = |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1193 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1194 | |
Ryan Sleevi | a9d6aa6 | 2019-07-26 13:32:18 | [diff] [blame] | 1195 | // Create a good directory (will be allowlisted) and a good file. |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1196 | base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); |
| 1197 | ASSERT_TRUE(base::CreateDirectory(good_dir)); |
| 1198 | base::FilePath good_file; |
| 1199 | ASSERT_TRUE(base::CreateTemporaryFileInDir(good_dir, &good_file)); |
| 1200 | std::string good_data("good"); |
| 1201 | base::WriteFile(good_file, good_data.data(), good_data.size()); |
| 1202 | // See the comment in AllowFileURLs() for why this is done. |
| 1203 | base::FilePath real_good_dir = good_file.DirName(); |
| 1204 | |
Ryan Sleevi | a9d6aa6 | 2019-07-26 13:32:18 | [diff] [blame] | 1205 | // Create a bad directory (will not be allowlisted) and a bad file. |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1206 | base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); |
| 1207 | ASSERT_TRUE(base::CreateDirectory(bad_dir)); |
| 1208 | base::FilePath bad_file; |
| 1209 | ASSERT_TRUE(base::CreateTemporaryFileInDir(bad_dir, &bad_file)); |
| 1210 | std::string bad_data("bad"); |
| 1211 | base::WriteFile(bad_file, bad_data.data(), bad_data.size()); |
| 1212 | |
| 1213 | // This symlink will point to the good file. Access to the symlink will be |
| 1214 | // allowed as both the symlink and the destination file are in the same |
| 1215 | // good directory. |
| 1216 | base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); |
| 1217 | ASSERT_TRUE(base::CreateSymbolicLink(good_file, good_symlink)); |
| 1218 | GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1219 | // This symlink will point to the bad file. Even though the symlink is in |
| 1220 | // the good directory, access to the symlink will be rejected since it |
| 1221 | // points to the bad file. |
| 1222 | base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); |
| 1223 | ASSERT_TRUE(base::CreateSymbolicLink(bad_file, bad_symlink)); |
| 1224 | GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1225 | |
| 1226 | CookieBlockingNetworkDelegate network_delegate; |
Ryan Sleevi | a9d6aa6 | 2019-07-26 13:32:18 | [diff] [blame] | 1227 | network_delegate.AddToAllowlist(real_good_dir); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1228 | { |
| 1229 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1230 | default_context().set_network_delegate(&network_delegate); |
| 1231 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1232 | good_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1233 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1234 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1235 | // good_file_url should be allowed. |
| 1236 | EXPECT_FALSE(d.request_failed()); |
| 1237 | EXPECT_EQ(good_data, d.data_received()); |
| 1238 | } |
| 1239 | |
| 1240 | { |
| 1241 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1242 | default_context().set_network_delegate(&network_delegate); |
| 1243 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1244 | bad_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1245 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1246 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1247 | // bad_file_url should be rejected. |
| 1248 | EXPECT_TRUE(d.request_failed()); |
| 1249 | EXPECT_EQ("", d.data_received()); |
| 1250 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
| 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | TEST_F(URLRequestTest, SymlinksToDirs) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1255 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1256 | // Get an absolute path since temp_dir can contain a symbolic link. |
| 1257 | base::FilePath absolute_temp_dir = |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1258 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1259 | |
Ryan Sleevi | a9d6aa6 | 2019-07-26 13:32:18 | [diff] [blame] | 1260 | // Create a good directory (will be allowlisted). |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1261 | base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); |
| 1262 | ASSERT_TRUE(base::CreateDirectory(good_dir)); |
| 1263 | |
Ryan Sleevi | a9d6aa6 | 2019-07-26 13:32:18 | [diff] [blame] | 1264 | // Create a bad directory (will not be allowlisted). |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1265 | base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); |
| 1266 | ASSERT_TRUE(base::CreateDirectory(bad_dir)); |
| 1267 | |
| 1268 | // This symlink will point to the good directory. Access to the symlink |
| 1269 | // will be allowed as the symlink is in the good dir that'll be white |
| 1270 | // listed. |
| 1271 | base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); |
| 1272 | ASSERT_TRUE(base::CreateSymbolicLink(good_dir, good_symlink)); |
| 1273 | GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1274 | // This symlink will point to the bad directory. Even though the symlink is |
| 1275 | // in the good directory, access to the symlink will be rejected since it |
| 1276 | // points to the bad directory. |
| 1277 | base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); |
| 1278 | ASSERT_TRUE(base::CreateSymbolicLink(bad_dir, bad_symlink)); |
| 1279 | GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1280 | |
| 1281 | CookieBlockingNetworkDelegate network_delegate; |
Ryan Sleevi | a9d6aa6 | 2019-07-26 13:32:18 | [diff] [blame] | 1282 | network_delegate.AddToAllowlist(good_dir); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1283 | { |
| 1284 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1285 | default_context().set_network_delegate(&network_delegate); |
| 1286 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1287 | good_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1288 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1289 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1290 | // good_file_url should be allowed. |
| 1291 | EXPECT_FALSE(d.request_failed()); |
| 1292 | ASSERT_NE(d.data_received().find("good_symlink"), std::string::npos); |
| 1293 | } |
| 1294 | |
| 1295 | { |
| 1296 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1297 | default_context().set_network_delegate(&network_delegate); |
| 1298 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1299 | bad_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1300 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1301 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1302 | // bad_file_url should be rejected. |
| 1303 | EXPECT_TRUE(d.request_failed()); |
| 1304 | EXPECT_EQ("", d.data_received()); |
| 1305 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
| 1306 | } |
| 1307 | } |
| 1308 | |
Wez | 3553913 | 2018-07-17 11:26:05 | [diff] [blame] | 1309 | #endif // defined(OS_POSIX) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1310 | |
| 1311 | TEST_F(URLRequestTest, FileDirCancelTest) { |
| 1312 | // Put in mock resource provider. |
| 1313 | NetModule::SetResourceProvider(TestNetResourceProvider); |
| 1314 | |
| 1315 | TestDelegate d; |
| 1316 | { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1317 | base::FilePath file_path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1318 | base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1319 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 1320 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
| 1321 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1322 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1323 | FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d, |
| 1324 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1325 | req->Start(); |
| 1326 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1327 | |
| 1328 | d.set_cancel_in_received_data_pending(true); |
| 1329 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1330 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | // Take out mock resource provider. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1334 | NetModule::SetResourceProvider(nullptr); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1335 | } |
| 1336 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1337 | TEST_F(URLRequestTest, FileDirOutputSanity) { |
| 1338 | // Verify the general sanity of the the output of the file: |
| 1339 | // directory lister by checking for the output of a known existing |
| 1340 | // file. |
| 1341 | const char sentinel_name[] = "filedir-sentinel"; |
| 1342 | |
| 1343 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1344 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1345 | path = path.Append(kTestFilePath); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1346 | |
| 1347 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1348 | std::unique_ptr<URLRequest> req( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1349 | default_context().CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 1350 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1351 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1352 | d.RunUntilComplete(); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1353 | |
| 1354 | // Generate entry for the sentinel file. |
| 1355 | base::FilePath sentinel_path = path.AppendASCII(sentinel_name); |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 1356 | base::File::Info info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 1357 | EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1358 | EXPECT_GT(info.size, 0); |
| 1359 | std::string sentinel_output = GetDirectoryListingEntry( |
| 1360 | base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1361 | std::string(sentinel_name), false /* is_dir */, info.size, |
| 1362 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1363 | info.last_modified); |
| 1364 | |
| 1365 | ASSERT_LT(0, d.bytes_received()); |
| 1366 | ASSERT_FALSE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1367 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1368 | // Check for the entry generated for the "sentinel" file. |
| 1369 | const std::string& data = d.data_received(); |
| 1370 | ASSERT_NE(data.find(sentinel_output), std::string::npos); |
| 1371 | } |
| 1372 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1373 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 1374 | // There is an implicit redirect when loading a file path that matches a |
| 1375 | // directory and does not end with a slash. Ensure that following such |
| 1376 | // redirects does not crash. See http://crbug.com/18686. |
| 1377 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1378 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1379 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1380 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1381 | |
| 1382 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1383 | std::unique_ptr<URLRequest> req( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1384 | default_context().CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 1385 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1386 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1387 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1388 | |
| 1389 | ASSERT_EQ(1, d.received_redirect_count()); |
| 1390 | ASSERT_LT(0, d.bytes_received()); |
| 1391 | ASSERT_FALSE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1392 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | #if defined(OS_WIN) |
| 1396 | // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
| 1397 | TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
| 1398 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1399 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1400 | GURL("file:///"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1401 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1402 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1403 | |
| 1404 | ASSERT_EQ(1, d.received_redirect_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1405 | EXPECT_NE(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1406 | } |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1407 | #endif // defined(OS_WIN) |
| 1408 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 1409 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1410 | |
| 1411 | TEST_F(URLRequestTest, InvalidUrlTest) { |
| 1412 | TestDelegate d; |
| 1413 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1414 | std::unique_ptr<URLRequest> r( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1415 | default_context().CreateRequest(GURL("invalid url"), DEFAULT_PRIORITY, |
| 1416 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1417 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1418 | r->Start(); |
| 1419 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1420 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1421 | d.RunUntilComplete(); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1422 | EXPECT_TRUE(d.request_failed()); |
| 1423 | } |
| 1424 | } |
| 1425 | |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1426 | TEST_F(URLRequestTest, InvalidReferrerTest) { |
| 1427 | TestURLRequestContext context; |
| 1428 | TestNetworkDelegate network_delegate; |
| 1429 | network_delegate.set_cancel_request_with_policy_violating_referrer(true); |
| 1430 | context.set_network_delegate(&network_delegate); |
| 1431 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1432 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1433 | context.CreateRequest(GURL("http://localhost/"), DEFAULT_PRIORITY, &d, |
| 1434 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1435 | req->SetReferrer("https://somewhere.com/"); |
| 1436 | |
| 1437 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1438 | d.RunUntilComplete(); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1439 | EXPECT_TRUE(d.request_failed()); |
| 1440 | } |
| 1441 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1442 | #if defined(OS_WIN) |
| 1443 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
| 1444 | base::FilePath app_path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1445 | base::PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1446 | app_path = app_path.Append(kTestFilePath); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1447 | app_path = app_path.AppendASCII("with-headers.html"); |
| 1448 | |
jdoerrie | 6312bf6 | 2019-02-01 22:03:42 | [diff] [blame] | 1449 | base::string16 lnk_path = app_path.value() + FILE_PATH_LITERAL(".lnk"); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1450 | |
| 1451 | base::win::ScopedCOMInitializer com_initializer; |
| 1452 | |
| 1453 | // Temporarily create a shortcut for test |
| 1454 | { |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 1455 | Microsoft::WRL::ComPtr<IShellLink> shell; |
robliao | eb9bfd64 | 2017-05-18 17:35:16 | [diff] [blame] | 1456 | ASSERT_TRUE(SUCCEEDED(::CoCreateInstance( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1457 | CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shell)))); |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 1458 | Microsoft::WRL::ComPtr<IPersistFile> persist; |
robliao | 310fa98b | 2017-05-11 17:14:00 | [diff] [blame] | 1459 | ASSERT_TRUE(SUCCEEDED(shell.CopyTo(persist.GetAddressOf()))); |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1460 | EXPECT_TRUE(SUCCEEDED(shell->SetPath(base::as_wcstr(app_path.value())))); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1461 | EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1462 | EXPECT_TRUE(SUCCEEDED(persist->Save(base::as_wcstr(lnk_path), TRUE))); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | TestDelegate d; |
| 1466 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1467 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1468 | FilePathToFileURL(base::FilePath(lnk_path)), DEFAULT_PRIORITY, &d, |
| 1469 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1470 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1471 | r->Start(); |
| 1472 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1473 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1474 | d.RunUntilComplete(); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1475 | |
| 1476 | WIN32_FILE_ATTRIBUTE_DATA data; |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1477 | GetFileAttributesEx(base::as_wcstr(app_path.value()), GetFileExInfoStandard, |
jdoerrie | 6312bf6 | 2019-02-01 22:03:42 | [diff] [blame] | 1478 | &data); |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1479 | HANDLE file = CreateFile(base::as_wcstr(app_path.value()), GENERIC_READ, |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1480 | FILE_SHARE_READ, nullptr, OPEN_EXISTING, |
| 1481 | FILE_ATTRIBUTE_NORMAL, nullptr); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1482 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1483 | std::unique_ptr<char[]> buffer(new char[data.nFileSizeLow]); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1484 | DWORD read_size; |
| 1485 | BOOL result; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1486 | result = |
| 1487 | ReadFile(file, buffer.get(), data.nFileSizeLow, &read_size, nullptr); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1488 | std::string content(buffer.get(), read_size); |
| 1489 | CloseHandle(file); |
| 1490 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1491 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1492 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1493 | EXPECT_EQ(content, d.data_received()); |
| 1494 | } |
| 1495 | |
| 1496 | // Clean the shortcut |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1497 | DeleteFile(base::as_wcstr(lnk_path)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1498 | } |
| 1499 | #endif // defined(OS_WIN) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1500 | |
| 1501 | // Custom URLRequestJobs for use with interceptor tests |
| 1502 | class RestartTestJob : public URLRequestTestJob { |
| 1503 | public: |
| 1504 | RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1505 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1506 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1507 | void StartAsync() override { this->NotifyRestartRequired(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1508 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1509 | ~RestartTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1510 | }; |
| 1511 | |
| 1512 | class CancelTestJob : public URLRequestTestJob { |
| 1513 | public: |
| 1514 | explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1515 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1516 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1517 | void StartAsync() override { request_->Cancel(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1518 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1519 | ~CancelTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1520 | }; |
| 1521 | |
| 1522 | class CancelThenRestartTestJob : public URLRequestTestJob { |
| 1523 | public: |
| 1524 | explicit CancelThenRestartTestJob(URLRequest* request, |
| 1525 | NetworkDelegate* network_delegate) |
| 1526 | : URLRequestTestJob(request, network_delegate, true) { |
| 1527 | } |
| 1528 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1529 | void StartAsync() override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1530 | request_->Cancel(); |
| 1531 | this->NotifyRestartRequired(); |
| 1532 | } |
| 1533 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1534 | ~CancelThenRestartTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1535 | }; |
| 1536 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1537 | // An Interceptor for use with interceptor tests. |
| 1538 | class MockURLRequestInterceptor : public URLRequestInterceptor { |
| 1539 | public: |
| 1540 | // Static getters for canned response header and data strings. |
| 1541 | static std::string ok_data() { |
| 1542 | return URLRequestTestJob::test_data_1(); |
| 1543 | } |
| 1544 | |
| 1545 | static std::string ok_headers() { |
| 1546 | return URLRequestTestJob::test_headers(); |
| 1547 | } |
| 1548 | |
| 1549 | static std::string redirect_data() { |
| 1550 | return std::string(); |
| 1551 | } |
| 1552 | |
| 1553 | static std::string redirect_headers() { |
| 1554 | return URLRequestTestJob::test_redirect_headers(); |
| 1555 | } |
| 1556 | |
| 1557 | static std::string error_data() { |
| 1558 | return std::string("ohhh nooooo mr. bill!"); |
| 1559 | } |
| 1560 | |
| 1561 | static std::string error_headers() { |
| 1562 | return URLRequestTestJob::test_error_headers(); |
| 1563 | } |
| 1564 | |
| 1565 | MockURLRequestInterceptor() |
| 1566 | : intercept_main_request_(false), restart_main_request_(false), |
| 1567 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 1568 | simulate_main_network_error_(false), |
| 1569 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 1570 | intercept_final_response_(false), cancel_final_request_(false), |
| 1571 | use_url_request_http_job_(false), |
| 1572 | did_intercept_main_(false), did_restart_main_(false), |
| 1573 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 1574 | did_simulate_error_main_(false), |
| 1575 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 1576 | did_intercept_final_(false), did_cancel_final_(false) { |
| 1577 | } |
| 1578 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1579 | ~MockURLRequestInterceptor() override = default; |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1580 | |
| 1581 | // URLRequestInterceptor implementation: |
| 1582 | URLRequestJob* MaybeInterceptRequest( |
| 1583 | URLRequest* request, |
| 1584 | NetworkDelegate* network_delegate) const override { |
| 1585 | if (restart_main_request_) { |
| 1586 | restart_main_request_ = false; |
| 1587 | did_restart_main_ = true; |
| 1588 | return new RestartTestJob(request, network_delegate); |
| 1589 | } |
| 1590 | if (cancel_main_request_) { |
| 1591 | cancel_main_request_ = false; |
| 1592 | did_cancel_main_ = true; |
| 1593 | return new CancelTestJob(request, network_delegate); |
| 1594 | } |
| 1595 | if (cancel_then_restart_main_request_) { |
| 1596 | cancel_then_restart_main_request_ = false; |
| 1597 | did_cancel_then_restart_main_ = true; |
| 1598 | return new CancelThenRestartTestJob(request, network_delegate); |
| 1599 | } |
| 1600 | if (simulate_main_network_error_) { |
| 1601 | simulate_main_network_error_ = false; |
| 1602 | did_simulate_error_main_ = true; |
| 1603 | if (use_url_request_http_job_) { |
| 1604 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1605 | } |
| 1606 | // This job will result in error since the requested URL is not one of the |
| 1607 | // URLs supported by these tests. |
| 1608 | return new URLRequestTestJob(request, network_delegate, true); |
| 1609 | } |
| 1610 | if (!intercept_main_request_) |
| 1611 | return nullptr; |
| 1612 | intercept_main_request_ = false; |
| 1613 | did_intercept_main_ = true; |
| 1614 | URLRequestTestJob* job = new URLRequestTestJob(request, |
| 1615 | network_delegate, |
| 1616 | main_headers_, |
| 1617 | main_data_, |
| 1618 | true); |
| 1619 | job->set_load_timing_info(main_request_load_timing_info_); |
| 1620 | return job; |
| 1621 | } |
| 1622 | |
| 1623 | URLRequestJob* MaybeInterceptRedirect(URLRequest* request, |
| 1624 | NetworkDelegate* network_delegate, |
| 1625 | const GURL& location) const override { |
| 1626 | if (cancel_redirect_request_) { |
| 1627 | cancel_redirect_request_ = false; |
| 1628 | did_cancel_redirect_ = true; |
| 1629 | return new CancelTestJob(request, network_delegate); |
| 1630 | } |
| 1631 | if (!intercept_redirect_) |
| 1632 | return nullptr; |
| 1633 | intercept_redirect_ = false; |
| 1634 | did_intercept_redirect_ = true; |
| 1635 | if (use_url_request_http_job_) { |
| 1636 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1637 | } |
| 1638 | return new URLRequestTestJob(request, |
| 1639 | network_delegate, |
| 1640 | redirect_headers_, |
| 1641 | redirect_data_, |
| 1642 | true); |
| 1643 | } |
| 1644 | |
| 1645 | URLRequestJob* MaybeInterceptResponse( |
| 1646 | URLRequest* request, |
| 1647 | NetworkDelegate* network_delegate) const override { |
| 1648 | if (cancel_final_request_) { |
| 1649 | cancel_final_request_ = false; |
| 1650 | did_cancel_final_ = true; |
| 1651 | return new CancelTestJob(request, network_delegate); |
| 1652 | } |
| 1653 | if (!intercept_final_response_) |
| 1654 | return nullptr; |
| 1655 | intercept_final_response_ = false; |
| 1656 | did_intercept_final_ = true; |
| 1657 | if (use_url_request_http_job_) { |
| 1658 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1659 | } |
| 1660 | return new URLRequestTestJob(request, |
| 1661 | network_delegate, |
| 1662 | final_headers_, |
| 1663 | final_data_, |
| 1664 | true); |
| 1665 | } |
| 1666 | |
| 1667 | void set_intercept_main_request(bool intercept_main_request) { |
| 1668 | intercept_main_request_ = intercept_main_request; |
| 1669 | } |
| 1670 | |
| 1671 | void set_main_headers(const std::string& main_headers) { |
| 1672 | main_headers_ = main_headers; |
| 1673 | } |
| 1674 | |
| 1675 | void set_main_data(const std::string& main_data) { |
| 1676 | main_data_ = main_data; |
| 1677 | } |
| 1678 | |
| 1679 | void set_main_request_load_timing_info( |
| 1680 | const LoadTimingInfo& main_request_load_timing_info) { |
| 1681 | main_request_load_timing_info_ = main_request_load_timing_info; |
| 1682 | } |
| 1683 | |
| 1684 | void set_restart_main_request(bool restart_main_request) { |
| 1685 | restart_main_request_ = restart_main_request; |
| 1686 | } |
| 1687 | |
| 1688 | void set_cancel_main_request(bool cancel_main_request) { |
| 1689 | cancel_main_request_ = cancel_main_request; |
| 1690 | } |
| 1691 | |
| 1692 | void set_cancel_then_restart_main_request( |
| 1693 | bool cancel_then_restart_main_request) { |
| 1694 | cancel_then_restart_main_request_ = cancel_then_restart_main_request; |
| 1695 | } |
| 1696 | |
| 1697 | void set_simulate_main_network_error(bool simulate_main_network_error) { |
| 1698 | simulate_main_network_error_ = simulate_main_network_error; |
| 1699 | } |
| 1700 | |
| 1701 | void set_intercept_redirect(bool intercept_redirect) { |
| 1702 | intercept_redirect_ = intercept_redirect; |
| 1703 | } |
| 1704 | |
| 1705 | void set_redirect_headers(const std::string& redirect_headers) { |
| 1706 | redirect_headers_ = redirect_headers; |
| 1707 | } |
| 1708 | |
| 1709 | void set_redirect_data(const std::string& redirect_data) { |
| 1710 | redirect_data_ = redirect_data; |
| 1711 | } |
| 1712 | |
| 1713 | void set_cancel_redirect_request(bool cancel_redirect_request) { |
| 1714 | cancel_redirect_request_ = cancel_redirect_request; |
| 1715 | } |
| 1716 | |
| 1717 | void set_intercept_final_response(bool intercept_final_response) { |
| 1718 | intercept_final_response_ = intercept_final_response; |
| 1719 | } |
| 1720 | |
| 1721 | void set_final_headers(const std::string& final_headers) { |
| 1722 | final_headers_ = final_headers; |
| 1723 | } |
| 1724 | |
| 1725 | void set_final_data(const std::string& final_data) { |
| 1726 | final_data_ = final_data; |
| 1727 | } |
| 1728 | |
| 1729 | void set_cancel_final_request(bool cancel_final_request) { |
| 1730 | cancel_final_request_ = cancel_final_request; |
| 1731 | } |
| 1732 | |
| 1733 | void set_use_url_request_http_job(bool use_url_request_http_job) { |
| 1734 | use_url_request_http_job_ = use_url_request_http_job; |
| 1735 | } |
| 1736 | |
| 1737 | bool did_intercept_main() const { |
| 1738 | return did_intercept_main_; |
| 1739 | } |
| 1740 | |
| 1741 | bool did_restart_main() const { |
| 1742 | return did_restart_main_; |
| 1743 | } |
| 1744 | |
| 1745 | bool did_cancel_main() const { |
| 1746 | return did_cancel_main_; |
| 1747 | } |
| 1748 | |
| 1749 | bool did_cancel_then_restart_main() const { |
| 1750 | return did_cancel_then_restart_main_; |
| 1751 | } |
| 1752 | |
| 1753 | bool did_simulate_error_main() const { |
| 1754 | return did_simulate_error_main_; |
| 1755 | } |
| 1756 | |
| 1757 | bool did_intercept_redirect() const { |
| 1758 | return did_intercept_redirect_; |
| 1759 | } |
| 1760 | |
| 1761 | bool did_cancel_redirect() const { |
| 1762 | return did_cancel_redirect_; |
| 1763 | } |
| 1764 | |
| 1765 | bool did_intercept_final() const { |
| 1766 | return did_intercept_final_; |
| 1767 | } |
| 1768 | |
| 1769 | bool did_cancel_final() const { |
| 1770 | return did_cancel_final_; |
| 1771 | } |
| 1772 | |
| 1773 | private: |
| 1774 | // Indicate whether to intercept the main request, and if so specify the |
| 1775 | // response to return and the LoadTimingInfo to use. |
| 1776 | mutable bool intercept_main_request_; |
| 1777 | mutable std::string main_headers_; |
| 1778 | mutable std::string main_data_; |
| 1779 | mutable LoadTimingInfo main_request_load_timing_info_; |
| 1780 | |
| 1781 | // These indicate actions that can be taken within MaybeInterceptRequest. |
| 1782 | mutable bool restart_main_request_; |
| 1783 | mutable bool cancel_main_request_; |
| 1784 | mutable bool cancel_then_restart_main_request_; |
| 1785 | mutable bool simulate_main_network_error_; |
| 1786 | |
| 1787 | // Indicate whether to intercept redirects, and if so specify the response to |
| 1788 | // return. |
| 1789 | mutable bool intercept_redirect_; |
| 1790 | mutable std::string redirect_headers_; |
| 1791 | mutable std::string redirect_data_; |
| 1792 | |
| 1793 | // Cancel the request within MaybeInterceptRedirect. |
| 1794 | mutable bool cancel_redirect_request_; |
| 1795 | |
| 1796 | // Indicate whether to intercept the final response, and if so specify the |
| 1797 | // response to return. |
| 1798 | mutable bool intercept_final_response_; |
| 1799 | mutable std::string final_headers_; |
| 1800 | mutable std::string final_data_; |
| 1801 | |
| 1802 | // Cancel the final request within MaybeInterceptResponse. |
| 1803 | mutable bool cancel_final_request_; |
| 1804 | |
| 1805 | // Instruct the interceptor to use a real URLRequestHTTPJob. |
| 1806 | mutable bool use_url_request_http_job_; |
| 1807 | |
| 1808 | // These indicate if the interceptor did something or not. |
| 1809 | mutable bool did_intercept_main_; |
| 1810 | mutable bool did_restart_main_; |
| 1811 | mutable bool did_cancel_main_; |
| 1812 | mutable bool did_cancel_then_restart_main_; |
| 1813 | mutable bool did_simulate_error_main_; |
| 1814 | mutable bool did_intercept_redirect_; |
| 1815 | mutable bool did_cancel_redirect_; |
| 1816 | mutable bool did_intercept_final_; |
| 1817 | mutable bool did_cancel_final_; |
| 1818 | }; |
| 1819 | |
| 1820 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
| 1821 | class URLRequestInterceptorTest : public URLRequestTest { |
| 1822 | public: |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1823 | URLRequestInterceptorTest() : URLRequestTest(), interceptor_(nullptr) {} |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1824 | |
| 1825 | ~URLRequestInterceptorTest() override { |
| 1826 | // URLRequestJobs may post clean-up tasks on destruction. |
| 1827 | base::RunLoop().RunUntilIdle(); |
| 1828 | } |
| 1829 | |
| 1830 | void SetUpFactory() override { |
| 1831 | interceptor_ = new MockURLRequestInterceptor(); |
| 1832 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1833 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | MockURLRequestInterceptor* interceptor() const { |
| 1837 | return interceptor_; |
| 1838 | } |
| 1839 | |
| 1840 | private: |
| 1841 | MockURLRequestInterceptor* interceptor_; |
| 1842 | }; |
| 1843 | |
| 1844 | TEST_F(URLRequestInterceptorTest, Intercept) { |
| 1845 | // Intercept the main request and respond with a simple response. |
| 1846 | interceptor()->set_intercept_main_request(true); |
| 1847 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1848 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1849 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1850 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1851 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1852 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1853 | base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); |
| 1854 | base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); |
| 1855 | base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); |
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 1856 | req->SetUserData(&user_data0, base::WrapUnique(user_data0)); |
avi | 0d99147 | 2017-04-27 07:04:04 | [diff] [blame] | 1857 | req->SetUserData(&user_data1, base::WrapUnique(user_data1)); |
| 1858 | req->SetUserData(&user_data2, base::WrapUnique(user_data2)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1859 | req->set_method("GET"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1860 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1861 | d.RunUntilComplete(); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1862 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1863 | // Make sure we can retrieve our specific user data. |
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 1864 | EXPECT_EQ(user_data0, req->GetUserData(&user_data0)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1865 | EXPECT_EQ(user_data1, req->GetUserData(&user_data1)); |
| 1866 | EXPECT_EQ(user_data2, req->GetUserData(&user_data2)); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1867 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1868 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1869 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1870 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1871 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1872 | EXPECT_EQ(1, d.response_started_count()); |
| 1873 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1874 | } |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1875 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1876 | TEST_F(URLRequestInterceptorTest, InterceptRedirect) { |
| 1877 | // Intercept the main request and respond with a redirect. |
| 1878 | interceptor()->set_intercept_main_request(true); |
| 1879 | interceptor()->set_main_headers( |
| 1880 | MockURLRequestInterceptor::redirect_headers()); |
| 1881 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 1882 | |
| 1883 | // Intercept that redirect and respond with a final OK response. |
| 1884 | interceptor()->set_intercept_redirect(true); |
| 1885 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 1886 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 1887 | |
| 1888 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1889 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1890 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1891 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1892 | req->set_method("GET"); |
| 1893 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1894 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1895 | |
| 1896 | // Check that the interceptor got called as expected. |
| 1897 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1898 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 1899 | |
| 1900 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1901 | int status = d.request_status(); |
| 1902 | EXPECT_EQ(OK, status); |
| 1903 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1904 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1905 | |
| 1906 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1907 | EXPECT_EQ(1, d.response_started_count()); |
| 1908 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1909 | } |
| 1910 | |
| 1911 | TEST_F(URLRequestInterceptorTest, InterceptServerError) { |
| 1912 | // Intercept the main request to generate a server error response. |
| 1913 | interceptor()->set_intercept_main_request(true); |
| 1914 | interceptor()->set_main_headers(MockURLRequestInterceptor::error_headers()); |
| 1915 | interceptor()->set_main_data(MockURLRequestInterceptor::error_data()); |
| 1916 | |
| 1917 | // Intercept that error and respond with an OK response. |
| 1918 | interceptor()->set_intercept_final_response(true); |
| 1919 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1920 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1921 | |
| 1922 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1923 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1924 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1925 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1926 | req->set_method("GET"); |
| 1927 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1928 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1929 | |
| 1930 | // Check that the interceptor got called as expected. |
| 1931 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1932 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1933 | |
| 1934 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1935 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1936 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1937 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1938 | EXPECT_EQ(1, d.response_started_count()); |
| 1939 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1940 | } |
| 1941 | |
| 1942 | TEST_F(URLRequestInterceptorTest, InterceptNetworkError) { |
| 1943 | // Intercept the main request to simulate a network error. |
| 1944 | interceptor()->set_simulate_main_network_error(true); |
| 1945 | |
| 1946 | // Intercept that error and respond with an OK response. |
| 1947 | interceptor()->set_intercept_final_response(true); |
| 1948 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1949 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1950 | |
| 1951 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1952 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1953 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1954 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1955 | req->set_method("GET"); |
| 1956 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1957 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1958 | |
| 1959 | // Check that the interceptor got called as expected. |
| 1960 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 1961 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1962 | |
| 1963 | // Check that we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1964 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1965 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1966 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1967 | EXPECT_EQ(1, d.response_started_count()); |
| 1968 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1969 | } |
| 1970 | |
| 1971 | TEST_F(URLRequestInterceptorTest, InterceptRestartRequired) { |
| 1972 | // Restart the main request. |
| 1973 | interceptor()->set_restart_main_request(true); |
| 1974 | |
| 1975 | // then intercept the new main request and respond with an OK response |
| 1976 | interceptor()->set_intercept_main_request(true); |
| 1977 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1978 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
| 1979 | |
| 1980 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1981 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1982 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1983 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1984 | req->set_method("GET"); |
| 1985 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1986 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1987 | |
| 1988 | // Check that the interceptor got called as expected. |
| 1989 | EXPECT_TRUE(interceptor()->did_restart_main()); |
| 1990 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1991 | |
| 1992 | // Check that we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1993 | int status = d.request_status(); |
| 1994 | EXPECT_EQ(OK, status); |
| 1995 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1996 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1997 | |
| 1998 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1999 | EXPECT_EQ(1, d.response_started_count()); |
| 2000 | EXPECT_EQ(0, d.received_redirect_count()); |
| 2001 | } |
| 2002 | |
| 2003 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelMain) { |
| 2004 | // Intercept the main request and cancel from within the restarted job. |
| 2005 | interceptor()->set_cancel_main_request(true); |
| 2006 | |
| 2007 | // Set up to intercept the final response and override it with an OK response. |
| 2008 | interceptor()->set_intercept_final_response(true); |
| 2009 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2010 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 2011 | |
| 2012 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2013 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2014 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2015 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2016 | req->set_method("GET"); |
| 2017 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2018 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2019 | |
| 2020 | // Check that the interceptor got called as expected. |
| 2021 | EXPECT_TRUE(interceptor()->did_cancel_main()); |
| 2022 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2023 | |
| 2024 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2025 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelRedirect) { |
| 2029 | // Intercept the main request and respond with a redirect. |
| 2030 | interceptor()->set_intercept_main_request(true); |
| 2031 | interceptor()->set_main_headers( |
| 2032 | MockURLRequestInterceptor::redirect_headers()); |
| 2033 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 2034 | |
| 2035 | // Intercept the redirect and cancel from within that job. |
| 2036 | interceptor()->set_cancel_redirect_request(true); |
| 2037 | |
| 2038 | // Set up to intercept the final response and override it with an OK response. |
| 2039 | interceptor()->set_intercept_final_response(true); |
| 2040 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2041 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 2042 | |
| 2043 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2044 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2045 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2046 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2047 | req->set_method("GET"); |
| 2048 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2049 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2050 | |
| 2051 | // Check that the interceptor got called as expected. |
| 2052 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 2053 | EXPECT_TRUE(interceptor()->did_cancel_redirect()); |
| 2054 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2055 | |
| 2056 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2057 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2058 | } |
| 2059 | |
| 2060 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelFinal) { |
| 2061 | // Intercept the main request to simulate a network error. |
| 2062 | interceptor()->set_simulate_main_network_error(true); |
| 2063 | |
| 2064 | // Set up to intercept final the response and cancel from within that job. |
| 2065 | interceptor()->set_cancel_final_request(true); |
| 2066 | |
| 2067 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2068 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2069 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2070 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2071 | req->set_method("GET"); |
| 2072 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2073 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2074 | |
| 2075 | // Check that the interceptor got called as expected. |
| 2076 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 2077 | EXPECT_TRUE(interceptor()->did_cancel_final()); |
| 2078 | |
| 2079 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2080 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2081 | } |
| 2082 | |
| 2083 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelInRestart) { |
| 2084 | // Intercept the main request and cancel then restart from within that job. |
| 2085 | interceptor()->set_cancel_then_restart_main_request(true); |
| 2086 | |
| 2087 | // Set up to intercept the final response and override it with an OK response. |
| 2088 | interceptor()->set_intercept_final_response(true); |
bengr | b50d631e | 2014-11-17 22:50:50 | [diff] [blame] | 2089 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2090 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2091 | |
| 2092 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2093 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2094 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2095 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2096 | req->set_method("GET"); |
| 2097 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2098 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2099 | |
| 2100 | // Check that the interceptor got called as expected. |
| 2101 | EXPECT_TRUE(interceptor()->did_cancel_then_restart_main()); |
| 2102 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2103 | |
| 2104 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2105 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not |
| 2109 | // reused. |connect_time_flags| is used to indicate if there should be dns |
| 2110 | // or SSL times, and |used_proxy| is used for proxy times. |
| 2111 | LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now, |
| 2112 | int connect_time_flags, |
| 2113 | bool used_proxy) { |
| 2114 | LoadTimingInfo load_timing; |
| 2115 | load_timing.socket_log_id = 1; |
| 2116 | |
| 2117 | if (used_proxy) { |
| 2118 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 2119 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 2120 | } |
| 2121 | |
| 2122 | LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing; |
| 2123 | if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) { |
| 2124 | connect_timing.dns_start = now + base::TimeDelta::FromDays(3); |
| 2125 | connect_timing.dns_end = now + base::TimeDelta::FromDays(4); |
| 2126 | } |
| 2127 | connect_timing.connect_start = now + base::TimeDelta::FromDays(5); |
| 2128 | if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) { |
| 2129 | connect_timing.ssl_start = now + base::TimeDelta::FromDays(6); |
| 2130 | connect_timing.ssl_end = now + base::TimeDelta::FromDays(7); |
| 2131 | } |
| 2132 | connect_timing.connect_end = now + base::TimeDelta::FromDays(8); |
| 2133 | |
| 2134 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 2135 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 2136 | load_timing.receive_headers_start = now + base::TimeDelta::FromDays(11); |
| 2137 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(12); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2138 | return load_timing; |
| 2139 | } |
| 2140 | |
| 2141 | // Same as above, but in the case of a reused socket. |
| 2142 | LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now, |
| 2143 | bool used_proxy) { |
| 2144 | LoadTimingInfo load_timing; |
| 2145 | load_timing.socket_log_id = 1; |
| 2146 | load_timing.socket_reused = true; |
| 2147 | |
| 2148 | if (used_proxy) { |
| 2149 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 2150 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 2151 | } |
| 2152 | |
| 2153 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 2154 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 2155 | load_timing.receive_headers_start = now + base::TimeDelta::FromDays(11); |
| 2156 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(12); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2157 | return load_timing; |
| 2158 | } |
| 2159 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2160 | LoadTimingInfo RunURLRequestInterceptorLoadTimingTest( |
| 2161 | const LoadTimingInfo& job_load_timing, |
| 2162 | const URLRequestContext& context, |
| 2163 | MockURLRequestInterceptor* interceptor) { |
| 2164 | interceptor->set_intercept_main_request(true); |
| 2165 | interceptor->set_main_request_load_timing_info(job_load_timing); |
| 2166 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2167 | std::unique_ptr<URLRequest> req( |
| 2168 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 2169 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2170 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2171 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2172 | |
| 2173 | LoadTimingInfo resulting_load_timing; |
| 2174 | req->GetLoadTimingInfo(&resulting_load_timing); |
| 2175 | |
| 2176 | // None of these should be modified by the URLRequest. |
| 2177 | EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); |
| 2178 | EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); |
| 2179 | EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); |
| 2180 | EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 2181 | EXPECT_EQ(job_load_timing.receive_headers_start, |
| 2182 | resulting_load_timing.receive_headers_start); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2183 | EXPECT_EQ(job_load_timing.receive_headers_end, |
| 2184 | resulting_load_timing.receive_headers_end); |
caseq | e8340bc9 | 2016-04-20 00:02:57 | [diff] [blame] | 2185 | EXPECT_EQ(job_load_timing.push_start, resulting_load_timing.push_start); |
| 2186 | EXPECT_EQ(job_load_timing.push_end, resulting_load_timing.push_end); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2187 | |
| 2188 | return resulting_load_timing; |
| 2189 | } |
| 2190 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2191 | // Basic test that the intercept + load timing tests work. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2192 | TEST_F(URLRequestInterceptorTest, InterceptLoadTiming) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2193 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2194 | LoadTimingInfo job_load_timing = |
| 2195 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); |
| 2196 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2197 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2198 | RunURLRequestInterceptorLoadTimingTest( |
| 2199 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2200 | |
| 2201 | // Nothing should have been changed by the URLRequest. |
| 2202 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 2203 | load_timing_result.proxy_resolve_start); |
| 2204 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 2205 | load_timing_result.proxy_resolve_end); |
| 2206 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 2207 | load_timing_result.connect_timing.dns_start); |
| 2208 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 2209 | load_timing_result.connect_timing.dns_end); |
| 2210 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 2211 | load_timing_result.connect_timing.connect_start); |
| 2212 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 2213 | load_timing_result.connect_timing.connect_end); |
| 2214 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 2215 | load_timing_result.connect_timing.ssl_start); |
| 2216 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 2217 | load_timing_result.connect_timing.ssl_end); |
| 2218 | |
| 2219 | // Redundant sanity check. |
| 2220 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2221 | } |
| 2222 | |
| 2223 | // Another basic test, with proxy and SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2224 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2225 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2226 | LoadTimingInfo job_load_timing = |
| 2227 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); |
| 2228 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2229 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2230 | RunURLRequestInterceptorLoadTimingTest( |
| 2231 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2232 | |
| 2233 | // Nothing should have been changed by the URLRequest. |
| 2234 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 2235 | load_timing_result.proxy_resolve_start); |
| 2236 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 2237 | load_timing_result.proxy_resolve_end); |
| 2238 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 2239 | load_timing_result.connect_timing.dns_start); |
| 2240 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 2241 | load_timing_result.connect_timing.dns_end); |
| 2242 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 2243 | load_timing_result.connect_timing.connect_start); |
| 2244 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 2245 | load_timing_result.connect_timing.connect_end); |
| 2246 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 2247 | load_timing_result.connect_timing.ssl_start); |
| 2248 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 2249 | load_timing_result.connect_timing.ssl_end); |
| 2250 | |
| 2251 | // Redundant sanity check. |
| 2252 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2253 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 2254 | } |
| 2255 | |
| 2256 | // Make sure that URLRequest correctly adjusts proxy times when they're before |
| 2257 | // |request_start|, due to already having a connected socket. This happens in |
[email protected] | cf4cae3 | 2014-05-27 00:39:10 | [diff] [blame] | 2258 | // the case of reusing a SPDY session. The connected socket is not considered |
| 2259 | // reused in this test (May be a preconnect). |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2260 | // |
| 2261 | // To mix things up from the test above, assumes DNS times but no SSL times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2262 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyProxyResolution) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2263 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2264 | LoadTimingInfo job_load_timing = |
| 2265 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); |
| 2266 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); |
| 2267 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); |
| 2268 | job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); |
| 2269 | job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); |
| 2270 | job_load_timing.connect_timing.connect_start = |
| 2271 | now - base::TimeDelta::FromDays(2); |
| 2272 | job_load_timing.connect_timing.connect_end = |
| 2273 | now - base::TimeDelta::FromDays(1); |
| 2274 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2275 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2276 | RunURLRequestInterceptorLoadTimingTest( |
| 2277 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2278 | |
| 2279 | // Proxy times, connect times, and DNS times should all be replaced with |
| 2280 | // request_start. |
| 2281 | EXPECT_EQ(load_timing_result.request_start, |
| 2282 | load_timing_result.proxy_resolve_start); |
| 2283 | EXPECT_EQ(load_timing_result.request_start, |
| 2284 | load_timing_result.proxy_resolve_end); |
| 2285 | EXPECT_EQ(load_timing_result.request_start, |
| 2286 | load_timing_result.connect_timing.dns_start); |
| 2287 | EXPECT_EQ(load_timing_result.request_start, |
| 2288 | load_timing_result.connect_timing.dns_end); |
| 2289 | EXPECT_EQ(load_timing_result.request_start, |
| 2290 | load_timing_result.connect_timing.connect_start); |
| 2291 | EXPECT_EQ(load_timing_result.request_start, |
| 2292 | load_timing_result.connect_timing.connect_end); |
| 2293 | |
| 2294 | // Other times should have been left null. |
| 2295 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2296 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 2297 | } |
| 2298 | |
| 2299 | // Same as above, but in the reused case. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2300 | TEST_F(URLRequestInterceptorTest, |
| 2301 | InterceptLoadTimingEarlyProxyResolutionReused) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2302 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2303 | LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); |
| 2304 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); |
| 2305 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); |
| 2306 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2307 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2308 | RunURLRequestInterceptorLoadTimingTest( |
| 2309 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2310 | |
| 2311 | // Proxy times and connect times should all be replaced with request_start. |
| 2312 | EXPECT_EQ(load_timing_result.request_start, |
| 2313 | load_timing_result.proxy_resolve_start); |
| 2314 | EXPECT_EQ(load_timing_result.request_start, |
| 2315 | load_timing_result.proxy_resolve_end); |
| 2316 | |
| 2317 | // Other times should have been left null. |
| 2318 | TestLoadTimingReusedWithProxy(load_timing_result); |
| 2319 | } |
| 2320 | |
| 2321 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2322 | // |request_start|, due to reusing a connected socket. The connected socket is |
| 2323 | // not considered reused in this test (May be a preconnect). |
| 2324 | // |
| 2325 | // To mix things up, the request has SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2326 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnect) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2327 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2328 | LoadTimingInfo job_load_timing = |
| 2329 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); |
| 2330 | job_load_timing.connect_timing.connect_start = |
| 2331 | now - base::TimeDelta::FromDays(1); |
| 2332 | job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); |
| 2333 | job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); |
| 2334 | job_load_timing.connect_timing.connect_end = |
| 2335 | now - base::TimeDelta::FromDays(4); |
| 2336 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2337 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2338 | RunURLRequestInterceptorLoadTimingTest( |
| 2339 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2340 | |
| 2341 | // Connect times, and SSL times should be replaced with request_start. |
| 2342 | EXPECT_EQ(load_timing_result.request_start, |
| 2343 | load_timing_result.connect_timing.connect_start); |
| 2344 | EXPECT_EQ(load_timing_result.request_start, |
| 2345 | load_timing_result.connect_timing.ssl_start); |
| 2346 | EXPECT_EQ(load_timing_result.request_start, |
| 2347 | load_timing_result.connect_timing.ssl_end); |
| 2348 | EXPECT_EQ(load_timing_result.request_start, |
| 2349 | load_timing_result.connect_timing.connect_end); |
| 2350 | |
| 2351 | // Other times should have been left null. |
| 2352 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); |
| 2353 | } |
| 2354 | |
| 2355 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2356 | // |request_start|, due to reusing a connected socket in the case that there |
| 2357 | // are also proxy times. The connected socket is not considered reused in this |
| 2358 | // test (May be a preconnect). |
| 2359 | // |
| 2360 | // In this test, there are no SSL or DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2361 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnectWithProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2362 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2363 | LoadTimingInfo job_load_timing = |
| 2364 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); |
| 2365 | job_load_timing.connect_timing.connect_start = |
| 2366 | now - base::TimeDelta::FromDays(1); |
| 2367 | job_load_timing.connect_timing.connect_end = |
| 2368 | now - base::TimeDelta::FromDays(2); |
| 2369 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2370 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2371 | RunURLRequestInterceptorLoadTimingTest( |
| 2372 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2373 | |
| 2374 | // Connect times should be replaced with proxy_resolve_end. |
| 2375 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2376 | load_timing_result.connect_timing.connect_start); |
| 2377 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2378 | load_timing_result.connect_timing.connect_end); |
| 2379 | |
| 2380 | // Other times should have been left null. |
| 2381 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2382 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 2383 | } |
| 2384 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2385 | // Check that two different URL requests have different identifiers. |
| 2386 | TEST_F(URLRequestTest, Identifiers) { |
| 2387 | TestDelegate d; |
| 2388 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2389 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2390 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2391 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2392 | std::unique_ptr<URLRequest> other_req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2393 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2394 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2395 | |
mmenke | 19378d2 | 2014-09-09 04:12:59 | [diff] [blame] | 2396 | ASSERT_NE(req->identifier(), other_req->identifier()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2397 | } |
| 2398 | |
Matt Menke | 39e6de6 | 2019-06-27 20:11:39 | [diff] [blame] | 2399 | TEST_F(URLRequestTest, NetworkDelegateProxyError) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2400 | MockHostResolver host_resolver; |
| 2401 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 2402 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2403 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
Matt Menke | 39e6de6 | 2019-06-27 20:11:39 | [diff] [blame] | 2404 | TestURLRequestContextWithProxy context("myproxy:70", &network_delegate, |
| 2405 | true /* delay_initialization */); |
| 2406 | context.set_host_resolver(&host_resolver); |
| 2407 | context.Init(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2408 | |
| 2409 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2410 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2411 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2412 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2413 | req->set_method("GET"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2414 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2415 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2416 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2417 | |
| 2418 | // Check we see a failed request. |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 2419 | // The proxy server should be set before failure. |
| 2420 | EXPECT_EQ(ProxyServer::FromPacString("PROXY myproxy:70"), |
| 2421 | req->proxy_server()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2422 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2423 | |
| 2424 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2425 | EXPECT_THAT(network_delegate.last_error(), |
| 2426 | IsError(ERR_PROXY_CONNECTION_FAILED)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2427 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2428 | } |
| 2429 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2430 | // Make sure that NetworkDelegate::NotifyCompleted is called if |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2431 | // content is empty. |
| 2432 | TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) { |
| 2433 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2434 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2435 | GURL("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2436 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2437 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2438 | EXPECT_EQ("", d.data_received()); |
| 2439 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 2440 | } |
| 2441 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2442 | // Make sure that SetPriority actually sets the URLRequest's priority |
| 2443 | // correctly, both before and after start. |
| 2444 | TEST_F(URLRequestTest, SetPriorityBasic) { |
| 2445 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2446 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2447 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2448 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2449 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2450 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2451 | req->SetPriority(LOW); |
| 2452 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2453 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2454 | req->Start(); |
| 2455 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2456 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2457 | req->SetPriority(MEDIUM); |
| 2458 | EXPECT_EQ(MEDIUM, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2459 | } |
| 2460 | |
| 2461 | // Make sure that URLRequest calls SetPriority on a job before calling |
| 2462 | // Start on it. |
| 2463 | TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) { |
| 2464 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2465 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2466 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2467 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2468 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2469 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2470 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2471 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2472 | req.get(), &default_network_delegate_, &job_priority)); |
| 2473 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
| 2474 | EXPECT_EQ(DEFAULT_PRIORITY, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2475 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2476 | req->SetPriority(LOW); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2477 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2478 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2479 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | // Make sure that URLRequest passes on its priority updates to its |
| 2483 | // job. |
| 2484 | TEST_F(URLRequestTest, SetJobPriority) { |
| 2485 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2486 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2487 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2488 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2489 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2490 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2491 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2492 | req.get(), &default_network_delegate_, &job_priority)); |
| 2493 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2494 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2495 | req->SetPriority(LOW); |
| 2496 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2497 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2498 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2499 | req->SetPriority(MEDIUM); |
| 2500 | EXPECT_EQ(MEDIUM, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2501 | EXPECT_EQ(MEDIUM, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2502 | } |
| 2503 | |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2504 | // Setting the IGNORE_LIMITS load flag should be okay if the priority |
| 2505 | // is MAXIMUM_PRIORITY. |
| 2506 | TEST_F(URLRequestTest, PriorityIgnoreLimits) { |
| 2507 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2508 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2509 | GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY, &d, |
| 2510 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2511 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2512 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2513 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2514 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2515 | req.get(), &default_network_delegate_, &job_priority)); |
| 2516 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2517 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2518 | req->SetLoadFlags(LOAD_IGNORE_LIMITS); |
| 2519 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2520 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2521 | req->SetPriority(MAXIMUM_PRIORITY); |
| 2522 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2523 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2524 | req->Start(); |
| 2525 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2526 | EXPECT_EQ(MAXIMUM_PRIORITY, job_priority); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2527 | } |
| 2528 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2529 | namespace { |
| 2530 | |
| 2531 | // Less verbose way of running a simple testserver for the tests below. |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2532 | class HttpTestServer : public EmbeddedTestServer { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2533 | public: |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2534 | explicit HttpTestServer(const base::FilePath& document_root) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2535 | AddDefaultHandlers(document_root); |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2536 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2537 | |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2538 | HttpTestServer() { AddDefaultHandlers(base::FilePath()); } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2539 | }; |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2540 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2541 | } // namespace |
| 2542 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2543 | TEST_F(URLRequestTest, DelayedCookieCallback) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2544 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2545 | ASSERT_TRUE(test_server.Start()); |
| 2546 | |
| 2547 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2548 | std::unique_ptr<DelayedCookieMonster> delayed_cm(new DelayedCookieMonster()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2549 | context.set_cookie_store(delayed_cm.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2550 | |
| 2551 | // Set up a cookie. |
| 2552 | { |
| 2553 | TestNetworkDelegate network_delegate; |
| 2554 | context.set_network_delegate(&network_delegate); |
| 2555 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2556 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2557 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2558 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2559 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2560 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2561 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2562 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2563 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2564 | } |
| 2565 | |
| 2566 | // Verify that the cookie is set. |
| 2567 | { |
| 2568 | TestNetworkDelegate network_delegate; |
| 2569 | context.set_network_delegate(&network_delegate); |
| 2570 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2571 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2572 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2573 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2574 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2575 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2576 | |
| 2577 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2578 | != std::string::npos); |
| 2579 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2580 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2581 | } |
| 2582 | } |
| 2583 | |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 2584 | class FilteringTestLayeredNetworkDelegate : public LayeredNetworkDelegate { |
| 2585 | public: |
| 2586 | FilteringTestLayeredNetworkDelegate( |
| 2587 | std::unique_ptr<NetworkDelegate> network_delegate) |
| 2588 | : LayeredNetworkDelegate(std::move((network_delegate))), |
| 2589 | set_cookie_called_count_(0), |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 2590 | blocked_set_cookie_count_(0), |
| 2591 | block_get_cookies_(false), |
| 2592 | get_cookie_called_count_(0), |
| 2593 | blocked_get_cookie_count_(0) {} |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 2594 | ~FilteringTestLayeredNetworkDelegate() override = default; |
| 2595 | |
| 2596 | bool OnCanSetCookieInternal(const URLRequest& request, |
| 2597 | const net::CanonicalCookie& cookie, |
| 2598 | CookieOptions* options, |
| 2599 | bool allowed_from_caller) override { |
| 2600 | // Filter out cookies with the same name as |cookie_name_filter_| and |
| 2601 | // combine with |allowed_from_caller|. |
| 2602 | bool allowed = |
| 2603 | allowed_from_caller && !(cookie.Name() == cookie_name_filter_); |
| 2604 | |
| 2605 | ++set_cookie_called_count_; |
| 2606 | |
| 2607 | if (!allowed) |
| 2608 | ++blocked_set_cookie_count_; |
| 2609 | |
| 2610 | return allowed; |
| 2611 | } |
| 2612 | |
| 2613 | void SetCookieFilter(std::string filter) { |
| 2614 | cookie_name_filter_ = std::move(filter); |
| 2615 | } |
| 2616 | |
| 2617 | int set_cookie_called_count() { return set_cookie_called_count_; } |
| 2618 | |
| 2619 | int blocked_set_cookie_count() { return blocked_set_cookie_count_; } |
| 2620 | |
| 2621 | void ResetSetCookieCalledCount() { set_cookie_called_count_ = 0; } |
| 2622 | |
| 2623 | void ResetBlockedSetCookieCount() { blocked_set_cookie_count_ = 0; } |
| 2624 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 2625 | bool OnCanGetCookiesInternal(const URLRequest& request, |
| 2626 | const net::CookieList& cookie, |
| 2627 | bool allowed_from_caller) override { |
| 2628 | // Filter out cookies if |block_get_cookies_| is set and |
| 2629 | // combine with |allowed_from_caller|. |
| 2630 | bool allowed = allowed_from_caller && !block_get_cookies_; |
| 2631 | |
| 2632 | ++get_cookie_called_count_; |
| 2633 | |
| 2634 | if (!allowed) |
| 2635 | ++blocked_get_cookie_count_; |
| 2636 | |
| 2637 | return allowed; |
| 2638 | } |
| 2639 | |
| 2640 | void set_block_get_cookies() { block_get_cookies_ = true; } |
| 2641 | |
| 2642 | void unset_block_get_cookies() { block_get_cookies_ = false; } |
| 2643 | |
| 2644 | int get_cookie_called_count() const { return get_cookie_called_count_; } |
| 2645 | |
| 2646 | int blocked_get_cookie_count() const { return blocked_get_cookie_count_; } |
| 2647 | |
| 2648 | void ResetGetCookieCalledCount() { get_cookie_called_count_ = 0; } |
| 2649 | |
| 2650 | void ResetBlockedGetCookieCount() { blocked_get_cookie_count_ = 0; } |
| 2651 | |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 2652 | private: |
| 2653 | std::string cookie_name_filter_; |
| 2654 | int set_cookie_called_count_; |
| 2655 | int blocked_set_cookie_count_; |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 2656 | |
| 2657 | bool block_get_cookies_; |
| 2658 | int get_cookie_called_count_; |
| 2659 | int blocked_get_cookie_count_; |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 2660 | }; |
| 2661 | |
| 2662 | TEST_F(URLRequestTest, DelayedCookieCallbackAsync) { |
| 2663 | HttpTestServer test_server; |
| 2664 | ASSERT_TRUE(test_server.Start()); |
| 2665 | |
| 2666 | TestURLRequestContext async_context; |
| 2667 | std::unique_ptr<DelayedCookieMonster> delayed_cm = |
| 2668 | std::make_unique<DelayedCookieMonster>(); |
| 2669 | async_context.set_cookie_store(delayed_cm.get()); |
| 2670 | FilteringTestLayeredNetworkDelegate async_filter_network_delegate( |
| 2671 | std::make_unique<TestNetworkDelegate>()); |
| 2672 | async_filter_network_delegate.SetCookieFilter("CookieBlockedOnCanGetCookie"); |
| 2673 | async_context.set_network_delegate(&async_filter_network_delegate); |
| 2674 | TestDelegate async_delegate; |
| 2675 | |
| 2676 | TestURLRequestContext sync_context; |
| 2677 | std::unique_ptr<CookieMonster> cm = |
Nick Harper | 57142b1c | 2019-03-14 21:03:59 | [diff] [blame] | 2678 | std::make_unique<CookieMonster>(nullptr, nullptr); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 2679 | sync_context.set_cookie_store(cm.get()); |
| 2680 | FilteringTestLayeredNetworkDelegate sync_filter_network_delegate( |
| 2681 | std::make_unique<TestNetworkDelegate>()); |
| 2682 | sync_filter_network_delegate.SetCookieFilter("CookieBlockedOnCanGetCookie"); |
| 2683 | sync_context.set_network_delegate(&sync_filter_network_delegate); |
| 2684 | TestDelegate sync_delegate; |
| 2685 | |
| 2686 | // Add a secure cookie so we can try to set an insecure cookie and have |
Lily Chen | 0f208ea | 2019-08-08 23:37:53 | [diff] [blame] | 2687 | // SetCanonicalCookie fail. |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 2688 | GURL::Replacements replace_scheme; |
| 2689 | replace_scheme.SetSchemeStr("https"); |
| 2690 | GURL url = test_server.base_url().ReplaceComponents(replace_scheme); |
| 2691 | |
Lily Chen | 0f208ea | 2019-08-08 23:37:53 | [diff] [blame] | 2692 | auto cookie1 = CanonicalCookie::Create(url, "AlreadySetCookie=1;Secure", |
| 2693 | base::Time::Now(), |
| 2694 | base::nullopt /* server_time */); |
| 2695 | delayed_cm->SetCanonicalCookieAsync(std::move(cookie1), url.scheme(), |
| 2696 | CookieOptions(), |
| 2697 | CookieStore::SetCookiesCallback()); |
| 2698 | auto cookie2 = CanonicalCookie::Create(url, "AlreadySetCookie=1;Secure", |
| 2699 | base::Time::Now(), |
| 2700 | base::nullopt /* server_time */); |
| 2701 | cm->SetCanonicalCookieAsync(std::move(cookie2), url.scheme(), CookieOptions(), |
| 2702 | CookieStore::SetCookiesCallback()); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 2703 | |
| 2704 | std::vector<std::string> cookie_lines( |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 2705 | {// Fails in SetCanonicalCookie for trying to set a secure cookie |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 2706 | // on an insecure host. |
| 2707 | "CookieNotSet=1;Secure", |
| 2708 | // Fail in FilteringTestLayeredNetworkDelegate::CanGetCookie. |
| 2709 | "CookieBlockedOnCanGetCookie=1", |
| 2710 | // Fails in SetCanonicalCookie for trying to overwrite a secure cookie |
| 2711 | // with an insecure cookie. |
| 2712 | "AlreadySetCookie=1", |
| 2713 | // Succeeds and added cookie to store. Delayed (which makes the callback |
| 2714 | // run asynchronously) in DelayedCookieMonster. |
| 2715 | "CookieSet=1"}); |
| 2716 | |
| 2717 | for (auto first_cookie_line : cookie_lines) { |
| 2718 | for (auto second_cookie_line : cookie_lines) { |
| 2719 | // Run with the delayed cookie monster. |
| 2720 | std::unique_ptr<URLRequest> request = async_context.CreateRequest( |
| 2721 | test_server.GetURL("/set-cookie?" + first_cookie_line + "&" + |
| 2722 | second_cookie_line), |
| 2723 | DEFAULT_PRIORITY, &async_delegate, TRAFFIC_ANNOTATION_FOR_TESTS); |
| 2724 | |
| 2725 | request->Start(); |
| 2726 | async_delegate.RunUntilComplete(); |
| 2727 | EXPECT_THAT(async_delegate.request_status(), IsOk()); |
| 2728 | |
| 2729 | // Run with the regular cookie monster. |
| 2730 | request = sync_context.CreateRequest( |
| 2731 | test_server.GetURL("/set-cookie?" + first_cookie_line + "&" + |
| 2732 | second_cookie_line), |
| 2733 | DEFAULT_PRIORITY, &sync_delegate, TRAFFIC_ANNOTATION_FOR_TESTS); |
| 2734 | |
| 2735 | request->Start(); |
| 2736 | sync_delegate.RunUntilComplete(); |
| 2737 | EXPECT_THAT(sync_delegate.request_status(), IsOk()); |
| 2738 | |
| 2739 | int expected_set_cookie_count = 0; |
| 2740 | int expected_blocked_cookie_count = 0; |
| 2741 | |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 2742 | // 2 calls to the delegate's OnCanSetCookie method are expected, even if |
| 2743 | // the cookies don't end up getting set. |
| 2744 | expected_set_cookie_count += 2; |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 2745 | |
| 2746 | if (first_cookie_line == "CookieBlockedOnCanGetCookie=1") |
| 2747 | ++expected_blocked_cookie_count; |
| 2748 | if (second_cookie_line == "CookieBlockedOnCanGetCookie=1") |
| 2749 | ++expected_blocked_cookie_count; |
| 2750 | |
| 2751 | EXPECT_EQ(expected_set_cookie_count, |
| 2752 | async_filter_network_delegate.set_cookie_called_count()); |
| 2753 | EXPECT_EQ(expected_blocked_cookie_count, |
| 2754 | async_filter_network_delegate.blocked_set_cookie_count()); |
| 2755 | |
| 2756 | EXPECT_EQ(expected_set_cookie_count, |
| 2757 | sync_filter_network_delegate.set_cookie_called_count()); |
| 2758 | EXPECT_EQ(expected_blocked_cookie_count, |
| 2759 | sync_filter_network_delegate.blocked_set_cookie_count()); |
| 2760 | |
| 2761 | async_filter_network_delegate.ResetSetCookieCalledCount(); |
| 2762 | async_filter_network_delegate.ResetBlockedSetCookieCount(); |
| 2763 | |
| 2764 | sync_filter_network_delegate.ResetSetCookieCalledCount(); |
| 2765 | sync_filter_network_delegate.ResetBlockedSetCookieCount(); |
| 2766 | } |
| 2767 | } |
| 2768 | } |
| 2769 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2770 | TEST_F(URLRequestTest, DoNotSendCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2771 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2772 | ASSERT_TRUE(test_server.Start()); |
| 2773 | |
| 2774 | // Set up a cookie. |
| 2775 | { |
| 2776 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2777 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2778 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2779 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2780 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2781 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2782 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2783 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2784 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2785 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2786 | } |
| 2787 | |
| 2788 | // Verify that the cookie is set. |
| 2789 | { |
| 2790 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2791 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2792 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2793 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2794 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2795 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2796 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2797 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2798 | |
| 2799 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2800 | != std::string::npos); |
| 2801 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2802 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2803 | } |
| 2804 | |
| 2805 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 2806 | { |
| 2807 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2808 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2809 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2810 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2811 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2812 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2813 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
| 2814 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2815 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2816 | |
| 2817 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2818 | == std::string::npos); |
| 2819 | |
| 2820 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
| 2821 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2822 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2823 | } |
| 2824 | } |
| 2825 | |
| 2826 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2827 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2828 | ASSERT_TRUE(test_server.Start()); |
| 2829 | |
| 2830 | // Set up a cookie. |
| 2831 | { |
| 2832 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2833 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2834 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2835 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2836 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2837 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2838 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2839 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2840 | |
| 2841 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2842 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2843 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2844 | } |
| 2845 | |
| 2846 | // Try to set-up another cookie and update the previous cookie. |
| 2847 | { |
| 2848 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2849 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2850 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2851 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2852 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2853 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2854 | req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES); |
| 2855 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2856 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2857 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2858 | |
| 2859 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
| 2860 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2861 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2862 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2863 | } |
| 2864 | |
| 2865 | // Verify the cookies weren't saved or updated. |
| 2866 | { |
| 2867 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2868 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2869 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2870 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2871 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2872 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2873 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2874 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2875 | |
| 2876 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2877 | == std::string::npos); |
| 2878 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2879 | != std::string::npos); |
| 2880 | |
| 2881 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2882 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2883 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2884 | } |
| 2885 | } |
| 2886 | |
| 2887 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2888 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2889 | ASSERT_TRUE(test_server.Start()); |
| 2890 | |
| 2891 | // Set up a cookie. |
| 2892 | { |
| 2893 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2894 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2895 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2896 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2897 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2898 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2899 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2900 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2901 | |
| 2902 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2903 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2904 | } |
| 2905 | |
| 2906 | // Verify that the cookie is set. |
| 2907 | { |
| 2908 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2909 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2910 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2911 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2912 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2913 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2914 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2915 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2916 | |
| 2917 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2918 | != std::string::npos); |
| 2919 | |
| 2920 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2921 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2922 | auto entries = net_log_.GetEntries(); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2923 | for (const auto& entry : entries) { |
| 2924 | EXPECT_NE(entry.type, |
| 2925 | NetLogEventType::COOKIE_GET_BLOCKED_BY_NETWORK_DELEGATE); |
| 2926 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2927 | } |
| 2928 | |
| 2929 | // Verify that the cookie isn't sent. |
| 2930 | { |
| 2931 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2932 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2933 | TestDelegate d; |
| 2934 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2935 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2936 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2937 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2938 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2939 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2940 | |
| 2941 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2942 | == std::string::npos); |
| 2943 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2944 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2945 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2946 | auto entries = net_log_.GetEntries(); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2947 | ExpectLogContainsSomewhereAfter( |
| 2948 | entries, 0, NetLogEventType::COOKIE_GET_BLOCKED_BY_NETWORK_DELEGATE, |
| 2949 | NetLogEventPhase::NONE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2950 | } |
| 2951 | } |
| 2952 | |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 2953 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 2954 | #if defined(OS_IOS) |
| 2955 | #define MAYBE_DoNotSaveCookies_ViaPolicy FLAKY_DoNotSaveCookies_ViaPolicy |
| 2956 | #else |
| 2957 | #define MAYBE_DoNotSaveCookies_ViaPolicy DoNotSaveCookies_ViaPolicy |
| 2958 | #endif |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2959 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2960 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2961 | ASSERT_TRUE(test_server.Start()); |
| 2962 | |
| 2963 | // Set up a cookie. |
| 2964 | { |
| 2965 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2966 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2967 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2968 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2969 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2970 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2971 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2972 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2973 | |
| 2974 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2975 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2976 | auto entries = net_log_.GetEntries(); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2977 | for (const auto& entry : entries) { |
| 2978 | EXPECT_NE(entry.type, |
| 2979 | NetLogEventType::COOKIE_SET_BLOCKED_BY_NETWORK_DELEGATE); |
| 2980 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2981 | } |
| 2982 | |
| 2983 | // Try to set-up another cookie and update the previous cookie. |
| 2984 | { |
| 2985 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2986 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2987 | TestDelegate d; |
| 2988 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2989 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2990 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2991 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2992 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2993 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2994 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2995 | |
| 2996 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2997 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2998 | auto entries = net_log_.GetEntries(); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2999 | ExpectLogContainsSomewhereAfter( |
| 3000 | entries, 0, NetLogEventType::COOKIE_SET_BLOCKED_BY_NETWORK_DELEGATE, |
| 3001 | NetLogEventPhase::NONE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | // Verify the cookies weren't saved or updated. |
| 3005 | { |
| 3006 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3007 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3008 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3009 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3010 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3011 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3012 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3013 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3014 | |
| 3015 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 3016 | == std::string::npos); |
| 3017 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 3018 | != std::string::npos); |
| 3019 | |
| 3020 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3021 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3022 | } |
| 3023 | } |
| 3024 | |
| 3025 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3026 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3027 | ASSERT_TRUE(test_server.Start()); |
| 3028 | |
| 3029 | // Set up an empty cookie. |
| 3030 | { |
| 3031 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3032 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3033 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3034 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3035 | test_server.GetURL("/set-cookie"), DEFAULT_PRIORITY, &d, |
| 3036 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3037 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3038 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3039 | |
| 3040 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3041 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3042 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 3043 | } |
| 3044 | } |
| 3045 | |
| 3046 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3047 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3048 | ASSERT_TRUE(test_server.Start()); |
| 3049 | |
| 3050 | // Set up a cookie. |
| 3051 | { |
| 3052 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3053 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3054 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3055 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3056 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3057 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3058 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3059 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3060 | |
| 3061 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3062 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3063 | } |
| 3064 | |
| 3065 | // Verify that the cookie is set. |
| 3066 | { |
| 3067 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3068 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3069 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3070 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3071 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3072 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3073 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3074 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3075 | |
| 3076 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 3077 | != std::string::npos); |
| 3078 | |
| 3079 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3080 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3081 | } |
| 3082 | |
| 3083 | // Verify that the cookie isn't sent. |
| 3084 | { |
| 3085 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3086 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3087 | TestDelegate d; |
| 3088 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3089 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3090 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3091 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3092 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3093 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3094 | |
| 3095 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 3096 | == std::string::npos); |
| 3097 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 3098 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3099 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3100 | } |
| 3101 | } |
| 3102 | |
| 3103 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3104 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3105 | ASSERT_TRUE(test_server.Start()); |
| 3106 | |
| 3107 | // Set up a cookie. |
| 3108 | { |
| 3109 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3110 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3111 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3112 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3113 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3114 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3115 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3116 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3117 | |
| 3118 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3119 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3120 | } |
| 3121 | |
| 3122 | // Try to set-up another cookie and update the previous cookie. |
| 3123 | { |
| 3124 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3125 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3126 | TestDelegate d; |
| 3127 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3128 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3129 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3130 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3131 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3132 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3133 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3134 | |
| 3135 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3136 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 3137 | } |
| 3138 | |
| 3139 | // Verify the cookies weren't saved or updated. |
| 3140 | { |
| 3141 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3142 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3143 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3144 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3145 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3146 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3147 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3148 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3149 | |
| 3150 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 3151 | == std::string::npos); |
| 3152 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 3153 | != std::string::npos); |
| 3154 | |
| 3155 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3156 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3157 | } |
| 3158 | } |
| 3159 | |
mkwst | c5fa776 | 2016-03-28 09:28:23 | [diff] [blame] | 3160 | TEST_F(URLRequestTest, SameSiteCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3161 | HttpTestServer test_server; |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3162 | ASSERT_TRUE(test_server.Start()); |
| 3163 | |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3164 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3165 | default_context().set_network_delegate(&network_delegate); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3166 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3167 | const std::string kHost = "example.test"; |
| 3168 | const std::string kSubHost = "subdomain.example.test"; |
| 3169 | const std::string kCrossHost = "cross-origin.test"; |
| 3170 | |
| 3171 | // Set up two 'SameSite' cookies on 'example.test' |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3172 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3173 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3174 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3175 | test_server.GetURL(kHost, |
| 3176 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 3177 | "LaxSameSiteCookie=1;SameSite=Lax"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3178 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3179 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
| 3180 | req->set_initiator(url::Origin::Create(test_server.GetURL(kHost, "/"))); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3181 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3182 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3183 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3184 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3185 | EXPECT_EQ(2, network_delegate.set_cookie_count()); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3186 | } |
| 3187 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3188 | // Verify that both cookies are sent for same-site requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3189 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3190 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3191 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3192 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3193 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3194 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3195 | req->set_initiator(url::Origin::Create(test_server.GetURL(kHost, "/"))); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3196 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3197 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3198 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3199 | EXPECT_NE(std::string::npos, |
| 3200 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3201 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3202 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3203 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3204 | } |
| 3205 | |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3206 | // Verify that both cookies are sent when the request has no initiator (can |
| 3207 | // happen for main frame browser-initiated navigations). |
| 3208 | { |
| 3209 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3210 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3211 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3212 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3213 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3214 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3215 | d.RunUntilComplete(); |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3216 | |
| 3217 | EXPECT_NE(std::string::npos, |
| 3218 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3219 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 3220 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3221 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3222 | } |
| 3223 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3224 | // Verify that both cookies are sent for same-registrable-domain requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3225 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3226 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3227 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3228 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3229 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3230 | req->set_site_for_cookies(test_server.GetURL(kSubHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3231 | req->set_initiator(url::Origin::Create(test_server.GetURL(kSubHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3232 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3233 | d.RunUntilComplete(); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3234 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3235 | EXPECT_NE(std::string::npos, |
| 3236 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3237 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3238 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3239 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3240 | } |
| 3241 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3242 | // Verify that neither cookie is not sent for cross-site requests. |
| 3243 | { |
| 3244 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3245 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3246 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3247 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3248 | req->set_site_for_cookies(test_server.GetURL(kCrossHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3249 | req->set_initiator( |
| 3250 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3251 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3252 | d.RunUntilComplete(); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3253 | |
| 3254 | EXPECT_EQ(std::string::npos, |
| 3255 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3256 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 3257 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3258 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3259 | } |
| 3260 | |
| 3261 | // Verify that the lax cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3262 | // method is "safe". |
| 3263 | { |
| 3264 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3265 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3266 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3267 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3268 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3269 | req->set_initiator( |
| 3270 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3271 | req->set_method("GET"); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3272 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3273 | d.RunUntilComplete(); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3274 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3275 | EXPECT_EQ(std::string::npos, |
| 3276 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3277 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3278 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3279 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3280 | } |
| 3281 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3282 | // Verify that neither cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3283 | // method is unsafe (e.g. POST). |
| 3284 | { |
| 3285 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3286 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3287 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3288 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3289 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3290 | req->set_initiator( |
| 3291 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3292 | req->set_method("POST"); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3293 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3294 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3295 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3296 | EXPECT_EQ(std::string::npos, |
| 3297 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3298 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3299 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3300 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3301 | } |
| 3302 | } |
| 3303 | |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3304 | TEST_F(URLRequestTest, SettingSameSiteCookies) { |
| 3305 | HttpTestServer test_server; |
| 3306 | ASSERT_TRUE(test_server.Start()); |
| 3307 | |
| 3308 | TestNetworkDelegate network_delegate; |
| 3309 | default_context().set_network_delegate(&network_delegate); |
| 3310 | |
| 3311 | const std::string kHost = "example.test"; |
| 3312 | const std::string kSubHost = "subdomain.example.test"; |
| 3313 | const std::string kCrossHost = "cross-origin.test"; |
| 3314 | |
| 3315 | int expected_cookies = 0; |
| 3316 | |
| 3317 | { |
| 3318 | TestDelegate d; |
| 3319 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 3320 | test_server.GetURL(kHost, |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3321 | "/set-cookie?Strict1=1;SameSite=Strict&" |
| 3322 | "Lax1=1;SameSite=Lax"), |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3323 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3324 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
| 3325 | req->set_initiator(url::Origin::Create(test_server.GetURL(kHost, "/"))); |
| 3326 | |
| 3327 | // 'SameSite' cookies are settable from strict same-site contexts |
| 3328 | // (same-origin site_for_cookies, same-origin initiator), so this request |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3329 | // should result in two cookies being set. |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3330 | expected_cookies += 2; |
| 3331 | |
| 3332 | req->Start(); |
| 3333 | d.RunUntilComplete(); |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3334 | EXPECT_EQ(expected_cookies, |
| 3335 | static_cast<int>(GetAllCookies(&default_context()).size())); |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3336 | EXPECT_EQ(expected_cookies, network_delegate.set_cookie_count()); |
| 3337 | } |
| 3338 | |
| 3339 | { |
| 3340 | TestDelegate d; |
| 3341 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 3342 | test_server.GetURL(kHost, |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3343 | "/set-cookie?Strict2=1;SameSite=Strict&" |
| 3344 | "Lax2=1;SameSite=Lax"), |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3345 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3346 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
| 3347 | req->set_initiator( |
| 3348 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
| 3349 | |
| 3350 | // 'SameSite' cookies are settable from lax same-site contexts (same-origin |
| 3351 | // site_for_cookies, cross-site initiator), so this request should result in |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3352 | // two cookies being set. |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3353 | expected_cookies += 2; |
| 3354 | |
| 3355 | req->Start(); |
| 3356 | d.RunUntilComplete(); |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3357 | EXPECT_EQ(expected_cookies, |
| 3358 | static_cast<int>(GetAllCookies(&default_context()).size())); |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3359 | EXPECT_EQ(expected_cookies, network_delegate.set_cookie_count()); |
| 3360 | } |
| 3361 | |
| 3362 | { |
| 3363 | TestDelegate d; |
| 3364 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 3365 | test_server.GetURL(kHost, |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3366 | "/set-cookie?Strict3=1;SameSite=Strict&" |
| 3367 | "Lax3=1;SameSite=Lax"), |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3368 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3369 | req->set_site_for_cookies(test_server.GetURL(kSubHost, "/")); |
| 3370 | req->set_initiator( |
| 3371 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
| 3372 | |
| 3373 | // 'SameSite' cookies are settable from lax same-site contexts (same-site |
| 3374 | // site_for_cookies, cross-site initiator), so this request should result in |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3375 | // two cookies being set. |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3376 | expected_cookies += 2; |
| 3377 | |
| 3378 | req->Start(); |
| 3379 | d.RunUntilComplete(); |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3380 | EXPECT_EQ(expected_cookies, |
| 3381 | static_cast<int>(GetAllCookies(&default_context()).size())); |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3382 | EXPECT_EQ(expected_cookies, network_delegate.set_cookie_count()); |
| 3383 | } |
| 3384 | |
| 3385 | { |
| 3386 | TestDelegate d; |
| 3387 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 3388 | test_server.GetURL(kHost, |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3389 | "/set-cookie?Strict4=1;SameSite=Strict&" |
| 3390 | "Lax4=1;SameSite=Lax"), |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3391 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3392 | req->set_site_for_cookies(test_server.GetURL(kSubHost, "/")); |
| 3393 | |
| 3394 | // 'SameSite' cookies are settable from strict same-site contexts (same-site |
| 3395 | // site_for_cookies, no initiator), so this request should result in two |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3396 | // cookies being set. |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3397 | expected_cookies += 2; |
| 3398 | |
| 3399 | req->Start(); |
| 3400 | d.RunUntilComplete(); |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3401 | EXPECT_EQ(expected_cookies, |
| 3402 | static_cast<int>(GetAllCookies(&default_context()).size())); |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3403 | EXPECT_EQ(expected_cookies, network_delegate.set_cookie_count()); |
| 3404 | } |
| 3405 | |
| 3406 | { |
| 3407 | TestDelegate d; |
| 3408 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 3409 | test_server.GetURL(kHost, |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3410 | "/set-cookie?Strict5=1;SameSite=Strict&" |
| 3411 | "Lax5=1;SameSite=Lax"), |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3412 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3413 | req->set_site_for_cookies(test_server.GetURL(kCrossHost, "/")); |
| 3414 | req->set_initiator( |
| 3415 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
| 3416 | |
| 3417 | // 'SameSite' cookies are not settable from cross-site contexts, so this |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3418 | // should not result in any new cookies being set. |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3419 | expected_cookies += 0; |
| 3420 | |
| 3421 | req->Start(); |
| 3422 | d.RunUntilComplete(); |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3423 | // This counts the number of cookies actually set. |
| 3424 | EXPECT_EQ(expected_cookies, |
| 3425 | static_cast<int>(GetAllCookies(&default_context()).size())); |
| 3426 | // This counts the number of successful calls to CanSetCookie() when |
| 3427 | // attempting to set a cookie. The two cookies above were created and |
| 3428 | // attempted to be set, and were not rejected by the NetworkDelegate, so the |
| 3429 | // count here is 2 more than the number of cookies actually set. |
| 3430 | EXPECT_EQ(expected_cookies + 2, network_delegate.set_cookie_count()); |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3431 | } |
| 3432 | } |
| 3433 | |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3434 | // Tests that __Secure- cookies can't be set on non-secure origins. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3435 | TEST_F(URLRequestTest, SecureCookiePrefixOnNonsecureOrigin) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3436 | EmbeddedTestServer http_server; |
| 3437 | http_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3438 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3439 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3440 | https_server.AddDefaultHandlers( |
| 3441 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3442 | ASSERT_TRUE(http_server.Start()); |
| 3443 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3444 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3445 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3446 | TestURLRequestContext context(true); |
| 3447 | context.set_network_delegate(&network_delegate); |
| 3448 | context.Init(); |
| 3449 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3450 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3451 | { |
| 3452 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3453 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3454 | http_server.GetURL("/set-cookie?__Secure-nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3455 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3456 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3457 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3458 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3459 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3460 | } |
| 3461 | |
| 3462 | // Verify that the cookie is not set. |
| 3463 | { |
| 3464 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3465 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3466 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3467 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3468 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3469 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3470 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3471 | EXPECT_EQ(d.data_received().find("__Secure-nonsecure-origin=1"), |
| 3472 | std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3473 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3474 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3475 | } |
| 3476 | } |
| 3477 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3478 | TEST_F(URLRequestTest, SecureCookiePrefixNonsecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3479 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3480 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3481 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3482 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3483 | |
| 3484 | TestNetworkDelegate network_delegate; |
| 3485 | TestURLRequestContext context(true); |
| 3486 | context.set_network_delegate(&network_delegate); |
| 3487 | context.Init(); |
| 3488 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3489 | // Try to set a non-Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3490 | { |
| 3491 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3492 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 3493 | https_server.GetURL("/set-cookie?__Secure-foo=1"), DEFAULT_PRIORITY, &d, |
| 3494 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3495 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3496 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3497 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3498 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3499 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3500 | } |
| 3501 | |
| 3502 | // Verify that the cookie is not set. |
| 3503 | { |
| 3504 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3505 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3506 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3507 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3508 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3509 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3510 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3511 | EXPECT_EQ(d.data_received().find("__Secure-foo=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3512 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3513 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3514 | } |
| 3515 | } |
| 3516 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3517 | TEST_F(URLRequestTest, SecureCookiePrefixSecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3518 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3519 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3520 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3521 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3522 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3523 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3524 | TestURLRequestContext context(true); |
| 3525 | context.set_network_delegate(&network_delegate); |
| 3526 | context.Init(); |
| 3527 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3528 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3529 | { |
| 3530 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3531 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3532 | https_server.GetURL("/set-cookie?__Secure-bar=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3533 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3534 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3535 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3536 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3537 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3538 | } |
| 3539 | |
| 3540 | // Verify that the cookie is set. |
| 3541 | { |
| 3542 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3543 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3544 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3545 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3546 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3547 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3548 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3549 | EXPECT_NE(d.data_received().find("__Secure-bar=1"), std::string::npos); |
| 3550 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3551 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3552 | } |
| 3553 | } |
| 3554 | |
| 3555 | // Tests that secure cookies can't be set on non-secure origins if strict secure |
| 3556 | // cookies are enabled. |
| 3557 | TEST_F(URLRequestTest, StrictSecureCookiesOnNonsecureOrigin) { |
| 3558 | EmbeddedTestServer http_server; |
| 3559 | http_server.AddDefaultHandlers( |
| 3560 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3561 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3562 | https_server.AddDefaultHandlers( |
| 3563 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3564 | ASSERT_TRUE(http_server.Start()); |
| 3565 | ASSERT_TRUE(https_server.Start()); |
| 3566 | |
Matt Menke | be9b6eb | 2018-10-18 12:11:44 | [diff] [blame] | 3567 | TestNetworkDelegate network_delegate; |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3568 | TestURLRequestContext context(true); |
| 3569 | context.set_network_delegate(&network_delegate); |
| 3570 | context.Init(); |
| 3571 | |
| 3572 | // Try to set a Secure cookie, with experimental features enabled. |
| 3573 | { |
| 3574 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3575 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3576 | http_server.GetURL("/set-cookie?nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3577 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3578 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3579 | d.RunUntilComplete(); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3580 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3581 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3582 | } |
| 3583 | |
| 3584 | // Verify that the cookie is not set. |
| 3585 | { |
| 3586 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3587 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3588 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3589 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3590 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3591 | d.RunUntilComplete(); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3592 | |
| 3593 | EXPECT_EQ(d.data_received().find("nonsecure-origin=1"), std::string::npos); |
| 3594 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3595 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3596 | } |
| 3597 | } |
| 3598 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3599 | // The parameter is true for same-site and false for cross-site requests. |
| 3600 | class URLRequestTestParameterizedSameSite |
| 3601 | : public URLRequestTest, |
| 3602 | public ::testing::WithParamInterface<bool> { |
| 3603 | protected: |
| 3604 | URLRequestTestParameterizedSameSite() { |
| 3605 | auto params = std::make_unique<HttpNetworkSession::Params>(); |
| 3606 | params->ignore_certificate_errors = true; |
| 3607 | context_.set_http_network_session_params(std::move(params)); |
| 3608 | context_.set_network_delegate(&network_delegate_); |
| 3609 | https_server_.AddDefaultHandlers( |
| 3610 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3611 | EXPECT_TRUE(https_server_.Start()); |
| 3612 | } |
| 3613 | |
| 3614 | // To be called after configuration of |context_| has been finalized. |
| 3615 | void InitContext() { context_.Init(); } |
| 3616 | |
| 3617 | const std::string kHost_ = "example.test"; |
| 3618 | const std::string kCrossHost_ = "cross-site.test"; |
| 3619 | TestURLRequestContext context_{true}; |
| 3620 | TestNetworkDelegate network_delegate_; |
| 3621 | base::HistogramTester histograms_; |
| 3622 | EmbeddedTestServer https_server_{EmbeddedTestServer::TYPE_HTTPS}; |
| 3623 | }; |
| 3624 | |
Victor Costan | 8fb98f6f | 2019-02-01 17:08:29 | [diff] [blame] | 3625 | INSTANTIATE_TEST_SUITE_P(URLRequestTest, |
| 3626 | URLRequestTestParameterizedSameSite, |
| 3627 | ::testing::Bool()); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3628 | |
| 3629 | TEST_P(URLRequestTestParameterizedSameSite, CookieAgeMetrics) { |
| 3630 | const bool same_site = GetParam(); |
| 3631 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3632 | InitContext(); |
| 3633 | |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3634 | EmbeddedTestServer http_server; |
| 3635 | http_server.AddDefaultHandlers( |
| 3636 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3637 | ASSERT_TRUE(http_server.Start()); |
| 3638 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3639 | // Set two test cookies. |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3640 | { |
| 3641 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3642 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3643 | http_server.GetURL(kHost_, "/set-cookie?cookie=value&cookie2=value2"), |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3644 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3645 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3646 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3647 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3648 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3649 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 0); |
| 3650 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", 0); |
| 3651 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", 0); |
| 3652 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3653 | 0); |
| 3654 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3655 | 0); |
| 3656 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", 0); |
| 3657 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", 0); |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3658 | } |
| 3659 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3660 | // Make a secure request. |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3661 | { |
| 3662 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3663 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3664 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3665 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3666 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3667 | req->set_initiator( |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3668 | url::Origin::Create(https_server_.GetURL(kInitiatingHost, "/"))); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3669 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3670 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3671 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3672 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 0); |
| 3673 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", |
| 3674 | !same_site); |
| 3675 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", |
| 3676 | same_site); |
| 3677 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3678 | 0); |
| 3679 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3680 | 0); |
| 3681 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", |
| 3682 | same_site ? 0 : 2); |
| 3683 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", |
| 3684 | same_site ? 2 : 0); |
| 3685 | EXPECT_TRUE(d.data_received().find("cookie=value") != std::string::npos); |
| 3686 | EXPECT_TRUE(d.data_received().find("cookie2=value2") != std::string::npos); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3687 | } |
| 3688 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3689 | // Make a non-secure request. |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3690 | { |
| 3691 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3692 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3693 | http_server.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3694 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3695 | req->set_site_for_cookies(http_server.GetURL(kInitiatingHost, "/")); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3696 | req->set_initiator( |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3697 | url::Origin::Create(http_server.GetURL(kInitiatingHost, "/"))); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3698 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3699 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3700 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", |
| 3701 | !same_site); |
| 3702 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", |
| 3703 | same_site); |
| 3704 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", |
| 3705 | !same_site); |
| 3706 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", |
| 3707 | same_site); |
| 3708 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3709 | same_site ? 0 : 2); |
| 3710 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3711 | same_site ? 2 : 0); |
| 3712 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", |
| 3713 | same_site ? 0 : 2); |
| 3714 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", |
| 3715 | same_site ? 2 : 0); |
| 3716 | EXPECT_TRUE(d.data_received().find("cookie=value") != std::string::npos); |
| 3717 | EXPECT_TRUE(d.data_received().find("cookie2=value2") != std::string::npos); |
| 3718 | } |
| 3719 | } |
| 3720 | |
| 3721 | // Cookies with secure attribute (no HSTS) --> k1pSecureAttribute |
| 3722 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3723 | CookieNetworkSecurityMetricSecureAttribute) { |
| 3724 | const bool same_site = GetParam(); |
| 3725 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3726 | InitContext(); |
| 3727 | |
| 3728 | // Set cookies. |
| 3729 | { |
| 3730 | TestDelegate d; |
| 3731 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3732 | https_server_.GetURL(kHost_, |
| 3733 | "/set-cookie?session-cookie=value;Secure&" |
| 3734 | "longlived-cookie=value;Secure;domain=" + |
| 3735 | kHost_ + ";Max-Age=360000"), |
| 3736 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3737 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3738 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3739 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3740 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3741 | } |
| 3742 | |
| 3743 | // Verify that the cookies fall into the correct metrics bucket. |
| 3744 | { |
| 3745 | TestDelegate d; |
| 3746 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3747 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3748 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3749 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3750 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3751 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3752 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3753 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3754 | // Static cast of boolean required for MSVC 1911. |
| 3755 | histograms_.ExpectBucketCount( |
| 3756 | "Cookie.NetworkSecurity", |
| 3757 | static_cast<int>(CookieNetworkSecurity::k1pSecureAttribute) | |
| 3758 | static_cast<int>(!same_site), |
| 3759 | 2); |
| 3760 | } |
| 3761 | } |
| 3762 | |
| 3763 | // Short-lived host cookie --> k1pHSTSHostCookie |
| 3764 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3765 | CookieNetworkSecurityMetricShortlivedHostCookie) { |
| 3766 | const bool same_site = GetParam(); |
| 3767 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3768 | |
| 3769 | TransportSecurityState transport_security_state; |
| 3770 | transport_security_state.AddHSTS( |
| 3771 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3772 | false /* include_subdomains */); |
| 3773 | context_.set_transport_security_state(&transport_security_state); |
| 3774 | InitContext(); |
| 3775 | |
| 3776 | // Set cookie. |
| 3777 | { |
| 3778 | TestDelegate d; |
| 3779 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3780 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;Max-Age=3600"), |
| 3781 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3782 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3783 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3784 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3785 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3786 | } |
| 3787 | |
| 3788 | // Verify that the cookie falls into the correct metrics bucket. |
| 3789 | { |
| 3790 | TestDelegate d; |
| 3791 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3792 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3793 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3794 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3795 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3796 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3797 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3798 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3799 | // Static cast of boolean required for MSVC 1911. |
| 3800 | histograms_.ExpectBucketCount( |
| 3801 | "Cookie.NetworkSecurity", |
| 3802 | static_cast<int>(CookieNetworkSecurity::k1pHSTSHostCookie) | |
| 3803 | static_cast<int>(!same_site), |
| 3804 | 1); |
| 3805 | } |
| 3806 | } |
| 3807 | |
| 3808 | // Long-lived (either due to expiry or due to being a session cookie) host |
| 3809 | // cookies --> k1pExpiringHSTSHostCookie |
| 3810 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3811 | CookieNetworkSecurityMetricLonglivedHostCookie) { |
| 3812 | const bool same_site = GetParam(); |
| 3813 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3814 | |
| 3815 | TransportSecurityState transport_security_state; |
| 3816 | transport_security_state.AddHSTS( |
| 3817 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3818 | false /* include_subdomains */); |
| 3819 | context_.set_transport_security_state(&transport_security_state); |
| 3820 | InitContext(); |
| 3821 | |
| 3822 | // Set cookies. |
| 3823 | { |
| 3824 | TestDelegate d; |
| 3825 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3826 | https_server_.GetURL(kHost_, |
| 3827 | "/set-cookie?session-cookie=value&" |
| 3828 | "longlived-cookie=value;Max-Age=360000"), |
| 3829 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3830 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3831 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3832 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3833 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3834 | } |
| 3835 | |
| 3836 | // Verify that the cookies fall into the correct metrics bucket. |
| 3837 | { |
| 3838 | TestDelegate d; |
| 3839 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3840 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3841 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3842 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3843 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3844 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3845 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3846 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3847 | // Static cast of boolean required for MSVC 1911. |
| 3848 | histograms_.ExpectBucketCount( |
| 3849 | "Cookie.NetworkSecurity", |
| 3850 | static_cast<int>(CookieNetworkSecurity::k1pExpiringHSTSHostCookie) | |
| 3851 | static_cast<int>(!same_site), |
| 3852 | 2); |
| 3853 | } |
| 3854 | } |
| 3855 | |
| 3856 | // Domain cookie with HSTS subdomains with cookie expiry before HSTS expiry --> |
| 3857 | // k1pHSTSSubdomainsIncluded |
| 3858 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3859 | CookieNetworkSecurityMetricShortlivedDomainCookie) { |
| 3860 | const bool same_site = GetParam(); |
| 3861 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3862 | |
| 3863 | TransportSecurityState transport_security_state; |
| 3864 | transport_security_state.AddHSTS( |
| 3865 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3866 | true /* include_subdomains */); |
| 3867 | context_.set_transport_security_state(&transport_security_state); |
| 3868 | InitContext(); |
| 3869 | |
| 3870 | // Set cookie. |
| 3871 | { |
| 3872 | TestDelegate d; |
| 3873 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3874 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;domain=" + |
| 3875 | kHost_ + ";Max-Age=3600"), |
| 3876 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3877 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3878 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3879 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3880 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3881 | } |
| 3882 | |
| 3883 | // Verify that the cookie falls into the correct metrics bucket. |
| 3884 | { |
| 3885 | TestDelegate d; |
| 3886 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3887 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3888 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3889 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3890 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3891 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3892 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3893 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3894 | // Static cast of boolean required for MSVC 1911. |
| 3895 | histograms_.ExpectBucketCount( |
| 3896 | "Cookie.NetworkSecurity", |
| 3897 | static_cast<int>(CookieNetworkSecurity::k1pHSTSSubdomainsIncluded) | |
| 3898 | static_cast<int>(!same_site), |
| 3899 | 1); |
| 3900 | } |
| 3901 | } |
| 3902 | |
| 3903 | // Long-lived (either due to expiry or due to being a session cookie) domain |
| 3904 | // cookies with HSTS subdomains --> k1pExpiringHSTSSubdomainsIncluded |
| 3905 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3906 | CookieNetworkSecurityMetricLonglivedDomainCookie) { |
| 3907 | const bool same_site = GetParam(); |
| 3908 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3909 | |
| 3910 | TransportSecurityState transport_security_state; |
| 3911 | transport_security_state.AddHSTS( |
| 3912 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3913 | true /* include_subdomains */); |
| 3914 | context_.set_transport_security_state(&transport_security_state); |
| 3915 | InitContext(); |
| 3916 | |
| 3917 | // Set cookies. |
| 3918 | { |
| 3919 | TestDelegate d; |
| 3920 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3921 | https_server_.GetURL( |
| 3922 | kHost_, "/set-cookie?session-cookie=value;domain=" + kHost_ + "&" + |
| 3923 | "longlived-cookie=value;domain=" + kHost_ + |
| 3924 | ";Max-Age=360000"), |
| 3925 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3926 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3927 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3928 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3929 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3930 | } |
| 3931 | |
| 3932 | // Verify that the cookies fall into the correct metrics bucket. |
| 3933 | { |
| 3934 | TestDelegate d; |
| 3935 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3936 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3937 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3938 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3939 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3940 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3941 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3942 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3943 | // Static cast of boolean required for MSVC 1911. |
| 3944 | histograms_.ExpectBucketCount( |
| 3945 | "Cookie.NetworkSecurity", |
| 3946 | static_cast<int>( |
| 3947 | CookieNetworkSecurity::k1pExpiringHSTSSubdomainsIncluded) | |
| 3948 | static_cast<int>(!same_site), |
| 3949 | 2); |
| 3950 | } |
| 3951 | } |
| 3952 | |
| 3953 | // Domain cookie with HSTS subdomains not included --> k1pHSTSSpoofable |
| 3954 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3955 | CookieNetworkSecurityMetricSpoofableDomainCookie) { |
| 3956 | const bool same_site = GetParam(); |
| 3957 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3958 | |
| 3959 | TransportSecurityState transport_security_state; |
| 3960 | transport_security_state.AddHSTS( |
| 3961 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3962 | false /* include_subdomains */); |
| 3963 | context_.set_transport_security_state(&transport_security_state); |
| 3964 | InitContext(); |
| 3965 | |
| 3966 | // Set cookie. |
| 3967 | { |
| 3968 | TestDelegate d; |
| 3969 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3970 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;domain=" + |
| 3971 | kHost_ + ";Max-Age=3600"), |
| 3972 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3973 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3974 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3975 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3976 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3977 | } |
| 3978 | |
| 3979 | // Verify that the cookie falls into the correct metrics bucket. |
| 3980 | { |
| 3981 | TestDelegate d; |
| 3982 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3983 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3984 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3985 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3986 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3987 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3988 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3989 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3990 | // Static cast of boolean required for MSVC 1911. |
| 3991 | histograms_.ExpectBucketCount( |
| 3992 | "Cookie.NetworkSecurity", |
| 3993 | static_cast<int>(CookieNetworkSecurity::k1pHSTSSpoofable) | |
| 3994 | static_cast<int>(!same_site), |
| 3995 | 1); |
| 3996 | } |
| 3997 | } |
| 3998 | |
| 3999 | // Cookie without HSTS --> k1p(Non)SecureConnection |
| 4000 | TEST_P(URLRequestTestParameterizedSameSite, CookieNetworkSecurityMetricNoHSTS) { |
| 4001 | const bool same_site = GetParam(); |
| 4002 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 4003 | InitContext(); |
| 4004 | |
| 4005 | EmbeddedTestServer http_server; |
| 4006 | http_server.AddDefaultHandlers( |
| 4007 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 4008 | ASSERT_TRUE(http_server.Start()); |
| 4009 | |
| 4010 | // Set cookies. |
| 4011 | { |
| 4012 | TestDelegate d; |
| 4013 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 4014 | https_server_.GetURL(kHost_, |
| 4015 | "/set-cookie?cookie=value;domain=" + kHost_ + |
| 4016 | ";Max-Age=3600&host-cookie=value"), |
| 4017 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 4018 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4019 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 4020 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 4021 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 4022 | } |
| 4023 | |
| 4024 | // Verify that the cookie falls into the correct metrics bucket. |
| 4025 | { |
| 4026 | TestDelegate d; |
| 4027 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 4028 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 4029 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 4030 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 4031 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 4032 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4033 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 4034 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 4035 | // Static cast of boolean required for MSVC 1911. |
| 4036 | histograms_.ExpectBucketCount( |
| 4037 | "Cookie.NetworkSecurity", |
| 4038 | static_cast<int>(CookieNetworkSecurity::k1pSecureConnection) | |
| 4039 | static_cast<int>(!same_site), |
| 4040 | 2); |
| 4041 | } |
| 4042 | |
| 4043 | // Verify that the cookie falls into the correct metrics bucket. |
| 4044 | { |
| 4045 | TestDelegate d; |
| 4046 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 4047 | http_server.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 4048 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 4049 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 4050 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 4051 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4052 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 4053 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 4); |
| 4054 | // Static cast of boolean required for MSVC 1911. |
| 4055 | histograms_.ExpectBucketCount( |
| 4056 | "Cookie.NetworkSecurity", |
| 4057 | static_cast<int>(CookieNetworkSecurity::k1pSecureConnection) | |
| 4058 | static_cast<int>(!same_site), |
| 4059 | 2); |
| 4060 | // Static cast of boolean required for MSVC 1911. |
| 4061 | histograms_.ExpectBucketCount( |
| 4062 | "Cookie.NetworkSecurity", |
| 4063 | static_cast<int>(CookieNetworkSecurity::k1pNonsecureConnection) | |
| 4064 | static_cast<int>(!same_site), |
| 4065 | 2); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 4066 | } |
| 4067 | } |
| 4068 | |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4069 | // FixedDateNetworkDelegate swaps out the server's HTTP Date response header |
| 4070 | // value for the |fixed_date| argument given to the constructor. |
| 4071 | class FixedDateNetworkDelegate : public TestNetworkDelegate { |
| 4072 | public: |
| 4073 | explicit FixedDateNetworkDelegate(const std::string& fixed_date) |
| 4074 | : fixed_date_(fixed_date) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 4075 | ~FixedDateNetworkDelegate() override = default; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4076 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4077 | // NetworkDelegate implementation |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4078 | int OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4079 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 4080 | CompletionOnceCallback callback, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4081 | const HttpResponseHeaders* original_response_headers, |
| 4082 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 4083 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4084 | |
| 4085 | private: |
| 4086 | std::string fixed_date_; |
| 4087 | |
| 4088 | DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); |
| 4089 | }; |
| 4090 | |
| 4091 | int FixedDateNetworkDelegate::OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4092 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 4093 | CompletionOnceCallback callback, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4094 | const HttpResponseHeaders* original_response_headers, |
| 4095 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4096 | GURL* allowed_unsafe_redirect_url) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4097 | HttpResponseHeaders* new_response_headers = |
| 4098 | new HttpResponseHeaders(original_response_headers->raw_headers()); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4099 | |
| 4100 | new_response_headers->RemoveHeader("Date"); |
| 4101 | new_response_headers->AddHeader("Date: " + fixed_date_); |
| 4102 | |
| 4103 | *override_response_headers = new_response_headers; |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 4104 | return TestNetworkDelegate::OnHeadersReceived( |
| 4105 | request, std::move(callback), original_response_headers, |
| 4106 | override_response_headers, allowed_unsafe_redirect_url); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4107 | } |
| 4108 | |
| 4109 | // Test that cookie expiration times are adjusted for server/client clock |
| 4110 | // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date |
| 4111 | // headers by defaulting to GMT. (crbug.com/135131) |
| 4112 | TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4113 | HttpTestServer test_server; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4114 | ASSERT_TRUE(test_server.Start()); |
| 4115 | |
| 4116 | // Set up an expired cookie. |
| 4117 | { |
| 4118 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4119 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4120 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4121 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4122 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4123 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4124 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4125 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4126 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4127 | } |
| 4128 | // Verify that the cookie is not set. |
| 4129 | { |
| 4130 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4131 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4132 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4133 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4134 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 4135 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4136 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4137 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4138 | |
| 4139 | EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos); |
| 4140 | } |
| 4141 | // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier. |
| 4142 | { |
| 4143 | FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC"); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4144 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4145 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4146 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4147 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4148 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4149 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4150 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4151 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4152 | } |
| 4153 | // Verify that the cookie is set. |
| 4154 | { |
| 4155 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4156 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4157 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4158 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4159 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 4160 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4161 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4162 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4163 | |
| 4164 | EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos); |
| 4165 | } |
| 4166 | } |
| 4167 | |
| 4168 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4169 | // Check that it is impossible to change the referrer in the extra headers of |
| 4170 | // an URLRequest. |
| 4171 | TEST_F(URLRequestTest, DoNotOverrideReferrer) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4172 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4173 | ASSERT_TRUE(test_server.Start()); |
| 4174 | |
| 4175 | // If extra headers contain referer and the request contains a referer, |
| 4176 | // only the latter shall be respected. |
| 4177 | { |
| 4178 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4179 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4180 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 4181 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4182 | req->SetReferrer("http://foo.com/"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4183 | |
| 4184 | HttpRequestHeaders headers; |
| 4185 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4186 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4187 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4188 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4189 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4190 | |
| 4191 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 4192 | } |
| 4193 | |
| 4194 | // If extra headers contain a referer but the request does not, no referer |
| 4195 | // shall be sent in the header. |
| 4196 | { |
| 4197 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4198 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4199 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 4200 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4201 | |
| 4202 | HttpRequestHeaders headers; |
| 4203 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4204 | req->SetExtraRequestHeaders(headers); |
| 4205 | req->SetLoadFlags(LOAD_VALIDATE_CACHE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4206 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4207 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4208 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4209 | |
| 4210 | EXPECT_EQ("None", d.data_received()); |
| 4211 | } |
| 4212 | } |
| 4213 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4214 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4215 | public: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 4216 | URLRequestTestHTTP() : test_server_(base::FilePath(kTestFilePath)) {} |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4217 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4218 | protected: |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4219 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 4220 | // |request_method| is the method to use for the initial request. |
| 4221 | // |redirect_method| is the method that is expected to be used for the second |
| 4222 | // request, after redirection. |
| 4223 | // If |include_data| is true, data is uploaded with the request. The |
| 4224 | // response body is expected to match it exactly, if and only if |
| 4225 | // |request_method| == |redirect_method|. |
| 4226 | void HTTPRedirectMethodTest(const GURL& redirect_url, |
| 4227 | const std::string& request_method, |
| 4228 | const std::string& redirect_method, |
| 4229 | bool include_data) { |
| 4230 | static const char kData[] = "hello world"; |
| 4231 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4232 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4233 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4234 | req->set_method(request_method); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4235 | if (include_data) { |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4236 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4237 | HttpRequestHeaders headers; |
| 4238 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 4239 | base::NumberToString(base::size(kData) - 1)); |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 4240 | headers.SetHeader(HttpRequestHeaders::kContentType, "text/plain"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4241 | req->SetExtraRequestHeaders(headers); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4242 | } |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4243 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4244 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4245 | EXPECT_EQ(redirect_method, req->method()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4246 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4247 | if (include_data) { |
| 4248 | if (request_method == redirect_method) { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 4249 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 4250 | HttpRequestHeaders::kContentLength)); |
| 4251 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 4252 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4253 | EXPECT_EQ(kData, d.data_received()); |
| 4254 | } else { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 4255 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 4256 | HttpRequestHeaders::kContentLength)); |
| 4257 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 4258 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4259 | EXPECT_NE(kData, d.data_received()); |
| 4260 | } |
| 4261 | } |
| 4262 | if (HasFailure()) |
| 4263 | LOG(WARNING) << "Request method was: " << request_method; |
| 4264 | } |
| 4265 | |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4266 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. It's also |
| 4267 | // used as the initial origin. |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4268 | // |request_method| is the method to use for the initial request. |
| 4269 | // |redirect_method| is the method that is expected to be used for the second |
| 4270 | // request, after redirection. |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4271 | // |expected_origin_value| is the expected value for the Origin header after |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4272 | // redirection. If empty, expects that there will be no Origin header. |
| 4273 | void HTTPRedirectOriginHeaderTest(const GURL& redirect_url, |
| 4274 | const std::string& request_method, |
| 4275 | const std::string& redirect_method, |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4276 | const std::string& expected_origin_value) { |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4277 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4278 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4279 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4280 | req->set_method(request_method); |
| 4281 | req->SetExtraRequestHeaderByName(HttpRequestHeaders::kOrigin, |
| 4282 | redirect_url.GetOrigin().spec(), false); |
| 4283 | req->Start(); |
| 4284 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4285 | d.RunUntilComplete(); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4286 | |
| 4287 | EXPECT_EQ(redirect_method, req->method()); |
| 4288 | // Note that there is no check for request success here because, for |
| 4289 | // purposes of testing, the request very well may fail. For example, if the |
| 4290 | // test redirects to an HTTPS server from an HTTP origin, thus it is cross |
| 4291 | // origin, there is not an HTTPS server in this unit test framework, so the |
| 4292 | // request would fail. However, that's fine, as long as the request headers |
| 4293 | // are in order and pass the checks below. |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4294 | if (expected_origin_value.empty()) { |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4295 | EXPECT_FALSE( |
| 4296 | req->extra_request_headers().HasHeader(HttpRequestHeaders::kOrigin)); |
| 4297 | } else { |
| 4298 | std::string origin_header; |
| 4299 | EXPECT_TRUE(req->extra_request_headers().GetHeader( |
| 4300 | HttpRequestHeaders::kOrigin, &origin_header)); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4301 | EXPECT_EQ(expected_origin_value, origin_header); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4302 | } |
| 4303 | } |
| 4304 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4305 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4306 | const int kMsgSize = 20000; // multiple of 10 |
| 4307 | const int kIterations = 50; |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 4308 | char* uploadBytes = new char[kMsgSize+1]; |
| 4309 | char* ptr = uploadBytes; |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4310 | char marker = 'a'; |
| 4311 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 4312 | memcpy(ptr, "----------", 10); |
| 4313 | ptr += 10; |
| 4314 | if (idx % 100 == 0) { |
| 4315 | ptr--; |
| 4316 | *ptr++ = marker; |
| 4317 | if (++marker > 'z') |
| 4318 | marker = 'a'; |
| 4319 | } |
| 4320 | } |
| 4321 | uploadBytes[kMsgSize] = '\0'; |
| 4322 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4323 | for (int i = 0; i < kIterations; ++i) { |
| 4324 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4325 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4326 | test_server_.GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 4327 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4328 | r->set_method(method.c_str()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4329 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4330 | r->set_upload(CreateSimpleUploadData(uploadBytes)); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4331 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4332 | r->Start(); |
| 4333 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4334 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4335 | d.RunUntilComplete(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4336 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4337 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 4338 | << d.request_status(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4339 | |
| 4340 | EXPECT_FALSE(d.received_data_before_response()); |
| 4341 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4342 | } |
| 4343 | delete[] uploadBytes; |
| 4344 | } |
| 4345 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 4346 | bool DoManyCookiesRequest(int num_cookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4347 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4348 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4349 | test_server_.GetURL("/set-many-cookies?" + |
Raul Tambre | 8c1981d | 2019-02-08 02:22:26 | [diff] [blame] | 4350 | base::NumberToString(num_cookies)), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4351 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4352 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4353 | r->Start(); |
| 4354 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4355 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4356 | d.RunUntilComplete(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4357 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4358 | if (d.request_status() != OK) { |
| 4359 | EXPECT_EQ(ERR_RESPONSE_HEADERS_TOO_BIG, d.request_status()); |
| 4360 | return false; |
| 4361 | } |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4362 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4363 | return true; |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4364 | } |
| 4365 | |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4366 | HttpTestServer* http_test_server() { return &test_server_; } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 4367 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4368 | private: |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4369 | HttpTestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4370 | }; |
| 4371 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4372 | namespace { |
| 4373 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4374 | std::unique_ptr<test_server::HttpResponse> HandleRedirectConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4375 | const test_server::HttpRequest& request) { |
| 4376 | if (request.headers.find("Host") == request.headers.end() || |
| 4377 | request.headers.at("Host") != "www.redirect.com" || |
| 4378 | request.method != test_server::METHOD_CONNECT) { |
| 4379 | return nullptr; |
| 4380 | } |
| 4381 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4382 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4383 | new test_server::BasicHttpResponse); |
| 4384 | http_response->set_code(HTTP_FOUND); |
| 4385 | http_response->AddCustomHeader("Location", |
| 4386 | "http://www.destination.com/foo.js"); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 4387 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4388 | } |
| 4389 | |
Nick Harper | 89bc721 | 2018-07-31 19:07:57 | [diff] [blame] | 4390 | } // namespace |
| 4391 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4392 | // In this unit test, we're using the HTTPTestServer as a proxy server and |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4393 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4394 | // The EmbeddedTestServer will return a 302 response, which we should not |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4395 | // follow. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 4396 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4397 | http_test_server()->RegisterRequestHandler( |
Matt Menke | 2b2a6047 | 2018-02-07 02:12:12 | [diff] [blame] | 4398 | base::BindRepeating(&HandleRedirectConnect)); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4399 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4400 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4401 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4402 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4403 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4404 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4405 | TestDelegate d; |
| 4406 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4407 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4408 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 4409 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4410 | r->Start(); |
| 4411 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4412 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4413 | d.RunUntilComplete(); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4414 | |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 4415 | // The proxy server should be set before failure. |
| 4416 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4417 | http_test_server()->host_port_pair()), |
| 4418 | r->proxy_server()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4419 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4420 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4421 | // We should not have followed the redirect. |
| 4422 | EXPECT_EQ(0, d.received_redirect_count()); |
| 4423 | } |
| 4424 | } |
| 4425 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4426 | // This is the same as the previous test, but checks that the network delegate |
| 4427 | // registers the error. |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 4428 | TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4429 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4430 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4431 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4432 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4433 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4434 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4435 | TestDelegate d; |
| 4436 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4437 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4438 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 4439 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4440 | r->Start(); |
| 4441 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4442 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4443 | d.RunUntilComplete(); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4444 | |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 4445 | // The proxy server should be set before failure. |
| 4446 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4447 | http_test_server()->host_port_pair()), |
| 4448 | r->proxy_server()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4449 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4450 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4451 | // We should not have followed the redirect. |
| 4452 | EXPECT_EQ(0, d.received_redirect_count()); |
| 4453 | |
| 4454 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4455 | EXPECT_THAT(network_delegate.last_error(), |
| 4456 | IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4457 | } |
| 4458 | } |
| 4459 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4460 | // Tests that we can block and asynchronously return OK in various stages. |
| 4461 | TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) { |
| 4462 | static const BlockingNetworkDelegate::Stage blocking_stages[] = { |
| 4463 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 4464 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 4465 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED |
| 4466 | }; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 4467 | static const size_t blocking_stages_length = base::size(blocking_stages); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4468 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4469 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4470 | |
| 4471 | TestDelegate d; |
| 4472 | BlockingNetworkDelegate network_delegate( |
| 4473 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4474 | network_delegate.set_block_on( |
| 4475 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST | |
| 4476 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS | |
| 4477 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 4478 | |
| 4479 | TestURLRequestContext context(true); |
| 4480 | context.set_network_delegate(&network_delegate); |
| 4481 | context.Init(); |
| 4482 | |
| 4483 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4484 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4485 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4486 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4487 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4488 | r->Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4489 | for (size_t i = 0; i < blocking_stages_length; ++i) { |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4490 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4491 | EXPECT_EQ(blocking_stages[i], |
| 4492 | network_delegate.stage_blocked_for_callback()); |
| 4493 | network_delegate.DoCallback(OK); |
| 4494 | } |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4495 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4496 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4497 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4498 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4499 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4500 | } |
| 4501 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4502 | } |
| 4503 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4504 | // Tests that the network delegate can block and cancel a request. |
| 4505 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4506 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4507 | |
| 4508 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4509 | BlockingNetworkDelegate network_delegate( |
| 4510 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4511 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 4512 | network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4513 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4514 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4515 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4516 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4517 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4518 | std::unique_ptr<URLRequest> r( |
| 4519 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4520 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4521 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4522 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4523 | d.RunUntilComplete(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4524 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4525 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4526 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4527 | EXPECT_EQ(ERR_EMPTY_RESPONSE, d.request_status()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4528 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4529 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4530 | } |
| 4531 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4532 | } |
| 4533 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4534 | // Helper function for NetworkDelegateCancelRequestAsynchronously and |
| 4535 | // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network |
| 4536 | // delegate operating in |block_mode| and a request for |url|. It blocks the |
| 4537 | // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT. |
| 4538 | void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode, |
| 4539 | BlockingNetworkDelegate::Stage stage, |
| 4540 | const GURL& url) { |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4541 | TestDelegate d; |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4542 | BlockingNetworkDelegate network_delegate(block_mode); |
| 4543 | network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT); |
| 4544 | network_delegate.set_block_on(stage); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4545 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4546 | TestURLRequestContext context(true); |
| 4547 | context.set_network_delegate(&network_delegate); |
| 4548 | context.Init(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4549 | |
| 4550 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4551 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4552 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4553 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4554 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4555 | d.RunUntilComplete(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4556 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4557 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4558 | if (stage == BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST || |
| 4559 | stage == BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS) { |
| 4560 | EXPECT_FALSE(r->proxy_server().is_valid()); |
| 4561 | } else if (stage == BlockingNetworkDelegate::ON_HEADERS_RECEIVED) { |
| 4562 | EXPECT_TRUE(r->proxy_server().is_direct()); |
| 4563 | } else { |
| 4564 | NOTREACHED(); |
| 4565 | } |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4566 | EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, d.request_status()); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4567 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4568 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4569 | } |
| 4570 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4571 | } |
| 4572 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4573 | // The following 3 tests check that the network delegate can cancel a request |
| 4574 | // synchronously in various stages of the request. |
| 4575 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4576 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4577 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4578 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4579 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4580 | } |
| 4581 | |
| 4582 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4583 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4584 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4585 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4586 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4587 | } |
| 4588 | |
| 4589 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4590 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4591 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4592 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4593 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4594 | } |
| 4595 | |
| 4596 | // The following 3 tests check that the network delegate can cancel a request |
| 4597 | // asynchronously in various stages of the request. |
| 4598 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4599 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4600 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4601 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4602 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4603 | } |
| 4604 | |
| 4605 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4606 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4607 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4608 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4609 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4610 | } |
| 4611 | |
| 4612 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4613 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4614 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4615 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4616 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4617 | } |
| 4618 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4619 | // Tests that the network delegate can block and redirect a request to a new |
| 4620 | // URL. |
| 4621 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4622 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4623 | |
| 4624 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4625 | BlockingNetworkDelegate network_delegate( |
| 4626 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4627 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4628 | GURL redirect_url("http://does.not.resolve.test/simple.html"); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4629 | network_delegate.set_redirect_url(redirect_url); |
| 4630 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4631 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4632 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4633 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4634 | { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4635 | GURL original_url("http://does.not.resolve.test/defaultresponse"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4636 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4637 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4638 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4639 | // Quit after hitting the redirect, so can check the headers. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4640 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 4641 | d.RunUntilRedirect(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4642 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4643 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4644 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4645 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4646 | std::string location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4647 | ASSERT_TRUE( |
| 4648 | r->response_headers()->EnumerateHeader(nullptr, "Location", &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4649 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4650 | |
| 4651 | // Let the request finish. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 4652 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 4653 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4654 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4655 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4656 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4657 | http_test_server()->host_port_pair()), |
| 4658 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4659 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4660 | // through an untunneled proxy. |
| 4661 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4662 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4663 | http_test_server()->host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 4664 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4665 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4666 | EXPECT_EQ(redirect_url, r->url()); |
| 4667 | EXPECT_EQ(original_url, r->original_url()); |
| 4668 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4669 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4670 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4671 | } |
| 4672 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4673 | } |
| 4674 | |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4675 | // Tests that the network delegate can block and redirect a request to a new |
| 4676 | // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 4677 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4678 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4679 | |
| 4680 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4681 | BlockingNetworkDelegate network_delegate( |
| 4682 | BlockingNetworkDelegate::SYNCHRONOUS); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4683 | GURL redirect_url("http://does.not.resolve.test/simple.html"); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4684 | network_delegate.set_redirect_url(redirect_url); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4685 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4686 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4687 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4688 | |
| 4689 | { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4690 | GURL original_url("http://does.not.resolve.test/defaultresponse"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4691 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4692 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4693 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4694 | // Quit after hitting the redirect, so can check the headers. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4695 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 4696 | d.RunUntilRedirect(); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4697 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4698 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4699 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4700 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4701 | std::string location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4702 | ASSERT_TRUE( |
| 4703 | r->response_headers()->EnumerateHeader(nullptr, "Location", &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4704 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4705 | |
| 4706 | // Let the request finish. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 4707 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 4708 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4709 | d.RunUntilComplete(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4710 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4711 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4712 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4713 | http_test_server()->host_port_pair()), |
| 4714 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4715 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4716 | // through an untunneled proxy. |
| 4717 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4718 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4719 | http_test_server()->host_port_pair())); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4720 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4721 | EXPECT_EQ(redirect_url, r->url()); |
| 4722 | EXPECT_EQ(original_url, r->original_url()); |
| 4723 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4724 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4725 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4726 | } |
| 4727 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4728 | } |
| 4729 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4730 | // Tests that redirects caused by the network delegate preserve POST data. |
| 4731 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4732 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4733 | |
| 4734 | const char kData[] = "hello world"; |
| 4735 | |
| 4736 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4737 | BlockingNetworkDelegate network_delegate( |
| 4738 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4739 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4740 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4741 | network_delegate.set_redirect_url(redirect_url); |
| 4742 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4743 | TestURLRequestContext context(true); |
| 4744 | context.set_network_delegate(&network_delegate); |
| 4745 | context.Init(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4746 | |
| 4747 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4748 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4749 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4750 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4751 | r->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4752 | r->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4753 | HttpRequestHeaders headers; |
| 4754 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 4755 | base::NumberToString(base::size(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4756 | r->SetExtraRequestHeaders(headers); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4757 | |
| 4758 | // Quit after hitting the redirect, so can check the headers. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4759 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 4760 | d.RunUntilRedirect(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4761 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4762 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4763 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4764 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4765 | std::string location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4766 | ASSERT_TRUE( |
| 4767 | r->response_headers()->EnumerateHeader(nullptr, "Location", &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4768 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4769 | |
| 4770 | // Let the request finish. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 4771 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 4772 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4773 | d.RunUntilComplete(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4774 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4775 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4776 | EXPECT_EQ(redirect_url, r->url()); |
| 4777 | EXPECT_EQ(original_url, r->original_url()); |
| 4778 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4779 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4780 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4781 | EXPECT_EQ("POST", r->method()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4782 | EXPECT_EQ(kData, d.data_received()); |
| 4783 | } |
| 4784 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4785 | } |
| 4786 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4787 | // Tests that the network delegate can block and redirect a request to a new |
| 4788 | // URL during OnHeadersReceived. |
| 4789 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4790 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4791 | |
| 4792 | TestDelegate d; |
| 4793 | BlockingNetworkDelegate network_delegate( |
| 4794 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4795 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4796 | GURL redirect_url("http://does.not.resolve.test/simple.html"); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4797 | network_delegate.set_redirect_on_headers_received_url(redirect_url); |
| 4798 | |
| 4799 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4800 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4801 | |
| 4802 | { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4803 | GURL original_url("http://does.not.resolve.test/defaultresponse"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4804 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4805 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4806 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4807 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4808 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4809 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4810 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4811 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4812 | http_test_server()->host_port_pair()), |
| 4813 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4814 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4815 | // through an untunneled proxy. |
| 4816 | EXPECT_EQ(2, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4817 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4818 | http_test_server()->host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4819 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4820 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4821 | EXPECT_EQ(redirect_url, r->url()); |
| 4822 | EXPECT_EQ(original_url, r->original_url()); |
| 4823 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4824 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 4825 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4826 | } |
| 4827 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4828 | } |
| 4829 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4830 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4831 | // by taking no action. This indicates that the NetworkDelegate does not want to |
| 4832 | // handle the challenge, and is passing the buck along to the |
| 4833 | // URLRequest::Delegate. |
| 4834 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4835 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4836 | |
| 4837 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4838 | BlockingNetworkDelegate network_delegate( |
| 4839 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4840 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4841 | TestURLRequestContext context(true); |
| 4842 | context.set_network_delegate(&network_delegate); |
| 4843 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4844 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4845 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4846 | |
| 4847 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4848 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4849 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4850 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4851 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4852 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4853 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4854 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4855 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4856 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4857 | EXPECT_TRUE(d.auth_required_called()); |
| 4858 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4859 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4860 | } |
| 4861 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4862 | } |
| 4863 | |
| 4864 | TEST_F(URLRequestTestHTTP, |
| 4865 | NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4866 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4867 | |
| 4868 | TestDelegate d; |
| 4869 | BlockingNetworkDelegate network_delegate( |
| 4870 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4871 | |
| 4872 | TestURLRequestContext context(true); |
| 4873 | context.set_network_delegate(&network_delegate); |
| 4874 | context.Init(); |
| 4875 | |
| 4876 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 4877 | |
| 4878 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4879 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4880 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4881 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4882 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4883 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4884 | d.RunUntilComplete(); |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4885 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4886 | { |
| 4887 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4888 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4889 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4890 | } |
| 4891 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4892 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4893 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4894 | EXPECT_TRUE(d.auth_required_called()); |
| 4895 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4896 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4897 | } |
| 4898 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4899 | } |
| 4900 | |
| 4901 | // Tests that the network delegate can synchronously complete OnAuthRequired |
[email protected] | 1e110eae | 2013-05-10 22:02:40 | [diff] [blame] | 4902 | // by setting credentials. |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4903 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4904 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4905 | |
| 4906 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4907 | BlockingNetworkDelegate network_delegate( |
| 4908 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4909 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4910 | network_delegate.set_auth_retval( |
| 4911 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4912 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4913 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4914 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4915 | TestURLRequestContext context(true); |
| 4916 | context.set_network_delegate(&network_delegate); |
| 4917 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4918 | |
| 4919 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4920 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4921 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4922 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4923 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4924 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4925 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4926 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4927 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4928 | EXPECT_FALSE(d.auth_required_called()); |
| 4929 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4930 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4931 | } |
| 4932 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4933 | } |
| 4934 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4935 | // Same as above, but also tests that GetFullRequestHeaders returns the proper |
| 4936 | // headers (for the first or second request) when called at the proper times. |
| 4937 | TEST_F(URLRequestTestHTTP, |
| 4938 | NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4939 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4940 | |
| 4941 | TestDelegate d; |
| 4942 | BlockingNetworkDelegate network_delegate( |
| 4943 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4944 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 4945 | network_delegate.set_auth_retval( |
| 4946 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4947 | |
| 4948 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
| 4949 | |
| 4950 | TestURLRequestContext context(true); |
| 4951 | context.set_network_delegate(&network_delegate); |
| 4952 | context.Init(); |
| 4953 | |
| 4954 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4955 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4956 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4957 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4958 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4959 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4960 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4961 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4962 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4963 | EXPECT_FALSE(d.auth_required_called()); |
| 4964 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4965 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4966 | |
| 4967 | { |
| 4968 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4969 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4970 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
| 4971 | } |
| 4972 | } |
| 4973 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4974 | } |
| 4975 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4976 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4977 | // by cancelling authentication. |
| 4978 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4979 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4980 | |
| 4981 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4982 | BlockingNetworkDelegate network_delegate( |
| 4983 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4984 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4985 | network_delegate.set_auth_retval( |
| 4986 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4987 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4988 | TestURLRequestContext context(true); |
| 4989 | context.set_network_delegate(&network_delegate); |
| 4990 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4991 | |
| 4992 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4993 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4994 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4995 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4996 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4997 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4998 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4999 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5000 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5001 | EXPECT_FALSE(d.auth_required_called()); |
| 5002 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5003 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5004 | } |
| 5005 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5006 | } |
| 5007 | |
| 5008 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 5009 | // by taking no action. This indicates that the NetworkDelegate does not want |
| 5010 | // to handle the challenge, and is passing the buck along to the |
| 5011 | // URLRequest::Delegate. |
| 5012 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5013 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5014 | |
| 5015 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5016 | BlockingNetworkDelegate network_delegate( |
| 5017 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 5018 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5019 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5020 | TestURLRequestContext context(true); |
| 5021 | context.set_network_delegate(&network_delegate); |
| 5022 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5023 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 5024 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5025 | |
| 5026 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5027 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5028 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5029 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5030 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5031 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5032 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5033 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5034 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5035 | EXPECT_TRUE(d.auth_required_called()); |
| 5036 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5037 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5038 | } |
| 5039 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5040 | } |
| 5041 | |
| 5042 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 5043 | // by setting credentials. |
| 5044 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5045 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5046 | |
| 5047 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5048 | BlockingNetworkDelegate network_delegate( |
| 5049 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 5050 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5051 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5052 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 5053 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 5054 | AuthCredentials auth_credentials(kUser, kSecret); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5055 | network_delegate.set_auth_credentials(auth_credentials); |
| 5056 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5057 | TestURLRequestContext context(true); |
| 5058 | context.set_network_delegate(&network_delegate); |
| 5059 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5060 | |
| 5061 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5062 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5063 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5064 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5065 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5066 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5067 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5068 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5069 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5070 | EXPECT_FALSE(d.auth_required_called()); |
| 5071 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5072 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5073 | } |
| 5074 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5075 | } |
| 5076 | |
| 5077 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 5078 | // by cancelling authentication. |
| 5079 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5080 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5081 | |
| 5082 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5083 | BlockingNetworkDelegate network_delegate( |
| 5084 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 5085 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5086 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5087 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 5088 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5089 | TestURLRequestContext context(true); |
| 5090 | context.set_network_delegate(&network_delegate); |
| 5091 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5092 | |
| 5093 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5094 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5095 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5096 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5097 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5098 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5099 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5100 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5101 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5102 | EXPECT_FALSE(d.auth_required_called()); |
| 5103 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5104 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5105 | } |
| 5106 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5107 | } |
| 5108 | |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5109 | // Tests that NetworkDelegate header overrides from the 401 response do not |
| 5110 | // affect the 200 response. This is a regression test for |
| 5111 | // https://crbug.com/801237. |
| 5112 | TEST_F(URLRequestTestHTTP, NetworkDelegateOverrideHeadersWithAuth) { |
| 5113 | ASSERT_TRUE(http_test_server()->Start()); |
| 5114 | |
| 5115 | TestDelegate d; |
| 5116 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 5117 | default_network_delegate_.set_add_header_to_first_response(true); |
| 5118 | |
| 5119 | { |
| 5120 | GURL url(http_test_server()->GetURL("/auth-basic")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5121 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5122 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 5123 | r->Start(); |
| 5124 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5125 | d.RunUntilComplete(); |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5126 | |
| 5127 | EXPECT_EQ(OK, d.request_status()); |
| 5128 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5129 | EXPECT_TRUE(d.auth_required_called()); |
| 5130 | EXPECT_FALSE(r->response_headers()->HasHeader("X-Network-Delegate")); |
| 5131 | } |
| 5132 | |
| 5133 | { |
| 5134 | GURL url(http_test_server()->GetURL("/defaultresponse")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5135 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5136 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 5137 | r->Start(); |
| 5138 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5139 | d.RunUntilComplete(); |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5140 | |
| 5141 | // Check that set_add_header_to_first_response normally adds a header. |
| 5142 | EXPECT_EQ(OK, d.request_status()); |
| 5143 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5144 | EXPECT_TRUE(r->response_headers()->HasHeader("X-Network-Delegate")); |
| 5145 | } |
| 5146 | } |
| 5147 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5148 | // Tests that we can handle when a network request was canceled while we were |
| 5149 | // waiting for the network delegate. |
| 5150 | // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 5151 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5152 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5153 | |
| 5154 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5155 | BlockingNetworkDelegate network_delegate( |
| 5156 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5157 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5158 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5159 | TestURLRequestContext context(true); |
| 5160 | context.set_network_delegate(&network_delegate); |
| 5161 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5162 | |
| 5163 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5164 | std::unique_ptr<URLRequest> r( |
| 5165 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 5166 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5167 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5168 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5169 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5170 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 5171 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5172 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5173 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5174 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5175 | // Ensure that network delegate is notified. |
| 5176 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5177 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5178 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5179 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5180 | } |
| 5181 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5182 | } |
| 5183 | |
| 5184 | // Tests that we can handle when a network request was canceled while we were |
| 5185 | // waiting for the network delegate. |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5186 | // Part 2: Request is cancelled while waiting for OnBeforeStartTransaction |
| 5187 | // callback. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5188 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5189 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5190 | |
| 5191 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5192 | BlockingNetworkDelegate network_delegate( |
| 5193 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5194 | network_delegate.set_block_on( |
| 5195 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5196 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5197 | TestURLRequestContext context(true); |
| 5198 | context.set_network_delegate(&network_delegate); |
| 5199 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5200 | |
| 5201 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5202 | std::unique_ptr<URLRequest> r( |
| 5203 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 5204 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5205 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5206 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5207 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5208 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 5209 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5210 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5211 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5212 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5213 | // Ensure that network delegate is notified. |
| 5214 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5215 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5216 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5217 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5218 | } |
| 5219 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5220 | } |
| 5221 | |
| 5222 | // Tests that we can handle when a network request was canceled while we were |
| 5223 | // waiting for the network delegate. |
| 5224 | // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 5225 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5226 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5227 | |
| 5228 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5229 | BlockingNetworkDelegate network_delegate( |
| 5230 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5231 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5232 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5233 | TestURLRequestContext context(true); |
| 5234 | context.set_network_delegate(&network_delegate); |
| 5235 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5236 | |
| 5237 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5238 | std::unique_ptr<URLRequest> r( |
| 5239 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 5240 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5241 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5242 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5243 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5244 | EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 5245 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5246 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5247 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5248 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5249 | // Ensure that network delegate is notified. |
| 5250 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5251 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5252 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5253 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5254 | } |
| 5255 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5256 | } |
| 5257 | |
| 5258 | // Tests that we can handle when a network request was canceled while we were |
| 5259 | // waiting for the network delegate. |
| 5260 | // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
[email protected] | bfe8b30 | 2013-02-15 12:16:02 | [diff] [blame] | 5261 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5262 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5263 | |
| 5264 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5265 | BlockingNetworkDelegate network_delegate( |
| 5266 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5267 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5268 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5269 | TestURLRequestContext context(true); |
| 5270 | context.set_network_delegate(&network_delegate); |
| 5271 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5272 | |
| 5273 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5274 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5275 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 5276 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5277 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5278 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5279 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5280 | EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED, |
| 5281 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5282 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5283 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5284 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5285 | // Ensure that network delegate is notified. |
| 5286 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5287 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5288 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5289 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5290 | } |
| 5291 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5292 | } |
| 5293 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5294 | namespace { |
| 5295 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5296 | std::unique_ptr<test_server::HttpResponse> HandleServerAuthConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5297 | const test_server::HttpRequest& request) { |
| 5298 | if (request.headers.find("Host") == request.headers.end() || |
| 5299 | request.headers.at("Host") != "www.server-auth.com" || |
| 5300 | request.method != test_server::METHOD_CONNECT) { |
| 5301 | return nullptr; |
| 5302 | } |
| 5303 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5304 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5305 | new test_server::BasicHttpResponse); |
| 5306 | http_response->set_code(HTTP_UNAUTHORIZED); |
| 5307 | http_response->AddCustomHeader("WWW-Authenticate", |
| 5308 | "Basic realm=\"WallyWorld\""); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 5309 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5310 | } |
| 5311 | |
| 5312 | } // namespace |
| 5313 | |
| 5314 | // In this unit test, we're using the EmbeddedTestServer as a proxy server and |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5315 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5316 | // The EmbeddedTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5317 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5318 | http_test_server()->RegisterRequestHandler( |
Matt Menke | 2b2a6047 | 2018-02-07 02:12:12 | [diff] [blame] | 5319 | base::BindRepeating(&HandleServerAuthConnect)); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5320 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5321 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5322 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 5323 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5324 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 5325 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5326 | TestDelegate d; |
| 5327 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5328 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5329 | GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d, |
| 5330 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5331 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5332 | r->Start(); |
| 5333 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5334 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5335 | d.RunUntilComplete(); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5336 | |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 5337 | // The proxy server should be set before failure. |
| 5338 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 5339 | http_test_server()->host_port_pair()), |
| 5340 | r->proxy_server()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5341 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5342 | } |
| 5343 | } |
| 5344 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5345 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5346 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5347 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5348 | TestDelegate d; |
| 5349 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5350 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5351 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5352 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5353 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5354 | r->Start(); |
| 5355 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5356 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5357 | d.RunUntilComplete(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5358 | |
| 5359 | EXPECT_EQ(1, d.response_started_count()); |
| 5360 | EXPECT_FALSE(d.received_data_before_response()); |
| 5361 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5362 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5363 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5364 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5365 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 5366 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5367 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5368 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5369 | } |
| 5370 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5371 | // This test has the server send a large number of cookies to the client. |
| 5372 | // To ensure that no number of cookies causes a crash, a galloping binary |
| 5373 | // search is used to estimate that maximum number of cookies that are accepted |
| 5374 | // by the browser. Beyond the maximum number, the request will fail with |
| 5375 | // ERR_RESPONSE_HEADERS_TOO_BIG. |
Sergey Ulanov | 0fb900c | 2017-09-20 00:27:31 | [diff] [blame] | 5376 | #if defined(OS_WIN) || defined(OS_FUCHSIA) |
[email protected] | 69dd6fe | 2013-02-23 23:15:30 | [diff] [blame] | 5377 | // http://crbug.com/177916 |
| 5378 | #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies |
| 5379 | #else |
| 5380 | #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies |
| 5381 | #endif // defined(OS_WIN) |
| 5382 | TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5383 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5384 | |
| 5385 | int lower_bound = 0; |
| 5386 | int upper_bound = 1; |
| 5387 | |
| 5388 | // Double the number of cookies until the response header limits are |
| 5389 | // exceeded. |
| 5390 | while (DoManyCookiesRequest(upper_bound)) { |
| 5391 | lower_bound = upper_bound; |
| 5392 | upper_bound *= 2; |
| 5393 | ASSERT_LT(upper_bound, 1000000); |
| 5394 | } |
| 5395 | |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 5396 | int tolerance = static_cast<int>(upper_bound * 0.005); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5397 | if (tolerance < 2) |
| 5398 | tolerance = 2; |
| 5399 | |
| 5400 | // Perform a binary search to find the highest possible number of cookies, |
| 5401 | // within the desired tolerance. |
| 5402 | while (upper_bound - lower_bound >= tolerance) { |
| 5403 | int num_cookies = (lower_bound + upper_bound) / 2; |
| 5404 | |
| 5405 | if (DoManyCookiesRequest(num_cookies)) |
| 5406 | lower_bound = num_cookies; |
| 5407 | else |
| 5408 | upper_bound = num_cookies; |
| 5409 | } |
| 5410 | // Success: the test did not crash. |
| 5411 | } |
| 5412 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5413 | TEST_F(URLRequestTestHTTP, GetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5414 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5415 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5416 | TestDelegate d; |
| 5417 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5418 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5419 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5420 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5421 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5422 | r->Start(); |
| 5423 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5424 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5425 | d.RunUntilComplete(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5426 | |
| 5427 | EXPECT_EQ(1, d.response_started_count()); |
| 5428 | EXPECT_FALSE(d.received_data_before_response()); |
| 5429 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5430 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5431 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5432 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5433 | r->GetResponseRemoteEndpoint().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5434 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 5435 | } |
| 5436 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5437 | TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5438 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5439 | |
| 5440 | TestDelegate d; |
| 5441 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5442 | GURL test_url(http_test_server()->GetURL("/defaultresponse")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5443 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5444 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5445 | |
| 5446 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5447 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5448 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5449 | r->Start(); |
| 5450 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5451 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5452 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5453 | |
| 5454 | EXPECT_EQ(1, d.response_started_count()); |
| 5455 | EXPECT_FALSE(d.received_data_before_response()); |
| 5456 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5457 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5458 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5459 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5460 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5461 | |
| 5462 | EXPECT_TRUE(d.have_full_request_headers()); |
| 5463 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 5464 | } |
| 5465 | } |
| 5466 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5467 | TEST_F(URLRequestTestHTTP, GetTestLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5468 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5469 | |
| 5470 | TestDelegate d; |
| 5471 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5472 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5473 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5474 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5475 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5476 | r->Start(); |
| 5477 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5478 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5479 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5480 | |
| 5481 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5482 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5483 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5484 | |
| 5485 | EXPECT_EQ(1, d.response_started_count()); |
| 5486 | EXPECT_FALSE(d.received_data_before_response()); |
| 5487 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5488 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5489 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5490 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5491 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5492 | } |
| 5493 | } |
| 5494 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5495 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 5496 | #if !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5497 | TEST_F(URLRequestTestHTTP, GetZippedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5498 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5499 | base::FilePath(kTestFilePath)); |
| 5500 | |
| 5501 | ASSERT_TRUE(test_server.Start()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5502 | |
| 5503 | // Parameter that specifies the Content-Length field in the response: |
| 5504 | // C - Compressed length. |
| 5505 | // U - Uncompressed length. |
| 5506 | // L - Large length (larger than both C & U). |
| 5507 | // M - Medium length (between C & U). |
| 5508 | // S - Small length (smaller than both C & U). |
| 5509 | const char test_parameters[] = "CULMS"; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 5510 | const int num_tests = base::size(test_parameters) - 1; // Skip NULL. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5511 | // C & U should be OK. |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 5512 | // L & M are larger than the data sent, and show an error. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5513 | // S has too little data, but we seem to accept it. |
| 5514 | const bool test_expect_success[num_tests] = |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 5515 | { true, true, false, false, true }; |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5516 | |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5517 | base::FilePath file_path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 5518 | base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5519 | file_path = file_path.Append(kTestFilePath); |
| 5520 | file_path = file_path.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); |
| 5521 | std::string expected_content; |
| 5522 | ASSERT_TRUE(base::ReadFileToString(file_path, &expected_content)); |
| 5523 | |
| 5524 | for (int i = 0; i < num_tests; i++) { |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5525 | TestDelegate d; |
| 5526 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5527 | std::string test_file = base::StringPrintf( |
| 5528 | "compressedfiles/BullRunSpeech.txt?%c", test_parameters[i]); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5529 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5530 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5531 | TestURLRequestContext context(true); |
| 5532 | context.set_network_delegate(&network_delegate); |
| 5533 | context.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 5534 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5535 | std::unique_ptr<URLRequest> r( |
| 5536 | context.CreateRequest(test_server.GetURL(test_file), DEFAULT_PRIORITY, |
| 5537 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5538 | r->Start(); |
| 5539 | EXPECT_TRUE(r->is_pending()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5540 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5541 | d.RunUntilComplete(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5542 | |
| 5543 | EXPECT_EQ(1, d.response_started_count()); |
| 5544 | EXPECT_FALSE(d.received_data_before_response()); |
| 5545 | VLOG(1) << " Received " << d.bytes_received() << " bytes" |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5546 | << " error = " << d.request_status(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5547 | if (test_expect_success[i]) { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5548 | EXPECT_EQ(OK, d.request_status()) << " Parameter = \"" << test_file |
| 5549 | << "\""; |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5550 | if (test_parameters[i] == 'S') { |
| 5551 | // When content length is smaller than both compressed length and |
| 5552 | // uncompressed length, HttpStreamParser might not read the full |
| 5553 | // response body. |
| 5554 | continue; |
| 5555 | } |
| 5556 | EXPECT_EQ(expected_content, d.data_received()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5557 | } else { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5558 | EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, d.request_status()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5559 | << " Parameter = \"" << test_file << "\""; |
| 5560 | } |
| 5561 | } |
| 5562 | } |
| 5563 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5564 | #endif // !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5565 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5566 | TEST_F(URLRequestTestHTTP, RedirectLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5567 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5568 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5569 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5570 | GURL original_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5571 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5572 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5573 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5574 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5575 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5576 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5577 | |
| 5578 | EXPECT_EQ(1, d.response_started_count()); |
| 5579 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5580 | EXPECT_EQ(destination_url, req->url()); |
| 5581 | EXPECT_EQ(original_url, req->original_url()); |
| 5582 | ASSERT_EQ(2U, req->url_chain().size()); |
| 5583 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5584 | EXPECT_EQ(destination_url, req->url_chain()[1]); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5585 | |
| 5586 | LoadTimingInfo load_timing_info_before_redirect; |
| 5587 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect( |
| 5588 | &load_timing_info_before_redirect)); |
| 5589 | TestLoadTimingNotReused(load_timing_info_before_redirect, |
| 5590 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 5591 | |
| 5592 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5593 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5594 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5595 | |
| 5596 | // Check that a new socket was used on redirect, since the server does not |
| 5597 | // supposed keep-alive sockets, and that the times before the redirect are |
| 5598 | // before the ones recorded for the second request. |
| 5599 | EXPECT_NE(load_timing_info_before_redirect.socket_log_id, |
| 5600 | load_timing_info.socket_log_id); |
| 5601 | EXPECT_LE(load_timing_info_before_redirect.receive_headers_end, |
| 5602 | load_timing_info.connect_timing.connect_start); |
| 5603 | } |
| 5604 | |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5605 | TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5606 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5607 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5608 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5609 | GURL middle_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5610 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
| 5611 | GURL original_url = http_test_server()->GetURL("/server-redirect?" + |
| 5612 | middle_redirect_url.spec()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5613 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5614 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5615 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5616 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5617 | d.RunUntilComplete(); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5618 | |
| 5619 | EXPECT_EQ(1, d.response_started_count()); |
| 5620 | EXPECT_EQ(2, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5621 | EXPECT_EQ(destination_url, req->url()); |
| 5622 | EXPECT_EQ(original_url, req->original_url()); |
| 5623 | ASSERT_EQ(3U, req->url_chain().size()); |
| 5624 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5625 | EXPECT_EQ(middle_redirect_url, req->url_chain()[1]); |
| 5626 | EXPECT_EQ(destination_url, req->url_chain()[2]); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5627 | } |
| 5628 | |
David Benjamin | a7fde61 | 2019-03-15 14:20:58 | [diff] [blame] | 5629 | // This is a regression test for https://crbug.com/942073. |
| 5630 | TEST_F(URLRequestTestHTTP, RedirectEscaping) { |
| 5631 | ASSERT_TRUE(http_test_server()->Start()); |
| 5632 | |
| 5633 | // Assemble the destination URL as a string so it is not escaped by GURL. |
| 5634 | GURL destination_base = http_test_server()->GetURL("/defaultresponse"); |
| 5635 | // Add a URL fragment of U+2603 unescaped, U+2603 escaped, and then a UTF-8 |
| 5636 | // encoding error. |
| 5637 | std::string destination_url = |
| 5638 | destination_base.spec() + "#\xE2\x98\x83_%E2%98%83_\xE0\xE0"; |
| 5639 | // Redirect resolution should percent-escape bytes and preserve the UTF-8 |
| 5640 | // error at the end. |
| 5641 | std::string destination_escaped = |
| 5642 | destination_base.spec() + "#%E2%98%83_%E2%98%83_%E0%E0"; |
| 5643 | GURL original_url = http_test_server()->GetURL( |
| 5644 | "/server-redirect?" + EscapeQueryParamValue(destination_url, false)); |
| 5645 | TestDelegate d; |
| 5646 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 5647 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 5648 | req->Start(); |
| 5649 | d.RunUntilComplete(); |
| 5650 | |
| 5651 | EXPECT_EQ(1, d.response_started_count()); |
| 5652 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5653 | EXPECT_EQ(destination_escaped, req->url().spec()); |
| 5654 | EXPECT_EQ(original_url, req->original_url()); |
| 5655 | ASSERT_EQ(2U, req->url_chain().size()); |
| 5656 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5657 | EXPECT_EQ(destination_escaped, req->url_chain()[1].spec()); |
| 5658 | } |
| 5659 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5660 | // First and second pieces of information logged by delegates to URLRequests. |
| 5661 | const char kFirstDelegateInfo[] = "Wonderful delegate"; |
| 5662 | const char kSecondDelegateInfo[] = "Exciting delegate"; |
| 5663 | |
| 5664 | // Logs delegate information to a URLRequest. The first string is logged |
| 5665 | // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is |
| 5666 | // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then |
| 5667 | // another asynchronous call is used to clear the delegate information |
| 5668 | // before calling a callback. The object then deletes itself. |
| 5669 | class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> { |
| 5670 | public: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5671 | using Callback = base::OnceCallback<void()>; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5672 | |
| 5673 | // Each time delegate information is added to the URLRequest, the resulting |
| 5674 | // load state is checked. The expected load state after each request is |
| 5675 | // passed in as an argument. |
| 5676 | static void Run(URLRequest* url_request, |
| 5677 | LoadState expected_first_load_state, |
| 5678 | LoadState expected_second_load_state, |
| 5679 | LoadState expected_third_load_state, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5680 | Callback callback) { |
tzik | 38f920a | 2018-08-01 04:41:20 | [diff] [blame] | 5681 | // base::MakeRefCounted<AsyncDelegateLogger> is unavailable here, since the |
| 5682 | // constructor of AsyncDelegateLogger is private. |
| 5683 | auto logger = base::WrapRefCounted(new AsyncDelegateLogger( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5684 | url_request, expected_first_load_state, expected_second_load_state, |
tzik | 38f920a | 2018-08-01 04:41:20 | [diff] [blame] | 5685 | expected_third_load_state, std::move(callback))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5686 | logger->Start(); |
| 5687 | } |
| 5688 | |
| 5689 | // Checks that the log entries, starting with log_position, contain the |
| 5690 | // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have |
| 5691 | // recorded. Returns the index of entry after the expected number of |
| 5692 | // events this logged, or entries.size() if there aren't enough entries. |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 5693 | static size_t CheckDelegateInfo(const std::vector<NetLogEntry>& entries, |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 5694 | size_t log_position) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5695 | // There should be 4 DELEGATE_INFO events: Two begins and two ends. |
| 5696 | if (log_position + 3 >= entries.size()) { |
| 5697 | ADD_FAILURE() << "Not enough log entries"; |
| 5698 | return entries.size(); |
| 5699 | } |
| 5700 | std::string delegate_info; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5701 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5702 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 5703 | EXPECT_EQ( |
| 5704 | kFirstDelegateInfo, |
| 5705 | GetStringValueFromParams(entries[log_position], "delegate_blocked_by")); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5706 | |
| 5707 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5708 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5709 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5710 | |
| 5711 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5712 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5713 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 5714 | EXPECT_EQ( |
| 5715 | kSecondDelegateInfo, |
| 5716 | GetStringValueFromParams(entries[log_position], "delegate_blocked_by")); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5717 | |
| 5718 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5719 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5720 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5721 | |
| 5722 | return log_position + 1; |
| 5723 | } |
| 5724 | |
| 5725 | private: |
| 5726 | friend class base::RefCounted<AsyncDelegateLogger>; |
| 5727 | |
| 5728 | AsyncDelegateLogger(URLRequest* url_request, |
| 5729 | LoadState expected_first_load_state, |
| 5730 | LoadState expected_second_load_state, |
| 5731 | LoadState expected_third_load_state, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5732 | Callback callback) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5733 | : url_request_(url_request), |
| 5734 | expected_first_load_state_(expected_first_load_state), |
| 5735 | expected_second_load_state_(expected_second_load_state), |
| 5736 | expected_third_load_state_(expected_third_load_state), |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5737 | callback_(std::move(callback)) {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5738 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5739 | ~AsyncDelegateLogger() = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5740 | |
| 5741 | void Start() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5742 | url_request_->LogBlockedBy(kFirstDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5743 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5744 | EXPECT_EQ(expected_first_load_state_, load_state.state); |
| 5745 | EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5746 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5747 | FROM_HERE, |
| 5748 | base::BindOnce(&AsyncDelegateLogger::LogSecondDelegate, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5749 | } |
| 5750 | |
| 5751 | void LogSecondDelegate() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5752 | url_request_->LogAndReportBlockedBy(kSecondDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5753 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5754 | EXPECT_EQ(expected_second_load_state_, load_state.state); |
| 5755 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) { |
| 5756 | EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5757 | } else { |
| 5758 | EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5759 | } |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5760 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5761 | FROM_HERE, base::BindOnce(&AsyncDelegateLogger::LogComplete, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5762 | } |
| 5763 | |
| 5764 | void LogComplete() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5765 | url_request_->LogUnblocked(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5766 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5767 | EXPECT_EQ(expected_third_load_state_, load_state.state); |
| 5768 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5769 | EXPECT_EQ(base::string16(), load_state.param); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5770 | std::move(callback_).Run(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5771 | } |
| 5772 | |
| 5773 | URLRequest* url_request_; |
| 5774 | const int expected_first_load_state_; |
| 5775 | const int expected_second_load_state_; |
| 5776 | const int expected_third_load_state_; |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5777 | Callback callback_; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5778 | |
| 5779 | DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger); |
| 5780 | }; |
| 5781 | |
| 5782 | // NetworkDelegate that logs delegate information before a request is started, |
| 5783 | // before headers are sent, when headers are read, and when auth information |
| 5784 | // is requested. Uses AsyncDelegateLogger. |
| 5785 | class AsyncLoggingNetworkDelegate : public TestNetworkDelegate { |
| 5786 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5787 | AsyncLoggingNetworkDelegate() = default; |
| 5788 | ~AsyncLoggingNetworkDelegate() override = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5789 | |
| 5790 | // NetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5791 | int OnBeforeURLRequest(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5792 | CompletionOnceCallback callback, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5793 | GURL* new_url) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5794 | // TestNetworkDelegate always completes synchronously. |
| 5795 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeURLRequest( |
| 5796 | request, base::NullCallback(), new_url)); |
| 5797 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5798 | } |
| 5799 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5800 | int OnBeforeStartTransaction(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5801 | CompletionOnceCallback callback, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5802 | HttpRequestHeaders* headers) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5803 | // TestNetworkDelegate always completes synchronously. |
| 5804 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeStartTransaction( |
| 5805 | request, base::NullCallback(), headers)); |
| 5806 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5807 | } |
| 5808 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5809 | int OnHeadersReceived( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5810 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5811 | CompletionOnceCallback callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5812 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5813 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5814 | GURL* allowed_unsafe_redirect_url) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5815 | // TestNetworkDelegate always completes synchronously. |
| 5816 | CHECK_NE(ERR_IO_PENDING, |
| 5817 | TestNetworkDelegate::OnHeadersReceived( |
| 5818 | request, base::NullCallback(), original_response_headers, |
| 5819 | override_response_headers, allowed_unsafe_redirect_url)); |
| 5820 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5821 | } |
| 5822 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5823 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5824 | URLRequest* request, |
| 5825 | const AuthChallengeInfo& auth_info, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5826 | AuthCallback callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5827 | AuthCredentials* credentials) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5828 | AsyncDelegateLogger::Run( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5829 | request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5830 | LOAD_STATE_WAITING_FOR_DELEGATE, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5831 | base::BindOnce(&AsyncLoggingNetworkDelegate::SetAuthAndResume, |
| 5832 | std::move(callback), credentials)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5833 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 5834 | } |
| 5835 | |
| 5836 | private: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5837 | static int RunCallbackAsynchronously(URLRequest* request, |
| 5838 | CompletionOnceCallback callback) { |
| 5839 | AsyncDelegateLogger::Run(request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5840 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5841 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5842 | base::BindOnce(std::move(callback), OK)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5843 | return ERR_IO_PENDING; |
| 5844 | } |
| 5845 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5846 | static void SetAuthAndResume(AuthCallback callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5847 | AuthCredentials* credentials) { |
| 5848 | *credentials = AuthCredentials(kUser, kSecret); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5849 | std::move(callback).Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5850 | } |
| 5851 | |
| 5852 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate); |
| 5853 | }; |
| 5854 | |
| 5855 | // URLRequest::Delegate that logs delegate information when the headers |
| 5856 | // are received, when each read completes, and during redirects. Uses |
| 5857 | // AsyncDelegateLogger. Can optionally cancel a request in any phase. |
| 5858 | // |
| 5859 | // Inherits from TestDelegate to reuse the TestDelegate code to handle |
| 5860 | // advancing to the next step in most cases, as well as cancellation. |
| 5861 | class AsyncLoggingUrlRequestDelegate : public TestDelegate { |
| 5862 | public: |
| 5863 | enum CancelStage { |
| 5864 | NO_CANCEL = 0, |
| 5865 | CANCEL_ON_RECEIVED_REDIRECT, |
| 5866 | CANCEL_ON_RESPONSE_STARTED, |
| 5867 | CANCEL_ON_READ_COMPLETED |
| 5868 | }; |
| 5869 | |
| 5870 | explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage) |
| 5871 | : cancel_stage_(cancel_stage) { |
| 5872 | if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT) |
| 5873 | set_cancel_in_received_redirect(true); |
| 5874 | else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED) |
| 5875 | set_cancel_in_response_started(true); |
| 5876 | else if (cancel_stage == CANCEL_ON_READ_COMPLETED) |
| 5877 | set_cancel_in_received_data(true); |
| 5878 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5879 | ~AsyncLoggingUrlRequestDelegate() override = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5880 | |
| 5881 | // URLRequest::Delegate implementation: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5882 | void OnReceivedRedirect(URLRequest* request, |
| 5883 | const RedirectInfo& redirect_info, |
| 5884 | bool* defer_redirect) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5885 | *defer_redirect = true; |
| 5886 | AsyncDelegateLogger::Run( |
| 5887 | request, |
| 5888 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5889 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5890 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5891 | base::Bind( |
| 5892 | &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5893 | base::Unretained(this), request, redirect_info)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5894 | } |
| 5895 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5896 | void OnResponseStarted(URLRequest* request, int net_error) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5897 | AsyncDelegateLogger::Run( |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5898 | request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5899 | LOAD_STATE_WAITING_FOR_DELEGATE, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5900 | base::Bind( |
| 5901 | &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete, |
| 5902 | base::Unretained(this), request, net_error)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5903 | } |
| 5904 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5905 | void OnReadCompleted(URLRequest* request, int bytes_read) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5906 | AsyncDelegateLogger::Run( |
| 5907 | request, |
| 5908 | LOAD_STATE_IDLE, |
| 5909 | LOAD_STATE_IDLE, |
| 5910 | LOAD_STATE_IDLE, |
| 5911 | base::Bind( |
| 5912 | &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete, |
| 5913 | base::Unretained(this), request, bytes_read)); |
| 5914 | } |
| 5915 | |
| 5916 | private: |
| 5917 | void OnReceivedRedirectLoggingComplete(URLRequest* request, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5918 | const RedirectInfo& redirect_info) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5919 | bool defer_redirect = false; |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5920 | TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5921 | // FollowDeferredRedirect should not be called after cancellation. |
| 5922 | if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT) |
| 5923 | return; |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 5924 | if (!defer_redirect) { |
| 5925 | request->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 5926 | base::nullopt /* modified_headers */); |
| 5927 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5928 | } |
| 5929 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5930 | void OnResponseStartedLoggingComplete(URLRequest* request, int net_error) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5931 | // The parent class continues the request. |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5932 | TestDelegate::OnResponseStarted(request, net_error); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5933 | } |
| 5934 | |
| 5935 | void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) { |
| 5936 | // The parent class continues the request. |
| 5937 | TestDelegate::OnReadCompleted(request, bytes_read); |
| 5938 | } |
| 5939 | |
| 5940 | const CancelStage cancel_stage_; |
| 5941 | |
| 5942 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate); |
| 5943 | }; |
| 5944 | |
| 5945 | // Tests handling of delegate info before a request starts. |
| 5946 | TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5947 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5948 | |
| 5949 | TestDelegate request_delegate; |
| 5950 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 5951 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5952 | context.set_net_log(&net_log_); |
| 5953 | context.Init(); |
| 5954 | |
| 5955 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5956 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5957 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, |
| 5958 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5959 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5960 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5961 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5962 | |
| 5963 | AsyncDelegateLogger::Run( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5964 | r.get(), |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5965 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5966 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5967 | LOAD_STATE_IDLE, |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5968 | base::Bind(&URLRequest::Start, base::Unretained(r.get()))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5969 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5970 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5971 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5972 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5973 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5974 | } |
| 5975 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 5976 | auto entries = net_log_.GetEntries(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5977 | size_t log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5978 | entries, 0, NetLogEventType::DELEGATE_INFO, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5979 | |
| 5980 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position); |
| 5981 | |
| 5982 | // Nothing else should add any delegate info to the request. |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5983 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5984 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5985 | } |
| 5986 | |
| 5987 | // Tests handling of delegate info from a network delegate. |
| 5988 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5989 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5990 | |
| 5991 | TestDelegate request_delegate; |
| 5992 | AsyncLoggingNetworkDelegate network_delegate; |
| 5993 | TestURLRequestContext context(true); |
| 5994 | context.set_network_delegate(&network_delegate); |
| 5995 | context.set_net_log(&net_log_); |
| 5996 | context.Init(); |
| 5997 | |
| 5998 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5999 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 6000 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, |
| 6001 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6002 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6003 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 6004 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6005 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6006 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6007 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6008 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6009 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6010 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6011 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 6012 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 6013 | } |
| 6014 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 6015 | |
| 6016 | size_t log_position = 0; |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 6017 | auto entries = net_log_.GetEntries(); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6018 | static const NetLogEventType kExpectedEvents[] = { |
| 6019 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 6020 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6021 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6022 | }; |
| 6023 | for (NetLogEventType event : kExpectedEvents) { |
| 6024 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6025 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6026 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6027 | |
| 6028 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 6029 | log_position + 1); |
| 6030 | |
| 6031 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6032 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6033 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6034 | } |
| 6035 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6036 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6037 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6038 | } |
| 6039 | |
| 6040 | // Tests handling of delegate info from a network delegate in the case of an |
| 6041 | // HTTP redirect. |
| 6042 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6043 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6044 | |
| 6045 | TestDelegate request_delegate; |
| 6046 | AsyncLoggingNetworkDelegate network_delegate; |
| 6047 | TestURLRequestContext context(true); |
| 6048 | context.set_network_delegate(&network_delegate); |
| 6049 | context.set_net_log(&net_log_); |
| 6050 | context.Init(); |
| 6051 | |
| 6052 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6053 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6054 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6055 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6056 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6057 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 6058 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6059 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6060 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6061 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6062 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6063 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6064 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6065 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 6066 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 6067 | } |
| 6068 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 6069 | |
| 6070 | size_t log_position = 0; |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 6071 | auto entries = net_log_.GetEntries(); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6072 | static const NetLogEventType kExpectedEvents[] = { |
| 6073 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 6074 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6075 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6076 | }; |
| 6077 | for (NetLogEventType event : kExpectedEvents) { |
| 6078 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6079 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6080 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6081 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6082 | log_position = |
| 6083 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6084 | |
| 6085 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6086 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6087 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6088 | } |
| 6089 | |
| 6090 | // The URLRequest::Delegate then gets informed about the redirect. |
| 6091 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6092 | entries, log_position + 1, |
| 6093 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6094 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6095 | |
| 6096 | // The NetworkDelegate logged information in the same three events as before. |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6097 | for (NetLogEventType event : kExpectedEvents) { |
| 6098 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6099 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6100 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6101 | |
| 6102 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 6103 | log_position + 1); |
| 6104 | |
| 6105 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6106 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6107 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6108 | } |
| 6109 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6110 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6111 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6112 | } |
| 6113 | |
| 6114 | // Tests handling of delegate info from a network delegate in the case of HTTP |
| 6115 | // AUTH. |
| 6116 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6117 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6118 | |
| 6119 | TestDelegate request_delegate; |
| 6120 | AsyncLoggingNetworkDelegate network_delegate; |
| 6121 | TestURLRequestContext context(true); |
| 6122 | context.set_network_delegate(&network_delegate); |
| 6123 | context.set_net_log(&net_log_); |
| 6124 | context.Init(); |
| 6125 | |
| 6126 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6127 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 6128 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, |
| 6129 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6130 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6131 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 6132 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6133 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6134 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6135 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6136 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6137 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6138 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6139 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 6140 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 6141 | } |
| 6142 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 6143 | |
| 6144 | size_t log_position = 0; |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 6145 | auto entries = net_log_.GetEntries(); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6146 | static const NetLogEventType kExpectedEvents[] = { |
| 6147 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 6148 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6149 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6150 | NetLogEventType::NETWORK_DELEGATE_AUTH_REQUIRED, |
| 6151 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6152 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6153 | }; |
| 6154 | for (NetLogEventType event : kExpectedEvents) { |
| 6155 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6156 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6157 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6158 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6159 | log_position = |
| 6160 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6161 | |
| 6162 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6163 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6164 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6165 | } |
| 6166 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6167 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6168 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6169 | } |
| 6170 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6171 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 6172 | #if !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6173 | // Tests handling of delegate info from a URLRequest::Delegate. |
| 6174 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6175 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6176 | base::FilePath(kTestFilePath)); |
| 6177 | |
| 6178 | ASSERT_TRUE(test_server.Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6179 | |
| 6180 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 6181 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 6182 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6183 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6184 | context.set_net_log(&net_log_); |
| 6185 | context.Init(); |
| 6186 | |
| 6187 | { |
| 6188 | // A chunked response with delays between chunks is used to make sure that |
| 6189 | // attempts by the URLRequest delegate to log information while reading the |
| 6190 | // body are ignored. Since they are ignored, this test is robust against |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6191 | // the possibility of multiple reads being combined in the unlikely event |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6192 | // that it occurs. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6193 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6194 | test_server.GetURL("/chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6195 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6196 | LoadStateWithParam load_state = r->GetLoadState(); |
| 6197 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6198 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6199 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6200 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6201 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6202 | } |
| 6203 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 6204 | auto entries = net_log_.GetEntries(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6205 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6206 | size_t log_position = 0; |
| 6207 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6208 | // The delegate info should only have been logged on header complete. Other |
| 6209 | // times it should silently be ignored. |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6210 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 6211 | entries, 0, NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST)); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6212 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6213 | entries, log_position + 1, |
| 6214 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6215 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6216 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6217 | log_position = |
| 6218 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6219 | |
| 6220 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6221 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 6222 | entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6223 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6224 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6225 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6226 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6227 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6228 | entries, log_position + 1, |
| 6229 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6230 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6231 | #endif // !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6232 | |
| 6233 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 6234 | // an HTTP redirect. |
| 6235 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6236 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6237 | |
| 6238 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 6239 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 6240 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6241 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6242 | context.set_net_log(&net_log_); |
| 6243 | context.Init(); |
| 6244 | |
| 6245 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6246 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6247 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6248 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6249 | LoadStateWithParam load_state = r->GetLoadState(); |
| 6250 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6251 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6252 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6253 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6254 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6255 | } |
| 6256 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 6257 | auto entries = net_log_.GetEntries(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6258 | |
| 6259 | // Delegate info should only have been logged in OnReceivedRedirect and |
| 6260 | // OnResponseStarted. |
| 6261 | size_t log_position = 0; |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6262 | static const NetLogEventType kExpectedEvents[] = { |
| 6263 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
| 6264 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 6265 | }; |
| 6266 | for (NetLogEventType event : kExpectedEvents) { |
| 6267 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
| 6268 | log_position = ExpectLogContainsSomewhereAfter(entries, log_position, event, |
| 6269 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6270 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6271 | log_position = |
| 6272 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6273 | |
| 6274 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6275 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6276 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6277 | } |
| 6278 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6279 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6280 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6281 | } |
| 6282 | |
| 6283 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 6284 | // an HTTP redirect, with cancellation at various points. |
| 6285 | TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6286 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6287 | |
| 6288 | const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = { |
| 6289 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT, |
| 6290 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED, |
| 6291 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED, |
| 6292 | }; |
| 6293 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6294 | for (auto cancel_stage : kCancelStages) { |
| 6295 | AsyncLoggingUrlRequestDelegate request_delegate(cancel_stage); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 6296 | TestNetLog net_log; |
krasin | 0bfeb6b | 2017-01-13 21:48:04 | [diff] [blame] | 6297 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6298 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6299 | context.set_net_log(&net_log); |
| 6300 | context.Init(); |
| 6301 | |
| 6302 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6303 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6304 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6305 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6306 | LoadStateWithParam load_state = r->GetLoadState(); |
| 6307 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6308 | request_delegate.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6309 | EXPECT_EQ(ERR_ABORTED, request_delegate.request_status()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6310 | |
| 6311 | // Spin the message loop to run AsyncDelegateLogger task(s) posted after |
| 6312 | // the |request_delegate| completion task. |
| 6313 | base::RunLoop().RunUntilIdle(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6314 | } |
| 6315 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 6316 | auto entries = net_log.GetEntries(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6317 | |
| 6318 | // Delegate info is always logged in both OnReceivedRedirect and |
| 6319 | // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the |
| 6320 | // OnResponseStarted delegate call is after cancellation, but logging is |
| 6321 | // still currently supported in that call. |
| 6322 | size_t log_position = 0; |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6323 | static const NetLogEventType kExpectedEvents[] = { |
| 6324 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
| 6325 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 6326 | }; |
| 6327 | for (NetLogEventType event : kExpectedEvents) { |
| 6328 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6329 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6330 | entries, log_position, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6331 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6332 | log_position = |
| 6333 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6334 | |
| 6335 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6336 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6337 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6338 | } |
| 6339 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6340 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6341 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6342 | } |
| 6343 | } |
| 6344 | |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6345 | namespace { |
| 6346 | |
| 6347 | const char kExtraHeader[] = "Allow-Snafu"; |
| 6348 | const char kExtraValue[] = "fubar"; |
| 6349 | |
| 6350 | class RedirectWithAdditionalHeadersDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 6351 | void OnReceivedRedirect(URLRequest* request, |
| 6352 | const RedirectInfo& redirect_info, |
| 6353 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6354 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6355 | request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false); |
| 6356 | } |
| 6357 | }; |
| 6358 | |
| 6359 | } // namespace |
| 6360 | |
| 6361 | TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6362 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6363 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6364 | GURL destination_url = |
| 6365 | http_test_server()->GetURL("/echoheader?" + std::string(kExtraHeader)); |
| 6366 | GURL original_url = |
| 6367 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6368 | RedirectWithAdditionalHeadersDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6369 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6370 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6371 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6372 | d.RunUntilComplete(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6373 | |
| 6374 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6375 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6376 | EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value)); |
| 6377 | EXPECT_EQ(kExtraValue, value); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6378 | EXPECT_FALSE(req->is_pending()); |
| 6379 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6380 | EXPECT_EQ(kExtraValue, d.data_received()); |
| 6381 | } |
| 6382 | |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6383 | namespace { |
| 6384 | |
| 6385 | const char kExtraHeaderToRemove[] = "To-Be-Removed"; |
| 6386 | |
| 6387 | class RedirectWithHeaderRemovalDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 6388 | void OnReceivedRedirect(URLRequest* request, |
| 6389 | const RedirectInfo& redirect_info, |
| 6390 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6391 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6392 | request->RemoveRequestHeaderByName(kExtraHeaderToRemove); |
| 6393 | } |
| 6394 | }; |
| 6395 | |
| 6396 | } // namespace |
| 6397 | |
| 6398 | TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6399 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6400 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6401 | GURL destination_url = http_test_server()->GetURL( |
| 6402 | "/echoheader?" + std::string(kExtraHeaderToRemove)); |
| 6403 | GURL original_url = |
| 6404 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6405 | RedirectWithHeaderRemovalDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6406 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6407 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6408 | req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false); |
| 6409 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6410 | d.RunUntilComplete(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6411 | |
| 6412 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6413 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6414 | EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6415 | EXPECT_FALSE(req->is_pending()); |
| 6416 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6417 | EXPECT_EQ("None", d.data_received()); |
| 6418 | } |
| 6419 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6420 | TEST_F(URLRequestTestHTTP, CancelAfterStart) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6421 | TestDelegate d; |
| 6422 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6423 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6424 | GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d, |
| 6425 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6426 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6427 | r->Start(); |
| 6428 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6429 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6430 | r->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6431 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6432 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6433 | |
| 6434 | // We expect to receive OnResponseStarted even though the request has been |
| 6435 | // cancelled. |
| 6436 | EXPECT_EQ(1, d.response_started_count()); |
| 6437 | EXPECT_EQ(0, d.bytes_received()); |
| 6438 | EXPECT_FALSE(d.received_data_before_response()); |
| 6439 | } |
| 6440 | } |
| 6441 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6442 | TEST_F(URLRequestTestHTTP, CancelInResponseStarted) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6443 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6444 | |
| 6445 | TestDelegate d; |
| 6446 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6447 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6448 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6449 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6450 | |
| 6451 | d.set_cancel_in_response_started(true); |
| 6452 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6453 | r->Start(); |
| 6454 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6455 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6456 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6457 | |
| 6458 | EXPECT_EQ(1, d.response_started_count()); |
| 6459 | EXPECT_EQ(0, d.bytes_received()); |
| 6460 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6461 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6462 | } |
| 6463 | } |
| 6464 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6465 | TEST_F(URLRequestTestHTTP, CancelOnDataReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6466 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6467 | |
| 6468 | TestDelegate d; |
| 6469 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6470 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6471 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 6472 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6473 | |
| 6474 | d.set_cancel_in_received_data(true); |
| 6475 | |
| 6476 | r->Start(); |
| 6477 | EXPECT_TRUE(r->is_pending()); |
| 6478 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6479 | d.RunUntilComplete(); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6480 | |
| 6481 | EXPECT_EQ(1, d.response_started_count()); |
| 6482 | EXPECT_NE(0, d.received_bytes_count()); |
| 6483 | EXPECT_FALSE(d.received_data_before_response()); |
| 6484 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
| 6485 | } |
| 6486 | } |
| 6487 | |
| 6488 | TEST_F(URLRequestTestHTTP, CancelDuringEofRead) { |
| 6489 | ASSERT_TRUE(http_test_server()->Start()); |
| 6490 | |
| 6491 | TestDelegate d; |
| 6492 | { |
| 6493 | // This returns an empty response (With headers). |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6494 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6495 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6496 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6497 | |
| 6498 | d.set_cancel_in_received_data(true); |
| 6499 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6500 | r->Start(); |
| 6501 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6502 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6503 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6504 | |
| 6505 | EXPECT_EQ(1, d.response_started_count()); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6506 | EXPECT_EQ(0, d.received_bytes_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6507 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6508 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6509 | } |
| 6510 | } |
| 6511 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6512 | TEST_F(URLRequestTestHTTP, CancelByDestroyingAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6513 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6514 | |
| 6515 | TestDelegate d; |
| 6516 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6517 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6518 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6519 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6520 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6521 | r->Start(); |
| 6522 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6523 | |
| 6524 | // The request will be implicitly canceled when it is destroyed. The |
| 6525 | // test delegate must not post a quit message when this happens because |
| 6526 | // this test doesn't actually have a message loop. The quit message would |
| 6527 | // get put on this thread's message queue and the next test would exit |
| 6528 | // early, causing problems. |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 6529 | d.set_on_complete(base::DoNothing()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6530 | } |
| 6531 | // expect things to just cleanup properly. |
| 6532 | |
kimwjdalsl | 2bb4ff0 | 2015-12-16 22:06:02 | [diff] [blame] | 6533 | // we won't actually get a received response here because we've never run the |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6534 | // message loop |
| 6535 | EXPECT_FALSE(d.received_data_before_response()); |
| 6536 | EXPECT_EQ(0, d.bytes_received()); |
| 6537 | } |
| 6538 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6539 | TEST_F(URLRequestTestHTTP, CancelWhileReadingFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6540 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6541 | |
| 6542 | // populate cache |
| 6543 | { |
| 6544 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6545 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6546 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6547 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6548 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6549 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6550 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6551 | } |
| 6552 | |
| 6553 | // cancel read from cache (see bug 990242) |
| 6554 | { |
| 6555 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6556 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6557 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6558 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6559 | r->Start(); |
| 6560 | r->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6561 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6562 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6563 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6564 | EXPECT_EQ(1, d.response_started_count()); |
| 6565 | EXPECT_EQ(0, d.bytes_received()); |
| 6566 | EXPECT_FALSE(d.received_data_before_response()); |
| 6567 | } |
| 6568 | } |
| 6569 | |
| 6570 | TEST_F(URLRequestTestHTTP, PostTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6571 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6572 | HTTPUploadDataOperationTest("POST"); |
| 6573 | } |
| 6574 | |
| 6575 | TEST_F(URLRequestTestHTTP, PutTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6576 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6577 | HTTPUploadDataOperationTest("PUT"); |
| 6578 | } |
| 6579 | |
| 6580 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6581 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6582 | |
| 6583 | TestDelegate d; |
| 6584 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6585 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6586 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6587 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6588 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6589 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6590 | r->Start(); |
| 6591 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6592 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6593 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6594 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6595 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6596 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6597 | |
| 6598 | EXPECT_FALSE(d.received_data_before_response()); |
| 6599 | EXPECT_TRUE(d.data_received().empty()); |
| 6600 | } |
| 6601 | } |
| 6602 | |
| 6603 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6604 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6605 | |
| 6606 | TestDelegate d; |
| 6607 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6608 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6609 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6610 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6611 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6612 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6613 | base::FilePath dir; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 6614 | base::PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 6615 | base::SetCurrentDirectory(dir); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6616 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6617 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6618 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6619 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 6620 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 6621 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6622 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6623 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6624 | base::ThreadTaskRunnerHandle::Get().get(), path, 0, |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6625 | std::numeric_limits<uint64_t>::max(), base::Time())); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6626 | r->set_upload(std::make_unique<ElementsUploadDataStream>( |
| 6627 | std::move(element_readers), 0)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6628 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6629 | r->Start(); |
| 6630 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6631 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6632 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6633 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6634 | int64_t size64 = 0; |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 6635 | ASSERT_EQ(true, base::GetFileSize(path, &size64)); |
| 6636 | ASSERT_LE(size64, std::numeric_limits<int>::max()); |
| 6637 | int size = static_cast<int>(size64); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6638 | std::unique_ptr<char[]> buf(new char[size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6639 | |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 6640 | ASSERT_EQ(size, base::ReadFile(path, buf.get(), size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6641 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6642 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6643 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6644 | |
| 6645 | EXPECT_FALSE(d.received_data_before_response()); |
| 6646 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6647 | EXPECT_EQ(size, d.bytes_received()); |
| 6648 | EXPECT_EQ(std::string(&buf[0], size), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6649 | } |
| 6650 | } |
| 6651 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6652 | TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6653 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6654 | |
| 6655 | TestDelegate d; |
| 6656 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6657 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6658 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6659 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6660 | r->set_method("POST"); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6661 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6662 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6663 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6664 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 6665 | base::ThreadTaskRunnerHandle::Get().get(), |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6666 | base::FilePath(FILE_PATH_LITERAL( |
| 6667 | "c:\\path\\to\\non\\existant\\file.randomness.12345")), |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6668 | 0, std::numeric_limits<uint64_t>::max(), base::Time())); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6669 | r->set_upload(std::make_unique<ElementsUploadDataStream>( |
| 6670 | std::move(element_readers), 0)); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6671 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6672 | r->Start(); |
| 6673 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6674 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6675 | d.RunUntilComplete(); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6676 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6677 | EXPECT_TRUE(d.request_failed()); |
| 6678 | EXPECT_FALSE(d.received_data_before_response()); |
| 6679 | EXPECT_EQ(0, d.bytes_received()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6680 | EXPECT_EQ(ERR_FILE_NOT_FOUND, d.request_status()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6681 | } |
| 6682 | } |
| 6683 | |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6684 | namespace { |
| 6685 | |
| 6686 | // Adds a standard set of data to an upload for chunked upload integration |
| 6687 | // tests. |
| 6688 | void AddDataToUpload(ChunkedUploadDataStream::Writer* writer) { |
| 6689 | writer->AppendData("a", 1, false); |
| 6690 | writer->AppendData("bcd", 3, false); |
| 6691 | writer->AppendData("this is a longer chunk than before.", 35, false); |
| 6692 | writer->AppendData("\r\n\r\n", 4, false); |
| 6693 | writer->AppendData("0", 1, false); |
| 6694 | writer->AppendData("2323", 4, true); |
| 6695 | } |
| 6696 | |
| 6697 | // Checks that the upload data added in AddChunksToUpload() was echoed back from |
| 6698 | // the server. |
| 6699 | void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { |
| 6700 | // This should match the chunks sent by AddChunksToUpload(). |
| 6701 | const std::string expected_data = |
| 6702 | "abcdthis is a longer chunk than before.\r\n\r\n02323"; |
| 6703 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6704 | ASSERT_EQ(1, d->response_started_count()) << "request failed. Error: " |
| 6705 | << d->request_status(); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6706 | |
| 6707 | EXPECT_FALSE(d->received_data_before_response()); |
| 6708 | |
| 6709 | EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); |
| 6710 | EXPECT_EQ(expected_data, d->data_received()); |
| 6711 | } |
| 6712 | |
| 6713 | } // namespace |
| 6714 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6715 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6716 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6717 | |
| 6718 | TestDelegate d; |
| 6719 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6720 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6721 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6722 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6723 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6724 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6725 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6726 | upload_data_stream->CreateWriter(); |
| 6727 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6728 | r->set_method("POST"); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6729 | AddDataToUpload(writer.get()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6730 | r->Start(); |
| 6731 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6732 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6733 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6734 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6735 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6736 | } |
| 6737 | } |
| 6738 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6739 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6740 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6741 | |
| 6742 | TestDelegate d; |
| 6743 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6744 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6745 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6746 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6747 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6748 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6749 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6750 | upload_data_stream->CreateWriter(); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6751 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6752 | r->set_method("POST"); |
| 6753 | r->Start(); |
| 6754 | EXPECT_TRUE(r->is_pending()); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6755 | AddDataToUpload(writer.get()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6756 | d.RunUntilComplete(); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6757 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6758 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6759 | } |
| 6760 | } |
| 6761 | |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6762 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6763 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6764 | |
| 6765 | TestDelegate d; |
| 6766 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6767 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6768 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6769 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6770 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6771 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6772 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6773 | upload_data_stream->CreateWriter(); |
| 6774 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6775 | r->set_method("POST"); |
| 6776 | r->Start(); |
| 6777 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6778 | |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6779 | // Pump messages until we start sending headers.. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6780 | base::RunLoop().RunUntilIdle(); |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6781 | |
| 6782 | // And now wait for completion. |
| 6783 | base::RunLoop run_loop; |
| 6784 | d.set_on_complete(run_loop.QuitClosure()); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6785 | AddDataToUpload(writer.get()); |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6786 | run_loop.Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6787 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6788 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6789 | } |
| 6790 | } |
| 6791 | |
| 6792 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6793 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6794 | |
| 6795 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6796 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6797 | http_test_server()->GetURL("/with-headers.html"), DEFAULT_PRIORITY, &d, |
| 6798 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6799 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6800 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6801 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6802 | const HttpResponseHeaders* headers = req->response_headers(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6803 | |
| 6804 | // Simple sanity check that response_info() accesses the same data. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6805 | EXPECT_EQ(headers, req->response_info().headers.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6806 | |
| 6807 | std::string header; |
| 6808 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 6809 | EXPECT_EQ("private", header); |
| 6810 | |
| 6811 | header.clear(); |
| 6812 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 6813 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 6814 | |
| 6815 | // The response has two "X-Multiple-Entries" headers. |
| 6816 | // This verfies our output has them concatenated together. |
| 6817 | header.clear(); |
| 6818 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 6819 | EXPECT_EQ("a, b", header); |
| 6820 | } |
| 6821 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6822 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 6823 | // security state. (see http://crbug.com/550977). |
| 6824 | #if !defined(OS_IOS) |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6825 | TEST_F(URLRequestTestHTTP, ProcessSTS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6826 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6827 | https_test_server.SetSSLConfig( |
| 6828 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6829 | https_test_server.ServeFilesFromSourceDirectory( |
| 6830 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6831 | ASSERT_TRUE(https_test_server.Start()); |
| 6832 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6833 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6834 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6835 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6836 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6837 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6838 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6839 | d.RunUntilComplete(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6840 | |
| 6841 | TransportSecurityState* security_state = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6842 | default_context().transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6843 | TransportSecurityState::STSState sts_state; |
| 6844 | TransportSecurityState::PKPState pkp_state; |
| 6845 | EXPECT_TRUE( |
| 6846 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6847 | EXPECT_FALSE( |
| 6848 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6849 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6850 | sts_state.upgrade_mode); |
| 6851 | EXPECT_TRUE(sts_state.include_subdomains); |
| 6852 | EXPECT_FALSE(pkp_state.include_subdomains); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6853 | #if defined(OS_ANDROID) |
| 6854 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6855 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6856 | EXPECT_FALSE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6857 | #endif |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6858 | } |
| 6859 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6860 | TEST_F(URLRequestTestHTTP, STSNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6861 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6862 | https_test_server.ServeFilesFromSourceDirectory( |
| 6863 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6864 | ASSERT_TRUE(https_test_server.Start()); |
| 6865 | // Make sure this test fails if the test server is changed to not |
| 6866 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6867 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 6868 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6869 | |
| 6870 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6871 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6872 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6873 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6874 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6875 | d.RunUntilComplete(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6876 | TransportSecurityState* security_state = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6877 | default_context().transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6878 | TransportSecurityState::STSState sts_state; |
| 6879 | EXPECT_FALSE( |
| 6880 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6881 | } |
| 6882 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6883 | namespace { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 6884 | const char kExpectCTStaticHostname[] = "expect-ct.preloaded.test"; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6885 | const char kPKPReportUri[] = "http://report-uri.preloaded.test/pkp"; |
| 6886 | const char kPKPHost[] = "with-report-uri-pkp.preloaded.test"; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6887 | } // namespace |
| 6888 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6889 | // Tests that reports get sent on PKP violations when a report-uri is set. |
Hiroki Nakagawa | 4e4967bcb | 2018-10-11 04:09:51 | [diff] [blame] | 6890 | TEST_F(URLRequestTestHTTP, ProcessPKPAndSendReport) { |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6891 | GURL report_uri(kPKPReportUri); |
Hiroki Nakagawa | 4e4967bcb | 2018-10-11 04:09:51 | [diff] [blame] | 6892 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6893 | https_test_server.SetSSLConfig( |
| 6894 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6895 | https_test_server.ServeFilesFromSourceDirectory( |
| 6896 | base::FilePath(kTestFilePath)); |
| 6897 | ASSERT_TRUE(https_test_server.Start()); |
| 6898 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6899 | std::string test_server_hostname = kPKPHost; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6900 | |
| 6901 | // Set up a pin for |test_server_hostname|. |
| 6902 | TransportSecurityState security_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6903 | security_state.EnableStaticPinsForTesting(); |
| 6904 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6905 | |
| 6906 | MockCertificateReportSender mock_report_sender; |
| 6907 | security_state.SetReportSender(&mock_report_sender); |
| 6908 | |
| 6909 | // Set up a MockCertVerifier to trigger a violation of the previously |
| 6910 | // set pin. |
| 6911 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6912 | ASSERT_TRUE(cert); |
| 6913 | |
| 6914 | MockCertVerifier cert_verifier; |
| 6915 | CertVerifyResult verify_result; |
| 6916 | verify_result.verified_cert = cert; |
| 6917 | verify_result.is_issued_by_known_root = true; |
| 6918 | HashValue hash3; |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6919 | ASSERT_TRUE( |
| 6920 | hash3.FromString("sha256/3333333333333333333333333333333333333333333=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6921 | verify_result.public_key_hashes.push_back(hash3); |
| 6922 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6923 | |
| 6924 | TestNetworkDelegate network_delegate; |
| 6925 | TestURLRequestContext context(true); |
| 6926 | context.set_transport_security_state(&security_state); |
| 6927 | context.set_network_delegate(&network_delegate); |
| 6928 | context.set_cert_verifier(&cert_verifier); |
| 6929 | context.Init(); |
| 6930 | |
| 6931 | // Now send a request to trigger the violation. |
| 6932 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6933 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6934 | https_test_server.GetURL(test_server_hostname, "/simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6935 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6936 | violating_request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6937 | d.RunUntilComplete(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6938 | |
| 6939 | // Check that a report was sent. |
| 6940 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 6941 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 6942 | EXPECT_EQ("application/json; charset=utf-8", |
| 6943 | mock_report_sender.latest_content_type()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6944 | std::unique_ptr<base::Value> value( |
Lei Zhang | a8b4c5fb | 2019-02-16 03:02:03 | [diff] [blame] | 6945 | base::JSONReader::ReadDeprecated(mock_report_sender.latest_report())); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6946 | ASSERT_TRUE(value); |
jdoerrie | e48b26a | 2017-12-09 14:19:08 | [diff] [blame] | 6947 | ASSERT_TRUE(value->is_dict()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6948 | base::DictionaryValue* report_dict; |
| 6949 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 6950 | std::string report_hostname; |
| 6951 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 6952 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 6953 | } |
| 6954 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6955 | // Tests that reports do not get sent on requests to static pkp hosts that |
| 6956 | // don't have pin violations. |
| 6957 | TEST_F(URLRequestTestHTTP, ProcessPKPWithNoViolation) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6958 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6959 | https_test_server.SetSSLConfig( |
| 6960 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6961 | https_test_server.ServeFilesFromSourceDirectory( |
| 6962 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6963 | ASSERT_TRUE(https_test_server.Start()); |
| 6964 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6965 | std::string test_server_hostname = kPKPHost; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6966 | |
| 6967 | TransportSecurityState security_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6968 | security_state.EnableStaticPinsForTesting(); |
| 6969 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6970 | MockCertificateReportSender mock_report_sender; |
| 6971 | security_state.SetReportSender(&mock_report_sender); |
| 6972 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6973 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6974 | ASSERT_TRUE(cert); |
Hiroki Nakagawa | 4e4967bcb | 2018-10-11 04:09:51 | [diff] [blame] | 6975 | MockCertVerifier mock_cert_verifier; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6976 | CertVerifyResult verify_result; |
| 6977 | verify_result.verified_cert = cert; |
| 6978 | verify_result.is_issued_by_known_root = true; |
| 6979 | HashValue hash; |
| 6980 | // The expected value of GoodPin1 used by |test_default::kHSTSSource|. |
| 6981 | ASSERT_TRUE( |
| 6982 | hash.FromString("sha256/Nn8jk5By4Vkq6BeOVZ7R7AC6XUUBZsWmUbJR1f1Y5FY=")); |
| 6983 | verify_result.public_key_hashes.push_back(hash); |
| 6984 | mock_cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6985 | |
| 6986 | TestNetworkDelegate network_delegate; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6987 | TestURLRequestContext context(true); |
| 6988 | context.set_transport_security_state(&security_state); |
| 6989 | context.set_network_delegate(&network_delegate); |
| 6990 | context.set_cert_verifier(&mock_cert_verifier); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6991 | context.Init(); |
| 6992 | |
| 6993 | // Now send a request that does not trigger the violation. |
| 6994 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6995 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6996 | https_test_server.GetURL(test_server_hostname, "/simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6997 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6998 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6999 | d.RunUntilComplete(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 7000 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7001 | // Check that the request succeeded, a report was not sent and the pkp was |
| 7002 | // not bypassed. |
| 7003 | EXPECT_EQ(OK, d.request_status()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 7004 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 7005 | EXPECT_EQ(std::string(), mock_report_sender.latest_report()); |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7006 | TransportSecurityState::STSState sts_state; |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 7007 | TransportSecurityState::PKPState pkp_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7008 | EXPECT_TRUE(security_state.GetStaticDomainState(test_server_hostname, |
| 7009 | &sts_state, &pkp_state)); |
| 7010 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
| 7011 | EXPECT_FALSE(request->ssl_info().pkp_bypassed); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 7012 | } |
| 7013 | |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7014 | TEST_F(URLRequestTestHTTP, PKPBypassRecorded) { |
| 7015 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7016 | https_test_server.SetSSLConfig( |
| 7017 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7018 | https_test_server.ServeFilesFromSourceDirectory( |
| 7019 | base::FilePath(kTestFilePath)); |
| 7020 | ASSERT_TRUE(https_test_server.Start()); |
| 7021 | |
| 7022 | // Set up a MockCertVerifier to be a local root that violates the pin |
| 7023 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7024 | ASSERT_TRUE(cert); |
| 7025 | |
| 7026 | MockCertVerifier cert_verifier; |
| 7027 | CertVerifyResult verify_result; |
| 7028 | verify_result.verified_cert = cert; |
| 7029 | verify_result.is_issued_by_known_root = false; |
| 7030 | HashValue hash; |
| 7031 | ASSERT_TRUE( |
| 7032 | hash.FromString("sha256/1111111111111111111111111111111111111111111=")); |
| 7033 | verify_result.public_key_hashes.push_back(hash); |
| 7034 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7035 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7036 | std::string test_server_hostname = kPKPHost; |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7037 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7038 | // Set up PKP |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7039 | TransportSecurityState security_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7040 | security_state.EnableStaticPinsForTesting(); |
| 7041 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 7042 | MockCertificateReportSender mock_report_sender; |
| 7043 | security_state.SetReportSender(&mock_report_sender); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7044 | |
| 7045 | TestNetworkDelegate network_delegate; |
| 7046 | TestURLRequestContext context(true); |
| 7047 | context.set_transport_security_state(&security_state); |
| 7048 | context.set_network_delegate(&network_delegate); |
| 7049 | context.set_cert_verifier(&cert_verifier); |
| 7050 | context.Init(); |
| 7051 | |
| 7052 | TestDelegate d; |
| 7053 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7054 | https_test_server.GetURL(test_server_hostname, "/simple.html"), |
| 7055 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7056 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7057 | d.RunUntilComplete(); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7058 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7059 | // Check that the request succeeded, a report was not sent and the PKP was |
| 7060 | // bypassed. |
| 7061 | EXPECT_EQ(OK, d.request_status()); |
| 7062 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 7063 | EXPECT_EQ(std::string(), mock_report_sender.latest_report()); |
| 7064 | TransportSecurityState::STSState sts_state; |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7065 | TransportSecurityState::PKPState pkp_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7066 | EXPECT_TRUE(security_state.GetStaticDomainState(test_server_hostname, |
| 7067 | &sts_state, &pkp_state)); |
| 7068 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7069 | EXPECT_TRUE(request->ssl_info().pkp_bypassed); |
| 7070 | } |
| 7071 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7072 | TEST_F(URLRequestTestHTTP, ProcessSTSOnce) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7073 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7074 | https_test_server.SetSSLConfig( |
| 7075 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7076 | https_test_server.ServeFilesFromSourceDirectory( |
| 7077 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7078 | ASSERT_TRUE(https_test_server.Start()); |
| 7079 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7080 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 7081 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7082 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7083 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7084 | https_test_server.GetURL("/hsts-multiple-headers.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7085 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7086 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7087 | d.RunUntilComplete(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7088 | |
| 7089 | // We should have set parameters from the first header, not the second. |
| 7090 | TransportSecurityState* security_state = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7091 | default_context().transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 7092 | TransportSecurityState::STSState sts_state; |
| 7093 | EXPECT_TRUE( |
| 7094 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 7095 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 7096 | sts_state.upgrade_mode); |
| 7097 | EXPECT_FALSE(sts_state.include_subdomains); |
| 7098 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7099 | } |
| 7100 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7101 | // An ExpectCTReporter that records the number of times OnExpectCTFailed() was |
| 7102 | // called. |
| 7103 | class MockExpectCTReporter : public TransportSecurityState::ExpectCTReporter { |
| 7104 | public: |
| 7105 | MockExpectCTReporter() : num_failures_(0) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 7106 | ~MockExpectCTReporter() override = default; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7107 | |
| 7108 | void OnExpectCTFailed(const HostPortPair& host_port_pair, |
| 7109 | const GURL& report_uri, |
estark | ae028b46 | 2017-06-20 23:25:01 | [diff] [blame] | 7110 | base::Time expiration, |
estark | bf1b5296 | 2017-05-05 17:05:25 | [diff] [blame] | 7111 | const X509Certificate* validated_certificate_chain, |
| 7112 | const X509Certificate* served_certificate_chain, |
| 7113 | const SignedCertificateTimestampAndStatusList& |
| 7114 | signed_certificate_timestamps) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7115 | num_failures_++; |
| 7116 | } |
| 7117 | |
| 7118 | uint32_t num_failures() { return num_failures_; } |
| 7119 | |
| 7120 | private: |
| 7121 | uint32_t num_failures_; |
| 7122 | }; |
| 7123 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7124 | // A CTPolicyEnforcer that returns a default CTPolicyCompliance value |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7125 | // for every certificate. |
| 7126 | class MockCTPolicyEnforcer : public CTPolicyEnforcer { |
| 7127 | public: |
| 7128 | MockCTPolicyEnforcer() |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7129 | : default_result_(ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 7130 | ~MockCTPolicyEnforcer() override = default; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7131 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7132 | ct::CTPolicyCompliance CheckCompliance( |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7133 | X509Certificate* cert, |
Ryan Sleevi | 8a9c9c1 | 2018-05-09 02:36:23 | [diff] [blame] | 7134 | const ct::SCTList& verified_scts, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 7135 | const NetLogWithSource& net_log) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7136 | return default_result_; |
| 7137 | } |
| 7138 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7139 | void set_default_result(ct::CTPolicyCompliance default_result) { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7140 | default_result_ = default_result; |
| 7141 | } |
| 7142 | |
| 7143 | private: |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7144 | ct::CTPolicyCompliance default_result_; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7145 | }; |
| 7146 | |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7147 | // Tests that Expect CT headers for the preload list are processed correctly. |
| 7148 | TEST_F(URLRequestTestHTTP, PreloadExpectCTHeader) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 7149 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 7150 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7151 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7152 | https_test_server.SetSSLConfig( |
| 7153 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7154 | https_test_server.ServeFilesFromSourceDirectory( |
| 7155 | base::FilePath(kTestFilePath)); |
| 7156 | ASSERT_TRUE(https_test_server.Start()); |
| 7157 | |
| 7158 | MockExpectCTReporter reporter; |
| 7159 | TransportSecurityState transport_security_state; |
| 7160 | transport_security_state.enable_static_expect_ct_ = true; |
| 7161 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7162 | |
| 7163 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7164 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7165 | ASSERT_TRUE(cert); |
| 7166 | MockCertVerifier cert_verifier; |
| 7167 | CertVerifyResult verify_result; |
| 7168 | verify_result.verified_cert = cert; |
| 7169 | verify_result.is_issued_by_known_root = true; |
| 7170 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7171 | |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 7172 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to trigger an Expect |
| 7173 | // CT violation. |
| 7174 | DoNothingCTVerifier ct_verifier; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7175 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7176 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7177 | ct::CTPolicyCompliance::CT_POLICY_NOT_ENOUGH_SCTS); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7178 | |
| 7179 | TestNetworkDelegate network_delegate; |
| 7180 | // Use a MockHostResolver (which by default maps all hosts to |
| 7181 | // 127.0.0.1) so that the request can be sent to a site on the Expect |
| 7182 | // CT preload list. |
| 7183 | MockHostResolver host_resolver; |
| 7184 | TestURLRequestContext context(true); |
| 7185 | context.set_host_resolver(&host_resolver); |
| 7186 | context.set_transport_security_state(&transport_security_state); |
| 7187 | context.set_network_delegate(&network_delegate); |
| 7188 | context.set_cert_verifier(&cert_verifier); |
| 7189 | context.set_cert_transparency_verifier(&ct_verifier); |
vabr | 13d0074 | 2017-06-05 10:31:46 | [diff] [blame] | 7190 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7191 | context.Init(); |
| 7192 | |
| 7193 | // Now send a request to trigger the violation. |
| 7194 | TestDelegate d; |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7195 | GURL url = https_test_server.GetURL("/expect-ct-header-preload.html"); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7196 | GURL::Replacements replace_host; |
| 7197 | replace_host.SetHostStr(kExpectCTStaticHostname); |
| 7198 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7199 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
| 7200 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7201 | violating_request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7202 | d.RunUntilComplete(); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7203 | |
| 7204 | EXPECT_EQ(1u, reporter.num_failures()); |
| 7205 | } |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7206 | |
| 7207 | // Tests that Expect CT HTTP headers are processed correctly. |
| 7208 | TEST_F(URLRequestTestHTTP, ExpectCTHeader) { |
| 7209 | base::test::ScopedFeatureList feature_list; |
| 7210 | feature_list.InitAndEnableFeature( |
| 7211 | TransportSecurityState::kDynamicExpectCTFeature); |
| 7212 | |
| 7213 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7214 | https_test_server.SetSSLConfig( |
| 7215 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7216 | https_test_server.ServeFilesFromSourceDirectory( |
| 7217 | base::FilePath(kTestFilePath)); |
| 7218 | ASSERT_TRUE(https_test_server.Start()); |
| 7219 | |
| 7220 | MockExpectCTReporter reporter; |
| 7221 | TransportSecurityState transport_security_state; |
| 7222 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7223 | |
| 7224 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7225 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7226 | ASSERT_TRUE(cert); |
| 7227 | MockCertVerifier cert_verifier; |
| 7228 | CertVerifyResult verify_result; |
| 7229 | verify_result.verified_cert = cert; |
| 7230 | verify_result.is_issued_by_known_root = true; |
| 7231 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7232 | |
| 7233 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 7234 | // compliance. |
| 7235 | DoNothingCTVerifier ct_verifier; |
| 7236 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7237 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7238 | ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7239 | |
| 7240 | TestNetworkDelegate network_delegate; |
| 7241 | // Use a MockHostResolver (which by default maps all hosts to |
| 7242 | // 127.0.0.1). |
| 7243 | MockHostResolver host_resolver; |
| 7244 | TestURLRequestContext context(true); |
| 7245 | context.set_host_resolver(&host_resolver); |
| 7246 | context.set_transport_security_state(&transport_security_state); |
| 7247 | context.set_network_delegate(&network_delegate); |
| 7248 | context.set_cert_verifier(&cert_verifier); |
| 7249 | context.set_cert_transparency_verifier(&ct_verifier); |
| 7250 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 7251 | context.Init(); |
| 7252 | |
| 7253 | // Now send a request to trigger the header processing. |
| 7254 | TestDelegate d; |
| 7255 | GURL url = https_test_server.GetURL("/expect-ct-header.html"); |
| 7256 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7257 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7258 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7259 | d.RunUntilComplete(); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7260 | |
| 7261 | TransportSecurityState::ExpectCTState state; |
| 7262 | ASSERT_TRUE( |
| 7263 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 7264 | EXPECT_TRUE(state.enforce); |
| 7265 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 7266 | } |
| 7267 | |
| 7268 | // Tests that if multiple Expect CT HTTP headers are sent, they are all |
| 7269 | // processed. |
| 7270 | TEST_F(URLRequestTestHTTP, MultipleExpectCTHeaders) { |
| 7271 | base::test::ScopedFeatureList feature_list; |
| 7272 | feature_list.InitAndEnableFeature( |
| 7273 | TransportSecurityState::kDynamicExpectCTFeature); |
| 7274 | |
| 7275 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7276 | https_test_server.SetSSLConfig( |
| 7277 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7278 | https_test_server.ServeFilesFromSourceDirectory( |
| 7279 | base::FilePath(kTestFilePath)); |
| 7280 | ASSERT_TRUE(https_test_server.Start()); |
| 7281 | |
| 7282 | MockExpectCTReporter reporter; |
| 7283 | TransportSecurityState transport_security_state; |
| 7284 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7285 | |
| 7286 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7287 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7288 | ASSERT_TRUE(cert); |
| 7289 | MockCertVerifier cert_verifier; |
| 7290 | CertVerifyResult verify_result; |
| 7291 | verify_result.verified_cert = cert; |
| 7292 | verify_result.is_issued_by_known_root = true; |
| 7293 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7294 | |
| 7295 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 7296 | // compliance. |
| 7297 | DoNothingCTVerifier ct_verifier; |
| 7298 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7299 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7300 | ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7301 | |
| 7302 | TestNetworkDelegate network_delegate; |
| 7303 | // Use a MockHostResolver (which by default maps all hosts to |
| 7304 | // 127.0.0.1). |
| 7305 | MockHostResolver host_resolver; |
| 7306 | TestURLRequestContext context(true); |
| 7307 | context.set_host_resolver(&host_resolver); |
| 7308 | context.set_transport_security_state(&transport_security_state); |
| 7309 | context.set_network_delegate(&network_delegate); |
| 7310 | context.set_cert_verifier(&cert_verifier); |
| 7311 | context.set_cert_transparency_verifier(&ct_verifier); |
| 7312 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 7313 | context.Init(); |
| 7314 | |
| 7315 | // Now send a request to trigger the header processing. |
| 7316 | TestDelegate d; |
| 7317 | GURL url = https_test_server.GetURL("/expect-ct-header-multiple.html"); |
| 7318 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7319 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7320 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7321 | d.RunUntilComplete(); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7322 | |
| 7323 | TransportSecurityState::ExpectCTState state; |
| 7324 | ASSERT_TRUE( |
| 7325 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 7326 | EXPECT_TRUE(state.enforce); |
| 7327 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 7328 | } |
| 7329 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 7330 | #endif // !defined(OS_IOS) |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7331 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7332 | #if BUILDFLAG(ENABLE_REPORTING) |
| 7333 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7334 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_DontReportIfNetworkNotAccessed) { |
| 7335 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7336 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7337 | ASSERT_TRUE(https_test_server.Start()); |
| 7338 | GURL request_url = https_test_server.GetURL("/cachetime"); |
| 7339 | |
| 7340 | TestNetworkErrorLoggingService nel_service; |
| 7341 | TestURLRequestContext context(true); |
| 7342 | context.set_network_error_logging_service(&nel_service); |
| 7343 | context.Init(); |
| 7344 | |
| 7345 | // Populate the cache. |
| 7346 | TestDelegate d; |
| 7347 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7348 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7349 | request->Start(); |
| 7350 | d.RunUntilComplete(); |
| 7351 | |
| 7352 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7353 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7354 | nel_service.errors()[0]; |
| 7355 | EXPECT_EQ(request_url, error.uri); |
| 7356 | EXPECT_EQ(200, error.status_code); |
| 7357 | EXPECT_EQ(OK, error.type); |
| 7358 | |
| 7359 | request = context.CreateRequest(request_url, DEFAULT_PRIORITY, &d, |
| 7360 | TRAFFIC_ANNOTATION_FOR_TESTS); |
| 7361 | request->Start(); |
| 7362 | d.RunUntilComplete(); |
| 7363 | |
| 7364 | EXPECT_FALSE(request->response_info().network_accessed); |
| 7365 | EXPECT_TRUE(request->response_info().was_cached); |
| 7366 | // No additional NEL report was generated. |
| 7367 | EXPECT_EQ(1u, nel_service.errors().size()); |
| 7368 | } |
| 7369 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7370 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_BasicSuccess) { |
| 7371 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7372 | https_test_server.ServeFilesFromSourceDirectory( |
| 7373 | base::FilePath(kTestFilePath)); |
| 7374 | ASSERT_TRUE(https_test_server.Start()); |
| 7375 | GURL request_url = https_test_server.GetURL("/simple.html"); |
| 7376 | |
| 7377 | TestNetworkErrorLoggingService nel_service; |
| 7378 | TestURLRequestContext context(true); |
| 7379 | context.set_network_error_logging_service(&nel_service); |
| 7380 | context.Init(); |
| 7381 | |
| 7382 | TestDelegate d; |
| 7383 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7384 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7385 | request->Start(); |
| 7386 | d.RunUntilComplete(); |
| 7387 | |
| 7388 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7389 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7390 | nel_service.errors()[0]; |
| 7391 | EXPECT_EQ(request_url, error.uri); |
| 7392 | EXPECT_EQ(200, error.status_code); |
| 7393 | EXPECT_EQ(OK, error.type); |
| 7394 | } |
| 7395 | |
| 7396 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_BasicError) { |
| 7397 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7398 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7399 | ASSERT_TRUE(https_test_server.Start()); |
| 7400 | GURL request_url = https_test_server.GetURL("/close-socket"); |
| 7401 | |
| 7402 | TestNetworkErrorLoggingService nel_service; |
| 7403 | TestURLRequestContext context(true); |
| 7404 | context.set_network_error_logging_service(&nel_service); |
| 7405 | context.Init(); |
| 7406 | |
| 7407 | TestDelegate d; |
| 7408 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7409 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7410 | request->Start(); |
| 7411 | d.RunUntilComplete(); |
| 7412 | |
| 7413 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7414 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7415 | nel_service.errors()[0]; |
| 7416 | EXPECT_EQ(request_url, error.uri); |
| 7417 | EXPECT_EQ(0, error.status_code); |
| 7418 | EXPECT_EQ(ERR_EMPTY_RESPONSE, error.type); |
| 7419 | } |
| 7420 | |
| 7421 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_Redirect) { |
| 7422 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7423 | https_test_server.ServeFilesFromSourceDirectory( |
| 7424 | base::FilePath(kTestFilePath)); |
| 7425 | ASSERT_TRUE(https_test_server.Start()); |
| 7426 | GURL request_url = https_test_server.GetURL("/redirect-test.html"); |
| 7427 | GURL redirect_url = https_test_server.GetURL("/with-headers.html"); |
| 7428 | |
| 7429 | TestNetworkErrorLoggingService nel_service; |
| 7430 | TestURLRequestContext context(true); |
| 7431 | context.set_network_error_logging_service(&nel_service); |
| 7432 | context.Init(); |
| 7433 | |
| 7434 | TestDelegate d; |
| 7435 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7436 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7437 | request->Start(); |
| 7438 | d.RunUntilComplete(); |
| 7439 | |
| 7440 | ASSERT_EQ(2u, nel_service.errors().size()); |
| 7441 | const TestNetworkErrorLoggingService::RequestDetails& error1 = |
| 7442 | nel_service.errors()[0]; |
| 7443 | EXPECT_EQ(request_url, error1.uri); |
| 7444 | EXPECT_EQ(302, error1.status_code); |
| 7445 | EXPECT_EQ(OK, error1.type); |
| 7446 | const TestNetworkErrorLoggingService::RequestDetails& error2 = |
| 7447 | nel_service.errors()[1]; |
| 7448 | EXPECT_EQ(redirect_url, error2.uri); |
| 7449 | EXPECT_EQ(200, error2.status_code); |
| 7450 | EXPECT_EQ(OK, error2.type); |
| 7451 | } |
| 7452 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7453 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_RedirectWithoutLocationHeader) { |
| 7454 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7455 | https_test_server.ServeFilesFromSourceDirectory( |
| 7456 | base::FilePath(kTestFilePath)); |
| 7457 | ASSERT_TRUE(https_test_server.Start()); |
| 7458 | GURL request_url = https_test_server.GetURL("/308-without-location-header"); |
| 7459 | |
| 7460 | TestNetworkErrorLoggingService nel_service; |
| 7461 | TestURLRequestContext context(true); |
| 7462 | context.set_network_error_logging_service(&nel_service); |
| 7463 | context.Init(); |
| 7464 | |
| 7465 | TestDelegate d; |
| 7466 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7467 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7468 | request->Start(); |
| 7469 | d.RunUntilComplete(); |
| 7470 | |
| 7471 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7472 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7473 | nel_service.errors()[0]; |
| 7474 | EXPECT_EQ(request_url, error.uri); |
| 7475 | EXPECT_EQ(308, error.status_code); |
| 7476 | // The body of the response was successfully read. |
| 7477 | EXPECT_EQ(OK, error.type); |
| 7478 | } |
| 7479 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7480 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_Auth) { |
| 7481 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7482 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7483 | ASSERT_TRUE(https_test_server.Start()); |
| 7484 | GURL request_url = https_test_server.GetURL("/auth-basic"); |
| 7485 | |
| 7486 | TestNetworkErrorLoggingService nel_service; |
| 7487 | TestURLRequestContext context(true); |
| 7488 | context.set_network_error_logging_service(&nel_service); |
| 7489 | context.Init(); |
| 7490 | |
| 7491 | TestDelegate d; |
| 7492 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7493 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7494 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7495 | request->Start(); |
| 7496 | d.RunUntilComplete(); |
| 7497 | |
| 7498 | ASSERT_EQ(2u, nel_service.errors().size()); |
| 7499 | const TestNetworkErrorLoggingService::RequestDetails& error1 = |
| 7500 | nel_service.errors()[0]; |
| 7501 | EXPECT_EQ(request_url, error1.uri); |
| 7502 | EXPECT_EQ(401, error1.status_code); |
| 7503 | EXPECT_EQ(OK, error1.type); |
| 7504 | const TestNetworkErrorLoggingService::RequestDetails& error2 = |
| 7505 | nel_service.errors()[1]; |
| 7506 | EXPECT_EQ(request_url, error2.uri); |
| 7507 | EXPECT_EQ(200, error2.status_code); |
| 7508 | EXPECT_EQ(OK, error2.type); |
| 7509 | } |
| 7510 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7511 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_304Response) { |
| 7512 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7513 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7514 | ASSERT_TRUE(https_test_server.Start()); |
| 7515 | GURL request_url = https_test_server.GetURL("/auth-basic"); |
| 7516 | |
| 7517 | TestNetworkErrorLoggingService nel_service; |
| 7518 | TestURLRequestContext context(true); |
| 7519 | context.set_network_error_logging_service(&nel_service); |
| 7520 | context.Init(); |
| 7521 | |
| 7522 | // populate the cache |
| 7523 | { |
| 7524 | TestDelegate d; |
| 7525 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7526 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 7527 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7528 | r->Start(); |
| 7529 | d.RunUntilComplete(); |
| 7530 | } |
| 7531 | ASSERT_EQ(2u, nel_service.errors().size()); |
| 7532 | const TestNetworkErrorLoggingService::RequestDetails& error1 = |
| 7533 | nel_service.errors()[0]; |
| 7534 | EXPECT_EQ(request_url, error1.uri); |
| 7535 | EXPECT_EQ(401, error1.status_code); |
| 7536 | EXPECT_EQ(OK, error1.type); |
| 7537 | const TestNetworkErrorLoggingService::RequestDetails& error2 = |
| 7538 | nel_service.errors()[1]; |
| 7539 | EXPECT_EQ(request_url, error2.uri); |
| 7540 | EXPECT_EQ(200, error2.status_code); |
| 7541 | EXPECT_EQ(OK, error2.type); |
| 7542 | |
| 7543 | // repeat request with end-to-end validation. since auth-basic results in a |
| 7544 | // cachable page, we expect this test to result in a 304. in which case, the |
| 7545 | // response should be fetched from the cache. |
| 7546 | { |
| 7547 | TestDelegate d; |
| 7548 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7549 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 7550 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7551 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 7552 | r->Start(); |
| 7553 | d.RunUntilComplete(); |
| 7554 | |
| 7555 | // Should be the same cached document. |
| 7556 | EXPECT_TRUE(r->was_cached()); |
| 7557 | } |
| 7558 | ASSERT_EQ(3u, nel_service.errors().size()); |
| 7559 | const TestNetworkErrorLoggingService::RequestDetails& error3 = |
| 7560 | nel_service.errors()[2]; |
| 7561 | EXPECT_EQ(request_url, error3.uri); |
| 7562 | EXPECT_EQ(304, error3.status_code); |
| 7563 | EXPECT_EQ(OK, error3.type); |
| 7564 | } |
| 7565 | |
| 7566 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_CancelInResponseStarted) { |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7567 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7568 | https_test_server.ServeFilesFromSourceDirectory( |
| 7569 | base::FilePath(kTestFilePath)); |
| 7570 | ASSERT_TRUE(https_test_server.Start()); |
| 7571 | GURL request_url = https_test_server.GetURL("/simple.html"); |
| 7572 | |
| 7573 | TestNetworkErrorLoggingService nel_service; |
| 7574 | TestURLRequestContext context(true); |
| 7575 | context.set_network_error_logging_service(&nel_service); |
| 7576 | context.Init(); |
| 7577 | |
| 7578 | TestDelegate d; |
| 7579 | d.set_cancel_in_response_started(true); |
| 7580 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7581 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7582 | request->Start(); |
| 7583 | d.RunUntilComplete(); |
| 7584 | |
| 7585 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7586 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7587 | nel_service.errors()[0]; |
| 7588 | EXPECT_EQ(request_url, error.uri); |
| 7589 | EXPECT_EQ(200, error.status_code); |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7590 | // Headers were received and the body should have been read but was not. |
| 7591 | EXPECT_EQ(ERR_ABORTED, error.type); |
| 7592 | } |
| 7593 | |
| 7594 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_CancelOnDataReceived) { |
| 7595 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7596 | https_test_server.ServeFilesFromSourceDirectory( |
| 7597 | base::FilePath(kTestFilePath)); |
| 7598 | ASSERT_TRUE(https_test_server.Start()); |
| 7599 | GURL request_url = https_test_server.GetURL("/simple.html"); |
| 7600 | |
| 7601 | TestNetworkErrorLoggingService nel_service; |
| 7602 | TestURLRequestContext context(true); |
| 7603 | context.set_network_error_logging_service(&nel_service); |
| 7604 | context.Init(); |
| 7605 | |
| 7606 | TestDelegate d; |
| 7607 | d.set_cancel_in_received_data(true); |
| 7608 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7609 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7610 | request->Start(); |
| 7611 | d.RunUntilComplete(); |
| 7612 | |
| 7613 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7614 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7615 | nel_service.errors()[0]; |
| 7616 | EXPECT_EQ(request_url, error.uri); |
| 7617 | EXPECT_EQ(200, error.status_code); |
| 7618 | // Data was received but the body was not completely read. |
| 7619 | EXPECT_EQ(ERR_ABORTED, error.type); |
| 7620 | } |
| 7621 | |
| 7622 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_CancelRedirect) { |
| 7623 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7624 | https_test_server.ServeFilesFromSourceDirectory( |
| 7625 | base::FilePath(kTestFilePath)); |
| 7626 | ASSERT_TRUE(https_test_server.Start()); |
| 7627 | GURL request_url = https_test_server.GetURL("/redirect-test.html"); |
| 7628 | |
| 7629 | TestNetworkErrorLoggingService nel_service; |
| 7630 | TestURLRequestContext context(true); |
| 7631 | context.set_network_error_logging_service(&nel_service); |
| 7632 | context.Init(); |
| 7633 | |
| 7634 | TestDelegate d; |
| 7635 | d.set_cancel_in_received_redirect(true); |
| 7636 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7637 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7638 | request->Start(); |
| 7639 | d.RunUntilComplete(); |
| 7640 | |
| 7641 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7642 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7643 | nel_service.errors()[0]; |
| 7644 | EXPECT_EQ(request_url, error.uri); |
| 7645 | EXPECT_EQ(302, error.status_code); |
| 7646 | // A valid HTTP response was received, even though the request was cancelled. |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7647 | EXPECT_EQ(OK, error.type); |
| 7648 | } |
| 7649 | |
| 7650 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 7651 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7652 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7653 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7654 | |
| 7655 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7656 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7657 | http_test_server()->GetURL("/content-type-normalization.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7658 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7659 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7660 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7661 | |
| 7662 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7663 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7664 | EXPECT_EQ("text/html", mime_type); |
| 7665 | |
| 7666 | std::string charset; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7667 | req->GetCharset(&charset); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7668 | EXPECT_EQ("utf-8", charset); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7669 | req->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7670 | } |
| 7671 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7672 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) { |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7673 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7674 | GURL data_url("data:,foo"); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7675 | DataProtocolHandler data_protocol_handler; |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7676 | EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7677 | |
| 7678 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7679 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7680 | } |
| 7681 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7682 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7683 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) { |
| 7684 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
| 7685 | GURL file_url("file:///foo.txt"); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 7686 | FileProtocolHandler file_protocol_handler( |
| 7687 | base::ThreadTaskRunnerHandle::Get()); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7688 | EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url)); |
| 7689 | |
| 7690 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7691 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(file_url)); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7692 | } |
| 7693 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7694 | TEST_F(URLRequestTestHTTP, RestrictFileRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7695 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7696 | |
| 7697 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7698 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7699 | http_test_server()->GetURL("/redirect-to-file.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7700 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7701 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7702 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7703 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7704 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7705 | |
| 7706 | // The redirect should have been rejected before reporting it to the caller. |
| 7707 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7708 | } |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7709 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7710 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7711 | TEST_F(URLRequestTestHTTP, RestrictDataRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7712 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7713 | |
| 7714 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7715 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7716 | http_test_server()->GetURL("/redirect-to-data.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7717 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7718 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7719 | d.RunUntilComplete(); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7720 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7721 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7722 | |
| 7723 | // The redirect should have been rejected before reporting it to the |
| 7724 | // caller. See https://crbug.com/723796 |
| 7725 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7726 | } |
| 7727 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7728 | // Test that redirects to invalid URLs are rejected. See |
| 7729 | // https://crbug.com/462272. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7730 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7731 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7732 | |
| 7733 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7734 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7735 | http_test_server()->GetURL("/redirect-to-invalid-url.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7736 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7737 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7738 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7739 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7740 | EXPECT_EQ(1, d.response_started_count()); |
| 7741 | EXPECT_EQ(ERR_INVALID_REDIRECT, d.request_status()); |
| 7742 | |
| 7743 | // The redirect should have been rejected before reporting it to the caller. |
| 7744 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7745 | } |
| 7746 | |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7747 | // Make sure redirects are cached, despite not reading their bodies. |
| 7748 | TEST_F(URLRequestTestHTTP, CacheRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7749 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7750 | GURL redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7751 | http_test_server()->GetURL("/redirect302-to-echo-cacheable"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7752 | |
| 7753 | { |
| 7754 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7755 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7756 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7757 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7758 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7759 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7760 | EXPECT_EQ(1, d.received_redirect_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7761 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7762 | } |
| 7763 | |
| 7764 | { |
| 7765 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7766 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7767 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7768 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 7769 | d.RunUntilRedirect(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7770 | |
| 7771 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7772 | EXPECT_EQ(0, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7773 | EXPECT_TRUE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7774 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 7775 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 7776 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7777 | d.RunUntilComplete(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7778 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7779 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7780 | EXPECT_EQ(OK, d.request_status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7781 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7782 | } |
| 7783 | } |
| 7784 | |
| 7785 | // Make sure a request isn't cached when a NetworkDelegate forces a redirect |
| 7786 | // when the headers are read, since the body won't have been read. |
| 7787 | TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7788 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7789 | // URL that is normally cached. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7790 | GURL initial_url = http_test_server()->GetURL("/cachetime"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7791 | |
| 7792 | { |
| 7793 | // Set up the TestNetworkDelegate tp force a redirect. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7794 | GURL redirect_to_url = http_test_server()->GetURL("/echo"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7795 | default_network_delegate_.set_redirect_on_headers_received_url( |
| 7796 | redirect_to_url); |
| 7797 | |
| 7798 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7799 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7800 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7801 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7802 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7803 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7804 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7805 | EXPECT_EQ(redirect_to_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7806 | } |
| 7807 | |
| 7808 | { |
| 7809 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7810 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7811 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7812 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7813 | d.RunUntilComplete(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7814 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7815 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7816 | EXPECT_FALSE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7817 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7818 | EXPECT_EQ(initial_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7819 | } |
| 7820 | } |
| 7821 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7822 | // Tests that redirection to an unsafe URL is allowed when it has been marked as |
| 7823 | // safe. |
Ryan Sleevi | a9d6aa6 | 2019-07-26 13:32:18 | [diff] [blame] | 7824 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToAllowedUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7825 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7826 | |
| 7827 | GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url"); |
| 7828 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7829 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7830 | |
| 7831 | TestDelegate d; |
| 7832 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7833 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7834 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 7835 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7836 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7837 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7838 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7839 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7840 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7841 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7842 | EXPECT_EQ(unsafe_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7843 | EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received()); |
| 7844 | } |
| 7845 | } |
| 7846 | |
| 7847 | // Tests that a redirect to a different unsafe URL is blocked, even after adding |
Ryan Sleevi | a9d6aa6 | 2019-07-26 13:32:18 | [diff] [blame] | 7848 | // some other URL to the allowlist. |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7849 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7850 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7851 | |
| 7852 | GURL unsafe_url("data:text/html,something"); |
| 7853 | GURL different_unsafe_url("data:text/html,something-else"); |
| 7854 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7855 | default_network_delegate_.set_allowed_unsafe_redirect_url( |
| 7856 | different_unsafe_url); |
| 7857 | |
| 7858 | TestDelegate d; |
| 7859 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7860 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7861 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 7862 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7863 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7864 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7865 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7866 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7867 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7868 | |
| 7869 | // The redirect should have been rejected before reporting it to the caller. |
| 7870 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7871 | } |
| 7872 | } |
| 7873 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7874 | // Redirects from an URL with fragment to an unsafe URL with fragment should |
| 7875 | // be allowed, and the reference fragment of the target URL should be preserved. |
| 7876 | TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7877 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7878 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7879 | GURL original_url(http_test_server()->GetURL("/original#fragment1")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7880 | GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 7881 | GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 7882 | |
| 7883 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7884 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7885 | |
| 7886 | TestDelegate d; |
| 7887 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7888 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7889 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7890 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7891 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7892 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7893 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7894 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7895 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7896 | EXPECT_EQ(original_url, r->original_url()); |
| 7897 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7898 | } |
| 7899 | } |
| 7900 | |
| 7901 | // When a delegate has specified a safe redirect URL, but it does not match the |
| 7902 | // redirect target, then do not prevent the reference fragment from being added. |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7903 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7904 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7905 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7906 | GURL original_url(http_test_server()->GetURL("/original#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7907 | GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7908 | GURL redirect_url(http_test_server()->GetURL("/target")); |
| 7909 | GURL expected_redirect_url( |
| 7910 | http_test_server()->GetURL("/target#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7911 | |
| 7912 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 7913 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7914 | |
| 7915 | TestDelegate d; |
| 7916 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7917 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7918 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7919 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7920 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7921 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7922 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7923 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7924 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7925 | EXPECT_EQ(original_url, r->original_url()); |
| 7926 | EXPECT_EQ(expected_redirect_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7927 | } |
| 7928 | } |
| 7929 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7930 | // When a delegate has specified a safe redirect URL, assume that the redirect |
| 7931 | // URL should not be changed. In particular, the reference fragment should not |
| 7932 | // be modified. |
| 7933 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7934 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7935 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7936 | GURL original_url( |
| 7937 | http_test_server()->GetURL("/original#should-not-be-appended")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7938 | GURL redirect_url("data:text/html,expect-no-reference-fragment"); |
| 7939 | |
| 7940 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 7941 | default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url); |
| 7942 | |
| 7943 | TestDelegate d; |
| 7944 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7945 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7946 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7947 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7948 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7949 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7950 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7951 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7952 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7953 | EXPECT_EQ(original_url, r->original_url()); |
| 7954 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7955 | } |
| 7956 | } |
| 7957 | |
| 7958 | // When a URLRequestRedirectJob is created, the redirection must be followed and |
| 7959 | // the reference fragment of the target URL must not be modified. |
| 7960 | TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7961 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7962 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7963 | GURL original_url( |
| 7964 | http_test_server()->GetURL("/original#should-not-be-appended")); |
| 7965 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7966 | |
| 7967 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7968 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7969 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7970 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7971 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7972 | r.get(), &default_network_delegate_, redirect_url, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7973 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 7974 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7975 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7976 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7977 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7978 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7979 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7980 | EXPECT_EQ(original_url, r->original_url()); |
| 7981 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7982 | } |
| 7983 | |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7984 | TEST_F(URLRequestTestHTTP, UnsupportedReferrerScheme) { |
| 7985 | ASSERT_TRUE(http_test_server()->Start()); |
| 7986 | |
| 7987 | const std::string referrer("foobar://totally.legit.referrer"); |
| 7988 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7989 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7990 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7991 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7992 | req->SetReferrer(referrer); |
| 7993 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7994 | d.RunUntilComplete(); |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7995 | |
| 7996 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 7997 | } |
| 7998 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7999 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8000 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8001 | |
| 8002 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8003 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8004 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8005 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8006 | req->SetReferrer("http://user:[email protected]/"); |
| 8007 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8008 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8009 | |
| 8010 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 8011 | } |
| 8012 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8013 | TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8014 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8015 | |
| 8016 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8017 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8018 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8019 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8020 | req->SetReferrer("http://foo.com/test#fragment"); |
| 8021 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8022 | d.RunUntilComplete(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8023 | |
| 8024 | EXPECT_EQ(std::string("http://foo.com/test"), d.data_received()); |
| 8025 | } |
| 8026 | |
| 8027 | TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8028 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8029 | |
| 8030 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8031 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8032 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8033 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8034 | req->SetReferrer("http://foo.com/test#fragment"); |
| 8035 | req->SetReferrer(""); |
| 8036 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8037 | d.RunUntilComplete(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8038 | |
| 8039 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 8040 | } |
| 8041 | |
Mike West | fbd4411 | 2019-05-08 18:23:37 | [diff] [blame] | 8042 | TEST_F(URLRequestTestHTTP, CapRefererDisabled) { |
| 8043 | ASSERT_TRUE(http_test_server()->Start()); |
| 8044 | |
| 8045 | // Create a string, and pad it out to ~10k with a very exciting path. |
| 8046 | std::string long_referer_header = "http://foo.com/"; |
| 8047 | long_referer_header.resize(10000, 'a'); |
| 8048 | |
| 8049 | // If the feature isn't enabled, a long `referer` will remain long. |
| 8050 | TestDelegate d; |
Mike West | 9e3affdd | 2019-06-07 07:43:57 | [diff] [blame] | 8051 | base::test::ScopedFeatureList feature_list; |
| 8052 | feature_list.InitAndDisableFeature(features::kCapRefererHeaderLength); |
Mike West | fbd4411 | 2019-05-08 18:23:37 | [diff] [blame] | 8053 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8054 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8055 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8056 | req->SetReferrer(long_referer_header); |
| 8057 | req->Start(); |
| 8058 | d.RunUntilComplete(); |
| 8059 | |
| 8060 | EXPECT_EQ(long_referer_header, d.data_received()); |
| 8061 | } |
| 8062 | |
| 8063 | TEST_F(URLRequestTestHTTP, CapRefererHeaderLengthEnabled) { |
| 8064 | ASSERT_TRUE(http_test_server()->Start()); |
| 8065 | |
| 8066 | // Create a string, and pad it out to ~10k with a very exciting path. |
| 8067 | std::string long_referer_header = "http://foo.com/"; |
| 8068 | long_referer_header.resize(10000, 'a'); |
| 8069 | |
| 8070 | // If the feature is enabled without params, a `referer` longer than 4096 |
| 8071 | // bytes will be shortened. |
| 8072 | { |
| 8073 | TestDelegate d; |
| 8074 | base::test::ScopedFeatureList feature_list; |
| 8075 | feature_list.InitAndEnableFeature(features::kCapRefererHeaderLength); |
| 8076 | |
| 8077 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8078 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8079 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8080 | req->SetReferrer(long_referer_header); |
| 8081 | req->Start(); |
| 8082 | d.RunUntilComplete(); |
| 8083 | |
| 8084 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 8085 | } |
| 8086 | |
| 8087 | // If the feature is enabled with params, they will govern the shortening |
| 8088 | // behavior as expected. The following three tests verify behavior for a |
| 8089 | // param larger than the referrer length, exactly the same as the string |
| 8090 | // length, and shorter than the string length. |
| 8091 | { |
| 8092 | TestDelegate d; |
| 8093 | std::map<std::string, std::string> params; |
| 8094 | params["MaxRefererHeaderLength"] = |
| 8095 | base::NumberToString(long_referer_header.length() + 1); |
| 8096 | |
| 8097 | base::test::ScopedFeatureList feature_list; |
| 8098 | feature_list.InitAndEnableFeatureWithParameters( |
| 8099 | features::kCapRefererHeaderLength, params); |
| 8100 | |
| 8101 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8102 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8103 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8104 | req->SetReferrer(long_referer_header); |
| 8105 | req->Start(); |
| 8106 | d.RunUntilComplete(); |
| 8107 | |
| 8108 | EXPECT_EQ(long_referer_header, d.data_received()); |
| 8109 | } |
| 8110 | |
| 8111 | { |
| 8112 | TestDelegate d; |
| 8113 | std::map<std::string, std::string> params; |
| 8114 | params["MaxRefererHeaderLength"] = |
| 8115 | base::NumberToString(long_referer_header.length()); |
| 8116 | |
| 8117 | base::test::ScopedFeatureList feature_list; |
| 8118 | feature_list.InitAndEnableFeatureWithParameters( |
| 8119 | features::kCapRefererHeaderLength, params); |
| 8120 | |
| 8121 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8122 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8123 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8124 | req->SetReferrer(long_referer_header); |
| 8125 | req->Start(); |
| 8126 | d.RunUntilComplete(); |
| 8127 | |
| 8128 | EXPECT_EQ(long_referer_header, d.data_received()); |
| 8129 | } |
| 8130 | |
| 8131 | { |
| 8132 | TestDelegate d; |
| 8133 | std::map<std::string, std::string> params; |
| 8134 | params["MaxRefererHeaderLength"] = |
| 8135 | base::NumberToString(long_referer_header.length() - 1); |
| 8136 | |
| 8137 | base::test::ScopedFeatureList feature_list; |
| 8138 | feature_list.InitAndEnableFeatureWithParameters( |
| 8139 | features::kCapRefererHeaderLength, params); |
| 8140 | |
| 8141 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8142 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8143 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8144 | req->SetReferrer(long_referer_header); |
| 8145 | req->Start(); |
| 8146 | d.RunUntilComplete(); |
| 8147 | |
| 8148 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 8149 | } |
| 8150 | } |
| 8151 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8152 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8153 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8154 | |
| 8155 | TestDelegate d; |
| 8156 | { |
| 8157 | d.set_cancel_in_received_redirect(true); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8158 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8159 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 8160 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8161 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8162 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8163 | |
| 8164 | EXPECT_EQ(1, d.response_started_count()); |
| 8165 | EXPECT_EQ(0, d.bytes_received()); |
| 8166 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8167 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8168 | } |
| 8169 | } |
| 8170 | |
| 8171 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8172 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8173 | |
| 8174 | TestDelegate d; |
| 8175 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8176 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8177 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8178 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8179 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8180 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 8181 | d.RunUntilRedirect(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8182 | |
| 8183 | EXPECT_EQ(1, d.received_redirect_count()); |
Shivani Sharma | c18f976 | 2017-10-23 16:43:23 | [diff] [blame] | 8184 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8185 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 8186 | d.ClearFullRequestHeaders(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8187 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8188 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 8189 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8190 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8191 | |
| 8192 | EXPECT_EQ(1, d.response_started_count()); |
| 8193 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8194 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8195 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 8196 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 8197 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 8198 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8199 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 8200 | |
| 8201 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 8202 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8203 | EXPECT_EQ(contents, d.data_received()); |
| 8204 | } |
| 8205 | } |
| 8206 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8207 | TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8208 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8209 | |
| 8210 | TestDelegate d; |
| 8211 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8212 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8213 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8214 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8215 | |
| 8216 | EXPECT_FALSE(d.have_full_request_headers()); |
| 8217 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8218 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 8219 | d.RunUntilRedirect(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8220 | |
| 8221 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8222 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8223 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 8224 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8225 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8226 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8227 | GURL target_url(http_test_server()->GetURL("/with-headers.html")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8228 | EXPECT_EQ(1, d.response_started_count()); |
| 8229 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8230 | CheckFullRequestHeaders(d.full_request_headers(), target_url); |
| 8231 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8232 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8233 | |
| 8234 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 8235 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 8236 | path = path.Append(kTestFilePath); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8237 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 8238 | |
| 8239 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 8240 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8241 | EXPECT_EQ(contents, d.data_received()); |
| 8242 | } |
| 8243 | } |
| 8244 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8245 | TEST_F(URLRequestTestHTTP, DeferredRedirect_ModifiedHeaders) { |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8246 | ASSERT_TRUE(http_test_server()->Start()); |
| 8247 | |
| 8248 | TestDelegate d; |
| 8249 | { |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8250 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8251 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8252 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8253 | |
| 8254 | // Set initial headers for the request. |
| 8255 | req->SetExtraRequestHeaderByName("Header1", "Value1", true /* overwrite */); |
| 8256 | req->SetExtraRequestHeaderByName("Header2", "Value2", true /* overwrite */); |
| 8257 | |
| 8258 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 8259 | d.RunUntilRedirect(); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8260 | |
| 8261 | // Initial request should only have initial headers. |
| 8262 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8263 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8264 | const HttpRequestHeaders& sent_headers1 = d.full_request_headers(); |
| 8265 | std::string sent_value; |
| 8266 | EXPECT_TRUE(sent_headers1.GetHeader("Header1", &sent_value)); |
| 8267 | EXPECT_EQ("Value1", sent_value); |
| 8268 | EXPECT_TRUE(sent_headers1.GetHeader("Header2", &sent_value)); |
| 8269 | EXPECT_EQ("Value2", sent_value); |
| 8270 | EXPECT_FALSE(sent_headers1.GetHeader("Header3", &sent_value)); |
| 8271 | d.ClearFullRequestHeaders(); |
| 8272 | |
| 8273 | // Overwrite Header2 and add Header3. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8274 | net::HttpRequestHeaders modified_headers; |
| 8275 | modified_headers.SetHeader("Header2", ""); |
| 8276 | modified_headers.SetHeader("Header3", "Value3"); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8277 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8278 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 8279 | modified_headers); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8280 | d.RunUntilComplete(); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8281 | |
| 8282 | EXPECT_EQ(1, d.response_started_count()); |
| 8283 | EXPECT_FALSE(d.received_data_before_response()); |
| 8284 | EXPECT_EQ(OK, d.request_status()); |
| 8285 | |
| 8286 | // Redirected request should also have modified headers. |
| 8287 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8288 | const HttpRequestHeaders& sent_headers2 = d.full_request_headers(); |
| 8289 | EXPECT_TRUE(sent_headers2.GetHeader("Header1", &sent_value)); |
| 8290 | EXPECT_EQ("Value1", sent_value); |
| 8291 | EXPECT_TRUE(sent_headers2.GetHeader("Header2", &sent_value)); |
| 8292 | EXPECT_EQ("", sent_value); |
| 8293 | EXPECT_TRUE(sent_headers2.GetHeader("Header3", &sent_value)); |
| 8294 | EXPECT_EQ("Value3", sent_value); |
| 8295 | } |
| 8296 | } |
| 8297 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8298 | TEST_F(URLRequestTestHTTP, DeferredRedirect_RemovedHeaders) { |
| 8299 | ASSERT_TRUE(http_test_server()->Start()); |
| 8300 | |
| 8301 | TestDelegate d; |
| 8302 | { |
| 8303 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
| 8304 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8305 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8306 | |
| 8307 | // Set initial headers for the request. |
| 8308 | req->SetExtraRequestHeaderByName("Header1", "Value1", true /* overwrite */); |
| 8309 | req->SetExtraRequestHeaderByName("Header2", "Value2", true /* overwrite */); |
| 8310 | |
| 8311 | req->Start(); |
| 8312 | d.RunUntilRedirect(); |
| 8313 | |
| 8314 | // Initial request should have initial headers. |
| 8315 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8316 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8317 | const HttpRequestHeaders& sent_headers1 = d.full_request_headers(); |
| 8318 | std::string sent_value; |
| 8319 | EXPECT_TRUE(sent_headers1.GetHeader("Header1", &sent_value)); |
| 8320 | EXPECT_EQ("Value1", sent_value); |
| 8321 | EXPECT_TRUE(sent_headers1.GetHeader("Header2", &sent_value)); |
| 8322 | EXPECT_EQ("Value2", sent_value); |
| 8323 | d.ClearFullRequestHeaders(); |
| 8324 | |
| 8325 | // Keep Header1 and remove Header2. |
| 8326 | std::vector<std::string> removed_headers({"Header2"}); |
| 8327 | req->FollowDeferredRedirect(removed_headers, |
| 8328 | base::nullopt /* modified_headers */); |
| 8329 | d.RunUntilComplete(); |
| 8330 | |
| 8331 | EXPECT_EQ(1, d.response_started_count()); |
| 8332 | EXPECT_FALSE(d.received_data_before_response()); |
| 8333 | EXPECT_EQ(OK, d.request_status()); |
| 8334 | |
| 8335 | // Redirected request should also have |
| 8336 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8337 | const HttpRequestHeaders& sent_headers2 = d.full_request_headers(); |
| 8338 | EXPECT_TRUE(sent_headers2.GetHeader("Header1", &sent_value)); |
| 8339 | EXPECT_EQ("Value1", sent_value); |
| 8340 | EXPECT_FALSE(sent_headers2.GetHeader("Header2", &sent_value)); |
| 8341 | } |
| 8342 | } |
| 8343 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8344 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8345 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8346 | |
| 8347 | TestDelegate d; |
| 8348 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8349 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8350 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 8351 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8352 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 8353 | d.RunUntilRedirect(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8354 | |
| 8355 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8356 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8357 | req->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8358 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8359 | |
| 8360 | EXPECT_EQ(1, d.response_started_count()); |
| 8361 | EXPECT_EQ(0, d.bytes_received()); |
| 8362 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8363 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8364 | } |
| 8365 | } |
| 8366 | |
| 8367 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8368 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8369 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8370 | // Populate the cache. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8371 | { |
| 8372 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8373 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8374 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8375 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8376 | HttpRequestHeaders headers; |
| 8377 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8378 | req->SetExtraRequestHeaders(headers); |
| 8379 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8380 | d.RunUntilComplete(); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8381 | |
| 8382 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8383 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8384 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8385 | } |
| 8386 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8387 | // Expect a cache hit. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8388 | { |
| 8389 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8390 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8391 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8392 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8393 | HttpRequestHeaders headers; |
| 8394 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8395 | req->SetExtraRequestHeaders(headers); |
| 8396 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8397 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8398 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8399 | EXPECT_TRUE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8400 | |
| 8401 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8402 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8403 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8404 | } |
| 8405 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8406 | // Expect a cache miss. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8407 | { |
| 8408 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8409 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8410 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8411 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8412 | HttpRequestHeaders headers; |
| 8413 | headers.SetHeader("foo", "2"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8414 | req->SetExtraRequestHeaders(headers); |
| 8415 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8416 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8417 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8418 | EXPECT_FALSE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8419 | |
| 8420 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8421 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8422 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8423 | } |
| 8424 | } |
| 8425 | |
| 8426 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8427 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8428 | |
| 8429 | // populate the cache |
| 8430 | { |
| 8431 | TestDelegate d; |
| 8432 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8433 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8434 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8435 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8436 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8437 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8438 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8439 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8440 | |
| 8441 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8442 | } |
| 8443 | |
| 8444 | // repeat request with end-to-end validation. since auth-basic results in a |
| 8445 | // cachable page, we expect this test to result in a 304. in which case, the |
| 8446 | // response should be fetched from the cache. |
| 8447 | { |
| 8448 | TestDelegate d; |
| 8449 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8450 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8451 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8452 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8453 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8454 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 8455 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8456 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8457 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8458 | |
| 8459 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8460 | |
| 8461 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8462 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8463 | } |
| 8464 | } |
| 8465 | |
| 8466 | // Check that Set-Cookie headers in 401 responses are respected. |
| 8467 | // http://crbug.com/6450 |
| 8468 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8469 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8470 | |
| 8471 | GURL url_requiring_auth = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8472 | http_test_server()->GetURL("/auth-basic?set-cookie-if-challenged"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8473 | |
| 8474 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 8475 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 8476 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8477 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8478 | TestURLRequestContext context(true); |
| 8479 | context.set_network_delegate(&network_delegate); |
| 8480 | context.Init(); |
| 8481 | |
| 8482 | TestDelegate d; |
| 8483 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8484 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8485 | std::unique_ptr<URLRequest> r( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8486 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &d, |
| 8487 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8488 | r->set_site_for_cookies(url_requiring_auth); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8489 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8490 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8491 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8492 | |
| 8493 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8494 | |
| 8495 | // Make sure we sent the cookie in the restarted transaction. |
| 8496 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 8497 | != std::string::npos); |
| 8498 | } |
| 8499 | |
| 8500 | // Same test as above, except this time the restart is initiated earlier |
| 8501 | // (without user intervention since identity is embedded in the URL). |
| 8502 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8503 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8504 | TestURLRequestContext context(true); |
| 8505 | context.set_network_delegate(&network_delegate); |
| 8506 | context.Init(); |
| 8507 | |
| 8508 | TestDelegate d; |
| 8509 | |
| 8510 | GURL::Replacements replacements; |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 8511 | replacements.SetUsernameStr("user2"); |
| 8512 | replacements.SetPasswordStr("secret"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8513 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 8514 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8515 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 8516 | url_with_identity, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8517 | r->set_site_for_cookies(url_with_identity); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8518 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8519 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8520 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8521 | |
| 8522 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 8523 | |
| 8524 | // Make sure we sent the cookie in the restarted transaction. |
| 8525 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 8526 | != std::string::npos); |
| 8527 | } |
| 8528 | } |
| 8529 | |
Matt Menke | 0743e34 | 2019-06-11 20:48:10 | [diff] [blame] | 8530 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookiesCancelAuth) { |
| 8531 | ASSERT_TRUE(http_test_server()->Start()); |
| 8532 | |
| 8533 | GURL url_requiring_auth = |
| 8534 | http_test_server()->GetURL("/auth-basic?set-cookie-if-challenged"); |
| 8535 | |
| 8536 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 8537 | // Verify that cookies are set before credentials are provided, and then |
| 8538 | // cancelling auth does not result in setting the cookies again. |
| 8539 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 8540 | TestURLRequestContext context(true); |
| 8541 | context.set_network_delegate(&network_delegate); |
| 8542 | context.Init(); |
| 8543 | |
| 8544 | TestDelegate d; |
| 8545 | |
| 8546 | EXPECT_TRUE(GetAllCookies(&context).empty()); |
| 8547 | |
| 8548 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 8549 | url_requiring_auth, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8550 | r->set_site_for_cookies(url_requiring_auth); |
| 8551 | r->Start(); |
| 8552 | d.RunUntilAuthRequired(); |
| 8553 | |
| 8554 | // Cookie should have been set. |
| 8555 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 8556 | CookieList cookies = GetAllCookies(&context); |
| 8557 | ASSERT_EQ(1u, cookies.size()); |
| 8558 | EXPECT_EQ("got_challenged", cookies[0].Name()); |
| 8559 | EXPECT_EQ("true", cookies[0].Value()); |
| 8560 | |
| 8561 | // Delete cookie. |
| 8562 | context.cookie_store()->DeleteAllAsync(CookieStore::DeleteCallback()); |
| 8563 | |
| 8564 | // Cancel auth and continue the request. |
| 8565 | r->CancelAuth(); |
| 8566 | d.RunUntilComplete(); |
| 8567 | ASSERT_TRUE(r->response_headers()); |
| 8568 | EXPECT_EQ(401, r->response_headers()->response_code()); |
| 8569 | |
| 8570 | // Cookie should not have been set again. |
| 8571 | EXPECT_TRUE(GetAllCookies(&context).empty()); |
| 8572 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 8573 | } |
| 8574 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8575 | TEST_F(URLRequestTest, ReportCookieActivity) { |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8576 | HttpTestServer test_server; |
| 8577 | ASSERT_TRUE(test_server.Start()); |
| 8578 | |
| 8579 | FilteringTestLayeredNetworkDelegate network_delegate( |
| 8580 | std::make_unique<TestNetworkDelegate>()); |
| 8581 | network_delegate.SetCookieFilter("not_stored_cookie"); |
| 8582 | network_delegate.set_block_get_cookies(); |
| 8583 | TestURLRequestContext context(true); |
| 8584 | context.set_network_delegate(&network_delegate); |
| 8585 | context.Init(); |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8586 | // Make sure cookies blocked from being stored are caught, and those that are |
| 8587 | // accepted are reported as well. |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8588 | { |
| 8589 | TestDelegate d; |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8590 | GURL test_url = test_server.GetURL( |
| 8591 | "/set-cookie?not_stored_cookie=true&" |
| 8592 | "stored_cookie=tasty" |
| 8593 | "&path_cookie=narrow;path=/set-cookie"); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8594 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8595 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8596 | req->set_site_for_cookies(test_url); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8597 | req->Start(); |
| 8598 | d.RunUntilComplete(); |
| 8599 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8600 | ASSERT_EQ(3u, req->maybe_stored_cookies().size()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8601 | EXPECT_EQ("not_stored_cookie", |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8602 | req->maybe_stored_cookies()[0].cookie->Name()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8603 | EXPECT_EQ( |
| 8604 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES, |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8605 | req->maybe_stored_cookies()[0].status); |
| 8606 | EXPECT_EQ("stored_cookie", req->maybe_stored_cookies()[1].cookie->Name()); |
| 8607 | EXPECT_EQ(net::CanonicalCookie::CookieInclusionStatus::INCLUDE, |
| 8608 | req->maybe_stored_cookies()[1].status); |
| 8609 | EXPECT_EQ("stored_cookie", req->maybe_stored_cookies()[1].cookie->Name()); |
| 8610 | EXPECT_EQ(net::CanonicalCookie::CookieInclusionStatus::INCLUDE, |
| 8611 | req->maybe_stored_cookies()[2].status); |
| 8612 | EXPECT_EQ("path_cookie", req->maybe_stored_cookies()[2].cookie->Name()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8613 | } |
| 8614 | { |
| 8615 | TestDelegate d; |
| 8616 | // Make sure cookies blocked from being sent are caught. |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8617 | GURL test_url = test_server.GetURL("/echoheader?Cookie"); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8618 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8619 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8620 | req->set_site_for_cookies(test_url); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8621 | req->Start(); |
| 8622 | d.RunUntilComplete(); |
| 8623 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8624 | EXPECT_TRUE(d.data_received().find("stored_cookie=tasty") == |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8625 | std::string::npos); |
| 8626 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8627 | ASSERT_EQ(2u, req->maybe_sent_cookies().size()); |
| 8628 | EXPECT_EQ("path_cookie", req->maybe_sent_cookies()[0].cookie.Name()); |
| 8629 | EXPECT_EQ(net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_NOT_ON_PATH, |
| 8630 | req->maybe_sent_cookies()[0].status); |
| 8631 | EXPECT_EQ("stored_cookie", req->maybe_sent_cookies()[1].cookie.Name()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8632 | EXPECT_EQ( |
| 8633 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES, |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8634 | req->maybe_sent_cookies()[1].status); |
| 8635 | } |
| 8636 | |
| 8637 | network_delegate.unset_block_get_cookies(); |
| 8638 | { |
| 8639 | // Now with sending cookies re-enabled, it should actually be sent. |
| 8640 | TestDelegate d; |
| 8641 | GURL test_url = test_server.GetURL("/echoheader?Cookie"); |
| 8642 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 8643 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8644 | req->set_site_for_cookies(test_url); |
| 8645 | req->Start(); |
| 8646 | d.RunUntilComplete(); |
| 8647 | |
| 8648 | EXPECT_TRUE(d.data_received().find("stored_cookie=tasty") != |
| 8649 | std::string::npos); |
| 8650 | |
| 8651 | ASSERT_EQ(2u, req->maybe_sent_cookies().size()); |
| 8652 | EXPECT_EQ("path_cookie", req->maybe_sent_cookies()[0].cookie.Name()); |
| 8653 | EXPECT_EQ(net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_NOT_ON_PATH, |
| 8654 | req->maybe_sent_cookies()[0].status); |
| 8655 | EXPECT_EQ("stored_cookie", req->maybe_sent_cookies()[1].cookie.Name()); |
| 8656 | EXPECT_EQ(net::CanonicalCookie::CookieInclusionStatus::INCLUDE, |
| 8657 | req->maybe_sent_cookies()[1].status); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8658 | } |
| 8659 | } |
| 8660 | |
Maks Orlovich | 0d9da12 | 2019-06-13 20:14:43 | [diff] [blame] | 8661 | TEST_F(URLRequestTestHTTP, AuthChallengeCancelCookieCollect) { |
| 8662 | ASSERT_TRUE(http_test_server()->Start()); |
| 8663 | GURL url_requiring_auth = |
| 8664 | http_test_server()->GetURL("/auth-basic?set-cookie-if-challenged"); |
| 8665 | |
| 8666 | FilteringTestLayeredNetworkDelegate filtering_network_delegate( |
| 8667 | std::make_unique<TestNetworkDelegate>()); |
| 8668 | filtering_network_delegate.SetCookieFilter("got_challenged"); |
| 8669 | TestURLRequestContext context(true); |
| 8670 | context.set_network_delegate(&filtering_network_delegate); |
| 8671 | context.Init(); |
| 8672 | |
| 8673 | TestDelegate delegate; |
| 8674 | |
| 8675 | std::unique_ptr<URLRequest> request( |
| 8676 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &delegate, |
| 8677 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8678 | request->set_site_for_cookies(url_requiring_auth); |
| 8679 | request->Start(); |
| 8680 | |
| 8681 | delegate.RunUntilAuthRequired(); |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8682 | ASSERT_EQ(1u, request->maybe_stored_cookies().size()); |
Maks Orlovich | 0d9da12 | 2019-06-13 20:14:43 | [diff] [blame] | 8683 | EXPECT_EQ( |
| 8684 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES, |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8685 | request->maybe_stored_cookies()[0].status); |
Maks Orlovich | 0d9da12 | 2019-06-13 20:14:43 | [diff] [blame] | 8686 | EXPECT_EQ("got_challenged=true", |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8687 | request->maybe_stored_cookies()[0].cookie_string); |
Maks Orlovich | 0d9da12 | 2019-06-13 20:14:43 | [diff] [blame] | 8688 | |
| 8689 | // This shouldn't DCHECK-fail. |
| 8690 | request->CancelAuth(); |
| 8691 | delegate.RunUntilComplete(); |
| 8692 | } |
| 8693 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8694 | TEST_F(URLRequestTestHTTP, AuthChallengeWithFilteredCookies) { |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8695 | ASSERT_TRUE(http_test_server()->Start()); |
| 8696 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8697 | GURL url_requiring_auth = |
| 8698 | http_test_server()->GetURL("/auth-basic?set-cookie-if-challenged"); |
| 8699 | GURL url_requiring_auth_wo_cookies = |
| 8700 | http_test_server()->GetURL("/auth-basic"); |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8701 | // Check maybe_stored_cookies is populated first round trip, and cleared on |
| 8702 | // the second. |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8703 | { |
| 8704 | FilteringTestLayeredNetworkDelegate filtering_network_delegate( |
| 8705 | std::make_unique<TestNetworkDelegate>()); |
| 8706 | filtering_network_delegate.SetCookieFilter("got_challenged"); |
| 8707 | TestURLRequestContext context(true); |
| 8708 | context.set_network_delegate(&filtering_network_delegate); |
| 8709 | context.Init(); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8710 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8711 | TestDelegate delegate; |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8712 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8713 | std::unique_ptr<URLRequest> request( |
| 8714 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &delegate, |
| 8715 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8716 | request->set_site_for_cookies(url_requiring_auth); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8717 | request->Start(); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8718 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8719 | delegate.RunUntilAuthRequired(); |
| 8720 | // Make sure it was blocked once. |
| 8721 | EXPECT_EQ(1, filtering_network_delegate.blocked_set_cookie_count()); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8722 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8723 | // The number of cookies blocked from the most recent round trip. |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8724 | ASSERT_EQ(1u, request->maybe_stored_cookies().size()); |
| 8725 | EXPECT_EQ( |
| 8726 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES, |
| 8727 | request->maybe_stored_cookies().front().status); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8728 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8729 | // Now check the second round trip |
| 8730 | request->SetAuth(AuthCredentials(kUser, kSecret)); |
| 8731 | delegate.RunUntilComplete(); |
| 8732 | EXPECT_THAT(delegate.request_status(), IsOk()); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8733 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8734 | // There are DCHECKs in URLRequestHttpJob that would fail if |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8735 | // maybe_sent_cookies and maybe_stored_cookies were not cleared properly. |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8736 | |
| 8737 | // Make sure the cookie was actually filtered and not sent. |
| 8738 | EXPECT_EQ(std::string::npos, |
| 8739 | delegate.data_received().find("Cookie: got_challenged=true")); |
| 8740 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8741 | // The number of cookies that most recent round trip tried to set. |
| 8742 | ASSERT_EQ(0u, request->maybe_stored_cookies().size()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8743 | } |
| 8744 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8745 | // Check maybe_sent_cookies on first round trip (and cleared for the second). |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8746 | { |
| 8747 | FilteringTestLayeredNetworkDelegate filtering_network_delegate( |
| 8748 | std::make_unique<TestNetworkDelegate>()); |
| 8749 | filtering_network_delegate.set_block_get_cookies(); |
| 8750 | TestURLRequestContext context(true); |
| 8751 | context.set_network_delegate(&filtering_network_delegate); |
| 8752 | |
| 8753 | std::unique_ptr<CookieMonster> cm = |
| 8754 | std::make_unique<CookieMonster>(nullptr, nullptr); |
Lily Chen | 0f208ea | 2019-08-08 23:37:53 | [diff] [blame] | 8755 | auto another_cookie = CanonicalCookie::Create( |
| 8756 | url_requiring_auth_wo_cookies, "another_cookie=true", base::Time::Now(), |
| 8757 | base::nullopt /* server_time */); |
| 8758 | cm->SetCanonicalCookieAsync( |
| 8759 | std::move(another_cookie), url_requiring_auth_wo_cookies.scheme(), |
| 8760 | CookieOptions(), CookieStore::SetCookiesCallback()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8761 | context.set_cookie_store(cm.get()); |
| 8762 | context.Init(); |
| 8763 | |
| 8764 | TestDelegate delegate; |
| 8765 | |
| 8766 | std::unique_ptr<URLRequest> request( |
| 8767 | context.CreateRequest(url_requiring_auth_wo_cookies, DEFAULT_PRIORITY, |
| 8768 | &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8769 | request->set_site_for_cookies(url_requiring_auth_wo_cookies); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8770 | request->Start(); |
| 8771 | |
| 8772 | delegate.RunUntilAuthRequired(); |
| 8773 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8774 | ASSERT_EQ(1u, request->maybe_sent_cookies().size()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8775 | EXPECT_EQ("another_cookie", |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8776 | request->maybe_sent_cookies().front().cookie.Name()); |
| 8777 | EXPECT_EQ("true", request->maybe_sent_cookies().front().cookie.Value()); |
| 8778 | EXPECT_EQ( |
| 8779 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES, |
| 8780 | request->maybe_sent_cookies().front().status); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8781 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8782 | // Check maybe_sent_cookies on second roundtrip. |
| 8783 | request->set_maybe_sent_cookies({}); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8784 | cm->DeleteAllAsync(CookieStore::DeleteCallback()); |
Lily Chen | 0f208ea | 2019-08-08 23:37:53 | [diff] [blame] | 8785 | auto one_more_cookie = CanonicalCookie::Create( |
| 8786 | url_requiring_auth_wo_cookies, "one_more_cookie=true", |
| 8787 | base::Time::Now(), base::nullopt /* server_time */); |
| 8788 | cm->SetCanonicalCookieAsync( |
| 8789 | std::move(one_more_cookie), url_requiring_auth_wo_cookies.scheme(), |
| 8790 | CookieOptions(), CookieStore::SetCookiesCallback()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8791 | |
| 8792 | request->SetAuth(AuthCredentials(kUser, kSecret)); |
| 8793 | delegate.RunUntilComplete(); |
| 8794 | EXPECT_THAT(delegate.request_status(), IsOk()); |
| 8795 | |
| 8796 | // There are DCHECKs in URLRequestHttpJob that would fail if |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8797 | // maybe_sent_cookies and maybe_stored_cookies were not cleared properly. |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8798 | |
| 8799 | // Make sure the cookie was actually filtered. |
| 8800 | EXPECT_EQ(std::string::npos, |
| 8801 | delegate.data_received().find("Cookie: one_more_cookie=true")); |
| 8802 | // got_challenged was set after the first request and blocked on the second, |
| 8803 | // so it should only have been blocked this time |
| 8804 | EXPECT_EQ(2, filtering_network_delegate.blocked_get_cookie_count()); |
| 8805 | |
| 8806 | // // The number of cookies blocked from the most recent round trip. |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8807 | ASSERT_EQ(1u, request->maybe_sent_cookies().size()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8808 | EXPECT_EQ("one_more_cookie", |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8809 | request->maybe_sent_cookies().front().cookie.Name()); |
| 8810 | EXPECT_EQ( |
| 8811 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES, |
| 8812 | request->maybe_sent_cookies().front().status); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8813 | } |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8814 | } |
| 8815 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8816 | // Tests that load timing works as expected with auth and the cache. |
| 8817 | TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8818 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8819 | |
| 8820 | // populate the cache |
| 8821 | { |
| 8822 | TestDelegate d; |
| 8823 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8824 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8825 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8826 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8827 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8828 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8829 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8830 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8831 | |
| 8832 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8833 | |
| 8834 | LoadTimingInfo load_timing_info_before_auth; |
| 8835 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth( |
| 8836 | &load_timing_info_before_auth)); |
| 8837 | TestLoadTimingNotReused(load_timing_info_before_auth, |
| 8838 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 8839 | |
| 8840 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8841 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8842 | // The test server does not support keep alive sockets, so the second |
| 8843 | // request with auth should use a new socket. |
| 8844 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 8845 | EXPECT_NE(load_timing_info_before_auth.socket_log_id, |
| 8846 | load_timing_info.socket_log_id); |
| 8847 | EXPECT_LE(load_timing_info_before_auth.receive_headers_end, |
| 8848 | load_timing_info.connect_timing.connect_start); |
| 8849 | } |
| 8850 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8851 | // Repeat request with end-to-end validation. Since auth-basic results in a |
| 8852 | // cachable page, we expect this test to result in a 304. In which case, the |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8853 | // response should be fetched from the cache. |
| 8854 | { |
| 8855 | TestDelegate d; |
| 8856 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8857 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8858 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8859 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8860 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8861 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 8862 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8863 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8864 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8865 | |
| 8866 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8867 | |
| 8868 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8869 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8870 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8871 | // Since there was a request that went over the wire, the load timing |
| 8872 | // information should include connection times. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8873 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8874 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8875 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8876 | } |
| 8877 | } |
| 8878 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8879 | // In this test, we do a POST which the server will 302 redirect. |
| 8880 | // The subsequent transaction should use GET, and should not send the |
| 8881 | // Content-Type header. |
| 8882 | // http://code.google.com/p/chromium/issues/detail?id=843 |
| 8883 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8884 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8885 | |
| 8886 | const char kData[] = "hello world"; |
| 8887 | |
| 8888 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8889 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8890 | http_test_server()->GetURL("/redirect-to-echoall"), DEFAULT_PRIORITY, &d, |
| 8891 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8892 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8893 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8894 | |
| 8895 | // Set headers (some of which are specific to the POST). |
| 8896 | HttpRequestHeaders headers; |
Yeol | 45b9e0a | 2018-07-27 02:03:29 | [diff] [blame] | 8897 | headers.SetHeader("Content-Type", |
| 8898 | "multipart/form-data;" |
| 8899 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ"); |
| 8900 | headers.SetHeader("Accept", |
| 8901 | "text/xml,application/xml,application/xhtml+xml," |
| 8902 | "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); |
| 8903 | headers.SetHeader("Accept-Language", "en-US,en"); |
| 8904 | headers.SetHeader("Accept-Charset", "ISO-8859-1,*,utf-8"); |
| 8905 | headers.SetHeader("Content-Length", "11"); |
| 8906 | headers.SetHeader("Origin", "http://localhost:1337/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8907 | req->SetExtraRequestHeaders(headers); |
| 8908 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8909 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8910 | |
| 8911 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8912 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8913 | EXPECT_EQ("text/html", mime_type); |
| 8914 | |
| 8915 | const std::string& data = d.data_received(); |
| 8916 | |
| 8917 | // Check that the post-specific headers were stripped: |
| 8918 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 8919 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8920 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8921 | |
| 8922 | // These extra request headers should not have been stripped. |
| 8923 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 8924 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 8925 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 8926 | } |
| 8927 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8928 | // The following tests check that we handle mutating the request for HTTP |
| 8929 | // redirects as expected. |
| 8930 | // See https://crbug.com/56373, https://crbug.com/102130, and |
| 8931 | // https://crbug.com/465517. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8932 | |
| 8933 | TEST_F(URLRequestTestHTTP, Redirect301Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8934 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8935 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8936 | const GURL url = http_test_server()->GetURL("/redirect301-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8937 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8938 | http_test_server()->GetURL("/redirect301-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8939 | |
| 8940 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8941 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8942 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8943 | |
| 8944 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8945 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8946 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8947 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8948 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8949 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8950 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8951 | } |
| 8952 | |
| 8953 | TEST_F(URLRequestTestHTTP, Redirect302Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8954 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8955 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8956 | const GURL url = http_test_server()->GetURL("/redirect302-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8957 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8958 | http_test_server()->GetURL("/redirect302-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8959 | |
| 8960 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8961 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8962 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8963 | |
| 8964 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8965 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8966 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8967 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8968 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8969 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8970 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8971 | } |
| 8972 | |
| 8973 | TEST_F(URLRequestTestHTTP, Redirect303Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8974 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8975 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8976 | const GURL url = http_test_server()->GetURL("/redirect303-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8977 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8978 | http_test_server()->GetURL("/redirect303-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8979 | |
| 8980 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8981 | HTTPRedirectMethodTest(url, "PUT", "GET", true); |
| 8982 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8983 | |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8984 | HTTPRedirectOriginHeaderTest(url, "CONNECT", "GET", std::string()); |
| 8985 | HTTPRedirectOriginHeaderTest(https_redirect_url, "CONNECT", "GET", |
| 8986 | std::string()); |
| 8987 | HTTPRedirectOriginHeaderTest(url, "DELETE", "GET", std::string()); |
| 8988 | HTTPRedirectOriginHeaderTest(https_redirect_url, "DELETE", "GET", |
| 8989 | std::string()); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8990 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8991 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8992 | HTTPRedirectOriginHeaderTest(url, "HEAD", "HEAD", url.GetOrigin().spec()); |
| 8993 | HTTPRedirectOriginHeaderTest(https_redirect_url, "HEAD", "HEAD", "null"); |
| 8994 | HTTPRedirectOriginHeaderTest(url, "OPTIONS", "GET", std::string()); |
| 8995 | HTTPRedirectOriginHeaderTest(https_redirect_url, "OPTIONS", "GET", |
| 8996 | std::string()); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8997 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8998 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8999 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 9000 | HTTPRedirectOriginHeaderTest(url, "PUT", "GET", std::string()); |
| 9001 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "GET", std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9002 | } |
| 9003 | |
| 9004 | TEST_F(URLRequestTestHTTP, Redirect307Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9005 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9006 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9007 | const GURL url = http_test_server()->GetURL("/redirect307-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 9008 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9009 | http_test_server()->GetURL("/redirect307-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9010 | |
| 9011 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 9012 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 9013 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 9014 | |
| 9015 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 9016 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 9017 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 9018 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 9019 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 9020 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9021 | } |
| 9022 | |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 9023 | TEST_F(URLRequestTestHTTP, Redirect308Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9024 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 9025 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9026 | const GURL url = http_test_server()->GetURL("/redirect308-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 9027 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9028 | http_test_server()->GetURL("/redirect308-to-https"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 9029 | |
| 9030 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 9031 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 9032 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 9033 | |
| 9034 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 9035 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 9036 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 9037 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 9038 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 9039 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 9040 | } |
| 9041 | |
| 9042 | // Make sure that 308 responses without bodies are not treated as redirects. |
| 9043 | // Certain legacy apis that pre-date the response code expect this behavior |
| 9044 | // (Like Google Drive). |
| 9045 | TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9046 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 9047 | |
| 9048 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9049 | const GURL url = http_test_server()->GetURL("/308-without-location-header"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 9050 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9051 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9052 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 9053 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9054 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9055 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9056 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 9057 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9058 | EXPECT_EQ(308, request->response_headers()->response_code()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 9059 | EXPECT_EQ("This is not a redirect.", d.data_received()); |
| 9060 | } |
| 9061 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 9062 | TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9063 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 9064 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9065 | GURL original_url( |
| 9066 | http_test_server()->GetURL("/redirect302-to-echo#fragment")); |
| 9067 | GURL expected_url(http_test_server()->GetURL("/echo#fragment")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 9068 | |
| 9069 | TestDelegate d; |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 9070 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 9071 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 9072 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 9073 | r->Start(); |
| 9074 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 9075 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 9076 | EXPECT_EQ(2U, r->url_chain().size()); |
| 9077 | EXPECT_EQ(OK, d.request_status()); |
| 9078 | EXPECT_EQ(original_url, r->original_url()); |
| 9079 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 9080 | } |
| 9081 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9082 | TEST_F(URLRequestTestHTTP, RedirectWithFilteredCookies) { |
| 9083 | ASSERT_TRUE(http_test_server()->Start()); |
| 9084 | |
| 9085 | // FilteringTestLayeredNetworkDelegate filters by name, so the names of the |
| 9086 | // two cookies have to be the same. The values have been set to different |
| 9087 | // strings (the value of the server-redirect cookies is "true" and set-cookie |
| 9088 | // is "other") to differentiate between the two round trips. |
| 9089 | GURL redirect_to( |
| 9090 | http_test_server()->GetURL("/set-cookie?server-redirect=other")); |
| 9091 | |
| 9092 | GURL original_url(http_test_server()->GetURL("/server-redirect-with-cookie?" + |
| 9093 | redirect_to.spec())); |
| 9094 | |
| 9095 | GURL original_url_wo_cookie( |
| 9096 | http_test_server()->GetURL("/server-redirect?" + redirect_to.spec())); |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9097 | // Check maybe_stored_cookies on first round trip. |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9098 | { |
| 9099 | FilteringTestLayeredNetworkDelegate filtering_network_delegate( |
| 9100 | std::make_unique<TestNetworkDelegate>()); // Must outlive URLRequest. |
| 9101 | filtering_network_delegate.SetCookieFilter( |
| 9102 | "server-redirect"); // Filter the cookie server-redirect sets. |
| 9103 | TestURLRequestContext context(true); |
| 9104 | context.set_network_delegate(&filtering_network_delegate); |
| 9105 | context.Init(); |
| 9106 | |
| 9107 | TestDelegate delegate; |
| 9108 | std::unique_ptr<URLRequest> request( |
| 9109 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &delegate, |
| 9110 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9111 | |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 9112 | request->set_site_for_cookies(original_url); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9113 | request->Start(); |
| 9114 | delegate.RunUntilRedirect(); |
| 9115 | |
| 9116 | // Make sure it was blocked once. |
| 9117 | EXPECT_EQ(1, filtering_network_delegate.blocked_set_cookie_count()); |
| 9118 | |
| 9119 | // The number of cookies blocked from the most recent round trip. |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9120 | ASSERT_EQ(1u, request->maybe_stored_cookies().size()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9121 | EXPECT_EQ("server-redirect", |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9122 | request->maybe_stored_cookies().front().cookie->Name()); |
| 9123 | EXPECT_EQ("true", request->maybe_stored_cookies().front().cookie->Value()); |
| 9124 | EXPECT_EQ( |
| 9125 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES, |
| 9126 | request->maybe_stored_cookies().front().status); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9127 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9128 | // Check maybe_stored_cookies on second round trip (and clearing from the |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9129 | // first). |
| 9130 | request->FollowDeferredRedirect(base::nullopt, base::nullopt); |
| 9131 | delegate.RunUntilComplete(); |
| 9132 | EXPECT_THAT(delegate.request_status(), IsOk()); |
| 9133 | |
| 9134 | // There are DCHECKs in URLRequestHttpJob that would fail if |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9135 | // maybe_sent_cookies and maybe_stored_cookies we not cleared properly. |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9136 | |
| 9137 | // Make sure it was blocked twice. |
| 9138 | EXPECT_EQ(2, filtering_network_delegate.blocked_set_cookie_count()); |
| 9139 | |
| 9140 | // The number of cookies blocked from the most recent round trip. |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9141 | ASSERT_EQ(1u, request->maybe_stored_cookies().size()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9142 | EXPECT_EQ("server-redirect", |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9143 | request->maybe_stored_cookies().front().cookie->Name()); |
| 9144 | EXPECT_EQ("other", request->maybe_stored_cookies().front().cookie->Value()); |
| 9145 | EXPECT_EQ( |
| 9146 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES, |
| 9147 | request->maybe_stored_cookies().front().status); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9148 | } |
| 9149 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9150 | // Check maybe_sent_cookies on first round trip. |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9151 | { |
| 9152 | FilteringTestLayeredNetworkDelegate filtering_network_delegate( |
| 9153 | std::make_unique<TestNetworkDelegate>()); |
| 9154 | filtering_network_delegate.set_block_get_cookies(); |
| 9155 | TestURLRequestContext context(true); |
| 9156 | context.set_network_delegate(&filtering_network_delegate); |
| 9157 | std::unique_ptr<CookieMonster> cm = |
| 9158 | std::make_unique<CookieMonster>(nullptr, nullptr); |
Lily Chen | 0f208ea | 2019-08-08 23:37:53 | [diff] [blame] | 9159 | auto another_cookie = CanonicalCookie::Create( |
| 9160 | original_url, "another_cookie=true", base::Time::Now(), |
| 9161 | base::nullopt /* server_time */); |
| 9162 | cm->SetCanonicalCookieAsync(std::move(another_cookie), |
| 9163 | original_url.scheme(), CookieOptions(), |
| 9164 | CookieStore::SetCookiesCallback()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9165 | context.set_cookie_store(cm.get()); |
| 9166 | context.Init(); |
| 9167 | |
| 9168 | TestDelegate delegate; |
| 9169 | std::unique_ptr<URLRequest> request( |
| 9170 | context.CreateRequest(original_url_wo_cookie, DEFAULT_PRIORITY, |
| 9171 | &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9172 | |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 9173 | request->set_site_for_cookies(original_url_wo_cookie); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9174 | request->Start(); |
| 9175 | |
| 9176 | delegate.RunUntilRedirect(); |
| 9177 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9178 | ASSERT_EQ(1u, request->maybe_sent_cookies().size()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9179 | EXPECT_EQ("another_cookie", |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9180 | request->maybe_sent_cookies().front().cookie.Name()); |
| 9181 | EXPECT_EQ( |
| 9182 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES, |
| 9183 | request->maybe_sent_cookies().front().status); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9184 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9185 | // Check maybe_sent_cookies on second round trip |
| 9186 | request->set_maybe_sent_cookies({}); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9187 | cm->DeleteAllAsync(CookieStore::DeleteCallback()); |
Lily Chen | 0f208ea | 2019-08-08 23:37:53 | [diff] [blame] | 9188 | auto one_more_cookie = CanonicalCookie::Create( |
| 9189 | original_url_wo_cookie, "one_more_cookie=true", base::Time::Now(), |
| 9190 | base::nullopt /* server_time */); |
| 9191 | cm->SetCanonicalCookieAsync( |
| 9192 | std::move(one_more_cookie), original_url_wo_cookie.scheme(), |
| 9193 | CookieOptions(), CookieStore::SetCookiesCallback()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9194 | |
| 9195 | request->FollowDeferredRedirect(base::nullopt, base::nullopt); |
| 9196 | delegate.RunUntilComplete(); |
| 9197 | EXPECT_THAT(delegate.request_status(), IsOk()); |
| 9198 | |
| 9199 | // There are DCHECKs in URLRequestHttpJob that would fail if |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9200 | // maybe_sent_cookies and maybe_stored_cookies we not cleared properly. |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9201 | |
| 9202 | EXPECT_EQ(2, filtering_network_delegate.blocked_get_cookie_count()); |
| 9203 | |
| 9204 | // The number of cookies blocked from the most recent round trip. |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9205 | ASSERT_EQ(1u, request->maybe_sent_cookies().size()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9206 | EXPECT_EQ("one_more_cookie", |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9207 | request->maybe_sent_cookies().front().cookie.Name()); |
| 9208 | EXPECT_EQ("true", request->maybe_sent_cookies().front().cookie.Value()); |
| 9209 | EXPECT_EQ( |
| 9210 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES, |
| 9211 | request->maybe_sent_cookies().front().status); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9212 | } |
| 9213 | } |
| 9214 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9215 | TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9216 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9217 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9218 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9219 | GURL first_party_url("http://example.com"); |
| 9220 | |
| 9221 | TestDelegate d; |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 9222 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 9223 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9224 | r->set_site_for_cookies(first_party_url); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9225 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 9226 | r->Start(); |
| 9227 | d.RunUntilComplete(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9228 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 9229 | EXPECT_EQ(2U, r->url_chain().size()); |
| 9230 | EXPECT_EQ(OK, d.request_status()); |
| 9231 | EXPECT_EQ(first_party_url, r->site_for_cookies()); |
| 9232 | } |
| 9233 | |
| 9234 | TEST_F(URLRequestTestHTTP, RedirectPreserveTopFrameOrigin) { |
| 9235 | ASSERT_TRUE(http_test_server()->Start()); |
| 9236 | |
| 9237 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 9238 | url::Origin top_frame_origin = |
| 9239 | url::Origin::Create(GURL("http://example.com")); |
| 9240 | TestDelegate d; |
| 9241 | |
| 9242 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 9243 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9244 | r->set_top_frame_origin(top_frame_origin); |
| 9245 | |
| 9246 | r->Start(); |
| 9247 | d.RunUntilComplete(); |
| 9248 | |
| 9249 | EXPECT_EQ(2U, r->url_chain().size()); |
| 9250 | EXPECT_EQ(OK, d.request_status()); |
| 9251 | EXPECT_EQ(top_frame_origin, *r->top_frame_origin()); |
| 9252 | } |
| 9253 | |
| 9254 | TEST_F(URLRequestTestHTTP, RedirectPreserveUnsetTopFrameOrigin) { |
| 9255 | ASSERT_TRUE(http_test_server()->Start()); |
| 9256 | |
| 9257 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 9258 | TestDelegate d; |
| 9259 | |
| 9260 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 9261 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9262 | // Since we're not setting a top frame origin, we wouldn't expect one after |
| 9263 | // the redirect. |
| 9264 | r->Start(); |
| 9265 | d.RunUntilComplete(); |
| 9266 | |
| 9267 | EXPECT_EQ(2U, r->url_chain().size()); |
| 9268 | EXPECT_EQ(OK, d.request_status()); |
| 9269 | EXPECT_FALSE(r->top_frame_origin()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9270 | } |
| 9271 | |
Josh Karlin | f3caee164 | 2018-12-10 22:54:27 | [diff] [blame] | 9272 | TEST_F(URLRequestTestHTTP, RedirectPreserveTopFrameURL) { |
| 9273 | ASSERT_TRUE(http_test_server()->Start()); |
| 9274 | |
| 9275 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 9276 | url::Origin top_frame_origin = |
| 9277 | url::Origin::Create(GURL("http://example.com")); |
| 9278 | TestDelegate d; |
| 9279 | { |
| 9280 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 9281 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9282 | r->set_top_frame_origin(top_frame_origin); |
| 9283 | |
| 9284 | r->Start(); |
| 9285 | d.RunUntilComplete(); |
| 9286 | |
| 9287 | EXPECT_EQ(2U, r->url_chain().size()); |
| 9288 | EXPECT_EQ(OK, d.request_status()); |
| 9289 | EXPECT_EQ(top_frame_origin, *r->top_frame_origin()); |
| 9290 | } |
| 9291 | } |
| 9292 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9293 | TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9294 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9295 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9296 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9297 | GURL original_first_party_url("http://example.com"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9298 | GURL expected_first_party_url(http_test_server()->GetURL("/echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9299 | |
| 9300 | TestDelegate d; |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 9301 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9302 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9303 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 9304 | r->set_site_for_cookies(original_first_party_url); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9305 | r->set_first_party_url_policy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9306 | URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 9307 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9308 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9309 | d.RunUntilComplete(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9310 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9311 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9312 | EXPECT_EQ(OK, d.request_status()); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 9313 | EXPECT_EQ(expected_first_party_url, r->site_for_cookies()); |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 9314 | } |
| 9315 | |
| 9316 | TEST_F(URLRequestTestHTTP, RedirectUpdateTopFrameOrigin) { |
| 9317 | ASSERT_TRUE(http_test_server()->Start()); |
| 9318 | |
| 9319 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 9320 | url::Origin original_top_frame_origin = |
| 9321 | url::Origin::Create(GURL("http://example.com")); |
| 9322 | url::Origin expected_top_frame_origin = |
| 9323 | url::Origin::Create(GURL(http_test_server()->GetURL("/echo"))); |
| 9324 | |
| 9325 | TestDelegate d; |
| 9326 | |
| 9327 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 9328 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9329 | r->set_top_frame_origin(original_top_frame_origin); |
| 9330 | r->set_first_party_url_policy(URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 9331 | |
| 9332 | r->Start(); |
| 9333 | d.RunUntilComplete(); |
| 9334 | |
| 9335 | EXPECT_EQ(2U, r->url_chain().size()); |
| 9336 | EXPECT_EQ(OK, d.request_status()); |
| 9337 | EXPECT_EQ(expected_top_frame_origin, *r->top_frame_origin()); |
| 9338 | } |
| 9339 | |
| 9340 | TEST_F(URLRequestTestHTTP, RedirectIgnoreUnsetTopFrameOrigin) { |
| 9341 | ASSERT_TRUE(http_test_server()->Start()); |
| 9342 | |
| 9343 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 9344 | TestDelegate d; |
| 9345 | |
| 9346 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 9347 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9348 | // Since we're not setting a top frame origin, we wouldn't expect one after |
| 9349 | // the redirect. |
| 9350 | r->set_first_party_url_policy(URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 9351 | |
| 9352 | r->Start(); |
| 9353 | d.RunUntilComplete(); |
| 9354 | |
| 9355 | EXPECT_EQ(2U, r->url_chain().size()); |
| 9356 | EXPECT_EQ(OK, d.request_status()); |
| 9357 | EXPECT_FALSE(r->top_frame_origin()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9358 | } |
| 9359 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9360 | TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9361 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9362 | |
| 9363 | const char kData[] = "hello world"; |
| 9364 | |
| 9365 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9366 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9367 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9368 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9369 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 9370 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9371 | HttpRequestHeaders headers; |
| 9372 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 9373 | base::NumberToString(base::size(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9374 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9375 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9376 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9377 | req.get(), &default_network_delegate_, |
| 9378 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9379 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 9380 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9381 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9382 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9383 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9384 | EXPECT_EQ("GET", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9385 | } |
| 9386 | |
| 9387 | TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9388 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9389 | |
| 9390 | const char kData[] = "hello world"; |
| 9391 | |
| 9392 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9393 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9394 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9395 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9396 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 9397 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9398 | HttpRequestHeaders headers; |
| 9399 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 9400 | base::NumberToString(base::size(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9401 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9402 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9403 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9404 | req.get(), &default_network_delegate_, |
| 9405 | http_test_server()->GetURL("/echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 9406 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9407 | "Very Good Reason")); |
| 9408 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9409 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9410 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9411 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9412 | EXPECT_EQ("POST", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9413 | EXPECT_EQ(kData, d.data_received()); |
| 9414 | } |
| 9415 | |
| 9416 | // Check that default A-L header is sent. |
| 9417 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9418 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9419 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 9420 | StaticHttpUserAgentSettings settings("en", std::string()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9421 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9422 | TestURLRequestContext context(true); |
| 9423 | context.set_network_delegate(&network_delegate); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9424 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9425 | context.Init(); |
| 9426 | |
| 9427 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9428 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9429 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9430 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9431 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9432 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9433 | EXPECT_EQ("en", d.data_received()); |
| 9434 | } |
| 9435 | |
| 9436 | // Check that an empty A-L header is not sent. http://crbug.com/77365. |
| 9437 | TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9438 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9439 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 9440 | std::string empty_string; // Avoid most vexing parse on line below. |
| 9441 | StaticHttpUserAgentSettings settings(empty_string, empty_string); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9442 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9443 | TestURLRequestContext context(true); |
| 9444 | context.set_network_delegate(&network_delegate); |
| 9445 | context.Init(); |
| 9446 | // We override the language after initialization because empty entries |
| 9447 | // get overridden by Init(). |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9448 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9449 | |
| 9450 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9451 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9452 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9453 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9454 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9455 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9456 | EXPECT_EQ("None", d.data_received()); |
| 9457 | } |
| 9458 | |
| 9459 | // Check that if request overrides the A-L header, the default is not appended. |
| 9460 | // See http://crbug.com/20894 |
| 9461 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9462 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9463 | |
| 9464 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9465 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9466 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9467 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9468 | HttpRequestHeaders headers; |
| 9469 | headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9470 | req->SetExtraRequestHeaders(headers); |
| 9471 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9472 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9473 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 9474 | } |
| 9475 | |
| 9476 | // Check that default A-E header is sent. |
| 9477 | TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9478 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9479 | |
| 9480 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9481 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9482 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9483 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9484 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9485 | req->SetExtraRequestHeaders(headers); |
| 9486 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9487 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9488 | EXPECT_TRUE(ContainsString(d.data_received(), "gzip")); |
| 9489 | } |
| 9490 | |
| 9491 | // Check that if request overrides the A-E header, the default is not appended. |
| 9492 | // See http://crbug.com/47381 |
| 9493 | TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9494 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9495 | |
| 9496 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9497 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9498 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9499 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9500 | HttpRequestHeaders headers; |
| 9501 | headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9502 | req->SetExtraRequestHeaders(headers); |
| 9503 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9504 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9505 | EXPECT_FALSE(ContainsString(d.data_received(), "gzip")); |
| 9506 | EXPECT_TRUE(ContainsString(d.data_received(), "identity")); |
| 9507 | } |
| 9508 | |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 9509 | // Check that setting the A-C header sends the proper header. |
| 9510 | TEST_F(URLRequestTestHTTP, SetAcceptCharset) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9511 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9512 | |
| 9513 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9514 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9515 | http_test_server()->GetURL("/echoheader?Accept-Charset"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9516 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9517 | HttpRequestHeaders headers; |
| 9518 | headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9519 | req->SetExtraRequestHeaders(headers); |
| 9520 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9521 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9522 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 9523 | } |
| 9524 | |
| 9525 | // Check that default User-Agent header is sent. |
| 9526 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9527 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9528 | |
| 9529 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9530 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9531 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9532 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9533 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9534 | d.RunUntilComplete(); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9535 | EXPECT_EQ(default_context().http_user_agent_settings()->GetUserAgent(), |
[email protected] | 051a4b3 | 2014-01-09 04:02:37 | [diff] [blame] | 9536 | d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9537 | } |
| 9538 | |
| 9539 | // Check that if request overrides the User-Agent header, |
| 9540 | // the default is not appended. |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 9541 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 9542 | #if defined(OS_IOS) |
| 9543 | #define MAYBE_OverrideUserAgent FLAKY_OverrideUserAgent |
| 9544 | #else |
| 9545 | #define MAYBE_OverrideUserAgent OverrideUserAgent |
| 9546 | #endif |
| 9547 | TEST_F(URLRequestTestHTTP, MAYBE_OverrideUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9548 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9549 | |
| 9550 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9551 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9552 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9553 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9554 | HttpRequestHeaders headers; |
| 9555 | headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9556 | req->SetExtraRequestHeaders(headers); |
| 9557 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9558 | d.RunUntilComplete(); |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 9559 | EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9560 | } |
| 9561 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9562 | // Check that a NULL HttpUserAgentSettings causes the corresponding empty |
| 9563 | // User-Agent header to be sent but does not send the Accept-Language and |
| 9564 | // Accept-Charset headers. |
| 9565 | TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9566 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9567 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9568 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9569 | TestURLRequestContext context(true); |
| 9570 | context.set_network_delegate(&network_delegate); |
| 9571 | context.Init(); |
| 9572 | // We override the HttpUserAgentSettings after initialization because empty |
| 9573 | // entries get overridden by Init(). |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 9574 | context.set_http_user_agent_settings(nullptr); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9575 | |
| 9576 | struct { |
| 9577 | const char* request; |
| 9578 | const char* expected_response; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9579 | } tests[] = {{"/echoheader?Accept-Language", "None"}, |
| 9580 | {"/echoheader?Accept-Charset", "None"}, |
| 9581 | {"/echoheader?User-Agent", ""}}; |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9582 | |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 9583 | for (size_t i = 0; i < base::size(tests); i++) { |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9584 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9585 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9586 | http_test_server()->GetURL(tests[i].request), DEFAULT_PRIORITY, &d, |
| 9587 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9588 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9589 | d.RunUntilComplete(); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9590 | EXPECT_EQ(tests[i].expected_response, d.data_received()) |
| 9591 | << " Request = \"" << tests[i].request << "\""; |
| 9592 | } |
| 9593 | } |
| 9594 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9595 | // Make sure that URLRequest passes on its priority updates to |
| 9596 | // newly-created jobs after the first one. |
| 9597 | TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9598 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9599 | |
| 9600 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9601 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9602 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9603 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9604 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9605 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9606 | std::unique_ptr<URLRequestRedirectJob> redirect_job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9607 | req.get(), &default_network_delegate_, |
| 9608 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9609 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 9610 | AddTestInterceptor()->set_main_intercept_job(std::move(redirect_job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9611 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9612 | req->SetPriority(LOW); |
| 9613 | req->Start(); |
| 9614 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9615 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9616 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9617 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9618 | req.get(), &default_network_delegate_, &job_priority)); |
| 9619 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9620 | |
| 9621 | // Should trigger |job| to be started. |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9622 | d.RunUntilComplete(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9623 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9624 | } |
| 9625 | |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9626 | // Check that creating a network request while entering/exiting suspend mode |
| 9627 | // fails as it should. This is the only case where an HttpTransactionFactory |
| 9628 | // does not return an HttpTransaction. |
| 9629 | TEST_F(URLRequestTestHTTP, NetworkSuspendTest) { |
| 9630 | // Create a new HttpNetworkLayer that thinks it's suspended. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9631 | std::unique_ptr<HttpNetworkLayer> network_layer(new HttpNetworkLayer( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9632 | default_context().http_transaction_factory()->GetSession())); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9633 | network_layer->OnSuspend(); |
| 9634 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 9635 | HttpCache http_cache(std::move(network_layer), |
zhongyi | 3ceab9f | 2017-02-04 02:06:03 | [diff] [blame] | 9636 | HttpCache::DefaultBackend::InMemory(0), |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 9637 | false /* is_main_cache */); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9638 | |
| 9639 | TestURLRequestContext context(true); |
| 9640 | context.set_http_transaction_factory(&http_cache); |
| 9641 | context.Init(); |
| 9642 | |
| 9643 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9644 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9645 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 9646 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9647 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9648 | d.RunUntilComplete(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9649 | |
| 9650 | EXPECT_TRUE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9651 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, d.request_status()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9652 | } |
| 9653 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9654 | namespace { |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9655 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9656 | // HttpTransactionFactory that synchronously fails to create transactions. |
| 9657 | class FailingHttpTransactionFactory : public HttpTransactionFactory { |
| 9658 | public: |
| 9659 | explicit FailingHttpTransactionFactory(HttpNetworkSession* network_session) |
| 9660 | : network_session_(network_session) {} |
| 9661 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 9662 | ~FailingHttpTransactionFactory() override = default; |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9663 | |
| 9664 | // HttpTransactionFactory methods: |
| 9665 | int CreateTransaction(RequestPriority priority, |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9666 | std::unique_ptr<HttpTransaction>* trans) override { |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9667 | return ERR_FAILED; |
| 9668 | } |
| 9669 | |
| 9670 | HttpCache* GetCache() override { return nullptr; } |
| 9671 | |
| 9672 | HttpNetworkSession* GetSession() override { return network_session_; } |
| 9673 | |
| 9674 | private: |
| 9675 | HttpNetworkSession* network_session_; |
| 9676 | |
| 9677 | DISALLOW_COPY_AND_ASSIGN(FailingHttpTransactionFactory); |
| 9678 | }; |
| 9679 | |
| 9680 | } // namespace |
| 9681 | |
| 9682 | // Check that when a request that fails to create an HttpTransaction can be |
| 9683 | // cancelled while the failure notification is pending, and doesn't send two |
| 9684 | // failure notifications. |
| 9685 | // |
| 9686 | // This currently only happens when in suspend mode and there's no cache, but |
| 9687 | // just use a special HttpTransactionFactory, to avoid depending on those |
| 9688 | // behaviors. |
| 9689 | TEST_F(URLRequestTestHTTP, NetworkCancelAfterCreateTransactionFailsTest) { |
| 9690 | FailingHttpTransactionFactory http_transaction_factory( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9691 | default_context().http_transaction_factory()->GetSession()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9692 | TestURLRequestContext context(true); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9693 | context.set_http_transaction_factory(&http_transaction_factory); |
| 9694 | context.set_network_delegate(default_network_delegate()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9695 | context.Init(); |
| 9696 | |
| 9697 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9698 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9699 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 9700 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9701 | // Don't send cookies (Collecting cookies is asynchronous, and need request to |
| 9702 | // try to create an HttpNetworkTransaction synchronously on start). |
| 9703 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9704 | req->Start(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9705 | req->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9706 | d.RunUntilComplete(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9707 | // Run pending error task, if there is one. |
| 9708 | base::RunLoop().RunUntilIdle(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9709 | |
| 9710 | EXPECT_TRUE(d.request_failed()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9711 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9712 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9713 | |
| 9714 | // NetworkDelegate should see the cancellation, but not the error. |
| 9715 | EXPECT_EQ(1, default_network_delegate()->canceled_requests()); |
| 9716 | EXPECT_EQ(0, default_network_delegate()->error_count()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9717 | } |
| 9718 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9719 | TEST_F(URLRequestTestHTTP, NetworkAccessedSetOnNetworkRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9720 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9721 | |
| 9722 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9723 | GURL test_url(http_test_server()->GetURL("/")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9724 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9725 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9726 | |
| 9727 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9728 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9729 | |
| 9730 | EXPECT_TRUE(req->response_info().network_accessed); |
| 9731 | } |
| 9732 | |
| 9733 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnCachedResponse) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9734 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9735 | |
| 9736 | // Populate the cache. |
| 9737 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9738 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9739 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 9740 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9741 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9742 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9743 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9744 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9745 | EXPECT_TRUE(req->response_info().network_accessed); |
| 9746 | EXPECT_FALSE(req->response_info().was_cached); |
| 9747 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9748 | req = default_context().CreateRequest( |
| 9749 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 9750 | TRAFFIC_ANNOTATION_FOR_TESTS); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9751 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9752 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9753 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9754 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9755 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9756 | EXPECT_TRUE(req->response_info().was_cached); |
| 9757 | } |
| 9758 | |
| 9759 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnLoadOnlyFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9760 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9761 | |
| 9762 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9763 | GURL test_url(http_test_server()->GetURL("/")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9764 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9765 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jkarlin | a067deed | 2016-10-27 14:48:33 | [diff] [blame] | 9766 | req->SetLoadFlags(LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9767 | |
| 9768 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9769 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9770 | |
| 9771 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9772 | } |
| 9773 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 9774 | // Test that a single job with a THROTTLED priority completes |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 9775 | // correctly in the absence of contention. |
| 9776 | TEST_F(URLRequestTestHTTP, ThrottledPriority) { |
| 9777 | ASSERT_TRUE(http_test_server()->Start()); |
| 9778 | |
| 9779 | TestDelegate d; |
| 9780 | GURL test_url(http_test_server()->GetURL("/")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9781 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9782 | test_url, THROTTLED, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 9783 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9784 | d.RunUntilComplete(); |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 9785 | |
| 9786 | EXPECT_TRUE(req->status().is_success()); |
| 9787 | } |
| 9788 | |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9789 | TEST_F(URLRequestTestHTTP, RawBodyBytesNoContentEncoding) { |
| 9790 | ASSERT_TRUE(http_test_server()->Start()); |
| 9791 | |
| 9792 | TestDelegate d; |
| 9793 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9794 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 9795 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9796 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9797 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9798 | |
| 9799 | EXPECT_EQ(5, req->GetRawBodyBytes()); |
| 9800 | } |
| 9801 | |
| 9802 | TEST_F(URLRequestTestHTTP, RawBodyBytesGzipEncoding) { |
| 9803 | ASSERT_TRUE(http_test_server()->Start()); |
| 9804 | |
| 9805 | TestDelegate d; |
| 9806 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9807 | http_test_server()->GetURL("/gzip-encoded"), DEFAULT_PRIORITY, &d, |
| 9808 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9809 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9810 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9811 | |
| 9812 | EXPECT_EQ(30, req->GetRawBodyBytes()); |
| 9813 | } |
| 9814 | |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9815 | // Check that if NetworkDelegate::OnBeforeStartTransaction returns an error, |
| 9816 | // the delegate isn't called back synchronously. |
| 9817 | TEST_F(URLRequestTestHTTP, TesBeforeStartTransactionFails) { |
| 9818 | ASSERT_TRUE(http_test_server()->Start()); |
| 9819 | default_network_delegate_.set_before_start_transaction_fails(); |
| 9820 | |
| 9821 | TestDelegate d; |
| 9822 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9823 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 9824 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9825 | req->Start(); |
| 9826 | DCHECK(!d.response_completed()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9827 | d.RunUntilComplete(); |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9828 | DCHECK(d.response_completed()); |
| 9829 | EXPECT_EQ(ERR_FAILED, d.request_status()); |
| 9830 | } |
| 9831 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9832 | class URLRequestInterceptorTestHTTP : public URLRequestTestHTTP { |
| 9833 | public: |
| 9834 | // TODO(bengr): Merge this with the URLRequestInterceptorHTTPTest fixture, |
| 9835 | // ideally remove the dependency on URLRequestTestJob, and maybe move these |
| 9836 | // tests into the factory tests. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 9837 | URLRequestInterceptorTestHTTP() |
| 9838 | : URLRequestTestHTTP(), interceptor_(nullptr) {} |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9839 | |
| 9840 | void SetUpFactory() override { |
| 9841 | interceptor_ = new MockURLRequestInterceptor(); |
| 9842 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9843 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9844 | } |
| 9845 | |
| 9846 | MockURLRequestInterceptor* interceptor() const { |
| 9847 | return interceptor_; |
| 9848 | } |
| 9849 | |
| 9850 | private: |
| 9851 | MockURLRequestInterceptor* interceptor_; |
| 9852 | }; |
| 9853 | |
| 9854 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9855 | NetworkDelegateNotificationOnRedirectIntercept) { |
| 9856 | interceptor()->set_intercept_redirect(true); |
| 9857 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 9858 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 9859 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9860 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9861 | |
| 9862 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9863 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9864 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 9865 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9866 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9867 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9868 | |
| 9869 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 9870 | // Check we got one good response |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9871 | int status = d.request_status(); |
| 9872 | EXPECT_EQ(OK, status); |
| 9873 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9874 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9875 | |
| 9876 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 9877 | EXPECT_EQ(1, d.response_started_count()); |
| 9878 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9879 | |
| 9880 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9881 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9882 | EXPECT_EQ(1, default_network_delegate()->headers_received_count()); |
| 9883 | } |
| 9884 | |
| 9885 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9886 | NetworkDelegateNotificationOnErrorIntercept) { |
| 9887 | // Intercept that error and respond with an OK response. |
| 9888 | interceptor()->set_intercept_final_response(true); |
| 9889 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 9890 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 9891 | default_network_delegate()->set_can_be_intercepted_on_error(true); |
| 9892 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9893 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9894 | |
| 9895 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9896 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9897 | http_test_server()->GetURL("/two-content-lengths.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9898 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9899 | req->set_method("GET"); |
| 9900 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9901 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9902 | |
| 9903 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 9904 | |
| 9905 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9906 | int status = d.request_status(); |
| 9907 | EXPECT_EQ(OK, status); |
| 9908 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9909 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9910 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 9911 | EXPECT_EQ(1, d.response_started_count()); |
| 9912 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9913 | |
| 9914 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9915 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9916 | EXPECT_EQ(0, default_network_delegate()->headers_received_count()); |
| 9917 | } |
| 9918 | |
| 9919 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9920 | NetworkDelegateNotificationOnResponseIntercept) { |
| 9921 | // Intercept that error and respond with an OK response. |
| 9922 | interceptor()->set_intercept_final_response(true); |
| 9923 | |
| 9924 | // Intercept with a real URLRequestHttpJob. |
| 9925 | interceptor()->set_use_url_request_http_job(true); |
| 9926 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9927 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9928 | |
| 9929 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9930 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9931 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 9932 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9933 | req->set_method("GET"); |
| 9934 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9935 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9936 | |
| 9937 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 9938 | |
| 9939 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9940 | int status = d.request_status(); |
| 9941 | EXPECT_EQ(OK, status); |
| 9942 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9943 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9944 | EXPECT_EQ("hello", d.data_received()); |
| 9945 | EXPECT_EQ(1, d.response_started_count()); |
| 9946 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9947 | |
| 9948 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9949 | EXPECT_EQ(2, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9950 | EXPECT_EQ(2, default_network_delegate()->headers_received_count()); |
| 9951 | } |
| 9952 | |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9953 | class URLRequestTestReferrerPolicy : public URLRequestTest { |
| 9954 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 9955 | URLRequestTestReferrerPolicy() = default; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9956 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9957 | void InstantiateSameOriginServers(net::EmbeddedTestServer::Type type) { |
| 9958 | origin_server_.reset(new EmbeddedTestServer(type)); |
| 9959 | if (type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9960 | origin_server_->AddDefaultHandlers( |
| 9961 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9962 | } else { |
| 9963 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9964 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9965 | ASSERT_TRUE(origin_server_->Start()); |
| 9966 | } |
| 9967 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9968 | void InstantiateCrossOriginServers(net::EmbeddedTestServer::Type origin_type, |
| 9969 | net::EmbeddedTestServer::Type dest_type) { |
| 9970 | origin_server_.reset(new EmbeddedTestServer(origin_type)); |
| 9971 | if (origin_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9972 | origin_server_->AddDefaultHandlers( |
| 9973 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9974 | } else { |
| 9975 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9976 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9977 | ASSERT_TRUE(origin_server_->Start()); |
| 9978 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9979 | destination_server_.reset(new EmbeddedTestServer(dest_type)); |
| 9980 | if (dest_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9981 | destination_server_->AddDefaultHandlers( |
| 9982 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9983 | } else { |
| 9984 | destination_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9985 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9986 | ASSERT_TRUE(destination_server_->Start()); |
| 9987 | } |
| 9988 | |
| 9989 | void VerifyReferrerAfterRedirect(URLRequest::ReferrerPolicy policy, |
| 9990 | const GURL& referrer, |
| 9991 | const GURL& expected) { |
| 9992 | // Create and execute the request: we'll only have a |destination_server_| |
| 9993 | // if the origins are meant to be distinct. Otherwise, we'll use the |
| 9994 | // |origin_server_| for both endpoints. |
| 9995 | GURL destination_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9996 | destination_server_ ? destination_server_->GetURL("/echoheader?Referer") |
| 9997 | : origin_server_->GetURL("/echoheader?Referer"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9998 | GURL origin_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9999 | origin_server_->GetURL("/server-redirect?" + destination_url.spec()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10000 | |
| 10001 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 10002 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10003 | origin_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10004 | req->set_referrer_policy(policy); |
| 10005 | req->SetReferrer(referrer.spec()); |
| 10006 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10007 | d.RunUntilComplete(); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10008 | |
| 10009 | EXPECT_EQ(1, d.response_started_count()); |
| 10010 | EXPECT_EQ(1, d.received_redirect_count()); |
| 10011 | EXPECT_EQ(destination_url, req->url()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 10012 | EXPECT_EQ(OK, d.request_status()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10013 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 10014 | |
| 10015 | EXPECT_EQ(expected.spec(), req->referrer()); |
| 10016 | if (expected.is_empty()) |
| 10017 | EXPECT_EQ("None", d.data_received()); |
| 10018 | else |
| 10019 | EXPECT_EQ(expected.spec(), d.data_received()); |
| 10020 | } |
| 10021 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10022 | EmbeddedTestServer* origin_server() const { return origin_server_.get(); } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10023 | |
| 10024 | private: |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10025 | std::unique_ptr<EmbeddedTestServer> origin_server_; |
| 10026 | std::unique_ptr<EmbeddedTestServer> destination_server_; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10027 | }; |
| 10028 | |
| 10029 | TEST_F(URLRequestTestReferrerPolicy, HTTPToSameOriginHTTP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10030 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTP); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10031 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10032 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10033 | VerifyReferrerAfterRedirect( |
| 10034 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10035 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10036 | |
| 10037 | VerifyReferrerAfterRedirect( |
| 10038 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10039 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10040 | |
| 10041 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10042 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10043 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10044 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 10045 | referrer); |
| 10046 | |
| 10047 | // The original referrer set on the request is expected to obey the referrer |
| 10048 | // policy and already be stripped to the origin; thus this test case just |
| 10049 | // checks that this policy doesn't cause the referrer to change when following |
| 10050 | // a redirect. |
| 10051 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 10052 | referrer.GetOrigin()); |
| 10053 | |
| 10054 | VerifyReferrerAfterRedirect( |
| 10055 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 10056 | referrer); |
| 10057 | |
| 10058 | // The original referrer set on the request is expected to obey the referrer |
| 10059 | // policy and already be stripped to the origin; thus this test case just |
| 10060 | // checks that this policy doesn't cause the referrer to change when following |
| 10061 | // a redirect. |
| 10062 | VerifyReferrerAfterRedirect( |
| 10063 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 10064 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 10065 | |
| 10066 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10067 | } |
| 10068 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10069 | TEST_F(URLRequestTestReferrerPolicy, HTTPToCrossOriginHTTP) { |
| 10070 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 10071 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10072 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10073 | |
| 10074 | VerifyReferrerAfterRedirect( |
| 10075 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10076 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10077 | |
| 10078 | VerifyReferrerAfterRedirect( |
| 10079 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10080 | referrer, referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10081 | |
| 10082 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10083 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 10084 | referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10085 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10086 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 10087 | referrer); |
| 10088 | |
| 10089 | // The original referrer set on the request is expected to obey the referrer |
| 10090 | // policy and already be stripped to the origin; thus this test case just |
| 10091 | // checks that this policy doesn't cause the referrer to change when following |
| 10092 | // a redirect. |
| 10093 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 10094 | referrer.GetOrigin()); |
| 10095 | |
| 10096 | VerifyReferrerAfterRedirect( |
| 10097 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 10098 | |
| 10099 | // The original referrer set on the request is expected to obey the referrer |
| 10100 | // policy and already be stripped to the origin; thus this test case just |
| 10101 | // checks that this policy doesn't cause the referrer to change when following |
| 10102 | // a redirect. |
| 10103 | VerifyReferrerAfterRedirect( |
| 10104 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 10105 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 10106 | |
| 10107 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10108 | } |
| 10109 | |
| 10110 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToSameOriginHTTPS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10111 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10112 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10113 | |
| 10114 | VerifyReferrerAfterRedirect( |
| 10115 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10116 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10117 | |
| 10118 | VerifyReferrerAfterRedirect( |
| 10119 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10120 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10121 | |
| 10122 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10123 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10124 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10125 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 10126 | referrer); |
| 10127 | |
| 10128 | // The original referrer set on the request is expected to obey the referrer |
| 10129 | // policy and already be stripped to the origin; thus this test case just |
| 10130 | // checks that this policy doesn't cause the referrer to change when following |
| 10131 | // a redirect. |
| 10132 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 10133 | referrer.GetOrigin()); |
| 10134 | |
| 10135 | VerifyReferrerAfterRedirect( |
| 10136 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 10137 | referrer); |
| 10138 | |
| 10139 | // The original referrer set on the request is expected to obey the referrer |
| 10140 | // policy and already be stripped to the origin; thus this test case just |
| 10141 | // checks that this policy doesn't cause the referrer to change when following |
| 10142 | // a redirect. |
| 10143 | VerifyReferrerAfterRedirect( |
| 10144 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 10145 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 10146 | |
| 10147 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10148 | } |
| 10149 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10150 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToCrossOriginHTTPS) { |
| 10151 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 10152 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10153 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10154 | |
| 10155 | VerifyReferrerAfterRedirect( |
| 10156 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10157 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10158 | |
| 10159 | VerifyReferrerAfterRedirect( |
| 10160 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10161 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10162 | |
| 10163 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10164 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10165 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10166 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10167 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 10168 | referrer); |
| 10169 | |
| 10170 | // The original referrer set on the request is expected to obey the referrer |
| 10171 | // policy and already be stripped to the origin; thus this test case just |
| 10172 | // checks that this policy doesn't cause the referrer to change when following |
| 10173 | // a redirect. |
| 10174 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 10175 | referrer.GetOrigin()); |
| 10176 | |
| 10177 | VerifyReferrerAfterRedirect( |
| 10178 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 10179 | |
| 10180 | // The original referrer set on the request is expected to obey the referrer |
| 10181 | // policy and already be stripped to the origin; thus this test case just |
| 10182 | // checks that this policy doesn't cause the referrer to change when following |
| 10183 | // a redirect. |
| 10184 | VerifyReferrerAfterRedirect( |
| 10185 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 10186 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 10187 | |
| 10188 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10189 | } |
| 10190 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10191 | TEST_F(URLRequestTestReferrerPolicy, HTTPToHTTPS) { |
| 10192 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 10193 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10194 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10195 | |
| 10196 | VerifyReferrerAfterRedirect( |
| 10197 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10198 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10199 | |
| 10200 | VerifyReferrerAfterRedirect( |
| 10201 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10202 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10203 | |
| 10204 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10205 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10206 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10207 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10208 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 10209 | referrer); |
| 10210 | |
| 10211 | // The original referrer set on the request is expected to obey the referrer |
| 10212 | // policy and already be stripped to the origin; thus this test case just |
| 10213 | // checks that this policy doesn't cause the referrer to change when following |
| 10214 | // a redirect. |
| 10215 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 10216 | referrer.GetOrigin()); |
| 10217 | |
| 10218 | VerifyReferrerAfterRedirect( |
| 10219 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 10220 | |
| 10221 | // The original referrer set on the request is expected to obey the referrer |
| 10222 | // policy and already be stripped to the origin; thus this test case just |
| 10223 | // checks that this policy doesn't cause the referrer to change when following |
| 10224 | // a redirect. |
| 10225 | VerifyReferrerAfterRedirect( |
| 10226 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 10227 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 10228 | |
| 10229 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10230 | } |
| 10231 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10232 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToHTTP) { |
| 10233 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 10234 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10235 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10236 | |
| 10237 | VerifyReferrerAfterRedirect( |
| 10238 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10239 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10240 | |
| 10241 | VerifyReferrerAfterRedirect( |
| 10242 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10243 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10244 | |
| 10245 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10246 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10247 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10248 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10249 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 10250 | referrer); |
| 10251 | |
| 10252 | // The original referrer set on the request is expected to obey the referrer |
| 10253 | // policy and already be stripped to the origin; thus this test case just |
| 10254 | // checks that this policy doesn't cause the referrer to change when following |
| 10255 | // a redirect. |
| 10256 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 10257 | referrer.GetOrigin()); |
| 10258 | |
| 10259 | VerifyReferrerAfterRedirect( |
| 10260 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 10261 | |
| 10262 | // The original referrer set on the request is expected to obey the referrer |
| 10263 | // policy and already be stripped to the origin, though it should be |
| 10264 | // subsequently cleared during the downgrading redirect. |
| 10265 | VerifyReferrerAfterRedirect( |
| 10266 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 10267 | referrer.GetOrigin(), GURL()); |
| 10268 | |
| 10269 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10270 | } |
| 10271 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame^] | 10272 | class HTTPSRequestTest : public TestWithTaskEnvironment { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 10273 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 10274 | HTTPSRequestTest() : default_context_(true) { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10275 | default_context_.set_network_delegate(&default_network_delegate_); |
| 10276 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 10277 | } |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10278 | ~HTTPSRequestTest() override { |
| 10279 | SetTransportSecurityStateSourceForTesting(nullptr); |
| 10280 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 10281 | |
| 10282 | protected: |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 10283 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10284 | TestURLRequestContext default_context_; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10285 | }; |
| 10286 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 10287 | TEST_F(HTTPSRequestTest, HTTPSGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10288 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10289 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10290 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 10291 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10292 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10293 | TestDelegate d; |
| 10294 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10295 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10296 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10297 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10298 | r->Start(); |
| 10299 | EXPECT_TRUE(r->is_pending()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10300 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10301 | d.RunUntilComplete(); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10302 | |
| 10303 | EXPECT_EQ(1, d.response_started_count()); |
| 10304 | EXPECT_FALSE(d.received_data_before_response()); |
| 10305 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10306 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 10307 | EXPECT_EQ(test_server.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 10308 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 10309 | EXPECT_EQ(test_server.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 10310 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10311 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10312 | } |
| 10313 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 10314 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10315 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10316 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 10317 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10318 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 10319 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10320 | |
| 10321 | bool err_allowed = true; |
| 10322 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 10323 | TestDelegate d; |
| 10324 | { |
| 10325 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10326 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10327 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10328 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10329 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10330 | r->Start(); |
| 10331 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10332 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10333 | d.RunUntilComplete(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10334 | |
| 10335 | EXPECT_EQ(1, d.response_started_count()); |
| 10336 | EXPECT_FALSE(d.received_data_before_response()); |
| 10337 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 10338 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10339 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10340 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 10341 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10342 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 10343 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10344 | } |
| 10345 | } |
| 10346 | } |
| 10347 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 10348 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10349 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10350 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED); |
| 10351 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10352 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 10353 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10354 | |
| 10355 | // Iterate from false to true, just so that we do the opposite of the |
| 10356 | // previous test in order to increase test coverage. |
| 10357 | bool err_allowed = false; |
| 10358 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 10359 | TestDelegate d; |
| 10360 | { |
| 10361 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10362 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10363 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10364 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10365 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10366 | r->Start(); |
| 10367 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10368 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10369 | d.RunUntilComplete(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10370 | |
| 10371 | EXPECT_EQ(1, d.response_started_count()); |
| 10372 | EXPECT_FALSE(d.received_data_before_response()); |
| 10373 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 10374 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10375 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10376 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 10377 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10378 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 10379 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10380 | } |
| 10381 | } |
| 10382 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 10383 | |
Emily Stark | 79fba584 | 2019-04-25 04:59:36 | [diff] [blame] | 10384 | // A TestDelegate used to test that an appropriate net error code is provided |
| 10385 | // when an SSL certificate error occurs. |
| 10386 | class SSLNetErrorTestDelegate : public TestDelegate { |
| 10387 | public: |
| 10388 | void OnSSLCertificateError(URLRequest* request, |
| 10389 | int net_error, |
| 10390 | const SSLInfo& ssl_info, |
| 10391 | bool fatal) override { |
| 10392 | net_error_ = net_error; |
| 10393 | on_ssl_certificate_error_called_ = true; |
| 10394 | TestDelegate::OnSSLCertificateError(request, net_error, ssl_info, fatal); |
| 10395 | } |
| 10396 | |
| 10397 | bool on_ssl_certificate_error_called() { |
| 10398 | return on_ssl_certificate_error_called_; |
| 10399 | } |
| 10400 | |
| 10401 | int net_error() { return net_error_; } |
| 10402 | |
| 10403 | private: |
| 10404 | bool on_ssl_certificate_error_called_ = false; |
| 10405 | int net_error_ = net::OK; |
| 10406 | }; |
| 10407 | |
| 10408 | // Tests that the URLRequest::Delegate receives an appropriate net error code |
| 10409 | // when an SSL certificate error occurs. |
| 10410 | TEST_F(HTTPSRequestTest, SSLNetErrorReportedToDelegate) { |
| 10411 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10412 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED); |
| 10413 | test_server.AddDefaultHandlers( |
| 10414 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10415 | ASSERT_TRUE(test_server.Start()); |
| 10416 | |
| 10417 | SSLNetErrorTestDelegate d; |
| 10418 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10419 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10420 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10421 | r->Start(); |
| 10422 | EXPECT_TRUE(r->is_pending()); |
| 10423 | d.RunUntilComplete(); |
| 10424 | |
| 10425 | EXPECT_TRUE(d.on_ssl_certificate_error_called()); |
| 10426 | EXPECT_EQ(net::ERR_CERT_DATE_INVALID, d.net_error()); |
| 10427 | } |
| 10428 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10429 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 10430 | // security state. (see http://crbug.com/550977). |
| 10431 | #if !defined(OS_IOS) |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10432 | // This tests that a load of a domain with preloaded HSTS and HPKP with a |
| 10433 | // certificate error sets the |certificate_errors_are_fatal| flag correctly. |
| 10434 | // This flag will cause the interstitial to be fatal. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10435 | TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10436 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 10437 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10438 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10439 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 10440 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10441 | ASSERT_TRUE(test_server.Start()); |
| 10442 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10443 | // We require that the URL be hsts-hpkp-preloaded.test. This is a test domain |
| 10444 | // that has a preloaded HSTS+HPKP entry in the TransportSecurityState. This |
| 10445 | // means that we have to use a MockHostResolver in order to direct |
| 10446 | // hsts-hpkp-preloaded.test to the testserver. By default, MockHostResolver |
| 10447 | // maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10448 | |
| 10449 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 10450 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10451 | TestURLRequestContext context(true); |
| 10452 | context.set_network_delegate(&network_delegate); |
| 10453 | context.set_host_resolver(&host_resolver); |
| 10454 | TransportSecurityState transport_security_state; |
| 10455 | context.set_transport_security_state(&transport_security_state); |
| 10456 | context.Init(); |
| 10457 | |
| 10458 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10459 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10460 | GURL(base::StringPrintf("https://hsts-hpkp-preloaded.test:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 10461 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10462 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10463 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10464 | r->Start(); |
| 10465 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10466 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10467 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10468 | |
| 10469 | EXPECT_EQ(1, d.response_started_count()); |
| 10470 | EXPECT_FALSE(d.received_data_before_response()); |
| 10471 | EXPECT_TRUE(d.have_certificate_errors()); |
| 10472 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 10473 | } |
| 10474 | |
| 10475 | // This tests that cached HTTPS page loads do not cause any updates to the |
| 10476 | // TransportSecurityState. |
| 10477 | TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10478 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 10479 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10480 | // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't |
| 10481 | // matter. It just has to be any error. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10482 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10483 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 10484 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10485 | ASSERT_TRUE(test_server.Start()); |
| 10486 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10487 | // We require that the URL be hsts-hpkp-preloaded.test. This is a test domain |
| 10488 | // that has a preloaded HSTS+HPKP entry in the TransportSecurityState. This |
| 10489 | // means that we have to use a MockHostResolver in order to direct |
| 10490 | // hsts-hpkp-preloaded.test to the testserver. By default, MockHostResolver |
| 10491 | // maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10492 | |
| 10493 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 10494 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10495 | TestURLRequestContext context(true); |
| 10496 | context.set_network_delegate(&network_delegate); |
| 10497 | context.set_host_resolver(&host_resolver); |
| 10498 | TransportSecurityState transport_security_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10499 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10500 | TransportSecurityState::STSState static_sts_state; |
| 10501 | TransportSecurityState::PKPState static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10502 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10503 | "hsts-hpkp-preloaded.test", &static_sts_state, &static_pkp_state)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10504 | context.set_transport_security_state(&transport_security_state); |
| 10505 | context.Init(); |
| 10506 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10507 | TransportSecurityState::STSState dynamic_sts_state; |
| 10508 | TransportSecurityState::PKPState dynamic_pkp_state; |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10509 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
| 10510 | "hsts-hpkp-preloaded.test", &dynamic_sts_state)); |
| 10511 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
| 10512 | "hsts-hpkp-preloaded.test", &dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10513 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10514 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10515 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10516 | GURL(base::StringPrintf("https://hsts-hpkp-preloaded.test:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 10517 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10518 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10519 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10520 | r->Start(); |
| 10521 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10522 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10523 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10524 | |
| 10525 | EXPECT_EQ(1, d.response_started_count()); |
| 10526 | EXPECT_FALSE(d.received_data_before_response()); |
| 10527 | EXPECT_TRUE(d.have_certificate_errors()); |
| 10528 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 10529 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10530 | // Get a fresh copy of the states, and check that they haven't changed. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10531 | TransportSecurityState::STSState new_static_sts_state; |
| 10532 | TransportSecurityState::PKPState new_static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10533 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10534 | "hsts-hpkp-preloaded.test", &new_static_sts_state, |
| 10535 | &new_static_pkp_state)); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10536 | TransportSecurityState::STSState new_dynamic_sts_state; |
| 10537 | TransportSecurityState::PKPState new_dynamic_pkp_state; |
| 10538 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10539 | "hsts-hpkp-preloaded.test", &new_dynamic_sts_state)); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10540 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10541 | "hsts-hpkp-preloaded.test", &new_dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10542 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10543 | EXPECT_EQ(new_static_sts_state.upgrade_mode, static_sts_state.upgrade_mode); |
| 10544 | EXPECT_EQ(new_static_sts_state.include_subdomains, |
| 10545 | static_sts_state.include_subdomains); |
| 10546 | EXPECT_EQ(new_static_pkp_state.include_subdomains, |
| 10547 | static_pkp_state.include_subdomains); |
rsleevi | 91d4c9c | 2016-05-14 20:28:48 | [diff] [blame] | 10548 | EXPECT_EQ(new_static_pkp_state.spki_hashes, static_pkp_state.spki_hashes); |
| 10549 | EXPECT_EQ(new_static_pkp_state.bad_spki_hashes, |
| 10550 | static_pkp_state.bad_spki_hashes); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10551 | } |
| 10552 | |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10553 | // Make sure HSTS preserves a POST request's method and body. |
| 10554 | TEST_F(HTTPSRequestTest, HSTSPreservesPosts) { |
| 10555 | static const char kData[] = "hello world"; |
| 10556 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10557 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10558 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10559 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10560 | ASSERT_TRUE(test_server.Start()); |
| 10561 | |
| 10562 | |
| 10563 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 10564 | // address, so a MockHostResolver is needed to redirect www.somewhere.com to |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10565 | // the EmbeddedTestServer. By default, MockHostResolver maps all hosts |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10566 | // to 127.0.0.1. |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10567 | MockHostResolver host_resolver; |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10568 | |
| 10569 | // Force https for www.somewhere.com. |
| 10570 | TransportSecurityState transport_security_state; |
[email protected] | 474f079e | 2013-03-02 19:11:20 | [diff] [blame] | 10571 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 10572 | bool include_subdomains = false; |
| 10573 | transport_security_state.AddHSTS("www.somewhere.com", expiry, |
| 10574 | include_subdomains); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10575 | |
| 10576 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 10577 | |
| 10578 | TestURLRequestContext context(true); |
| 10579 | context.set_host_resolver(&host_resolver); |
| 10580 | context.set_transport_security_state(&transport_security_state); |
| 10581 | context.set_network_delegate(&network_delegate); |
| 10582 | context.Init(); |
| 10583 | |
| 10584 | TestDelegate d; |
| 10585 | // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will |
| 10586 | // cause a certificate error. Ignore the error. |
| 10587 | d.set_allow_certificate_errors(true); |
| 10588 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10589 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10590 | GURL(base::StringPrintf("http://www.somewhere.com:%d/echo", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 10591 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10592 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10593 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 10594 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10595 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10596 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10597 | d.RunUntilComplete(); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10598 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10599 | EXPECT_EQ("https", req->url().scheme()); |
| 10600 | EXPECT_EQ("POST", req->method()); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10601 | EXPECT_EQ(kData, d.data_received()); |
[email protected] | 07d9bfd | 2013-06-27 14:16:40 | [diff] [blame] | 10602 | |
| 10603 | LoadTimingInfo load_timing_info; |
| 10604 | network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info); |
| 10605 | // LoadTimingInfo of HSTS redirects is similar to that of network cache hits |
| 10606 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10607 | } |
| 10608 | |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10609 | // Make sure that the CORS headers are added to cross-origin HSTS redirects. |
| 10610 | TEST_F(HTTPSRequestTest, HSTSCrossOriginAddHeaders) { |
| 10611 | static const char kOriginHeaderValue[] = "http://www.example.com"; |
| 10612 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10613 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10614 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10615 | ASSERT_TRUE(test_server.Start()); |
| 10616 | |
| 10617 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 10618 | // address, so a MockHostResolver is needed to redirect example.net to the |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10619 | // EmbeddedTestServer. MockHostResolver maps all hosts to 127.0.0.1 by |
| 10620 | // default. |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10621 | MockHostResolver host_resolver; |
| 10622 | |
| 10623 | TransportSecurityState transport_security_state; |
| 10624 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 10625 | bool include_subdomains = false; |
| 10626 | transport_security_state.AddHSTS("example.net", expiry, include_subdomains); |
| 10627 | |
| 10628 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 10629 | |
| 10630 | MockCertVerifier cert_verifier; |
| 10631 | cert_verifier.set_default_result(OK); |
| 10632 | |
| 10633 | TestURLRequestContext context(true); |
| 10634 | context.set_host_resolver(&host_resolver); |
| 10635 | context.set_transport_security_state(&transport_security_state); |
| 10636 | context.set_network_delegate(&network_delegate); |
| 10637 | context.set_cert_verifier(&cert_verifier); |
| 10638 | context.Init(); |
| 10639 | |
| 10640 | GURL hsts_http_url(base::StringPrintf("http://example.net:%d/somehstssite", |
| 10641 | test_server.host_port_pair().port())); |
| 10642 | url::Replacements<char> replacements; |
| 10643 | const char kNewScheme[] = "https"; |
| 10644 | replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme))); |
| 10645 | GURL hsts_https_url = hsts_http_url.ReplaceComponents(replacements); |
| 10646 | |
| 10647 | TestDelegate d; |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10648 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10649 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 10650 | hsts_http_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10651 | // Set Origin header to simulate a cross-origin request. |
| 10652 | HttpRequestHeaders request_headers; |
| 10653 | request_headers.SetHeader("Origin", kOriginHeaderValue); |
| 10654 | req->SetExtraRequestHeaders(request_headers); |
| 10655 | |
| 10656 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10657 | d.RunUntilRedirect(); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10658 | |
| 10659 | EXPECT_EQ(1, d.received_redirect_count()); |
| 10660 | |
| 10661 | const HttpResponseHeaders* headers = req->response_headers(); |
| 10662 | std::string redirect_location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 10663 | EXPECT_TRUE( |
| 10664 | headers->EnumerateHeader(nullptr, "Location", &redirect_location)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10665 | EXPECT_EQ(hsts_https_url.spec(), redirect_location); |
| 10666 | |
| 10667 | std::string received_cors_header; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 10668 | EXPECT_TRUE(headers->EnumerateHeader(nullptr, "Access-Control-Allow-Origin", |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10669 | &received_cors_header)); |
| 10670 | EXPECT_EQ(kOriginHeaderValue, received_cors_header); |
| 10671 | } |
| 10672 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10673 | namespace { |
| 10674 | |
| 10675 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 10676 | public: |
| 10677 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10678 | set_on_complete(base::DoNothing()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10679 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 10680 | void OnCertificateRequested(URLRequest* request, |
| 10681 | SSLCertRequestInfo* cert_request_info) override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10682 | on_certificate_requested_count_++; |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10683 | std::move(on_certificate_requested_).Run(); |
| 10684 | } |
| 10685 | void RunUntilCertificateRequested() { |
| 10686 | base::RunLoop run_loop; |
| 10687 | on_certificate_requested_ = run_loop.QuitClosure(); |
| 10688 | run_loop.Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10689 | } |
| 10690 | int on_certificate_requested_count() { |
| 10691 | return on_certificate_requested_count_; |
| 10692 | } |
| 10693 | private: |
| 10694 | int on_certificate_requested_count_; |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10695 | base::OnceClosure on_certificate_requested_; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10696 | }; |
| 10697 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10698 | class TestSSLPrivateKey : public SSLPrivateKey { |
| 10699 | public: |
| 10700 | explicit TestSSLPrivateKey(scoped_refptr<SSLPrivateKey> key) |
| 10701 | : key_(std::move(key)) {} |
| 10702 | |
| 10703 | void set_fail_signing(bool fail_signing) { fail_signing_ = fail_signing; } |
| 10704 | int sign_count() const { return sign_count_; } |
| 10705 | |
David Benjamin | b65b073 | 2018-11-09 20:33:53 | [diff] [blame] | 10706 | std::string GetProviderName() override { return key_->GetProviderName(); } |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10707 | std::vector<uint16_t> GetAlgorithmPreferences() override { |
| 10708 | return key_->GetAlgorithmPreferences(); |
| 10709 | } |
| 10710 | void Sign(uint16_t algorithm, |
| 10711 | base::span<const uint8_t> input, |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 10712 | SignCallback callback) override { |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10713 | sign_count_++; |
| 10714 | if (fail_signing_) { |
| 10715 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 10716 | FROM_HERE, base::BindOnce(std::move(callback), |
| 10717 | ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED, |
| 10718 | std::vector<uint8_t>())); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10719 | } else { |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 10720 | key_->Sign(algorithm, input, std::move(callback)); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10721 | } |
| 10722 | } |
| 10723 | |
| 10724 | private: |
| 10725 | ~TestSSLPrivateKey() override = default; |
| 10726 | |
| 10727 | scoped_refptr<SSLPrivateKey> key_; |
| 10728 | bool fail_signing_ = false; |
| 10729 | int sign_count_ = 0; |
| 10730 | }; |
| 10731 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10732 | } // namespace |
| 10733 | |
| 10734 | // TODO(davidben): Test the rest of the code. Specifically, |
| 10735 | // - Filtering which certificates to select. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10736 | // - Getting a certificate request in an SSL renegotiation sending the |
| 10737 | // HTTP request. |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10738 | TEST_F(HTTPSRequestTest, ClientAuthNoCertificate) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10739 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10740 | net::SSLServerConfig ssl_config; |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 10741 | ssl_config.client_cert_type = |
| 10742 | SSLServerConfig::ClientCertType::OPTIONAL_CLIENT_CERT; |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10743 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10744 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10745 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10746 | ASSERT_TRUE(test_server.Start()); |
| 10747 | |
| 10748 | SSLClientAuthTestDelegate d; |
| 10749 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10750 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10751 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10752 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10753 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10754 | r->Start(); |
| 10755 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10756 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10757 | d.RunUntilCertificateRequested(); |
| 10758 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10759 | |
| 10760 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10761 | EXPECT_FALSE(d.received_data_before_response()); |
| 10762 | EXPECT_EQ(0, d.bytes_received()); |
| 10763 | |
| 10764 | // Send no certificate. |
| 10765 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 10766 | // all platforms so we can test sending a cert as well. |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 10767 | r->ContinueWithCertificate(nullptr, nullptr); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10768 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10769 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10770 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10771 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10772 | EXPECT_EQ(1, d.response_started_count()); |
| 10773 | EXPECT_FALSE(d.received_data_before_response()); |
| 10774 | EXPECT_NE(0, d.bytes_received()); |
| 10775 | } |
| 10776 | } |
| 10777 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10778 | TEST_F(HTTPSRequestTest, ClientAuth) { |
| 10779 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 10780 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 10781 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 10782 | ASSERT_TRUE(identity); |
| 10783 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 10784 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 10785 | |
| 10786 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10787 | net::SSLServerConfig ssl_config; |
| 10788 | ssl_config.client_cert_type = |
| 10789 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 10790 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 10791 | test_server.AddDefaultHandlers( |
| 10792 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10793 | ASSERT_TRUE(test_server.Start()); |
| 10794 | |
| 10795 | { |
| 10796 | SSLClientAuthTestDelegate d; |
| 10797 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10798 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10799 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10800 | |
| 10801 | r->Start(); |
| 10802 | EXPECT_TRUE(r->is_pending()); |
| 10803 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10804 | d.RunUntilCertificateRequested(); |
| 10805 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10806 | |
| 10807 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10808 | EXPECT_FALSE(d.received_data_before_response()); |
| 10809 | EXPECT_EQ(0, d.bytes_received()); |
| 10810 | |
| 10811 | // Send a certificate. |
| 10812 | r->ContinueWithCertificate(identity->certificate(), private_key); |
| 10813 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10814 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10815 | |
| 10816 | EXPECT_EQ(OK, d.request_status()); |
| 10817 | EXPECT_EQ(1, d.response_started_count()); |
| 10818 | EXPECT_FALSE(d.received_data_before_response()); |
| 10819 | EXPECT_NE(0, d.bytes_received()); |
| 10820 | |
| 10821 | // The private key should have been used. |
| 10822 | EXPECT_EQ(1, private_key->sign_count()); |
| 10823 | } |
| 10824 | |
| 10825 | // Close all connections and clear the session cache to force a new handshake. |
| 10826 | default_context_.http_transaction_factory() |
| 10827 | ->GetSession() |
| 10828 | ->CloseAllConnections(); |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10829 | default_context_.http_transaction_factory() |
| 10830 | ->GetSession() |
| 10831 | ->ClearSSLSessionCache(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10832 | |
| 10833 | // Connecting again should not call OnCertificateRequested. The identity is |
| 10834 | // taken from the client auth cache. |
| 10835 | { |
| 10836 | SSLClientAuthTestDelegate d; |
| 10837 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10838 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10839 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10840 | |
| 10841 | r->Start(); |
| 10842 | EXPECT_TRUE(r->is_pending()); |
| 10843 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10844 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10845 | |
| 10846 | EXPECT_EQ(OK, d.request_status()); |
| 10847 | EXPECT_EQ(0, d.on_certificate_requested_count()); |
| 10848 | EXPECT_FALSE(d.received_data_before_response()); |
| 10849 | EXPECT_EQ(1, d.response_started_count()); |
| 10850 | EXPECT_FALSE(d.received_data_before_response()); |
| 10851 | EXPECT_NE(0, d.bytes_received()); |
| 10852 | |
| 10853 | // The private key should have been used. |
| 10854 | EXPECT_EQ(2, private_key->sign_count()); |
| 10855 | } |
| 10856 | } |
| 10857 | |
| 10858 | // Test that private keys that fail to sign anything get evicted from the cache. |
| 10859 | TEST_F(HTTPSRequestTest, ClientAuthFailSigning) { |
| 10860 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 10861 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 10862 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 10863 | ASSERT_TRUE(identity); |
| 10864 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 10865 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 10866 | private_key->set_fail_signing(true); |
| 10867 | |
| 10868 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10869 | net::SSLServerConfig ssl_config; |
| 10870 | ssl_config.client_cert_type = |
| 10871 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 10872 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 10873 | test_server.AddDefaultHandlers( |
| 10874 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10875 | ASSERT_TRUE(test_server.Start()); |
| 10876 | |
| 10877 | { |
| 10878 | SSLClientAuthTestDelegate d; |
| 10879 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10880 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10881 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10882 | |
| 10883 | r->Start(); |
| 10884 | EXPECT_TRUE(r->is_pending()); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10885 | |
| 10886 | d.RunUntilCertificateRequested(); |
| 10887 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10888 | |
| 10889 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10890 | EXPECT_FALSE(d.received_data_before_response()); |
| 10891 | EXPECT_EQ(0, d.bytes_received()); |
| 10892 | |
| 10893 | // Send a certificate. |
| 10894 | r->ContinueWithCertificate(identity->certificate(), private_key); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10895 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10896 | |
| 10897 | // The private key cannot sign anything, so we report an error. |
| 10898 | EXPECT_EQ(ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED, d.request_status()); |
| 10899 | EXPECT_EQ(1, d.response_started_count()); |
| 10900 | EXPECT_FALSE(d.received_data_before_response()); |
| 10901 | EXPECT_EQ(0, d.bytes_received()); |
| 10902 | |
| 10903 | // The private key should have been used. |
| 10904 | EXPECT_EQ(1, private_key->sign_count()); |
| 10905 | } |
| 10906 | |
| 10907 | // Close all connections and clear the session cache to force a new handshake. |
| 10908 | default_context_.http_transaction_factory() |
| 10909 | ->GetSession() |
| 10910 | ->CloseAllConnections(); |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10911 | default_context_.http_transaction_factory() |
| 10912 | ->GetSession() |
| 10913 | ->ClearSSLSessionCache(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10914 | |
| 10915 | // The bad identity should have been evicted from the cache, so connecting |
| 10916 | // again should call OnCertificateRequested again. |
| 10917 | { |
| 10918 | SSLClientAuthTestDelegate d; |
| 10919 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10920 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10921 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10922 | |
| 10923 | r->Start(); |
| 10924 | EXPECT_TRUE(r->is_pending()); |
| 10925 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10926 | d.RunUntilCertificateRequested(); |
| 10927 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10928 | |
| 10929 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10930 | EXPECT_FALSE(d.received_data_before_response()); |
| 10931 | EXPECT_EQ(0, d.bytes_received()); |
| 10932 | |
| 10933 | // There should have been no additional uses of the private key. |
| 10934 | EXPECT_EQ(1, private_key->sign_count()); |
| 10935 | } |
| 10936 | } |
| 10937 | |
| 10938 | // Test that cached private keys that fail to sign anything trigger a |
| 10939 | // retry. This is so we handle unplugged smartcards |
| 10940 | // gracefully. https://crbug.com/813022. |
| 10941 | TEST_F(HTTPSRequestTest, ClientAuthFailSigningRetry) { |
| 10942 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 10943 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 10944 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 10945 | ASSERT_TRUE(identity); |
| 10946 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 10947 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 10948 | |
| 10949 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10950 | net::SSLServerConfig ssl_config; |
| 10951 | ssl_config.client_cert_type = |
| 10952 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 10953 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 10954 | test_server.AddDefaultHandlers( |
| 10955 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10956 | ASSERT_TRUE(test_server.Start()); |
| 10957 | |
| 10958 | // Connect with a client certificate to put it in the client auth cache. |
| 10959 | { |
| 10960 | SSLClientAuthTestDelegate d; |
| 10961 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10962 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10963 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10964 | |
| 10965 | r->Start(); |
| 10966 | EXPECT_TRUE(r->is_pending()); |
| 10967 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10968 | d.RunUntilCertificateRequested(); |
| 10969 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10970 | |
| 10971 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10972 | EXPECT_FALSE(d.received_data_before_response()); |
| 10973 | EXPECT_EQ(0, d.bytes_received()); |
| 10974 | |
| 10975 | r->ContinueWithCertificate(identity->certificate(), private_key); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10976 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10977 | |
| 10978 | EXPECT_EQ(OK, d.request_status()); |
| 10979 | EXPECT_EQ(1, d.response_started_count()); |
| 10980 | EXPECT_FALSE(d.received_data_before_response()); |
| 10981 | EXPECT_NE(0, d.bytes_received()); |
| 10982 | |
| 10983 | // The private key should have been used. |
| 10984 | EXPECT_EQ(1, private_key->sign_count()); |
| 10985 | } |
| 10986 | |
| 10987 | // Close all connections and clear the session cache to force a new handshake. |
| 10988 | default_context_.http_transaction_factory() |
| 10989 | ->GetSession() |
| 10990 | ->CloseAllConnections(); |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10991 | default_context_.http_transaction_factory() |
| 10992 | ->GetSession() |
| 10993 | ->ClearSSLSessionCache(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10994 | |
| 10995 | // Cause the private key to fail. Connecting again should attempt to use it, |
| 10996 | // notice the failure, and then request a new identity via |
| 10997 | // OnCertificateRequested. |
| 10998 | private_key->set_fail_signing(true); |
| 10999 | |
| 11000 | { |
| 11001 | SSLClientAuthTestDelegate d; |
| 11002 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 11003 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 11004 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11005 | |
| 11006 | r->Start(); |
| 11007 | EXPECT_TRUE(r->is_pending()); |
| 11008 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 11009 | d.RunUntilCertificateRequested(); |
| 11010 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 11011 | |
| 11012 | // There was an additional signing call on the private key (the one which |
| 11013 | // failed). |
| 11014 | EXPECT_EQ(2, private_key->sign_count()); |
| 11015 | |
| 11016 | // That caused another OnCertificateRequested call. |
| 11017 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 11018 | EXPECT_FALSE(d.received_data_before_response()); |
| 11019 | EXPECT_EQ(0, d.bytes_received()); |
| 11020 | } |
| 11021 | } |
| 11022 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11023 | TEST_F(HTTPSRequestTest, ResumeTest) { |
| 11024 | // Test that we attempt a session resume when making two connections to the |
| 11025 | // same host. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11026 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11027 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11028 | SpawnedTestServer test_server( |
| 11029 | SpawnedTestServer::TYPE_HTTPS, |
| 11030 | ssl_options, |
| 11031 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11032 | ASSERT_TRUE(test_server.Start()); |
| 11033 | |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 11034 | default_context_.http_transaction_factory() |
| 11035 | ->GetSession() |
| 11036 | ->ClearSSLSessionCache(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11037 | |
| 11038 | { |
| 11039 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11040 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11041 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 11042 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11043 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11044 | r->Start(); |
| 11045 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11046 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11047 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11048 | |
| 11049 | EXPECT_EQ(1, d.response_started_count()); |
| 11050 | } |
| 11051 | |
| 11052 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 11053 | CloseAllConnections(); |
| 11054 | |
| 11055 | { |
| 11056 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11057 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11058 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 11059 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11060 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11061 | r->Start(); |
| 11062 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11063 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11064 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11065 | |
| 11066 | // The response will look like; |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 11067 | // lookup uvw (TLS 1.3's compatibility session ID) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11068 | // insert abc |
| 11069 | // lookup abc |
| 11070 | // insert xyz |
| 11071 | // |
| 11072 | // With a newline at the end which makes the split think that there are |
| 11073 | // four lines. |
| 11074 | |
| 11075 | EXPECT_EQ(1, d.response_started_count()); |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 11076 | std::vector<std::string> lines = base::SplitString( |
| 11077 | d.data_received(), "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 11078 | ASSERT_EQ(5u, lines.size()) << d.data_received(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11079 | |
| 11080 | std::string session_id; |
| 11081 | |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 11082 | for (size_t i = 0; i < 3; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 11083 | std::vector<std::string> parts = base::SplitString( |
| 11084 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11085 | ASSERT_EQ(2u, parts.size()); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 11086 | if (i % 2 == 1) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11087 | EXPECT_EQ("insert", parts[0]); |
| 11088 | session_id = parts[1]; |
| 11089 | } else { |
| 11090 | EXPECT_EQ("lookup", parts[0]); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 11091 | if (i != 0) |
| 11092 | EXPECT_EQ(session_id, parts[1]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11093 | } |
| 11094 | } |
| 11095 | } |
| 11096 | } |
| 11097 | |
| 11098 | TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { |
| 11099 | // Test that sessions aren't resumed when the value of ssl_session_cache_shard |
| 11100 | // differs. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11101 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11102 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11103 | SpawnedTestServer test_server( |
| 11104 | SpawnedTestServer::TYPE_HTTPS, |
| 11105 | ssl_options, |
| 11106 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11107 | ASSERT_TRUE(test_server.Start()); |
| 11108 | |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 11109 | default_context_.http_transaction_factory() |
| 11110 | ->GetSession() |
| 11111 | ->ClearSSLSessionCache(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11112 | |
| 11113 | { |
| 11114 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11115 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 11116 | test_server.GetURL("/"), DEFAULT_PRIORITY, &d, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11117 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11118 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11119 | r->Start(); |
| 11120 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11121 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11122 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11123 | |
| 11124 | EXPECT_EQ(1, d.response_started_count()); |
| 11125 | } |
| 11126 | |
| 11127 | // Now create a new HttpCache with a different ssl_session_cache_shard value. |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 11128 | HttpNetworkSession::Context session_context; |
| 11129 | session_context.host_resolver = default_context_.host_resolver(); |
| 11130 | session_context.cert_verifier = default_context_.cert_verifier(); |
| 11131 | session_context.transport_security_state = |
| 11132 | default_context_.transport_security_state(); |
| 11133 | session_context.cert_transparency_verifier = |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 11134 | default_context_.cert_transparency_verifier(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 11135 | session_context.ct_policy_enforcer = default_context_.ct_policy_enforcer(); |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 11136 | session_context.proxy_resolution_service = default_context_.proxy_resolution_service(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 11137 | session_context.ssl_config_service = default_context_.ssl_config_service(); |
| 11138 | session_context.http_auth_handler_factory = |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11139 | default_context_.http_auth_handler_factory(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 11140 | session_context.http_server_properties = |
| 11141 | default_context_.http_server_properties(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11142 | |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 11143 | HttpNetworkSession network_session(HttpNetworkSession::Params(), |
| 11144 | session_context); |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 11145 | std::unique_ptr<HttpCache> cache( |
| 11146 | new HttpCache(&network_session, HttpCache::DefaultBackend::InMemory(0), |
| 11147 | false /* is_main_cache */)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11148 | |
| 11149 | default_context_.set_http_transaction_factory(cache.get()); |
| 11150 | |
| 11151 | { |
| 11152 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11153 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11154 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 11155 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11156 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11157 | r->Start(); |
| 11158 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11159 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11160 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11161 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11162 | EXPECT_EQ(1, d.response_started_count()); |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 11163 | EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, r->ssl_info().handshake_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11164 | } |
| 11165 | } |
| 11166 | |
Daniel McArdle | da3fa94 | 2019-02-15 16:41:21 | [diff] [blame] | 11167 | // Test that sessions started with privacy mode enabled cannot be resumed when |
| 11168 | // it is disabled, and vice versa. |
| 11169 | TEST_F(HTTPSRequestTest, NoSessionResumptionBetweenPrivacyModes) { |
| 11170 | // Start a server. |
| 11171 | SpawnedTestServer test_server( |
| 11172 | SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::SSLOptions(), |
| 11173 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 11174 | ASSERT_TRUE(test_server.Start()); |
| 11175 | const auto url = test_server.GetURL("/"); |
| 11176 | |
| 11177 | auto ConnectAndCheckHandshake = [this, url](bool allow_credentials, |
| 11178 | auto expected_handshake) { |
| 11179 | // Construct request and indirectly set the privacy mode. |
| 11180 | TestDelegate d; |
| 11181 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 11182 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11183 | r->set_allow_credentials(allow_credentials); |
| 11184 | |
| 11185 | // Start the request and check the SSL handshake type. |
| 11186 | r->Start(); |
| 11187 | EXPECT_TRUE(r->is_pending()); |
| 11188 | d.RunUntilComplete(); |
| 11189 | EXPECT_EQ(1, d.response_started_count()); |
| 11190 | EXPECT_EQ(expected_handshake, r->ssl_info().handshake_type); |
| 11191 | }; |
| 11192 | |
| 11193 | // Exhaustively check all pairs of privacy mode values. Note that we are using |
| 11194 | // allow_credentials to indirectly enable/disable privacy mode. |
| 11195 | const bool kAllowCredentialsValues[] = {false, true}; |
| 11196 | for (const auto allow_creds_1 : kAllowCredentialsValues) { |
| 11197 | for (const auto allow_creds_2 : kAllowCredentialsValues) { |
| 11198 | SCOPED_TRACE(base::StringPrintf("allow_creds_1=%d, allow_creds_2=%d", |
| 11199 | allow_creds_1, allow_creds_2)); |
| 11200 | |
| 11201 | // The session cache starts off empty, so we expect a full handshake. |
| 11202 | ConnectAndCheckHandshake(allow_creds_1, SSLInfo::HANDSHAKE_FULL); |
| 11203 | |
| 11204 | // The second handshake depends on whether we are using the same session |
| 11205 | // cache as the first request. |
| 11206 | ConnectAndCheckHandshake(allow_creds_2, allow_creds_1 == allow_creds_2 |
| 11207 | ? SSLInfo::HANDSHAKE_RESUME |
| 11208 | : SSLInfo::HANDSHAKE_FULL); |
| 11209 | // Flush both session caches. |
| 11210 | auto* network_session = |
| 11211 | default_context_.http_transaction_factory()->GetSession(); |
| 11212 | network_session->ClearSSLSessionCache(); |
| 11213 | } |
| 11214 | } |
| 11215 | } |
| 11216 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame^] | 11217 | class HTTPSFallbackTest : public TestWithTaskEnvironment { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11218 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 11219 | HTTPSFallbackTest() : context_(true) { |
David Benjamin | 151ec6b | 2019-08-02 19:38:52 | [diff] [blame] | 11220 | ssl_config_service_ = |
| 11221 | std::make_unique<TestSSLConfigService>(SSLContextConfig()); |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 11222 | context_.set_ssl_config_service(ssl_config_service_.get()); |
| 11223 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 11224 | ~HTTPSFallbackTest() override = default; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11225 | |
| 11226 | protected: |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 11227 | TestSSLConfigService* ssl_config_service() { |
| 11228 | return ssl_config_service_.get(); |
| 11229 | } |
| 11230 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11231 | void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) { |
| 11232 | DCHECK(!request_); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 11233 | context_.Init(); |
| 11234 | delegate_.set_allow_certificate_errors(true); |
| 11235 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11236 | SpawnedTestServer test_server( |
| 11237 | SpawnedTestServer::TYPE_HTTPS, |
| 11238 | ssl_options, |
| 11239 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 11240 | ASSERT_TRUE(test_server.Start()); |
| 11241 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11242 | request_ = context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11243 | &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11244 | request_->Start(); |
| 11245 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11246 | delegate_.RunUntilComplete(); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11247 | } |
| 11248 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 11249 | void ExpectConnection(int version) { |
| 11250 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 11251 | EXPECT_NE(0, delegate_.bytes_received()); |
| 11252 | EXPECT_EQ(version, SSLConnectionStatusToVersion( |
| 11253 | request_->ssl_info().connection_status)); |
| 11254 | } |
| 11255 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11256 | void ExpectFailure(int error) { |
| 11257 | EXPECT_EQ(1, delegate_.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 11258 | EXPECT_EQ(error, delegate_.request_status()); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11259 | } |
| 11260 | |
| 11261 | private: |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11262 | std::unique_ptr<TestSSLConfigService> ssl_config_service_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11263 | TestDelegate delegate_; |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 11264 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11265 | std::unique_ptr<URLRequest> request_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11266 | }; |
| 11267 | |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 11268 | // Tests the TLS 1.0 fallback doesn't happen. |
| 11269 | TEST_F(HTTPSFallbackTest, TLSv1NoFallback) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11270 | SpawnedTestServer::SSLOptions ssl_options( |
| 11271 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 11272 | ssl_options.tls_intolerant = |
| 11273 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 11274 | |
| 11275 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 11276 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 11277 | } |
| 11278 | |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 11279 | // Tests the TLS 1.1 fallback doesn't happen. |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 11280 | TEST_F(HTTPSFallbackTest, TLSv1_1NoFallback) { |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 11281 | SpawnedTestServer::SSLOptions ssl_options( |
| 11282 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 11283 | ssl_options.tls_intolerant = |
| 11284 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2; |
| 11285 | |
| 11286 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11287 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
| 11288 | } |
| 11289 | |
David Benjamin | d61bd53 | 2019-04-23 21:11:37 | [diff] [blame] | 11290 | // Tests the TLS 1.2 fallback doesn't happen. |
| 11291 | TEST_F(HTTPSFallbackTest, TLSv1_2NoFallback) { |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 11292 | SpawnedTestServer::SSLOptions ssl_options( |
| 11293 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 11294 | ssl_options.tls_intolerant = |
| 11295 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_3; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 11296 | |
| 11297 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
David Benjamin | d61bd53 | 2019-04-23 21:11:37 | [diff] [blame] | 11298 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 11299 | } |
| 11300 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame^] | 11301 | class HTTPSSessionTest : public TestWithTaskEnvironment { |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11302 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 11303 | HTTPSSessionTest() : default_context_(true) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 11304 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11305 | |
| 11306 | default_context_.set_network_delegate(&default_network_delegate_); |
| 11307 | default_context_.set_cert_verifier(&cert_verifier_); |
| 11308 | default_context_.Init(); |
| 11309 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 11310 | ~HTTPSSessionTest() override = default; |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11311 | |
| 11312 | protected: |
| 11313 | MockCertVerifier cert_verifier_; |
| 11314 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 11315 | TestURLRequestContext default_context_; |
| 11316 | }; |
| 11317 | |
| 11318 | // Tests that session resumption is not attempted if an invalid certificate |
| 11319 | // is presented. |
| 11320 | TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) { |
| 11321 | SpawnedTestServer::SSLOptions ssl_options; |
| 11322 | ssl_options.record_resume = true; |
| 11323 | SpawnedTestServer test_server( |
| 11324 | SpawnedTestServer::TYPE_HTTPS, |
| 11325 | ssl_options, |
| 11326 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 11327 | ASSERT_TRUE(test_server.Start()); |
| 11328 | |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 11329 | default_context_.http_transaction_factory() |
| 11330 | ->GetSession() |
| 11331 | ->ClearSSLSessionCache(); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11332 | |
| 11333 | // Simulate the certificate being expired and attempt a connection. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 11334 | cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11335 | { |
| 11336 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11337 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 11338 | test_server.GetURL("/"), DEFAULT_PRIORITY, &d, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11339 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11340 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11341 | r->Start(); |
| 11342 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11343 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11344 | d.RunUntilComplete(); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11345 | |
| 11346 | EXPECT_EQ(1, d.response_started_count()); |
| 11347 | } |
| 11348 | |
| 11349 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 11350 | CloseAllConnections(); |
| 11351 | |
| 11352 | // Now change the certificate to be acceptable (so that the response is |
| 11353 | // loaded), and ensure that no session id is presented to the peer. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 11354 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11355 | { |
| 11356 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11357 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11358 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 11359 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11360 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11361 | r->Start(); |
| 11362 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11363 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11364 | d.RunUntilComplete(); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11365 | |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11366 | EXPECT_EQ(1, d.response_started_count()); |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 11367 | EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, r->ssl_info().handshake_type); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11368 | } |
| 11369 | } |
| 11370 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11371 | // This the fingerprint of the "Testing CA" certificate used by the testserver. |
| 11372 | // See net/data/ssl/certificates/ocsp-test-root.pem. |
David Benjamin | 9cedc3a5 | 2017-08-20 21:30:58 | [diff] [blame] | 11373 | static const SHA256HashValue kOCSPTestCertFingerprint = {{ |
| 11374 | 0x0c, 0xa9, 0x05, 0x11, 0xb0, 0xa2, 0xc0, 0x1d, 0x40, 0x6a, 0x99, |
| 11375 | 0x04, 0x21, 0x36, 0x45, 0x3f, 0x59, 0x12, 0x5c, 0x80, 0x64, 0x2d, |
| 11376 | 0x46, 0x6a, 0x3b, 0x78, 0x9e, 0x84, 0xea, 0x54, 0x0f, 0x8b, |
mattm | 3b4376f1 | 2016-10-03 21:07:15 | [diff] [blame] | 11377 | }}; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11378 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11379 | // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the |
| 11380 | // testserver. |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11381 | static const SHA256HashValue kOCSPTestCertSPKI = {{ |
| 11382 | 0x05, 0xa8, 0xf6, 0xfd, 0x8e, 0x10, 0xfe, 0x92, 0x2f, 0x22, 0x75, |
| 11383 | 0x46, 0x40, 0xf4, 0xc4, 0x57, 0x06, 0x0d, 0x95, 0xfd, 0x60, 0x31, |
| 11384 | 0x3b, 0xf3, 0xfc, 0x12, 0x47, 0xe7, 0x66, 0x1a, 0x82, 0xa3, |
| 11385 | }}; |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11386 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11387 | // This is the policy OID contained in the certificates that testserver |
| 11388 | // generates. |
| 11389 | static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1"; |
| 11390 | |
| 11391 | class HTTPSOCSPTest : public HTTPSRequestTest { |
| 11392 | public: |
| 11393 | HTTPSOCSPTest() |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 11394 | : context_(true), |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11395 | ev_test_policy_( |
| 11396 | new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(), |
| 11397 | kOCSPTestCertFingerprint, |
| 11398 | kOCSPTestCertPolicy)) { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11399 | } |
| 11400 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 11401 | void SetUp() override { |
Matt Mueller | a4aaa9d | 2019-04-24 21:09:14 | [diff] [blame] | 11402 | cert_net_fetcher_ = base::MakeRefCounted<CertNetFetcherImpl>(); |
| 11403 | cert_verifier_ = CertVerifier::CreateDefault(cert_net_fetcher_); |
| 11404 | context_.set_cert_verifier(cert_verifier_.get()); |
Ryan Sleevi | 8a9c9c1 | 2018-05-09 02:36:23 | [diff] [blame] | 11405 | context_.SetCTPolicyEnforcer(std::make_unique<DefaultCTPolicyEnforcer>()); |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 11406 | context_.Init(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11407 | |
Matt Mueller | a4aaa9d | 2019-04-24 21:09:14 | [diff] [blame] | 11408 | cert_net_fetcher_->SetURLRequestContext(&context_); |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11409 | context_.cert_verifier()->SetConfig(GetCertVerifierConfig()); |
| 11410 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 11411 | scoped_refptr<X509Certificate> root_cert = |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11412 | ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
Matt Mueller | 09c8fd9a | 2019-05-04 00:37:24 | [diff] [blame] | 11413 | ASSERT_TRUE(root_cert); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 11414 | test_root_.reset(new ScopedTestRoot(root_cert.get())); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11415 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 11416 | #if defined(USE_NSS_CERTS) |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 11417 | SetURLRequestContextForNSSHttpIO(&context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11418 | #endif |
| 11419 | } |
| 11420 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11421 | void DoConnectionWithDelegate( |
| 11422 | const SpawnedTestServer::SSLOptions& ssl_options, |
| 11423 | TestDelegate* delegate, |
| 11424 | SSLInfo* out_ssl_info) { |
| 11425 | // Always overwrite |out_ssl_info|. |
| 11426 | out_ssl_info->Reset(); |
| 11427 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11428 | SpawnedTestServer test_server( |
| 11429 | SpawnedTestServer::TYPE_HTTPS, |
| 11430 | ssl_options, |
| 11431 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11432 | ASSERT_TRUE(test_server.Start()); |
| 11433 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11434 | delegate->set_allow_certificate_errors(true); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11435 | std::unique_ptr<URLRequest> r( |
| 11436 | context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
| 11437 | delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11438 | r->Start(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11439 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11440 | delegate->RunUntilComplete(); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11441 | EXPECT_EQ(1, delegate->response_started_count()); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11442 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11443 | *out_ssl_info = r->ssl_info(); |
| 11444 | } |
| 11445 | |
| 11446 | void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options, |
| 11447 | CertStatus* out_cert_status) { |
| 11448 | // Always overwrite |out_cert_status|. |
| 11449 | *out_cert_status = 0; |
| 11450 | |
| 11451 | TestDelegate d; |
| 11452 | SSLInfo ssl_info; |
| 11453 | ASSERT_NO_FATAL_FAILURE( |
| 11454 | DoConnectionWithDelegate(ssl_options, &d, &ssl_info)); |
| 11455 | |
| 11456 | *out_cert_status = ssl_info.cert_status; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11457 | } |
| 11458 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 11459 | ~HTTPSOCSPTest() override { |
Matt Mueller | a4aaa9d | 2019-04-24 21:09:14 | [diff] [blame] | 11460 | cert_net_fetcher_->Shutdown(); |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 11461 | #if defined(USE_NSS_CERTS) |
Matt Mueller | a17ffaf | 2018-03-28 20:08:18 | [diff] [blame] | 11462 | SetURLRequestContextForNSSHttpIO(nullptr); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11463 | #endif |
| 11464 | } |
| 11465 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11466 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11467 | // GetCertVerifierConfig() configures the URLRequestContext that will be used |
| 11468 | // for making connections to the testserver. This can be overridden in test |
| 11469 | // subclasses for different behaviour. |
| 11470 | virtual CertVerifier::Config GetCertVerifierConfig() { |
| 11471 | CertVerifier::Config config; |
| 11472 | config.enable_rev_checking = true; |
| 11473 | return config; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11474 | } |
| 11475 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11476 | std::unique_ptr<ScopedTestRoot> test_root_; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11477 | std::unique_ptr<TestSSLConfigService> ssl_config_service_; |
Matt Mueller | a4aaa9d | 2019-04-24 21:09:14 | [diff] [blame] | 11478 | scoped_refptr<CertNetFetcherImpl> cert_net_fetcher_; |
| 11479 | std::unique_ptr<CertVerifier> cert_verifier_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 11480 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11481 | std::unique_ptr<ScopedTestEVPolicy> ev_test_policy_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11482 | }; |
| 11483 | |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 11484 | static bool UsingBuiltinCertVerifier() { |
| 11485 | #if defined(OS_FUCHSIA) |
| 11486 | return true; |
| 11487 | #elif BUILDFLAG(BUILTIN_CERT_VERIFIER_FEATURE_SUPPORTED) |
| 11488 | if (base::FeatureList::IsEnabled(features::kCertVerifierBuiltinFeature)) |
| 11489 | return true; |
| 11490 | #endif |
| 11491 | return false; |
| 11492 | } |
| 11493 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11494 | static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() { |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 11495 | if (UsingBuiltinCertVerifier()) |
| 11496 | return 0; |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11497 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11498 | // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't |
| 11499 | // have that ability on other platforms. |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11500 | // TODO(eroman): Should this also be the return value for |
| 11501 | // CertVerifyProcBuiltin? |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11502 | return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 11503 | #else |
| 11504 | return 0; |
| 11505 | #endif |
| 11506 | } |
| 11507 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11508 | // SystemSupportsHardFailRevocationChecking returns true iff the current |
| 11509 | // operating system supports revocation checking and can distinguish between |
| 11510 | // situations where a given certificate lacks any revocation information (eg: |
| 11511 | // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when |
| 11512 | // revocation information cannot be obtained (eg: the CRL was unreachable). |
| 11513 | // If it does not, then tests which rely on 'hard fail' behaviour should be |
| 11514 | // skipped. |
| 11515 | static bool SystemSupportsHardFailRevocationChecking() { |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 11516 | if (UsingBuiltinCertVerifier()) |
| 11517 | return true; |
| 11518 | #if defined(OS_WIN) || defined(USE_NSS_CERTS) |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11519 | return true; |
| 11520 | #else |
| 11521 | return false; |
| 11522 | #endif |
| 11523 | } |
| 11524 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11525 | // SystemUsesChromiumEVMetadata returns true iff the current operating system |
| 11526 | // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then |
| 11527 | // several tests are effected because our testing EV certificate won't be |
| 11528 | // recognised as EV. |
| 11529 | static bool SystemUsesChromiumEVMetadata() { |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 11530 | if (UsingBuiltinCertVerifier()) |
| 11531 | return true; |
Eric Roman | 5f8d9d2 | 2017-10-17 02:32:52 | [diff] [blame] | 11532 | #if defined(PLATFORM_USES_CHROMIUM_EV_METADATA) |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 11533 | return true; |
Eric Roman | 5f8d9d2 | 2017-10-17 02:32:52 | [diff] [blame] | 11534 | #else |
| 11535 | return false; |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 11536 | #endif |
| 11537 | } |
| 11538 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11539 | // Returns the expected CertStatus for tests that expect an online revocation |
| 11540 | // check failure as a result of checking a test EV cert, which will not |
| 11541 | // actually trigger an online revocation check on some platforms. |
| 11542 | static CertStatus ExpectedCertStatusForFailedOnlineEVRevocationCheck() { |
| 11543 | if (SystemUsesChromiumEVMetadata()) { |
| 11544 | return ExpectedCertStatusForFailedOnlineRevocationCheck(); |
| 11545 | } else { |
| 11546 | // If SystemUsesChromiumEVMetadata is false, revocation checking will not |
| 11547 | // be enabled, and thus there will not be a revocation check to fail. |
| 11548 | return 0u; |
| 11549 | } |
| 11550 | } |
| 11551 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11552 | static bool SystemSupportsOCSP() { |
Sergey Ulanov | ee7c8db | 2017-12-08 00:18:39 | [diff] [blame] | 11553 | #if defined(OS_ANDROID) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11554 | // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. |
| 11555 | return false; |
| 11556 | #else |
| 11557 | return true; |
| 11558 | #endif |
| 11559 | } |
| 11560 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11561 | static bool SystemSupportsOCSPStapling() { |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 11562 | if (UsingBuiltinCertVerifier()) |
| 11563 | return true; |
Matt Mueller | 7f60dc4 | 2019-04-26 22:47:10 | [diff] [blame] | 11564 | #if defined(OS_ANDROID) |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11565 | return false; |
Matt Mueller | 7f60dc4 | 2019-04-26 22:47:10 | [diff] [blame] | 11566 | #elif defined(OS_MACOSX) |
| 11567 | // The SecTrustSetOCSPResponse function exists since macOS 10.9+, but does |
| 11568 | // not actually do anything until 10.12. |
| 11569 | if (base::mac::IsAtLeastOS10_12()) |
| 11570 | return true; |
| 11571 | return false; |
| 11572 | #else |
| 11573 | return true; |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11574 | #endif |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11575 | } |
| 11576 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11577 | TEST_F(HTTPSOCSPTest, Valid) { |
| 11578 | if (!SystemSupportsOCSP()) { |
| 11579 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11580 | return; |
| 11581 | } |
| 11582 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11583 | SpawnedTestServer::SSLOptions ssl_options( |
| 11584 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11585 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11586 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11587 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11588 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11589 | |
| 11590 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11591 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11592 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11593 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11594 | |
| 11595 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11596 | } |
| 11597 | |
| 11598 | TEST_F(HTTPSOCSPTest, Revoked) { |
| 11599 | if (!SystemSupportsOCSP()) { |
| 11600 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11601 | return; |
| 11602 | } |
| 11603 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11604 | SpawnedTestServer::SSLOptions ssl_options( |
| 11605 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11606 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11607 | |
| 11608 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11609 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11610 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11611 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11612 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11613 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11614 | } |
| 11615 | |
| 11616 | TEST_F(HTTPSOCSPTest, Invalid) { |
| 11617 | if (!SystemSupportsOCSP()) { |
| 11618 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11619 | return; |
| 11620 | } |
| 11621 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11622 | SpawnedTestServer::SSLOptions ssl_options( |
| 11623 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11624 | ssl_options.ocsp_status = |
| 11625 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11626 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11627 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11628 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11629 | |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 11630 | if (UsingBuiltinCertVerifier()) { |
| 11631 | // TODO(649017): This test uses soft-fail revocation checking, but returns |
| 11632 | // an invalid OCSP response (can't parse). CertVerifyProcBuiltin currently |
| 11633 | // doesn't consider this a candidate for soft-fail (only considers |
| 11634 | // network-level failures as skippable). |
| 11635 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 11636 | cert_status & CERT_STATUS_UNABLE_TO_CHECK_REVOCATION); |
| 11637 | } else { |
| 11638 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 11639 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11640 | } |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11641 | |
| 11642 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 11643 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11644 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11645 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11646 | |
Matt Mueller | 5339d86 | 2018-05-02 18:53:57 | [diff] [blame] | 11647 | TEST_F(HTTPSOCSPTest, IntermediateValid) { |
| 11648 | if (!SystemSupportsOCSP()) { |
| 11649 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11650 | return; |
| 11651 | } |
| 11652 | |
| 11653 | SpawnedTestServer::SSLOptions ssl_options( |
| 11654 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11655 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11656 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11657 | |
| 11658 | CertStatus cert_status; |
| 11659 | DoConnection(ssl_options, &cert_status); |
| 11660 | |
| 11661 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11662 | |
| 11663 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11664 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11665 | |
| 11666 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11667 | } |
| 11668 | |
| 11669 | TEST_F(HTTPSOCSPTest, IntermediateResponseOldButStillValid) { |
| 11670 | if (!SystemSupportsOCSP()) { |
| 11671 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11672 | return; |
| 11673 | } |
| 11674 | |
| 11675 | SpawnedTestServer::SSLOptions ssl_options( |
| 11676 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11677 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11678 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11679 | // Use an OCSP response for the intermediate that would be too old for a leaf |
| 11680 | // cert, but is still valid for an intermediate. |
| 11681 | ssl_options.ocsp_intermediate_date = |
| 11682 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG; |
| 11683 | |
| 11684 | CertStatus cert_status; |
| 11685 | DoConnection(ssl_options, &cert_status); |
| 11686 | |
| 11687 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11688 | |
| 11689 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11690 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11691 | |
| 11692 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11693 | } |
| 11694 | |
| 11695 | TEST_F(HTTPSOCSPTest, IntermediateResponseTooOld) { |
| 11696 | if (!SystemSupportsOCSP()) { |
| 11697 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11698 | return; |
| 11699 | } |
| 11700 | |
| 11701 | SpawnedTestServer::SSLOptions ssl_options( |
| 11702 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11703 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11704 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11705 | ssl_options.ocsp_intermediate_date = |
| 11706 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONGER; |
| 11707 | |
| 11708 | CertStatus cert_status; |
| 11709 | DoConnection(ssl_options, &cert_status); |
| 11710 | |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 11711 | if (UsingBuiltinCertVerifier()) { |
| 11712 | // The builtin verifier enforces the baseline requirements for max age of an |
| 11713 | // intermediate's OCSP response. |
| 11714 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 11715 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11716 | EXPECT_EQ(0u, cert_status & CERT_STATUS_IS_EV); |
| 11717 | } else { |
| 11718 | // The platform verifiers are more lenient. |
| 11719 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11720 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11721 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11722 | } |
Matt Mueller | 5339d86 | 2018-05-02 18:53:57 | [diff] [blame] | 11723 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11724 | } |
| 11725 | |
| 11726 | TEST_F(HTTPSOCSPTest, IntermediateRevoked) { |
| 11727 | if (!SystemSupportsOCSP()) { |
| 11728 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11729 | return; |
| 11730 | } |
| 11731 | |
| 11732 | SpawnedTestServer::SSLOptions ssl_options( |
| 11733 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11734 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11735 | ssl_options.ocsp_intermediate_status = |
| 11736 | SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 11737 | |
| 11738 | CertStatus cert_status; |
| 11739 | DoConnection(ssl_options, &cert_status); |
| 11740 | |
| 11741 | #if defined(OS_WIN) |
| 11742 | // TODO(mattm): why does CertVerifyProcWin accept this? |
| 11743 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11744 | #else |
| 11745 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11746 | #endif |
| 11747 | EXPECT_EQ(0u, cert_status & CERT_STATUS_IS_EV); |
| 11748 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11749 | } |
| 11750 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11751 | TEST_F(HTTPSOCSPTest, ValidStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11752 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11753 | LOG(WARNING) |
| 11754 | << "Skipping test because system doesn't support OCSP stapling"; |
| 11755 | return; |
| 11756 | } |
| 11757 | |
| 11758 | SpawnedTestServer::SSLOptions ssl_options( |
| 11759 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11760 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11761 | ssl_options.staple_ocsp_response = true; |
| 11762 | ssl_options.ocsp_server_unavailable = true; |
| 11763 | |
| 11764 | CertStatus cert_status; |
| 11765 | DoConnection(ssl_options, &cert_status); |
| 11766 | |
| 11767 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11768 | |
| 11769 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11770 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11771 | |
| 11772 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11773 | } |
| 11774 | |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 11775 | // Disabled on NSS ports. See https://crbug.com/431716. |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 11776 | #if defined(USE_NSS_CERTS) |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 11777 | #define MAYBE_RevokedStapled DISABLED_RevokedStapled |
| 11778 | #else |
| 11779 | #define MAYBE_RevokedStapled RevokedStapled |
| 11780 | #endif |
| 11781 | TEST_F(HTTPSOCSPTest, MAYBE_RevokedStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11782 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11783 | LOG(WARNING) |
| 11784 | << "Skipping test because system doesn't support OCSP stapling"; |
| 11785 | return; |
| 11786 | } |
| 11787 | |
| 11788 | SpawnedTestServer::SSLOptions ssl_options( |
| 11789 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11790 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 11791 | ssl_options.staple_ocsp_response = true; |
| 11792 | ssl_options.ocsp_server_unavailable = true; |
| 11793 | |
| 11794 | CertStatus cert_status; |
| 11795 | DoConnection(ssl_options, &cert_status); |
| 11796 | |
| 11797 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11798 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11799 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11800 | } |
| 11801 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11802 | static const struct OCSPVerifyTestData { |
| 11803 | std::vector<SpawnedTestServer::SSLOptions::OCSPSingleResponse> ocsp_responses; |
| 11804 | SpawnedTestServer::SSLOptions::OCSPProduced ocsp_produced; |
| 11805 | OCSPVerifyResult::ResponseStatus response_status; |
| 11806 | bool has_revocation_status; |
| 11807 | OCSPRevocationStatus cert_status; |
| 11808 | } kOCSPVerifyData[] = { |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11809 | // 0 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11810 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11811 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11812 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11813 | OCSPVerifyResult::PROVIDED, |
| 11814 | true, |
| 11815 | OCSPRevocationStatus::GOOD}, |
| 11816 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11817 | // 1 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11818 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11819 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 11820 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11821 | OCSPVerifyResult::INVALID_DATE, |
| 11822 | false, |
| 11823 | OCSPRevocationStatus::UNKNOWN}, |
| 11824 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11825 | // 2 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11826 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11827 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11828 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11829 | OCSPVerifyResult::INVALID_DATE, |
| 11830 | false, |
| 11831 | OCSPRevocationStatus::UNKNOWN}, |
| 11832 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11833 | // 3 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11834 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11835 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11836 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11837 | OCSPVerifyResult::INVALID_DATE, |
| 11838 | false, |
| 11839 | OCSPRevocationStatus::UNKNOWN}, |
| 11840 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11841 | // 4 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11842 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11843 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11844 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11845 | OCSPVerifyResult::INVALID_DATE, |
| 11846 | false, |
| 11847 | OCSPRevocationStatus::UNKNOWN}, |
| 11848 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11849 | // 5 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11850 | {{{SpawnedTestServer::SSLOptions::OCSP_TRY_LATER, |
| 11851 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11852 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11853 | OCSPVerifyResult::ERROR_RESPONSE, |
| 11854 | false, |
| 11855 | OCSPRevocationStatus::UNKNOWN}, |
| 11856 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11857 | // 6 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11858 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE, |
| 11859 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11860 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11861 | OCSPVerifyResult::PARSE_RESPONSE_ERROR, |
| 11862 | false, |
| 11863 | OCSPRevocationStatus::UNKNOWN}, |
| 11864 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11865 | // 7 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11866 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE_DATA, |
| 11867 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11868 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11869 | OCSPVerifyResult::PARSE_RESPONSE_DATA_ERROR, |
| 11870 | false, |
| 11871 | OCSPRevocationStatus::UNKNOWN}, |
| 11872 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11873 | // 8 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11874 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11875 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11876 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11877 | OCSPVerifyResult::INVALID_DATE, |
| 11878 | false, |
| 11879 | OCSPRevocationStatus::UNKNOWN}, |
| 11880 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11881 | // 9 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11882 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11883 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11884 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11885 | OCSPVerifyResult::PROVIDED, |
| 11886 | true, |
| 11887 | OCSPRevocationStatus::UNKNOWN}, |
| 11888 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11889 | // 10 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11890 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11891 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 11892 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11893 | OCSPVerifyResult::INVALID_DATE, |
| 11894 | false, |
| 11895 | OCSPRevocationStatus::UNKNOWN}, |
| 11896 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11897 | // 11 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11898 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11899 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11900 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11901 | OCSPVerifyResult::INVALID_DATE, |
| 11902 | false, |
| 11903 | OCSPRevocationStatus::UNKNOWN}, |
| 11904 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11905 | // 12 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11906 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11907 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11908 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_BEFORE_CERT, |
| 11909 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11910 | false, |
| 11911 | OCSPRevocationStatus::UNKNOWN}, |
| 11912 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11913 | // 13 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11914 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11915 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11916 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 11917 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11918 | false, |
| 11919 | OCSPRevocationStatus::UNKNOWN}, |
| 11920 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11921 | // 14 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11922 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11923 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11924 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 11925 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11926 | false, |
| 11927 | OCSPRevocationStatus::UNKNOWN}, |
| 11928 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11929 | // 15 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11930 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11931 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11932 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11933 | OCSPVerifyResult::PROVIDED, |
| 11934 | true, |
| 11935 | OCSPRevocationStatus::GOOD}, |
| 11936 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11937 | // 16 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11938 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11939 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 11940 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11941 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11942 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11943 | OCSPVerifyResult::PROVIDED, |
| 11944 | true, |
| 11945 | OCSPRevocationStatus::GOOD}, |
| 11946 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11947 | // 17 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11948 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11949 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 11950 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11951 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11952 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11953 | OCSPVerifyResult::PROVIDED, |
| 11954 | true, |
| 11955 | OCSPRevocationStatus::GOOD}, |
| 11956 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11957 | // 18 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11958 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11959 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 11960 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11961 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11962 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11963 | OCSPVerifyResult::PROVIDED, |
| 11964 | true, |
| 11965 | OCSPRevocationStatus::GOOD}, |
| 11966 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11967 | // 19 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11968 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11969 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 11970 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11971 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 11972 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11973 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11974 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11975 | OCSPVerifyResult::INVALID_DATE, |
| 11976 | false, |
| 11977 | OCSPRevocationStatus::UNKNOWN}, |
| 11978 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11979 | // 20 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11980 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11981 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11982 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11983 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11984 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11985 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11986 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11987 | OCSPVerifyResult::PROVIDED, |
| 11988 | true, |
| 11989 | OCSPRevocationStatus::REVOKED}, |
| 11990 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11991 | // 21 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11992 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11993 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11994 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11995 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11996 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11997 | OCSPVerifyResult::PROVIDED, |
| 11998 | true, |
| 11999 | OCSPRevocationStatus::UNKNOWN}, |
| 12000 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 12001 | // 22 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12002 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 12003 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 12004 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 12005 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 12006 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 12007 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 12008 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 12009 | OCSPVerifyResult::PROVIDED, |
| 12010 | true, |
| 12011 | OCSPRevocationStatus::UNKNOWN}, |
| 12012 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 12013 | // 23 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12014 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 12015 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 12016 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 12017 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 12018 | false, |
| 12019 | OCSPRevocationStatus::UNKNOWN}, |
| 12020 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 12021 | // 24 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12022 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 12023 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 12024 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 12025 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 12026 | false, |
| 12027 | OCSPRevocationStatus::UNKNOWN}, |
| 12028 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 12029 | // These tests fail when using NSS for certificate verification, as NSS fails |
| 12030 | // and doesn't return the partial path. As a result the OCSP checks being done |
| 12031 | // at the CertVerifyProc layer cannot access the issuer certificate. |
| 12032 | #if !defined(USE_NSS_CERTS) |
| 12033 | // 25 |
| 12034 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 12035 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 12036 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 12037 | OCSPVerifyResult::PROVIDED, |
| 12038 | true, |
| 12039 | OCSPRevocationStatus::REVOKED}, |
| 12040 | |
| 12041 | // 26 |
| 12042 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 12043 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 12044 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 12045 | OCSPVerifyResult::INVALID_DATE, |
| 12046 | false, |
| 12047 | OCSPRevocationStatus::UNKNOWN}, |
| 12048 | |
| 12049 | // 27 |
| 12050 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 12051 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 12052 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 12053 | OCSPVerifyResult::INVALID_DATE, |
| 12054 | false, |
| 12055 | OCSPRevocationStatus::UNKNOWN}, |
| 12056 | #endif |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12057 | }; |
| 12058 | |
| 12059 | class HTTPSOCSPVerifyTest |
| 12060 | : public HTTPSOCSPTest, |
| 12061 | public testing::WithParamInterface<OCSPVerifyTestData> {}; |
| 12062 | |
Christos Froussios | 09e6ac4 | 2019-04-05 14:08:20 | [diff] [blame] | 12063 | // TODO(crbug.com/949958): The test is flaky on Mac |
| 12064 | #if defined(OS_MACOSX) |
| 12065 | #define MAYBE_VerifyResult DISABLED_VerifyResult |
| 12066 | #else |
| 12067 | #define MAYBE_VerifyResult VerifyResult |
| 12068 | #endif |
| 12069 | TEST_P(HTTPSOCSPVerifyTest, MAYBE_VerifyResult) { |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12070 | SpawnedTestServer::SSLOptions ssl_options( |
| 12071 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12072 | OCSPVerifyTestData test = GetParam(); |
| 12073 | |
| 12074 | ssl_options.ocsp_responses = test.ocsp_responses; |
| 12075 | ssl_options.ocsp_produced = test.ocsp_produced; |
| 12076 | ssl_options.staple_ocsp_response = true; |
| 12077 | |
| 12078 | SSLInfo ssl_info; |
| 12079 | OCSPErrorTestDelegate delegate; |
| 12080 | ASSERT_NO_FATAL_FAILURE( |
| 12081 | DoConnectionWithDelegate(ssl_options, &delegate, &ssl_info)); |
| 12082 | |
| 12083 | // The SSLInfo must be extracted from |delegate| on error, due to how |
| 12084 | // URLRequest caches certificate errors. |
| 12085 | if (delegate.have_certificate_errors()) { |
| 12086 | ASSERT_TRUE(delegate.on_ssl_certificate_error_called()); |
| 12087 | ssl_info = delegate.ssl_info(); |
| 12088 | } |
| 12089 | |
| 12090 | EXPECT_EQ(test.response_status, ssl_info.ocsp_result.response_status); |
| 12091 | |
| 12092 | if (test.has_revocation_status) |
| 12093 | EXPECT_EQ(test.cert_status, ssl_info.ocsp_result.revocation_status); |
| 12094 | } |
| 12095 | |
Victor Costan | 8fb98f6f | 2019-02-01 17:08:29 | [diff] [blame] | 12096 | INSTANTIATE_TEST_SUITE_P(OCSPVerify, |
| 12097 | HTTPSOCSPVerifyTest, |
| 12098 | testing::ValuesIn(kOCSPVerifyData)); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12099 | |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 12100 | class HTTPSAIATest : public HTTPSOCSPTest { |
| 12101 | public: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 12102 | CertVerifier::Config GetCertVerifierConfig() override { |
| 12103 | CertVerifier::Config config; |
| 12104 | return config; |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 12105 | } |
| 12106 | }; |
| 12107 | |
| 12108 | TEST_F(HTTPSAIATest, AIAFetching) { |
| 12109 | SpawnedTestServer::SSLOptions ssl_options( |
| 12110 | SpawnedTestServer::SSLOptions::CERT_AUTO_AIA_INTERMEDIATE); |
| 12111 | SpawnedTestServer test_server( |
| 12112 | SpawnedTestServer::TYPE_HTTPS, ssl_options, |
| 12113 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 12114 | ASSERT_TRUE(test_server.Start()); |
| 12115 | |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12116 | // Unmark the certificate's OID as EV, which will disable revocation |
| 12117 | // checking. |
| 12118 | ev_test_policy_.reset(); |
| 12119 | |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 12120 | TestDelegate d; |
| 12121 | d.set_allow_certificate_errors(true); |
| 12122 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12123 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 12124 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 12125 | |
| 12126 | r->Start(); |
| 12127 | EXPECT_TRUE(r->is_pending()); |
| 12128 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12129 | d.RunUntilComplete(); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 12130 | |
| 12131 | EXPECT_EQ(1, d.response_started_count()); |
| 12132 | |
| 12133 | CertStatus cert_status = r->ssl_info().cert_status; |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 12134 | EXPECT_EQ(OK, d.request_status()); |
| 12135 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12136 | ASSERT_TRUE(r->ssl_info().cert); |
| 12137 | EXPECT_EQ(2u, r->ssl_info().cert->intermediate_buffers().size()); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 12138 | ASSERT_TRUE(r->ssl_info().unverified_cert); |
Matt Mueller | a419327 | 2017-12-07 00:23:34 | [diff] [blame] | 12139 | EXPECT_EQ(0u, r->ssl_info().unverified_cert->intermediate_buffers().size()); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 12140 | } |
| 12141 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 12142 | class HTTPSHardFailTest : public HTTPSOCSPTest { |
| 12143 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 12144 | CertVerifier::Config GetCertVerifierConfig() override { |
| 12145 | CertVerifier::Config config; |
| 12146 | config.require_rev_checking_local_anchors = true; |
| 12147 | return config; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 12148 | } |
| 12149 | }; |
| 12150 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 12151 | TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) { |
| 12152 | if (!SystemSupportsOCSP()) { |
| 12153 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12154 | return; |
| 12155 | } |
| 12156 | |
| 12157 | if (!SystemSupportsHardFailRevocationChecking()) { |
| 12158 | LOG(WARNING) << "Skipping test because system doesn't support hard fail " |
| 12159 | << "revocation checking"; |
| 12160 | return; |
| 12161 | } |
| 12162 | |
| 12163 | SpawnedTestServer::SSLOptions ssl_options( |
| 12164 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12165 | ssl_options.ocsp_status = |
| 12166 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 12167 | |
| 12168 | CertStatus cert_status; |
| 12169 | DoConnection(ssl_options, &cert_status); |
| 12170 | |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 12171 | if (UsingBuiltinCertVerifier()) { |
| 12172 | // TODO(crbug.com/649017): Should we consider invalid response as |
| 12173 | // affirmatively revoked? |
| 12174 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 12175 | cert_status & CERT_STATUS_UNABLE_TO_CHECK_REVOCATION); |
| 12176 | } else { |
| 12177 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_REVOKED); |
| 12178 | } |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 12179 | |
| 12180 | // Without a positive OCSP response, we shouldn't show the EV status. |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 12181 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 12182 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 12183 | } |
| 12184 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12185 | class HTTPSEVCRLSetTest : public HTTPSOCSPTest { |
| 12186 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 12187 | CertVerifier::Config GetCertVerifierConfig() override { |
| 12188 | CertVerifier::Config config; |
| 12189 | return config; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12190 | } |
| 12191 | }; |
| 12192 | |
| 12193 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) { |
| 12194 | if (!SystemSupportsOCSP()) { |
| 12195 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12196 | return; |
| 12197 | } |
| 12198 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 12199 | SpawnedTestServer::SSLOptions ssl_options( |
| 12200 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12201 | ssl_options.ocsp_status = |
| 12202 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12203 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 12204 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 12205 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12206 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 12207 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12208 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 12209 | |
| 12210 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 12211 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 12212 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12213 | } |
| 12214 | |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 12215 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) { |
| 12216 | if (!SystemSupportsOCSP()) { |
| 12217 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12218 | return; |
| 12219 | } |
| 12220 | |
| 12221 | SpawnedTestServer::SSLOptions ssl_options( |
| 12222 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12223 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 12224 | |
| 12225 | CertStatus cert_status; |
| 12226 | DoConnection(ssl_options, &cert_status); |
| 12227 | |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 12228 | // Currently only works for Windows and OS X. When using NSS, it's not |
| 12229 | // possible to determine whether the check failed because of actual |
| 12230 | // revocation or because there was an OCSP failure. |
| 12231 | if (UsingBuiltinCertVerifier()) { |
| 12232 | // TODO(https://crbug.com/410574): Handle this in builtin verifier too? |
| 12233 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12234 | } else { |
mattm | 1a282f5 | 2016-11-10 21:49:42 | [diff] [blame] | 12235 | #if defined(OS_WIN) || defined(OS_MACOSX) |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 12236 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 12237 | #else |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 12238 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 12239 | #endif |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 12240 | } |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 12241 | |
| 12242 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 12243 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 12244 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 12245 | } |
| 12246 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12247 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) { |
| 12248 | if (!SystemSupportsOCSP()) { |
| 12249 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12250 | return; |
| 12251 | } |
| 12252 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 12253 | SpawnedTestServer::SSLOptions ssl_options( |
| 12254 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12255 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12256 | |
| 12257 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 12258 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12259 | |
| 12260 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12261 | |
| 12262 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 12263 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 12264 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 12265 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12266 | } |
| 12267 | |
| 12268 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) { |
| 12269 | if (!SystemSupportsOCSP()) { |
| 12270 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12271 | return; |
| 12272 | } |
| 12273 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 12274 | SpawnedTestServer::SSLOptions ssl_options( |
| 12275 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12276 | ssl_options.ocsp_status = |
| 12277 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12278 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12279 | cert_verifier_config.crl_set = CRLSet::ExpiredCRLSetForTesting(); |
| 12280 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12281 | |
| 12282 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 12283 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12284 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 12285 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12286 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 12287 | |
| 12288 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 12289 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 12290 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12291 | } |
| 12292 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12293 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) { |
| 12294 | if (!SystemSupportsOCSP()) { |
| 12295 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12296 | return; |
| 12297 | } |
| 12298 | |
| 12299 | SpawnedTestServer::SSLOptions ssl_options( |
| 12300 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12301 | ssl_options.ocsp_status = |
| 12302 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12303 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12304 | cert_verifier_config.crl_set = |
| 12305 | CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "", "", {}); |
| 12306 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12307 | |
| 12308 | CertStatus cert_status; |
| 12309 | DoConnection(ssl_options, &cert_status); |
| 12310 | |
| 12311 | // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a |
| 12312 | // revocation check for EV. |
| 12313 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12314 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 12315 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 12316 | EXPECT_FALSE( |
| 12317 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 12318 | } |
| 12319 | |
| 12320 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) { |
| 12321 | if (!SystemSupportsOCSP()) { |
| 12322 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12323 | return; |
| 12324 | } |
| 12325 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 12326 | SpawnedTestServer::SSLOptions ssl_options( |
| 12327 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12328 | ssl_options.ocsp_status = |
| 12329 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12330 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12331 | cert_verifier_config.crl_set = CRLSet::EmptyCRLSetForTesting(); |
| 12332 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12333 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12334 | CertStatus cert_status = 0; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 12335 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12336 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12337 | // Even with a fresh CRLSet, we should still do online revocation checks when |
| 12338 | // the certificate chain isn't covered by the CRLSet, which it isn't in this |
| 12339 | // test. |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 12340 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12341 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12342 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12343 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12344 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12345 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12346 | } |
| 12347 | |
| 12348 | class HTTPSCRLSetTest : public HTTPSOCSPTest { |
| 12349 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 12350 | CertVerifier::Config GetCertVerifierConfig() override { |
| 12351 | CertVerifier::Config config; |
| 12352 | return config; |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12353 | } |
| 12354 | |
| 12355 | void SetUp() override { |
| 12356 | HTTPSOCSPTest::SetUp(); |
| 12357 | |
| 12358 | // Unmark the certificate's OID as EV, which should disable revocation |
| 12359 | // checking (as per the user preference). |
| 12360 | ev_test_policy_.reset(); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12361 | } |
| 12362 | }; |
| 12363 | |
| 12364 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 12365 | SpawnedTestServer::SSLOptions ssl_options( |
| 12366 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12367 | ssl_options.ocsp_status = |
| 12368 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12369 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12370 | cert_verifier_config.crl_set = CRLSet::ExpiredCRLSetForTesting(); |
| 12371 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12372 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 12373 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 12374 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12375 | |
| 12376 | // If we're not trying EV verification then, even if the CRLSet has expired, |
| 12377 | // we don't fall back to online revocation checks. |
| 12378 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12379 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 12380 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 12381 | } |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12382 | |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12383 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSetAndRevoked) { |
| 12384 | // Test that when online revocation checking is disabled, and the leaf |
| 12385 | // certificate is not EV, that no revocation checking actually happens. |
| 12386 | if (!SystemSupportsOCSP()) { |
| 12387 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12388 | return; |
| 12389 | } |
| 12390 | |
| 12391 | SpawnedTestServer::SSLOptions ssl_options( |
| 12392 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12393 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12394 | |
| 12395 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12396 | cert_verifier_config.crl_set = CRLSet::ExpiredCRLSetForTesting(); |
| 12397 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12398 | |
| 12399 | CertStatus cert_status; |
| 12400 | DoConnection(ssl_options, &cert_status); |
| 12401 | |
| 12402 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12403 | |
| 12404 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 12405 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 12406 | } |
| 12407 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12408 | TEST_F(HTTPSCRLSetTest, CRLSetRevoked) { |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 12409 | #if defined(OS_ANDROID) |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12410 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 12411 | return; |
| 12412 | #endif |
| 12413 | |
| 12414 | SpawnedTestServer::SSLOptions ssl_options( |
| 12415 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12416 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 12417 | ssl_options.cert_serial = 10; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12418 | |
| 12419 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12420 | cert_verifier_config.crl_set = |
| 12421 | CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "\x0a", "", {}); |
| 12422 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12423 | |
| 12424 | CertStatus cert_status = 0; |
| 12425 | DoConnection(ssl_options, &cert_status); |
| 12426 | |
| 12427 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 12428 | // reflected without online revocation checking. |
| 12429 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12430 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12431 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12432 | } |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 12433 | |
| 12434 | TEST_F(HTTPSCRLSetTest, CRLSetRevokedBySubject) { |
| 12435 | #if defined(OS_ANDROID) |
| 12436 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 12437 | return; |
| 12438 | #endif |
| 12439 | |
| 12440 | SpawnedTestServer::SSLOptions ssl_options( |
| 12441 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12442 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 12443 | static const char kCommonName[] = "Test CN"; |
| 12444 | ssl_options.cert_common_name = kCommonName; |
| 12445 | |
| 12446 | { |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12447 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12448 | cert_verifier_config.crl_set = |
| 12449 | CRLSet::ForTesting(false, nullptr, "", kCommonName, {}); |
| 12450 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 12451 | |
| 12452 | CertStatus cert_status = 0; |
| 12453 | DoConnection(ssl_options, &cert_status); |
| 12454 | |
| 12455 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 12456 | // reflected without online revocation checking. |
| 12457 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12458 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12459 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 12460 | } |
| 12461 | |
| 12462 | const uint8_t kTestServerSPKISHA256[32] = { |
| 12463 | 0xb3, 0x91, 0xac, 0x73, 0x32, 0x54, 0x7f, 0x7b, 0x8a, 0x62, 0x77, |
| 12464 | 0x73, 0x1d, 0x45, 0x7b, 0x23, 0x46, 0x69, 0xef, 0x6f, 0x05, 0x3d, |
| 12465 | 0x07, 0x22, 0x15, 0x18, 0xd6, 0x10, 0x8b, 0xa1, 0x49, 0x33, |
| 12466 | }; |
| 12467 | const std::string spki_hash( |
| 12468 | reinterpret_cast<const char*>(kTestServerSPKISHA256), |
| 12469 | sizeof(kTestServerSPKISHA256)); |
| 12470 | |
| 12471 | { |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12472 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12473 | cert_verifier_config.crl_set = |
| 12474 | CRLSet::ForTesting(false, nullptr, "", kCommonName, {spki_hash}); |
| 12475 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 12476 | |
| 12477 | CertStatus cert_status = 0; |
| 12478 | DoConnection(ssl_options, &cert_status); |
| 12479 | |
| 12480 | // When the correct SPKI hash is specified, the connection should succeed |
| 12481 | // even though the subject is listed in the CRLSet. |
| 12482 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12483 | } |
| 12484 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 12485 | #endif // !defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 12486 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 12487 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 12488 | !defined(OS_FUCHSIA) |
| 12489 | // FTP uses a second TCP connection with the port number allocated dynamically |
| 12490 | // on the server side, so it would be hard to make RemoteTestServer proxy FTP |
| 12491 | // connections reliably. FTP tests are disabled on platforms that use |
| 12492 | // RemoteTestServer. See http://crbug.com/495220 |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 12493 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12494 | public: |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 12495 | URLRequestTestFTP() |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 12496 | : ftp_test_server_(SpawnedTestServer::TYPE_FTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12497 | base::FilePath(kTestFilePath)) { |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12498 | // Can't use |default_context_|'s HostResolver to set up the |
| 12499 | // FTPTransactionFactory because it hasn't been created yet. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12500 | default_context().set_host_resolver(&host_resolver_); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12501 | } |
| 12502 | |
| 12503 | // URLRequestTest interface: |
| 12504 | void SetUpFactory() override { |
| 12505 | // Add FTP support to the default URLRequestContext. |
| 12506 | job_factory_impl_->SetProtocolHandler( |
Emily Stark | 0122798 | 2019-06-06 18:45:56 | [diff] [blame] | 12507 | "ftp", FtpProtocolHandler::Create(&host_resolver_, &ftp_auth_cache_)); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12508 | } |
| 12509 | |
| 12510 | std::string GetTestFileContents() { |
| 12511 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 12512 | EXPECT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &path)); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12513 | path = path.Append(kTestFilePath); |
| 12514 | path = path.AppendASCII(kFtpTestFile); |
| 12515 | std::string contents; |
| 12516 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
| 12517 | return contents; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12518 | } |
| 12519 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 12520 | protected: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12521 | MockHostResolver host_resolver_; |
Emily Stark | 0122798 | 2019-06-06 18:45:56 | [diff] [blame] | 12522 | FtpAuthCache ftp_auth_cache_; |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12523 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12524 | SpawnedTestServer ftp_test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 12525 | }; |
| 12526 | |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12527 | // Make sure an FTP request using an unsafe ports fails. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 12528 | TEST_F(URLRequestTestFTP, UnsafePort) { |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12529 | GURL url("ftp://127.0.0.1:7"); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12530 | |
| 12531 | TestDelegate d; |
| 12532 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12533 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12534 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12535 | r->Start(); |
| 12536 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12537 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12538 | d.RunUntilComplete(); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12539 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12540 | EXPECT_FALSE(r->is_pending()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 12541 | EXPECT_EQ(ERR_UNSAFE_PORT, d.request_status()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12542 | } |
| 12543 | } |
| 12544 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12545 | TEST_F(URLRequestTestFTP, FTPDirectoryListing) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12546 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12547 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12548 | TestDelegate d; |
| 12549 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12550 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12551 | ftp_test_server_.GetURL("/"), DEFAULT_PRIORITY, &d, |
| 12552 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12553 | r->Start(); |
| 12554 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12555 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12556 | d.RunUntilComplete(); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12557 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12558 | EXPECT_FALSE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12559 | EXPECT_EQ(1, d.response_started_count()); |
| 12560 | EXPECT_FALSE(d.received_data_before_response()); |
| 12561 | EXPECT_LT(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12562 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12563 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12564 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12565 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12566 | } |
| 12567 | } |
| 12568 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12569 | TEST_F(URLRequestTestFTP, FTPGetTestAnonymous) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12570 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12571 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12572 | TestDelegate d; |
| 12573 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12574 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12575 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, &d, |
| 12576 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12577 | r->Start(); |
| 12578 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12579 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12580 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12581 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12582 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12583 | EXPECT_EQ(1, d.response_started_count()); |
| 12584 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12585 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12586 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12587 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12588 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12589 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12590 | } |
| 12591 | } |
| 12592 | |
Mike West | 80462a1 | 2018-11-27 16:05:06 | [diff] [blame] | 12593 | TEST_F(URLRequestTestFTP, FTPMimeType) { |
| 12594 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 12595 | |
| 12596 | struct { |
| 12597 | const char* path; |
| 12598 | const char* mime; |
| 12599 | } test_cases[] = { |
| 12600 | {"/", "text/vnd.chromium.ftp-dir"}, |
| 12601 | {kFtpTestFile, "application/octet-stream"}, |
| 12602 | }; |
| 12603 | |
| 12604 | for (const auto test : test_cases) { |
| 12605 | TestDelegate d; |
| 12606 | |
| 12607 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 12608 | ftp_test_server_.GetURL(test.path), DEFAULT_PRIORITY, &d, |
| 12609 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12610 | r->Start(); |
| 12611 | EXPECT_TRUE(r->is_pending()); |
| 12612 | |
| 12613 | d.RunUntilComplete(); |
| 12614 | |
| 12615 | std::string mime; |
| 12616 | r->GetMimeType(&mime); |
| 12617 | EXPECT_EQ(test.mime, mime); |
| 12618 | } |
| 12619 | } |
| 12620 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12621 | TEST_F(URLRequestTestFTP, FTPGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12622 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12623 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12624 | TestDelegate d; |
| 12625 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12626 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12627 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12628 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12629 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12630 | r->Start(); |
| 12631 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12632 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12633 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12634 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12635 | EXPECT_FALSE(r->is_pending()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12636 | EXPECT_EQ(1, d.response_started_count()); |
| 12637 | EXPECT_FALSE(d.received_data_before_response()); |
| 12638 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12639 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12640 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12641 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12642 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 12643 | |
| 12644 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12645 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 12646 | TestLoadTimingNoHttpResponse(load_timing_info); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12647 | } |
| 12648 | } |
| 12649 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12650 | TEST_F(URLRequestTestFTP, FTPCheckWrongPassword) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12651 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12652 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12653 | TestDelegate d; |
| 12654 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12655 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12656 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12657 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12658 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12659 | r->Start(); |
| 12660 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12661 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12662 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12663 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12664 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12665 | EXPECT_EQ(1, d.response_started_count()); |
| 12666 | EXPECT_FALSE(d.received_data_before_response()); |
| 12667 | EXPECT_EQ(d.bytes_received(), 0); |
| 12668 | } |
| 12669 | } |
| 12670 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12671 | TEST_F(URLRequestTestFTP, FTPCheckWrongPasswordRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12672 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12673 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12674 | TestDelegate d; |
| 12675 | // Set correct login credentials. The delegate will be asked for them when |
| 12676 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 12677 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12678 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12679 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12680 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12681 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12682 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12683 | r->Start(); |
| 12684 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12685 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12686 | d.RunUntilComplete(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12687 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12688 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12689 | EXPECT_EQ(1, d.response_started_count()); |
| 12690 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12691 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12692 | } |
| 12693 | } |
| 12694 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12695 | TEST_F(URLRequestTestFTP, FTPCheckWrongUser) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12696 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12697 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12698 | TestDelegate d; |
| 12699 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12700 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12701 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 12702 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12703 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12704 | r->Start(); |
| 12705 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12706 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12707 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12708 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12709 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12710 | EXPECT_EQ(1, d.response_started_count()); |
| 12711 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12712 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12713 | } |
| 12714 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12715 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12716 | TEST_F(URLRequestTestFTP, FTPCheckWrongUserRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12717 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12718 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12719 | TestDelegate d; |
| 12720 | // Set correct login credentials. The delegate will be asked for them when |
| 12721 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 12722 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12723 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12724 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12725 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 12726 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12727 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12728 | r->Start(); |
| 12729 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12730 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12731 | d.RunUntilComplete(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12732 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12733 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12734 | EXPECT_EQ(1, d.response_started_count()); |
| 12735 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12736 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12737 | } |
| 12738 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12739 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12740 | TEST_F(URLRequestTestFTP, FTPCacheURLCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12741 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12742 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12743 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12744 | { |
| 12745 | // Pass correct login identity in the URL. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12746 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12747 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12748 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12749 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12750 | r->Start(); |
| 12751 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12752 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12753 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12754 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12755 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12756 | EXPECT_EQ(1, d->response_started_count()); |
| 12757 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12758 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12759 | } |
| 12760 | |
| 12761 | d.reset(new TestDelegate); |
| 12762 | { |
| 12763 | // This request should use cached identity from previous request. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12764 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12765 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 12766 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12767 | r->Start(); |
| 12768 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12769 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12770 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12771 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12772 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12773 | EXPECT_EQ(1, d->response_started_count()); |
| 12774 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12775 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12776 | } |
| 12777 | } |
| 12778 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12779 | TEST_F(URLRequestTestFTP, FTPCacheLoginBoxCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12780 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12781 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12782 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12783 | // Set correct login credentials. The delegate will be asked for them when |
| 12784 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 12785 | d->set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12786 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12787 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12788 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12789 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12790 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12791 | r->Start(); |
| 12792 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12793 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12794 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12795 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12796 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12797 | EXPECT_EQ(1, d->response_started_count()); |
| 12798 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12799 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12800 | } |
| 12801 | |
| 12802 | // Use a new delegate without explicit credentials. The cached ones should be |
| 12803 | // used. |
| 12804 | d.reset(new TestDelegate); |
| 12805 | { |
| 12806 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 12807 | // ones. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12808 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12809 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 12810 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12811 | r->Start(); |
| 12812 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12813 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12814 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12815 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12816 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12817 | EXPECT_EQ(1, d->response_started_count()); |
| 12818 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12819 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12820 | } |
| 12821 | } |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 12822 | |
| 12823 | TEST_F(URLRequestTestFTP, RawBodyBytes) { |
| 12824 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 12825 | |
| 12826 | TestDelegate d; |
| 12827 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12828 | ftp_test_server_.GetURL("simple.html"), DEFAULT_PRIORITY, &d, |
| 12829 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 12830 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12831 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 12832 | |
| 12833 | EXPECT_EQ(6, req->GetRawBodyBytes()); |
| 12834 | } |
| 12835 | |
Matt Menke | beb95e8a | 2019-07-15 06:04:09 | [diff] [blame] | 12836 | TEST_F(URLRequestTestFTP, FtpAuthCancellation) { |
Emily Stark | 6668e09 | 2019-06-20 18:36:38 | [diff] [blame] | 12837 | ftp_test_server_.set_no_anonymous_ftp_user(true); |
| 12838 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 12839 | TestDelegate d; |
| 12840 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 12841 | ftp_test_server_.GetURL("simple.html"), DEFAULT_PRIORITY, &d, |
| 12842 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12843 | req->Start(); |
| 12844 | d.RunUntilComplete(); |
| 12845 | |
| 12846 | ASSERT_TRUE(d.auth_required_called()); |
Emily Stark | 89a3247d | 2019-06-26 18:07:40 | [diff] [blame] | 12847 | EXPECT_EQ(OK, d.request_status()); |
Emily Stark | 6668e09 | 2019-06-20 18:36:38 | [diff] [blame] | 12848 | EXPECT_TRUE(req->auth_challenge_info()); |
Emily Stark | 89a3247d | 2019-06-26 18:07:40 | [diff] [blame] | 12849 | std::string mime_type; |
| 12850 | req->GetMimeType(&mime_type); |
| 12851 | EXPECT_EQ("text/plain", mime_type); |
| 12852 | EXPECT_EQ("", d.data_received()); |
| 12853 | EXPECT_EQ(-1, req->GetExpectedContentSize()); |
Emily Stark | 6668e09 | 2019-06-20 18:36:38 | [diff] [blame] | 12854 | } |
| 12855 | |
Matt Menke | f1b97e0 | 2019-05-13 23:52:41 | [diff] [blame] | 12856 | class URLRequestTestFTPOverHttpProxy : public URLRequestTestFTP { |
| 12857 | public: |
| 12858 | // Test interface: |
| 12859 | void SetUp() override { |
| 12860 | proxy_resolution_service_ = ProxyResolutionService::CreateFixed( |
| 12861 | "localhost", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 12862 | default_context_->set_proxy_resolution_service( |
| 12863 | proxy_resolution_service_.get()); |
| 12864 | URLRequestTestFTP::SetUp(); |
| 12865 | } |
| 12866 | |
| 12867 | private: |
| 12868 | std::unique_ptr<ProxyResolutionService> proxy_resolution_service_; |
| 12869 | }; |
| 12870 | |
| 12871 | // Check that FTP is not supported over an HTTP proxy. |
| 12872 | TEST_F(URLRequestTestFTPOverHttpProxy, Fails) { |
| 12873 | TestDelegate delegate; |
| 12874 | std::unique_ptr<URLRequest> request( |
| 12875 | default_context_->CreateRequest(GURL("ftp://foo.test/"), DEFAULT_PRIORITY, |
| 12876 | &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12877 | request->Start(); |
| 12878 | delegate.RunUntilComplete(); |
| 12879 | |
| 12880 | EXPECT_THAT(delegate.request_status(), IsError(ERR_NO_SUPPORTED_PROXIES)); |
| 12881 | } |
| 12882 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 12883 | #endif // !BUILDFLAG(DISABLE_FTP_SUPPORT) |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 12884 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12885 | TEST_F(URLRequestTest, NetworkAccessedClearOnDataRequest) { |
| 12886 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12887 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12888 | GURL("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12889 | |
| 12890 | EXPECT_FALSE(req->response_info().network_accessed); |
| 12891 | |
| 12892 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12893 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12894 | |
| 12895 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 12896 | EXPECT_FALSE(req->response_info().network_accessed); |
| 12897 | } |
| 12898 | |
| 12899 | TEST_F(URLRequestTest, NetworkAccessedSetOnHostResolutionFailure) { |
| 12900 | MockHostResolver host_resolver; |
| 12901 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 12902 | TestURLRequestContext context(true); |
| 12903 | context.set_network_delegate(&network_delegate); |
| 12904 | context.set_host_resolver(&host_resolver); |
| 12905 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 12906 | context.Init(); |
| 12907 | |
| 12908 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12909 | std::unique_ptr<URLRequest> req( |
| 12910 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 12911 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12912 | |
| 12913 | EXPECT_FALSE(req->response_info().network_accessed); |
| 12914 | |
| 12915 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12916 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12917 | EXPECT_TRUE(req->response_info().network_accessed); |
| 12918 | } |
| 12919 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 12920 | // Test that URLRequest is canceled correctly. |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12921 | // See http://crbug.com/508900 |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 12922 | TEST_F(URLRequestTest, URLRequestRedirectJobCancelRequest) { |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12923 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12924 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12925 | GURL("http://not-a-real-domain/"), DEFAULT_PRIORITY, &d, |
| 12926 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12927 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12928 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12929 | req.get(), &default_network_delegate_, |
| 12930 | GURL("http://this-should-never-be-navigated-to/"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 12931 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Jumbo shrimp")); |
| 12932 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12933 | |
| 12934 | req->Start(); |
| 12935 | req->Cancel(); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12936 | base::RunLoop().RunUntilIdle(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 12937 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12938 | EXPECT_EQ(0, d.received_redirect_count()); |
| 12939 | } |
| 12940 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12941 | TEST_F(URLRequestTestHTTP, HeadersCallbacks) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12942 | ASSERT_TRUE(http_test_server()->Start()); |
| 12943 | TestURLRequestContext context; |
| 12944 | GURL url(http_test_server()->GetURL("/cachetime")); |
| 12945 | TestDelegate delegate; |
| 12946 | HttpRequestHeaders extra_headers; |
| 12947 | extra_headers.SetHeader("X-Foo", "bar"); |
| 12948 | |
| 12949 | { |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12950 | HttpRawRequestHeaders raw_req_headers; |
| 12951 | scoped_refptr<const HttpResponseHeaders> raw_resp_headers; |
| 12952 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12953 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12954 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12955 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12956 | r->SetRequestHeadersCallback(base::Bind( |
| 12957 | &HttpRawRequestHeaders::Assign, base::Unretained(&raw_req_headers))); |
| 12958 | r->SetResponseHeadersCallback(base::Bind( |
| 12959 | [](scoped_refptr<const HttpResponseHeaders>* left, |
| 12960 | scoped_refptr<const HttpResponseHeaders> right) { *left = right; }, |
| 12961 | base::Unretained(&raw_resp_headers))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12962 | r->Start(); |
| 12963 | while (!delegate.response_started_count()) |
| 12964 | base::RunLoop().RunUntilIdle(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12965 | EXPECT_FALSE(raw_req_headers.headers().empty()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12966 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12967 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12968 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12969 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12970 | EXPECT_EQ("gzip, deflate", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12971 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Connection", &value)); |
| 12972 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Host", &value)); |
| 12973 | EXPECT_EQ("GET /cachetime HTTP/1.1\r\n", raw_req_headers.request_line()); |
| 12974 | EXPECT_EQ(raw_resp_headers.get(), r->response_headers()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12975 | } |
| 12976 | { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12977 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12978 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12979 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12980 | r->SetRequestHeadersCallback(base::Bind([](HttpRawRequestHeaders) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12981 | FAIL() << "Callback should not be called unless request is sent"; |
| 12982 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12983 | r->SetResponseHeadersCallback( |
| 12984 | base::Bind([](scoped_refptr<const HttpResponseHeaders>) { |
| 12985 | FAIL() << "Callback should not be called unless request is sent"; |
| 12986 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12987 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12988 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12989 | EXPECT_TRUE(r->was_cached()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12990 | } |
| 12991 | } |
| 12992 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12993 | TEST_F(URLRequestTestHTTP, HeadersCallbacksWithRedirect) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12994 | ASSERT_TRUE(http_test_server()->Start()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12995 | HttpRawRequestHeaders raw_req_headers; |
| 12996 | scoped_refptr<const HttpResponseHeaders> raw_resp_headers; |
| 12997 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12998 | TestURLRequestContext context; |
| 12999 | TestDelegate delegate; |
| 13000 | HttpRequestHeaders extra_headers; |
| 13001 | extra_headers.SetHeader("X-Foo", "bar"); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13002 | GURL url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 13003 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13004 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13005 | r->SetExtraRequestHeaders(extra_headers); |
| 13006 | r->SetRequestHeadersCallback(base::Bind(&HttpRawRequestHeaders::Assign, |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13007 | base::Unretained(&raw_req_headers))); |
| 13008 | r->SetResponseHeadersCallback(base::Bind( |
| 13009 | [](scoped_refptr<const HttpResponseHeaders>* left, |
| 13010 | scoped_refptr<const HttpResponseHeaders> right) { *left = right; }, |
| 13011 | base::Unretained(&raw_resp_headers))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13012 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 13013 | delegate.RunUntilRedirect(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13014 | |
| 13015 | ASSERT_EQ(1, delegate.received_redirect_count()); |
| 13016 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13017 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13018 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13019 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13020 | EXPECT_EQ("gzip, deflate", value); |
| 13021 | EXPECT_EQ(1, delegate.received_redirect_count()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13022 | EXPECT_EQ("GET /redirect-test.html HTTP/1.1\r\n", |
| 13023 | raw_req_headers.request_line()); |
| 13024 | EXPECT_TRUE(raw_resp_headers->HasHeader("Location")); |
| 13025 | EXPECT_EQ(302, raw_resp_headers->response_code()); |
| 13026 | EXPECT_EQ("Redirect", raw_resp_headers->GetStatusText()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13027 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13028 | raw_req_headers = HttpRawRequestHeaders(); |
| 13029 | raw_resp_headers = nullptr; |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 13030 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 13031 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 13032 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13033 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13034 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13035 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13036 | EXPECT_EQ("gzip, deflate", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13037 | EXPECT_EQ("GET /with-headers.html HTTP/1.1\r\n", |
| 13038 | raw_req_headers.request_line()); |
| 13039 | EXPECT_EQ(r->response_headers(), raw_resp_headers.get()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13040 | } |
| 13041 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13042 | TEST_F(URLRequestTest, HeadersCallbacksConnectFailed) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13043 | TestDelegate request_delegate; |
| 13044 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 13045 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13046 | GURL("http://127.0.0.1:9/"), DEFAULT_PRIORITY, &request_delegate, |
| 13047 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13048 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
| 13049 | FAIL() << "Callback should not be called unless request is sent"; |
| 13050 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13051 | r->SetResponseHeadersCallback( |
| 13052 | base::Bind([](scoped_refptr<const net::HttpResponseHeaders>) { |
| 13053 | FAIL() << "Callback should not be called unless request is sent"; |
| 13054 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13055 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 13056 | request_delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13057 | EXPECT_FALSE(r->is_pending()); |
| 13058 | } |
| 13059 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13060 | TEST_F(URLRequestTestHTTP, HeadersCallbacksAuthRetry) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13061 | ASSERT_TRUE(http_test_server()->Start()); |
| 13062 | GURL url(http_test_server()->GetURL("/auth-basic")); |
| 13063 | |
| 13064 | TestURLRequestContext context; |
| 13065 | TestDelegate delegate; |
| 13066 | |
| 13067 | delegate.set_credentials(AuthCredentials(kUser, kSecret)); |
| 13068 | HttpRequestHeaders extra_headers; |
| 13069 | extra_headers.SetHeader("X-Foo", "bar"); |
| 13070 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13071 | using ReqHeadersVector = std::vector<std::unique_ptr<HttpRawRequestHeaders>>; |
| 13072 | ReqHeadersVector raw_req_headers; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13073 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13074 | using RespHeadersVector = |
| 13075 | std::vector<scoped_refptr<const HttpResponseHeaders>>; |
| 13076 | RespHeadersVector raw_resp_headers; |
| 13077 | |
| 13078 | auto req_headers_callback = base::Bind( |
| 13079 | [](ReqHeadersVector* vec, HttpRawRequestHeaders headers) { |
| 13080 | vec->emplace_back(new HttpRawRequestHeaders(std::move(headers))); |
| 13081 | }, |
| 13082 | &raw_req_headers); |
| 13083 | auto resp_headers_callback = base::Bind( |
| 13084 | [](RespHeadersVector* vec, |
| 13085 | scoped_refptr<const HttpResponseHeaders> headers) { |
| 13086 | vec->push_back(headers); |
| 13087 | }, |
| 13088 | &raw_resp_headers); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13089 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 13090 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13091 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13092 | r->SetRequestHeadersCallback(req_headers_callback); |
| 13093 | r->SetResponseHeadersCallback(resp_headers_callback); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13094 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 13095 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13096 | EXPECT_FALSE(r->is_pending()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13097 | ASSERT_EQ(raw_req_headers.size(), 2u); |
| 13098 | ASSERT_EQ(raw_resp_headers.size(), 2u); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13099 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13100 | EXPECT_FALSE(raw_req_headers[0]->FindHeaderForTest("Authorization", &value)); |
| 13101 | EXPECT_TRUE(raw_req_headers[0]->FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13102 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13103 | EXPECT_TRUE(raw_req_headers[1]->FindHeaderForTest("Authorization", &value)); |
| 13104 | EXPECT_TRUE(raw_req_headers[1]->FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13105 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13106 | EXPECT_EQ(raw_resp_headers[1], r->response_headers()); |
| 13107 | EXPECT_NE(raw_resp_headers[0], raw_resp_headers[1]); |
| 13108 | EXPECT_EQ(401, raw_resp_headers[0]->response_code()); |
| 13109 | EXPECT_EQ("Unauthorized", raw_resp_headers[0]->GetStatusText()); |
| 13110 | |
| 13111 | std::unique_ptr<URLRequest> r2(context.CreateRequest( |
| 13112 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13113 | r2->SetExtraRequestHeaders(extra_headers); |
| 13114 | r2->SetRequestHeadersCallback(req_headers_callback); |
| 13115 | r2->SetResponseHeadersCallback(resp_headers_callback); |
| 13116 | r2->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 13117 | r2->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 13118 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13119 | EXPECT_FALSE(r2->is_pending()); |
| 13120 | ASSERT_EQ(raw_req_headers.size(), 3u); |
| 13121 | ASSERT_EQ(raw_resp_headers.size(), 3u); |
| 13122 | EXPECT_TRUE(raw_req_headers[2]->FindHeaderForTest("If-None-Match", &value)); |
| 13123 | EXPECT_NE(raw_resp_headers[2].get(), r2->response_headers()); |
| 13124 | EXPECT_EQ(304, raw_resp_headers[2]->response_code()); |
| 13125 | EXPECT_EQ("Not Modified", raw_resp_headers[2]->GetStatusText()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13126 | } |
| 13127 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13128 | TEST_F(URLRequestTest, HeadersCallbacksNonHTTP) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13129 | GURL data_url("data:text/html,<html><body>Hello!</body></html>"); |
| 13130 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 13131 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13132 | data_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13133 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13134 | FAIL() << "Callback should not be called for non-HTTP schemes"; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13135 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13136 | r->SetResponseHeadersCallback( |
| 13137 | base::Bind([](scoped_refptr<const net::HttpResponseHeaders>) { |
| 13138 | FAIL() << "Callback should not be called for non-HTTP schemes"; |
| 13139 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13140 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 13141 | d.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13142 | EXPECT_FALSE(r->is_pending()); |
| 13143 | } |
| 13144 | |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13145 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSet) { |
| 13146 | TestDelegate d; |
| 13147 | BlockingNetworkDelegate network_delegate( |
| 13148 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 13149 | const GURL kOriginalUrl("https://original.test"); |
| 13150 | const GURL kRedirectUrl("http://redirect.test"); |
| 13151 | network_delegate.set_redirect_url(kRedirectUrl); |
| 13152 | TestURLRequestContext context(true /* delay_initialization */); |
| 13153 | context.set_network_delegate(&network_delegate); |
| 13154 | context.Init(); |
| 13155 | |
| 13156 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 13157 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13158 | r->set_upgrade_if_insecure(true); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13159 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 13160 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13161 | GURL::Replacements replacements; |
| 13162 | // Check that the redirect URL was upgraded to HTTPS since upgrade_if_insecure |
| 13163 | // was set. |
| 13164 | replacements.SetSchemeStr("https"); |
| 13165 | EXPECT_EQ(kRedirectUrl.ReplaceComponents(replacements), |
| 13166 | d.redirect_info().new_url); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 13167 | EXPECT_TRUE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13168 | } |
| 13169 | |
| 13170 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSetExplicitPort80) { |
| 13171 | TestDelegate d; |
| 13172 | BlockingNetworkDelegate network_delegate( |
| 13173 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 13174 | const GURL kOriginalUrl("https://original.test"); |
| 13175 | const GURL kRedirectUrl("http://redirect.test:80"); |
| 13176 | network_delegate.set_redirect_url(kRedirectUrl); |
| 13177 | TestURLRequestContext context(true /* delay_initialization */); |
| 13178 | context.set_network_delegate(&network_delegate); |
| 13179 | context.Init(); |
| 13180 | |
| 13181 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 13182 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13183 | r->set_upgrade_if_insecure(true); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13184 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 13185 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13186 | GURL::Replacements replacements; |
| 13187 | // The URL host should have not been changed. |
| 13188 | EXPECT_EQ(d.redirect_info().new_url.host(), kRedirectUrl.host()); |
| 13189 | // The scheme should now be https, and the effective port should now be 443. |
| 13190 | EXPECT_TRUE(d.redirect_info().new_url.SchemeIs("https")); |
| 13191 | EXPECT_EQ(d.redirect_info().new_url.EffectiveIntPort(), 443); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 13192 | EXPECT_TRUE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13193 | } |
| 13194 | |
| 13195 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSetNonStandardPort) { |
| 13196 | TestDelegate d; |
| 13197 | BlockingNetworkDelegate network_delegate( |
| 13198 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 13199 | const GURL kOriginalUrl("https://original.test"); |
| 13200 | const GURL kRedirectUrl("http://redirect.test:1234"); |
| 13201 | network_delegate.set_redirect_url(kRedirectUrl); |
| 13202 | TestURLRequestContext context(true /* delay_initialization */); |
| 13203 | context.set_network_delegate(&network_delegate); |
| 13204 | context.Init(); |
| 13205 | |
| 13206 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 13207 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13208 | r->set_upgrade_if_insecure(true); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13209 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 13210 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13211 | GURL::Replacements replacements; |
| 13212 | // Check that the redirect URL was upgraded to HTTPS since upgrade_if_insecure |
| 13213 | // was set, nonstandard port should not have been modified. |
| 13214 | replacements.SetSchemeStr("https"); |
| 13215 | EXPECT_EQ(kRedirectUrl.ReplaceComponents(replacements), |
| 13216 | d.redirect_info().new_url); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 13217 | EXPECT_TRUE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13218 | } |
| 13219 | |
| 13220 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagNotSet) { |
| 13221 | TestDelegate d; |
| 13222 | BlockingNetworkDelegate network_delegate( |
| 13223 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 13224 | const GURL kOriginalUrl("https://original.test"); |
| 13225 | const GURL kRedirectUrl("http://redirect.test"); |
| 13226 | network_delegate.set_redirect_url(kRedirectUrl); |
| 13227 | TestURLRequestContext context(true /* delay_initialization */); |
| 13228 | context.set_network_delegate(&network_delegate); |
| 13229 | context.Init(); |
| 13230 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 13231 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13232 | r->set_upgrade_if_insecure(false); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13233 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 13234 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13235 | // The redirect URL should not be changed if the upgrade_if_insecure flag is |
| 13236 | // not set. |
| 13237 | EXPECT_EQ(kRedirectUrl, d.redirect_info().new_url); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 13238 | EXPECT_FALSE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13239 | } |
| 13240 | |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 13241 | // Test that URLRequests get properly tagged. |
| 13242 | #if defined(OS_ANDROID) |
| 13243 | TEST_F(URLRequestTestHTTP, TestTagging) { |
Peter Collingbourne | 8ccab72 | 2019-02-12 18:10:48 | [diff] [blame] | 13244 | if (!CanGetTaggedBytes()) { |
| 13245 | DVLOG(0) << "Skipping test - GetTaggedBytes unsupported."; |
| 13246 | return; |
| 13247 | } |
| 13248 | |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 13249 | ASSERT_TRUE(http_test_server()->Start()); |
| 13250 | |
| 13251 | // The tag under which the system reports untagged traffic. |
| 13252 | static const int32_t UNTAGGED_TAG = 0; |
| 13253 | |
| 13254 | uint64_t old_traffic = GetTaggedBytes(UNTAGGED_TAG); |
| 13255 | |
| 13256 | // Untagged traffic should be tagged with tag UNTAGGED_TAG. |
| 13257 | TestDelegate delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 13258 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 13259 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &delegate, |
| 13260 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13261 | EXPECT_EQ(SocketTag(), req->socket_tag()); |
| 13262 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 13263 | delegate.RunUntilComplete(); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 13264 | |
| 13265 | EXPECT_GT(GetTaggedBytes(UNTAGGED_TAG), old_traffic); |
| 13266 | |
| 13267 | int32_t tag_val1 = 0x12345678; |
| 13268 | SocketTag tag1(SocketTag::UNSET_UID, tag_val1); |
| 13269 | old_traffic = GetTaggedBytes(tag_val1); |
| 13270 | |
| 13271 | // Test specific tag value. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 13272 | req = default_context().CreateRequest(http_test_server()->GetURL("/"), |
| 13273 | DEFAULT_PRIORITY, &delegate, |
| 13274 | TRAFFIC_ANNOTATION_FOR_TESTS); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 13275 | req->set_socket_tag(tag1); |
| 13276 | EXPECT_EQ(tag1, req->socket_tag()); |
| 13277 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 13278 | delegate.RunUntilComplete(); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 13279 | |
| 13280 | EXPECT_GT(GetTaggedBytes(tag_val1), old_traffic); |
| 13281 | } |
| 13282 | #endif |
| 13283 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 13284 | // Provides a response to the 0RTT request indicating whether it was received |
| 13285 | // as early data, sending HTTP_TOO_EARLY if enabled. |
| 13286 | class ZeroRTTResponse : public test_server::BasicHttpResponse { |
| 13287 | public: |
| 13288 | ZeroRTTResponse(bool zero_rtt, bool send_too_early) |
| 13289 | : zero_rtt_(zero_rtt), send_too_early_(send_too_early) {} |
| 13290 | ~ZeroRTTResponse() override {} |
| 13291 | |
| 13292 | void SendResponse(const test_server::SendBytesCallback& send, |
| 13293 | const test_server::SendCompleteCallback& done) override { |
| 13294 | AddCustomHeader("Vary", "Early-Data"); |
| 13295 | set_content_type("text/plain"); |
| 13296 | AddCustomHeader("Cache-Control", "no-cache"); |
| 13297 | if (zero_rtt_) { |
| 13298 | if (send_too_early_) |
| 13299 | set_code(HTTP_TOO_EARLY); |
| 13300 | set_content("1"); |
| 13301 | } else { |
| 13302 | set_content("0"); |
| 13303 | } |
| 13304 | |
| 13305 | // Since the EmbeddedTestServer doesn't keep the socket open by default, |
| 13306 | // it is explicitly kept alive to allow the remaining leg of the 0RTT |
| 13307 | // handshake to be received after the early data. |
| 13308 | send.Run(ToResponseString(), base::DoNothing()); |
| 13309 | } |
| 13310 | |
| 13311 | private: |
| 13312 | bool zero_rtt_; |
| 13313 | bool send_too_early_; |
| 13314 | |
| 13315 | DISALLOW_COPY_AND_ASSIGN(ZeroRTTResponse); |
| 13316 | }; |
| 13317 | |
| 13318 | std::unique_ptr<test_server::HttpResponse> HandleZeroRTTRequest( |
| 13319 | const test_server::HttpRequest& request) { |
| 13320 | if (request.GetURL().path() != "/zerortt") |
| 13321 | return nullptr; |
| 13322 | auto iter = request.headers.find("Early-Data"); |
| 13323 | bool zero_rtt = iter != request.headers.end() && iter->second == "1"; |
| 13324 | return std::make_unique<ZeroRTTResponse>(zero_rtt, false); |
| 13325 | } |
| 13326 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame^] | 13327 | class HTTPSEarlyDataTest : public TestWithTaskEnvironment { |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 13328 | public: |
| 13329 | HTTPSEarlyDataTest() |
| 13330 | : context_(true), test_server_(net::EmbeddedTestServer::TYPE_HTTPS) { |
| 13331 | auto params = std::make_unique<HttpNetworkSession::Params>(); |
| 13332 | params->enable_early_data = true; |
| 13333 | context_.set_http_network_session_params(std::move(params)); |
| 13334 | |
| 13335 | context_.set_network_delegate(&network_delegate_); |
| 13336 | cert_verifier_.set_default_result(OK); |
| 13337 | context_.set_cert_verifier(&cert_verifier_); |
| 13338 | |
David Benjamin | 151ec6b | 2019-08-02 19:38:52 | [diff] [blame] | 13339 | SSLContextConfig config; |
| 13340 | config.version_max = SSL_PROTOCOL_VERSION_TLS1_3; |
| 13341 | ssl_config_service_ = std::make_unique<TestSSLConfigService>(config); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 13342 | context_.set_ssl_config_service(ssl_config_service_.get()); |
| 13343 | |
| 13344 | context_.Init(); |
| 13345 | |
| 13346 | ssl_config_.version_max = SSL_PROTOCOL_VERSION_TLS1_3; |
| 13347 | ssl_config_.early_data_enabled = true; |
| 13348 | test_server_.SetSSLConfig(net::EmbeddedTestServer::CERT_OK, ssl_config_); |
| 13349 | test_server_.AddDefaultHandlers( |
| 13350 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 13351 | test_server_.RegisterRequestHandler( |
| 13352 | base::BindRepeating(&HandleZeroRTTRequest)); |
| 13353 | } |
| 13354 | |
| 13355 | ~HTTPSEarlyDataTest() override = default; |
| 13356 | |
| 13357 | void ResetSSLConfig(net::EmbeddedTestServer::ServerCertificate cert, |
| 13358 | uint16_t version) { |
| 13359 | ssl_config_.version_max = version; |
| 13360 | test_server_.ResetSSLConfig(cert, ssl_config_); |
| 13361 | } |
| 13362 | |
| 13363 | protected: |
| 13364 | MockCertVerifier cert_verifier_; |
| 13365 | TestNetworkDelegate network_delegate_; // Must outlive URLRequest. |
| 13366 | std::unique_ptr<TestSSLConfigService> ssl_config_service_; |
| 13367 | TestURLRequestContext context_; |
| 13368 | |
| 13369 | SSLServerConfig ssl_config_; |
| 13370 | EmbeddedTestServer test_server_; |
| 13371 | }; |
| 13372 | |
| 13373 | // TLSEarlyDataTest tests that we handle early data correctly. |
| 13374 | TEST_F(HTTPSEarlyDataTest, TLSEarlyDataTest) { |
| 13375 | ASSERT_TRUE(test_server_.Start()); |
| 13376 | context_.http_transaction_factory()->GetSession()->ClearSSLSessionCache(); |
| 13377 | |
| 13378 | { |
| 13379 | TestDelegate d; |
| 13380 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13381 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13382 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13383 | r->Start(); |
| 13384 | EXPECT_TRUE(r->is_pending()); |
| 13385 | |
| 13386 | base::RunLoop().Run(); |
| 13387 | |
| 13388 | EXPECT_EQ(1, d.response_started_count()); |
| 13389 | |
| 13390 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13391 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13392 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13393 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13394 | r->ssl_info().cert.get())); |
| 13395 | |
| 13396 | // The Early-Data header should be omitted in the initial request, and the |
| 13397 | // handler should return "0". |
| 13398 | EXPECT_EQ("0", d.data_received()); |
| 13399 | } |
| 13400 | |
| 13401 | context_.http_transaction_factory()->GetSession()->CloseAllConnections(); |
| 13402 | |
| 13403 | { |
| 13404 | TestDelegate d; |
| 13405 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13406 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13407 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13408 | |
| 13409 | r->Start(); |
| 13410 | EXPECT_TRUE(r->is_pending()); |
| 13411 | |
| 13412 | base::RunLoop().Run(); |
| 13413 | |
| 13414 | EXPECT_EQ(1, d.response_started_count()); |
| 13415 | |
| 13416 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13417 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13418 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13419 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13420 | r->ssl_info().cert.get())); |
| 13421 | |
| 13422 | // The Early-Data header should be a single '1' in the resumed request, and |
| 13423 | // the handler should return "1". |
| 13424 | EXPECT_EQ("1", d.data_received()); |
| 13425 | } |
| 13426 | } |
| 13427 | |
| 13428 | // TLSEarlyDataTest tests that we handle early data correctly for POST. |
| 13429 | TEST_F(HTTPSEarlyDataTest, TLSEarlyDataPOSTTest) { |
| 13430 | ASSERT_TRUE(test_server_.Start()); |
| 13431 | context_.http_transaction_factory()->GetSession()->ClearSSLSessionCache(); |
| 13432 | |
| 13433 | { |
| 13434 | TestDelegate d; |
| 13435 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13436 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13437 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13438 | r->Start(); |
| 13439 | EXPECT_TRUE(r->is_pending()); |
| 13440 | |
| 13441 | base::RunLoop().Run(); |
| 13442 | |
| 13443 | EXPECT_EQ(1, d.response_started_count()); |
| 13444 | |
| 13445 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13446 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13447 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13448 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13449 | r->ssl_info().cert.get())); |
| 13450 | |
| 13451 | // The Early-Data header should be omitted in the initial request, and the |
| 13452 | // handler should return "0". |
| 13453 | EXPECT_EQ("0", d.data_received()); |
| 13454 | } |
| 13455 | |
| 13456 | context_.http_transaction_factory()->GetSession()->CloseAllConnections(); |
| 13457 | |
| 13458 | { |
| 13459 | TestDelegate d; |
| 13460 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13461 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13462 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13463 | r->set_method("POST"); |
| 13464 | r->Start(); |
| 13465 | EXPECT_TRUE(r->is_pending()); |
| 13466 | |
| 13467 | base::RunLoop().Run(); |
| 13468 | |
| 13469 | EXPECT_EQ(1, d.response_started_count()); |
| 13470 | |
| 13471 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13472 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13473 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13474 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13475 | r->ssl_info().cert.get())); |
| 13476 | |
| 13477 | // The Early-Data header should be omitted in the request, since we don't |
| 13478 | // send POSTs over early data, and the handler should return "0". |
| 13479 | EXPECT_EQ("0", d.data_received()); |
| 13480 | } |
| 13481 | } |
| 13482 | |
| 13483 | std::unique_ptr<test_server::HttpResponse> HandleTooEarly( |
| 13484 | bool* sent_425, |
| 13485 | const test_server::HttpRequest& request) { |
| 13486 | if (request.GetURL().path() != "/tooearly") |
| 13487 | return nullptr; |
| 13488 | auto iter = request.headers.find("Early-Data"); |
| 13489 | bool zero_rtt = iter != request.headers.end() && iter->second == "1"; |
| 13490 | if (zero_rtt) |
| 13491 | *sent_425 = true; |
| 13492 | return std::make_unique<ZeroRTTResponse>(zero_rtt, true); |
| 13493 | } |
| 13494 | |
| 13495 | // Test that we handle 425 (Too Early) correctly. |
| 13496 | TEST_F(HTTPSEarlyDataTest, TLSEarlyDataTooEarlyTest) { |
| 13497 | bool sent_425 = false; |
| 13498 | test_server_.RegisterRequestHandler( |
| 13499 | base::BindRepeating(&HandleTooEarly, base::Unretained(&sent_425))); |
| 13500 | ASSERT_TRUE(test_server_.Start()); |
| 13501 | context_.http_transaction_factory()->GetSession()->ClearSSLSessionCache(); |
| 13502 | |
| 13503 | { |
| 13504 | TestDelegate d; |
| 13505 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13506 | test_server_.GetURL("/tooearly"), DEFAULT_PRIORITY, &d, |
| 13507 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13508 | r->Start(); |
| 13509 | EXPECT_TRUE(r->is_pending()); |
| 13510 | |
| 13511 | d.RunUntilComplete(); |
| 13512 | |
| 13513 | EXPECT_EQ(1, d.response_started_count()); |
| 13514 | |
| 13515 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13516 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13517 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13518 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13519 | r->ssl_info().cert.get())); |
| 13520 | |
| 13521 | // The Early-Data header should be omitted in the initial request, and the |
| 13522 | // handler should return "0". |
| 13523 | EXPECT_EQ("0", d.data_received()); |
| 13524 | EXPECT_FALSE(sent_425); |
| 13525 | } |
| 13526 | |
| 13527 | context_.http_transaction_factory()->GetSession()->CloseAllConnections(); |
| 13528 | |
| 13529 | { |
| 13530 | TestDelegate d; |
| 13531 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13532 | test_server_.GetURL("/tooearly"), DEFAULT_PRIORITY, &d, |
| 13533 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13534 | |
| 13535 | r->Start(); |
| 13536 | EXPECT_TRUE(r->is_pending()); |
| 13537 | |
| 13538 | d.RunUntilComplete(); |
| 13539 | |
| 13540 | EXPECT_EQ(1, d.response_started_count()); |
| 13541 | |
| 13542 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13543 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13544 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13545 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13546 | r->ssl_info().cert.get())); |
| 13547 | |
| 13548 | // The resumption request will encounter a 425 error and retry without early |
| 13549 | // data, and the handler should return "0". |
| 13550 | EXPECT_EQ("0", d.data_received()); |
| 13551 | EXPECT_TRUE(sent_425); |
| 13552 | } |
| 13553 | } |
| 13554 | |
| 13555 | // TLSEarlyDataRejectTest tests that we gracefully handle an early data reject |
| 13556 | // and retry without early data. |
| 13557 | TEST_F(HTTPSEarlyDataTest, TLSEarlyDataRejectTest) { |
| 13558 | ASSERT_TRUE(test_server_.Start()); |
| 13559 | context_.http_transaction_factory()->GetSession()->ClearSSLSessionCache(); |
| 13560 | |
| 13561 | { |
| 13562 | TestDelegate d; |
| 13563 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13564 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13565 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13566 | |
| 13567 | r->Start(); |
| 13568 | EXPECT_TRUE(r->is_pending()); |
| 13569 | |
| 13570 | d.RunUntilComplete(); |
| 13571 | |
| 13572 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13573 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13574 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13575 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13576 | r->ssl_info().cert.get())); |
| 13577 | |
| 13578 | // The Early-Data header should be omitted in the initial request, and the |
| 13579 | // handler should return "0". |
| 13580 | EXPECT_EQ("0", d.data_received()); |
| 13581 | } |
| 13582 | |
| 13583 | context_.http_transaction_factory()->GetSession()->CloseAllConnections(); |
| 13584 | |
| 13585 | // The certificate in the resumption is changed to confirm that the |
| 13586 | // certificate change is observed. |
| 13587 | scoped_refptr<X509Certificate> old_cert = test_server_.GetCertificate(); |
| 13588 | ResetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED, |
| 13589 | SSL_PROTOCOL_VERSION_TLS1_3); |
| 13590 | |
| 13591 | { |
| 13592 | TestDelegate d; |
| 13593 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13594 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13595 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13596 | |
| 13597 | r->Start(); |
| 13598 | EXPECT_TRUE(r->is_pending()); |
| 13599 | |
| 13600 | d.RunUntilComplete(); |
| 13601 | |
| 13602 | EXPECT_EQ(1, d.response_started_count()); |
| 13603 | |
| 13604 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13605 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13606 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13607 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13608 | r->ssl_info().cert.get())); |
| 13609 | EXPECT_FALSE(old_cert->EqualsIncludingChain(r->ssl_info().cert.get())); |
| 13610 | |
| 13611 | // The Early-Data header should be omitted in the rejected request, and the |
| 13612 | // handler should return "0". |
| 13613 | EXPECT_EQ("0", d.data_received()); |
| 13614 | } |
| 13615 | } |
| 13616 | |
| 13617 | // TLSEarlyDataTLS12RejectTest tests that we gracefully handle an early data |
| 13618 | // reject from a TLS 1.2 server and retry without early data. |
| 13619 | TEST_F(HTTPSEarlyDataTest, TLSEarlyDataTLS12RejectTest) { |
| 13620 | ASSERT_TRUE(test_server_.Start()); |
| 13621 | context_.http_transaction_factory()->GetSession()->ClearSSLSessionCache(); |
| 13622 | |
| 13623 | { |
| 13624 | TestDelegate d; |
| 13625 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13626 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13627 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13628 | |
| 13629 | r->Start(); |
| 13630 | EXPECT_TRUE(r->is_pending()); |
| 13631 | |
| 13632 | d.RunUntilComplete(); |
| 13633 | |
| 13634 | EXPECT_EQ(1, d.response_started_count()); |
| 13635 | |
| 13636 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13637 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13638 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13639 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13640 | r->ssl_info().cert.get())); |
| 13641 | |
| 13642 | // The Early-Data header should be omitted in the initial request, and the |
| 13643 | // handler should return "0". |
| 13644 | EXPECT_EQ("0", d.data_received()); |
| 13645 | } |
| 13646 | |
| 13647 | context_.http_transaction_factory()->GetSession()->CloseAllConnections(); |
| 13648 | |
| 13649 | // The certificate in the resumption is changed to confirm that the |
| 13650 | // certificate change is observed. |
| 13651 | scoped_refptr<X509Certificate> old_cert = test_server_.GetCertificate(); |
| 13652 | ResetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED, |
| 13653 | SSL_PROTOCOL_VERSION_TLS1_2); |
| 13654 | |
| 13655 | { |
| 13656 | TestDelegate d; |
| 13657 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13658 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13659 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13660 | |
| 13661 | r->Start(); |
| 13662 | EXPECT_TRUE(r->is_pending()); |
| 13663 | |
| 13664 | d.RunUntilComplete(); |
| 13665 | |
| 13666 | EXPECT_EQ(1, d.response_started_count()); |
| 13667 | |
| 13668 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_2, |
| 13669 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13670 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13671 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13672 | r->ssl_info().cert.get())); |
| 13673 | EXPECT_FALSE(old_cert->EqualsIncludingChain(r->ssl_info().cert.get())); |
| 13674 | |
| 13675 | // The Early-Data header should be omitted in the rejected request, and the |
| 13676 | // handler should return "0". |
| 13677 | EXPECT_EQ("0", d.data_received()); |
| 13678 | } |
| 13679 | } |
| 13680 | |
Emily Stark | 08f6c97 | 2019-05-28 17:24:27 | [diff] [blame] | 13681 | // Tests that AuthChallengeInfo is available on the request. |
| 13682 | TEST_F(URLRequestTestHTTP, AuthChallengeInfo) { |
| 13683 | ASSERT_TRUE(http_test_server()->Start()); |
| 13684 | GURL url(http_test_server()->GetURL("/auth-basic")); |
| 13685 | |
| 13686 | TestURLRequestContext context; |
| 13687 | TestDelegate delegate; |
| 13688 | |
| 13689 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 13690 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13691 | r->Start(); |
| 13692 | delegate.RunUntilComplete(); |
| 13693 | ASSERT_TRUE(r->auth_challenge_info().has_value()); |
| 13694 | EXPECT_FALSE(r->auth_challenge_info()->is_proxy); |
| 13695 | EXPECT_EQ(url::Origin::Create(url), r->auth_challenge_info()->challenger); |
| 13696 | EXPECT_EQ("basic", r->auth_challenge_info()->scheme); |
| 13697 | EXPECT_EQ("testrealm", r->auth_challenge_info()->realm); |
| 13698 | EXPECT_EQ("Basic realm=\"testrealm\"", r->auth_challenge_info()->challenge); |
| 13699 | EXPECT_EQ("/auth-basic", r->auth_challenge_info()->path); |
| 13700 | } |
| 13701 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 13702 | } // namespace net |