[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. |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 808 | class URLRequestTest : public PlatformTest, public WithScopedTaskEnvironment { |
[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 |
| 2687 | // SetCanonicalCookie (and therefore SetCookieWithOptions) fail. |
| 2688 | GURL::Replacements replace_scheme; |
| 2689 | replace_scheme.SetSchemeStr("https"); |
| 2690 | GURL url = test_server.base_url().ReplaceComponents(replace_scheme); |
| 2691 | |
| 2692 | delayed_cm->SetCookieWithOptionsAsync(url, "AlreadySetCookie=1;Secure", |
| 2693 | CookieOptions(), |
| 2694 | CookieStore::SetCookiesCallback()); |
| 2695 | cm->SetCookieWithOptionsAsync(url, "AlreadySetCookie=1;Secure", |
| 2696 | CookieOptions(), |
| 2697 | CookieStore::SetCookiesCallback()); |
| 2698 | |
| 2699 | std::vector<std::string> cookie_lines( |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 2700 | {// Fails in SetCanonicalCookie for trying to set a secure cookie |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 2701 | // on an insecure host. |
| 2702 | "CookieNotSet=1;Secure", |
| 2703 | // Fail in FilteringTestLayeredNetworkDelegate::CanGetCookie. |
| 2704 | "CookieBlockedOnCanGetCookie=1", |
| 2705 | // Fails in SetCanonicalCookie for trying to overwrite a secure cookie |
| 2706 | // with an insecure cookie. |
| 2707 | "AlreadySetCookie=1", |
| 2708 | // Succeeds and added cookie to store. Delayed (which makes the callback |
| 2709 | // run asynchronously) in DelayedCookieMonster. |
| 2710 | "CookieSet=1"}); |
| 2711 | |
| 2712 | for (auto first_cookie_line : cookie_lines) { |
| 2713 | for (auto second_cookie_line : cookie_lines) { |
| 2714 | // Run with the delayed cookie monster. |
| 2715 | std::unique_ptr<URLRequest> request = async_context.CreateRequest( |
| 2716 | test_server.GetURL("/set-cookie?" + first_cookie_line + "&" + |
| 2717 | second_cookie_line), |
| 2718 | DEFAULT_PRIORITY, &async_delegate, TRAFFIC_ANNOTATION_FOR_TESTS); |
| 2719 | |
| 2720 | request->Start(); |
| 2721 | async_delegate.RunUntilComplete(); |
| 2722 | EXPECT_THAT(async_delegate.request_status(), IsOk()); |
| 2723 | |
| 2724 | // Run with the regular cookie monster. |
| 2725 | request = sync_context.CreateRequest( |
| 2726 | test_server.GetURL("/set-cookie?" + first_cookie_line + "&" + |
| 2727 | second_cookie_line), |
| 2728 | DEFAULT_PRIORITY, &sync_delegate, TRAFFIC_ANNOTATION_FOR_TESTS); |
| 2729 | |
| 2730 | request->Start(); |
| 2731 | sync_delegate.RunUntilComplete(); |
| 2732 | EXPECT_THAT(sync_delegate.request_status(), IsOk()); |
| 2733 | |
| 2734 | int expected_set_cookie_count = 0; |
| 2735 | int expected_blocked_cookie_count = 0; |
| 2736 | |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 2737 | // 2 calls to the delegate's OnCanSetCookie method are expected, even if |
| 2738 | // the cookies don't end up getting set. |
| 2739 | expected_set_cookie_count += 2; |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 2740 | |
| 2741 | if (first_cookie_line == "CookieBlockedOnCanGetCookie=1") |
| 2742 | ++expected_blocked_cookie_count; |
| 2743 | if (second_cookie_line == "CookieBlockedOnCanGetCookie=1") |
| 2744 | ++expected_blocked_cookie_count; |
| 2745 | |
| 2746 | EXPECT_EQ(expected_set_cookie_count, |
| 2747 | async_filter_network_delegate.set_cookie_called_count()); |
| 2748 | EXPECT_EQ(expected_blocked_cookie_count, |
| 2749 | async_filter_network_delegate.blocked_set_cookie_count()); |
| 2750 | |
| 2751 | EXPECT_EQ(expected_set_cookie_count, |
| 2752 | sync_filter_network_delegate.set_cookie_called_count()); |
| 2753 | EXPECT_EQ(expected_blocked_cookie_count, |
| 2754 | sync_filter_network_delegate.blocked_set_cookie_count()); |
| 2755 | |
| 2756 | async_filter_network_delegate.ResetSetCookieCalledCount(); |
| 2757 | async_filter_network_delegate.ResetBlockedSetCookieCount(); |
| 2758 | |
| 2759 | sync_filter_network_delegate.ResetSetCookieCalledCount(); |
| 2760 | sync_filter_network_delegate.ResetBlockedSetCookieCount(); |
| 2761 | } |
| 2762 | } |
| 2763 | } |
| 2764 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2765 | TEST_F(URLRequestTest, DoNotSendCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2766 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2767 | ASSERT_TRUE(test_server.Start()); |
| 2768 | |
| 2769 | // Set up a cookie. |
| 2770 | { |
| 2771 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2772 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2773 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2774 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2775 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2776 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2777 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2778 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2779 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2780 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2781 | } |
| 2782 | |
| 2783 | // Verify that the cookie is set. |
| 2784 | { |
| 2785 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2786 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2787 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2788 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2789 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2790 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2791 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2792 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2793 | |
| 2794 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2795 | != std::string::npos); |
| 2796 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2797 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2798 | } |
| 2799 | |
| 2800 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 2801 | { |
| 2802 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2803 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2804 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2805 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2806 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2807 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2808 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
| 2809 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2810 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2811 | |
| 2812 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2813 | == std::string::npos); |
| 2814 | |
| 2815 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
| 2816 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2817 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2818 | } |
| 2819 | } |
| 2820 | |
| 2821 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2822 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2823 | ASSERT_TRUE(test_server.Start()); |
| 2824 | |
| 2825 | // Set up a cookie. |
| 2826 | { |
| 2827 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2828 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2829 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2830 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2831 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2832 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2833 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2834 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2835 | |
| 2836 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2837 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2838 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2839 | } |
| 2840 | |
| 2841 | // Try to set-up another cookie and update the previous cookie. |
| 2842 | { |
| 2843 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2844 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2845 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2846 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2847 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2848 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2849 | req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES); |
| 2850 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2851 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2852 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2853 | |
| 2854 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
| 2855 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2856 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2857 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2858 | } |
| 2859 | |
| 2860 | // Verify the cookies weren't saved or updated. |
| 2861 | { |
| 2862 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2863 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2864 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2865 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2866 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2867 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2868 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2869 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2870 | |
| 2871 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2872 | == std::string::npos); |
| 2873 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2874 | != std::string::npos); |
| 2875 | |
| 2876 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2877 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2878 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2879 | } |
| 2880 | } |
| 2881 | |
| 2882 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2883 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2884 | ASSERT_TRUE(test_server.Start()); |
| 2885 | |
| 2886 | // Set up a cookie. |
| 2887 | { |
| 2888 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2889 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2890 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2891 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2892 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2893 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2894 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2895 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2896 | |
| 2897 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2898 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2899 | } |
| 2900 | |
| 2901 | // Verify that the cookie is set. |
| 2902 | { |
| 2903 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2904 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2905 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2906 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2907 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2908 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2909 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2910 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2911 | |
| 2912 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2913 | != std::string::npos); |
| 2914 | |
| 2915 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2916 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2917 | auto entries = net_log_.GetEntries(); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2918 | for (const auto& entry : entries) { |
| 2919 | EXPECT_NE(entry.type, |
| 2920 | NetLogEventType::COOKIE_GET_BLOCKED_BY_NETWORK_DELEGATE); |
| 2921 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2922 | } |
| 2923 | |
| 2924 | // Verify that the cookie isn't sent. |
| 2925 | { |
| 2926 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2927 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2928 | TestDelegate d; |
| 2929 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2930 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2931 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2932 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2933 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2934 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2935 | |
| 2936 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2937 | == std::string::npos); |
| 2938 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2939 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2940 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2941 | auto entries = net_log_.GetEntries(); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2942 | ExpectLogContainsSomewhereAfter( |
| 2943 | entries, 0, NetLogEventType::COOKIE_GET_BLOCKED_BY_NETWORK_DELEGATE, |
| 2944 | NetLogEventPhase::NONE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2945 | } |
| 2946 | } |
| 2947 | |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 2948 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 2949 | #if defined(OS_IOS) |
| 2950 | #define MAYBE_DoNotSaveCookies_ViaPolicy FLAKY_DoNotSaveCookies_ViaPolicy |
| 2951 | #else |
| 2952 | #define MAYBE_DoNotSaveCookies_ViaPolicy DoNotSaveCookies_ViaPolicy |
| 2953 | #endif |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2954 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2955 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2956 | ASSERT_TRUE(test_server.Start()); |
| 2957 | |
| 2958 | // Set up a cookie. |
| 2959 | { |
| 2960 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2961 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2962 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2963 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2964 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2965 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2966 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2967 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2968 | |
| 2969 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2970 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2971 | auto entries = net_log_.GetEntries(); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2972 | for (const auto& entry : entries) { |
| 2973 | EXPECT_NE(entry.type, |
| 2974 | NetLogEventType::COOKIE_SET_BLOCKED_BY_NETWORK_DELEGATE); |
| 2975 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2976 | } |
| 2977 | |
| 2978 | // Try to set-up another cookie and update the previous cookie. |
| 2979 | { |
| 2980 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2981 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2982 | TestDelegate d; |
| 2983 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2984 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2985 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2986 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2987 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2988 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2989 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2990 | |
| 2991 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2992 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2993 | auto entries = net_log_.GetEntries(); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2994 | ExpectLogContainsSomewhereAfter( |
| 2995 | entries, 0, NetLogEventType::COOKIE_SET_BLOCKED_BY_NETWORK_DELEGATE, |
| 2996 | NetLogEventPhase::NONE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | // Verify the cookies weren't saved or updated. |
| 3000 | { |
| 3001 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3002 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3003 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3004 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3005 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3006 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3007 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3008 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3009 | |
| 3010 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 3011 | == std::string::npos); |
| 3012 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 3013 | != std::string::npos); |
| 3014 | |
| 3015 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3016 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3017 | } |
| 3018 | } |
| 3019 | |
| 3020 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3021 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3022 | ASSERT_TRUE(test_server.Start()); |
| 3023 | |
| 3024 | // Set up an empty cookie. |
| 3025 | { |
| 3026 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3027 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3028 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3029 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3030 | test_server.GetURL("/set-cookie"), DEFAULT_PRIORITY, &d, |
| 3031 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3032 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3033 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3034 | |
| 3035 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3036 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3037 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 3038 | } |
| 3039 | } |
| 3040 | |
| 3041 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3042 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3043 | ASSERT_TRUE(test_server.Start()); |
| 3044 | |
| 3045 | // Set up a cookie. |
| 3046 | { |
| 3047 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3048 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3049 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3050 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3051 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3052 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3053 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3054 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3055 | |
| 3056 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3057 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3058 | } |
| 3059 | |
| 3060 | // Verify that the cookie is set. |
| 3061 | { |
| 3062 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3063 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3064 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3065 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3066 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3067 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3068 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3069 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3070 | |
| 3071 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 3072 | != std::string::npos); |
| 3073 | |
| 3074 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3075 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3076 | } |
| 3077 | |
| 3078 | // Verify that the cookie isn't sent. |
| 3079 | { |
| 3080 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3081 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3082 | TestDelegate d; |
| 3083 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3084 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3085 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3086 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3087 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3088 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3089 | |
| 3090 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 3091 | == std::string::npos); |
| 3092 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 3093 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3094 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3095 | } |
| 3096 | } |
| 3097 | |
| 3098 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3099 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3100 | ASSERT_TRUE(test_server.Start()); |
| 3101 | |
| 3102 | // Set up a cookie. |
| 3103 | { |
| 3104 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3105 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3106 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3107 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3108 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3109 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3110 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3111 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3112 | |
| 3113 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3114 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3115 | } |
| 3116 | |
| 3117 | // Try to set-up another cookie and update the previous cookie. |
| 3118 | { |
| 3119 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3120 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3121 | TestDelegate d; |
| 3122 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3123 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3124 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3125 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3126 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3127 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3128 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3129 | |
| 3130 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3131 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 3132 | } |
| 3133 | |
| 3134 | // Verify the cookies weren't saved or updated. |
| 3135 | { |
| 3136 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3137 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3138 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3139 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3140 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3141 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3142 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3143 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3144 | |
| 3145 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 3146 | == std::string::npos); |
| 3147 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 3148 | != std::string::npos); |
| 3149 | |
| 3150 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3151 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3152 | } |
| 3153 | } |
| 3154 | |
mkwst | c5fa776 | 2016-03-28 09:28:23 | [diff] [blame] | 3155 | TEST_F(URLRequestTest, SameSiteCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3156 | HttpTestServer test_server; |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3157 | ASSERT_TRUE(test_server.Start()); |
| 3158 | |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3159 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3160 | default_context().set_network_delegate(&network_delegate); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3161 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3162 | const std::string kHost = "example.test"; |
| 3163 | const std::string kSubHost = "subdomain.example.test"; |
| 3164 | const std::string kCrossHost = "cross-origin.test"; |
| 3165 | |
| 3166 | // Set up two 'SameSite' cookies on 'example.test' |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3167 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3168 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3169 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3170 | test_server.GetURL(kHost, |
| 3171 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 3172 | "LaxSameSiteCookie=1;SameSite=Lax"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3173 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3174 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
| 3175 | req->set_initiator(url::Origin::Create(test_server.GetURL(kHost, "/"))); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3176 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3177 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3178 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3179 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3180 | EXPECT_EQ(2, network_delegate.set_cookie_count()); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3181 | } |
| 3182 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3183 | // Verify that both cookies are sent for same-site requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3184 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3185 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3186 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3187 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3188 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3189 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3190 | req->set_initiator(url::Origin::Create(test_server.GetURL(kHost, "/"))); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3191 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3192 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3193 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3194 | EXPECT_NE(std::string::npos, |
| 3195 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3196 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3197 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3198 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3199 | } |
| 3200 | |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3201 | // Verify that both cookies are sent when the request has no initiator (can |
| 3202 | // happen for main frame browser-initiated navigations). |
| 3203 | { |
| 3204 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3205 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3206 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3207 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3208 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3209 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3210 | d.RunUntilComplete(); |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3211 | |
| 3212 | EXPECT_NE(std::string::npos, |
| 3213 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3214 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 3215 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3216 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3217 | } |
| 3218 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3219 | // Verify that both cookies are sent for same-registrable-domain requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3220 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3221 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3222 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3223 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3224 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3225 | req->set_site_for_cookies(test_server.GetURL(kSubHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3226 | req->set_initiator(url::Origin::Create(test_server.GetURL(kSubHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3227 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3228 | d.RunUntilComplete(); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3229 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3230 | EXPECT_NE(std::string::npos, |
| 3231 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3232 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3233 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3234 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3235 | } |
| 3236 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3237 | // Verify that neither cookie is not sent for cross-site requests. |
| 3238 | { |
| 3239 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3240 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3241 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3242 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3243 | req->set_site_for_cookies(test_server.GetURL(kCrossHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3244 | req->set_initiator( |
| 3245 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3246 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3247 | d.RunUntilComplete(); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3248 | |
| 3249 | EXPECT_EQ(std::string::npos, |
| 3250 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3251 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 3252 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3253 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3254 | } |
| 3255 | |
| 3256 | // Verify that the lax cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3257 | // method is "safe". |
| 3258 | { |
| 3259 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3260 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3261 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3262 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3263 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3264 | req->set_initiator( |
| 3265 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3266 | req->set_method("GET"); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3267 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3268 | d.RunUntilComplete(); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3269 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3270 | EXPECT_EQ(std::string::npos, |
| 3271 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3272 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3273 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3274 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3275 | } |
| 3276 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3277 | // Verify that neither cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3278 | // method is unsafe (e.g. POST). |
| 3279 | { |
| 3280 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3281 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3282 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3283 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3284 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3285 | req->set_initiator( |
| 3286 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3287 | req->set_method("POST"); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3288 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3289 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3290 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3291 | EXPECT_EQ(std::string::npos, |
| 3292 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3293 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3294 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3295 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3296 | } |
| 3297 | } |
| 3298 | |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3299 | TEST_F(URLRequestTest, SettingSameSiteCookies) { |
| 3300 | HttpTestServer test_server; |
| 3301 | ASSERT_TRUE(test_server.Start()); |
| 3302 | |
| 3303 | TestNetworkDelegate network_delegate; |
| 3304 | default_context().set_network_delegate(&network_delegate); |
| 3305 | |
| 3306 | const std::string kHost = "example.test"; |
| 3307 | const std::string kSubHost = "subdomain.example.test"; |
| 3308 | const std::string kCrossHost = "cross-origin.test"; |
| 3309 | |
| 3310 | int expected_cookies = 0; |
| 3311 | |
| 3312 | { |
| 3313 | TestDelegate d; |
| 3314 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 3315 | test_server.GetURL(kHost, |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3316 | "/set-cookie?Strict1=1;SameSite=Strict&" |
| 3317 | "Lax1=1;SameSite=Lax"), |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3318 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3319 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
| 3320 | req->set_initiator(url::Origin::Create(test_server.GetURL(kHost, "/"))); |
| 3321 | |
| 3322 | // 'SameSite' cookies are settable from strict same-site contexts |
| 3323 | // (same-origin site_for_cookies, same-origin initiator), so this request |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3324 | // should result in two cookies being set. |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3325 | expected_cookies += 2; |
| 3326 | |
| 3327 | req->Start(); |
| 3328 | d.RunUntilComplete(); |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3329 | EXPECT_EQ(expected_cookies, |
| 3330 | static_cast<int>(GetAllCookies(&default_context()).size())); |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3331 | EXPECT_EQ(expected_cookies, network_delegate.set_cookie_count()); |
| 3332 | } |
| 3333 | |
| 3334 | { |
| 3335 | TestDelegate d; |
| 3336 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 3337 | test_server.GetURL(kHost, |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3338 | "/set-cookie?Strict2=1;SameSite=Strict&" |
| 3339 | "Lax2=1;SameSite=Lax"), |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3340 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3341 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
| 3342 | req->set_initiator( |
| 3343 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
| 3344 | |
| 3345 | // 'SameSite' cookies are settable from lax same-site contexts (same-origin |
| 3346 | // site_for_cookies, cross-site initiator), so this request should result in |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3347 | // two cookies being set. |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3348 | expected_cookies += 2; |
| 3349 | |
| 3350 | req->Start(); |
| 3351 | d.RunUntilComplete(); |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3352 | EXPECT_EQ(expected_cookies, |
| 3353 | static_cast<int>(GetAllCookies(&default_context()).size())); |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3354 | EXPECT_EQ(expected_cookies, network_delegate.set_cookie_count()); |
| 3355 | } |
| 3356 | |
| 3357 | { |
| 3358 | TestDelegate d; |
| 3359 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 3360 | test_server.GetURL(kHost, |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3361 | "/set-cookie?Strict3=1;SameSite=Strict&" |
| 3362 | "Lax3=1;SameSite=Lax"), |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3363 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3364 | req->set_site_for_cookies(test_server.GetURL(kSubHost, "/")); |
| 3365 | req->set_initiator( |
| 3366 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
| 3367 | |
| 3368 | // 'SameSite' cookies are settable from lax same-site contexts (same-site |
| 3369 | // site_for_cookies, cross-site initiator), so this request should result in |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3370 | // two cookies being set. |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3371 | expected_cookies += 2; |
| 3372 | |
| 3373 | req->Start(); |
| 3374 | d.RunUntilComplete(); |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3375 | EXPECT_EQ(expected_cookies, |
| 3376 | static_cast<int>(GetAllCookies(&default_context()).size())); |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3377 | EXPECT_EQ(expected_cookies, network_delegate.set_cookie_count()); |
| 3378 | } |
| 3379 | |
| 3380 | { |
| 3381 | TestDelegate d; |
| 3382 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 3383 | test_server.GetURL(kHost, |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3384 | "/set-cookie?Strict4=1;SameSite=Strict&" |
| 3385 | "Lax4=1;SameSite=Lax"), |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3386 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3387 | req->set_site_for_cookies(test_server.GetURL(kSubHost, "/")); |
| 3388 | |
| 3389 | // 'SameSite' cookies are settable from strict same-site contexts (same-site |
| 3390 | // site_for_cookies, no initiator), so this request should result in two |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3391 | // cookies being set. |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3392 | expected_cookies += 2; |
| 3393 | |
| 3394 | req->Start(); |
| 3395 | d.RunUntilComplete(); |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3396 | EXPECT_EQ(expected_cookies, |
| 3397 | static_cast<int>(GetAllCookies(&default_context()).size())); |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3398 | EXPECT_EQ(expected_cookies, network_delegate.set_cookie_count()); |
| 3399 | } |
| 3400 | |
| 3401 | { |
| 3402 | TestDelegate d; |
| 3403 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 3404 | test_server.GetURL(kHost, |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3405 | "/set-cookie?Strict5=1;SameSite=Strict&" |
| 3406 | "Lax5=1;SameSite=Lax"), |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3407 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3408 | req->set_site_for_cookies(test_server.GetURL(kCrossHost, "/")); |
| 3409 | req->set_initiator( |
| 3410 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
| 3411 | |
| 3412 | // 'SameSite' cookies are not settable from cross-site contexts, so this |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3413 | // should not result in any new cookies being set. |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3414 | expected_cookies += 0; |
| 3415 | |
| 3416 | req->Start(); |
| 3417 | d.RunUntilComplete(); |
Lily Chen | f5732c8 | 2019-08-01 00:01:54 | [diff] [blame] | 3418 | // This counts the number of cookies actually set. |
| 3419 | EXPECT_EQ(expected_cookies, |
| 3420 | static_cast<int>(GetAllCookies(&default_context()).size())); |
| 3421 | // This counts the number of successful calls to CanSetCookie() when |
| 3422 | // attempting to set a cookie. The two cookies above were created and |
| 3423 | // attempted to be set, and were not rejected by the NetworkDelegate, so the |
| 3424 | // count here is 2 more than the number of cookies actually set. |
| 3425 | EXPECT_EQ(expected_cookies + 2, network_delegate.set_cookie_count()); |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3426 | } |
| 3427 | } |
| 3428 | |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3429 | // Tests that __Secure- cookies can't be set on non-secure origins. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3430 | TEST_F(URLRequestTest, SecureCookiePrefixOnNonsecureOrigin) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3431 | EmbeddedTestServer http_server; |
| 3432 | http_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3433 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3434 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3435 | https_server.AddDefaultHandlers( |
| 3436 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3437 | ASSERT_TRUE(http_server.Start()); |
| 3438 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3439 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3440 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3441 | TestURLRequestContext context(true); |
| 3442 | context.set_network_delegate(&network_delegate); |
| 3443 | context.Init(); |
| 3444 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3445 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3446 | { |
| 3447 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3448 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3449 | http_server.GetURL("/set-cookie?__Secure-nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3450 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3451 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3452 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3453 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3454 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3455 | } |
| 3456 | |
| 3457 | // Verify that the cookie is not set. |
| 3458 | { |
| 3459 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3460 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3461 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3462 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3463 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3464 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3465 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3466 | EXPECT_EQ(d.data_received().find("__Secure-nonsecure-origin=1"), |
| 3467 | std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3468 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3469 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3470 | } |
| 3471 | } |
| 3472 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3473 | TEST_F(URLRequestTest, SecureCookiePrefixNonsecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3474 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3475 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3476 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3477 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3478 | |
| 3479 | TestNetworkDelegate network_delegate; |
| 3480 | TestURLRequestContext context(true); |
| 3481 | context.set_network_delegate(&network_delegate); |
| 3482 | context.Init(); |
| 3483 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3484 | // Try to set a non-Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3485 | { |
| 3486 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3487 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 3488 | https_server.GetURL("/set-cookie?__Secure-foo=1"), DEFAULT_PRIORITY, &d, |
| 3489 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3490 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3491 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3492 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3493 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3494 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3495 | } |
| 3496 | |
| 3497 | // Verify that the cookie is not set. |
| 3498 | { |
| 3499 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3500 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3501 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3502 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3503 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3504 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3505 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3506 | EXPECT_EQ(d.data_received().find("__Secure-foo=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3507 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3508 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3509 | } |
| 3510 | } |
| 3511 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3512 | TEST_F(URLRequestTest, SecureCookiePrefixSecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3513 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3514 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3515 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3516 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3517 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3518 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3519 | TestURLRequestContext context(true); |
| 3520 | context.set_network_delegate(&network_delegate); |
| 3521 | context.Init(); |
| 3522 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3523 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3524 | { |
| 3525 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3526 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3527 | https_server.GetURL("/set-cookie?__Secure-bar=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3528 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3529 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3530 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3531 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3532 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3533 | } |
| 3534 | |
| 3535 | // Verify that the cookie is set. |
| 3536 | { |
| 3537 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3538 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3539 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3540 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3541 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3542 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3543 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3544 | EXPECT_NE(d.data_received().find("__Secure-bar=1"), std::string::npos); |
| 3545 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3546 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3547 | } |
| 3548 | } |
| 3549 | |
| 3550 | // Tests that secure cookies can't be set on non-secure origins if strict secure |
| 3551 | // cookies are enabled. |
| 3552 | TEST_F(URLRequestTest, StrictSecureCookiesOnNonsecureOrigin) { |
| 3553 | EmbeddedTestServer http_server; |
| 3554 | http_server.AddDefaultHandlers( |
| 3555 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3556 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3557 | https_server.AddDefaultHandlers( |
| 3558 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3559 | ASSERT_TRUE(http_server.Start()); |
| 3560 | ASSERT_TRUE(https_server.Start()); |
| 3561 | |
Matt Menke | be9b6eb | 2018-10-18 12:11:44 | [diff] [blame] | 3562 | TestNetworkDelegate network_delegate; |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3563 | TestURLRequestContext context(true); |
| 3564 | context.set_network_delegate(&network_delegate); |
| 3565 | context.Init(); |
| 3566 | |
| 3567 | // Try to set a Secure cookie, with experimental features enabled. |
| 3568 | { |
| 3569 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3570 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3571 | http_server.GetURL("/set-cookie?nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3572 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3573 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3574 | d.RunUntilComplete(); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3575 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3576 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3577 | } |
| 3578 | |
| 3579 | // Verify that the cookie is not set. |
| 3580 | { |
| 3581 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3582 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3583 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3584 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3585 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3586 | d.RunUntilComplete(); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3587 | |
| 3588 | EXPECT_EQ(d.data_received().find("nonsecure-origin=1"), std::string::npos); |
| 3589 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3590 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3591 | } |
| 3592 | } |
| 3593 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3594 | // The parameter is true for same-site and false for cross-site requests. |
| 3595 | class URLRequestTestParameterizedSameSite |
| 3596 | : public URLRequestTest, |
| 3597 | public ::testing::WithParamInterface<bool> { |
| 3598 | protected: |
| 3599 | URLRequestTestParameterizedSameSite() { |
| 3600 | auto params = std::make_unique<HttpNetworkSession::Params>(); |
| 3601 | params->ignore_certificate_errors = true; |
| 3602 | context_.set_http_network_session_params(std::move(params)); |
| 3603 | context_.set_network_delegate(&network_delegate_); |
| 3604 | https_server_.AddDefaultHandlers( |
| 3605 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3606 | EXPECT_TRUE(https_server_.Start()); |
| 3607 | } |
| 3608 | |
| 3609 | // To be called after configuration of |context_| has been finalized. |
| 3610 | void InitContext() { context_.Init(); } |
| 3611 | |
| 3612 | const std::string kHost_ = "example.test"; |
| 3613 | const std::string kCrossHost_ = "cross-site.test"; |
| 3614 | TestURLRequestContext context_{true}; |
| 3615 | TestNetworkDelegate network_delegate_; |
| 3616 | base::HistogramTester histograms_; |
| 3617 | EmbeddedTestServer https_server_{EmbeddedTestServer::TYPE_HTTPS}; |
| 3618 | }; |
| 3619 | |
Victor Costan | 8fb98f6f | 2019-02-01 17:08:29 | [diff] [blame] | 3620 | INSTANTIATE_TEST_SUITE_P(URLRequestTest, |
| 3621 | URLRequestTestParameterizedSameSite, |
| 3622 | ::testing::Bool()); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3623 | |
| 3624 | TEST_P(URLRequestTestParameterizedSameSite, CookieAgeMetrics) { |
| 3625 | const bool same_site = GetParam(); |
| 3626 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3627 | InitContext(); |
| 3628 | |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3629 | EmbeddedTestServer http_server; |
| 3630 | http_server.AddDefaultHandlers( |
| 3631 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3632 | ASSERT_TRUE(http_server.Start()); |
| 3633 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3634 | // Set two test cookies. |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3635 | { |
| 3636 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3637 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3638 | http_server.GetURL(kHost_, "/set-cookie?cookie=value&cookie2=value2"), |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3639 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3640 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3641 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3642 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3643 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3644 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 0); |
| 3645 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", 0); |
| 3646 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", 0); |
| 3647 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3648 | 0); |
| 3649 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3650 | 0); |
| 3651 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", 0); |
| 3652 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", 0); |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3653 | } |
| 3654 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3655 | // Make a secure request. |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3656 | { |
| 3657 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3658 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3659 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3660 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3661 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3662 | req->set_initiator( |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3663 | url::Origin::Create(https_server_.GetURL(kInitiatingHost, "/"))); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3664 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3665 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3666 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3667 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 0); |
| 3668 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", |
| 3669 | !same_site); |
| 3670 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", |
| 3671 | same_site); |
| 3672 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3673 | 0); |
| 3674 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3675 | 0); |
| 3676 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", |
| 3677 | same_site ? 0 : 2); |
| 3678 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", |
| 3679 | same_site ? 2 : 0); |
| 3680 | EXPECT_TRUE(d.data_received().find("cookie=value") != std::string::npos); |
| 3681 | EXPECT_TRUE(d.data_received().find("cookie2=value2") != std::string::npos); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3682 | } |
| 3683 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3684 | // Make a non-secure request. |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3685 | { |
| 3686 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3687 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3688 | http_server.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3689 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3690 | req->set_site_for_cookies(http_server.GetURL(kInitiatingHost, "/")); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3691 | req->set_initiator( |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3692 | url::Origin::Create(http_server.GetURL(kInitiatingHost, "/"))); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3693 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3694 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3695 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", |
| 3696 | !same_site); |
| 3697 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", |
| 3698 | same_site); |
| 3699 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", |
| 3700 | !same_site); |
| 3701 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", |
| 3702 | same_site); |
| 3703 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3704 | same_site ? 0 : 2); |
| 3705 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3706 | same_site ? 2 : 0); |
| 3707 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", |
| 3708 | same_site ? 0 : 2); |
| 3709 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", |
| 3710 | same_site ? 2 : 0); |
| 3711 | EXPECT_TRUE(d.data_received().find("cookie=value") != std::string::npos); |
| 3712 | EXPECT_TRUE(d.data_received().find("cookie2=value2") != std::string::npos); |
| 3713 | } |
| 3714 | } |
| 3715 | |
| 3716 | // Cookies with secure attribute (no HSTS) --> k1pSecureAttribute |
| 3717 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3718 | CookieNetworkSecurityMetricSecureAttribute) { |
| 3719 | const bool same_site = GetParam(); |
| 3720 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3721 | InitContext(); |
| 3722 | |
| 3723 | // Set cookies. |
| 3724 | { |
| 3725 | TestDelegate d; |
| 3726 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3727 | https_server_.GetURL(kHost_, |
| 3728 | "/set-cookie?session-cookie=value;Secure&" |
| 3729 | "longlived-cookie=value;Secure;domain=" + |
| 3730 | kHost_ + ";Max-Age=360000"), |
| 3731 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3732 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3733 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3734 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3735 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3736 | } |
| 3737 | |
| 3738 | // Verify that the cookies fall into the correct metrics bucket. |
| 3739 | { |
| 3740 | TestDelegate d; |
| 3741 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3742 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3743 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3744 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3745 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3746 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3747 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3748 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3749 | // Static cast of boolean required for MSVC 1911. |
| 3750 | histograms_.ExpectBucketCount( |
| 3751 | "Cookie.NetworkSecurity", |
| 3752 | static_cast<int>(CookieNetworkSecurity::k1pSecureAttribute) | |
| 3753 | static_cast<int>(!same_site), |
| 3754 | 2); |
| 3755 | } |
| 3756 | } |
| 3757 | |
| 3758 | // Short-lived host cookie --> k1pHSTSHostCookie |
| 3759 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3760 | CookieNetworkSecurityMetricShortlivedHostCookie) { |
| 3761 | const bool same_site = GetParam(); |
| 3762 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3763 | |
| 3764 | TransportSecurityState transport_security_state; |
| 3765 | transport_security_state.AddHSTS( |
| 3766 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3767 | false /* include_subdomains */); |
| 3768 | context_.set_transport_security_state(&transport_security_state); |
| 3769 | InitContext(); |
| 3770 | |
| 3771 | // Set cookie. |
| 3772 | { |
| 3773 | TestDelegate d; |
| 3774 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3775 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;Max-Age=3600"), |
| 3776 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3777 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3778 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3779 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3780 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3781 | } |
| 3782 | |
| 3783 | // Verify that the cookie falls into the correct metrics bucket. |
| 3784 | { |
| 3785 | TestDelegate d; |
| 3786 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3787 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3788 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3789 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3790 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3791 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3792 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3793 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3794 | // Static cast of boolean required for MSVC 1911. |
| 3795 | histograms_.ExpectBucketCount( |
| 3796 | "Cookie.NetworkSecurity", |
| 3797 | static_cast<int>(CookieNetworkSecurity::k1pHSTSHostCookie) | |
| 3798 | static_cast<int>(!same_site), |
| 3799 | 1); |
| 3800 | } |
| 3801 | } |
| 3802 | |
| 3803 | // Long-lived (either due to expiry or due to being a session cookie) host |
| 3804 | // cookies --> k1pExpiringHSTSHostCookie |
| 3805 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3806 | CookieNetworkSecurityMetricLonglivedHostCookie) { |
| 3807 | const bool same_site = GetParam(); |
| 3808 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3809 | |
| 3810 | TransportSecurityState transport_security_state; |
| 3811 | transport_security_state.AddHSTS( |
| 3812 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3813 | false /* include_subdomains */); |
| 3814 | context_.set_transport_security_state(&transport_security_state); |
| 3815 | InitContext(); |
| 3816 | |
| 3817 | // Set cookies. |
| 3818 | { |
| 3819 | TestDelegate d; |
| 3820 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3821 | https_server_.GetURL(kHost_, |
| 3822 | "/set-cookie?session-cookie=value&" |
| 3823 | "longlived-cookie=value;Max-Age=360000"), |
| 3824 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3825 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3826 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3827 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3828 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3829 | } |
| 3830 | |
| 3831 | // Verify that the cookies fall into the correct metrics bucket. |
| 3832 | { |
| 3833 | TestDelegate d; |
| 3834 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3835 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3836 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3837 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3838 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3839 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3840 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3841 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3842 | // Static cast of boolean required for MSVC 1911. |
| 3843 | histograms_.ExpectBucketCount( |
| 3844 | "Cookie.NetworkSecurity", |
| 3845 | static_cast<int>(CookieNetworkSecurity::k1pExpiringHSTSHostCookie) | |
| 3846 | static_cast<int>(!same_site), |
| 3847 | 2); |
| 3848 | } |
| 3849 | } |
| 3850 | |
| 3851 | // Domain cookie with HSTS subdomains with cookie expiry before HSTS expiry --> |
| 3852 | // k1pHSTSSubdomainsIncluded |
| 3853 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3854 | CookieNetworkSecurityMetricShortlivedDomainCookie) { |
| 3855 | const bool same_site = GetParam(); |
| 3856 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3857 | |
| 3858 | TransportSecurityState transport_security_state; |
| 3859 | transport_security_state.AddHSTS( |
| 3860 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3861 | true /* include_subdomains */); |
| 3862 | context_.set_transport_security_state(&transport_security_state); |
| 3863 | InitContext(); |
| 3864 | |
| 3865 | // Set cookie. |
| 3866 | { |
| 3867 | TestDelegate d; |
| 3868 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3869 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;domain=" + |
| 3870 | kHost_ + ";Max-Age=3600"), |
| 3871 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3872 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3873 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3874 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3875 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3876 | } |
| 3877 | |
| 3878 | // Verify that the cookie falls into the correct metrics bucket. |
| 3879 | { |
| 3880 | TestDelegate d; |
| 3881 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3882 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3883 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3884 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3885 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3886 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3887 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3888 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3889 | // Static cast of boolean required for MSVC 1911. |
| 3890 | histograms_.ExpectBucketCount( |
| 3891 | "Cookie.NetworkSecurity", |
| 3892 | static_cast<int>(CookieNetworkSecurity::k1pHSTSSubdomainsIncluded) | |
| 3893 | static_cast<int>(!same_site), |
| 3894 | 1); |
| 3895 | } |
| 3896 | } |
| 3897 | |
| 3898 | // Long-lived (either due to expiry or due to being a session cookie) domain |
| 3899 | // cookies with HSTS subdomains --> k1pExpiringHSTSSubdomainsIncluded |
| 3900 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3901 | CookieNetworkSecurityMetricLonglivedDomainCookie) { |
| 3902 | const bool same_site = GetParam(); |
| 3903 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3904 | |
| 3905 | TransportSecurityState transport_security_state; |
| 3906 | transport_security_state.AddHSTS( |
| 3907 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3908 | true /* include_subdomains */); |
| 3909 | context_.set_transport_security_state(&transport_security_state); |
| 3910 | InitContext(); |
| 3911 | |
| 3912 | // Set cookies. |
| 3913 | { |
| 3914 | TestDelegate d; |
| 3915 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3916 | https_server_.GetURL( |
| 3917 | kHost_, "/set-cookie?session-cookie=value;domain=" + kHost_ + "&" + |
| 3918 | "longlived-cookie=value;domain=" + kHost_ + |
| 3919 | ";Max-Age=360000"), |
| 3920 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3921 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3922 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3923 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3924 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3925 | } |
| 3926 | |
| 3927 | // Verify that the cookies fall into the correct metrics bucket. |
| 3928 | { |
| 3929 | TestDelegate d; |
| 3930 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3931 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3932 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3933 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3934 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3935 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3936 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3937 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3938 | // Static cast of boolean required for MSVC 1911. |
| 3939 | histograms_.ExpectBucketCount( |
| 3940 | "Cookie.NetworkSecurity", |
| 3941 | static_cast<int>( |
| 3942 | CookieNetworkSecurity::k1pExpiringHSTSSubdomainsIncluded) | |
| 3943 | static_cast<int>(!same_site), |
| 3944 | 2); |
| 3945 | } |
| 3946 | } |
| 3947 | |
| 3948 | // Domain cookie with HSTS subdomains not included --> k1pHSTSSpoofable |
| 3949 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3950 | CookieNetworkSecurityMetricSpoofableDomainCookie) { |
| 3951 | const bool same_site = GetParam(); |
| 3952 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3953 | |
| 3954 | TransportSecurityState transport_security_state; |
| 3955 | transport_security_state.AddHSTS( |
| 3956 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3957 | false /* include_subdomains */); |
| 3958 | context_.set_transport_security_state(&transport_security_state); |
| 3959 | InitContext(); |
| 3960 | |
| 3961 | // Set cookie. |
| 3962 | { |
| 3963 | TestDelegate d; |
| 3964 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3965 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;domain=" + |
| 3966 | kHost_ + ";Max-Age=3600"), |
| 3967 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3968 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3969 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3970 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3971 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3972 | } |
| 3973 | |
| 3974 | // Verify that the cookie falls into the correct metrics bucket. |
| 3975 | { |
| 3976 | TestDelegate d; |
| 3977 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3978 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3979 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3980 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3981 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3982 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3983 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3984 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3985 | // Static cast of boolean required for MSVC 1911. |
| 3986 | histograms_.ExpectBucketCount( |
| 3987 | "Cookie.NetworkSecurity", |
| 3988 | static_cast<int>(CookieNetworkSecurity::k1pHSTSSpoofable) | |
| 3989 | static_cast<int>(!same_site), |
| 3990 | 1); |
| 3991 | } |
| 3992 | } |
| 3993 | |
| 3994 | // Cookie without HSTS --> k1p(Non)SecureConnection |
| 3995 | TEST_P(URLRequestTestParameterizedSameSite, CookieNetworkSecurityMetricNoHSTS) { |
| 3996 | const bool same_site = GetParam(); |
| 3997 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3998 | InitContext(); |
| 3999 | |
| 4000 | EmbeddedTestServer http_server; |
| 4001 | http_server.AddDefaultHandlers( |
| 4002 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 4003 | ASSERT_TRUE(http_server.Start()); |
| 4004 | |
| 4005 | // Set cookies. |
| 4006 | { |
| 4007 | TestDelegate d; |
| 4008 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 4009 | https_server_.GetURL(kHost_, |
| 4010 | "/set-cookie?cookie=value;domain=" + kHost_ + |
| 4011 | ";Max-Age=3600&host-cookie=value"), |
| 4012 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 4013 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4014 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 4015 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 4016 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 4017 | } |
| 4018 | |
| 4019 | // Verify that the cookie falls into the correct metrics bucket. |
| 4020 | { |
| 4021 | TestDelegate d; |
| 4022 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 4023 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 4024 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 4025 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 4026 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 4027 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4028 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 4029 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 4030 | // Static cast of boolean required for MSVC 1911. |
| 4031 | histograms_.ExpectBucketCount( |
| 4032 | "Cookie.NetworkSecurity", |
| 4033 | static_cast<int>(CookieNetworkSecurity::k1pSecureConnection) | |
| 4034 | static_cast<int>(!same_site), |
| 4035 | 2); |
| 4036 | } |
| 4037 | |
| 4038 | // Verify that the cookie falls into the correct metrics bucket. |
| 4039 | { |
| 4040 | TestDelegate d; |
| 4041 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 4042 | http_server.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 4043 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 4044 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 4045 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 4046 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4047 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 4048 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 4); |
| 4049 | // Static cast of boolean required for MSVC 1911. |
| 4050 | histograms_.ExpectBucketCount( |
| 4051 | "Cookie.NetworkSecurity", |
| 4052 | static_cast<int>(CookieNetworkSecurity::k1pSecureConnection) | |
| 4053 | static_cast<int>(!same_site), |
| 4054 | 2); |
| 4055 | // Static cast of boolean required for MSVC 1911. |
| 4056 | histograms_.ExpectBucketCount( |
| 4057 | "Cookie.NetworkSecurity", |
| 4058 | static_cast<int>(CookieNetworkSecurity::k1pNonsecureConnection) | |
| 4059 | static_cast<int>(!same_site), |
| 4060 | 2); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 4061 | } |
| 4062 | } |
| 4063 | |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4064 | // FixedDateNetworkDelegate swaps out the server's HTTP Date response header |
| 4065 | // value for the |fixed_date| argument given to the constructor. |
| 4066 | class FixedDateNetworkDelegate : public TestNetworkDelegate { |
| 4067 | public: |
| 4068 | explicit FixedDateNetworkDelegate(const std::string& fixed_date) |
| 4069 | : fixed_date_(fixed_date) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 4070 | ~FixedDateNetworkDelegate() override = default; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4071 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4072 | // NetworkDelegate implementation |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4073 | int OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4074 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 4075 | CompletionOnceCallback callback, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4076 | const HttpResponseHeaders* original_response_headers, |
| 4077 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 4078 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4079 | |
| 4080 | private: |
| 4081 | std::string fixed_date_; |
| 4082 | |
| 4083 | DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); |
| 4084 | }; |
| 4085 | |
| 4086 | int FixedDateNetworkDelegate::OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4087 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 4088 | CompletionOnceCallback callback, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4089 | const HttpResponseHeaders* original_response_headers, |
| 4090 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4091 | GURL* allowed_unsafe_redirect_url) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4092 | HttpResponseHeaders* new_response_headers = |
| 4093 | new HttpResponseHeaders(original_response_headers->raw_headers()); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4094 | |
| 4095 | new_response_headers->RemoveHeader("Date"); |
| 4096 | new_response_headers->AddHeader("Date: " + fixed_date_); |
| 4097 | |
| 4098 | *override_response_headers = new_response_headers; |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 4099 | return TestNetworkDelegate::OnHeadersReceived( |
| 4100 | request, std::move(callback), original_response_headers, |
| 4101 | override_response_headers, allowed_unsafe_redirect_url); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | // Test that cookie expiration times are adjusted for server/client clock |
| 4105 | // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date |
| 4106 | // headers by defaulting to GMT. (crbug.com/135131) |
| 4107 | TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4108 | HttpTestServer test_server; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4109 | ASSERT_TRUE(test_server.Start()); |
| 4110 | |
| 4111 | // Set up an expired cookie. |
| 4112 | { |
| 4113 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4114 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4115 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4116 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4117 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4118 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4119 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4120 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4121 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4122 | } |
| 4123 | // Verify that the cookie is not set. |
| 4124 | { |
| 4125 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4126 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4127 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4128 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4129 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 4130 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4131 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4132 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4133 | |
| 4134 | EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos); |
| 4135 | } |
| 4136 | // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier. |
| 4137 | { |
| 4138 | FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC"); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4139 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4140 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4141 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4142 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4143 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4144 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4145 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4146 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4147 | } |
| 4148 | // Verify that the cookie is set. |
| 4149 | { |
| 4150 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4151 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4152 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4153 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4154 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 4155 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4156 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4157 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4158 | |
| 4159 | EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos); |
| 4160 | } |
| 4161 | } |
| 4162 | |
| 4163 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4164 | // Check that it is impossible to change the referrer in the extra headers of |
| 4165 | // an URLRequest. |
| 4166 | TEST_F(URLRequestTest, DoNotOverrideReferrer) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4167 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4168 | ASSERT_TRUE(test_server.Start()); |
| 4169 | |
| 4170 | // If extra headers contain referer and the request contains a referer, |
| 4171 | // only the latter shall be respected. |
| 4172 | { |
| 4173 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4174 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4175 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 4176 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4177 | req->SetReferrer("http://foo.com/"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4178 | |
| 4179 | HttpRequestHeaders headers; |
| 4180 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4181 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4182 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4183 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4184 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4185 | |
| 4186 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 4187 | } |
| 4188 | |
| 4189 | // If extra headers contain a referer but the request does not, no referer |
| 4190 | // shall be sent in the header. |
| 4191 | { |
| 4192 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4193 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4194 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 4195 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4196 | |
| 4197 | HttpRequestHeaders headers; |
| 4198 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4199 | req->SetExtraRequestHeaders(headers); |
| 4200 | req->SetLoadFlags(LOAD_VALIDATE_CACHE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4201 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4202 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4203 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4204 | |
| 4205 | EXPECT_EQ("None", d.data_received()); |
| 4206 | } |
| 4207 | } |
| 4208 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4209 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4210 | public: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 4211 | URLRequestTestHTTP() : test_server_(base::FilePath(kTestFilePath)) {} |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4212 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4213 | protected: |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4214 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 4215 | // |request_method| is the method to use for the initial request. |
| 4216 | // |redirect_method| is the method that is expected to be used for the second |
| 4217 | // request, after redirection. |
| 4218 | // If |include_data| is true, data is uploaded with the request. The |
| 4219 | // response body is expected to match it exactly, if and only if |
| 4220 | // |request_method| == |redirect_method|. |
| 4221 | void HTTPRedirectMethodTest(const GURL& redirect_url, |
| 4222 | const std::string& request_method, |
| 4223 | const std::string& redirect_method, |
| 4224 | bool include_data) { |
| 4225 | static const char kData[] = "hello world"; |
| 4226 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4227 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4228 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4229 | req->set_method(request_method); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4230 | if (include_data) { |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4231 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4232 | HttpRequestHeaders headers; |
| 4233 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 4234 | base::NumberToString(base::size(kData) - 1)); |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 4235 | headers.SetHeader(HttpRequestHeaders::kContentType, "text/plain"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4236 | req->SetExtraRequestHeaders(headers); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4237 | } |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4238 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4239 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4240 | EXPECT_EQ(redirect_method, req->method()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4241 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4242 | if (include_data) { |
| 4243 | if (request_method == redirect_method) { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 4244 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 4245 | HttpRequestHeaders::kContentLength)); |
| 4246 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 4247 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4248 | EXPECT_EQ(kData, d.data_received()); |
| 4249 | } else { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 4250 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 4251 | HttpRequestHeaders::kContentLength)); |
| 4252 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 4253 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4254 | EXPECT_NE(kData, d.data_received()); |
| 4255 | } |
| 4256 | } |
| 4257 | if (HasFailure()) |
| 4258 | LOG(WARNING) << "Request method was: " << request_method; |
| 4259 | } |
| 4260 | |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4261 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. It's also |
| 4262 | // used as the initial origin. |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4263 | // |request_method| is the method to use for the initial request. |
| 4264 | // |redirect_method| is the method that is expected to be used for the second |
| 4265 | // request, after redirection. |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4266 | // |expected_origin_value| is the expected value for the Origin header after |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4267 | // redirection. If empty, expects that there will be no Origin header. |
| 4268 | void HTTPRedirectOriginHeaderTest(const GURL& redirect_url, |
| 4269 | const std::string& request_method, |
| 4270 | const std::string& redirect_method, |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4271 | const std::string& expected_origin_value) { |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4272 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4273 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4274 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4275 | req->set_method(request_method); |
| 4276 | req->SetExtraRequestHeaderByName(HttpRequestHeaders::kOrigin, |
| 4277 | redirect_url.GetOrigin().spec(), false); |
| 4278 | req->Start(); |
| 4279 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4280 | d.RunUntilComplete(); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4281 | |
| 4282 | EXPECT_EQ(redirect_method, req->method()); |
| 4283 | // Note that there is no check for request success here because, for |
| 4284 | // purposes of testing, the request very well may fail. For example, if the |
| 4285 | // test redirects to an HTTPS server from an HTTP origin, thus it is cross |
| 4286 | // origin, there is not an HTTPS server in this unit test framework, so the |
| 4287 | // request would fail. However, that's fine, as long as the request headers |
| 4288 | // are in order and pass the checks below. |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4289 | if (expected_origin_value.empty()) { |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4290 | EXPECT_FALSE( |
| 4291 | req->extra_request_headers().HasHeader(HttpRequestHeaders::kOrigin)); |
| 4292 | } else { |
| 4293 | std::string origin_header; |
| 4294 | EXPECT_TRUE(req->extra_request_headers().GetHeader( |
| 4295 | HttpRequestHeaders::kOrigin, &origin_header)); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4296 | EXPECT_EQ(expected_origin_value, origin_header); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4297 | } |
| 4298 | } |
| 4299 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4300 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4301 | const int kMsgSize = 20000; // multiple of 10 |
| 4302 | const int kIterations = 50; |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 4303 | char* uploadBytes = new char[kMsgSize+1]; |
| 4304 | char* ptr = uploadBytes; |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4305 | char marker = 'a'; |
| 4306 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 4307 | memcpy(ptr, "----------", 10); |
| 4308 | ptr += 10; |
| 4309 | if (idx % 100 == 0) { |
| 4310 | ptr--; |
| 4311 | *ptr++ = marker; |
| 4312 | if (++marker > 'z') |
| 4313 | marker = 'a'; |
| 4314 | } |
| 4315 | } |
| 4316 | uploadBytes[kMsgSize] = '\0'; |
| 4317 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4318 | for (int i = 0; i < kIterations; ++i) { |
| 4319 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4320 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4321 | test_server_.GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 4322 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4323 | r->set_method(method.c_str()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4324 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4325 | r->set_upload(CreateSimpleUploadData(uploadBytes)); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4326 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4327 | r->Start(); |
| 4328 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4329 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4330 | d.RunUntilComplete(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4331 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4332 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 4333 | << d.request_status(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4334 | |
| 4335 | EXPECT_FALSE(d.received_data_before_response()); |
| 4336 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4337 | } |
| 4338 | delete[] uploadBytes; |
| 4339 | } |
| 4340 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 4341 | bool DoManyCookiesRequest(int num_cookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4342 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4343 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4344 | test_server_.GetURL("/set-many-cookies?" + |
Raul Tambre | 8c1981d | 2019-02-08 02:22:26 | [diff] [blame] | 4345 | base::NumberToString(num_cookies)), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4346 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4347 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4348 | r->Start(); |
| 4349 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4350 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4351 | d.RunUntilComplete(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4352 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4353 | if (d.request_status() != OK) { |
| 4354 | EXPECT_EQ(ERR_RESPONSE_HEADERS_TOO_BIG, d.request_status()); |
| 4355 | return false; |
| 4356 | } |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4357 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4358 | return true; |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4359 | } |
| 4360 | |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4361 | HttpTestServer* http_test_server() { return &test_server_; } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 4362 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4363 | private: |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4364 | HttpTestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4365 | }; |
| 4366 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4367 | namespace { |
| 4368 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4369 | std::unique_ptr<test_server::HttpResponse> HandleRedirectConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4370 | const test_server::HttpRequest& request) { |
| 4371 | if (request.headers.find("Host") == request.headers.end() || |
| 4372 | request.headers.at("Host") != "www.redirect.com" || |
| 4373 | request.method != test_server::METHOD_CONNECT) { |
| 4374 | return nullptr; |
| 4375 | } |
| 4376 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4377 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4378 | new test_server::BasicHttpResponse); |
| 4379 | http_response->set_code(HTTP_FOUND); |
| 4380 | http_response->AddCustomHeader("Location", |
| 4381 | "http://www.destination.com/foo.js"); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 4382 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4383 | } |
| 4384 | |
Nick Harper | 89bc721 | 2018-07-31 19:07:57 | [diff] [blame] | 4385 | } // namespace |
| 4386 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4387 | // 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] | 4388 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4389 | // The EmbeddedTestServer will return a 302 response, which we should not |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4390 | // follow. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 4391 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4392 | http_test_server()->RegisterRequestHandler( |
Matt Menke | 2b2a6047 | 2018-02-07 02:12:12 | [diff] [blame] | 4393 | base::BindRepeating(&HandleRedirectConnect)); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4394 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4395 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4396 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4397 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4398 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4399 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4400 | TestDelegate d; |
| 4401 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4402 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4403 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 4404 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4405 | r->Start(); |
| 4406 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4407 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4408 | d.RunUntilComplete(); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4409 | |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 4410 | // The proxy server should be set before failure. |
| 4411 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4412 | http_test_server()->host_port_pair()), |
| 4413 | r->proxy_server()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4414 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4415 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4416 | // We should not have followed the redirect. |
| 4417 | EXPECT_EQ(0, d.received_redirect_count()); |
| 4418 | } |
| 4419 | } |
| 4420 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4421 | // This is the same as the previous test, but checks that the network delegate |
| 4422 | // registers the error. |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 4423 | TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4424 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4425 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4426 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4427 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4428 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4429 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4430 | TestDelegate d; |
| 4431 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4432 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4433 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 4434 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4435 | r->Start(); |
| 4436 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4437 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4438 | d.RunUntilComplete(); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4439 | |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 4440 | // The proxy server should be set before failure. |
| 4441 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4442 | http_test_server()->host_port_pair()), |
| 4443 | r->proxy_server()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4444 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4445 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4446 | // We should not have followed the redirect. |
| 4447 | EXPECT_EQ(0, d.received_redirect_count()); |
| 4448 | |
| 4449 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4450 | EXPECT_THAT(network_delegate.last_error(), |
| 4451 | IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4452 | } |
| 4453 | } |
| 4454 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4455 | // Tests that we can block and asynchronously return OK in various stages. |
| 4456 | TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) { |
| 4457 | static const BlockingNetworkDelegate::Stage blocking_stages[] = { |
| 4458 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 4459 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 4460 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED |
| 4461 | }; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 4462 | static const size_t blocking_stages_length = base::size(blocking_stages); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4463 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4464 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4465 | |
| 4466 | TestDelegate d; |
| 4467 | BlockingNetworkDelegate network_delegate( |
| 4468 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4469 | network_delegate.set_block_on( |
| 4470 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST | |
| 4471 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS | |
| 4472 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 4473 | |
| 4474 | TestURLRequestContext context(true); |
| 4475 | context.set_network_delegate(&network_delegate); |
| 4476 | context.Init(); |
| 4477 | |
| 4478 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4479 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4480 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4481 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4482 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4483 | r->Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4484 | for (size_t i = 0; i < blocking_stages_length; ++i) { |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4485 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4486 | EXPECT_EQ(blocking_stages[i], |
| 4487 | network_delegate.stage_blocked_for_callback()); |
| 4488 | network_delegate.DoCallback(OK); |
| 4489 | } |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4490 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4491 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4492 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4493 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4494 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4495 | } |
| 4496 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4497 | } |
| 4498 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4499 | // Tests that the network delegate can block and cancel a request. |
| 4500 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4501 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4502 | |
| 4503 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4504 | BlockingNetworkDelegate network_delegate( |
| 4505 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4506 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 4507 | network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4508 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4509 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4510 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4511 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4512 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4513 | std::unique_ptr<URLRequest> r( |
| 4514 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4515 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4516 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4517 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4518 | d.RunUntilComplete(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4519 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4520 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4521 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4522 | EXPECT_EQ(ERR_EMPTY_RESPONSE, d.request_status()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4523 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4524 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4525 | } |
| 4526 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4527 | } |
| 4528 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4529 | // Helper function for NetworkDelegateCancelRequestAsynchronously and |
| 4530 | // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network |
| 4531 | // delegate operating in |block_mode| and a request for |url|. It blocks the |
| 4532 | // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT. |
| 4533 | void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode, |
| 4534 | BlockingNetworkDelegate::Stage stage, |
| 4535 | const GURL& url) { |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4536 | TestDelegate d; |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4537 | BlockingNetworkDelegate network_delegate(block_mode); |
| 4538 | network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT); |
| 4539 | network_delegate.set_block_on(stage); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4540 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4541 | TestURLRequestContext context(true); |
| 4542 | context.set_network_delegate(&network_delegate); |
| 4543 | context.Init(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4544 | |
| 4545 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4546 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4547 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4548 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4549 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4550 | d.RunUntilComplete(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4551 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4552 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4553 | if (stage == BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST || |
| 4554 | stage == BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS) { |
| 4555 | EXPECT_FALSE(r->proxy_server().is_valid()); |
| 4556 | } else if (stage == BlockingNetworkDelegate::ON_HEADERS_RECEIVED) { |
| 4557 | EXPECT_TRUE(r->proxy_server().is_direct()); |
| 4558 | } else { |
| 4559 | NOTREACHED(); |
| 4560 | } |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4561 | EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, d.request_status()); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4562 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4563 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4564 | } |
| 4565 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4566 | } |
| 4567 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4568 | // The following 3 tests check that the network delegate can cancel a request |
| 4569 | // synchronously in various stages of the request. |
| 4570 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4571 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4572 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4573 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4574 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4575 | } |
| 4576 | |
| 4577 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4578 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4579 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4580 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4581 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4582 | } |
| 4583 | |
| 4584 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4585 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4586 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4587 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4588 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4589 | } |
| 4590 | |
| 4591 | // The following 3 tests check that the network delegate can cancel a request |
| 4592 | // asynchronously in various stages of the request. |
| 4593 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4594 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4595 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4596 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4597 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4598 | } |
| 4599 | |
| 4600 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4601 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4602 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4603 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4604 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4605 | } |
| 4606 | |
| 4607 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4608 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4609 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4610 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4611 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4612 | } |
| 4613 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4614 | // Tests that the network delegate can block and redirect a request to a new |
| 4615 | // URL. |
| 4616 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4617 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4618 | |
| 4619 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4620 | BlockingNetworkDelegate network_delegate( |
| 4621 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4622 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4623 | GURL redirect_url("http://does.not.resolve.test/simple.html"); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4624 | network_delegate.set_redirect_url(redirect_url); |
| 4625 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4626 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4627 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4628 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4629 | { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4630 | GURL original_url("http://does.not.resolve.test/defaultresponse"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4631 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4632 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4633 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4634 | // Quit after hitting the redirect, so can check the headers. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4635 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 4636 | d.RunUntilRedirect(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4637 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4638 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4639 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4640 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4641 | std::string location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4642 | ASSERT_TRUE( |
| 4643 | r->response_headers()->EnumerateHeader(nullptr, "Location", &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4644 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4645 | |
| 4646 | // Let the request finish. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 4647 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 4648 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4649 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4650 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4651 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4652 | http_test_server()->host_port_pair()), |
| 4653 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4654 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4655 | // through an untunneled proxy. |
| 4656 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4657 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4658 | http_test_server()->host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 4659 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4660 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4661 | EXPECT_EQ(redirect_url, r->url()); |
| 4662 | EXPECT_EQ(original_url, r->original_url()); |
| 4663 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4664 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4665 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4666 | } |
| 4667 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4668 | } |
| 4669 | |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4670 | // Tests that the network delegate can block and redirect a request to a new |
| 4671 | // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 4672 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4673 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4674 | |
| 4675 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4676 | BlockingNetworkDelegate network_delegate( |
| 4677 | BlockingNetworkDelegate::SYNCHRONOUS); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4678 | GURL redirect_url("http://does.not.resolve.test/simple.html"); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4679 | network_delegate.set_redirect_url(redirect_url); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4680 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4681 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4682 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4683 | |
| 4684 | { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4685 | GURL original_url("http://does.not.resolve.test/defaultresponse"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4686 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4687 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4688 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4689 | // Quit after hitting the redirect, so can check the headers. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4690 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 4691 | d.RunUntilRedirect(); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4692 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4693 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4694 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4695 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4696 | std::string location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4697 | ASSERT_TRUE( |
| 4698 | r->response_headers()->EnumerateHeader(nullptr, "Location", &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4699 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4700 | |
| 4701 | // Let the request finish. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 4702 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 4703 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4704 | d.RunUntilComplete(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4705 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4706 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4707 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4708 | http_test_server()->host_port_pair()), |
| 4709 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4710 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4711 | // through an untunneled proxy. |
| 4712 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4713 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4714 | http_test_server()->host_port_pair())); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4715 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4716 | EXPECT_EQ(redirect_url, r->url()); |
| 4717 | EXPECT_EQ(original_url, r->original_url()); |
| 4718 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4719 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4720 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4721 | } |
| 4722 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4723 | } |
| 4724 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4725 | // Tests that redirects caused by the network delegate preserve POST data. |
| 4726 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4727 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4728 | |
| 4729 | const char kData[] = "hello world"; |
| 4730 | |
| 4731 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4732 | BlockingNetworkDelegate network_delegate( |
| 4733 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4734 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4735 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4736 | network_delegate.set_redirect_url(redirect_url); |
| 4737 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4738 | TestURLRequestContext context(true); |
| 4739 | context.set_network_delegate(&network_delegate); |
| 4740 | context.Init(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4741 | |
| 4742 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4743 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4744 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4745 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4746 | r->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4747 | r->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4748 | HttpRequestHeaders headers; |
| 4749 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 4750 | base::NumberToString(base::size(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4751 | r->SetExtraRequestHeaders(headers); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4752 | |
| 4753 | // Quit after hitting the redirect, so can check the headers. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4754 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 4755 | d.RunUntilRedirect(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4756 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4757 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4758 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4759 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4760 | std::string location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4761 | ASSERT_TRUE( |
| 4762 | r->response_headers()->EnumerateHeader(nullptr, "Location", &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4763 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4764 | |
| 4765 | // Let the request finish. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 4766 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 4767 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4768 | d.RunUntilComplete(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4769 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4770 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4771 | EXPECT_EQ(redirect_url, r->url()); |
| 4772 | EXPECT_EQ(original_url, r->original_url()); |
| 4773 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4774 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4775 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4776 | EXPECT_EQ("POST", r->method()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4777 | EXPECT_EQ(kData, d.data_received()); |
| 4778 | } |
| 4779 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4780 | } |
| 4781 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4782 | // Tests that the network delegate can block and redirect a request to a new |
| 4783 | // URL during OnHeadersReceived. |
| 4784 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4785 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4786 | |
| 4787 | TestDelegate d; |
| 4788 | BlockingNetworkDelegate network_delegate( |
| 4789 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4790 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4791 | GURL redirect_url("http://does.not.resolve.test/simple.html"); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4792 | network_delegate.set_redirect_on_headers_received_url(redirect_url); |
| 4793 | |
| 4794 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4795 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4796 | |
| 4797 | { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4798 | GURL original_url("http://does.not.resolve.test/defaultresponse"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4799 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4800 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4801 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4802 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4803 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4804 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4805 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4806 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4807 | http_test_server()->host_port_pair()), |
| 4808 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4809 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4810 | // through an untunneled proxy. |
| 4811 | EXPECT_EQ(2, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4812 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4813 | http_test_server()->host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4814 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4815 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4816 | EXPECT_EQ(redirect_url, r->url()); |
| 4817 | EXPECT_EQ(original_url, r->original_url()); |
| 4818 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4819 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 4820 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4821 | } |
| 4822 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4823 | } |
| 4824 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4825 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4826 | // by taking no action. This indicates that the NetworkDelegate does not want to |
| 4827 | // handle the challenge, and is passing the buck along to the |
| 4828 | // URLRequest::Delegate. |
| 4829 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4830 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4831 | |
| 4832 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4833 | BlockingNetworkDelegate network_delegate( |
| 4834 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4835 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4836 | TestURLRequestContext context(true); |
| 4837 | context.set_network_delegate(&network_delegate); |
| 4838 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4839 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4840 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4841 | |
| 4842 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4843 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4844 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4845 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4846 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4847 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4848 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4849 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4850 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4851 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4852 | EXPECT_TRUE(d.auth_required_called()); |
| 4853 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4854 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4855 | } |
| 4856 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4857 | } |
| 4858 | |
| 4859 | TEST_F(URLRequestTestHTTP, |
| 4860 | NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4861 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4862 | |
| 4863 | TestDelegate d; |
| 4864 | BlockingNetworkDelegate network_delegate( |
| 4865 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4866 | |
| 4867 | TestURLRequestContext context(true); |
| 4868 | context.set_network_delegate(&network_delegate); |
| 4869 | context.Init(); |
| 4870 | |
| 4871 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 4872 | |
| 4873 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4874 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4875 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4876 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4877 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4878 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4879 | d.RunUntilComplete(); |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4880 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4881 | { |
| 4882 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4883 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4884 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4885 | } |
| 4886 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4887 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4888 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4889 | EXPECT_TRUE(d.auth_required_called()); |
| 4890 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4891 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4892 | } |
| 4893 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4894 | } |
| 4895 | |
| 4896 | // Tests that the network delegate can synchronously complete OnAuthRequired |
[email protected] | 1e110eae | 2013-05-10 22:02:40 | [diff] [blame] | 4897 | // by setting credentials. |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4898 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4899 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4900 | |
| 4901 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4902 | BlockingNetworkDelegate network_delegate( |
| 4903 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4904 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4905 | network_delegate.set_auth_retval( |
| 4906 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4907 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4908 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4909 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4910 | TestURLRequestContext context(true); |
| 4911 | context.set_network_delegate(&network_delegate); |
| 4912 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4913 | |
| 4914 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4915 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4916 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4917 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4918 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4919 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4920 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4921 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4922 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4923 | EXPECT_FALSE(d.auth_required_called()); |
| 4924 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4925 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4926 | } |
| 4927 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4928 | } |
| 4929 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4930 | // Same as above, but also tests that GetFullRequestHeaders returns the proper |
| 4931 | // headers (for the first or second request) when called at the proper times. |
| 4932 | TEST_F(URLRequestTestHTTP, |
| 4933 | NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4934 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4935 | |
| 4936 | TestDelegate d; |
| 4937 | BlockingNetworkDelegate network_delegate( |
| 4938 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4939 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 4940 | network_delegate.set_auth_retval( |
| 4941 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4942 | |
| 4943 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
| 4944 | |
| 4945 | TestURLRequestContext context(true); |
| 4946 | context.set_network_delegate(&network_delegate); |
| 4947 | context.Init(); |
| 4948 | |
| 4949 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4950 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4951 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4952 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4953 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4954 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4955 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4956 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4957 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4958 | EXPECT_FALSE(d.auth_required_called()); |
| 4959 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4960 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4961 | |
| 4962 | { |
| 4963 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4964 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4965 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
| 4966 | } |
| 4967 | } |
| 4968 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4969 | } |
| 4970 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4971 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4972 | // by cancelling authentication. |
| 4973 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4974 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4975 | |
| 4976 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4977 | BlockingNetworkDelegate network_delegate( |
| 4978 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4979 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4980 | network_delegate.set_auth_retval( |
| 4981 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4982 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4983 | TestURLRequestContext context(true); |
| 4984 | context.set_network_delegate(&network_delegate); |
| 4985 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4986 | |
| 4987 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4988 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4989 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4990 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4991 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4992 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4993 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4994 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4995 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4996 | EXPECT_FALSE(d.auth_required_called()); |
| 4997 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4998 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4999 | } |
| 5000 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5001 | } |
| 5002 | |
| 5003 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 5004 | // by taking no action. This indicates that the NetworkDelegate does not want |
| 5005 | // to handle the challenge, and is passing the buck along to the |
| 5006 | // URLRequest::Delegate. |
| 5007 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5008 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5009 | |
| 5010 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5011 | BlockingNetworkDelegate network_delegate( |
| 5012 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 5013 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5014 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5015 | TestURLRequestContext context(true); |
| 5016 | context.set_network_delegate(&network_delegate); |
| 5017 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5018 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 5019 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5020 | |
| 5021 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5022 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5023 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5024 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5025 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5026 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5027 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5028 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5029 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5030 | EXPECT_TRUE(d.auth_required_called()); |
| 5031 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5032 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5033 | } |
| 5034 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5035 | } |
| 5036 | |
| 5037 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 5038 | // by setting credentials. |
| 5039 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5040 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5041 | |
| 5042 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5043 | BlockingNetworkDelegate network_delegate( |
| 5044 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 5045 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5046 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5047 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 5048 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 5049 | AuthCredentials auth_credentials(kUser, kSecret); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5050 | network_delegate.set_auth_credentials(auth_credentials); |
| 5051 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5052 | TestURLRequestContext context(true); |
| 5053 | context.set_network_delegate(&network_delegate); |
| 5054 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5055 | |
| 5056 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5057 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5058 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5059 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5060 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5061 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5062 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5063 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5064 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5065 | EXPECT_FALSE(d.auth_required_called()); |
| 5066 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5067 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5068 | } |
| 5069 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5070 | } |
| 5071 | |
| 5072 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 5073 | // by cancelling authentication. |
| 5074 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5075 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5076 | |
| 5077 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5078 | BlockingNetworkDelegate network_delegate( |
| 5079 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 5080 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5081 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5082 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 5083 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5084 | TestURLRequestContext context(true); |
| 5085 | context.set_network_delegate(&network_delegate); |
| 5086 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5087 | |
| 5088 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5089 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5090 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5091 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5092 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5093 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5094 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5095 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5096 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5097 | EXPECT_FALSE(d.auth_required_called()); |
| 5098 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5099 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5100 | } |
| 5101 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5102 | } |
| 5103 | |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5104 | // Tests that NetworkDelegate header overrides from the 401 response do not |
| 5105 | // affect the 200 response. This is a regression test for |
| 5106 | // https://crbug.com/801237. |
| 5107 | TEST_F(URLRequestTestHTTP, NetworkDelegateOverrideHeadersWithAuth) { |
| 5108 | ASSERT_TRUE(http_test_server()->Start()); |
| 5109 | |
| 5110 | TestDelegate d; |
| 5111 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 5112 | default_network_delegate_.set_add_header_to_first_response(true); |
| 5113 | |
| 5114 | { |
| 5115 | GURL url(http_test_server()->GetURL("/auth-basic")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5116 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5117 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 5118 | r->Start(); |
| 5119 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5120 | d.RunUntilComplete(); |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5121 | |
| 5122 | EXPECT_EQ(OK, d.request_status()); |
| 5123 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5124 | EXPECT_TRUE(d.auth_required_called()); |
| 5125 | EXPECT_FALSE(r->response_headers()->HasHeader("X-Network-Delegate")); |
| 5126 | } |
| 5127 | |
| 5128 | { |
| 5129 | GURL url(http_test_server()->GetURL("/defaultresponse")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5130 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5131 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 5132 | r->Start(); |
| 5133 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5134 | d.RunUntilComplete(); |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5135 | |
| 5136 | // Check that set_add_header_to_first_response normally adds a header. |
| 5137 | EXPECT_EQ(OK, d.request_status()); |
| 5138 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5139 | EXPECT_TRUE(r->response_headers()->HasHeader("X-Network-Delegate")); |
| 5140 | } |
| 5141 | } |
| 5142 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5143 | // Tests that we can handle when a network request was canceled while we were |
| 5144 | // waiting for the network delegate. |
| 5145 | // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 5146 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5147 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5148 | |
| 5149 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5150 | BlockingNetworkDelegate network_delegate( |
| 5151 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5152 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5153 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5154 | TestURLRequestContext context(true); |
| 5155 | context.set_network_delegate(&network_delegate); |
| 5156 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5157 | |
| 5158 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5159 | std::unique_ptr<URLRequest> r( |
| 5160 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 5161 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5162 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5163 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5164 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5165 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 5166 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5167 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5168 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5169 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5170 | // Ensure that network delegate is notified. |
| 5171 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5172 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5173 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5174 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5175 | } |
| 5176 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5177 | } |
| 5178 | |
| 5179 | // Tests that we can handle when a network request was canceled while we were |
| 5180 | // waiting for the network delegate. |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5181 | // Part 2: Request is cancelled while waiting for OnBeforeStartTransaction |
| 5182 | // callback. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5183 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5184 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5185 | |
| 5186 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5187 | BlockingNetworkDelegate network_delegate( |
| 5188 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5189 | network_delegate.set_block_on( |
| 5190 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5191 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5192 | TestURLRequestContext context(true); |
| 5193 | context.set_network_delegate(&network_delegate); |
| 5194 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5195 | |
| 5196 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5197 | std::unique_ptr<URLRequest> r( |
| 5198 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 5199 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5200 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5201 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5202 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5203 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 5204 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5205 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5206 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5207 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5208 | // Ensure that network delegate is notified. |
| 5209 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5210 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5211 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5212 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5213 | } |
| 5214 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5215 | } |
| 5216 | |
| 5217 | // Tests that we can handle when a network request was canceled while we were |
| 5218 | // waiting for the network delegate. |
| 5219 | // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 5220 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5221 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5222 | |
| 5223 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5224 | BlockingNetworkDelegate network_delegate( |
| 5225 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5226 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5227 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5228 | TestURLRequestContext context(true); |
| 5229 | context.set_network_delegate(&network_delegate); |
| 5230 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5231 | |
| 5232 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5233 | std::unique_ptr<URLRequest> r( |
| 5234 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 5235 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5236 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5237 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5238 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5239 | EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 5240 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5241 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5242 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5243 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5244 | // Ensure that network delegate is notified. |
| 5245 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5246 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5247 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5248 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5249 | } |
| 5250 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5251 | } |
| 5252 | |
| 5253 | // Tests that we can handle when a network request was canceled while we were |
| 5254 | // waiting for the network delegate. |
| 5255 | // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
[email protected] | bfe8b30 | 2013-02-15 12:16:02 | [diff] [blame] | 5256 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5257 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5258 | |
| 5259 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5260 | BlockingNetworkDelegate network_delegate( |
| 5261 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5262 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5263 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5264 | TestURLRequestContext context(true); |
| 5265 | context.set_network_delegate(&network_delegate); |
| 5266 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5267 | |
| 5268 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5269 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5270 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 5271 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5272 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5273 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5274 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5275 | EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED, |
| 5276 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5277 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5278 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5279 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5280 | // Ensure that network delegate is notified. |
| 5281 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5282 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5283 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5284 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5285 | } |
| 5286 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5287 | } |
| 5288 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5289 | namespace { |
| 5290 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5291 | std::unique_ptr<test_server::HttpResponse> HandleServerAuthConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5292 | const test_server::HttpRequest& request) { |
| 5293 | if (request.headers.find("Host") == request.headers.end() || |
| 5294 | request.headers.at("Host") != "www.server-auth.com" || |
| 5295 | request.method != test_server::METHOD_CONNECT) { |
| 5296 | return nullptr; |
| 5297 | } |
| 5298 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5299 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5300 | new test_server::BasicHttpResponse); |
| 5301 | http_response->set_code(HTTP_UNAUTHORIZED); |
| 5302 | http_response->AddCustomHeader("WWW-Authenticate", |
| 5303 | "Basic realm=\"WallyWorld\""); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 5304 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5305 | } |
| 5306 | |
| 5307 | } // namespace |
| 5308 | |
| 5309 | // 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] | 5310 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5311 | // The EmbeddedTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5312 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5313 | http_test_server()->RegisterRequestHandler( |
Matt Menke | 2b2a6047 | 2018-02-07 02:12:12 | [diff] [blame] | 5314 | base::BindRepeating(&HandleServerAuthConnect)); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5315 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5316 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5317 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 5318 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5319 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 5320 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5321 | TestDelegate d; |
| 5322 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5323 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5324 | GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d, |
| 5325 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5326 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5327 | r->Start(); |
| 5328 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5329 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5330 | d.RunUntilComplete(); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5331 | |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 5332 | // The proxy server should be set before failure. |
| 5333 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 5334 | http_test_server()->host_port_pair()), |
| 5335 | r->proxy_server()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5336 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5337 | } |
| 5338 | } |
| 5339 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5340 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5341 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5342 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5343 | TestDelegate d; |
| 5344 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5345 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5346 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5347 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5348 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5349 | r->Start(); |
| 5350 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5351 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5352 | d.RunUntilComplete(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5353 | |
| 5354 | EXPECT_EQ(1, d.response_started_count()); |
| 5355 | EXPECT_FALSE(d.received_data_before_response()); |
| 5356 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5357 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5358 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5359 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5360 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 5361 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5362 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5363 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5364 | } |
| 5365 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5366 | // This test has the server send a large number of cookies to the client. |
| 5367 | // To ensure that no number of cookies causes a crash, a galloping binary |
| 5368 | // search is used to estimate that maximum number of cookies that are accepted |
| 5369 | // by the browser. Beyond the maximum number, the request will fail with |
| 5370 | // ERR_RESPONSE_HEADERS_TOO_BIG. |
Sergey Ulanov | 0fb900c | 2017-09-20 00:27:31 | [diff] [blame] | 5371 | #if defined(OS_WIN) || defined(OS_FUCHSIA) |
[email protected] | 69dd6fe | 2013-02-23 23:15:30 | [diff] [blame] | 5372 | // http://crbug.com/177916 |
| 5373 | #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies |
| 5374 | #else |
| 5375 | #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies |
| 5376 | #endif // defined(OS_WIN) |
| 5377 | TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5378 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5379 | |
| 5380 | int lower_bound = 0; |
| 5381 | int upper_bound = 1; |
| 5382 | |
| 5383 | // Double the number of cookies until the response header limits are |
| 5384 | // exceeded. |
| 5385 | while (DoManyCookiesRequest(upper_bound)) { |
| 5386 | lower_bound = upper_bound; |
| 5387 | upper_bound *= 2; |
| 5388 | ASSERT_LT(upper_bound, 1000000); |
| 5389 | } |
| 5390 | |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 5391 | int tolerance = static_cast<int>(upper_bound * 0.005); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5392 | if (tolerance < 2) |
| 5393 | tolerance = 2; |
| 5394 | |
| 5395 | // Perform a binary search to find the highest possible number of cookies, |
| 5396 | // within the desired tolerance. |
| 5397 | while (upper_bound - lower_bound >= tolerance) { |
| 5398 | int num_cookies = (lower_bound + upper_bound) / 2; |
| 5399 | |
| 5400 | if (DoManyCookiesRequest(num_cookies)) |
| 5401 | lower_bound = num_cookies; |
| 5402 | else |
| 5403 | upper_bound = num_cookies; |
| 5404 | } |
| 5405 | // Success: the test did not crash. |
| 5406 | } |
| 5407 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5408 | TEST_F(URLRequestTestHTTP, GetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5409 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5410 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5411 | TestDelegate d; |
| 5412 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5413 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5414 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5415 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5416 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5417 | r->Start(); |
| 5418 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5419 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5420 | d.RunUntilComplete(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5421 | |
| 5422 | EXPECT_EQ(1, d.response_started_count()); |
| 5423 | EXPECT_FALSE(d.received_data_before_response()); |
| 5424 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5425 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5426 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5427 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5428 | r->GetResponseRemoteEndpoint().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5429 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 5430 | } |
| 5431 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5432 | TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5433 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5434 | |
| 5435 | TestDelegate d; |
| 5436 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5437 | GURL test_url(http_test_server()->GetURL("/defaultresponse")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5438 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5439 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5440 | |
| 5441 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5442 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5443 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5444 | r->Start(); |
| 5445 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5446 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5447 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5448 | |
| 5449 | EXPECT_EQ(1, d.response_started_count()); |
| 5450 | EXPECT_FALSE(d.received_data_before_response()); |
| 5451 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5452 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5453 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5454 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5455 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5456 | |
| 5457 | EXPECT_TRUE(d.have_full_request_headers()); |
| 5458 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 5459 | } |
| 5460 | } |
| 5461 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5462 | TEST_F(URLRequestTestHTTP, GetTestLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5463 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5464 | |
| 5465 | TestDelegate d; |
| 5466 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5467 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5468 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5469 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5470 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5471 | r->Start(); |
| 5472 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5473 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5474 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5475 | |
| 5476 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5477 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5478 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5479 | |
| 5480 | EXPECT_EQ(1, d.response_started_count()); |
| 5481 | EXPECT_FALSE(d.received_data_before_response()); |
| 5482 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5483 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5484 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5485 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5486 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5487 | } |
| 5488 | } |
| 5489 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5490 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 5491 | #if !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5492 | TEST_F(URLRequestTestHTTP, GetZippedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5493 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5494 | base::FilePath(kTestFilePath)); |
| 5495 | |
| 5496 | ASSERT_TRUE(test_server.Start()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5497 | |
| 5498 | // Parameter that specifies the Content-Length field in the response: |
| 5499 | // C - Compressed length. |
| 5500 | // U - Uncompressed length. |
| 5501 | // L - Large length (larger than both C & U). |
| 5502 | // M - Medium length (between C & U). |
| 5503 | // S - Small length (smaller than both C & U). |
| 5504 | const char test_parameters[] = "CULMS"; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 5505 | const int num_tests = base::size(test_parameters) - 1; // Skip NULL. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5506 | // C & U should be OK. |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 5507 | // L & M are larger than the data sent, and show an error. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5508 | // S has too little data, but we seem to accept it. |
| 5509 | const bool test_expect_success[num_tests] = |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 5510 | { true, true, false, false, true }; |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5511 | |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5512 | base::FilePath file_path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 5513 | base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5514 | file_path = file_path.Append(kTestFilePath); |
| 5515 | file_path = file_path.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); |
| 5516 | std::string expected_content; |
| 5517 | ASSERT_TRUE(base::ReadFileToString(file_path, &expected_content)); |
| 5518 | |
| 5519 | for (int i = 0; i < num_tests; i++) { |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5520 | TestDelegate d; |
| 5521 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5522 | std::string test_file = base::StringPrintf( |
| 5523 | "compressedfiles/BullRunSpeech.txt?%c", test_parameters[i]); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5524 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5525 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5526 | TestURLRequestContext context(true); |
| 5527 | context.set_network_delegate(&network_delegate); |
| 5528 | context.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 5529 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5530 | std::unique_ptr<URLRequest> r( |
| 5531 | context.CreateRequest(test_server.GetURL(test_file), DEFAULT_PRIORITY, |
| 5532 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5533 | r->Start(); |
| 5534 | EXPECT_TRUE(r->is_pending()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5535 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5536 | d.RunUntilComplete(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5537 | |
| 5538 | EXPECT_EQ(1, d.response_started_count()); |
| 5539 | EXPECT_FALSE(d.received_data_before_response()); |
| 5540 | VLOG(1) << " Received " << d.bytes_received() << " bytes" |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5541 | << " error = " << d.request_status(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5542 | if (test_expect_success[i]) { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5543 | EXPECT_EQ(OK, d.request_status()) << " Parameter = \"" << test_file |
| 5544 | << "\""; |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5545 | if (test_parameters[i] == 'S') { |
| 5546 | // When content length is smaller than both compressed length and |
| 5547 | // uncompressed length, HttpStreamParser might not read the full |
| 5548 | // response body. |
| 5549 | continue; |
| 5550 | } |
| 5551 | EXPECT_EQ(expected_content, d.data_received()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5552 | } else { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5553 | EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, d.request_status()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5554 | << " Parameter = \"" << test_file << "\""; |
| 5555 | } |
| 5556 | } |
| 5557 | } |
| 5558 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5559 | #endif // !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5560 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5561 | TEST_F(URLRequestTestHTTP, RedirectLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5562 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5563 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5564 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5565 | GURL original_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5566 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5567 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5568 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5569 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5570 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5571 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5572 | |
| 5573 | EXPECT_EQ(1, d.response_started_count()); |
| 5574 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5575 | EXPECT_EQ(destination_url, req->url()); |
| 5576 | EXPECT_EQ(original_url, req->original_url()); |
| 5577 | ASSERT_EQ(2U, req->url_chain().size()); |
| 5578 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5579 | EXPECT_EQ(destination_url, req->url_chain()[1]); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5580 | |
| 5581 | LoadTimingInfo load_timing_info_before_redirect; |
| 5582 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect( |
| 5583 | &load_timing_info_before_redirect)); |
| 5584 | TestLoadTimingNotReused(load_timing_info_before_redirect, |
| 5585 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 5586 | |
| 5587 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5588 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5589 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5590 | |
| 5591 | // Check that a new socket was used on redirect, since the server does not |
| 5592 | // supposed keep-alive sockets, and that the times before the redirect are |
| 5593 | // before the ones recorded for the second request. |
| 5594 | EXPECT_NE(load_timing_info_before_redirect.socket_log_id, |
| 5595 | load_timing_info.socket_log_id); |
| 5596 | EXPECT_LE(load_timing_info_before_redirect.receive_headers_end, |
| 5597 | load_timing_info.connect_timing.connect_start); |
| 5598 | } |
| 5599 | |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5600 | TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5601 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5602 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5603 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5604 | GURL middle_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5605 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
| 5606 | GURL original_url = http_test_server()->GetURL("/server-redirect?" + |
| 5607 | middle_redirect_url.spec()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5608 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5609 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5610 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5611 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5612 | d.RunUntilComplete(); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5613 | |
| 5614 | EXPECT_EQ(1, d.response_started_count()); |
| 5615 | EXPECT_EQ(2, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5616 | EXPECT_EQ(destination_url, req->url()); |
| 5617 | EXPECT_EQ(original_url, req->original_url()); |
| 5618 | ASSERT_EQ(3U, req->url_chain().size()); |
| 5619 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5620 | EXPECT_EQ(middle_redirect_url, req->url_chain()[1]); |
| 5621 | EXPECT_EQ(destination_url, req->url_chain()[2]); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5622 | } |
| 5623 | |
David Benjamin | a7fde61 | 2019-03-15 14:20:58 | [diff] [blame] | 5624 | // This is a regression test for https://crbug.com/942073. |
| 5625 | TEST_F(URLRequestTestHTTP, RedirectEscaping) { |
| 5626 | ASSERT_TRUE(http_test_server()->Start()); |
| 5627 | |
| 5628 | // Assemble the destination URL as a string so it is not escaped by GURL. |
| 5629 | GURL destination_base = http_test_server()->GetURL("/defaultresponse"); |
| 5630 | // Add a URL fragment of U+2603 unescaped, U+2603 escaped, and then a UTF-8 |
| 5631 | // encoding error. |
| 5632 | std::string destination_url = |
| 5633 | destination_base.spec() + "#\xE2\x98\x83_%E2%98%83_\xE0\xE0"; |
| 5634 | // Redirect resolution should percent-escape bytes and preserve the UTF-8 |
| 5635 | // error at the end. |
| 5636 | std::string destination_escaped = |
| 5637 | destination_base.spec() + "#%E2%98%83_%E2%98%83_%E0%E0"; |
| 5638 | GURL original_url = http_test_server()->GetURL( |
| 5639 | "/server-redirect?" + EscapeQueryParamValue(destination_url, false)); |
| 5640 | TestDelegate d; |
| 5641 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 5642 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 5643 | req->Start(); |
| 5644 | d.RunUntilComplete(); |
| 5645 | |
| 5646 | EXPECT_EQ(1, d.response_started_count()); |
| 5647 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5648 | EXPECT_EQ(destination_escaped, req->url().spec()); |
| 5649 | EXPECT_EQ(original_url, req->original_url()); |
| 5650 | ASSERT_EQ(2U, req->url_chain().size()); |
| 5651 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5652 | EXPECT_EQ(destination_escaped, req->url_chain()[1].spec()); |
| 5653 | } |
| 5654 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5655 | // First and second pieces of information logged by delegates to URLRequests. |
| 5656 | const char kFirstDelegateInfo[] = "Wonderful delegate"; |
| 5657 | const char kSecondDelegateInfo[] = "Exciting delegate"; |
| 5658 | |
| 5659 | // Logs delegate information to a URLRequest. The first string is logged |
| 5660 | // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is |
| 5661 | // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then |
| 5662 | // another asynchronous call is used to clear the delegate information |
| 5663 | // before calling a callback. The object then deletes itself. |
| 5664 | class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> { |
| 5665 | public: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5666 | using Callback = base::OnceCallback<void()>; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5667 | |
| 5668 | // Each time delegate information is added to the URLRequest, the resulting |
| 5669 | // load state is checked. The expected load state after each request is |
| 5670 | // passed in as an argument. |
| 5671 | static void Run(URLRequest* url_request, |
| 5672 | LoadState expected_first_load_state, |
| 5673 | LoadState expected_second_load_state, |
| 5674 | LoadState expected_third_load_state, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5675 | Callback callback) { |
tzik | 38f920a | 2018-08-01 04:41:20 | [diff] [blame] | 5676 | // base::MakeRefCounted<AsyncDelegateLogger> is unavailable here, since the |
| 5677 | // constructor of AsyncDelegateLogger is private. |
| 5678 | auto logger = base::WrapRefCounted(new AsyncDelegateLogger( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5679 | url_request, expected_first_load_state, expected_second_load_state, |
tzik | 38f920a | 2018-08-01 04:41:20 | [diff] [blame] | 5680 | expected_third_load_state, std::move(callback))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5681 | logger->Start(); |
| 5682 | } |
| 5683 | |
| 5684 | // Checks that the log entries, starting with log_position, contain the |
| 5685 | // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have |
| 5686 | // recorded. Returns the index of entry after the expected number of |
| 5687 | // events this logged, or entries.size() if there aren't enough entries. |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 5688 | static size_t CheckDelegateInfo(const std::vector<NetLogEntry>& entries, |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 5689 | size_t log_position) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5690 | // There should be 4 DELEGATE_INFO events: Two begins and two ends. |
| 5691 | if (log_position + 3 >= entries.size()) { |
| 5692 | ADD_FAILURE() << "Not enough log entries"; |
| 5693 | return entries.size(); |
| 5694 | } |
| 5695 | std::string delegate_info; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5696 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5697 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 5698 | EXPECT_EQ( |
| 5699 | kFirstDelegateInfo, |
| 5700 | GetStringValueFromParams(entries[log_position], "delegate_blocked_by")); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5701 | |
| 5702 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5703 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5704 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5705 | |
| 5706 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5707 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5708 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 5709 | EXPECT_EQ( |
| 5710 | kSecondDelegateInfo, |
| 5711 | GetStringValueFromParams(entries[log_position], "delegate_blocked_by")); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5712 | |
| 5713 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5714 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5715 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5716 | |
| 5717 | return log_position + 1; |
| 5718 | } |
| 5719 | |
| 5720 | private: |
| 5721 | friend class base::RefCounted<AsyncDelegateLogger>; |
| 5722 | |
| 5723 | AsyncDelegateLogger(URLRequest* url_request, |
| 5724 | LoadState expected_first_load_state, |
| 5725 | LoadState expected_second_load_state, |
| 5726 | LoadState expected_third_load_state, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5727 | Callback callback) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5728 | : url_request_(url_request), |
| 5729 | expected_first_load_state_(expected_first_load_state), |
| 5730 | expected_second_load_state_(expected_second_load_state), |
| 5731 | expected_third_load_state_(expected_third_load_state), |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5732 | callback_(std::move(callback)) {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5733 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5734 | ~AsyncDelegateLogger() = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5735 | |
| 5736 | void Start() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5737 | url_request_->LogBlockedBy(kFirstDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5738 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5739 | EXPECT_EQ(expected_first_load_state_, load_state.state); |
| 5740 | EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5741 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5742 | FROM_HERE, |
| 5743 | base::BindOnce(&AsyncDelegateLogger::LogSecondDelegate, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5744 | } |
| 5745 | |
| 5746 | void LogSecondDelegate() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5747 | url_request_->LogAndReportBlockedBy(kSecondDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5748 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5749 | EXPECT_EQ(expected_second_load_state_, load_state.state); |
| 5750 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) { |
| 5751 | EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5752 | } else { |
| 5753 | EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5754 | } |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5755 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5756 | FROM_HERE, base::BindOnce(&AsyncDelegateLogger::LogComplete, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5757 | } |
| 5758 | |
| 5759 | void LogComplete() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5760 | url_request_->LogUnblocked(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5761 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5762 | EXPECT_EQ(expected_third_load_state_, load_state.state); |
| 5763 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5764 | EXPECT_EQ(base::string16(), load_state.param); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5765 | std::move(callback_).Run(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5766 | } |
| 5767 | |
| 5768 | URLRequest* url_request_; |
| 5769 | const int expected_first_load_state_; |
| 5770 | const int expected_second_load_state_; |
| 5771 | const int expected_third_load_state_; |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5772 | Callback callback_; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5773 | |
| 5774 | DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger); |
| 5775 | }; |
| 5776 | |
| 5777 | // NetworkDelegate that logs delegate information before a request is started, |
| 5778 | // before headers are sent, when headers are read, and when auth information |
| 5779 | // is requested. Uses AsyncDelegateLogger. |
| 5780 | class AsyncLoggingNetworkDelegate : public TestNetworkDelegate { |
| 5781 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5782 | AsyncLoggingNetworkDelegate() = default; |
| 5783 | ~AsyncLoggingNetworkDelegate() override = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5784 | |
| 5785 | // NetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5786 | int OnBeforeURLRequest(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5787 | CompletionOnceCallback callback, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5788 | GURL* new_url) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5789 | // TestNetworkDelegate always completes synchronously. |
| 5790 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeURLRequest( |
| 5791 | request, base::NullCallback(), new_url)); |
| 5792 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5793 | } |
| 5794 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5795 | int OnBeforeStartTransaction(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5796 | CompletionOnceCallback callback, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5797 | HttpRequestHeaders* headers) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5798 | // TestNetworkDelegate always completes synchronously. |
| 5799 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeStartTransaction( |
| 5800 | request, base::NullCallback(), headers)); |
| 5801 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5802 | } |
| 5803 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5804 | int OnHeadersReceived( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5805 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5806 | CompletionOnceCallback callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5807 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5808 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5809 | GURL* allowed_unsafe_redirect_url) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5810 | // TestNetworkDelegate always completes synchronously. |
| 5811 | CHECK_NE(ERR_IO_PENDING, |
| 5812 | TestNetworkDelegate::OnHeadersReceived( |
| 5813 | request, base::NullCallback(), original_response_headers, |
| 5814 | override_response_headers, allowed_unsafe_redirect_url)); |
| 5815 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5816 | } |
| 5817 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5818 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5819 | URLRequest* request, |
| 5820 | const AuthChallengeInfo& auth_info, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5821 | AuthCallback callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5822 | AuthCredentials* credentials) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5823 | AsyncDelegateLogger::Run( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5824 | request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5825 | LOAD_STATE_WAITING_FOR_DELEGATE, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5826 | base::BindOnce(&AsyncLoggingNetworkDelegate::SetAuthAndResume, |
| 5827 | std::move(callback), credentials)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5828 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 5829 | } |
| 5830 | |
| 5831 | private: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5832 | static int RunCallbackAsynchronously(URLRequest* request, |
| 5833 | CompletionOnceCallback callback) { |
| 5834 | AsyncDelegateLogger::Run(request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5835 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5836 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5837 | base::BindOnce(std::move(callback), OK)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5838 | return ERR_IO_PENDING; |
| 5839 | } |
| 5840 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5841 | static void SetAuthAndResume(AuthCallback callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5842 | AuthCredentials* credentials) { |
| 5843 | *credentials = AuthCredentials(kUser, kSecret); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5844 | std::move(callback).Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5845 | } |
| 5846 | |
| 5847 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate); |
| 5848 | }; |
| 5849 | |
| 5850 | // URLRequest::Delegate that logs delegate information when the headers |
| 5851 | // are received, when each read completes, and during redirects. Uses |
| 5852 | // AsyncDelegateLogger. Can optionally cancel a request in any phase. |
| 5853 | // |
| 5854 | // Inherits from TestDelegate to reuse the TestDelegate code to handle |
| 5855 | // advancing to the next step in most cases, as well as cancellation. |
| 5856 | class AsyncLoggingUrlRequestDelegate : public TestDelegate { |
| 5857 | public: |
| 5858 | enum CancelStage { |
| 5859 | NO_CANCEL = 0, |
| 5860 | CANCEL_ON_RECEIVED_REDIRECT, |
| 5861 | CANCEL_ON_RESPONSE_STARTED, |
| 5862 | CANCEL_ON_READ_COMPLETED |
| 5863 | }; |
| 5864 | |
| 5865 | explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage) |
| 5866 | : cancel_stage_(cancel_stage) { |
| 5867 | if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT) |
| 5868 | set_cancel_in_received_redirect(true); |
| 5869 | else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED) |
| 5870 | set_cancel_in_response_started(true); |
| 5871 | else if (cancel_stage == CANCEL_ON_READ_COMPLETED) |
| 5872 | set_cancel_in_received_data(true); |
| 5873 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5874 | ~AsyncLoggingUrlRequestDelegate() override = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5875 | |
| 5876 | // URLRequest::Delegate implementation: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5877 | void OnReceivedRedirect(URLRequest* request, |
| 5878 | const RedirectInfo& redirect_info, |
| 5879 | bool* defer_redirect) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5880 | *defer_redirect = true; |
| 5881 | AsyncDelegateLogger::Run( |
| 5882 | request, |
| 5883 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5884 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5885 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5886 | base::Bind( |
| 5887 | &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5888 | base::Unretained(this), request, redirect_info)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5889 | } |
| 5890 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5891 | void OnResponseStarted(URLRequest* request, int net_error) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5892 | AsyncDelegateLogger::Run( |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5893 | request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5894 | LOAD_STATE_WAITING_FOR_DELEGATE, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5895 | base::Bind( |
| 5896 | &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete, |
| 5897 | base::Unretained(this), request, net_error)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5898 | } |
| 5899 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5900 | void OnReadCompleted(URLRequest* request, int bytes_read) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5901 | AsyncDelegateLogger::Run( |
| 5902 | request, |
| 5903 | LOAD_STATE_IDLE, |
| 5904 | LOAD_STATE_IDLE, |
| 5905 | LOAD_STATE_IDLE, |
| 5906 | base::Bind( |
| 5907 | &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete, |
| 5908 | base::Unretained(this), request, bytes_read)); |
| 5909 | } |
| 5910 | |
| 5911 | private: |
| 5912 | void OnReceivedRedirectLoggingComplete(URLRequest* request, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5913 | const RedirectInfo& redirect_info) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5914 | bool defer_redirect = false; |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5915 | TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5916 | // FollowDeferredRedirect should not be called after cancellation. |
| 5917 | if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT) |
| 5918 | return; |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 5919 | if (!defer_redirect) { |
| 5920 | request->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 5921 | base::nullopt /* modified_headers */); |
| 5922 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5923 | } |
| 5924 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5925 | void OnResponseStartedLoggingComplete(URLRequest* request, int net_error) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5926 | // The parent class continues the request. |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5927 | TestDelegate::OnResponseStarted(request, net_error); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5928 | } |
| 5929 | |
| 5930 | void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) { |
| 5931 | // The parent class continues the request. |
| 5932 | TestDelegate::OnReadCompleted(request, bytes_read); |
| 5933 | } |
| 5934 | |
| 5935 | const CancelStage cancel_stage_; |
| 5936 | |
| 5937 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate); |
| 5938 | }; |
| 5939 | |
| 5940 | // Tests handling of delegate info before a request starts. |
| 5941 | TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5942 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5943 | |
| 5944 | TestDelegate request_delegate; |
| 5945 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 5946 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5947 | context.set_net_log(&net_log_); |
| 5948 | context.Init(); |
| 5949 | |
| 5950 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5951 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5952 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, |
| 5953 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5954 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5955 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5956 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5957 | |
| 5958 | AsyncDelegateLogger::Run( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5959 | r.get(), |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5960 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5961 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5962 | LOAD_STATE_IDLE, |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5963 | base::Bind(&URLRequest::Start, base::Unretained(r.get()))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5964 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5965 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5966 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5967 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5968 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5969 | } |
| 5970 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 5971 | auto entries = net_log_.GetEntries(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5972 | size_t log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5973 | entries, 0, NetLogEventType::DELEGATE_INFO, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5974 | |
| 5975 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position); |
| 5976 | |
| 5977 | // Nothing else should add any delegate info to the request. |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5978 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5979 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5980 | } |
| 5981 | |
| 5982 | // Tests handling of delegate info from a network delegate. |
| 5983 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5984 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5985 | |
| 5986 | TestDelegate request_delegate; |
| 5987 | AsyncLoggingNetworkDelegate network_delegate; |
| 5988 | TestURLRequestContext context(true); |
| 5989 | context.set_network_delegate(&network_delegate); |
| 5990 | context.set_net_log(&net_log_); |
| 5991 | context.Init(); |
| 5992 | |
| 5993 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5994 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5995 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, |
| 5996 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5997 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5998 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5999 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6000 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6001 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6002 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6003 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6004 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6005 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6006 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 6007 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 6008 | } |
| 6009 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 6010 | |
| 6011 | size_t log_position = 0; |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 6012 | auto entries = net_log_.GetEntries(); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6013 | static const NetLogEventType kExpectedEvents[] = { |
| 6014 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 6015 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6016 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6017 | }; |
| 6018 | for (NetLogEventType event : kExpectedEvents) { |
| 6019 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6020 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6021 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6022 | |
| 6023 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 6024 | log_position + 1); |
| 6025 | |
| 6026 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6027 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6028 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6029 | } |
| 6030 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6031 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6032 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6033 | } |
| 6034 | |
| 6035 | // Tests handling of delegate info from a network delegate in the case of an |
| 6036 | // HTTP redirect. |
| 6037 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6038 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6039 | |
| 6040 | TestDelegate request_delegate; |
| 6041 | AsyncLoggingNetworkDelegate network_delegate; |
| 6042 | TestURLRequestContext context(true); |
| 6043 | context.set_network_delegate(&network_delegate); |
| 6044 | context.set_net_log(&net_log_); |
| 6045 | context.Init(); |
| 6046 | |
| 6047 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6048 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6049 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6050 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6051 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6052 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 6053 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6054 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6055 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6056 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6057 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6058 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6059 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6060 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 6061 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 6062 | } |
| 6063 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 6064 | |
| 6065 | size_t log_position = 0; |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 6066 | auto entries = net_log_.GetEntries(); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6067 | static const NetLogEventType kExpectedEvents[] = { |
| 6068 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 6069 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6070 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6071 | }; |
| 6072 | for (NetLogEventType event : kExpectedEvents) { |
| 6073 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6074 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6075 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6076 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6077 | log_position = |
| 6078 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6079 | |
| 6080 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6081 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6082 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6083 | } |
| 6084 | |
| 6085 | // The URLRequest::Delegate then gets informed about the redirect. |
| 6086 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6087 | entries, log_position + 1, |
| 6088 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6089 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6090 | |
| 6091 | // The NetworkDelegate logged information in the same three events as before. |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6092 | for (NetLogEventType event : kExpectedEvents) { |
| 6093 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6094 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6095 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6096 | |
| 6097 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 6098 | log_position + 1); |
| 6099 | |
| 6100 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6101 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6102 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6103 | } |
| 6104 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6105 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6106 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6107 | } |
| 6108 | |
| 6109 | // Tests handling of delegate info from a network delegate in the case of HTTP |
| 6110 | // AUTH. |
| 6111 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6112 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6113 | |
| 6114 | TestDelegate request_delegate; |
| 6115 | AsyncLoggingNetworkDelegate network_delegate; |
| 6116 | TestURLRequestContext context(true); |
| 6117 | context.set_network_delegate(&network_delegate); |
| 6118 | context.set_net_log(&net_log_); |
| 6119 | context.Init(); |
| 6120 | |
| 6121 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6122 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 6123 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, |
| 6124 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6125 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6126 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 6127 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6128 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6129 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6130 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6131 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6132 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6133 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6134 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 6135 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 6136 | } |
| 6137 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 6138 | |
| 6139 | size_t log_position = 0; |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 6140 | auto entries = net_log_.GetEntries(); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6141 | static const NetLogEventType kExpectedEvents[] = { |
| 6142 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 6143 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6144 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6145 | NetLogEventType::NETWORK_DELEGATE_AUTH_REQUIRED, |
| 6146 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6147 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6148 | }; |
| 6149 | for (NetLogEventType event : kExpectedEvents) { |
| 6150 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6151 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6152 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6153 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6154 | log_position = |
| 6155 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6156 | |
| 6157 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6158 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6159 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6160 | } |
| 6161 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6162 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6163 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6164 | } |
| 6165 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6166 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 6167 | #if !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6168 | // Tests handling of delegate info from a URLRequest::Delegate. |
| 6169 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6170 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6171 | base::FilePath(kTestFilePath)); |
| 6172 | |
| 6173 | ASSERT_TRUE(test_server.Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6174 | |
| 6175 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 6176 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 6177 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6178 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6179 | context.set_net_log(&net_log_); |
| 6180 | context.Init(); |
| 6181 | |
| 6182 | { |
| 6183 | // A chunked response with delays between chunks is used to make sure that |
| 6184 | // attempts by the URLRequest delegate to log information while reading the |
| 6185 | // body are ignored. Since they are ignored, this test is robust against |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6186 | // the possibility of multiple reads being combined in the unlikely event |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6187 | // that it occurs. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6188 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6189 | test_server.GetURL("/chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6190 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6191 | LoadStateWithParam load_state = r->GetLoadState(); |
| 6192 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6193 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6194 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6195 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6196 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6197 | } |
| 6198 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 6199 | auto entries = net_log_.GetEntries(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6200 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6201 | size_t log_position = 0; |
| 6202 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6203 | // The delegate info should only have been logged on header complete. Other |
| 6204 | // times it should silently be ignored. |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6205 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 6206 | entries, 0, NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST)); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6207 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6208 | entries, log_position + 1, |
| 6209 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6210 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6211 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6212 | log_position = |
| 6213 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6214 | |
| 6215 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6216 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 6217 | entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6218 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6219 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6220 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6221 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6222 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6223 | entries, log_position + 1, |
| 6224 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6225 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6226 | #endif // !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6227 | |
| 6228 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 6229 | // an HTTP redirect. |
| 6230 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6231 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6232 | |
| 6233 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 6234 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 6235 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6236 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6237 | context.set_net_log(&net_log_); |
| 6238 | context.Init(); |
| 6239 | |
| 6240 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6241 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6242 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6243 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6244 | LoadStateWithParam load_state = r->GetLoadState(); |
| 6245 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6246 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6247 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6248 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6249 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6250 | } |
| 6251 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 6252 | auto entries = net_log_.GetEntries(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6253 | |
| 6254 | // Delegate info should only have been logged in OnReceivedRedirect and |
| 6255 | // OnResponseStarted. |
| 6256 | size_t log_position = 0; |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6257 | static const NetLogEventType kExpectedEvents[] = { |
| 6258 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
| 6259 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 6260 | }; |
| 6261 | for (NetLogEventType event : kExpectedEvents) { |
| 6262 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
| 6263 | log_position = ExpectLogContainsSomewhereAfter(entries, log_position, event, |
| 6264 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6265 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6266 | log_position = |
| 6267 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6268 | |
| 6269 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6270 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6271 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6272 | } |
| 6273 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6274 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6275 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6276 | } |
| 6277 | |
| 6278 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 6279 | // an HTTP redirect, with cancellation at various points. |
| 6280 | TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6281 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6282 | |
| 6283 | const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = { |
| 6284 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT, |
| 6285 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED, |
| 6286 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED, |
| 6287 | }; |
| 6288 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6289 | for (auto cancel_stage : kCancelStages) { |
| 6290 | AsyncLoggingUrlRequestDelegate request_delegate(cancel_stage); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 6291 | TestNetLog net_log; |
krasin | 0bfeb6b | 2017-01-13 21:48:04 | [diff] [blame] | 6292 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6293 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6294 | context.set_net_log(&net_log); |
| 6295 | context.Init(); |
| 6296 | |
| 6297 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6298 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6299 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6300 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6301 | LoadStateWithParam load_state = r->GetLoadState(); |
| 6302 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6303 | request_delegate.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6304 | EXPECT_EQ(ERR_ABORTED, request_delegate.request_status()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6305 | |
| 6306 | // Spin the message loop to run AsyncDelegateLogger task(s) posted after |
| 6307 | // the |request_delegate| completion task. |
| 6308 | base::RunLoop().RunUntilIdle(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6309 | } |
| 6310 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 6311 | auto entries = net_log.GetEntries(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6312 | |
| 6313 | // Delegate info is always logged in both OnReceivedRedirect and |
| 6314 | // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the |
| 6315 | // OnResponseStarted delegate call is after cancellation, but logging is |
| 6316 | // still currently supported in that call. |
| 6317 | size_t log_position = 0; |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6318 | static const NetLogEventType kExpectedEvents[] = { |
| 6319 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
| 6320 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 6321 | }; |
| 6322 | for (NetLogEventType event : kExpectedEvents) { |
| 6323 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6324 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6325 | entries, log_position, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6326 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6327 | log_position = |
| 6328 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6329 | |
| 6330 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6331 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6332 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6333 | } |
| 6334 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6335 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6336 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6337 | } |
| 6338 | } |
| 6339 | |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6340 | namespace { |
| 6341 | |
| 6342 | const char kExtraHeader[] = "Allow-Snafu"; |
| 6343 | const char kExtraValue[] = "fubar"; |
| 6344 | |
| 6345 | class RedirectWithAdditionalHeadersDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 6346 | void OnReceivedRedirect(URLRequest* request, |
| 6347 | const RedirectInfo& redirect_info, |
| 6348 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6349 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6350 | request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false); |
| 6351 | } |
| 6352 | }; |
| 6353 | |
| 6354 | } // namespace |
| 6355 | |
| 6356 | TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6357 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6358 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6359 | GURL destination_url = |
| 6360 | http_test_server()->GetURL("/echoheader?" + std::string(kExtraHeader)); |
| 6361 | GURL original_url = |
| 6362 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6363 | RedirectWithAdditionalHeadersDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6364 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6365 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6366 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6367 | d.RunUntilComplete(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6368 | |
| 6369 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6370 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6371 | EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value)); |
| 6372 | EXPECT_EQ(kExtraValue, value); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6373 | EXPECT_FALSE(req->is_pending()); |
| 6374 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6375 | EXPECT_EQ(kExtraValue, d.data_received()); |
| 6376 | } |
| 6377 | |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6378 | namespace { |
| 6379 | |
| 6380 | const char kExtraHeaderToRemove[] = "To-Be-Removed"; |
| 6381 | |
| 6382 | class RedirectWithHeaderRemovalDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 6383 | void OnReceivedRedirect(URLRequest* request, |
| 6384 | const RedirectInfo& redirect_info, |
| 6385 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6386 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6387 | request->RemoveRequestHeaderByName(kExtraHeaderToRemove); |
| 6388 | } |
| 6389 | }; |
| 6390 | |
| 6391 | } // namespace |
| 6392 | |
| 6393 | TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6394 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6395 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6396 | GURL destination_url = http_test_server()->GetURL( |
| 6397 | "/echoheader?" + std::string(kExtraHeaderToRemove)); |
| 6398 | GURL original_url = |
| 6399 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6400 | RedirectWithHeaderRemovalDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6401 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6402 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6403 | req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false); |
| 6404 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6405 | d.RunUntilComplete(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6406 | |
| 6407 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6408 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6409 | EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6410 | EXPECT_FALSE(req->is_pending()); |
| 6411 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6412 | EXPECT_EQ("None", d.data_received()); |
| 6413 | } |
| 6414 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6415 | TEST_F(URLRequestTestHTTP, CancelAfterStart) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6416 | TestDelegate d; |
| 6417 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6418 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6419 | GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d, |
| 6420 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6421 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6422 | r->Start(); |
| 6423 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6424 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6425 | r->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6426 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6427 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6428 | |
| 6429 | // We expect to receive OnResponseStarted even though the request has been |
| 6430 | // cancelled. |
| 6431 | EXPECT_EQ(1, d.response_started_count()); |
| 6432 | EXPECT_EQ(0, d.bytes_received()); |
| 6433 | EXPECT_FALSE(d.received_data_before_response()); |
| 6434 | } |
| 6435 | } |
| 6436 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6437 | TEST_F(URLRequestTestHTTP, CancelInResponseStarted) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6438 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6439 | |
| 6440 | TestDelegate d; |
| 6441 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6442 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6443 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6444 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6445 | |
| 6446 | d.set_cancel_in_response_started(true); |
| 6447 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6448 | r->Start(); |
| 6449 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6450 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6451 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6452 | |
| 6453 | EXPECT_EQ(1, d.response_started_count()); |
| 6454 | EXPECT_EQ(0, d.bytes_received()); |
| 6455 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6456 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6457 | } |
| 6458 | } |
| 6459 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6460 | TEST_F(URLRequestTestHTTP, CancelOnDataReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6461 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6462 | |
| 6463 | TestDelegate d; |
| 6464 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6465 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6466 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 6467 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6468 | |
| 6469 | d.set_cancel_in_received_data(true); |
| 6470 | |
| 6471 | r->Start(); |
| 6472 | EXPECT_TRUE(r->is_pending()); |
| 6473 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6474 | d.RunUntilComplete(); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6475 | |
| 6476 | EXPECT_EQ(1, d.response_started_count()); |
| 6477 | EXPECT_NE(0, d.received_bytes_count()); |
| 6478 | EXPECT_FALSE(d.received_data_before_response()); |
| 6479 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
| 6480 | } |
| 6481 | } |
| 6482 | |
| 6483 | TEST_F(URLRequestTestHTTP, CancelDuringEofRead) { |
| 6484 | ASSERT_TRUE(http_test_server()->Start()); |
| 6485 | |
| 6486 | TestDelegate d; |
| 6487 | { |
| 6488 | // This returns an empty response (With headers). |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6489 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6490 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6491 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6492 | |
| 6493 | d.set_cancel_in_received_data(true); |
| 6494 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6495 | r->Start(); |
| 6496 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6497 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6498 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6499 | |
| 6500 | EXPECT_EQ(1, d.response_started_count()); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6501 | EXPECT_EQ(0, d.received_bytes_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6502 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6503 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6504 | } |
| 6505 | } |
| 6506 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6507 | TEST_F(URLRequestTestHTTP, CancelByDestroyingAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6508 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6509 | |
| 6510 | TestDelegate d; |
| 6511 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6512 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6513 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6514 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6515 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6516 | r->Start(); |
| 6517 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6518 | |
| 6519 | // The request will be implicitly canceled when it is destroyed. The |
| 6520 | // test delegate must not post a quit message when this happens because |
| 6521 | // this test doesn't actually have a message loop. The quit message would |
| 6522 | // get put on this thread's message queue and the next test would exit |
| 6523 | // early, causing problems. |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 6524 | d.set_on_complete(base::DoNothing()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6525 | } |
| 6526 | // expect things to just cleanup properly. |
| 6527 | |
kimwjdalsl | 2bb4ff0 | 2015-12-16 22:06:02 | [diff] [blame] | 6528 | // 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] | 6529 | // message loop |
| 6530 | EXPECT_FALSE(d.received_data_before_response()); |
| 6531 | EXPECT_EQ(0, d.bytes_received()); |
| 6532 | } |
| 6533 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6534 | TEST_F(URLRequestTestHTTP, CancelWhileReadingFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6535 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6536 | |
| 6537 | // populate cache |
| 6538 | { |
| 6539 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6540 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6541 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6542 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6543 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6544 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6545 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6546 | } |
| 6547 | |
| 6548 | // cancel read from cache (see bug 990242) |
| 6549 | { |
| 6550 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6551 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6552 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6553 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6554 | r->Start(); |
| 6555 | r->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6556 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6557 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6558 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6559 | EXPECT_EQ(1, d.response_started_count()); |
| 6560 | EXPECT_EQ(0, d.bytes_received()); |
| 6561 | EXPECT_FALSE(d.received_data_before_response()); |
| 6562 | } |
| 6563 | } |
| 6564 | |
| 6565 | TEST_F(URLRequestTestHTTP, PostTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6566 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6567 | HTTPUploadDataOperationTest("POST"); |
| 6568 | } |
| 6569 | |
| 6570 | TEST_F(URLRequestTestHTTP, PutTest) { |
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("PUT"); |
| 6573 | } |
| 6574 | |
| 6575 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
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 | |
| 6578 | TestDelegate d; |
| 6579 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6580 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6581 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6582 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6583 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6584 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6585 | r->Start(); |
| 6586 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6587 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6588 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6589 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6590 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6591 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6592 | |
| 6593 | EXPECT_FALSE(d.received_data_before_response()); |
| 6594 | EXPECT_TRUE(d.data_received().empty()); |
| 6595 | } |
| 6596 | } |
| 6597 | |
| 6598 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6599 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6600 | |
| 6601 | TestDelegate d; |
| 6602 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6603 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6604 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6605 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6606 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6607 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6608 | base::FilePath dir; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 6609 | base::PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 6610 | base::SetCurrentDirectory(dir); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6611 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6612 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6613 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6614 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 6615 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 6616 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6617 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6618 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6619 | base::ThreadTaskRunnerHandle::Get().get(), path, 0, |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6620 | std::numeric_limits<uint64_t>::max(), base::Time())); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6621 | r->set_upload(std::make_unique<ElementsUploadDataStream>( |
| 6622 | std::move(element_readers), 0)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6623 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6624 | r->Start(); |
| 6625 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6626 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6627 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6628 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6629 | int64_t size64 = 0; |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 6630 | ASSERT_EQ(true, base::GetFileSize(path, &size64)); |
| 6631 | ASSERT_LE(size64, std::numeric_limits<int>::max()); |
| 6632 | int size = static_cast<int>(size64); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6633 | std::unique_ptr<char[]> buf(new char[size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6634 | |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 6635 | ASSERT_EQ(size, base::ReadFile(path, buf.get(), size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6636 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6637 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6638 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6639 | |
| 6640 | EXPECT_FALSE(d.received_data_before_response()); |
| 6641 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6642 | EXPECT_EQ(size, d.bytes_received()); |
| 6643 | EXPECT_EQ(std::string(&buf[0], size), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6644 | } |
| 6645 | } |
| 6646 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6647 | TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6648 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6649 | |
| 6650 | TestDelegate d; |
| 6651 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6652 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6653 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6654 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6655 | r->set_method("POST"); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6656 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6657 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6658 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6659 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 6660 | base::ThreadTaskRunnerHandle::Get().get(), |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6661 | base::FilePath(FILE_PATH_LITERAL( |
| 6662 | "c:\\path\\to\\non\\existant\\file.randomness.12345")), |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6663 | 0, std::numeric_limits<uint64_t>::max(), base::Time())); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6664 | r->set_upload(std::make_unique<ElementsUploadDataStream>( |
| 6665 | std::move(element_readers), 0)); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6666 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6667 | r->Start(); |
| 6668 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6669 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6670 | d.RunUntilComplete(); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6671 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6672 | EXPECT_TRUE(d.request_failed()); |
| 6673 | EXPECT_FALSE(d.received_data_before_response()); |
| 6674 | EXPECT_EQ(0, d.bytes_received()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6675 | EXPECT_EQ(ERR_FILE_NOT_FOUND, d.request_status()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6676 | } |
| 6677 | } |
| 6678 | |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6679 | namespace { |
| 6680 | |
| 6681 | // Adds a standard set of data to an upload for chunked upload integration |
| 6682 | // tests. |
| 6683 | void AddDataToUpload(ChunkedUploadDataStream::Writer* writer) { |
| 6684 | writer->AppendData("a", 1, false); |
| 6685 | writer->AppendData("bcd", 3, false); |
| 6686 | writer->AppendData("this is a longer chunk than before.", 35, false); |
| 6687 | writer->AppendData("\r\n\r\n", 4, false); |
| 6688 | writer->AppendData("0", 1, false); |
| 6689 | writer->AppendData("2323", 4, true); |
| 6690 | } |
| 6691 | |
| 6692 | // Checks that the upload data added in AddChunksToUpload() was echoed back from |
| 6693 | // the server. |
| 6694 | void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { |
| 6695 | // This should match the chunks sent by AddChunksToUpload(). |
| 6696 | const std::string expected_data = |
| 6697 | "abcdthis is a longer chunk than before.\r\n\r\n02323"; |
| 6698 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6699 | ASSERT_EQ(1, d->response_started_count()) << "request failed. Error: " |
| 6700 | << d->request_status(); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6701 | |
| 6702 | EXPECT_FALSE(d->received_data_before_response()); |
| 6703 | |
| 6704 | EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); |
| 6705 | EXPECT_EQ(expected_data, d->data_received()); |
| 6706 | } |
| 6707 | |
| 6708 | } // namespace |
| 6709 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6710 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6711 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6712 | |
| 6713 | TestDelegate d; |
| 6714 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6715 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6716 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6717 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6718 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6719 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6720 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6721 | upload_data_stream->CreateWriter(); |
| 6722 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6723 | r->set_method("POST"); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6724 | AddDataToUpload(writer.get()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6725 | r->Start(); |
| 6726 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6727 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6728 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6729 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6730 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6731 | } |
| 6732 | } |
| 6733 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6734 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6735 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6736 | |
| 6737 | TestDelegate d; |
| 6738 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6739 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6740 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6741 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6742 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6743 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6744 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6745 | upload_data_stream->CreateWriter(); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6746 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6747 | r->set_method("POST"); |
| 6748 | r->Start(); |
| 6749 | EXPECT_TRUE(r->is_pending()); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6750 | AddDataToUpload(writer.get()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6751 | d.RunUntilComplete(); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6752 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6753 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6754 | } |
| 6755 | } |
| 6756 | |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6757 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6758 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6759 | |
| 6760 | TestDelegate d; |
| 6761 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6762 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6763 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6764 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6765 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6766 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6767 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6768 | upload_data_stream->CreateWriter(); |
| 6769 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6770 | r->set_method("POST"); |
| 6771 | r->Start(); |
| 6772 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6773 | |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6774 | // Pump messages until we start sending headers.. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6775 | base::RunLoop().RunUntilIdle(); |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6776 | |
| 6777 | // And now wait for completion. |
| 6778 | base::RunLoop run_loop; |
| 6779 | d.set_on_complete(run_loop.QuitClosure()); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6780 | AddDataToUpload(writer.get()); |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6781 | run_loop.Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6782 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6783 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6784 | } |
| 6785 | } |
| 6786 | |
| 6787 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6788 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6789 | |
| 6790 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6791 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6792 | http_test_server()->GetURL("/with-headers.html"), DEFAULT_PRIORITY, &d, |
| 6793 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6794 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6795 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6796 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6797 | const HttpResponseHeaders* headers = req->response_headers(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6798 | |
| 6799 | // Simple sanity check that response_info() accesses the same data. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6800 | EXPECT_EQ(headers, req->response_info().headers.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6801 | |
| 6802 | std::string header; |
| 6803 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 6804 | EXPECT_EQ("private", header); |
| 6805 | |
| 6806 | header.clear(); |
| 6807 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 6808 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 6809 | |
| 6810 | // The response has two "X-Multiple-Entries" headers. |
| 6811 | // This verfies our output has them concatenated together. |
| 6812 | header.clear(); |
| 6813 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 6814 | EXPECT_EQ("a, b", header); |
| 6815 | } |
| 6816 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6817 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 6818 | // security state. (see http://crbug.com/550977). |
| 6819 | #if !defined(OS_IOS) |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6820 | TEST_F(URLRequestTestHTTP, ProcessSTS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6821 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6822 | https_test_server.SetSSLConfig( |
| 6823 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6824 | https_test_server.ServeFilesFromSourceDirectory( |
| 6825 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6826 | ASSERT_TRUE(https_test_server.Start()); |
| 6827 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6828 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6829 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6830 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6831 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6832 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6833 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6834 | d.RunUntilComplete(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6835 | |
| 6836 | TransportSecurityState* security_state = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6837 | default_context().transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6838 | TransportSecurityState::STSState sts_state; |
| 6839 | TransportSecurityState::PKPState pkp_state; |
| 6840 | EXPECT_TRUE( |
| 6841 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6842 | EXPECT_FALSE( |
| 6843 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6844 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6845 | sts_state.upgrade_mode); |
| 6846 | EXPECT_TRUE(sts_state.include_subdomains); |
| 6847 | EXPECT_FALSE(pkp_state.include_subdomains); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6848 | #if defined(OS_ANDROID) |
| 6849 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6850 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6851 | EXPECT_FALSE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6852 | #endif |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6853 | } |
| 6854 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6855 | TEST_F(URLRequestTestHTTP, STSNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6856 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6857 | https_test_server.ServeFilesFromSourceDirectory( |
| 6858 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6859 | ASSERT_TRUE(https_test_server.Start()); |
| 6860 | // Make sure this test fails if the test server is changed to not |
| 6861 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6862 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 6863 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6864 | |
| 6865 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6866 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6867 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6868 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6869 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6870 | d.RunUntilComplete(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6871 | TransportSecurityState* security_state = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6872 | default_context().transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6873 | TransportSecurityState::STSState sts_state; |
| 6874 | EXPECT_FALSE( |
| 6875 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6876 | } |
| 6877 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6878 | namespace { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 6879 | const char kExpectCTStaticHostname[] = "expect-ct.preloaded.test"; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6880 | const char kPKPReportUri[] = "http://report-uri.preloaded.test/pkp"; |
| 6881 | const char kPKPHost[] = "with-report-uri-pkp.preloaded.test"; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6882 | } // namespace |
| 6883 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6884 | // 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] | 6885 | TEST_F(URLRequestTestHTTP, ProcessPKPAndSendReport) { |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6886 | GURL report_uri(kPKPReportUri); |
Hiroki Nakagawa | 4e4967bcb | 2018-10-11 04:09:51 | [diff] [blame] | 6887 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6888 | https_test_server.SetSSLConfig( |
| 6889 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6890 | https_test_server.ServeFilesFromSourceDirectory( |
| 6891 | base::FilePath(kTestFilePath)); |
| 6892 | ASSERT_TRUE(https_test_server.Start()); |
| 6893 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6894 | std::string test_server_hostname = kPKPHost; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6895 | |
| 6896 | // Set up a pin for |test_server_hostname|. |
| 6897 | TransportSecurityState security_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6898 | security_state.EnableStaticPinsForTesting(); |
| 6899 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6900 | |
| 6901 | MockCertificateReportSender mock_report_sender; |
| 6902 | security_state.SetReportSender(&mock_report_sender); |
| 6903 | |
| 6904 | // Set up a MockCertVerifier to trigger a violation of the previously |
| 6905 | // set pin. |
| 6906 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6907 | ASSERT_TRUE(cert); |
| 6908 | |
| 6909 | MockCertVerifier cert_verifier; |
| 6910 | CertVerifyResult verify_result; |
| 6911 | verify_result.verified_cert = cert; |
| 6912 | verify_result.is_issued_by_known_root = true; |
| 6913 | HashValue hash3; |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6914 | ASSERT_TRUE( |
| 6915 | hash3.FromString("sha256/3333333333333333333333333333333333333333333=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6916 | verify_result.public_key_hashes.push_back(hash3); |
| 6917 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6918 | |
| 6919 | TestNetworkDelegate network_delegate; |
| 6920 | TestURLRequestContext context(true); |
| 6921 | context.set_transport_security_state(&security_state); |
| 6922 | context.set_network_delegate(&network_delegate); |
| 6923 | context.set_cert_verifier(&cert_verifier); |
| 6924 | context.Init(); |
| 6925 | |
| 6926 | // Now send a request to trigger the violation. |
| 6927 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6928 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6929 | https_test_server.GetURL(test_server_hostname, "/simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6930 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6931 | violating_request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6932 | d.RunUntilComplete(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6933 | |
| 6934 | // Check that a report was sent. |
| 6935 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 6936 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 6937 | EXPECT_EQ("application/json; charset=utf-8", |
| 6938 | mock_report_sender.latest_content_type()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6939 | std::unique_ptr<base::Value> value( |
Lei Zhang | a8b4c5fb | 2019-02-16 03:02:03 | [diff] [blame] | 6940 | base::JSONReader::ReadDeprecated(mock_report_sender.latest_report())); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6941 | ASSERT_TRUE(value); |
jdoerrie | e48b26a | 2017-12-09 14:19:08 | [diff] [blame] | 6942 | ASSERT_TRUE(value->is_dict()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6943 | base::DictionaryValue* report_dict; |
| 6944 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 6945 | std::string report_hostname; |
| 6946 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 6947 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 6948 | } |
| 6949 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6950 | // Tests that reports do not get sent on requests to static pkp hosts that |
| 6951 | // don't have pin violations. |
| 6952 | TEST_F(URLRequestTestHTTP, ProcessPKPWithNoViolation) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6953 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6954 | https_test_server.SetSSLConfig( |
| 6955 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6956 | https_test_server.ServeFilesFromSourceDirectory( |
| 6957 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6958 | ASSERT_TRUE(https_test_server.Start()); |
| 6959 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6960 | std::string test_server_hostname = kPKPHost; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6961 | |
| 6962 | TransportSecurityState security_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6963 | security_state.EnableStaticPinsForTesting(); |
| 6964 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6965 | MockCertificateReportSender mock_report_sender; |
| 6966 | security_state.SetReportSender(&mock_report_sender); |
| 6967 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6968 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6969 | ASSERT_TRUE(cert); |
Hiroki Nakagawa | 4e4967bcb | 2018-10-11 04:09:51 | [diff] [blame] | 6970 | MockCertVerifier mock_cert_verifier; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6971 | CertVerifyResult verify_result; |
| 6972 | verify_result.verified_cert = cert; |
| 6973 | verify_result.is_issued_by_known_root = true; |
| 6974 | HashValue hash; |
| 6975 | // The expected value of GoodPin1 used by |test_default::kHSTSSource|. |
| 6976 | ASSERT_TRUE( |
| 6977 | hash.FromString("sha256/Nn8jk5By4Vkq6BeOVZ7R7AC6XUUBZsWmUbJR1f1Y5FY=")); |
| 6978 | verify_result.public_key_hashes.push_back(hash); |
| 6979 | mock_cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6980 | |
| 6981 | TestNetworkDelegate network_delegate; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6982 | TestURLRequestContext context(true); |
| 6983 | context.set_transport_security_state(&security_state); |
| 6984 | context.set_network_delegate(&network_delegate); |
| 6985 | context.set_cert_verifier(&mock_cert_verifier); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6986 | context.Init(); |
| 6987 | |
| 6988 | // Now send a request that does not trigger the violation. |
| 6989 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6990 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6991 | https_test_server.GetURL(test_server_hostname, "/simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6992 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6993 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6994 | d.RunUntilComplete(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6995 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6996 | // Check that the request succeeded, a report was not sent and the pkp was |
| 6997 | // not bypassed. |
| 6998 | EXPECT_EQ(OK, d.request_status()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6999 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 7000 | EXPECT_EQ(std::string(), mock_report_sender.latest_report()); |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7001 | TransportSecurityState::STSState sts_state; |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 7002 | TransportSecurityState::PKPState pkp_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7003 | EXPECT_TRUE(security_state.GetStaticDomainState(test_server_hostname, |
| 7004 | &sts_state, &pkp_state)); |
| 7005 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
| 7006 | EXPECT_FALSE(request->ssl_info().pkp_bypassed); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 7007 | } |
| 7008 | |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7009 | TEST_F(URLRequestTestHTTP, PKPBypassRecorded) { |
| 7010 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7011 | https_test_server.SetSSLConfig( |
| 7012 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7013 | https_test_server.ServeFilesFromSourceDirectory( |
| 7014 | base::FilePath(kTestFilePath)); |
| 7015 | ASSERT_TRUE(https_test_server.Start()); |
| 7016 | |
| 7017 | // Set up a MockCertVerifier to be a local root that violates the pin |
| 7018 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7019 | ASSERT_TRUE(cert); |
| 7020 | |
| 7021 | MockCertVerifier cert_verifier; |
| 7022 | CertVerifyResult verify_result; |
| 7023 | verify_result.verified_cert = cert; |
| 7024 | verify_result.is_issued_by_known_root = false; |
| 7025 | HashValue hash; |
| 7026 | ASSERT_TRUE( |
| 7027 | hash.FromString("sha256/1111111111111111111111111111111111111111111=")); |
| 7028 | verify_result.public_key_hashes.push_back(hash); |
| 7029 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7030 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7031 | std::string test_server_hostname = kPKPHost; |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7032 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7033 | // Set up PKP |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7034 | TransportSecurityState security_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7035 | security_state.EnableStaticPinsForTesting(); |
| 7036 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 7037 | MockCertificateReportSender mock_report_sender; |
| 7038 | security_state.SetReportSender(&mock_report_sender); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7039 | |
| 7040 | TestNetworkDelegate network_delegate; |
| 7041 | TestURLRequestContext context(true); |
| 7042 | context.set_transport_security_state(&security_state); |
| 7043 | context.set_network_delegate(&network_delegate); |
| 7044 | context.set_cert_verifier(&cert_verifier); |
| 7045 | context.Init(); |
| 7046 | |
| 7047 | TestDelegate d; |
| 7048 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7049 | https_test_server.GetURL(test_server_hostname, "/simple.html"), |
| 7050 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7051 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7052 | d.RunUntilComplete(); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7053 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7054 | // Check that the request succeeded, a report was not sent and the PKP was |
| 7055 | // bypassed. |
| 7056 | EXPECT_EQ(OK, d.request_status()); |
| 7057 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 7058 | EXPECT_EQ(std::string(), mock_report_sender.latest_report()); |
| 7059 | TransportSecurityState::STSState sts_state; |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7060 | TransportSecurityState::PKPState pkp_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7061 | EXPECT_TRUE(security_state.GetStaticDomainState(test_server_hostname, |
| 7062 | &sts_state, &pkp_state)); |
| 7063 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7064 | EXPECT_TRUE(request->ssl_info().pkp_bypassed); |
| 7065 | } |
| 7066 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7067 | TEST_F(URLRequestTestHTTP, ProcessSTSOnce) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7068 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7069 | https_test_server.SetSSLConfig( |
| 7070 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7071 | https_test_server.ServeFilesFromSourceDirectory( |
| 7072 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7073 | ASSERT_TRUE(https_test_server.Start()); |
| 7074 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7075 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 7076 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7077 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7078 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7079 | https_test_server.GetURL("/hsts-multiple-headers.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7080 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7081 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7082 | d.RunUntilComplete(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7083 | |
| 7084 | // We should have set parameters from the first header, not the second. |
| 7085 | TransportSecurityState* security_state = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7086 | default_context().transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 7087 | TransportSecurityState::STSState sts_state; |
| 7088 | EXPECT_TRUE( |
| 7089 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 7090 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 7091 | sts_state.upgrade_mode); |
| 7092 | EXPECT_FALSE(sts_state.include_subdomains); |
| 7093 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7094 | } |
| 7095 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7096 | // An ExpectCTReporter that records the number of times OnExpectCTFailed() was |
| 7097 | // called. |
| 7098 | class MockExpectCTReporter : public TransportSecurityState::ExpectCTReporter { |
| 7099 | public: |
| 7100 | MockExpectCTReporter() : num_failures_(0) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 7101 | ~MockExpectCTReporter() override = default; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7102 | |
| 7103 | void OnExpectCTFailed(const HostPortPair& host_port_pair, |
| 7104 | const GURL& report_uri, |
estark | ae028b46 | 2017-06-20 23:25:01 | [diff] [blame] | 7105 | base::Time expiration, |
estark | bf1b5296 | 2017-05-05 17:05:25 | [diff] [blame] | 7106 | const X509Certificate* validated_certificate_chain, |
| 7107 | const X509Certificate* served_certificate_chain, |
| 7108 | const SignedCertificateTimestampAndStatusList& |
| 7109 | signed_certificate_timestamps) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7110 | num_failures_++; |
| 7111 | } |
| 7112 | |
| 7113 | uint32_t num_failures() { return num_failures_; } |
| 7114 | |
| 7115 | private: |
| 7116 | uint32_t num_failures_; |
| 7117 | }; |
| 7118 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7119 | // A CTPolicyEnforcer that returns a default CTPolicyCompliance value |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7120 | // for every certificate. |
| 7121 | class MockCTPolicyEnforcer : public CTPolicyEnforcer { |
| 7122 | public: |
| 7123 | MockCTPolicyEnforcer() |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7124 | : default_result_(ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 7125 | ~MockCTPolicyEnforcer() override = default; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7126 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7127 | ct::CTPolicyCompliance CheckCompliance( |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7128 | X509Certificate* cert, |
Ryan Sleevi | 8a9c9c1 | 2018-05-09 02:36:23 | [diff] [blame] | 7129 | const ct::SCTList& verified_scts, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 7130 | const NetLogWithSource& net_log) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7131 | return default_result_; |
| 7132 | } |
| 7133 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7134 | void set_default_result(ct::CTPolicyCompliance default_result) { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7135 | default_result_ = default_result; |
| 7136 | } |
| 7137 | |
| 7138 | private: |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7139 | ct::CTPolicyCompliance default_result_; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7140 | }; |
| 7141 | |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7142 | // Tests that Expect CT headers for the preload list are processed correctly. |
| 7143 | TEST_F(URLRequestTestHTTP, PreloadExpectCTHeader) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 7144 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 7145 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7146 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7147 | https_test_server.SetSSLConfig( |
| 7148 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7149 | https_test_server.ServeFilesFromSourceDirectory( |
| 7150 | base::FilePath(kTestFilePath)); |
| 7151 | ASSERT_TRUE(https_test_server.Start()); |
| 7152 | |
| 7153 | MockExpectCTReporter reporter; |
| 7154 | TransportSecurityState transport_security_state; |
| 7155 | transport_security_state.enable_static_expect_ct_ = true; |
| 7156 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7157 | |
| 7158 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7159 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7160 | ASSERT_TRUE(cert); |
| 7161 | MockCertVerifier cert_verifier; |
| 7162 | CertVerifyResult verify_result; |
| 7163 | verify_result.verified_cert = cert; |
| 7164 | verify_result.is_issued_by_known_root = true; |
| 7165 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7166 | |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 7167 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to trigger an Expect |
| 7168 | // CT violation. |
| 7169 | DoNothingCTVerifier ct_verifier; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7170 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7171 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7172 | ct::CTPolicyCompliance::CT_POLICY_NOT_ENOUGH_SCTS); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7173 | |
| 7174 | TestNetworkDelegate network_delegate; |
| 7175 | // Use a MockHostResolver (which by default maps all hosts to |
| 7176 | // 127.0.0.1) so that the request can be sent to a site on the Expect |
| 7177 | // CT preload list. |
| 7178 | MockHostResolver host_resolver; |
| 7179 | TestURLRequestContext context(true); |
| 7180 | context.set_host_resolver(&host_resolver); |
| 7181 | context.set_transport_security_state(&transport_security_state); |
| 7182 | context.set_network_delegate(&network_delegate); |
| 7183 | context.set_cert_verifier(&cert_verifier); |
| 7184 | context.set_cert_transparency_verifier(&ct_verifier); |
vabr | 13d0074 | 2017-06-05 10:31:46 | [diff] [blame] | 7185 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7186 | context.Init(); |
| 7187 | |
| 7188 | // Now send a request to trigger the violation. |
| 7189 | TestDelegate d; |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7190 | GURL url = https_test_server.GetURL("/expect-ct-header-preload.html"); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7191 | GURL::Replacements replace_host; |
| 7192 | replace_host.SetHostStr(kExpectCTStaticHostname); |
| 7193 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7194 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
| 7195 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7196 | violating_request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7197 | d.RunUntilComplete(); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7198 | |
| 7199 | EXPECT_EQ(1u, reporter.num_failures()); |
| 7200 | } |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7201 | |
| 7202 | // Tests that Expect CT HTTP headers are processed correctly. |
| 7203 | TEST_F(URLRequestTestHTTP, ExpectCTHeader) { |
| 7204 | base::test::ScopedFeatureList feature_list; |
| 7205 | feature_list.InitAndEnableFeature( |
| 7206 | TransportSecurityState::kDynamicExpectCTFeature); |
| 7207 | |
| 7208 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7209 | https_test_server.SetSSLConfig( |
| 7210 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7211 | https_test_server.ServeFilesFromSourceDirectory( |
| 7212 | base::FilePath(kTestFilePath)); |
| 7213 | ASSERT_TRUE(https_test_server.Start()); |
| 7214 | |
| 7215 | MockExpectCTReporter reporter; |
| 7216 | TransportSecurityState transport_security_state; |
| 7217 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7218 | |
| 7219 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7220 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7221 | ASSERT_TRUE(cert); |
| 7222 | MockCertVerifier cert_verifier; |
| 7223 | CertVerifyResult verify_result; |
| 7224 | verify_result.verified_cert = cert; |
| 7225 | verify_result.is_issued_by_known_root = true; |
| 7226 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7227 | |
| 7228 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 7229 | // compliance. |
| 7230 | DoNothingCTVerifier ct_verifier; |
| 7231 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7232 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7233 | ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7234 | |
| 7235 | TestNetworkDelegate network_delegate; |
| 7236 | // Use a MockHostResolver (which by default maps all hosts to |
| 7237 | // 127.0.0.1). |
| 7238 | MockHostResolver host_resolver; |
| 7239 | TestURLRequestContext context(true); |
| 7240 | context.set_host_resolver(&host_resolver); |
| 7241 | context.set_transport_security_state(&transport_security_state); |
| 7242 | context.set_network_delegate(&network_delegate); |
| 7243 | context.set_cert_verifier(&cert_verifier); |
| 7244 | context.set_cert_transparency_verifier(&ct_verifier); |
| 7245 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 7246 | context.Init(); |
| 7247 | |
| 7248 | // Now send a request to trigger the header processing. |
| 7249 | TestDelegate d; |
| 7250 | GURL url = https_test_server.GetURL("/expect-ct-header.html"); |
| 7251 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7252 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7253 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7254 | d.RunUntilComplete(); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7255 | |
| 7256 | TransportSecurityState::ExpectCTState state; |
| 7257 | ASSERT_TRUE( |
| 7258 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 7259 | EXPECT_TRUE(state.enforce); |
| 7260 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 7261 | } |
| 7262 | |
| 7263 | // Tests that if multiple Expect CT HTTP headers are sent, they are all |
| 7264 | // processed. |
| 7265 | TEST_F(URLRequestTestHTTP, MultipleExpectCTHeaders) { |
| 7266 | base::test::ScopedFeatureList feature_list; |
| 7267 | feature_list.InitAndEnableFeature( |
| 7268 | TransportSecurityState::kDynamicExpectCTFeature); |
| 7269 | |
| 7270 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7271 | https_test_server.SetSSLConfig( |
| 7272 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7273 | https_test_server.ServeFilesFromSourceDirectory( |
| 7274 | base::FilePath(kTestFilePath)); |
| 7275 | ASSERT_TRUE(https_test_server.Start()); |
| 7276 | |
| 7277 | MockExpectCTReporter reporter; |
| 7278 | TransportSecurityState transport_security_state; |
| 7279 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7280 | |
| 7281 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7282 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7283 | ASSERT_TRUE(cert); |
| 7284 | MockCertVerifier cert_verifier; |
| 7285 | CertVerifyResult verify_result; |
| 7286 | verify_result.verified_cert = cert; |
| 7287 | verify_result.is_issued_by_known_root = true; |
| 7288 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7289 | |
| 7290 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 7291 | // compliance. |
| 7292 | DoNothingCTVerifier ct_verifier; |
| 7293 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7294 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7295 | ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7296 | |
| 7297 | TestNetworkDelegate network_delegate; |
| 7298 | // Use a MockHostResolver (which by default maps all hosts to |
| 7299 | // 127.0.0.1). |
| 7300 | MockHostResolver host_resolver; |
| 7301 | TestURLRequestContext context(true); |
| 7302 | context.set_host_resolver(&host_resolver); |
| 7303 | context.set_transport_security_state(&transport_security_state); |
| 7304 | context.set_network_delegate(&network_delegate); |
| 7305 | context.set_cert_verifier(&cert_verifier); |
| 7306 | context.set_cert_transparency_verifier(&ct_verifier); |
| 7307 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 7308 | context.Init(); |
| 7309 | |
| 7310 | // Now send a request to trigger the header processing. |
| 7311 | TestDelegate d; |
| 7312 | GURL url = https_test_server.GetURL("/expect-ct-header-multiple.html"); |
| 7313 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7314 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7315 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7316 | d.RunUntilComplete(); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7317 | |
| 7318 | TransportSecurityState::ExpectCTState state; |
| 7319 | ASSERT_TRUE( |
| 7320 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 7321 | EXPECT_TRUE(state.enforce); |
| 7322 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 7323 | } |
| 7324 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 7325 | #endif // !defined(OS_IOS) |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7326 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7327 | #if BUILDFLAG(ENABLE_REPORTING) |
| 7328 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7329 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_DontReportIfNetworkNotAccessed) { |
| 7330 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7331 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7332 | ASSERT_TRUE(https_test_server.Start()); |
| 7333 | GURL request_url = https_test_server.GetURL("/cachetime"); |
| 7334 | |
| 7335 | TestNetworkErrorLoggingService nel_service; |
| 7336 | TestURLRequestContext context(true); |
| 7337 | context.set_network_error_logging_service(&nel_service); |
| 7338 | context.Init(); |
| 7339 | |
| 7340 | // Populate the cache. |
| 7341 | TestDelegate d; |
| 7342 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7343 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7344 | request->Start(); |
| 7345 | d.RunUntilComplete(); |
| 7346 | |
| 7347 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7348 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7349 | nel_service.errors()[0]; |
| 7350 | EXPECT_EQ(request_url, error.uri); |
| 7351 | EXPECT_EQ(200, error.status_code); |
| 7352 | EXPECT_EQ(OK, error.type); |
| 7353 | |
| 7354 | request = context.CreateRequest(request_url, DEFAULT_PRIORITY, &d, |
| 7355 | TRAFFIC_ANNOTATION_FOR_TESTS); |
| 7356 | request->Start(); |
| 7357 | d.RunUntilComplete(); |
| 7358 | |
| 7359 | EXPECT_FALSE(request->response_info().network_accessed); |
| 7360 | EXPECT_TRUE(request->response_info().was_cached); |
| 7361 | // No additional NEL report was generated. |
| 7362 | EXPECT_EQ(1u, nel_service.errors().size()); |
| 7363 | } |
| 7364 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7365 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_BasicSuccess) { |
| 7366 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7367 | https_test_server.ServeFilesFromSourceDirectory( |
| 7368 | base::FilePath(kTestFilePath)); |
| 7369 | ASSERT_TRUE(https_test_server.Start()); |
| 7370 | GURL request_url = https_test_server.GetURL("/simple.html"); |
| 7371 | |
| 7372 | TestNetworkErrorLoggingService nel_service; |
| 7373 | TestURLRequestContext context(true); |
| 7374 | context.set_network_error_logging_service(&nel_service); |
| 7375 | context.Init(); |
| 7376 | |
| 7377 | TestDelegate d; |
| 7378 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7379 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7380 | request->Start(); |
| 7381 | d.RunUntilComplete(); |
| 7382 | |
| 7383 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7384 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7385 | nel_service.errors()[0]; |
| 7386 | EXPECT_EQ(request_url, error.uri); |
| 7387 | EXPECT_EQ(200, error.status_code); |
| 7388 | EXPECT_EQ(OK, error.type); |
| 7389 | } |
| 7390 | |
| 7391 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_BasicError) { |
| 7392 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7393 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7394 | ASSERT_TRUE(https_test_server.Start()); |
| 7395 | GURL request_url = https_test_server.GetURL("/close-socket"); |
| 7396 | |
| 7397 | TestNetworkErrorLoggingService nel_service; |
| 7398 | TestURLRequestContext context(true); |
| 7399 | context.set_network_error_logging_service(&nel_service); |
| 7400 | context.Init(); |
| 7401 | |
| 7402 | TestDelegate d; |
| 7403 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7404 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7405 | request->Start(); |
| 7406 | d.RunUntilComplete(); |
| 7407 | |
| 7408 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7409 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7410 | nel_service.errors()[0]; |
| 7411 | EXPECT_EQ(request_url, error.uri); |
| 7412 | EXPECT_EQ(0, error.status_code); |
| 7413 | EXPECT_EQ(ERR_EMPTY_RESPONSE, error.type); |
| 7414 | } |
| 7415 | |
| 7416 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_Redirect) { |
| 7417 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7418 | https_test_server.ServeFilesFromSourceDirectory( |
| 7419 | base::FilePath(kTestFilePath)); |
| 7420 | ASSERT_TRUE(https_test_server.Start()); |
| 7421 | GURL request_url = https_test_server.GetURL("/redirect-test.html"); |
| 7422 | GURL redirect_url = https_test_server.GetURL("/with-headers.html"); |
| 7423 | |
| 7424 | TestNetworkErrorLoggingService nel_service; |
| 7425 | TestURLRequestContext context(true); |
| 7426 | context.set_network_error_logging_service(&nel_service); |
| 7427 | context.Init(); |
| 7428 | |
| 7429 | TestDelegate d; |
| 7430 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7431 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7432 | request->Start(); |
| 7433 | d.RunUntilComplete(); |
| 7434 | |
| 7435 | ASSERT_EQ(2u, nel_service.errors().size()); |
| 7436 | const TestNetworkErrorLoggingService::RequestDetails& error1 = |
| 7437 | nel_service.errors()[0]; |
| 7438 | EXPECT_EQ(request_url, error1.uri); |
| 7439 | EXPECT_EQ(302, error1.status_code); |
| 7440 | EXPECT_EQ(OK, error1.type); |
| 7441 | const TestNetworkErrorLoggingService::RequestDetails& error2 = |
| 7442 | nel_service.errors()[1]; |
| 7443 | EXPECT_EQ(redirect_url, error2.uri); |
| 7444 | EXPECT_EQ(200, error2.status_code); |
| 7445 | EXPECT_EQ(OK, error2.type); |
| 7446 | } |
| 7447 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7448 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_RedirectWithoutLocationHeader) { |
| 7449 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7450 | https_test_server.ServeFilesFromSourceDirectory( |
| 7451 | base::FilePath(kTestFilePath)); |
| 7452 | ASSERT_TRUE(https_test_server.Start()); |
| 7453 | GURL request_url = https_test_server.GetURL("/308-without-location-header"); |
| 7454 | |
| 7455 | TestNetworkErrorLoggingService nel_service; |
| 7456 | TestURLRequestContext context(true); |
| 7457 | context.set_network_error_logging_service(&nel_service); |
| 7458 | context.Init(); |
| 7459 | |
| 7460 | TestDelegate d; |
| 7461 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7462 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7463 | request->Start(); |
| 7464 | d.RunUntilComplete(); |
| 7465 | |
| 7466 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7467 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7468 | nel_service.errors()[0]; |
| 7469 | EXPECT_EQ(request_url, error.uri); |
| 7470 | EXPECT_EQ(308, error.status_code); |
| 7471 | // The body of the response was successfully read. |
| 7472 | EXPECT_EQ(OK, error.type); |
| 7473 | } |
| 7474 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7475 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_Auth) { |
| 7476 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7477 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7478 | ASSERT_TRUE(https_test_server.Start()); |
| 7479 | GURL request_url = https_test_server.GetURL("/auth-basic"); |
| 7480 | |
| 7481 | TestNetworkErrorLoggingService nel_service; |
| 7482 | TestURLRequestContext context(true); |
| 7483 | context.set_network_error_logging_service(&nel_service); |
| 7484 | context.Init(); |
| 7485 | |
| 7486 | TestDelegate d; |
| 7487 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7488 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7489 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7490 | request->Start(); |
| 7491 | d.RunUntilComplete(); |
| 7492 | |
| 7493 | ASSERT_EQ(2u, nel_service.errors().size()); |
| 7494 | const TestNetworkErrorLoggingService::RequestDetails& error1 = |
| 7495 | nel_service.errors()[0]; |
| 7496 | EXPECT_EQ(request_url, error1.uri); |
| 7497 | EXPECT_EQ(401, error1.status_code); |
| 7498 | EXPECT_EQ(OK, error1.type); |
| 7499 | const TestNetworkErrorLoggingService::RequestDetails& error2 = |
| 7500 | nel_service.errors()[1]; |
| 7501 | EXPECT_EQ(request_url, error2.uri); |
| 7502 | EXPECT_EQ(200, error2.status_code); |
| 7503 | EXPECT_EQ(OK, error2.type); |
| 7504 | } |
| 7505 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7506 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_304Response) { |
| 7507 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7508 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7509 | ASSERT_TRUE(https_test_server.Start()); |
| 7510 | GURL request_url = https_test_server.GetURL("/auth-basic"); |
| 7511 | |
| 7512 | TestNetworkErrorLoggingService nel_service; |
| 7513 | TestURLRequestContext context(true); |
| 7514 | context.set_network_error_logging_service(&nel_service); |
| 7515 | context.Init(); |
| 7516 | |
| 7517 | // populate the cache |
| 7518 | { |
| 7519 | TestDelegate d; |
| 7520 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7521 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 7522 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7523 | r->Start(); |
| 7524 | d.RunUntilComplete(); |
| 7525 | } |
| 7526 | ASSERT_EQ(2u, nel_service.errors().size()); |
| 7527 | const TestNetworkErrorLoggingService::RequestDetails& error1 = |
| 7528 | nel_service.errors()[0]; |
| 7529 | EXPECT_EQ(request_url, error1.uri); |
| 7530 | EXPECT_EQ(401, error1.status_code); |
| 7531 | EXPECT_EQ(OK, error1.type); |
| 7532 | const TestNetworkErrorLoggingService::RequestDetails& error2 = |
| 7533 | nel_service.errors()[1]; |
| 7534 | EXPECT_EQ(request_url, error2.uri); |
| 7535 | EXPECT_EQ(200, error2.status_code); |
| 7536 | EXPECT_EQ(OK, error2.type); |
| 7537 | |
| 7538 | // repeat request with end-to-end validation. since auth-basic results in a |
| 7539 | // cachable page, we expect this test to result in a 304. in which case, the |
| 7540 | // response should be fetched from the cache. |
| 7541 | { |
| 7542 | TestDelegate d; |
| 7543 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7544 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 7545 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7546 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 7547 | r->Start(); |
| 7548 | d.RunUntilComplete(); |
| 7549 | |
| 7550 | // Should be the same cached document. |
| 7551 | EXPECT_TRUE(r->was_cached()); |
| 7552 | } |
| 7553 | ASSERT_EQ(3u, nel_service.errors().size()); |
| 7554 | const TestNetworkErrorLoggingService::RequestDetails& error3 = |
| 7555 | nel_service.errors()[2]; |
| 7556 | EXPECT_EQ(request_url, error3.uri); |
| 7557 | EXPECT_EQ(304, error3.status_code); |
| 7558 | EXPECT_EQ(OK, error3.type); |
| 7559 | } |
| 7560 | |
| 7561 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_CancelInResponseStarted) { |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7562 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7563 | https_test_server.ServeFilesFromSourceDirectory( |
| 7564 | base::FilePath(kTestFilePath)); |
| 7565 | ASSERT_TRUE(https_test_server.Start()); |
| 7566 | GURL request_url = https_test_server.GetURL("/simple.html"); |
| 7567 | |
| 7568 | TestNetworkErrorLoggingService nel_service; |
| 7569 | TestURLRequestContext context(true); |
| 7570 | context.set_network_error_logging_service(&nel_service); |
| 7571 | context.Init(); |
| 7572 | |
| 7573 | TestDelegate d; |
| 7574 | d.set_cancel_in_response_started(true); |
| 7575 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7576 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7577 | request->Start(); |
| 7578 | d.RunUntilComplete(); |
| 7579 | |
| 7580 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7581 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7582 | nel_service.errors()[0]; |
| 7583 | EXPECT_EQ(request_url, error.uri); |
| 7584 | EXPECT_EQ(200, error.status_code); |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7585 | // Headers were received and the body should have been read but was not. |
| 7586 | EXPECT_EQ(ERR_ABORTED, error.type); |
| 7587 | } |
| 7588 | |
| 7589 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_CancelOnDataReceived) { |
| 7590 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7591 | https_test_server.ServeFilesFromSourceDirectory( |
| 7592 | base::FilePath(kTestFilePath)); |
| 7593 | ASSERT_TRUE(https_test_server.Start()); |
| 7594 | GURL request_url = https_test_server.GetURL("/simple.html"); |
| 7595 | |
| 7596 | TestNetworkErrorLoggingService nel_service; |
| 7597 | TestURLRequestContext context(true); |
| 7598 | context.set_network_error_logging_service(&nel_service); |
| 7599 | context.Init(); |
| 7600 | |
| 7601 | TestDelegate d; |
| 7602 | d.set_cancel_in_received_data(true); |
| 7603 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7604 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7605 | request->Start(); |
| 7606 | d.RunUntilComplete(); |
| 7607 | |
| 7608 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7609 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7610 | nel_service.errors()[0]; |
| 7611 | EXPECT_EQ(request_url, error.uri); |
| 7612 | EXPECT_EQ(200, error.status_code); |
| 7613 | // Data was received but the body was not completely read. |
| 7614 | EXPECT_EQ(ERR_ABORTED, error.type); |
| 7615 | } |
| 7616 | |
| 7617 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_CancelRedirect) { |
| 7618 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7619 | https_test_server.ServeFilesFromSourceDirectory( |
| 7620 | base::FilePath(kTestFilePath)); |
| 7621 | ASSERT_TRUE(https_test_server.Start()); |
| 7622 | GURL request_url = https_test_server.GetURL("/redirect-test.html"); |
| 7623 | |
| 7624 | TestNetworkErrorLoggingService nel_service; |
| 7625 | TestURLRequestContext context(true); |
| 7626 | context.set_network_error_logging_service(&nel_service); |
| 7627 | context.Init(); |
| 7628 | |
| 7629 | TestDelegate d; |
| 7630 | d.set_cancel_in_received_redirect(true); |
| 7631 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7632 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7633 | request->Start(); |
| 7634 | d.RunUntilComplete(); |
| 7635 | |
| 7636 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7637 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7638 | nel_service.errors()[0]; |
| 7639 | EXPECT_EQ(request_url, error.uri); |
| 7640 | EXPECT_EQ(302, error.status_code); |
| 7641 | // A valid HTTP response was received, even though the request was cancelled. |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7642 | EXPECT_EQ(OK, error.type); |
| 7643 | } |
| 7644 | |
| 7645 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 7646 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7647 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7648 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7649 | |
| 7650 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7651 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7652 | http_test_server()->GetURL("/content-type-normalization.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7653 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7654 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7655 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7656 | |
| 7657 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7658 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7659 | EXPECT_EQ("text/html", mime_type); |
| 7660 | |
| 7661 | std::string charset; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7662 | req->GetCharset(&charset); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7663 | EXPECT_EQ("utf-8", charset); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7664 | req->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7665 | } |
| 7666 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7667 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) { |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7668 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7669 | GURL data_url("data:,foo"); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7670 | DataProtocolHandler data_protocol_handler; |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7671 | EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7672 | |
| 7673 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7674 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7675 | } |
| 7676 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7677 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7678 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) { |
| 7679 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
| 7680 | GURL file_url("file:///foo.txt"); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 7681 | FileProtocolHandler file_protocol_handler( |
| 7682 | base::ThreadTaskRunnerHandle::Get()); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7683 | EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url)); |
| 7684 | |
| 7685 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7686 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(file_url)); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7687 | } |
| 7688 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7689 | TEST_F(URLRequestTestHTTP, RestrictFileRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7690 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7691 | |
| 7692 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7693 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7694 | http_test_server()->GetURL("/redirect-to-file.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7695 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7696 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7697 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7698 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7699 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7700 | |
| 7701 | // The redirect should have been rejected before reporting it to the caller. |
| 7702 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7703 | } |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7704 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7705 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7706 | TEST_F(URLRequestTestHTTP, RestrictDataRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7707 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7708 | |
| 7709 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7710 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7711 | http_test_server()->GetURL("/redirect-to-data.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7712 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7713 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7714 | d.RunUntilComplete(); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7715 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7716 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7717 | |
| 7718 | // The redirect should have been rejected before reporting it to the |
| 7719 | // caller. See https://crbug.com/723796 |
| 7720 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7721 | } |
| 7722 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7723 | // Test that redirects to invalid URLs are rejected. See |
| 7724 | // https://crbug.com/462272. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7725 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7726 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7727 | |
| 7728 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7729 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7730 | http_test_server()->GetURL("/redirect-to-invalid-url.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7731 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7732 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7733 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7734 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7735 | EXPECT_EQ(1, d.response_started_count()); |
| 7736 | EXPECT_EQ(ERR_INVALID_REDIRECT, d.request_status()); |
| 7737 | |
| 7738 | // The redirect should have been rejected before reporting it to the caller. |
| 7739 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7740 | } |
| 7741 | |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7742 | // Make sure redirects are cached, despite not reading their bodies. |
| 7743 | TEST_F(URLRequestTestHTTP, CacheRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7744 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7745 | GURL redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7746 | http_test_server()->GetURL("/redirect302-to-echo-cacheable"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7747 | |
| 7748 | { |
| 7749 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7750 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7751 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7752 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7753 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7754 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7755 | EXPECT_EQ(1, d.received_redirect_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7756 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7757 | } |
| 7758 | |
| 7759 | { |
| 7760 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7761 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7762 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7763 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 7764 | d.RunUntilRedirect(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7765 | |
| 7766 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7767 | EXPECT_EQ(0, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7768 | EXPECT_TRUE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7769 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 7770 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 7771 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7772 | d.RunUntilComplete(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7773 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7774 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7775 | EXPECT_EQ(OK, d.request_status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7776 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7777 | } |
| 7778 | } |
| 7779 | |
| 7780 | // Make sure a request isn't cached when a NetworkDelegate forces a redirect |
| 7781 | // when the headers are read, since the body won't have been read. |
| 7782 | TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7783 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7784 | // URL that is normally cached. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7785 | GURL initial_url = http_test_server()->GetURL("/cachetime"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7786 | |
| 7787 | { |
| 7788 | // Set up the TestNetworkDelegate tp force a redirect. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7789 | GURL redirect_to_url = http_test_server()->GetURL("/echo"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7790 | default_network_delegate_.set_redirect_on_headers_received_url( |
| 7791 | redirect_to_url); |
| 7792 | |
| 7793 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7794 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7795 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7796 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7797 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7798 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7799 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7800 | EXPECT_EQ(redirect_to_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7801 | } |
| 7802 | |
| 7803 | { |
| 7804 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7805 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7806 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7807 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7808 | d.RunUntilComplete(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7809 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7810 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7811 | EXPECT_FALSE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7812 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7813 | EXPECT_EQ(initial_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7814 | } |
| 7815 | } |
| 7816 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7817 | // Tests that redirection to an unsafe URL is allowed when it has been marked as |
| 7818 | // safe. |
Ryan Sleevi | a9d6aa6 | 2019-07-26 13:32:18 | [diff] [blame] | 7819 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToAllowedUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7820 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7821 | |
| 7822 | GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url"); |
| 7823 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7824 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7825 | |
| 7826 | TestDelegate d; |
| 7827 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7828 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7829 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 7830 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7831 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7832 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7833 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7834 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7835 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7836 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7837 | EXPECT_EQ(unsafe_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7838 | EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received()); |
| 7839 | } |
| 7840 | } |
| 7841 | |
| 7842 | // 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] | 7843 | // some other URL to the allowlist. |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7844 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7845 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7846 | |
| 7847 | GURL unsafe_url("data:text/html,something"); |
| 7848 | GURL different_unsafe_url("data:text/html,something-else"); |
| 7849 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7850 | default_network_delegate_.set_allowed_unsafe_redirect_url( |
| 7851 | different_unsafe_url); |
| 7852 | |
| 7853 | TestDelegate d; |
| 7854 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7855 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7856 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 7857 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7858 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7859 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7860 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7861 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7862 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7863 | |
| 7864 | // The redirect should have been rejected before reporting it to the caller. |
| 7865 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7866 | } |
| 7867 | } |
| 7868 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7869 | // Redirects from an URL with fragment to an unsafe URL with fragment should |
| 7870 | // be allowed, and the reference fragment of the target URL should be preserved. |
| 7871 | TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7872 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7873 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7874 | GURL original_url(http_test_server()->GetURL("/original#fragment1")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7875 | GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 7876 | GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 7877 | |
| 7878 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7879 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7880 | |
| 7881 | TestDelegate d; |
| 7882 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7883 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7884 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7885 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7886 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7887 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7888 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7889 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7890 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7891 | EXPECT_EQ(original_url, r->original_url()); |
| 7892 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7893 | } |
| 7894 | } |
| 7895 | |
| 7896 | // When a delegate has specified a safe redirect URL, but it does not match the |
| 7897 | // redirect target, then do not prevent the reference fragment from being added. |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7898 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7899 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7900 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7901 | GURL original_url(http_test_server()->GetURL("/original#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7902 | GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7903 | GURL redirect_url(http_test_server()->GetURL("/target")); |
| 7904 | GURL expected_redirect_url( |
| 7905 | http_test_server()->GetURL("/target#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7906 | |
| 7907 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 7908 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7909 | |
| 7910 | TestDelegate d; |
| 7911 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7912 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7913 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7914 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7915 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7916 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7917 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7918 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7919 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7920 | EXPECT_EQ(original_url, r->original_url()); |
| 7921 | EXPECT_EQ(expected_redirect_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7922 | } |
| 7923 | } |
| 7924 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7925 | // When a delegate has specified a safe redirect URL, assume that the redirect |
| 7926 | // URL should not be changed. In particular, the reference fragment should not |
| 7927 | // be modified. |
| 7928 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7929 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7930 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7931 | GURL original_url( |
| 7932 | http_test_server()->GetURL("/original#should-not-be-appended")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7933 | GURL redirect_url("data:text/html,expect-no-reference-fragment"); |
| 7934 | |
| 7935 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 7936 | default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url); |
| 7937 | |
| 7938 | TestDelegate d; |
| 7939 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7940 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7941 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7942 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7943 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7944 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7945 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7946 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7947 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7948 | EXPECT_EQ(original_url, r->original_url()); |
| 7949 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7950 | } |
| 7951 | } |
| 7952 | |
| 7953 | // When a URLRequestRedirectJob is created, the redirection must be followed and |
| 7954 | // the reference fragment of the target URL must not be modified. |
| 7955 | TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7956 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7957 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7958 | GURL original_url( |
| 7959 | http_test_server()->GetURL("/original#should-not-be-appended")); |
| 7960 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7961 | |
| 7962 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7963 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7964 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7965 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7966 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7967 | r.get(), &default_network_delegate_, redirect_url, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7968 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 7969 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7970 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7971 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7972 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7973 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7974 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7975 | EXPECT_EQ(original_url, r->original_url()); |
| 7976 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7977 | } |
| 7978 | |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7979 | TEST_F(URLRequestTestHTTP, UnsupportedReferrerScheme) { |
| 7980 | ASSERT_TRUE(http_test_server()->Start()); |
| 7981 | |
| 7982 | const std::string referrer("foobar://totally.legit.referrer"); |
| 7983 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7984 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7985 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7986 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7987 | req->SetReferrer(referrer); |
| 7988 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7989 | d.RunUntilComplete(); |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7990 | |
| 7991 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 7992 | } |
| 7993 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7994 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7995 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7996 | |
| 7997 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7998 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7999 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8000 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8001 | req->SetReferrer("http://user:[email protected]/"); |
| 8002 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8003 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8004 | |
| 8005 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 8006 | } |
| 8007 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8008 | TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8009 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8010 | |
| 8011 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8012 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8013 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8014 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8015 | req->SetReferrer("http://foo.com/test#fragment"); |
| 8016 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8017 | d.RunUntilComplete(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8018 | |
| 8019 | EXPECT_EQ(std::string("http://foo.com/test"), d.data_received()); |
| 8020 | } |
| 8021 | |
| 8022 | TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8023 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8024 | |
| 8025 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8026 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8027 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8028 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8029 | req->SetReferrer("http://foo.com/test#fragment"); |
| 8030 | req->SetReferrer(""); |
| 8031 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8032 | d.RunUntilComplete(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8033 | |
| 8034 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 8035 | } |
| 8036 | |
Mike West | fbd4411 | 2019-05-08 18:23:37 | [diff] [blame] | 8037 | TEST_F(URLRequestTestHTTP, CapRefererDisabled) { |
| 8038 | ASSERT_TRUE(http_test_server()->Start()); |
| 8039 | |
| 8040 | // Create a string, and pad it out to ~10k with a very exciting path. |
| 8041 | std::string long_referer_header = "http://foo.com/"; |
| 8042 | long_referer_header.resize(10000, 'a'); |
| 8043 | |
| 8044 | // If the feature isn't enabled, a long `referer` will remain long. |
| 8045 | TestDelegate d; |
Mike West | 9e3affdd | 2019-06-07 07:43:57 | [diff] [blame] | 8046 | base::test::ScopedFeatureList feature_list; |
| 8047 | feature_list.InitAndDisableFeature(features::kCapRefererHeaderLength); |
Mike West | fbd4411 | 2019-05-08 18:23:37 | [diff] [blame] | 8048 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8049 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8050 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8051 | req->SetReferrer(long_referer_header); |
| 8052 | req->Start(); |
| 8053 | d.RunUntilComplete(); |
| 8054 | |
| 8055 | EXPECT_EQ(long_referer_header, d.data_received()); |
| 8056 | } |
| 8057 | |
| 8058 | TEST_F(URLRequestTestHTTP, CapRefererHeaderLengthEnabled) { |
| 8059 | ASSERT_TRUE(http_test_server()->Start()); |
| 8060 | |
| 8061 | // Create a string, and pad it out to ~10k with a very exciting path. |
| 8062 | std::string long_referer_header = "http://foo.com/"; |
| 8063 | long_referer_header.resize(10000, 'a'); |
| 8064 | |
| 8065 | // If the feature is enabled without params, a `referer` longer than 4096 |
| 8066 | // bytes will be shortened. |
| 8067 | { |
| 8068 | TestDelegate d; |
| 8069 | base::test::ScopedFeatureList feature_list; |
| 8070 | feature_list.InitAndEnableFeature(features::kCapRefererHeaderLength); |
| 8071 | |
| 8072 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8073 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8074 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8075 | req->SetReferrer(long_referer_header); |
| 8076 | req->Start(); |
| 8077 | d.RunUntilComplete(); |
| 8078 | |
| 8079 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 8080 | } |
| 8081 | |
| 8082 | // If the feature is enabled with params, they will govern the shortening |
| 8083 | // behavior as expected. The following three tests verify behavior for a |
| 8084 | // param larger than the referrer length, exactly the same as the string |
| 8085 | // length, and shorter than the string length. |
| 8086 | { |
| 8087 | TestDelegate d; |
| 8088 | std::map<std::string, std::string> params; |
| 8089 | params["MaxRefererHeaderLength"] = |
| 8090 | base::NumberToString(long_referer_header.length() + 1); |
| 8091 | |
| 8092 | base::test::ScopedFeatureList feature_list; |
| 8093 | feature_list.InitAndEnableFeatureWithParameters( |
| 8094 | features::kCapRefererHeaderLength, params); |
| 8095 | |
| 8096 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8097 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8098 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8099 | req->SetReferrer(long_referer_header); |
| 8100 | req->Start(); |
| 8101 | d.RunUntilComplete(); |
| 8102 | |
| 8103 | EXPECT_EQ(long_referer_header, d.data_received()); |
| 8104 | } |
| 8105 | |
| 8106 | { |
| 8107 | TestDelegate d; |
| 8108 | std::map<std::string, std::string> params; |
| 8109 | params["MaxRefererHeaderLength"] = |
| 8110 | base::NumberToString(long_referer_header.length()); |
| 8111 | |
| 8112 | base::test::ScopedFeatureList feature_list; |
| 8113 | feature_list.InitAndEnableFeatureWithParameters( |
| 8114 | features::kCapRefererHeaderLength, params); |
| 8115 | |
| 8116 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8117 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8118 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8119 | req->SetReferrer(long_referer_header); |
| 8120 | req->Start(); |
| 8121 | d.RunUntilComplete(); |
| 8122 | |
| 8123 | EXPECT_EQ(long_referer_header, d.data_received()); |
| 8124 | } |
| 8125 | |
| 8126 | { |
| 8127 | TestDelegate d; |
| 8128 | std::map<std::string, std::string> params; |
| 8129 | params["MaxRefererHeaderLength"] = |
| 8130 | base::NumberToString(long_referer_header.length() - 1); |
| 8131 | |
| 8132 | base::test::ScopedFeatureList feature_list; |
| 8133 | feature_list.InitAndEnableFeatureWithParameters( |
| 8134 | features::kCapRefererHeaderLength, params); |
| 8135 | |
| 8136 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8137 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8138 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8139 | req->SetReferrer(long_referer_header); |
| 8140 | req->Start(); |
| 8141 | d.RunUntilComplete(); |
| 8142 | |
| 8143 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 8144 | } |
| 8145 | } |
| 8146 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8147 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8148 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8149 | |
| 8150 | TestDelegate d; |
| 8151 | { |
| 8152 | d.set_cancel_in_received_redirect(true); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8153 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8154 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 8155 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8156 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8157 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8158 | |
| 8159 | EXPECT_EQ(1, d.response_started_count()); |
| 8160 | EXPECT_EQ(0, d.bytes_received()); |
| 8161 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8162 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8163 | } |
| 8164 | } |
| 8165 | |
| 8166 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8167 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8168 | |
| 8169 | TestDelegate d; |
| 8170 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8171 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8172 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8173 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8174 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8175 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 8176 | d.RunUntilRedirect(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8177 | |
| 8178 | EXPECT_EQ(1, d.received_redirect_count()); |
Shivani Sharma | c18f976 | 2017-10-23 16:43:23 | [diff] [blame] | 8179 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8180 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 8181 | d.ClearFullRequestHeaders(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8182 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8183 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 8184 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8185 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8186 | |
| 8187 | EXPECT_EQ(1, d.response_started_count()); |
| 8188 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8189 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8190 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 8191 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 8192 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 8193 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8194 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 8195 | |
| 8196 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 8197 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8198 | EXPECT_EQ(contents, d.data_received()); |
| 8199 | } |
| 8200 | } |
| 8201 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8202 | TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8203 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8204 | |
| 8205 | TestDelegate d; |
| 8206 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8207 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8208 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8209 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8210 | |
| 8211 | EXPECT_FALSE(d.have_full_request_headers()); |
| 8212 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8213 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 8214 | d.RunUntilRedirect(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8215 | |
| 8216 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8217 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8218 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 8219 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8220 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8221 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8222 | GURL target_url(http_test_server()->GetURL("/with-headers.html")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8223 | EXPECT_EQ(1, d.response_started_count()); |
| 8224 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8225 | CheckFullRequestHeaders(d.full_request_headers(), target_url); |
| 8226 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8227 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8228 | |
| 8229 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 8230 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 8231 | path = path.Append(kTestFilePath); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8232 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 8233 | |
| 8234 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 8235 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8236 | EXPECT_EQ(contents, d.data_received()); |
| 8237 | } |
| 8238 | } |
| 8239 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8240 | TEST_F(URLRequestTestHTTP, DeferredRedirect_ModifiedHeaders) { |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8241 | ASSERT_TRUE(http_test_server()->Start()); |
| 8242 | |
| 8243 | TestDelegate d; |
| 8244 | { |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8245 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8246 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8247 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8248 | |
| 8249 | // Set initial headers for the request. |
| 8250 | req->SetExtraRequestHeaderByName("Header1", "Value1", true /* overwrite */); |
| 8251 | req->SetExtraRequestHeaderByName("Header2", "Value2", true /* overwrite */); |
| 8252 | |
| 8253 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 8254 | d.RunUntilRedirect(); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8255 | |
| 8256 | // Initial request should only have initial headers. |
| 8257 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8258 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8259 | const HttpRequestHeaders& sent_headers1 = d.full_request_headers(); |
| 8260 | std::string sent_value; |
| 8261 | EXPECT_TRUE(sent_headers1.GetHeader("Header1", &sent_value)); |
| 8262 | EXPECT_EQ("Value1", sent_value); |
| 8263 | EXPECT_TRUE(sent_headers1.GetHeader("Header2", &sent_value)); |
| 8264 | EXPECT_EQ("Value2", sent_value); |
| 8265 | EXPECT_FALSE(sent_headers1.GetHeader("Header3", &sent_value)); |
| 8266 | d.ClearFullRequestHeaders(); |
| 8267 | |
| 8268 | // Overwrite Header2 and add Header3. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8269 | net::HttpRequestHeaders modified_headers; |
| 8270 | modified_headers.SetHeader("Header2", ""); |
| 8271 | modified_headers.SetHeader("Header3", "Value3"); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8272 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8273 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 8274 | modified_headers); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8275 | d.RunUntilComplete(); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8276 | |
| 8277 | EXPECT_EQ(1, d.response_started_count()); |
| 8278 | EXPECT_FALSE(d.received_data_before_response()); |
| 8279 | EXPECT_EQ(OK, d.request_status()); |
| 8280 | |
| 8281 | // Redirected request should also have modified headers. |
| 8282 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8283 | const HttpRequestHeaders& sent_headers2 = d.full_request_headers(); |
| 8284 | EXPECT_TRUE(sent_headers2.GetHeader("Header1", &sent_value)); |
| 8285 | EXPECT_EQ("Value1", sent_value); |
| 8286 | EXPECT_TRUE(sent_headers2.GetHeader("Header2", &sent_value)); |
| 8287 | EXPECT_EQ("", sent_value); |
| 8288 | EXPECT_TRUE(sent_headers2.GetHeader("Header3", &sent_value)); |
| 8289 | EXPECT_EQ("Value3", sent_value); |
| 8290 | } |
| 8291 | } |
| 8292 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8293 | TEST_F(URLRequestTestHTTP, DeferredRedirect_RemovedHeaders) { |
| 8294 | ASSERT_TRUE(http_test_server()->Start()); |
| 8295 | |
| 8296 | TestDelegate d; |
| 8297 | { |
| 8298 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
| 8299 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8300 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8301 | |
| 8302 | // Set initial headers for the request. |
| 8303 | req->SetExtraRequestHeaderByName("Header1", "Value1", true /* overwrite */); |
| 8304 | req->SetExtraRequestHeaderByName("Header2", "Value2", true /* overwrite */); |
| 8305 | |
| 8306 | req->Start(); |
| 8307 | d.RunUntilRedirect(); |
| 8308 | |
| 8309 | // Initial request should have initial headers. |
| 8310 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8311 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8312 | const HttpRequestHeaders& sent_headers1 = d.full_request_headers(); |
| 8313 | std::string sent_value; |
| 8314 | EXPECT_TRUE(sent_headers1.GetHeader("Header1", &sent_value)); |
| 8315 | EXPECT_EQ("Value1", sent_value); |
| 8316 | EXPECT_TRUE(sent_headers1.GetHeader("Header2", &sent_value)); |
| 8317 | EXPECT_EQ("Value2", sent_value); |
| 8318 | d.ClearFullRequestHeaders(); |
| 8319 | |
| 8320 | // Keep Header1 and remove Header2. |
| 8321 | std::vector<std::string> removed_headers({"Header2"}); |
| 8322 | req->FollowDeferredRedirect(removed_headers, |
| 8323 | base::nullopt /* modified_headers */); |
| 8324 | d.RunUntilComplete(); |
| 8325 | |
| 8326 | EXPECT_EQ(1, d.response_started_count()); |
| 8327 | EXPECT_FALSE(d.received_data_before_response()); |
| 8328 | EXPECT_EQ(OK, d.request_status()); |
| 8329 | |
| 8330 | // Redirected request should also have |
| 8331 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8332 | const HttpRequestHeaders& sent_headers2 = d.full_request_headers(); |
| 8333 | EXPECT_TRUE(sent_headers2.GetHeader("Header1", &sent_value)); |
| 8334 | EXPECT_EQ("Value1", sent_value); |
| 8335 | EXPECT_FALSE(sent_headers2.GetHeader("Header2", &sent_value)); |
| 8336 | } |
| 8337 | } |
| 8338 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8339 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8340 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8341 | |
| 8342 | TestDelegate d; |
| 8343 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8344 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8345 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 8346 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8347 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 8348 | d.RunUntilRedirect(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8349 | |
| 8350 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8351 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8352 | req->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8353 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8354 | |
| 8355 | EXPECT_EQ(1, d.response_started_count()); |
| 8356 | EXPECT_EQ(0, d.bytes_received()); |
| 8357 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8358 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8359 | } |
| 8360 | } |
| 8361 | |
| 8362 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8363 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8364 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8365 | // Populate the cache. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8366 | { |
| 8367 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8368 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8369 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8370 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8371 | HttpRequestHeaders headers; |
| 8372 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8373 | req->SetExtraRequestHeaders(headers); |
| 8374 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8375 | d.RunUntilComplete(); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8376 | |
| 8377 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8378 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8379 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8380 | } |
| 8381 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8382 | // Expect a cache hit. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8383 | { |
| 8384 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8385 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8386 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8387 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8388 | HttpRequestHeaders headers; |
| 8389 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8390 | req->SetExtraRequestHeaders(headers); |
| 8391 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8392 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8393 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8394 | EXPECT_TRUE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8395 | |
| 8396 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8397 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8398 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8399 | } |
| 8400 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8401 | // Expect a cache miss. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8402 | { |
| 8403 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8404 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8405 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8406 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8407 | HttpRequestHeaders headers; |
| 8408 | headers.SetHeader("foo", "2"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8409 | req->SetExtraRequestHeaders(headers); |
| 8410 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8411 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8412 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8413 | EXPECT_FALSE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8414 | |
| 8415 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8416 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8417 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8418 | } |
| 8419 | } |
| 8420 | |
| 8421 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8422 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8423 | |
| 8424 | // populate the cache |
| 8425 | { |
| 8426 | TestDelegate d; |
| 8427 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8428 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8429 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8430 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8431 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8432 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8433 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8434 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8435 | |
| 8436 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8437 | } |
| 8438 | |
| 8439 | // repeat request with end-to-end validation. since auth-basic results in a |
| 8440 | // cachable page, we expect this test to result in a 304. in which case, the |
| 8441 | // response should be fetched from the cache. |
| 8442 | { |
| 8443 | TestDelegate d; |
| 8444 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8445 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8446 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8447 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8448 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8449 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 8450 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8451 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8452 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8453 | |
| 8454 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8455 | |
| 8456 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8457 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8458 | } |
| 8459 | } |
| 8460 | |
| 8461 | // Check that Set-Cookie headers in 401 responses are respected. |
| 8462 | // http://crbug.com/6450 |
| 8463 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8464 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8465 | |
| 8466 | GURL url_requiring_auth = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8467 | http_test_server()->GetURL("/auth-basic?set-cookie-if-challenged"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8468 | |
| 8469 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 8470 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 8471 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8472 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8473 | TestURLRequestContext context(true); |
| 8474 | context.set_network_delegate(&network_delegate); |
| 8475 | context.Init(); |
| 8476 | |
| 8477 | TestDelegate d; |
| 8478 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8479 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8480 | std::unique_ptr<URLRequest> r( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8481 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &d, |
| 8482 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8483 | r->set_site_for_cookies(url_requiring_auth); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8484 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8485 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8486 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8487 | |
| 8488 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8489 | |
| 8490 | // Make sure we sent the cookie in the restarted transaction. |
| 8491 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 8492 | != std::string::npos); |
| 8493 | } |
| 8494 | |
| 8495 | // Same test as above, except this time the restart is initiated earlier |
| 8496 | // (without user intervention since identity is embedded in the URL). |
| 8497 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8498 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8499 | TestURLRequestContext context(true); |
| 8500 | context.set_network_delegate(&network_delegate); |
| 8501 | context.Init(); |
| 8502 | |
| 8503 | TestDelegate d; |
| 8504 | |
| 8505 | GURL::Replacements replacements; |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 8506 | replacements.SetUsernameStr("user2"); |
| 8507 | replacements.SetPasswordStr("secret"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8508 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 8509 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8510 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 8511 | url_with_identity, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8512 | r->set_site_for_cookies(url_with_identity); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8513 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8514 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8515 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8516 | |
| 8517 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 8518 | |
| 8519 | // Make sure we sent the cookie in the restarted transaction. |
| 8520 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 8521 | != std::string::npos); |
| 8522 | } |
| 8523 | } |
| 8524 | |
Matt Menke | 0743e34 | 2019-06-11 20:48:10 | [diff] [blame] | 8525 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookiesCancelAuth) { |
| 8526 | ASSERT_TRUE(http_test_server()->Start()); |
| 8527 | |
| 8528 | GURL url_requiring_auth = |
| 8529 | http_test_server()->GetURL("/auth-basic?set-cookie-if-challenged"); |
| 8530 | |
| 8531 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 8532 | // Verify that cookies are set before credentials are provided, and then |
| 8533 | // cancelling auth does not result in setting the cookies again. |
| 8534 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 8535 | TestURLRequestContext context(true); |
| 8536 | context.set_network_delegate(&network_delegate); |
| 8537 | context.Init(); |
| 8538 | |
| 8539 | TestDelegate d; |
| 8540 | |
| 8541 | EXPECT_TRUE(GetAllCookies(&context).empty()); |
| 8542 | |
| 8543 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 8544 | url_requiring_auth, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8545 | r->set_site_for_cookies(url_requiring_auth); |
| 8546 | r->Start(); |
| 8547 | d.RunUntilAuthRequired(); |
| 8548 | |
| 8549 | // Cookie should have been set. |
| 8550 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 8551 | CookieList cookies = GetAllCookies(&context); |
| 8552 | ASSERT_EQ(1u, cookies.size()); |
| 8553 | EXPECT_EQ("got_challenged", cookies[0].Name()); |
| 8554 | EXPECT_EQ("true", cookies[0].Value()); |
| 8555 | |
| 8556 | // Delete cookie. |
| 8557 | context.cookie_store()->DeleteAllAsync(CookieStore::DeleteCallback()); |
| 8558 | |
| 8559 | // Cancel auth and continue the request. |
| 8560 | r->CancelAuth(); |
| 8561 | d.RunUntilComplete(); |
| 8562 | ASSERT_TRUE(r->response_headers()); |
| 8563 | EXPECT_EQ(401, r->response_headers()->response_code()); |
| 8564 | |
| 8565 | // Cookie should not have been set again. |
| 8566 | EXPECT_TRUE(GetAllCookies(&context).empty()); |
| 8567 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 8568 | } |
| 8569 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8570 | TEST_F(URLRequestTest, ReportCookieActivity) { |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8571 | HttpTestServer test_server; |
| 8572 | ASSERT_TRUE(test_server.Start()); |
| 8573 | |
| 8574 | FilteringTestLayeredNetworkDelegate network_delegate( |
| 8575 | std::make_unique<TestNetworkDelegate>()); |
| 8576 | network_delegate.SetCookieFilter("not_stored_cookie"); |
| 8577 | network_delegate.set_block_get_cookies(); |
| 8578 | TestURLRequestContext context(true); |
| 8579 | context.set_network_delegate(&network_delegate); |
| 8580 | context.Init(); |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8581 | // Make sure cookies blocked from being stored are caught, and those that are |
| 8582 | // accepted are reported as well. |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8583 | { |
| 8584 | TestDelegate d; |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8585 | GURL test_url = test_server.GetURL( |
| 8586 | "/set-cookie?not_stored_cookie=true&" |
| 8587 | "stored_cookie=tasty" |
| 8588 | "&path_cookie=narrow;path=/set-cookie"); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8589 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8590 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8591 | req->set_site_for_cookies(test_url); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8592 | req->Start(); |
| 8593 | d.RunUntilComplete(); |
| 8594 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8595 | ASSERT_EQ(3u, req->maybe_stored_cookies().size()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8596 | EXPECT_EQ("not_stored_cookie", |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8597 | req->maybe_stored_cookies()[0].cookie->Name()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8598 | EXPECT_EQ( |
| 8599 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES, |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8600 | req->maybe_stored_cookies()[0].status); |
| 8601 | EXPECT_EQ("stored_cookie", req->maybe_stored_cookies()[1].cookie->Name()); |
| 8602 | EXPECT_EQ(net::CanonicalCookie::CookieInclusionStatus::INCLUDE, |
| 8603 | req->maybe_stored_cookies()[1].status); |
| 8604 | EXPECT_EQ("stored_cookie", req->maybe_stored_cookies()[1].cookie->Name()); |
| 8605 | EXPECT_EQ(net::CanonicalCookie::CookieInclusionStatus::INCLUDE, |
| 8606 | req->maybe_stored_cookies()[2].status); |
| 8607 | EXPECT_EQ("path_cookie", req->maybe_stored_cookies()[2].cookie->Name()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8608 | } |
| 8609 | { |
| 8610 | TestDelegate d; |
| 8611 | // Make sure cookies blocked from being sent are caught. |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8612 | GURL test_url = test_server.GetURL("/echoheader?Cookie"); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8613 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8614 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8615 | req->set_site_for_cookies(test_url); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8616 | req->Start(); |
| 8617 | d.RunUntilComplete(); |
| 8618 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8619 | EXPECT_TRUE(d.data_received().find("stored_cookie=tasty") == |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8620 | std::string::npos); |
| 8621 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8622 | ASSERT_EQ(2u, req->maybe_sent_cookies().size()); |
| 8623 | EXPECT_EQ("path_cookie", req->maybe_sent_cookies()[0].cookie.Name()); |
| 8624 | EXPECT_EQ(net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_NOT_ON_PATH, |
| 8625 | req->maybe_sent_cookies()[0].status); |
| 8626 | EXPECT_EQ("stored_cookie", req->maybe_sent_cookies()[1].cookie.Name()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8627 | EXPECT_EQ( |
| 8628 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES, |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8629 | req->maybe_sent_cookies()[1].status); |
| 8630 | } |
| 8631 | |
| 8632 | network_delegate.unset_block_get_cookies(); |
| 8633 | { |
| 8634 | // Now with sending cookies re-enabled, it should actually be sent. |
| 8635 | TestDelegate d; |
| 8636 | GURL test_url = test_server.GetURL("/echoheader?Cookie"); |
| 8637 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 8638 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8639 | req->set_site_for_cookies(test_url); |
| 8640 | req->Start(); |
| 8641 | d.RunUntilComplete(); |
| 8642 | |
| 8643 | EXPECT_TRUE(d.data_received().find("stored_cookie=tasty") != |
| 8644 | std::string::npos); |
| 8645 | |
| 8646 | ASSERT_EQ(2u, req->maybe_sent_cookies().size()); |
| 8647 | EXPECT_EQ("path_cookie", req->maybe_sent_cookies()[0].cookie.Name()); |
| 8648 | EXPECT_EQ(net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_NOT_ON_PATH, |
| 8649 | req->maybe_sent_cookies()[0].status); |
| 8650 | EXPECT_EQ("stored_cookie", req->maybe_sent_cookies()[1].cookie.Name()); |
| 8651 | EXPECT_EQ(net::CanonicalCookie::CookieInclusionStatus::INCLUDE, |
| 8652 | req->maybe_sent_cookies()[1].status); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8653 | } |
| 8654 | } |
| 8655 | |
Maks Orlovich | 0d9da12 | 2019-06-13 20:14:43 | [diff] [blame] | 8656 | TEST_F(URLRequestTestHTTP, AuthChallengeCancelCookieCollect) { |
| 8657 | ASSERT_TRUE(http_test_server()->Start()); |
| 8658 | GURL url_requiring_auth = |
| 8659 | http_test_server()->GetURL("/auth-basic?set-cookie-if-challenged"); |
| 8660 | |
| 8661 | FilteringTestLayeredNetworkDelegate filtering_network_delegate( |
| 8662 | std::make_unique<TestNetworkDelegate>()); |
| 8663 | filtering_network_delegate.SetCookieFilter("got_challenged"); |
| 8664 | TestURLRequestContext context(true); |
| 8665 | context.set_network_delegate(&filtering_network_delegate); |
| 8666 | context.Init(); |
| 8667 | |
| 8668 | TestDelegate delegate; |
| 8669 | |
| 8670 | std::unique_ptr<URLRequest> request( |
| 8671 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &delegate, |
| 8672 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8673 | request->set_site_for_cookies(url_requiring_auth); |
| 8674 | request->Start(); |
| 8675 | |
| 8676 | delegate.RunUntilAuthRequired(); |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8677 | ASSERT_EQ(1u, request->maybe_stored_cookies().size()); |
Maks Orlovich | 0d9da12 | 2019-06-13 20:14:43 | [diff] [blame] | 8678 | EXPECT_EQ( |
| 8679 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES, |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8680 | request->maybe_stored_cookies()[0].status); |
Maks Orlovich | 0d9da12 | 2019-06-13 20:14:43 | [diff] [blame] | 8681 | EXPECT_EQ("got_challenged=true", |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8682 | request->maybe_stored_cookies()[0].cookie_string); |
Maks Orlovich | 0d9da12 | 2019-06-13 20:14:43 | [diff] [blame] | 8683 | |
| 8684 | // This shouldn't DCHECK-fail. |
| 8685 | request->CancelAuth(); |
| 8686 | delegate.RunUntilComplete(); |
| 8687 | } |
| 8688 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8689 | TEST_F(URLRequestTestHTTP, AuthChallengeWithFilteredCookies) { |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8690 | ASSERT_TRUE(http_test_server()->Start()); |
| 8691 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8692 | GURL url_requiring_auth = |
| 8693 | http_test_server()->GetURL("/auth-basic?set-cookie-if-challenged"); |
| 8694 | GURL url_requiring_auth_wo_cookies = |
| 8695 | http_test_server()->GetURL("/auth-basic"); |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8696 | // Check maybe_stored_cookies is populated first round trip, and cleared on |
| 8697 | // the second. |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8698 | { |
| 8699 | FilteringTestLayeredNetworkDelegate filtering_network_delegate( |
| 8700 | std::make_unique<TestNetworkDelegate>()); |
| 8701 | filtering_network_delegate.SetCookieFilter("got_challenged"); |
| 8702 | TestURLRequestContext context(true); |
| 8703 | context.set_network_delegate(&filtering_network_delegate); |
| 8704 | context.Init(); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8705 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8706 | TestDelegate delegate; |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8707 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8708 | std::unique_ptr<URLRequest> request( |
| 8709 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &delegate, |
| 8710 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8711 | request->set_site_for_cookies(url_requiring_auth); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8712 | request->Start(); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8713 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8714 | delegate.RunUntilAuthRequired(); |
| 8715 | // Make sure it was blocked once. |
| 8716 | EXPECT_EQ(1, filtering_network_delegate.blocked_set_cookie_count()); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8717 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8718 | // The number of cookies blocked from the most recent round trip. |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8719 | ASSERT_EQ(1u, request->maybe_stored_cookies().size()); |
| 8720 | EXPECT_EQ( |
| 8721 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES, |
| 8722 | request->maybe_stored_cookies().front().status); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8723 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8724 | // Now check the second round trip |
| 8725 | request->SetAuth(AuthCredentials(kUser, kSecret)); |
| 8726 | delegate.RunUntilComplete(); |
| 8727 | EXPECT_THAT(delegate.request_status(), IsOk()); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8728 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8729 | // There are DCHECKs in URLRequestHttpJob that would fail if |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8730 | // maybe_sent_cookies and maybe_stored_cookies were not cleared properly. |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8731 | |
| 8732 | // Make sure the cookie was actually filtered and not sent. |
| 8733 | EXPECT_EQ(std::string::npos, |
| 8734 | delegate.data_received().find("Cookie: got_challenged=true")); |
| 8735 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8736 | // The number of cookies that most recent round trip tried to set. |
| 8737 | ASSERT_EQ(0u, request->maybe_stored_cookies().size()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8738 | } |
| 8739 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8740 | // Check maybe_sent_cookies on first round trip (and cleared for the second). |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8741 | { |
| 8742 | FilteringTestLayeredNetworkDelegate filtering_network_delegate( |
| 8743 | std::make_unique<TestNetworkDelegate>()); |
| 8744 | filtering_network_delegate.set_block_get_cookies(); |
| 8745 | TestURLRequestContext context(true); |
| 8746 | context.set_network_delegate(&filtering_network_delegate); |
| 8747 | |
| 8748 | std::unique_ptr<CookieMonster> cm = |
| 8749 | std::make_unique<CookieMonster>(nullptr, nullptr); |
| 8750 | cm->SetCookieWithOptionsAsync(url_requiring_auth_wo_cookies, |
| 8751 | "another_cookie=true", CookieOptions(), |
| 8752 | CookieStore::SetCookiesCallback()); |
| 8753 | context.set_cookie_store(cm.get()); |
| 8754 | context.Init(); |
| 8755 | |
| 8756 | TestDelegate delegate; |
| 8757 | |
| 8758 | std::unique_ptr<URLRequest> request( |
| 8759 | context.CreateRequest(url_requiring_auth_wo_cookies, DEFAULT_PRIORITY, |
| 8760 | &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8761 | request->set_site_for_cookies(url_requiring_auth_wo_cookies); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8762 | request->Start(); |
| 8763 | |
| 8764 | delegate.RunUntilAuthRequired(); |
| 8765 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8766 | ASSERT_EQ(1u, request->maybe_sent_cookies().size()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8767 | EXPECT_EQ("another_cookie", |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8768 | request->maybe_sent_cookies().front().cookie.Name()); |
| 8769 | EXPECT_EQ("true", request->maybe_sent_cookies().front().cookie.Value()); |
| 8770 | EXPECT_EQ( |
| 8771 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES, |
| 8772 | request->maybe_sent_cookies().front().status); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8773 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8774 | // Check maybe_sent_cookies on second roundtrip. |
| 8775 | request->set_maybe_sent_cookies({}); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8776 | cm->DeleteAllAsync(CookieStore::DeleteCallback()); |
| 8777 | cm->SetCookieWithOptionsAsync(url_requiring_auth_wo_cookies, |
| 8778 | "one_more_cookie=true", CookieOptions(), |
| 8779 | CookieStore::SetCookiesCallback()); |
| 8780 | |
| 8781 | request->SetAuth(AuthCredentials(kUser, kSecret)); |
| 8782 | delegate.RunUntilComplete(); |
| 8783 | EXPECT_THAT(delegate.request_status(), IsOk()); |
| 8784 | |
| 8785 | // There are DCHECKs in URLRequestHttpJob that would fail if |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8786 | // maybe_sent_cookies and maybe_stored_cookies were not cleared properly. |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8787 | |
| 8788 | // Make sure the cookie was actually filtered. |
| 8789 | EXPECT_EQ(std::string::npos, |
| 8790 | delegate.data_received().find("Cookie: one_more_cookie=true")); |
| 8791 | // got_challenged was set after the first request and blocked on the second, |
| 8792 | // so it should only have been blocked this time |
| 8793 | EXPECT_EQ(2, filtering_network_delegate.blocked_get_cookie_count()); |
| 8794 | |
| 8795 | // // The number of cookies blocked from the most recent round trip. |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8796 | ASSERT_EQ(1u, request->maybe_sent_cookies().size()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8797 | EXPECT_EQ("one_more_cookie", |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 8798 | request->maybe_sent_cookies().front().cookie.Name()); |
| 8799 | EXPECT_EQ( |
| 8800 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES, |
| 8801 | request->maybe_sent_cookies().front().status); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8802 | } |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8803 | } |
| 8804 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8805 | // Tests that load timing works as expected with auth and the cache. |
| 8806 | TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8807 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8808 | |
| 8809 | // populate the cache |
| 8810 | { |
| 8811 | TestDelegate d; |
| 8812 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8813 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8814 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8815 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8816 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8817 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8818 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8819 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8820 | |
| 8821 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8822 | |
| 8823 | LoadTimingInfo load_timing_info_before_auth; |
| 8824 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth( |
| 8825 | &load_timing_info_before_auth)); |
| 8826 | TestLoadTimingNotReused(load_timing_info_before_auth, |
| 8827 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 8828 | |
| 8829 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8830 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8831 | // The test server does not support keep alive sockets, so the second |
| 8832 | // request with auth should use a new socket. |
| 8833 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 8834 | EXPECT_NE(load_timing_info_before_auth.socket_log_id, |
| 8835 | load_timing_info.socket_log_id); |
| 8836 | EXPECT_LE(load_timing_info_before_auth.receive_headers_end, |
| 8837 | load_timing_info.connect_timing.connect_start); |
| 8838 | } |
| 8839 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8840 | // Repeat request with end-to-end validation. Since auth-basic results in a |
| 8841 | // 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] | 8842 | // response should be fetched from the cache. |
| 8843 | { |
| 8844 | TestDelegate d; |
| 8845 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8846 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8847 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8848 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8849 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8850 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 8851 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8852 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8853 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8854 | |
| 8855 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8856 | |
| 8857 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8858 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8859 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8860 | // Since there was a request that went over the wire, the load timing |
| 8861 | // information should include connection times. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8862 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8863 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8864 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8865 | } |
| 8866 | } |
| 8867 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8868 | // In this test, we do a POST which the server will 302 redirect. |
| 8869 | // The subsequent transaction should use GET, and should not send the |
| 8870 | // Content-Type header. |
| 8871 | // http://code.google.com/p/chromium/issues/detail?id=843 |
| 8872 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8873 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8874 | |
| 8875 | const char kData[] = "hello world"; |
| 8876 | |
| 8877 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8878 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8879 | http_test_server()->GetURL("/redirect-to-echoall"), DEFAULT_PRIORITY, &d, |
| 8880 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8881 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8882 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8883 | |
| 8884 | // Set headers (some of which are specific to the POST). |
| 8885 | HttpRequestHeaders headers; |
Yeol | 45b9e0a | 2018-07-27 02:03:29 | [diff] [blame] | 8886 | headers.SetHeader("Content-Type", |
| 8887 | "multipart/form-data;" |
| 8888 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ"); |
| 8889 | headers.SetHeader("Accept", |
| 8890 | "text/xml,application/xml,application/xhtml+xml," |
| 8891 | "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); |
| 8892 | headers.SetHeader("Accept-Language", "en-US,en"); |
| 8893 | headers.SetHeader("Accept-Charset", "ISO-8859-1,*,utf-8"); |
| 8894 | headers.SetHeader("Content-Length", "11"); |
| 8895 | headers.SetHeader("Origin", "http://localhost:1337/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8896 | req->SetExtraRequestHeaders(headers); |
| 8897 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8898 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8899 | |
| 8900 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8901 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8902 | EXPECT_EQ("text/html", mime_type); |
| 8903 | |
| 8904 | const std::string& data = d.data_received(); |
| 8905 | |
| 8906 | // Check that the post-specific headers were stripped: |
| 8907 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 8908 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8909 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8910 | |
| 8911 | // These extra request headers should not have been stripped. |
| 8912 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 8913 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 8914 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 8915 | } |
| 8916 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8917 | // The following tests check that we handle mutating the request for HTTP |
| 8918 | // redirects as expected. |
| 8919 | // See https://crbug.com/56373, https://crbug.com/102130, and |
| 8920 | // https://crbug.com/465517. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8921 | |
| 8922 | TEST_F(URLRequestTestHTTP, Redirect301Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8923 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8924 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8925 | const GURL url = http_test_server()->GetURL("/redirect301-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8926 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8927 | http_test_server()->GetURL("/redirect301-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8928 | |
| 8929 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8930 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8931 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8932 | |
| 8933 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8934 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8935 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8936 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8937 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8938 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8939 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8940 | } |
| 8941 | |
| 8942 | TEST_F(URLRequestTestHTTP, Redirect302Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8943 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8944 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8945 | const GURL url = http_test_server()->GetURL("/redirect302-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8946 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8947 | http_test_server()->GetURL("/redirect302-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8948 | |
| 8949 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8950 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8951 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8952 | |
| 8953 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8954 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8955 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8956 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8957 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8958 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8959 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8960 | } |
| 8961 | |
| 8962 | TEST_F(URLRequestTestHTTP, Redirect303Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8963 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8964 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8965 | const GURL url = http_test_server()->GetURL("/redirect303-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8966 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8967 | http_test_server()->GetURL("/redirect303-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8968 | |
| 8969 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8970 | HTTPRedirectMethodTest(url, "PUT", "GET", true); |
| 8971 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8972 | |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8973 | HTTPRedirectOriginHeaderTest(url, "CONNECT", "GET", std::string()); |
| 8974 | HTTPRedirectOriginHeaderTest(https_redirect_url, "CONNECT", "GET", |
| 8975 | std::string()); |
| 8976 | HTTPRedirectOriginHeaderTest(url, "DELETE", "GET", std::string()); |
| 8977 | HTTPRedirectOriginHeaderTest(https_redirect_url, "DELETE", "GET", |
| 8978 | std::string()); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8979 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8980 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8981 | HTTPRedirectOriginHeaderTest(url, "HEAD", "HEAD", url.GetOrigin().spec()); |
| 8982 | HTTPRedirectOriginHeaderTest(https_redirect_url, "HEAD", "HEAD", "null"); |
| 8983 | HTTPRedirectOriginHeaderTest(url, "OPTIONS", "GET", std::string()); |
| 8984 | HTTPRedirectOriginHeaderTest(https_redirect_url, "OPTIONS", "GET", |
| 8985 | std::string()); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8986 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8987 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8988 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8989 | HTTPRedirectOriginHeaderTest(url, "PUT", "GET", std::string()); |
| 8990 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "GET", std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8991 | } |
| 8992 | |
| 8993 | TEST_F(URLRequestTestHTTP, Redirect307Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8994 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8995 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8996 | const GURL url = http_test_server()->GetURL("/redirect307-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8997 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8998 | http_test_server()->GetURL("/redirect307-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8999 | |
| 9000 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 9001 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 9002 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 9003 | |
| 9004 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 9005 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 9006 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 9007 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 9008 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 9009 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9010 | } |
| 9011 | |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 9012 | TEST_F(URLRequestTestHTTP, Redirect308Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9013 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 9014 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9015 | const GURL url = http_test_server()->GetURL("/redirect308-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 9016 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9017 | http_test_server()->GetURL("/redirect308-to-https"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 9018 | |
| 9019 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 9020 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 9021 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 9022 | |
| 9023 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 9024 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 9025 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 9026 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 9027 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 9028 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 9029 | } |
| 9030 | |
| 9031 | // Make sure that 308 responses without bodies are not treated as redirects. |
| 9032 | // Certain legacy apis that pre-date the response code expect this behavior |
| 9033 | // (Like Google Drive). |
| 9034 | TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9035 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 9036 | |
| 9037 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9038 | const GURL url = http_test_server()->GetURL("/308-without-location-header"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 9039 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9040 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9041 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 9042 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9043 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9044 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9045 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 9046 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9047 | EXPECT_EQ(308, request->response_headers()->response_code()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 9048 | EXPECT_EQ("This is not a redirect.", d.data_received()); |
| 9049 | } |
| 9050 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 9051 | TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9052 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 9053 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9054 | GURL original_url( |
| 9055 | http_test_server()->GetURL("/redirect302-to-echo#fragment")); |
| 9056 | GURL expected_url(http_test_server()->GetURL("/echo#fragment")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 9057 | |
| 9058 | TestDelegate d; |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 9059 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 9060 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 9061 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 9062 | r->Start(); |
| 9063 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 9064 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 9065 | EXPECT_EQ(2U, r->url_chain().size()); |
| 9066 | EXPECT_EQ(OK, d.request_status()); |
| 9067 | EXPECT_EQ(original_url, r->original_url()); |
| 9068 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 9069 | } |
| 9070 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9071 | TEST_F(URLRequestTestHTTP, RedirectWithFilteredCookies) { |
| 9072 | ASSERT_TRUE(http_test_server()->Start()); |
| 9073 | |
| 9074 | // FilteringTestLayeredNetworkDelegate filters by name, so the names of the |
| 9075 | // two cookies have to be the same. The values have been set to different |
| 9076 | // strings (the value of the server-redirect cookies is "true" and set-cookie |
| 9077 | // is "other") to differentiate between the two round trips. |
| 9078 | GURL redirect_to( |
| 9079 | http_test_server()->GetURL("/set-cookie?server-redirect=other")); |
| 9080 | |
| 9081 | GURL original_url(http_test_server()->GetURL("/server-redirect-with-cookie?" + |
| 9082 | redirect_to.spec())); |
| 9083 | |
| 9084 | GURL original_url_wo_cookie( |
| 9085 | http_test_server()->GetURL("/server-redirect?" + redirect_to.spec())); |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9086 | // Check maybe_stored_cookies on first round trip. |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9087 | { |
| 9088 | FilteringTestLayeredNetworkDelegate filtering_network_delegate( |
| 9089 | std::make_unique<TestNetworkDelegate>()); // Must outlive URLRequest. |
| 9090 | filtering_network_delegate.SetCookieFilter( |
| 9091 | "server-redirect"); // Filter the cookie server-redirect sets. |
| 9092 | TestURLRequestContext context(true); |
| 9093 | context.set_network_delegate(&filtering_network_delegate); |
| 9094 | context.Init(); |
| 9095 | |
| 9096 | TestDelegate delegate; |
| 9097 | std::unique_ptr<URLRequest> request( |
| 9098 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &delegate, |
| 9099 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9100 | |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 9101 | request->set_site_for_cookies(original_url); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9102 | request->Start(); |
| 9103 | delegate.RunUntilRedirect(); |
| 9104 | |
| 9105 | // Make sure it was blocked once. |
| 9106 | EXPECT_EQ(1, filtering_network_delegate.blocked_set_cookie_count()); |
| 9107 | |
| 9108 | // The number of cookies blocked from the most recent round trip. |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9109 | ASSERT_EQ(1u, request->maybe_stored_cookies().size()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9110 | EXPECT_EQ("server-redirect", |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9111 | request->maybe_stored_cookies().front().cookie->Name()); |
| 9112 | EXPECT_EQ("true", request->maybe_stored_cookies().front().cookie->Value()); |
| 9113 | EXPECT_EQ( |
| 9114 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES, |
| 9115 | request->maybe_stored_cookies().front().status); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9116 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9117 | // Check maybe_stored_cookies on second round trip (and clearing from the |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9118 | // first). |
| 9119 | request->FollowDeferredRedirect(base::nullopt, base::nullopt); |
| 9120 | delegate.RunUntilComplete(); |
| 9121 | EXPECT_THAT(delegate.request_status(), IsOk()); |
| 9122 | |
| 9123 | // There are DCHECKs in URLRequestHttpJob that would fail if |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9124 | // maybe_sent_cookies and maybe_stored_cookies we not cleared properly. |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9125 | |
| 9126 | // Make sure it was blocked twice. |
| 9127 | EXPECT_EQ(2, filtering_network_delegate.blocked_set_cookie_count()); |
| 9128 | |
| 9129 | // The number of cookies blocked from the most recent round trip. |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9130 | ASSERT_EQ(1u, request->maybe_stored_cookies().size()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9131 | EXPECT_EQ("server-redirect", |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9132 | request->maybe_stored_cookies().front().cookie->Name()); |
| 9133 | EXPECT_EQ("other", request->maybe_stored_cookies().front().cookie->Value()); |
| 9134 | EXPECT_EQ( |
| 9135 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES, |
| 9136 | request->maybe_stored_cookies().front().status); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9137 | } |
| 9138 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9139 | // Check maybe_sent_cookies on first round trip. |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9140 | { |
| 9141 | FilteringTestLayeredNetworkDelegate filtering_network_delegate( |
| 9142 | std::make_unique<TestNetworkDelegate>()); |
| 9143 | filtering_network_delegate.set_block_get_cookies(); |
| 9144 | TestURLRequestContext context(true); |
| 9145 | context.set_network_delegate(&filtering_network_delegate); |
| 9146 | std::unique_ptr<CookieMonster> cm = |
| 9147 | std::make_unique<CookieMonster>(nullptr, nullptr); |
| 9148 | cm->SetCookieWithOptionsAsync(original_url, "another_cookie=true", |
| 9149 | CookieOptions(), |
| 9150 | CookieStore::SetCookiesCallback()); |
| 9151 | context.set_cookie_store(cm.get()); |
| 9152 | context.Init(); |
| 9153 | |
| 9154 | TestDelegate delegate; |
| 9155 | std::unique_ptr<URLRequest> request( |
| 9156 | context.CreateRequest(original_url_wo_cookie, DEFAULT_PRIORITY, |
| 9157 | &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9158 | |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 9159 | request->set_site_for_cookies(original_url_wo_cookie); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9160 | request->Start(); |
| 9161 | |
| 9162 | delegate.RunUntilRedirect(); |
| 9163 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9164 | ASSERT_EQ(1u, request->maybe_sent_cookies().size()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9165 | EXPECT_EQ("another_cookie", |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9166 | request->maybe_sent_cookies().front().cookie.Name()); |
| 9167 | EXPECT_EQ( |
| 9168 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES, |
| 9169 | request->maybe_sent_cookies().front().status); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9170 | |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9171 | // Check maybe_sent_cookies on second round trip |
| 9172 | request->set_maybe_sent_cookies({}); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9173 | cm->DeleteAllAsync(CookieStore::DeleteCallback()); |
| 9174 | cm->SetCookieWithOptionsAsync(original_url_wo_cookie, |
| 9175 | "one_more_cookie=true", CookieOptions(), |
| 9176 | CookieStore::SetCookiesCallback()); |
| 9177 | |
| 9178 | request->FollowDeferredRedirect(base::nullopt, base::nullopt); |
| 9179 | delegate.RunUntilComplete(); |
| 9180 | EXPECT_THAT(delegate.request_status(), IsOk()); |
| 9181 | |
| 9182 | // There are DCHECKs in URLRequestHttpJob that would fail if |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9183 | // maybe_sent_cookies and maybe_stored_cookies we not cleared properly. |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9184 | |
| 9185 | EXPECT_EQ(2, filtering_network_delegate.blocked_get_cookie_count()); |
| 9186 | |
| 9187 | // The number of cookies blocked from the most recent round trip. |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9188 | ASSERT_EQ(1u, request->maybe_sent_cookies().size()); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9189 | EXPECT_EQ("one_more_cookie", |
Maks Orlovich | 1571423 | 2019-06-16 23:14:09 | [diff] [blame] | 9190 | request->maybe_sent_cookies().front().cookie.Name()); |
| 9191 | EXPECT_EQ("true", request->maybe_sent_cookies().front().cookie.Value()); |
| 9192 | EXPECT_EQ( |
| 9193 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES, |
| 9194 | request->maybe_sent_cookies().front().status); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9195 | } |
| 9196 | } |
| 9197 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9198 | TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9199 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9200 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9201 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9202 | GURL first_party_url("http://example.com"); |
| 9203 | |
| 9204 | TestDelegate d; |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 9205 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 9206 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9207 | r->set_site_for_cookies(first_party_url); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9208 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 9209 | r->Start(); |
| 9210 | d.RunUntilComplete(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9211 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 9212 | EXPECT_EQ(2U, r->url_chain().size()); |
| 9213 | EXPECT_EQ(OK, d.request_status()); |
| 9214 | EXPECT_EQ(first_party_url, r->site_for_cookies()); |
| 9215 | } |
| 9216 | |
| 9217 | TEST_F(URLRequestTestHTTP, RedirectPreserveTopFrameOrigin) { |
| 9218 | ASSERT_TRUE(http_test_server()->Start()); |
| 9219 | |
| 9220 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 9221 | url::Origin top_frame_origin = |
| 9222 | url::Origin::Create(GURL("http://example.com")); |
| 9223 | TestDelegate d; |
| 9224 | |
| 9225 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 9226 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9227 | r->set_top_frame_origin(top_frame_origin); |
| 9228 | |
| 9229 | r->Start(); |
| 9230 | d.RunUntilComplete(); |
| 9231 | |
| 9232 | EXPECT_EQ(2U, r->url_chain().size()); |
| 9233 | EXPECT_EQ(OK, d.request_status()); |
| 9234 | EXPECT_EQ(top_frame_origin, *r->top_frame_origin()); |
| 9235 | } |
| 9236 | |
| 9237 | TEST_F(URLRequestTestHTTP, RedirectPreserveUnsetTopFrameOrigin) { |
| 9238 | ASSERT_TRUE(http_test_server()->Start()); |
| 9239 | |
| 9240 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 9241 | TestDelegate d; |
| 9242 | |
| 9243 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 9244 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9245 | // Since we're not setting a top frame origin, we wouldn't expect one after |
| 9246 | // the redirect. |
| 9247 | r->Start(); |
| 9248 | d.RunUntilComplete(); |
| 9249 | |
| 9250 | EXPECT_EQ(2U, r->url_chain().size()); |
| 9251 | EXPECT_EQ(OK, d.request_status()); |
| 9252 | EXPECT_FALSE(r->top_frame_origin()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9253 | } |
| 9254 | |
Josh Karlin | f3caee164 | 2018-12-10 22:54:27 | [diff] [blame] | 9255 | TEST_F(URLRequestTestHTTP, RedirectPreserveTopFrameURL) { |
| 9256 | ASSERT_TRUE(http_test_server()->Start()); |
| 9257 | |
| 9258 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 9259 | url::Origin top_frame_origin = |
| 9260 | url::Origin::Create(GURL("http://example.com")); |
| 9261 | TestDelegate d; |
| 9262 | { |
| 9263 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 9264 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9265 | r->set_top_frame_origin(top_frame_origin); |
| 9266 | |
| 9267 | r->Start(); |
| 9268 | d.RunUntilComplete(); |
| 9269 | |
| 9270 | EXPECT_EQ(2U, r->url_chain().size()); |
| 9271 | EXPECT_EQ(OK, d.request_status()); |
| 9272 | EXPECT_EQ(top_frame_origin, *r->top_frame_origin()); |
| 9273 | } |
| 9274 | } |
| 9275 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9276 | TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9277 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9278 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9279 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9280 | GURL original_first_party_url("http://example.com"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9281 | GURL expected_first_party_url(http_test_server()->GetURL("/echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9282 | |
| 9283 | TestDelegate d; |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 9284 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9285 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9286 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 9287 | r->set_site_for_cookies(original_first_party_url); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9288 | r->set_first_party_url_policy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9289 | URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 9290 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9291 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9292 | d.RunUntilComplete(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9293 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9294 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9295 | EXPECT_EQ(OK, d.request_status()); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 9296 | EXPECT_EQ(expected_first_party_url, r->site_for_cookies()); |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 9297 | } |
| 9298 | |
| 9299 | TEST_F(URLRequestTestHTTP, RedirectUpdateTopFrameOrigin) { |
| 9300 | ASSERT_TRUE(http_test_server()->Start()); |
| 9301 | |
| 9302 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 9303 | url::Origin original_top_frame_origin = |
| 9304 | url::Origin::Create(GURL("http://example.com")); |
| 9305 | url::Origin expected_top_frame_origin = |
| 9306 | url::Origin::Create(GURL(http_test_server()->GetURL("/echo"))); |
| 9307 | |
| 9308 | TestDelegate d; |
| 9309 | |
| 9310 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 9311 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9312 | r->set_top_frame_origin(original_top_frame_origin); |
| 9313 | r->set_first_party_url_policy(URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 9314 | |
| 9315 | r->Start(); |
| 9316 | d.RunUntilComplete(); |
| 9317 | |
| 9318 | EXPECT_EQ(2U, r->url_chain().size()); |
| 9319 | EXPECT_EQ(OK, d.request_status()); |
| 9320 | EXPECT_EQ(expected_top_frame_origin, *r->top_frame_origin()); |
| 9321 | } |
| 9322 | |
| 9323 | TEST_F(URLRequestTestHTTP, RedirectIgnoreUnsetTopFrameOrigin) { |
| 9324 | ASSERT_TRUE(http_test_server()->Start()); |
| 9325 | |
| 9326 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 9327 | TestDelegate d; |
| 9328 | |
| 9329 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 9330 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9331 | // Since we're not setting a top frame origin, we wouldn't expect one after |
| 9332 | // the redirect. |
| 9333 | r->set_first_party_url_policy(URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 9334 | |
| 9335 | r->Start(); |
| 9336 | d.RunUntilComplete(); |
| 9337 | |
| 9338 | EXPECT_EQ(2U, r->url_chain().size()); |
| 9339 | EXPECT_EQ(OK, d.request_status()); |
| 9340 | EXPECT_FALSE(r->top_frame_origin()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9341 | } |
| 9342 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9343 | TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9344 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9345 | |
| 9346 | const char kData[] = "hello world"; |
| 9347 | |
| 9348 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9349 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9350 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9351 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9352 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 9353 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9354 | HttpRequestHeaders headers; |
| 9355 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 9356 | base::NumberToString(base::size(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9357 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9358 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9359 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9360 | req.get(), &default_network_delegate_, |
| 9361 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9362 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 9363 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9364 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9365 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9366 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9367 | EXPECT_EQ("GET", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9368 | } |
| 9369 | |
| 9370 | TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9371 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9372 | |
| 9373 | const char kData[] = "hello world"; |
| 9374 | |
| 9375 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9376 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9377 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9378 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9379 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 9380 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9381 | HttpRequestHeaders headers; |
| 9382 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 9383 | base::NumberToString(base::size(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9384 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9385 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9386 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9387 | req.get(), &default_network_delegate_, |
| 9388 | http_test_server()->GetURL("/echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 9389 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9390 | "Very Good Reason")); |
| 9391 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9392 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9393 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9394 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9395 | EXPECT_EQ("POST", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9396 | EXPECT_EQ(kData, d.data_received()); |
| 9397 | } |
| 9398 | |
| 9399 | // Check that default A-L header is sent. |
| 9400 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9401 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9402 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 9403 | StaticHttpUserAgentSettings settings("en", std::string()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9404 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9405 | TestURLRequestContext context(true); |
| 9406 | context.set_network_delegate(&network_delegate); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9407 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9408 | context.Init(); |
| 9409 | |
| 9410 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9411 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9412 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9413 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9414 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9415 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9416 | EXPECT_EQ("en", d.data_received()); |
| 9417 | } |
| 9418 | |
| 9419 | // Check that an empty A-L header is not sent. http://crbug.com/77365. |
| 9420 | TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9421 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9422 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 9423 | std::string empty_string; // Avoid most vexing parse on line below. |
| 9424 | StaticHttpUserAgentSettings settings(empty_string, empty_string); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9425 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9426 | TestURLRequestContext context(true); |
| 9427 | context.set_network_delegate(&network_delegate); |
| 9428 | context.Init(); |
| 9429 | // We override the language after initialization because empty entries |
| 9430 | // get overridden by Init(). |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9431 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9432 | |
| 9433 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9434 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9435 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9436 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9437 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9438 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9439 | EXPECT_EQ("None", d.data_received()); |
| 9440 | } |
| 9441 | |
| 9442 | // Check that if request overrides the A-L header, the default is not appended. |
| 9443 | // See http://crbug.com/20894 |
| 9444 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9445 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9446 | |
| 9447 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9448 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9449 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9450 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9451 | HttpRequestHeaders headers; |
| 9452 | headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9453 | req->SetExtraRequestHeaders(headers); |
| 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(std::string("ru"), d.data_received()); |
| 9457 | } |
| 9458 | |
| 9459 | // Check that default A-E header is sent. |
| 9460 | TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9461 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9462 | |
| 9463 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9464 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9465 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9466 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9467 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9468 | req->SetExtraRequestHeaders(headers); |
| 9469 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9470 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9471 | EXPECT_TRUE(ContainsString(d.data_received(), "gzip")); |
| 9472 | } |
| 9473 | |
| 9474 | // Check that if request overrides the A-E header, the default is not appended. |
| 9475 | // See http://crbug.com/47381 |
| 9476 | TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9477 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9478 | |
| 9479 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9480 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9481 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9482 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9483 | HttpRequestHeaders headers; |
| 9484 | headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity"); |
[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_FALSE(ContainsString(d.data_received(), "gzip")); |
| 9489 | EXPECT_TRUE(ContainsString(d.data_received(), "identity")); |
| 9490 | } |
| 9491 | |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 9492 | // Check that setting the A-C header sends the proper header. |
| 9493 | TEST_F(URLRequestTestHTTP, SetAcceptCharset) { |
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-Charset"), |
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::kAcceptCharset, "koi-8r"); |
[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_EQ(std::string("koi-8r"), d.data_received()); |
| 9506 | } |
| 9507 | |
| 9508 | // Check that default User-Agent header is sent. |
| 9509 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9510 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9511 | |
| 9512 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9513 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9514 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9515 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9516 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9517 | d.RunUntilComplete(); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9518 | EXPECT_EQ(default_context().http_user_agent_settings()->GetUserAgent(), |
[email protected] | 051a4b3 | 2014-01-09 04:02:37 | [diff] [blame] | 9519 | d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9520 | } |
| 9521 | |
| 9522 | // Check that if request overrides the User-Agent header, |
| 9523 | // the default is not appended. |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 9524 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 9525 | #if defined(OS_IOS) |
| 9526 | #define MAYBE_OverrideUserAgent FLAKY_OverrideUserAgent |
| 9527 | #else |
| 9528 | #define MAYBE_OverrideUserAgent OverrideUserAgent |
| 9529 | #endif |
| 9530 | TEST_F(URLRequestTestHTTP, MAYBE_OverrideUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9531 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9532 | |
| 9533 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9534 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9535 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9536 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9537 | HttpRequestHeaders headers; |
| 9538 | headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9539 | req->SetExtraRequestHeaders(headers); |
| 9540 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9541 | d.RunUntilComplete(); |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 9542 | EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9543 | } |
| 9544 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9545 | // Check that a NULL HttpUserAgentSettings causes the corresponding empty |
| 9546 | // User-Agent header to be sent but does not send the Accept-Language and |
| 9547 | // Accept-Charset headers. |
| 9548 | TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9549 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9550 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9551 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9552 | TestURLRequestContext context(true); |
| 9553 | context.set_network_delegate(&network_delegate); |
| 9554 | context.Init(); |
| 9555 | // We override the HttpUserAgentSettings after initialization because empty |
| 9556 | // entries get overridden by Init(). |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 9557 | context.set_http_user_agent_settings(nullptr); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9558 | |
| 9559 | struct { |
| 9560 | const char* request; |
| 9561 | const char* expected_response; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9562 | } tests[] = {{"/echoheader?Accept-Language", "None"}, |
| 9563 | {"/echoheader?Accept-Charset", "None"}, |
| 9564 | {"/echoheader?User-Agent", ""}}; |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9565 | |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 9566 | for (size_t i = 0; i < base::size(tests); i++) { |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9567 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9568 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9569 | http_test_server()->GetURL(tests[i].request), DEFAULT_PRIORITY, &d, |
| 9570 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9571 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9572 | d.RunUntilComplete(); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9573 | EXPECT_EQ(tests[i].expected_response, d.data_received()) |
| 9574 | << " Request = \"" << tests[i].request << "\""; |
| 9575 | } |
| 9576 | } |
| 9577 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9578 | // Make sure that URLRequest passes on its priority updates to |
| 9579 | // newly-created jobs after the first one. |
| 9580 | TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9581 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9582 | |
| 9583 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9584 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9585 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9586 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9587 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9588 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9589 | std::unique_ptr<URLRequestRedirectJob> redirect_job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9590 | req.get(), &default_network_delegate_, |
| 9591 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9592 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 9593 | AddTestInterceptor()->set_main_intercept_job(std::move(redirect_job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9594 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9595 | req->SetPriority(LOW); |
| 9596 | req->Start(); |
| 9597 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9598 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9599 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9600 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9601 | req.get(), &default_network_delegate_, &job_priority)); |
| 9602 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9603 | |
| 9604 | // Should trigger |job| to be started. |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9605 | d.RunUntilComplete(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9606 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9607 | } |
| 9608 | |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9609 | // Check that creating a network request while entering/exiting suspend mode |
| 9610 | // fails as it should. This is the only case where an HttpTransactionFactory |
| 9611 | // does not return an HttpTransaction. |
| 9612 | TEST_F(URLRequestTestHTTP, NetworkSuspendTest) { |
| 9613 | // Create a new HttpNetworkLayer that thinks it's suspended. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9614 | std::unique_ptr<HttpNetworkLayer> network_layer(new HttpNetworkLayer( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9615 | default_context().http_transaction_factory()->GetSession())); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9616 | network_layer->OnSuspend(); |
| 9617 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 9618 | HttpCache http_cache(std::move(network_layer), |
zhongyi | 3ceab9f | 2017-02-04 02:06:03 | [diff] [blame] | 9619 | HttpCache::DefaultBackend::InMemory(0), |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 9620 | false /* is_main_cache */); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9621 | |
| 9622 | TestURLRequestContext context(true); |
| 9623 | context.set_http_transaction_factory(&http_cache); |
| 9624 | context.Init(); |
| 9625 | |
| 9626 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9627 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9628 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 9629 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9630 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9631 | d.RunUntilComplete(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9632 | |
| 9633 | EXPECT_TRUE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9634 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, d.request_status()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9635 | } |
| 9636 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9637 | namespace { |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9638 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9639 | // HttpTransactionFactory that synchronously fails to create transactions. |
| 9640 | class FailingHttpTransactionFactory : public HttpTransactionFactory { |
| 9641 | public: |
| 9642 | explicit FailingHttpTransactionFactory(HttpNetworkSession* network_session) |
| 9643 | : network_session_(network_session) {} |
| 9644 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 9645 | ~FailingHttpTransactionFactory() override = default; |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9646 | |
| 9647 | // HttpTransactionFactory methods: |
| 9648 | int CreateTransaction(RequestPriority priority, |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9649 | std::unique_ptr<HttpTransaction>* trans) override { |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9650 | return ERR_FAILED; |
| 9651 | } |
| 9652 | |
| 9653 | HttpCache* GetCache() override { return nullptr; } |
| 9654 | |
| 9655 | HttpNetworkSession* GetSession() override { return network_session_; } |
| 9656 | |
| 9657 | private: |
| 9658 | HttpNetworkSession* network_session_; |
| 9659 | |
| 9660 | DISALLOW_COPY_AND_ASSIGN(FailingHttpTransactionFactory); |
| 9661 | }; |
| 9662 | |
| 9663 | } // namespace |
| 9664 | |
| 9665 | // Check that when a request that fails to create an HttpTransaction can be |
| 9666 | // cancelled while the failure notification is pending, and doesn't send two |
| 9667 | // failure notifications. |
| 9668 | // |
| 9669 | // This currently only happens when in suspend mode and there's no cache, but |
| 9670 | // just use a special HttpTransactionFactory, to avoid depending on those |
| 9671 | // behaviors. |
| 9672 | TEST_F(URLRequestTestHTTP, NetworkCancelAfterCreateTransactionFailsTest) { |
| 9673 | FailingHttpTransactionFactory http_transaction_factory( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9674 | default_context().http_transaction_factory()->GetSession()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9675 | TestURLRequestContext context(true); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9676 | context.set_http_transaction_factory(&http_transaction_factory); |
| 9677 | context.set_network_delegate(default_network_delegate()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9678 | context.Init(); |
| 9679 | |
| 9680 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9681 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9682 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 9683 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9684 | // Don't send cookies (Collecting cookies is asynchronous, and need request to |
| 9685 | // try to create an HttpNetworkTransaction synchronously on start). |
| 9686 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9687 | req->Start(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9688 | req->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9689 | d.RunUntilComplete(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9690 | // Run pending error task, if there is one. |
| 9691 | base::RunLoop().RunUntilIdle(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9692 | |
| 9693 | EXPECT_TRUE(d.request_failed()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9694 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9695 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9696 | |
| 9697 | // NetworkDelegate should see the cancellation, but not the error. |
| 9698 | EXPECT_EQ(1, default_network_delegate()->canceled_requests()); |
| 9699 | EXPECT_EQ(0, default_network_delegate()->error_count()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9700 | } |
| 9701 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9702 | TEST_F(URLRequestTestHTTP, NetworkAccessedSetOnNetworkRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9703 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9704 | |
| 9705 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9706 | GURL test_url(http_test_server()->GetURL("/")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9707 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9708 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9709 | |
| 9710 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9711 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9712 | |
| 9713 | EXPECT_TRUE(req->response_info().network_accessed); |
| 9714 | } |
| 9715 | |
| 9716 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnCachedResponse) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9717 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9718 | |
| 9719 | // Populate the cache. |
| 9720 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9721 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9722 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 9723 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9724 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9725 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9726 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9727 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9728 | EXPECT_TRUE(req->response_info().network_accessed); |
| 9729 | EXPECT_FALSE(req->response_info().was_cached); |
| 9730 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9731 | req = default_context().CreateRequest( |
| 9732 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 9733 | TRAFFIC_ANNOTATION_FOR_TESTS); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9734 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9735 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9736 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9737 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9738 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9739 | EXPECT_TRUE(req->response_info().was_cached); |
| 9740 | } |
| 9741 | |
| 9742 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnLoadOnlyFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9743 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9744 | |
| 9745 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9746 | GURL test_url(http_test_server()->GetURL("/")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9747 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9748 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jkarlin | a067deed | 2016-10-27 14:48:33 | [diff] [blame] | 9749 | req->SetLoadFlags(LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9750 | |
| 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 | |
| 9754 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9755 | } |
| 9756 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 9757 | // Test that a single job with a THROTTLED priority completes |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 9758 | // correctly in the absence of contention. |
| 9759 | TEST_F(URLRequestTestHTTP, ThrottledPriority) { |
| 9760 | ASSERT_TRUE(http_test_server()->Start()); |
| 9761 | |
| 9762 | TestDelegate d; |
| 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, THROTTLED, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 9766 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9767 | d.RunUntilComplete(); |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 9768 | |
| 9769 | EXPECT_TRUE(req->status().is_success()); |
| 9770 | } |
| 9771 | |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9772 | TEST_F(URLRequestTestHTTP, RawBodyBytesNoContentEncoding) { |
| 9773 | ASSERT_TRUE(http_test_server()->Start()); |
| 9774 | |
| 9775 | TestDelegate d; |
| 9776 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9777 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 9778 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9779 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9780 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9781 | |
| 9782 | EXPECT_EQ(5, req->GetRawBodyBytes()); |
| 9783 | } |
| 9784 | |
| 9785 | TEST_F(URLRequestTestHTTP, RawBodyBytesGzipEncoding) { |
| 9786 | ASSERT_TRUE(http_test_server()->Start()); |
| 9787 | |
| 9788 | TestDelegate d; |
| 9789 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9790 | http_test_server()->GetURL("/gzip-encoded"), DEFAULT_PRIORITY, &d, |
| 9791 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9792 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9793 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9794 | |
| 9795 | EXPECT_EQ(30, req->GetRawBodyBytes()); |
| 9796 | } |
| 9797 | |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9798 | // Check that if NetworkDelegate::OnBeforeStartTransaction returns an error, |
| 9799 | // the delegate isn't called back synchronously. |
| 9800 | TEST_F(URLRequestTestHTTP, TesBeforeStartTransactionFails) { |
| 9801 | ASSERT_TRUE(http_test_server()->Start()); |
| 9802 | default_network_delegate_.set_before_start_transaction_fails(); |
| 9803 | |
| 9804 | TestDelegate d; |
| 9805 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9806 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 9807 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9808 | req->Start(); |
| 9809 | DCHECK(!d.response_completed()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9810 | d.RunUntilComplete(); |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9811 | DCHECK(d.response_completed()); |
| 9812 | EXPECT_EQ(ERR_FAILED, d.request_status()); |
| 9813 | } |
| 9814 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9815 | class URLRequestInterceptorTestHTTP : public URLRequestTestHTTP { |
| 9816 | public: |
| 9817 | // TODO(bengr): Merge this with the URLRequestInterceptorHTTPTest fixture, |
| 9818 | // ideally remove the dependency on URLRequestTestJob, and maybe move these |
| 9819 | // tests into the factory tests. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 9820 | URLRequestInterceptorTestHTTP() |
| 9821 | : URLRequestTestHTTP(), interceptor_(nullptr) {} |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9822 | |
| 9823 | void SetUpFactory() override { |
| 9824 | interceptor_ = new MockURLRequestInterceptor(); |
| 9825 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9826 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9827 | } |
| 9828 | |
| 9829 | MockURLRequestInterceptor* interceptor() const { |
| 9830 | return interceptor_; |
| 9831 | } |
| 9832 | |
| 9833 | private: |
| 9834 | MockURLRequestInterceptor* interceptor_; |
| 9835 | }; |
| 9836 | |
| 9837 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9838 | NetworkDelegateNotificationOnRedirectIntercept) { |
| 9839 | interceptor()->set_intercept_redirect(true); |
| 9840 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 9841 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 9842 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9843 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9844 | |
| 9845 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9846 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9847 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 9848 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9849 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9850 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9851 | |
| 9852 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 9853 | // Check we got one good response |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9854 | int status = d.request_status(); |
| 9855 | EXPECT_EQ(OK, status); |
| 9856 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9857 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9858 | |
| 9859 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 9860 | EXPECT_EQ(1, d.response_started_count()); |
| 9861 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9862 | |
| 9863 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9864 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9865 | EXPECT_EQ(1, default_network_delegate()->headers_received_count()); |
| 9866 | } |
| 9867 | |
| 9868 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9869 | NetworkDelegateNotificationOnErrorIntercept) { |
| 9870 | // Intercept that error and respond with an OK response. |
| 9871 | interceptor()->set_intercept_final_response(true); |
| 9872 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 9873 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 9874 | default_network_delegate()->set_can_be_intercepted_on_error(true); |
| 9875 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9876 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9877 | |
| 9878 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9879 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9880 | http_test_server()->GetURL("/two-content-lengths.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9881 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9882 | req->set_method("GET"); |
| 9883 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9884 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9885 | |
| 9886 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 9887 | |
| 9888 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9889 | int status = d.request_status(); |
| 9890 | EXPECT_EQ(OK, status); |
| 9891 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9892 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9893 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 9894 | EXPECT_EQ(1, d.response_started_count()); |
| 9895 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9896 | |
| 9897 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9898 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9899 | EXPECT_EQ(0, default_network_delegate()->headers_received_count()); |
| 9900 | } |
| 9901 | |
| 9902 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9903 | NetworkDelegateNotificationOnResponseIntercept) { |
| 9904 | // Intercept that error and respond with an OK response. |
| 9905 | interceptor()->set_intercept_final_response(true); |
| 9906 | |
| 9907 | // Intercept with a real URLRequestHttpJob. |
| 9908 | interceptor()->set_use_url_request_http_job(true); |
| 9909 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9910 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9911 | |
| 9912 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9913 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9914 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 9915 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9916 | req->set_method("GET"); |
| 9917 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9918 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9919 | |
| 9920 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 9921 | |
| 9922 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9923 | int status = d.request_status(); |
| 9924 | EXPECT_EQ(OK, status); |
| 9925 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9926 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9927 | EXPECT_EQ("hello", d.data_received()); |
| 9928 | EXPECT_EQ(1, d.response_started_count()); |
| 9929 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9930 | |
| 9931 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9932 | EXPECT_EQ(2, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9933 | EXPECT_EQ(2, default_network_delegate()->headers_received_count()); |
| 9934 | } |
| 9935 | |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9936 | class URLRequestTestReferrerPolicy : public URLRequestTest { |
| 9937 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 9938 | URLRequestTestReferrerPolicy() = default; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9939 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9940 | void InstantiateSameOriginServers(net::EmbeddedTestServer::Type type) { |
| 9941 | origin_server_.reset(new EmbeddedTestServer(type)); |
| 9942 | if (type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9943 | origin_server_->AddDefaultHandlers( |
| 9944 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9945 | } else { |
| 9946 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9947 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9948 | ASSERT_TRUE(origin_server_->Start()); |
| 9949 | } |
| 9950 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9951 | void InstantiateCrossOriginServers(net::EmbeddedTestServer::Type origin_type, |
| 9952 | net::EmbeddedTestServer::Type dest_type) { |
| 9953 | origin_server_.reset(new EmbeddedTestServer(origin_type)); |
| 9954 | if (origin_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9955 | origin_server_->AddDefaultHandlers( |
| 9956 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9957 | } else { |
| 9958 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9959 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9960 | ASSERT_TRUE(origin_server_->Start()); |
| 9961 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9962 | destination_server_.reset(new EmbeddedTestServer(dest_type)); |
| 9963 | if (dest_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9964 | destination_server_->AddDefaultHandlers( |
| 9965 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9966 | } else { |
| 9967 | destination_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9968 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9969 | ASSERT_TRUE(destination_server_->Start()); |
| 9970 | } |
| 9971 | |
| 9972 | void VerifyReferrerAfterRedirect(URLRequest::ReferrerPolicy policy, |
| 9973 | const GURL& referrer, |
| 9974 | const GURL& expected) { |
| 9975 | // Create and execute the request: we'll only have a |destination_server_| |
| 9976 | // if the origins are meant to be distinct. Otherwise, we'll use the |
| 9977 | // |origin_server_| for both endpoints. |
| 9978 | GURL destination_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9979 | destination_server_ ? destination_server_->GetURL("/echoheader?Referer") |
| 9980 | : origin_server_->GetURL("/echoheader?Referer"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9981 | GURL origin_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9982 | origin_server_->GetURL("/server-redirect?" + destination_url.spec()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9983 | |
| 9984 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9985 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9986 | origin_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9987 | req->set_referrer_policy(policy); |
| 9988 | req->SetReferrer(referrer.spec()); |
| 9989 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9990 | d.RunUntilComplete(); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9991 | |
| 9992 | EXPECT_EQ(1, d.response_started_count()); |
| 9993 | EXPECT_EQ(1, d.received_redirect_count()); |
| 9994 | EXPECT_EQ(destination_url, req->url()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9995 | EXPECT_EQ(OK, d.request_status()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9996 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9997 | |
| 9998 | EXPECT_EQ(expected.spec(), req->referrer()); |
| 9999 | if (expected.is_empty()) |
| 10000 | EXPECT_EQ("None", d.data_received()); |
| 10001 | else |
| 10002 | EXPECT_EQ(expected.spec(), d.data_received()); |
| 10003 | } |
| 10004 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10005 | EmbeddedTestServer* origin_server() const { return origin_server_.get(); } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10006 | |
| 10007 | private: |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10008 | std::unique_ptr<EmbeddedTestServer> origin_server_; |
| 10009 | std::unique_ptr<EmbeddedTestServer> destination_server_; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10010 | }; |
| 10011 | |
| 10012 | TEST_F(URLRequestTestReferrerPolicy, HTTPToSameOriginHTTP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10013 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTP); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10014 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10015 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10016 | VerifyReferrerAfterRedirect( |
| 10017 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10018 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10019 | |
| 10020 | VerifyReferrerAfterRedirect( |
| 10021 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10022 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10023 | |
| 10024 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10025 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10026 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10027 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 10028 | referrer); |
| 10029 | |
| 10030 | // The original referrer set on the request is expected to obey the referrer |
| 10031 | // policy and already be stripped to the origin; thus this test case just |
| 10032 | // checks that this policy doesn't cause the referrer to change when following |
| 10033 | // a redirect. |
| 10034 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 10035 | referrer.GetOrigin()); |
| 10036 | |
| 10037 | VerifyReferrerAfterRedirect( |
| 10038 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 10039 | referrer); |
| 10040 | |
| 10041 | // The original referrer set on the request is expected to obey the referrer |
| 10042 | // policy and already be stripped to the origin; thus this test case just |
| 10043 | // checks that this policy doesn't cause the referrer to change when following |
| 10044 | // a redirect. |
| 10045 | VerifyReferrerAfterRedirect( |
| 10046 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 10047 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 10048 | |
| 10049 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10050 | } |
| 10051 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10052 | TEST_F(URLRequestTestReferrerPolicy, HTTPToCrossOriginHTTP) { |
| 10053 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 10054 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10055 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10056 | |
| 10057 | VerifyReferrerAfterRedirect( |
| 10058 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10059 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10060 | |
| 10061 | VerifyReferrerAfterRedirect( |
| 10062 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10063 | referrer, referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10064 | |
| 10065 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10066 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 10067 | referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10068 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10069 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 10070 | referrer); |
| 10071 | |
| 10072 | // The original referrer set on the request is expected to obey the referrer |
| 10073 | // policy and already be stripped to the origin; thus this test case just |
| 10074 | // checks that this policy doesn't cause the referrer to change when following |
| 10075 | // a redirect. |
| 10076 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 10077 | referrer.GetOrigin()); |
| 10078 | |
| 10079 | VerifyReferrerAfterRedirect( |
| 10080 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 10081 | |
| 10082 | // The original referrer set on the request is expected to obey the referrer |
| 10083 | // policy and already be stripped to the origin; thus this test case just |
| 10084 | // checks that this policy doesn't cause the referrer to change when following |
| 10085 | // a redirect. |
| 10086 | VerifyReferrerAfterRedirect( |
| 10087 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 10088 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 10089 | |
| 10090 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10091 | } |
| 10092 | |
| 10093 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToSameOriginHTTPS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10094 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10095 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10096 | |
| 10097 | VerifyReferrerAfterRedirect( |
| 10098 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10099 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10100 | |
| 10101 | VerifyReferrerAfterRedirect( |
| 10102 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10103 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10104 | |
| 10105 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10106 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10107 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10108 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 10109 | referrer); |
| 10110 | |
| 10111 | // The original referrer set on the request is expected to obey the referrer |
| 10112 | // policy and already be stripped to the origin; thus this test case just |
| 10113 | // checks that this policy doesn't cause the referrer to change when following |
| 10114 | // a redirect. |
| 10115 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 10116 | referrer.GetOrigin()); |
| 10117 | |
| 10118 | VerifyReferrerAfterRedirect( |
| 10119 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 10120 | referrer); |
| 10121 | |
| 10122 | // The original referrer set on the request is expected to obey the referrer |
| 10123 | // policy and already be stripped to the origin; thus this test case just |
| 10124 | // checks that this policy doesn't cause the referrer to change when following |
| 10125 | // a redirect. |
| 10126 | VerifyReferrerAfterRedirect( |
| 10127 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 10128 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 10129 | |
| 10130 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10131 | } |
| 10132 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10133 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToCrossOriginHTTPS) { |
| 10134 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 10135 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10136 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10137 | |
| 10138 | VerifyReferrerAfterRedirect( |
| 10139 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10140 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10141 | |
| 10142 | VerifyReferrerAfterRedirect( |
| 10143 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10144 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10145 | |
| 10146 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10147 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10148 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10149 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10150 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 10151 | referrer); |
| 10152 | |
| 10153 | // The original referrer set on the request is expected to obey the referrer |
| 10154 | // policy and already be stripped to the origin; thus this test case just |
| 10155 | // checks that this policy doesn't cause the referrer to change when following |
| 10156 | // a redirect. |
| 10157 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 10158 | referrer.GetOrigin()); |
| 10159 | |
| 10160 | VerifyReferrerAfterRedirect( |
| 10161 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 10162 | |
| 10163 | // The original referrer set on the request is expected to obey the referrer |
| 10164 | // policy and already be stripped to the origin; thus this test case just |
| 10165 | // checks that this policy doesn't cause the referrer to change when following |
| 10166 | // a redirect. |
| 10167 | VerifyReferrerAfterRedirect( |
| 10168 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 10169 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 10170 | |
| 10171 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10172 | } |
| 10173 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10174 | TEST_F(URLRequestTestReferrerPolicy, HTTPToHTTPS) { |
| 10175 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 10176 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10177 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10178 | |
| 10179 | VerifyReferrerAfterRedirect( |
| 10180 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10181 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10182 | |
| 10183 | VerifyReferrerAfterRedirect( |
| 10184 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10185 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10186 | |
| 10187 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10188 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10189 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10190 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10191 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 10192 | referrer); |
| 10193 | |
| 10194 | // The original referrer set on the request is expected to obey the referrer |
| 10195 | // policy and already be stripped to the origin; thus this test case just |
| 10196 | // checks that this policy doesn't cause the referrer to change when following |
| 10197 | // a redirect. |
| 10198 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 10199 | referrer.GetOrigin()); |
| 10200 | |
| 10201 | VerifyReferrerAfterRedirect( |
| 10202 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 10203 | |
| 10204 | // The original referrer set on the request is expected to obey the referrer |
| 10205 | // policy and already be stripped to the origin; thus this test case just |
| 10206 | // checks that this policy doesn't cause the referrer to change when following |
| 10207 | // a redirect. |
| 10208 | VerifyReferrerAfterRedirect( |
| 10209 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 10210 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 10211 | |
| 10212 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10213 | } |
| 10214 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10215 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToHTTP) { |
| 10216 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 10217 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10218 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10219 | |
| 10220 | VerifyReferrerAfterRedirect( |
| 10221 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10222 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10223 | |
| 10224 | VerifyReferrerAfterRedirect( |
| 10225 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10226 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10227 | |
| 10228 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10229 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10230 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10231 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10232 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 10233 | referrer); |
| 10234 | |
| 10235 | // The original referrer set on the request is expected to obey the referrer |
| 10236 | // policy and already be stripped to the origin; thus this test case just |
| 10237 | // checks that this policy doesn't cause the referrer to change when following |
| 10238 | // a redirect. |
| 10239 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 10240 | referrer.GetOrigin()); |
| 10241 | |
| 10242 | VerifyReferrerAfterRedirect( |
| 10243 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 10244 | |
| 10245 | // The original referrer set on the request is expected to obey the referrer |
| 10246 | // policy and already be stripped to the origin, though it should be |
| 10247 | // subsequently cleared during the downgrading redirect. |
| 10248 | VerifyReferrerAfterRedirect( |
| 10249 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 10250 | referrer.GetOrigin(), GURL()); |
| 10251 | |
| 10252 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10253 | } |
| 10254 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 10255 | class HTTPSRequestTest : public TestWithScopedTaskEnvironment { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 10256 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 10257 | HTTPSRequestTest() : default_context_(true) { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10258 | default_context_.set_network_delegate(&default_network_delegate_); |
| 10259 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 10260 | } |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10261 | ~HTTPSRequestTest() override { |
| 10262 | SetTransportSecurityStateSourceForTesting(nullptr); |
| 10263 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 10264 | |
| 10265 | protected: |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 10266 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10267 | TestURLRequestContext default_context_; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10268 | }; |
| 10269 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 10270 | TEST_F(HTTPSRequestTest, HTTPSGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10271 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10272 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10273 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 10274 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10275 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10276 | TestDelegate d; |
| 10277 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10278 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10279 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10280 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10281 | r->Start(); |
| 10282 | EXPECT_TRUE(r->is_pending()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10283 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10284 | d.RunUntilComplete(); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10285 | |
| 10286 | EXPECT_EQ(1, d.response_started_count()); |
| 10287 | EXPECT_FALSE(d.received_data_before_response()); |
| 10288 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10289 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 10290 | EXPECT_EQ(test_server.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 10291 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 10292 | EXPECT_EQ(test_server.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 10293 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10294 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10295 | } |
| 10296 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 10297 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10298 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10299 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 10300 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10301 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 10302 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10303 | |
| 10304 | bool err_allowed = true; |
| 10305 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 10306 | TestDelegate d; |
| 10307 | { |
| 10308 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10309 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10310 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10311 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10312 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10313 | r->Start(); |
| 10314 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10315 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10316 | d.RunUntilComplete(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10317 | |
| 10318 | EXPECT_EQ(1, d.response_started_count()); |
| 10319 | EXPECT_FALSE(d.received_data_before_response()); |
| 10320 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 10321 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10322 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10323 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 10324 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10325 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 10326 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10327 | } |
| 10328 | } |
| 10329 | } |
| 10330 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 10331 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10332 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10333 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED); |
| 10334 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10335 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 10336 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10337 | |
| 10338 | // Iterate from false to true, just so that we do the opposite of the |
| 10339 | // previous test in order to increase test coverage. |
| 10340 | bool err_allowed = false; |
| 10341 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 10342 | TestDelegate d; |
| 10343 | { |
| 10344 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10345 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10346 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10347 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10348 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10349 | r->Start(); |
| 10350 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10351 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10352 | d.RunUntilComplete(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10353 | |
| 10354 | EXPECT_EQ(1, d.response_started_count()); |
| 10355 | EXPECT_FALSE(d.received_data_before_response()); |
| 10356 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 10357 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10358 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10359 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 10360 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10361 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 10362 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10363 | } |
| 10364 | } |
| 10365 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 10366 | |
Emily Stark | 79fba584 | 2019-04-25 04:59:36 | [diff] [blame] | 10367 | // A TestDelegate used to test that an appropriate net error code is provided |
| 10368 | // when an SSL certificate error occurs. |
| 10369 | class SSLNetErrorTestDelegate : public TestDelegate { |
| 10370 | public: |
| 10371 | void OnSSLCertificateError(URLRequest* request, |
| 10372 | int net_error, |
| 10373 | const SSLInfo& ssl_info, |
| 10374 | bool fatal) override { |
| 10375 | net_error_ = net_error; |
| 10376 | on_ssl_certificate_error_called_ = true; |
| 10377 | TestDelegate::OnSSLCertificateError(request, net_error, ssl_info, fatal); |
| 10378 | } |
| 10379 | |
| 10380 | bool on_ssl_certificate_error_called() { |
| 10381 | return on_ssl_certificate_error_called_; |
| 10382 | } |
| 10383 | |
| 10384 | int net_error() { return net_error_; } |
| 10385 | |
| 10386 | private: |
| 10387 | bool on_ssl_certificate_error_called_ = false; |
| 10388 | int net_error_ = net::OK; |
| 10389 | }; |
| 10390 | |
| 10391 | // Tests that the URLRequest::Delegate receives an appropriate net error code |
| 10392 | // when an SSL certificate error occurs. |
| 10393 | TEST_F(HTTPSRequestTest, SSLNetErrorReportedToDelegate) { |
| 10394 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10395 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED); |
| 10396 | test_server.AddDefaultHandlers( |
| 10397 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10398 | ASSERT_TRUE(test_server.Start()); |
| 10399 | |
| 10400 | SSLNetErrorTestDelegate d; |
| 10401 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10402 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10403 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10404 | r->Start(); |
| 10405 | EXPECT_TRUE(r->is_pending()); |
| 10406 | d.RunUntilComplete(); |
| 10407 | |
| 10408 | EXPECT_TRUE(d.on_ssl_certificate_error_called()); |
| 10409 | EXPECT_EQ(net::ERR_CERT_DATE_INVALID, d.net_error()); |
| 10410 | } |
| 10411 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10412 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 10413 | // security state. (see http://crbug.com/550977). |
| 10414 | #if !defined(OS_IOS) |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10415 | // This tests that a load of a domain with preloaded HSTS and HPKP with a |
| 10416 | // certificate error sets the |certificate_errors_are_fatal| flag correctly. |
| 10417 | // This flag will cause the interstitial to be fatal. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10418 | TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10419 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 10420 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10421 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10422 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 10423 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10424 | ASSERT_TRUE(test_server.Start()); |
| 10425 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10426 | // We require that the URL be hsts-hpkp-preloaded.test. This is a test domain |
| 10427 | // that has a preloaded HSTS+HPKP entry in the TransportSecurityState. This |
| 10428 | // means that we have to use a MockHostResolver in order to direct |
| 10429 | // hsts-hpkp-preloaded.test to the testserver. By default, MockHostResolver |
| 10430 | // maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10431 | |
| 10432 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 10433 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10434 | TestURLRequestContext context(true); |
| 10435 | context.set_network_delegate(&network_delegate); |
| 10436 | context.set_host_resolver(&host_resolver); |
| 10437 | TransportSecurityState transport_security_state; |
| 10438 | context.set_transport_security_state(&transport_security_state); |
| 10439 | context.Init(); |
| 10440 | |
| 10441 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10442 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10443 | GURL(base::StringPrintf("https://hsts-hpkp-preloaded.test:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 10444 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10445 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10446 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10447 | r->Start(); |
| 10448 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10449 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10450 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10451 | |
| 10452 | EXPECT_EQ(1, d.response_started_count()); |
| 10453 | EXPECT_FALSE(d.received_data_before_response()); |
| 10454 | EXPECT_TRUE(d.have_certificate_errors()); |
| 10455 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 10456 | } |
| 10457 | |
| 10458 | // This tests that cached HTTPS page loads do not cause any updates to the |
| 10459 | // TransportSecurityState. |
| 10460 | TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10461 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 10462 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10463 | // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't |
| 10464 | // matter. It just has to be any error. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10465 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10466 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 10467 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10468 | ASSERT_TRUE(test_server.Start()); |
| 10469 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10470 | // We require that the URL be hsts-hpkp-preloaded.test. This is a test domain |
| 10471 | // that has a preloaded HSTS+HPKP entry in the TransportSecurityState. This |
| 10472 | // means that we have to use a MockHostResolver in order to direct |
| 10473 | // hsts-hpkp-preloaded.test to the testserver. By default, MockHostResolver |
| 10474 | // maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10475 | |
| 10476 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 10477 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10478 | TestURLRequestContext context(true); |
| 10479 | context.set_network_delegate(&network_delegate); |
| 10480 | context.set_host_resolver(&host_resolver); |
| 10481 | TransportSecurityState transport_security_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10482 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10483 | TransportSecurityState::STSState static_sts_state; |
| 10484 | TransportSecurityState::PKPState static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10485 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10486 | "hsts-hpkp-preloaded.test", &static_sts_state, &static_pkp_state)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10487 | context.set_transport_security_state(&transport_security_state); |
| 10488 | context.Init(); |
| 10489 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10490 | TransportSecurityState::STSState dynamic_sts_state; |
| 10491 | TransportSecurityState::PKPState dynamic_pkp_state; |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10492 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
| 10493 | "hsts-hpkp-preloaded.test", &dynamic_sts_state)); |
| 10494 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
| 10495 | "hsts-hpkp-preloaded.test", &dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10496 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10497 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10498 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10499 | GURL(base::StringPrintf("https://hsts-hpkp-preloaded.test:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 10500 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10501 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10502 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10503 | r->Start(); |
| 10504 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10505 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10506 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10507 | |
| 10508 | EXPECT_EQ(1, d.response_started_count()); |
| 10509 | EXPECT_FALSE(d.received_data_before_response()); |
| 10510 | EXPECT_TRUE(d.have_certificate_errors()); |
| 10511 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 10512 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10513 | // Get a fresh copy of the states, and check that they haven't changed. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10514 | TransportSecurityState::STSState new_static_sts_state; |
| 10515 | TransportSecurityState::PKPState new_static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10516 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10517 | "hsts-hpkp-preloaded.test", &new_static_sts_state, |
| 10518 | &new_static_pkp_state)); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10519 | TransportSecurityState::STSState new_dynamic_sts_state; |
| 10520 | TransportSecurityState::PKPState new_dynamic_pkp_state; |
| 10521 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10522 | "hsts-hpkp-preloaded.test", &new_dynamic_sts_state)); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10523 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10524 | "hsts-hpkp-preloaded.test", &new_dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10525 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10526 | EXPECT_EQ(new_static_sts_state.upgrade_mode, static_sts_state.upgrade_mode); |
| 10527 | EXPECT_EQ(new_static_sts_state.include_subdomains, |
| 10528 | static_sts_state.include_subdomains); |
| 10529 | EXPECT_EQ(new_static_pkp_state.include_subdomains, |
| 10530 | static_pkp_state.include_subdomains); |
rsleevi | 91d4c9c | 2016-05-14 20:28:48 | [diff] [blame] | 10531 | EXPECT_EQ(new_static_pkp_state.spki_hashes, static_pkp_state.spki_hashes); |
| 10532 | EXPECT_EQ(new_static_pkp_state.bad_spki_hashes, |
| 10533 | static_pkp_state.bad_spki_hashes); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10534 | } |
| 10535 | |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10536 | // Make sure HSTS preserves a POST request's method and body. |
| 10537 | TEST_F(HTTPSRequestTest, HSTSPreservesPosts) { |
| 10538 | static const char kData[] = "hello world"; |
| 10539 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10540 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10541 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10542 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10543 | ASSERT_TRUE(test_server.Start()); |
| 10544 | |
| 10545 | |
| 10546 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 10547 | // address, so a MockHostResolver is needed to redirect www.somewhere.com to |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10548 | // the EmbeddedTestServer. By default, MockHostResolver maps all hosts |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10549 | // to 127.0.0.1. |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10550 | MockHostResolver host_resolver; |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10551 | |
| 10552 | // Force https for www.somewhere.com. |
| 10553 | TransportSecurityState transport_security_state; |
[email protected] | 474f079e | 2013-03-02 19:11:20 | [diff] [blame] | 10554 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 10555 | bool include_subdomains = false; |
| 10556 | transport_security_state.AddHSTS("www.somewhere.com", expiry, |
| 10557 | include_subdomains); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10558 | |
| 10559 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 10560 | |
| 10561 | TestURLRequestContext context(true); |
| 10562 | context.set_host_resolver(&host_resolver); |
| 10563 | context.set_transport_security_state(&transport_security_state); |
| 10564 | context.set_network_delegate(&network_delegate); |
| 10565 | context.Init(); |
| 10566 | |
| 10567 | TestDelegate d; |
| 10568 | // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will |
| 10569 | // cause a certificate error. Ignore the error. |
| 10570 | d.set_allow_certificate_errors(true); |
| 10571 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10572 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10573 | GURL(base::StringPrintf("http://www.somewhere.com:%d/echo", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 10574 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10575 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10576 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 10577 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10578 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10579 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10580 | d.RunUntilComplete(); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10581 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10582 | EXPECT_EQ("https", req->url().scheme()); |
| 10583 | EXPECT_EQ("POST", req->method()); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10584 | EXPECT_EQ(kData, d.data_received()); |
[email protected] | 07d9bfd | 2013-06-27 14:16:40 | [diff] [blame] | 10585 | |
| 10586 | LoadTimingInfo load_timing_info; |
| 10587 | network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info); |
| 10588 | // LoadTimingInfo of HSTS redirects is similar to that of network cache hits |
| 10589 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10590 | } |
| 10591 | |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10592 | // Make sure that the CORS headers are added to cross-origin HSTS redirects. |
| 10593 | TEST_F(HTTPSRequestTest, HSTSCrossOriginAddHeaders) { |
| 10594 | static const char kOriginHeaderValue[] = "http://www.example.com"; |
| 10595 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10596 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10597 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10598 | ASSERT_TRUE(test_server.Start()); |
| 10599 | |
| 10600 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 10601 | // address, so a MockHostResolver is needed to redirect example.net to the |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10602 | // EmbeddedTestServer. MockHostResolver maps all hosts to 127.0.0.1 by |
| 10603 | // default. |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10604 | MockHostResolver host_resolver; |
| 10605 | |
| 10606 | TransportSecurityState transport_security_state; |
| 10607 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 10608 | bool include_subdomains = false; |
| 10609 | transport_security_state.AddHSTS("example.net", expiry, include_subdomains); |
| 10610 | |
| 10611 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 10612 | |
| 10613 | MockCertVerifier cert_verifier; |
| 10614 | cert_verifier.set_default_result(OK); |
| 10615 | |
| 10616 | TestURLRequestContext context(true); |
| 10617 | context.set_host_resolver(&host_resolver); |
| 10618 | context.set_transport_security_state(&transport_security_state); |
| 10619 | context.set_network_delegate(&network_delegate); |
| 10620 | context.set_cert_verifier(&cert_verifier); |
| 10621 | context.Init(); |
| 10622 | |
| 10623 | GURL hsts_http_url(base::StringPrintf("http://example.net:%d/somehstssite", |
| 10624 | test_server.host_port_pair().port())); |
| 10625 | url::Replacements<char> replacements; |
| 10626 | const char kNewScheme[] = "https"; |
| 10627 | replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme))); |
| 10628 | GURL hsts_https_url = hsts_http_url.ReplaceComponents(replacements); |
| 10629 | |
| 10630 | TestDelegate d; |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10631 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10632 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 10633 | hsts_http_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10634 | // Set Origin header to simulate a cross-origin request. |
| 10635 | HttpRequestHeaders request_headers; |
| 10636 | request_headers.SetHeader("Origin", kOriginHeaderValue); |
| 10637 | req->SetExtraRequestHeaders(request_headers); |
| 10638 | |
| 10639 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10640 | d.RunUntilRedirect(); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10641 | |
| 10642 | EXPECT_EQ(1, d.received_redirect_count()); |
| 10643 | |
| 10644 | const HttpResponseHeaders* headers = req->response_headers(); |
| 10645 | std::string redirect_location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 10646 | EXPECT_TRUE( |
| 10647 | headers->EnumerateHeader(nullptr, "Location", &redirect_location)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10648 | EXPECT_EQ(hsts_https_url.spec(), redirect_location); |
| 10649 | |
| 10650 | std::string received_cors_header; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 10651 | EXPECT_TRUE(headers->EnumerateHeader(nullptr, "Access-Control-Allow-Origin", |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10652 | &received_cors_header)); |
| 10653 | EXPECT_EQ(kOriginHeaderValue, received_cors_header); |
| 10654 | } |
| 10655 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10656 | namespace { |
| 10657 | |
| 10658 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 10659 | public: |
| 10660 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10661 | set_on_complete(base::DoNothing()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10662 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 10663 | void OnCertificateRequested(URLRequest* request, |
| 10664 | SSLCertRequestInfo* cert_request_info) override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10665 | on_certificate_requested_count_++; |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10666 | std::move(on_certificate_requested_).Run(); |
| 10667 | } |
| 10668 | void RunUntilCertificateRequested() { |
| 10669 | base::RunLoop run_loop; |
| 10670 | on_certificate_requested_ = run_loop.QuitClosure(); |
| 10671 | run_loop.Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10672 | } |
| 10673 | int on_certificate_requested_count() { |
| 10674 | return on_certificate_requested_count_; |
| 10675 | } |
| 10676 | private: |
| 10677 | int on_certificate_requested_count_; |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10678 | base::OnceClosure on_certificate_requested_; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10679 | }; |
| 10680 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10681 | class TestSSLPrivateKey : public SSLPrivateKey { |
| 10682 | public: |
| 10683 | explicit TestSSLPrivateKey(scoped_refptr<SSLPrivateKey> key) |
| 10684 | : key_(std::move(key)) {} |
| 10685 | |
| 10686 | void set_fail_signing(bool fail_signing) { fail_signing_ = fail_signing; } |
| 10687 | int sign_count() const { return sign_count_; } |
| 10688 | |
David Benjamin | b65b073 | 2018-11-09 20:33:53 | [diff] [blame] | 10689 | std::string GetProviderName() override { return key_->GetProviderName(); } |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10690 | std::vector<uint16_t> GetAlgorithmPreferences() override { |
| 10691 | return key_->GetAlgorithmPreferences(); |
| 10692 | } |
| 10693 | void Sign(uint16_t algorithm, |
| 10694 | base::span<const uint8_t> input, |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 10695 | SignCallback callback) override { |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10696 | sign_count_++; |
| 10697 | if (fail_signing_) { |
| 10698 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 10699 | FROM_HERE, base::BindOnce(std::move(callback), |
| 10700 | ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED, |
| 10701 | std::vector<uint8_t>())); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10702 | } else { |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 10703 | key_->Sign(algorithm, input, std::move(callback)); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10704 | } |
| 10705 | } |
| 10706 | |
| 10707 | private: |
| 10708 | ~TestSSLPrivateKey() override = default; |
| 10709 | |
| 10710 | scoped_refptr<SSLPrivateKey> key_; |
| 10711 | bool fail_signing_ = false; |
| 10712 | int sign_count_ = 0; |
| 10713 | }; |
| 10714 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10715 | } // namespace |
| 10716 | |
| 10717 | // TODO(davidben): Test the rest of the code. Specifically, |
| 10718 | // - Filtering which certificates to select. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10719 | // - Getting a certificate request in an SSL renegotiation sending the |
| 10720 | // HTTP request. |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10721 | TEST_F(HTTPSRequestTest, ClientAuthNoCertificate) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10722 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10723 | net::SSLServerConfig ssl_config; |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 10724 | ssl_config.client_cert_type = |
| 10725 | SSLServerConfig::ClientCertType::OPTIONAL_CLIENT_CERT; |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10726 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10727 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10728 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10729 | ASSERT_TRUE(test_server.Start()); |
| 10730 | |
| 10731 | SSLClientAuthTestDelegate d; |
| 10732 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10733 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10734 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10735 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10736 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10737 | r->Start(); |
| 10738 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10739 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10740 | d.RunUntilCertificateRequested(); |
| 10741 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10742 | |
| 10743 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10744 | EXPECT_FALSE(d.received_data_before_response()); |
| 10745 | EXPECT_EQ(0, d.bytes_received()); |
| 10746 | |
| 10747 | // Send no certificate. |
| 10748 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 10749 | // all platforms so we can test sending a cert as well. |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 10750 | r->ContinueWithCertificate(nullptr, nullptr); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10751 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10752 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10753 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10754 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10755 | EXPECT_EQ(1, d.response_started_count()); |
| 10756 | EXPECT_FALSE(d.received_data_before_response()); |
| 10757 | EXPECT_NE(0, d.bytes_received()); |
| 10758 | } |
| 10759 | } |
| 10760 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10761 | TEST_F(HTTPSRequestTest, ClientAuth) { |
| 10762 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 10763 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 10764 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 10765 | ASSERT_TRUE(identity); |
| 10766 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 10767 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 10768 | |
| 10769 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10770 | net::SSLServerConfig ssl_config; |
| 10771 | ssl_config.client_cert_type = |
| 10772 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 10773 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 10774 | test_server.AddDefaultHandlers( |
| 10775 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10776 | ASSERT_TRUE(test_server.Start()); |
| 10777 | |
| 10778 | { |
| 10779 | SSLClientAuthTestDelegate d; |
| 10780 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10781 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10782 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10783 | |
| 10784 | r->Start(); |
| 10785 | EXPECT_TRUE(r->is_pending()); |
| 10786 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10787 | d.RunUntilCertificateRequested(); |
| 10788 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10789 | |
| 10790 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10791 | EXPECT_FALSE(d.received_data_before_response()); |
| 10792 | EXPECT_EQ(0, d.bytes_received()); |
| 10793 | |
| 10794 | // Send a certificate. |
| 10795 | r->ContinueWithCertificate(identity->certificate(), private_key); |
| 10796 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10797 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10798 | |
| 10799 | EXPECT_EQ(OK, d.request_status()); |
| 10800 | EXPECT_EQ(1, d.response_started_count()); |
| 10801 | EXPECT_FALSE(d.received_data_before_response()); |
| 10802 | EXPECT_NE(0, d.bytes_received()); |
| 10803 | |
| 10804 | // The private key should have been used. |
| 10805 | EXPECT_EQ(1, private_key->sign_count()); |
| 10806 | } |
| 10807 | |
| 10808 | // Close all connections and clear the session cache to force a new handshake. |
| 10809 | default_context_.http_transaction_factory() |
| 10810 | ->GetSession() |
| 10811 | ->CloseAllConnections(); |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10812 | default_context_.http_transaction_factory() |
| 10813 | ->GetSession() |
| 10814 | ->ClearSSLSessionCache(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10815 | |
| 10816 | // Connecting again should not call OnCertificateRequested. The identity is |
| 10817 | // taken from the client auth cache. |
| 10818 | { |
| 10819 | SSLClientAuthTestDelegate d; |
| 10820 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10821 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10822 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10823 | |
| 10824 | r->Start(); |
| 10825 | EXPECT_TRUE(r->is_pending()); |
| 10826 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10827 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10828 | |
| 10829 | EXPECT_EQ(OK, d.request_status()); |
| 10830 | EXPECT_EQ(0, d.on_certificate_requested_count()); |
| 10831 | EXPECT_FALSE(d.received_data_before_response()); |
| 10832 | EXPECT_EQ(1, d.response_started_count()); |
| 10833 | EXPECT_FALSE(d.received_data_before_response()); |
| 10834 | EXPECT_NE(0, d.bytes_received()); |
| 10835 | |
| 10836 | // The private key should have been used. |
| 10837 | EXPECT_EQ(2, private_key->sign_count()); |
| 10838 | } |
| 10839 | } |
| 10840 | |
| 10841 | // Test that private keys that fail to sign anything get evicted from the cache. |
| 10842 | TEST_F(HTTPSRequestTest, ClientAuthFailSigning) { |
| 10843 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 10844 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 10845 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 10846 | ASSERT_TRUE(identity); |
| 10847 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 10848 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 10849 | private_key->set_fail_signing(true); |
| 10850 | |
| 10851 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10852 | net::SSLServerConfig ssl_config; |
| 10853 | ssl_config.client_cert_type = |
| 10854 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 10855 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 10856 | test_server.AddDefaultHandlers( |
| 10857 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10858 | ASSERT_TRUE(test_server.Start()); |
| 10859 | |
| 10860 | { |
| 10861 | SSLClientAuthTestDelegate d; |
| 10862 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10863 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10864 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10865 | |
| 10866 | r->Start(); |
| 10867 | EXPECT_TRUE(r->is_pending()); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10868 | |
| 10869 | d.RunUntilCertificateRequested(); |
| 10870 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10871 | |
| 10872 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10873 | EXPECT_FALSE(d.received_data_before_response()); |
| 10874 | EXPECT_EQ(0, d.bytes_received()); |
| 10875 | |
| 10876 | // Send a certificate. |
| 10877 | r->ContinueWithCertificate(identity->certificate(), private_key); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10878 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10879 | |
| 10880 | // The private key cannot sign anything, so we report an error. |
| 10881 | EXPECT_EQ(ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED, d.request_status()); |
| 10882 | EXPECT_EQ(1, d.response_started_count()); |
| 10883 | EXPECT_FALSE(d.received_data_before_response()); |
| 10884 | EXPECT_EQ(0, d.bytes_received()); |
| 10885 | |
| 10886 | // The private key should have been used. |
| 10887 | EXPECT_EQ(1, private_key->sign_count()); |
| 10888 | } |
| 10889 | |
| 10890 | // Close all connections and clear the session cache to force a new handshake. |
| 10891 | default_context_.http_transaction_factory() |
| 10892 | ->GetSession() |
| 10893 | ->CloseAllConnections(); |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10894 | default_context_.http_transaction_factory() |
| 10895 | ->GetSession() |
| 10896 | ->ClearSSLSessionCache(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10897 | |
| 10898 | // The bad identity should have been evicted from the cache, so connecting |
| 10899 | // again should call OnCertificateRequested again. |
| 10900 | { |
| 10901 | SSLClientAuthTestDelegate d; |
| 10902 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10903 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10904 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10905 | |
| 10906 | r->Start(); |
| 10907 | EXPECT_TRUE(r->is_pending()); |
| 10908 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10909 | d.RunUntilCertificateRequested(); |
| 10910 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10911 | |
| 10912 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10913 | EXPECT_FALSE(d.received_data_before_response()); |
| 10914 | EXPECT_EQ(0, d.bytes_received()); |
| 10915 | |
| 10916 | // There should have been no additional uses of the private key. |
| 10917 | EXPECT_EQ(1, private_key->sign_count()); |
| 10918 | } |
| 10919 | } |
| 10920 | |
| 10921 | // Test that cached private keys that fail to sign anything trigger a |
| 10922 | // retry. This is so we handle unplugged smartcards |
| 10923 | // gracefully. https://crbug.com/813022. |
| 10924 | TEST_F(HTTPSRequestTest, ClientAuthFailSigningRetry) { |
| 10925 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 10926 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 10927 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 10928 | ASSERT_TRUE(identity); |
| 10929 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 10930 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 10931 | |
| 10932 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10933 | net::SSLServerConfig ssl_config; |
| 10934 | ssl_config.client_cert_type = |
| 10935 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 10936 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 10937 | test_server.AddDefaultHandlers( |
| 10938 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10939 | ASSERT_TRUE(test_server.Start()); |
| 10940 | |
| 10941 | // Connect with a client certificate to put it in the client auth cache. |
| 10942 | { |
| 10943 | SSLClientAuthTestDelegate d; |
| 10944 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10945 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10946 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10947 | |
| 10948 | r->Start(); |
| 10949 | EXPECT_TRUE(r->is_pending()); |
| 10950 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10951 | d.RunUntilCertificateRequested(); |
| 10952 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10953 | |
| 10954 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10955 | EXPECT_FALSE(d.received_data_before_response()); |
| 10956 | EXPECT_EQ(0, d.bytes_received()); |
| 10957 | |
| 10958 | r->ContinueWithCertificate(identity->certificate(), private_key); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10959 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10960 | |
| 10961 | EXPECT_EQ(OK, d.request_status()); |
| 10962 | EXPECT_EQ(1, d.response_started_count()); |
| 10963 | EXPECT_FALSE(d.received_data_before_response()); |
| 10964 | EXPECT_NE(0, d.bytes_received()); |
| 10965 | |
| 10966 | // The private key should have been used. |
| 10967 | EXPECT_EQ(1, private_key->sign_count()); |
| 10968 | } |
| 10969 | |
| 10970 | // Close all connections and clear the session cache to force a new handshake. |
| 10971 | default_context_.http_transaction_factory() |
| 10972 | ->GetSession() |
| 10973 | ->CloseAllConnections(); |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10974 | default_context_.http_transaction_factory() |
| 10975 | ->GetSession() |
| 10976 | ->ClearSSLSessionCache(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10977 | |
| 10978 | // Cause the private key to fail. Connecting again should attempt to use it, |
| 10979 | // notice the failure, and then request a new identity via |
| 10980 | // OnCertificateRequested. |
| 10981 | private_key->set_fail_signing(true); |
| 10982 | |
| 10983 | { |
| 10984 | SSLClientAuthTestDelegate d; |
| 10985 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10986 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10987 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10988 | |
| 10989 | r->Start(); |
| 10990 | EXPECT_TRUE(r->is_pending()); |
| 10991 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10992 | d.RunUntilCertificateRequested(); |
| 10993 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10994 | |
| 10995 | // There was an additional signing call on the private key (the one which |
| 10996 | // failed). |
| 10997 | EXPECT_EQ(2, private_key->sign_count()); |
| 10998 | |
| 10999 | // That caused another OnCertificateRequested call. |
| 11000 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 11001 | EXPECT_FALSE(d.received_data_before_response()); |
| 11002 | EXPECT_EQ(0, d.bytes_received()); |
| 11003 | } |
| 11004 | } |
| 11005 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11006 | TEST_F(HTTPSRequestTest, ResumeTest) { |
| 11007 | // Test that we attempt a session resume when making two connections to the |
| 11008 | // same host. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11009 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11010 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11011 | SpawnedTestServer test_server( |
| 11012 | SpawnedTestServer::TYPE_HTTPS, |
| 11013 | ssl_options, |
| 11014 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11015 | ASSERT_TRUE(test_server.Start()); |
| 11016 | |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 11017 | default_context_.http_transaction_factory() |
| 11018 | ->GetSession() |
| 11019 | ->ClearSSLSessionCache(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11020 | |
| 11021 | { |
| 11022 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11023 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11024 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 11025 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11026 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11027 | r->Start(); |
| 11028 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11029 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11030 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11031 | |
| 11032 | EXPECT_EQ(1, d.response_started_count()); |
| 11033 | } |
| 11034 | |
| 11035 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 11036 | CloseAllConnections(); |
| 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 | // The response will look like; |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 11050 | // lookup uvw (TLS 1.3's compatibility session ID) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11051 | // insert abc |
| 11052 | // lookup abc |
| 11053 | // insert xyz |
| 11054 | // |
| 11055 | // With a newline at the end which makes the split think that there are |
| 11056 | // four lines. |
| 11057 | |
| 11058 | EXPECT_EQ(1, d.response_started_count()); |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 11059 | std::vector<std::string> lines = base::SplitString( |
| 11060 | d.data_received(), "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 11061 | ASSERT_EQ(5u, lines.size()) << d.data_received(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11062 | |
| 11063 | std::string session_id; |
| 11064 | |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 11065 | for (size_t i = 0; i < 3; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 11066 | std::vector<std::string> parts = base::SplitString( |
| 11067 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11068 | ASSERT_EQ(2u, parts.size()); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 11069 | if (i % 2 == 1) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11070 | EXPECT_EQ("insert", parts[0]); |
| 11071 | session_id = parts[1]; |
| 11072 | } else { |
| 11073 | EXPECT_EQ("lookup", parts[0]); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 11074 | if (i != 0) |
| 11075 | EXPECT_EQ(session_id, parts[1]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11076 | } |
| 11077 | } |
| 11078 | } |
| 11079 | } |
| 11080 | |
| 11081 | TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { |
| 11082 | // Test that sessions aren't resumed when the value of ssl_session_cache_shard |
| 11083 | // differs. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11084 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11085 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11086 | SpawnedTestServer test_server( |
| 11087 | SpawnedTestServer::TYPE_HTTPS, |
| 11088 | ssl_options, |
| 11089 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11090 | ASSERT_TRUE(test_server.Start()); |
| 11091 | |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 11092 | default_context_.http_transaction_factory() |
| 11093 | ->GetSession() |
| 11094 | ->ClearSSLSessionCache(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11095 | |
| 11096 | { |
| 11097 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11098 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 11099 | test_server.GetURL("/"), DEFAULT_PRIORITY, &d, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11100 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11101 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11102 | r->Start(); |
| 11103 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11104 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11105 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11106 | |
| 11107 | EXPECT_EQ(1, d.response_started_count()); |
| 11108 | } |
| 11109 | |
| 11110 | // Now create a new HttpCache with a different ssl_session_cache_shard value. |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 11111 | HttpNetworkSession::Context session_context; |
| 11112 | session_context.host_resolver = default_context_.host_resolver(); |
| 11113 | session_context.cert_verifier = default_context_.cert_verifier(); |
| 11114 | session_context.transport_security_state = |
| 11115 | default_context_.transport_security_state(); |
| 11116 | session_context.cert_transparency_verifier = |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 11117 | default_context_.cert_transparency_verifier(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 11118 | session_context.ct_policy_enforcer = default_context_.ct_policy_enforcer(); |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 11119 | session_context.proxy_resolution_service = default_context_.proxy_resolution_service(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 11120 | session_context.ssl_config_service = default_context_.ssl_config_service(); |
| 11121 | session_context.http_auth_handler_factory = |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11122 | default_context_.http_auth_handler_factory(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 11123 | session_context.http_server_properties = |
| 11124 | default_context_.http_server_properties(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11125 | |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 11126 | HttpNetworkSession network_session(HttpNetworkSession::Params(), |
| 11127 | session_context); |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 11128 | std::unique_ptr<HttpCache> cache( |
| 11129 | new HttpCache(&network_session, HttpCache::DefaultBackend::InMemory(0), |
| 11130 | false /* is_main_cache */)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11131 | |
| 11132 | default_context_.set_http_transaction_factory(cache.get()); |
| 11133 | |
| 11134 | { |
| 11135 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11136 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11137 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 11138 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11139 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11140 | r->Start(); |
| 11141 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11142 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11143 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11144 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11145 | EXPECT_EQ(1, d.response_started_count()); |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 11146 | EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, r->ssl_info().handshake_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11147 | } |
| 11148 | } |
| 11149 | |
Daniel McArdle | da3fa94 | 2019-02-15 16:41:21 | [diff] [blame] | 11150 | // Test that sessions started with privacy mode enabled cannot be resumed when |
| 11151 | // it is disabled, and vice versa. |
| 11152 | TEST_F(HTTPSRequestTest, NoSessionResumptionBetweenPrivacyModes) { |
| 11153 | // Start a server. |
| 11154 | SpawnedTestServer test_server( |
| 11155 | SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::SSLOptions(), |
| 11156 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 11157 | ASSERT_TRUE(test_server.Start()); |
| 11158 | const auto url = test_server.GetURL("/"); |
| 11159 | |
| 11160 | auto ConnectAndCheckHandshake = [this, url](bool allow_credentials, |
| 11161 | auto expected_handshake) { |
| 11162 | // Construct request and indirectly set the privacy mode. |
| 11163 | TestDelegate d; |
| 11164 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 11165 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11166 | r->set_allow_credentials(allow_credentials); |
| 11167 | |
| 11168 | // Start the request and check the SSL handshake type. |
| 11169 | r->Start(); |
| 11170 | EXPECT_TRUE(r->is_pending()); |
| 11171 | d.RunUntilComplete(); |
| 11172 | EXPECT_EQ(1, d.response_started_count()); |
| 11173 | EXPECT_EQ(expected_handshake, r->ssl_info().handshake_type); |
| 11174 | }; |
| 11175 | |
| 11176 | // Exhaustively check all pairs of privacy mode values. Note that we are using |
| 11177 | // allow_credentials to indirectly enable/disable privacy mode. |
| 11178 | const bool kAllowCredentialsValues[] = {false, true}; |
| 11179 | for (const auto allow_creds_1 : kAllowCredentialsValues) { |
| 11180 | for (const auto allow_creds_2 : kAllowCredentialsValues) { |
| 11181 | SCOPED_TRACE(base::StringPrintf("allow_creds_1=%d, allow_creds_2=%d", |
| 11182 | allow_creds_1, allow_creds_2)); |
| 11183 | |
| 11184 | // The session cache starts off empty, so we expect a full handshake. |
| 11185 | ConnectAndCheckHandshake(allow_creds_1, SSLInfo::HANDSHAKE_FULL); |
| 11186 | |
| 11187 | // The second handshake depends on whether we are using the same session |
| 11188 | // cache as the first request. |
| 11189 | ConnectAndCheckHandshake(allow_creds_2, allow_creds_1 == allow_creds_2 |
| 11190 | ? SSLInfo::HANDSHAKE_RESUME |
| 11191 | : SSLInfo::HANDSHAKE_FULL); |
| 11192 | // Flush both session caches. |
| 11193 | auto* network_session = |
| 11194 | default_context_.http_transaction_factory()->GetSession(); |
| 11195 | network_session->ClearSSLSessionCache(); |
| 11196 | } |
| 11197 | } |
| 11198 | } |
| 11199 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 11200 | class HTTPSFallbackTest : public TestWithScopedTaskEnvironment { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11201 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 11202 | HTTPSFallbackTest() : context_(true) { |
David Benjamin | 151ec6b | 2019-08-02 19:38:52 | [diff] [blame^] | 11203 | ssl_config_service_ = |
| 11204 | std::make_unique<TestSSLConfigService>(SSLContextConfig()); |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 11205 | context_.set_ssl_config_service(ssl_config_service_.get()); |
| 11206 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 11207 | ~HTTPSFallbackTest() override = default; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11208 | |
| 11209 | protected: |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 11210 | TestSSLConfigService* ssl_config_service() { |
| 11211 | return ssl_config_service_.get(); |
| 11212 | } |
| 11213 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11214 | void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) { |
| 11215 | DCHECK(!request_); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 11216 | context_.Init(); |
| 11217 | delegate_.set_allow_certificate_errors(true); |
| 11218 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11219 | SpawnedTestServer test_server( |
| 11220 | SpawnedTestServer::TYPE_HTTPS, |
| 11221 | ssl_options, |
| 11222 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 11223 | ASSERT_TRUE(test_server.Start()); |
| 11224 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11225 | request_ = context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11226 | &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11227 | request_->Start(); |
| 11228 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11229 | delegate_.RunUntilComplete(); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11230 | } |
| 11231 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 11232 | void ExpectConnection(int version) { |
| 11233 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 11234 | EXPECT_NE(0, delegate_.bytes_received()); |
| 11235 | EXPECT_EQ(version, SSLConnectionStatusToVersion( |
| 11236 | request_->ssl_info().connection_status)); |
| 11237 | } |
| 11238 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11239 | void ExpectFailure(int error) { |
| 11240 | EXPECT_EQ(1, delegate_.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 11241 | EXPECT_EQ(error, delegate_.request_status()); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11242 | } |
| 11243 | |
| 11244 | private: |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11245 | std::unique_ptr<TestSSLConfigService> ssl_config_service_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11246 | TestDelegate delegate_; |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 11247 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11248 | std::unique_ptr<URLRequest> request_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11249 | }; |
| 11250 | |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 11251 | // Tests the TLS 1.0 fallback doesn't happen. |
| 11252 | TEST_F(HTTPSFallbackTest, TLSv1NoFallback) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11253 | SpawnedTestServer::SSLOptions ssl_options( |
| 11254 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 11255 | ssl_options.tls_intolerant = |
| 11256 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 11257 | |
| 11258 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 11259 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 11260 | } |
| 11261 | |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 11262 | // Tests the TLS 1.1 fallback doesn't happen. |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 11263 | TEST_F(HTTPSFallbackTest, TLSv1_1NoFallback) { |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 11264 | SpawnedTestServer::SSLOptions ssl_options( |
| 11265 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 11266 | ssl_options.tls_intolerant = |
| 11267 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2; |
| 11268 | |
| 11269 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11270 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
| 11271 | } |
| 11272 | |
David Benjamin | d61bd53 | 2019-04-23 21:11:37 | [diff] [blame] | 11273 | // Tests the TLS 1.2 fallback doesn't happen. |
| 11274 | TEST_F(HTTPSFallbackTest, TLSv1_2NoFallback) { |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 11275 | SpawnedTestServer::SSLOptions ssl_options( |
| 11276 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 11277 | ssl_options.tls_intolerant = |
| 11278 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_3; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 11279 | |
| 11280 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
David Benjamin | d61bd53 | 2019-04-23 21:11:37 | [diff] [blame] | 11281 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 11282 | } |
| 11283 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 11284 | class HTTPSSessionTest : public TestWithScopedTaskEnvironment { |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11285 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 11286 | HTTPSSessionTest() : default_context_(true) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 11287 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11288 | |
| 11289 | default_context_.set_network_delegate(&default_network_delegate_); |
| 11290 | default_context_.set_cert_verifier(&cert_verifier_); |
| 11291 | default_context_.Init(); |
| 11292 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 11293 | ~HTTPSSessionTest() override = default; |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11294 | |
| 11295 | protected: |
| 11296 | MockCertVerifier cert_verifier_; |
| 11297 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 11298 | TestURLRequestContext default_context_; |
| 11299 | }; |
| 11300 | |
| 11301 | // Tests that session resumption is not attempted if an invalid certificate |
| 11302 | // is presented. |
| 11303 | TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) { |
| 11304 | SpawnedTestServer::SSLOptions ssl_options; |
| 11305 | ssl_options.record_resume = true; |
| 11306 | SpawnedTestServer test_server( |
| 11307 | SpawnedTestServer::TYPE_HTTPS, |
| 11308 | ssl_options, |
| 11309 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 11310 | ASSERT_TRUE(test_server.Start()); |
| 11311 | |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 11312 | default_context_.http_transaction_factory() |
| 11313 | ->GetSession() |
| 11314 | ->ClearSSLSessionCache(); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11315 | |
| 11316 | // Simulate the certificate being expired and attempt a connection. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 11317 | cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11318 | { |
| 11319 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11320 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 11321 | test_server.GetURL("/"), DEFAULT_PRIORITY, &d, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11322 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11323 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11324 | r->Start(); |
| 11325 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11326 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11327 | d.RunUntilComplete(); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11328 | |
| 11329 | EXPECT_EQ(1, d.response_started_count()); |
| 11330 | } |
| 11331 | |
| 11332 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 11333 | CloseAllConnections(); |
| 11334 | |
| 11335 | // Now change the certificate to be acceptable (so that the response is |
| 11336 | // loaded), and ensure that no session id is presented to the peer. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 11337 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11338 | { |
| 11339 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11340 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11341 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 11342 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11343 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11344 | r->Start(); |
| 11345 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11346 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11347 | d.RunUntilComplete(); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11348 | |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11349 | EXPECT_EQ(1, d.response_started_count()); |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 11350 | EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, r->ssl_info().handshake_type); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11351 | } |
| 11352 | } |
| 11353 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11354 | // This the fingerprint of the "Testing CA" certificate used by the testserver. |
| 11355 | // See net/data/ssl/certificates/ocsp-test-root.pem. |
David Benjamin | 9cedc3a5 | 2017-08-20 21:30:58 | [diff] [blame] | 11356 | static const SHA256HashValue kOCSPTestCertFingerprint = {{ |
| 11357 | 0x0c, 0xa9, 0x05, 0x11, 0xb0, 0xa2, 0xc0, 0x1d, 0x40, 0x6a, 0x99, |
| 11358 | 0x04, 0x21, 0x36, 0x45, 0x3f, 0x59, 0x12, 0x5c, 0x80, 0x64, 0x2d, |
| 11359 | 0x46, 0x6a, 0x3b, 0x78, 0x9e, 0x84, 0xea, 0x54, 0x0f, 0x8b, |
mattm | 3b4376f1 | 2016-10-03 21:07:15 | [diff] [blame] | 11360 | }}; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11361 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11362 | // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the |
| 11363 | // testserver. |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11364 | static const SHA256HashValue kOCSPTestCertSPKI = {{ |
| 11365 | 0x05, 0xa8, 0xf6, 0xfd, 0x8e, 0x10, 0xfe, 0x92, 0x2f, 0x22, 0x75, |
| 11366 | 0x46, 0x40, 0xf4, 0xc4, 0x57, 0x06, 0x0d, 0x95, 0xfd, 0x60, 0x31, |
| 11367 | 0x3b, 0xf3, 0xfc, 0x12, 0x47, 0xe7, 0x66, 0x1a, 0x82, 0xa3, |
| 11368 | }}; |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11369 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11370 | // This is the policy OID contained in the certificates that testserver |
| 11371 | // generates. |
| 11372 | static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1"; |
| 11373 | |
| 11374 | class HTTPSOCSPTest : public HTTPSRequestTest { |
| 11375 | public: |
| 11376 | HTTPSOCSPTest() |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 11377 | : context_(true), |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11378 | ev_test_policy_( |
| 11379 | new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(), |
| 11380 | kOCSPTestCertFingerprint, |
| 11381 | kOCSPTestCertPolicy)) { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11382 | } |
| 11383 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 11384 | void SetUp() override { |
Matt Mueller | a4aaa9d | 2019-04-24 21:09:14 | [diff] [blame] | 11385 | cert_net_fetcher_ = base::MakeRefCounted<CertNetFetcherImpl>(); |
| 11386 | cert_verifier_ = CertVerifier::CreateDefault(cert_net_fetcher_); |
| 11387 | context_.set_cert_verifier(cert_verifier_.get()); |
Ryan Sleevi | 8a9c9c1 | 2018-05-09 02:36:23 | [diff] [blame] | 11388 | context_.SetCTPolicyEnforcer(std::make_unique<DefaultCTPolicyEnforcer>()); |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 11389 | context_.Init(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11390 | |
Matt Mueller | a4aaa9d | 2019-04-24 21:09:14 | [diff] [blame] | 11391 | cert_net_fetcher_->SetURLRequestContext(&context_); |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11392 | context_.cert_verifier()->SetConfig(GetCertVerifierConfig()); |
| 11393 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 11394 | scoped_refptr<X509Certificate> root_cert = |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11395 | ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
Matt Mueller | 09c8fd9a | 2019-05-04 00:37:24 | [diff] [blame] | 11396 | ASSERT_TRUE(root_cert); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 11397 | test_root_.reset(new ScopedTestRoot(root_cert.get())); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11398 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 11399 | #if defined(USE_NSS_CERTS) |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 11400 | SetURLRequestContextForNSSHttpIO(&context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11401 | #endif |
| 11402 | } |
| 11403 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11404 | void DoConnectionWithDelegate( |
| 11405 | const SpawnedTestServer::SSLOptions& ssl_options, |
| 11406 | TestDelegate* delegate, |
| 11407 | SSLInfo* out_ssl_info) { |
| 11408 | // Always overwrite |out_ssl_info|. |
| 11409 | out_ssl_info->Reset(); |
| 11410 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11411 | SpawnedTestServer test_server( |
| 11412 | SpawnedTestServer::TYPE_HTTPS, |
| 11413 | ssl_options, |
| 11414 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11415 | ASSERT_TRUE(test_server.Start()); |
| 11416 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11417 | delegate->set_allow_certificate_errors(true); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11418 | std::unique_ptr<URLRequest> r( |
| 11419 | context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
| 11420 | delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11421 | r->Start(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11422 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11423 | delegate->RunUntilComplete(); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11424 | EXPECT_EQ(1, delegate->response_started_count()); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11425 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11426 | *out_ssl_info = r->ssl_info(); |
| 11427 | } |
| 11428 | |
| 11429 | void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options, |
| 11430 | CertStatus* out_cert_status) { |
| 11431 | // Always overwrite |out_cert_status|. |
| 11432 | *out_cert_status = 0; |
| 11433 | |
| 11434 | TestDelegate d; |
| 11435 | SSLInfo ssl_info; |
| 11436 | ASSERT_NO_FATAL_FAILURE( |
| 11437 | DoConnectionWithDelegate(ssl_options, &d, &ssl_info)); |
| 11438 | |
| 11439 | *out_cert_status = ssl_info.cert_status; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11440 | } |
| 11441 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 11442 | ~HTTPSOCSPTest() override { |
Matt Mueller | a4aaa9d | 2019-04-24 21:09:14 | [diff] [blame] | 11443 | cert_net_fetcher_->Shutdown(); |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 11444 | #if defined(USE_NSS_CERTS) |
Matt Mueller | a17ffaf | 2018-03-28 20:08:18 | [diff] [blame] | 11445 | SetURLRequestContextForNSSHttpIO(nullptr); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11446 | #endif |
| 11447 | } |
| 11448 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11449 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11450 | // GetCertVerifierConfig() configures the URLRequestContext that will be used |
| 11451 | // for making connections to the testserver. This can be overridden in test |
| 11452 | // subclasses for different behaviour. |
| 11453 | virtual CertVerifier::Config GetCertVerifierConfig() { |
| 11454 | CertVerifier::Config config; |
| 11455 | config.enable_rev_checking = true; |
| 11456 | return config; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11457 | } |
| 11458 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11459 | std::unique_ptr<ScopedTestRoot> test_root_; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11460 | std::unique_ptr<TestSSLConfigService> ssl_config_service_; |
Matt Mueller | a4aaa9d | 2019-04-24 21:09:14 | [diff] [blame] | 11461 | scoped_refptr<CertNetFetcherImpl> cert_net_fetcher_; |
| 11462 | std::unique_ptr<CertVerifier> cert_verifier_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 11463 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11464 | std::unique_ptr<ScopedTestEVPolicy> ev_test_policy_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11465 | }; |
| 11466 | |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 11467 | static bool UsingBuiltinCertVerifier() { |
| 11468 | #if defined(OS_FUCHSIA) |
| 11469 | return true; |
| 11470 | #elif BUILDFLAG(BUILTIN_CERT_VERIFIER_FEATURE_SUPPORTED) |
| 11471 | if (base::FeatureList::IsEnabled(features::kCertVerifierBuiltinFeature)) |
| 11472 | return true; |
| 11473 | #endif |
| 11474 | return false; |
| 11475 | } |
| 11476 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11477 | static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() { |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 11478 | if (UsingBuiltinCertVerifier()) |
| 11479 | return 0; |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11480 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11481 | // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't |
| 11482 | // have that ability on other platforms. |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11483 | // TODO(eroman): Should this also be the return value for |
| 11484 | // CertVerifyProcBuiltin? |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11485 | return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 11486 | #else |
| 11487 | return 0; |
| 11488 | #endif |
| 11489 | } |
| 11490 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11491 | // SystemSupportsHardFailRevocationChecking returns true iff the current |
| 11492 | // operating system supports revocation checking and can distinguish between |
| 11493 | // situations where a given certificate lacks any revocation information (eg: |
| 11494 | // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when |
| 11495 | // revocation information cannot be obtained (eg: the CRL was unreachable). |
| 11496 | // If it does not, then tests which rely on 'hard fail' behaviour should be |
| 11497 | // skipped. |
| 11498 | static bool SystemSupportsHardFailRevocationChecking() { |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 11499 | if (UsingBuiltinCertVerifier()) |
| 11500 | return true; |
| 11501 | #if defined(OS_WIN) || defined(USE_NSS_CERTS) |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11502 | return true; |
| 11503 | #else |
| 11504 | return false; |
| 11505 | #endif |
| 11506 | } |
| 11507 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11508 | // SystemUsesChromiumEVMetadata returns true iff the current operating system |
| 11509 | // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then |
| 11510 | // several tests are effected because our testing EV certificate won't be |
| 11511 | // recognised as EV. |
| 11512 | static bool SystemUsesChromiumEVMetadata() { |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 11513 | if (UsingBuiltinCertVerifier()) |
| 11514 | return true; |
Eric Roman | 5f8d9d2 | 2017-10-17 02:32:52 | [diff] [blame] | 11515 | #if defined(PLATFORM_USES_CHROMIUM_EV_METADATA) |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 11516 | return true; |
Eric Roman | 5f8d9d2 | 2017-10-17 02:32:52 | [diff] [blame] | 11517 | #else |
| 11518 | return false; |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 11519 | #endif |
| 11520 | } |
| 11521 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11522 | // Returns the expected CertStatus for tests that expect an online revocation |
| 11523 | // check failure as a result of checking a test EV cert, which will not |
| 11524 | // actually trigger an online revocation check on some platforms. |
| 11525 | static CertStatus ExpectedCertStatusForFailedOnlineEVRevocationCheck() { |
| 11526 | if (SystemUsesChromiumEVMetadata()) { |
| 11527 | return ExpectedCertStatusForFailedOnlineRevocationCheck(); |
| 11528 | } else { |
| 11529 | // If SystemUsesChromiumEVMetadata is false, revocation checking will not |
| 11530 | // be enabled, and thus there will not be a revocation check to fail. |
| 11531 | return 0u; |
| 11532 | } |
| 11533 | } |
| 11534 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11535 | static bool SystemSupportsOCSP() { |
Sergey Ulanov | ee7c8db | 2017-12-08 00:18:39 | [diff] [blame] | 11536 | #if defined(OS_ANDROID) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11537 | // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. |
| 11538 | return false; |
| 11539 | #else |
| 11540 | return true; |
| 11541 | #endif |
| 11542 | } |
| 11543 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11544 | static bool SystemSupportsOCSPStapling() { |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 11545 | if (UsingBuiltinCertVerifier()) |
| 11546 | return true; |
Matt Mueller | 7f60dc4 | 2019-04-26 22:47:10 | [diff] [blame] | 11547 | #if defined(OS_ANDROID) |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11548 | return false; |
Matt Mueller | 7f60dc4 | 2019-04-26 22:47:10 | [diff] [blame] | 11549 | #elif defined(OS_MACOSX) |
| 11550 | // The SecTrustSetOCSPResponse function exists since macOS 10.9+, but does |
| 11551 | // not actually do anything until 10.12. |
| 11552 | if (base::mac::IsAtLeastOS10_12()) |
| 11553 | return true; |
| 11554 | return false; |
| 11555 | #else |
| 11556 | return true; |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11557 | #endif |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11558 | } |
| 11559 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11560 | TEST_F(HTTPSOCSPTest, Valid) { |
| 11561 | if (!SystemSupportsOCSP()) { |
| 11562 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11563 | return; |
| 11564 | } |
| 11565 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11566 | SpawnedTestServer::SSLOptions ssl_options( |
| 11567 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11568 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11569 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11570 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11571 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11572 | |
| 11573 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11574 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11575 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11576 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11577 | |
| 11578 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11579 | } |
| 11580 | |
| 11581 | TEST_F(HTTPSOCSPTest, Revoked) { |
| 11582 | if (!SystemSupportsOCSP()) { |
| 11583 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11584 | return; |
| 11585 | } |
| 11586 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11587 | SpawnedTestServer::SSLOptions ssl_options( |
| 11588 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11589 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11590 | |
| 11591 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11592 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11593 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11594 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11595 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11596 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11597 | } |
| 11598 | |
| 11599 | TEST_F(HTTPSOCSPTest, Invalid) { |
| 11600 | if (!SystemSupportsOCSP()) { |
| 11601 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11602 | return; |
| 11603 | } |
| 11604 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11605 | SpawnedTestServer::SSLOptions ssl_options( |
| 11606 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11607 | ssl_options.ocsp_status = |
| 11608 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11609 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11610 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11611 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11612 | |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 11613 | if (UsingBuiltinCertVerifier()) { |
| 11614 | // TODO(649017): This test uses soft-fail revocation checking, but returns |
| 11615 | // an invalid OCSP response (can't parse). CertVerifyProcBuiltin currently |
| 11616 | // doesn't consider this a candidate for soft-fail (only considers |
| 11617 | // network-level failures as skippable). |
| 11618 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 11619 | cert_status & CERT_STATUS_UNABLE_TO_CHECK_REVOCATION); |
| 11620 | } else { |
| 11621 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 11622 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11623 | } |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11624 | |
| 11625 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 11626 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11627 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11628 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11629 | |
Matt Mueller | 5339d86 | 2018-05-02 18:53:57 | [diff] [blame] | 11630 | TEST_F(HTTPSOCSPTest, IntermediateValid) { |
| 11631 | if (!SystemSupportsOCSP()) { |
| 11632 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11633 | return; |
| 11634 | } |
| 11635 | |
| 11636 | SpawnedTestServer::SSLOptions ssl_options( |
| 11637 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11638 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11639 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11640 | |
| 11641 | CertStatus cert_status; |
| 11642 | DoConnection(ssl_options, &cert_status); |
| 11643 | |
| 11644 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11645 | |
| 11646 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11647 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11648 | |
| 11649 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11650 | } |
| 11651 | |
| 11652 | TEST_F(HTTPSOCSPTest, IntermediateResponseOldButStillValid) { |
| 11653 | if (!SystemSupportsOCSP()) { |
| 11654 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11655 | return; |
| 11656 | } |
| 11657 | |
| 11658 | SpawnedTestServer::SSLOptions ssl_options( |
| 11659 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11660 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11661 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11662 | // Use an OCSP response for the intermediate that would be too old for a leaf |
| 11663 | // cert, but is still valid for an intermediate. |
| 11664 | ssl_options.ocsp_intermediate_date = |
| 11665 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG; |
| 11666 | |
| 11667 | CertStatus cert_status; |
| 11668 | DoConnection(ssl_options, &cert_status); |
| 11669 | |
| 11670 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11671 | |
| 11672 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11673 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11674 | |
| 11675 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11676 | } |
| 11677 | |
| 11678 | TEST_F(HTTPSOCSPTest, IntermediateResponseTooOld) { |
| 11679 | if (!SystemSupportsOCSP()) { |
| 11680 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11681 | return; |
| 11682 | } |
| 11683 | |
| 11684 | SpawnedTestServer::SSLOptions ssl_options( |
| 11685 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11686 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11687 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11688 | ssl_options.ocsp_intermediate_date = |
| 11689 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONGER; |
| 11690 | |
| 11691 | CertStatus cert_status; |
| 11692 | DoConnection(ssl_options, &cert_status); |
| 11693 | |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 11694 | if (UsingBuiltinCertVerifier()) { |
| 11695 | // The builtin verifier enforces the baseline requirements for max age of an |
| 11696 | // intermediate's OCSP response. |
| 11697 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 11698 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11699 | EXPECT_EQ(0u, cert_status & CERT_STATUS_IS_EV); |
| 11700 | } else { |
| 11701 | // The platform verifiers are more lenient. |
| 11702 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11703 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11704 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11705 | } |
Matt Mueller | 5339d86 | 2018-05-02 18:53:57 | [diff] [blame] | 11706 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11707 | } |
| 11708 | |
| 11709 | TEST_F(HTTPSOCSPTest, IntermediateRevoked) { |
| 11710 | if (!SystemSupportsOCSP()) { |
| 11711 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11712 | return; |
| 11713 | } |
| 11714 | |
| 11715 | SpawnedTestServer::SSLOptions ssl_options( |
| 11716 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11717 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11718 | ssl_options.ocsp_intermediate_status = |
| 11719 | SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 11720 | |
| 11721 | CertStatus cert_status; |
| 11722 | DoConnection(ssl_options, &cert_status); |
| 11723 | |
| 11724 | #if defined(OS_WIN) |
| 11725 | // TODO(mattm): why does CertVerifyProcWin accept this? |
| 11726 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11727 | #else |
| 11728 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11729 | #endif |
| 11730 | EXPECT_EQ(0u, cert_status & CERT_STATUS_IS_EV); |
| 11731 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11732 | } |
| 11733 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11734 | TEST_F(HTTPSOCSPTest, ValidStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11735 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11736 | LOG(WARNING) |
| 11737 | << "Skipping test because system doesn't support OCSP stapling"; |
| 11738 | return; |
| 11739 | } |
| 11740 | |
| 11741 | SpawnedTestServer::SSLOptions ssl_options( |
| 11742 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11743 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11744 | ssl_options.staple_ocsp_response = true; |
| 11745 | ssl_options.ocsp_server_unavailable = true; |
| 11746 | |
| 11747 | CertStatus cert_status; |
| 11748 | DoConnection(ssl_options, &cert_status); |
| 11749 | |
| 11750 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11751 | |
| 11752 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11753 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11754 | |
| 11755 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11756 | } |
| 11757 | |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 11758 | // Disabled on NSS ports. See https://crbug.com/431716. |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 11759 | #if defined(USE_NSS_CERTS) |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 11760 | #define MAYBE_RevokedStapled DISABLED_RevokedStapled |
| 11761 | #else |
| 11762 | #define MAYBE_RevokedStapled RevokedStapled |
| 11763 | #endif |
| 11764 | TEST_F(HTTPSOCSPTest, MAYBE_RevokedStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11765 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11766 | LOG(WARNING) |
| 11767 | << "Skipping test because system doesn't support OCSP stapling"; |
| 11768 | return; |
| 11769 | } |
| 11770 | |
| 11771 | SpawnedTestServer::SSLOptions ssl_options( |
| 11772 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11773 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 11774 | ssl_options.staple_ocsp_response = true; |
| 11775 | ssl_options.ocsp_server_unavailable = true; |
| 11776 | |
| 11777 | CertStatus cert_status; |
| 11778 | DoConnection(ssl_options, &cert_status); |
| 11779 | |
| 11780 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11781 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11782 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11783 | } |
| 11784 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11785 | static const struct OCSPVerifyTestData { |
| 11786 | std::vector<SpawnedTestServer::SSLOptions::OCSPSingleResponse> ocsp_responses; |
| 11787 | SpawnedTestServer::SSLOptions::OCSPProduced ocsp_produced; |
| 11788 | OCSPVerifyResult::ResponseStatus response_status; |
| 11789 | bool has_revocation_status; |
| 11790 | OCSPRevocationStatus cert_status; |
| 11791 | } kOCSPVerifyData[] = { |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11792 | // 0 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11793 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11794 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11795 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11796 | OCSPVerifyResult::PROVIDED, |
| 11797 | true, |
| 11798 | OCSPRevocationStatus::GOOD}, |
| 11799 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11800 | // 1 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11801 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11802 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 11803 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11804 | OCSPVerifyResult::INVALID_DATE, |
| 11805 | false, |
| 11806 | OCSPRevocationStatus::UNKNOWN}, |
| 11807 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11808 | // 2 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11809 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11810 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11811 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11812 | OCSPVerifyResult::INVALID_DATE, |
| 11813 | false, |
| 11814 | OCSPRevocationStatus::UNKNOWN}, |
| 11815 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11816 | // 3 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11817 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11818 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11819 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11820 | OCSPVerifyResult::INVALID_DATE, |
| 11821 | false, |
| 11822 | OCSPRevocationStatus::UNKNOWN}, |
| 11823 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11824 | // 4 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11825 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11826 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11827 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11828 | OCSPVerifyResult::INVALID_DATE, |
| 11829 | false, |
| 11830 | OCSPRevocationStatus::UNKNOWN}, |
| 11831 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11832 | // 5 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11833 | {{{SpawnedTestServer::SSLOptions::OCSP_TRY_LATER, |
| 11834 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11835 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11836 | OCSPVerifyResult::ERROR_RESPONSE, |
| 11837 | false, |
| 11838 | OCSPRevocationStatus::UNKNOWN}, |
| 11839 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11840 | // 6 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11841 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE, |
| 11842 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11843 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11844 | OCSPVerifyResult::PARSE_RESPONSE_ERROR, |
| 11845 | false, |
| 11846 | OCSPRevocationStatus::UNKNOWN}, |
| 11847 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11848 | // 7 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11849 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE_DATA, |
| 11850 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11851 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11852 | OCSPVerifyResult::PARSE_RESPONSE_DATA_ERROR, |
| 11853 | false, |
| 11854 | OCSPRevocationStatus::UNKNOWN}, |
| 11855 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11856 | // 8 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11857 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11858 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11859 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11860 | OCSPVerifyResult::INVALID_DATE, |
| 11861 | false, |
| 11862 | OCSPRevocationStatus::UNKNOWN}, |
| 11863 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11864 | // 9 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11865 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11866 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11867 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11868 | OCSPVerifyResult::PROVIDED, |
| 11869 | true, |
| 11870 | OCSPRevocationStatus::UNKNOWN}, |
| 11871 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11872 | // 10 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11873 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11874 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 11875 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11876 | OCSPVerifyResult::INVALID_DATE, |
| 11877 | false, |
| 11878 | OCSPRevocationStatus::UNKNOWN}, |
| 11879 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11880 | // 11 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11881 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11882 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11883 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11884 | OCSPVerifyResult::INVALID_DATE, |
| 11885 | false, |
| 11886 | OCSPRevocationStatus::UNKNOWN}, |
| 11887 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11888 | // 12 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11889 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11890 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11891 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_BEFORE_CERT, |
| 11892 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11893 | false, |
| 11894 | OCSPRevocationStatus::UNKNOWN}, |
| 11895 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11896 | // 13 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11897 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11898 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11899 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 11900 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11901 | false, |
| 11902 | OCSPRevocationStatus::UNKNOWN}, |
| 11903 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11904 | // 14 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11905 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11906 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11907 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 11908 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11909 | false, |
| 11910 | OCSPRevocationStatus::UNKNOWN}, |
| 11911 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11912 | // 15 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11913 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11914 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11915 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11916 | OCSPVerifyResult::PROVIDED, |
| 11917 | true, |
| 11918 | OCSPRevocationStatus::GOOD}, |
| 11919 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11920 | // 16 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11921 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11922 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 11923 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11924 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11925 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11926 | OCSPVerifyResult::PROVIDED, |
| 11927 | true, |
| 11928 | OCSPRevocationStatus::GOOD}, |
| 11929 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11930 | // 17 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11931 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11932 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 11933 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11934 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11935 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11936 | OCSPVerifyResult::PROVIDED, |
| 11937 | true, |
| 11938 | OCSPRevocationStatus::GOOD}, |
| 11939 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11940 | // 18 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11941 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11942 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 11943 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11944 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11945 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11946 | OCSPVerifyResult::PROVIDED, |
| 11947 | true, |
| 11948 | OCSPRevocationStatus::GOOD}, |
| 11949 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11950 | // 19 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11951 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11952 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 11953 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11954 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 11955 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11956 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11957 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11958 | OCSPVerifyResult::INVALID_DATE, |
| 11959 | false, |
| 11960 | OCSPRevocationStatus::UNKNOWN}, |
| 11961 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11962 | // 20 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11963 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11964 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11965 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11966 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11967 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11968 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11969 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11970 | OCSPVerifyResult::PROVIDED, |
| 11971 | true, |
| 11972 | OCSPRevocationStatus::REVOKED}, |
| 11973 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11974 | // 21 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11975 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11976 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11977 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11978 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11979 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11980 | OCSPVerifyResult::PROVIDED, |
| 11981 | true, |
| 11982 | OCSPRevocationStatus::UNKNOWN}, |
| 11983 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11984 | // 22 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11985 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11986 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11987 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11988 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 11989 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11990 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11991 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11992 | OCSPVerifyResult::PROVIDED, |
| 11993 | true, |
| 11994 | OCSPRevocationStatus::UNKNOWN}, |
| 11995 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11996 | // 23 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11997 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 11998 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11999 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 12000 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 12001 | false, |
| 12002 | OCSPRevocationStatus::UNKNOWN}, |
| 12003 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 12004 | // 24 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12005 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 12006 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 12007 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 12008 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 12009 | false, |
| 12010 | OCSPRevocationStatus::UNKNOWN}, |
| 12011 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 12012 | // These tests fail when using NSS for certificate verification, as NSS fails |
| 12013 | // and doesn't return the partial path. As a result the OCSP checks being done |
| 12014 | // at the CertVerifyProc layer cannot access the issuer certificate. |
| 12015 | #if !defined(USE_NSS_CERTS) |
| 12016 | // 25 |
| 12017 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 12018 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 12019 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 12020 | OCSPVerifyResult::PROVIDED, |
| 12021 | true, |
| 12022 | OCSPRevocationStatus::REVOKED}, |
| 12023 | |
| 12024 | // 26 |
| 12025 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 12026 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 12027 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 12028 | OCSPVerifyResult::INVALID_DATE, |
| 12029 | false, |
| 12030 | OCSPRevocationStatus::UNKNOWN}, |
| 12031 | |
| 12032 | // 27 |
| 12033 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 12034 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 12035 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 12036 | OCSPVerifyResult::INVALID_DATE, |
| 12037 | false, |
| 12038 | OCSPRevocationStatus::UNKNOWN}, |
| 12039 | #endif |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12040 | }; |
| 12041 | |
| 12042 | class HTTPSOCSPVerifyTest |
| 12043 | : public HTTPSOCSPTest, |
| 12044 | public testing::WithParamInterface<OCSPVerifyTestData> {}; |
| 12045 | |
Christos Froussios | 09e6ac4 | 2019-04-05 14:08:20 | [diff] [blame] | 12046 | // TODO(crbug.com/949958): The test is flaky on Mac |
| 12047 | #if defined(OS_MACOSX) |
| 12048 | #define MAYBE_VerifyResult DISABLED_VerifyResult |
| 12049 | #else |
| 12050 | #define MAYBE_VerifyResult VerifyResult |
| 12051 | #endif |
| 12052 | TEST_P(HTTPSOCSPVerifyTest, MAYBE_VerifyResult) { |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12053 | SpawnedTestServer::SSLOptions ssl_options( |
| 12054 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12055 | OCSPVerifyTestData test = GetParam(); |
| 12056 | |
| 12057 | ssl_options.ocsp_responses = test.ocsp_responses; |
| 12058 | ssl_options.ocsp_produced = test.ocsp_produced; |
| 12059 | ssl_options.staple_ocsp_response = true; |
| 12060 | |
| 12061 | SSLInfo ssl_info; |
| 12062 | OCSPErrorTestDelegate delegate; |
| 12063 | ASSERT_NO_FATAL_FAILURE( |
| 12064 | DoConnectionWithDelegate(ssl_options, &delegate, &ssl_info)); |
| 12065 | |
| 12066 | // The SSLInfo must be extracted from |delegate| on error, due to how |
| 12067 | // URLRequest caches certificate errors. |
| 12068 | if (delegate.have_certificate_errors()) { |
| 12069 | ASSERT_TRUE(delegate.on_ssl_certificate_error_called()); |
| 12070 | ssl_info = delegate.ssl_info(); |
| 12071 | } |
| 12072 | |
| 12073 | EXPECT_EQ(test.response_status, ssl_info.ocsp_result.response_status); |
| 12074 | |
| 12075 | if (test.has_revocation_status) |
| 12076 | EXPECT_EQ(test.cert_status, ssl_info.ocsp_result.revocation_status); |
| 12077 | } |
| 12078 | |
Victor Costan | 8fb98f6f | 2019-02-01 17:08:29 | [diff] [blame] | 12079 | INSTANTIATE_TEST_SUITE_P(OCSPVerify, |
| 12080 | HTTPSOCSPVerifyTest, |
| 12081 | testing::ValuesIn(kOCSPVerifyData)); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12082 | |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 12083 | class HTTPSAIATest : public HTTPSOCSPTest { |
| 12084 | public: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 12085 | CertVerifier::Config GetCertVerifierConfig() override { |
| 12086 | CertVerifier::Config config; |
| 12087 | return config; |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 12088 | } |
| 12089 | }; |
| 12090 | |
| 12091 | TEST_F(HTTPSAIATest, AIAFetching) { |
| 12092 | SpawnedTestServer::SSLOptions ssl_options( |
| 12093 | SpawnedTestServer::SSLOptions::CERT_AUTO_AIA_INTERMEDIATE); |
| 12094 | SpawnedTestServer test_server( |
| 12095 | SpawnedTestServer::TYPE_HTTPS, ssl_options, |
| 12096 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 12097 | ASSERT_TRUE(test_server.Start()); |
| 12098 | |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12099 | // Unmark the certificate's OID as EV, which will disable revocation |
| 12100 | // checking. |
| 12101 | ev_test_policy_.reset(); |
| 12102 | |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 12103 | TestDelegate d; |
| 12104 | d.set_allow_certificate_errors(true); |
| 12105 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12106 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 12107 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 12108 | |
| 12109 | r->Start(); |
| 12110 | EXPECT_TRUE(r->is_pending()); |
| 12111 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12112 | d.RunUntilComplete(); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 12113 | |
| 12114 | EXPECT_EQ(1, d.response_started_count()); |
| 12115 | |
| 12116 | CertStatus cert_status = r->ssl_info().cert_status; |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 12117 | EXPECT_EQ(OK, d.request_status()); |
| 12118 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12119 | ASSERT_TRUE(r->ssl_info().cert); |
| 12120 | EXPECT_EQ(2u, r->ssl_info().cert->intermediate_buffers().size()); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 12121 | ASSERT_TRUE(r->ssl_info().unverified_cert); |
Matt Mueller | a419327 | 2017-12-07 00:23:34 | [diff] [blame] | 12122 | EXPECT_EQ(0u, r->ssl_info().unverified_cert->intermediate_buffers().size()); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 12123 | } |
| 12124 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 12125 | class HTTPSHardFailTest : public HTTPSOCSPTest { |
| 12126 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 12127 | CertVerifier::Config GetCertVerifierConfig() override { |
| 12128 | CertVerifier::Config config; |
| 12129 | config.require_rev_checking_local_anchors = true; |
| 12130 | return config; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 12131 | } |
| 12132 | }; |
| 12133 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 12134 | TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) { |
| 12135 | if (!SystemSupportsOCSP()) { |
| 12136 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12137 | return; |
| 12138 | } |
| 12139 | |
| 12140 | if (!SystemSupportsHardFailRevocationChecking()) { |
| 12141 | LOG(WARNING) << "Skipping test because system doesn't support hard fail " |
| 12142 | << "revocation checking"; |
| 12143 | return; |
| 12144 | } |
| 12145 | |
| 12146 | SpawnedTestServer::SSLOptions ssl_options( |
| 12147 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12148 | ssl_options.ocsp_status = |
| 12149 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 12150 | |
| 12151 | CertStatus cert_status; |
| 12152 | DoConnection(ssl_options, &cert_status); |
| 12153 | |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 12154 | if (UsingBuiltinCertVerifier()) { |
| 12155 | // TODO(crbug.com/649017): Should we consider invalid response as |
| 12156 | // affirmatively revoked? |
| 12157 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 12158 | cert_status & CERT_STATUS_UNABLE_TO_CHECK_REVOCATION); |
| 12159 | } else { |
| 12160 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_REVOKED); |
| 12161 | } |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 12162 | |
| 12163 | // Without a positive OCSP response, we shouldn't show the EV status. |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 12164 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 12165 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 12166 | } |
| 12167 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12168 | class HTTPSEVCRLSetTest : public HTTPSOCSPTest { |
| 12169 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 12170 | CertVerifier::Config GetCertVerifierConfig() override { |
| 12171 | CertVerifier::Config config; |
| 12172 | return config; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12173 | } |
| 12174 | }; |
| 12175 | |
| 12176 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) { |
| 12177 | if (!SystemSupportsOCSP()) { |
| 12178 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12179 | return; |
| 12180 | } |
| 12181 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 12182 | SpawnedTestServer::SSLOptions ssl_options( |
| 12183 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12184 | ssl_options.ocsp_status = |
| 12185 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12186 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 12187 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 12188 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12189 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 12190 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12191 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 12192 | |
| 12193 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 12194 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 12195 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12196 | } |
| 12197 | |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 12198 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) { |
| 12199 | if (!SystemSupportsOCSP()) { |
| 12200 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12201 | return; |
| 12202 | } |
| 12203 | |
| 12204 | SpawnedTestServer::SSLOptions ssl_options( |
| 12205 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12206 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 12207 | |
| 12208 | CertStatus cert_status; |
| 12209 | DoConnection(ssl_options, &cert_status); |
| 12210 | |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 12211 | // Currently only works for Windows and OS X. When using NSS, it's not |
| 12212 | // possible to determine whether the check failed because of actual |
| 12213 | // revocation or because there was an OCSP failure. |
| 12214 | if (UsingBuiltinCertVerifier()) { |
| 12215 | // TODO(https://crbug.com/410574): Handle this in builtin verifier too? |
| 12216 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12217 | } else { |
mattm | 1a282f5 | 2016-11-10 21:49:42 | [diff] [blame] | 12218 | #if defined(OS_WIN) || defined(OS_MACOSX) |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 12219 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 12220 | #else |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 12221 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 12222 | #endif |
Matt Mueller | 36c3b10 | 2019-06-18 02:28:20 | [diff] [blame] | 12223 | } |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 12224 | |
| 12225 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 12226 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 12227 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 12228 | } |
| 12229 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12230 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) { |
| 12231 | if (!SystemSupportsOCSP()) { |
| 12232 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12233 | return; |
| 12234 | } |
| 12235 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 12236 | SpawnedTestServer::SSLOptions ssl_options( |
| 12237 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12238 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12239 | |
| 12240 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 12241 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12242 | |
| 12243 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12244 | |
| 12245 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 12246 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 12247 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 12248 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12249 | } |
| 12250 | |
| 12251 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) { |
| 12252 | if (!SystemSupportsOCSP()) { |
| 12253 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12254 | return; |
| 12255 | } |
| 12256 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 12257 | SpawnedTestServer::SSLOptions ssl_options( |
| 12258 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12259 | ssl_options.ocsp_status = |
| 12260 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12261 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12262 | cert_verifier_config.crl_set = CRLSet::ExpiredCRLSetForTesting(); |
| 12263 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12264 | |
| 12265 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 12266 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12267 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 12268 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12269 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 12270 | |
| 12271 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 12272 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 12273 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12274 | } |
| 12275 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12276 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) { |
| 12277 | if (!SystemSupportsOCSP()) { |
| 12278 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12279 | return; |
| 12280 | } |
| 12281 | |
| 12282 | SpawnedTestServer::SSLOptions ssl_options( |
| 12283 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12284 | ssl_options.ocsp_status = |
| 12285 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12286 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12287 | cert_verifier_config.crl_set = |
| 12288 | CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "", "", {}); |
| 12289 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12290 | |
| 12291 | CertStatus cert_status; |
| 12292 | DoConnection(ssl_options, &cert_status); |
| 12293 | |
| 12294 | // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a |
| 12295 | // revocation check for EV. |
| 12296 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12297 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 12298 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 12299 | EXPECT_FALSE( |
| 12300 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 12301 | } |
| 12302 | |
| 12303 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) { |
| 12304 | if (!SystemSupportsOCSP()) { |
| 12305 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12306 | return; |
| 12307 | } |
| 12308 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 12309 | SpawnedTestServer::SSLOptions ssl_options( |
| 12310 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12311 | ssl_options.ocsp_status = |
| 12312 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12313 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12314 | cert_verifier_config.crl_set = CRLSet::EmptyCRLSetForTesting(); |
| 12315 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12316 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12317 | CertStatus cert_status = 0; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 12318 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12319 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12320 | // Even with a fresh CRLSet, we should still do online revocation checks when |
| 12321 | // the certificate chain isn't covered by the CRLSet, which it isn't in this |
| 12322 | // test. |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 12323 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12324 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12325 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12326 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12327 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12328 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12329 | } |
| 12330 | |
| 12331 | class HTTPSCRLSetTest : public HTTPSOCSPTest { |
| 12332 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 12333 | CertVerifier::Config GetCertVerifierConfig() override { |
| 12334 | CertVerifier::Config config; |
| 12335 | return config; |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12336 | } |
| 12337 | |
| 12338 | void SetUp() override { |
| 12339 | HTTPSOCSPTest::SetUp(); |
| 12340 | |
| 12341 | // Unmark the certificate's OID as EV, which should disable revocation |
| 12342 | // checking (as per the user preference). |
| 12343 | ev_test_policy_.reset(); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12344 | } |
| 12345 | }; |
| 12346 | |
| 12347 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 12348 | SpawnedTestServer::SSLOptions ssl_options( |
| 12349 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12350 | ssl_options.ocsp_status = |
| 12351 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12352 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12353 | cert_verifier_config.crl_set = CRLSet::ExpiredCRLSetForTesting(); |
| 12354 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12355 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 12356 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 12357 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12358 | |
| 12359 | // If we're not trying EV verification then, even if the CRLSet has expired, |
| 12360 | // we don't fall back to online revocation checks. |
| 12361 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12362 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 12363 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 12364 | } |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12365 | |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12366 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSetAndRevoked) { |
| 12367 | // Test that when online revocation checking is disabled, and the leaf |
| 12368 | // certificate is not EV, that no revocation checking actually happens. |
| 12369 | if (!SystemSupportsOCSP()) { |
| 12370 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12371 | return; |
| 12372 | } |
| 12373 | |
| 12374 | SpawnedTestServer::SSLOptions ssl_options( |
| 12375 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12376 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12377 | |
| 12378 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12379 | cert_verifier_config.crl_set = CRLSet::ExpiredCRLSetForTesting(); |
| 12380 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12381 | |
| 12382 | CertStatus cert_status; |
| 12383 | DoConnection(ssl_options, &cert_status); |
| 12384 | |
| 12385 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12386 | |
| 12387 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 12388 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 12389 | } |
| 12390 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12391 | TEST_F(HTTPSCRLSetTest, CRLSetRevoked) { |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 12392 | #if defined(OS_ANDROID) |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12393 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 12394 | return; |
| 12395 | #endif |
| 12396 | |
| 12397 | SpawnedTestServer::SSLOptions ssl_options( |
| 12398 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12399 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 12400 | ssl_options.cert_serial = 10; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12401 | |
| 12402 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12403 | cert_verifier_config.crl_set = |
| 12404 | CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "\x0a", "", {}); |
| 12405 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12406 | |
| 12407 | CertStatus cert_status = 0; |
| 12408 | DoConnection(ssl_options, &cert_status); |
| 12409 | |
| 12410 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 12411 | // reflected without online revocation checking. |
| 12412 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12413 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12414 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12415 | } |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 12416 | |
| 12417 | TEST_F(HTTPSCRLSetTest, CRLSetRevokedBySubject) { |
| 12418 | #if defined(OS_ANDROID) |
| 12419 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 12420 | return; |
| 12421 | #endif |
| 12422 | |
| 12423 | SpawnedTestServer::SSLOptions ssl_options( |
| 12424 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12425 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 12426 | static const char kCommonName[] = "Test CN"; |
| 12427 | ssl_options.cert_common_name = kCommonName; |
| 12428 | |
| 12429 | { |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12430 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12431 | cert_verifier_config.crl_set = |
| 12432 | CRLSet::ForTesting(false, nullptr, "", kCommonName, {}); |
| 12433 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 12434 | |
| 12435 | CertStatus cert_status = 0; |
| 12436 | DoConnection(ssl_options, &cert_status); |
| 12437 | |
| 12438 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 12439 | // reflected without online revocation checking. |
| 12440 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12441 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12442 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 12443 | } |
| 12444 | |
| 12445 | const uint8_t kTestServerSPKISHA256[32] = { |
| 12446 | 0xb3, 0x91, 0xac, 0x73, 0x32, 0x54, 0x7f, 0x7b, 0x8a, 0x62, 0x77, |
| 12447 | 0x73, 0x1d, 0x45, 0x7b, 0x23, 0x46, 0x69, 0xef, 0x6f, 0x05, 0x3d, |
| 12448 | 0x07, 0x22, 0x15, 0x18, 0xd6, 0x10, 0x8b, 0xa1, 0x49, 0x33, |
| 12449 | }; |
| 12450 | const std::string spki_hash( |
| 12451 | reinterpret_cast<const char*>(kTestServerSPKISHA256), |
| 12452 | sizeof(kTestServerSPKISHA256)); |
| 12453 | |
| 12454 | { |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12455 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12456 | cert_verifier_config.crl_set = |
| 12457 | CRLSet::ForTesting(false, nullptr, "", kCommonName, {spki_hash}); |
| 12458 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 12459 | |
| 12460 | CertStatus cert_status = 0; |
| 12461 | DoConnection(ssl_options, &cert_status); |
| 12462 | |
| 12463 | // When the correct SPKI hash is specified, the connection should succeed |
| 12464 | // even though the subject is listed in the CRLSet. |
| 12465 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12466 | } |
| 12467 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 12468 | #endif // !defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 12469 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 12470 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 12471 | !defined(OS_FUCHSIA) |
| 12472 | // FTP uses a second TCP connection with the port number allocated dynamically |
| 12473 | // on the server side, so it would be hard to make RemoteTestServer proxy FTP |
| 12474 | // connections reliably. FTP tests are disabled on platforms that use |
| 12475 | // RemoteTestServer. See http://crbug.com/495220 |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 12476 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12477 | public: |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 12478 | URLRequestTestFTP() |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 12479 | : ftp_test_server_(SpawnedTestServer::TYPE_FTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12480 | base::FilePath(kTestFilePath)) { |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12481 | // Can't use |default_context_|'s HostResolver to set up the |
| 12482 | // FTPTransactionFactory because it hasn't been created yet. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12483 | default_context().set_host_resolver(&host_resolver_); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12484 | } |
| 12485 | |
| 12486 | // URLRequestTest interface: |
| 12487 | void SetUpFactory() override { |
| 12488 | // Add FTP support to the default URLRequestContext. |
| 12489 | job_factory_impl_->SetProtocolHandler( |
Emily Stark | 0122798 | 2019-06-06 18:45:56 | [diff] [blame] | 12490 | "ftp", FtpProtocolHandler::Create(&host_resolver_, &ftp_auth_cache_)); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12491 | } |
| 12492 | |
| 12493 | std::string GetTestFileContents() { |
| 12494 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 12495 | EXPECT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &path)); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12496 | path = path.Append(kTestFilePath); |
| 12497 | path = path.AppendASCII(kFtpTestFile); |
| 12498 | std::string contents; |
| 12499 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
| 12500 | return contents; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12501 | } |
| 12502 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 12503 | protected: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12504 | MockHostResolver host_resolver_; |
Emily Stark | 0122798 | 2019-06-06 18:45:56 | [diff] [blame] | 12505 | FtpAuthCache ftp_auth_cache_; |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12506 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12507 | SpawnedTestServer ftp_test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 12508 | }; |
| 12509 | |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12510 | // Make sure an FTP request using an unsafe ports fails. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 12511 | TEST_F(URLRequestTestFTP, UnsafePort) { |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12512 | GURL url("ftp://127.0.0.1:7"); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12513 | |
| 12514 | TestDelegate d; |
| 12515 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12516 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12517 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12518 | r->Start(); |
| 12519 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12520 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12521 | d.RunUntilComplete(); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12522 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12523 | EXPECT_FALSE(r->is_pending()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 12524 | EXPECT_EQ(ERR_UNSAFE_PORT, d.request_status()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12525 | } |
| 12526 | } |
| 12527 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12528 | TEST_F(URLRequestTestFTP, FTPDirectoryListing) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12529 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12530 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 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 | ftp_test_server_.GetURL("/"), DEFAULT_PRIORITY, &d, |
| 12535 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12536 | r->Start(); |
| 12537 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12538 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12539 | d.RunUntilComplete(); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12540 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12541 | EXPECT_FALSE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12542 | EXPECT_EQ(1, d.response_started_count()); |
| 12543 | EXPECT_FALSE(d.received_data_before_response()); |
| 12544 | EXPECT_LT(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12545 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12546 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12547 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12548 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12549 | } |
| 12550 | } |
| 12551 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12552 | TEST_F(URLRequestTestFTP, FTPGetTestAnonymous) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12553 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12554 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12555 | TestDelegate d; |
| 12556 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12557 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12558 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, &d, |
| 12559 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12560 | r->Start(); |
| 12561 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12562 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12563 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12564 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12565 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12566 | EXPECT_EQ(1, d.response_started_count()); |
| 12567 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12568 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12569 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12570 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12571 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12572 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12573 | } |
| 12574 | } |
| 12575 | |
Mike West | 80462a1 | 2018-11-27 16:05:06 | [diff] [blame] | 12576 | TEST_F(URLRequestTestFTP, FTPMimeType) { |
| 12577 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 12578 | |
| 12579 | struct { |
| 12580 | const char* path; |
| 12581 | const char* mime; |
| 12582 | } test_cases[] = { |
| 12583 | {"/", "text/vnd.chromium.ftp-dir"}, |
| 12584 | {kFtpTestFile, "application/octet-stream"}, |
| 12585 | }; |
| 12586 | |
| 12587 | for (const auto test : test_cases) { |
| 12588 | TestDelegate d; |
| 12589 | |
| 12590 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 12591 | ftp_test_server_.GetURL(test.path), DEFAULT_PRIORITY, &d, |
| 12592 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12593 | r->Start(); |
| 12594 | EXPECT_TRUE(r->is_pending()); |
| 12595 | |
| 12596 | d.RunUntilComplete(); |
| 12597 | |
| 12598 | std::string mime; |
| 12599 | r->GetMimeType(&mime); |
| 12600 | EXPECT_EQ(test.mime, mime); |
| 12601 | } |
| 12602 | } |
| 12603 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12604 | TEST_F(URLRequestTestFTP, FTPGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12605 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12606 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12607 | TestDelegate d; |
| 12608 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12609 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12610 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12611 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12612 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12613 | r->Start(); |
| 12614 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12615 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12616 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12617 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12618 | EXPECT_FALSE(r->is_pending()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12619 | EXPECT_EQ(1, d.response_started_count()); |
| 12620 | EXPECT_FALSE(d.received_data_before_response()); |
| 12621 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12622 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12623 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12624 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12625 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 12626 | |
| 12627 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12628 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 12629 | TestLoadTimingNoHttpResponse(load_timing_info); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12630 | } |
| 12631 | } |
| 12632 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12633 | TEST_F(URLRequestTestFTP, FTPCheckWrongPassword) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12634 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12635 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12636 | TestDelegate d; |
| 12637 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12638 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12639 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12640 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12641 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12642 | r->Start(); |
| 12643 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12644 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12645 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12646 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12647 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12648 | EXPECT_EQ(1, d.response_started_count()); |
| 12649 | EXPECT_FALSE(d.received_data_before_response()); |
| 12650 | EXPECT_EQ(d.bytes_received(), 0); |
| 12651 | } |
| 12652 | } |
| 12653 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12654 | TEST_F(URLRequestTestFTP, FTPCheckWrongPasswordRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12655 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12656 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12657 | TestDelegate d; |
| 12658 | // Set correct login credentials. The delegate will be asked for them when |
| 12659 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 12660 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12661 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12662 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12663 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12664 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12665 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12666 | r->Start(); |
| 12667 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12668 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12669 | d.RunUntilComplete(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12670 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12671 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12672 | EXPECT_EQ(1, d.response_started_count()); |
| 12673 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12674 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12675 | } |
| 12676 | } |
| 12677 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12678 | TEST_F(URLRequestTestFTP, FTPCheckWrongUser) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12679 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12680 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12681 | TestDelegate d; |
| 12682 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12683 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12684 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 12685 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12686 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12687 | r->Start(); |
| 12688 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12689 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12690 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12691 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12692 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12693 | EXPECT_EQ(1, d.response_started_count()); |
| 12694 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12695 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12696 | } |
| 12697 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12698 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12699 | TEST_F(URLRequestTestFTP, FTPCheckWrongUserRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12700 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12701 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12702 | TestDelegate d; |
| 12703 | // Set correct login credentials. The delegate will be asked for them when |
| 12704 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 12705 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12706 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12707 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12708 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 12709 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12710 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12711 | r->Start(); |
| 12712 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12713 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12714 | d.RunUntilComplete(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12715 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12716 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12717 | EXPECT_EQ(1, d.response_started_count()); |
| 12718 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12719 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12720 | } |
| 12721 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12722 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12723 | TEST_F(URLRequestTestFTP, FTPCacheURLCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12724 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12725 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12726 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12727 | { |
| 12728 | // Pass correct login identity in the URL. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12729 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12730 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12731 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12732 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12733 | r->Start(); |
| 12734 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12735 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12736 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12737 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12738 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12739 | EXPECT_EQ(1, d->response_started_count()); |
| 12740 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12741 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12742 | } |
| 12743 | |
| 12744 | d.reset(new TestDelegate); |
| 12745 | { |
| 12746 | // This request should use cached identity from previous request. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12747 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12748 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 12749 | 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 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12762 | TEST_F(URLRequestTestFTP, FTPCacheLoginBoxCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12763 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12764 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12765 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12766 | // Set correct login credentials. The delegate will be asked for them when |
| 12767 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 12768 | d->set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12769 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12770 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12771 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12772 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12773 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12774 | r->Start(); |
| 12775 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12776 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12777 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12778 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12779 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12780 | EXPECT_EQ(1, d->response_started_count()); |
| 12781 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12782 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12783 | } |
| 12784 | |
| 12785 | // Use a new delegate without explicit credentials. The cached ones should be |
| 12786 | // used. |
| 12787 | d.reset(new TestDelegate); |
| 12788 | { |
| 12789 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 12790 | // ones. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12791 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12792 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 12793 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12794 | r->Start(); |
| 12795 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12796 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12797 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12798 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12799 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12800 | EXPECT_EQ(1, d->response_started_count()); |
| 12801 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12802 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12803 | } |
| 12804 | } |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 12805 | |
| 12806 | TEST_F(URLRequestTestFTP, RawBodyBytes) { |
| 12807 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 12808 | |
| 12809 | TestDelegate d; |
| 12810 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12811 | ftp_test_server_.GetURL("simple.html"), DEFAULT_PRIORITY, &d, |
| 12812 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 12813 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12814 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 12815 | |
| 12816 | EXPECT_EQ(6, req->GetRawBodyBytes()); |
| 12817 | } |
| 12818 | |
Matt Menke | beb95e8a | 2019-07-15 06:04:09 | [diff] [blame] | 12819 | TEST_F(URLRequestTestFTP, FtpAuthCancellation) { |
Emily Stark | 6668e09 | 2019-06-20 18:36:38 | [diff] [blame] | 12820 | ftp_test_server_.set_no_anonymous_ftp_user(true); |
| 12821 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 12822 | TestDelegate d; |
| 12823 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 12824 | ftp_test_server_.GetURL("simple.html"), DEFAULT_PRIORITY, &d, |
| 12825 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12826 | req->Start(); |
| 12827 | d.RunUntilComplete(); |
| 12828 | |
| 12829 | ASSERT_TRUE(d.auth_required_called()); |
Emily Stark | 89a3247d | 2019-06-26 18:07:40 | [diff] [blame] | 12830 | EXPECT_EQ(OK, d.request_status()); |
Emily Stark | 6668e09 | 2019-06-20 18:36:38 | [diff] [blame] | 12831 | EXPECT_TRUE(req->auth_challenge_info()); |
Emily Stark | 89a3247d | 2019-06-26 18:07:40 | [diff] [blame] | 12832 | std::string mime_type; |
| 12833 | req->GetMimeType(&mime_type); |
| 12834 | EXPECT_EQ("text/plain", mime_type); |
| 12835 | EXPECT_EQ("", d.data_received()); |
| 12836 | EXPECT_EQ(-1, req->GetExpectedContentSize()); |
Emily Stark | 6668e09 | 2019-06-20 18:36:38 | [diff] [blame] | 12837 | } |
| 12838 | |
Matt Menke | f1b97e0 | 2019-05-13 23:52:41 | [diff] [blame] | 12839 | class URLRequestTestFTPOverHttpProxy : public URLRequestTestFTP { |
| 12840 | public: |
| 12841 | // Test interface: |
| 12842 | void SetUp() override { |
| 12843 | proxy_resolution_service_ = ProxyResolutionService::CreateFixed( |
| 12844 | "localhost", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 12845 | default_context_->set_proxy_resolution_service( |
| 12846 | proxy_resolution_service_.get()); |
| 12847 | URLRequestTestFTP::SetUp(); |
| 12848 | } |
| 12849 | |
| 12850 | private: |
| 12851 | std::unique_ptr<ProxyResolutionService> proxy_resolution_service_; |
| 12852 | }; |
| 12853 | |
| 12854 | // Check that FTP is not supported over an HTTP proxy. |
| 12855 | TEST_F(URLRequestTestFTPOverHttpProxy, Fails) { |
| 12856 | TestDelegate delegate; |
| 12857 | std::unique_ptr<URLRequest> request( |
| 12858 | default_context_->CreateRequest(GURL("ftp://foo.test/"), DEFAULT_PRIORITY, |
| 12859 | &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12860 | request->Start(); |
| 12861 | delegate.RunUntilComplete(); |
| 12862 | |
| 12863 | EXPECT_THAT(delegate.request_status(), IsError(ERR_NO_SUPPORTED_PROXIES)); |
| 12864 | } |
| 12865 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 12866 | #endif // !BUILDFLAG(DISABLE_FTP_SUPPORT) |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 12867 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12868 | TEST_F(URLRequestTest, NetworkAccessedClearOnDataRequest) { |
| 12869 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12870 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12871 | GURL("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12872 | |
| 12873 | EXPECT_FALSE(req->response_info().network_accessed); |
| 12874 | |
| 12875 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12876 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12877 | |
| 12878 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 12879 | EXPECT_FALSE(req->response_info().network_accessed); |
| 12880 | } |
| 12881 | |
| 12882 | TEST_F(URLRequestTest, NetworkAccessedSetOnHostResolutionFailure) { |
| 12883 | MockHostResolver host_resolver; |
| 12884 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 12885 | TestURLRequestContext context(true); |
| 12886 | context.set_network_delegate(&network_delegate); |
| 12887 | context.set_host_resolver(&host_resolver); |
| 12888 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 12889 | context.Init(); |
| 12890 | |
| 12891 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12892 | std::unique_ptr<URLRequest> req( |
| 12893 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 12894 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12895 | |
| 12896 | EXPECT_FALSE(req->response_info().network_accessed); |
| 12897 | |
| 12898 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12899 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12900 | EXPECT_TRUE(req->response_info().network_accessed); |
| 12901 | } |
| 12902 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 12903 | // Test that URLRequest is canceled correctly. |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12904 | // See http://crbug.com/508900 |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 12905 | TEST_F(URLRequestTest, URLRequestRedirectJobCancelRequest) { |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12906 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12907 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12908 | GURL("http://not-a-real-domain/"), DEFAULT_PRIORITY, &d, |
| 12909 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12910 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12911 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12912 | req.get(), &default_network_delegate_, |
| 12913 | GURL("http://this-should-never-be-navigated-to/"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 12914 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Jumbo shrimp")); |
| 12915 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12916 | |
| 12917 | req->Start(); |
| 12918 | req->Cancel(); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12919 | base::RunLoop().RunUntilIdle(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 12920 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12921 | EXPECT_EQ(0, d.received_redirect_count()); |
| 12922 | } |
| 12923 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12924 | TEST_F(URLRequestTestHTTP, HeadersCallbacks) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12925 | ASSERT_TRUE(http_test_server()->Start()); |
| 12926 | TestURLRequestContext context; |
| 12927 | GURL url(http_test_server()->GetURL("/cachetime")); |
| 12928 | TestDelegate delegate; |
| 12929 | HttpRequestHeaders extra_headers; |
| 12930 | extra_headers.SetHeader("X-Foo", "bar"); |
| 12931 | |
| 12932 | { |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12933 | HttpRawRequestHeaders raw_req_headers; |
| 12934 | scoped_refptr<const HttpResponseHeaders> raw_resp_headers; |
| 12935 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12936 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12937 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12938 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12939 | r->SetRequestHeadersCallback(base::Bind( |
| 12940 | &HttpRawRequestHeaders::Assign, base::Unretained(&raw_req_headers))); |
| 12941 | r->SetResponseHeadersCallback(base::Bind( |
| 12942 | [](scoped_refptr<const HttpResponseHeaders>* left, |
| 12943 | scoped_refptr<const HttpResponseHeaders> right) { *left = right; }, |
| 12944 | base::Unretained(&raw_resp_headers))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12945 | r->Start(); |
| 12946 | while (!delegate.response_started_count()) |
| 12947 | base::RunLoop().RunUntilIdle(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12948 | EXPECT_FALSE(raw_req_headers.headers().empty()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12949 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12950 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12951 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12952 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12953 | EXPECT_EQ("gzip, deflate", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12954 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Connection", &value)); |
| 12955 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Host", &value)); |
| 12956 | EXPECT_EQ("GET /cachetime HTTP/1.1\r\n", raw_req_headers.request_line()); |
| 12957 | EXPECT_EQ(raw_resp_headers.get(), r->response_headers()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12958 | } |
| 12959 | { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12960 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12961 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12962 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12963 | r->SetRequestHeadersCallback(base::Bind([](HttpRawRequestHeaders) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12964 | FAIL() << "Callback should not be called unless request is sent"; |
| 12965 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12966 | r->SetResponseHeadersCallback( |
| 12967 | base::Bind([](scoped_refptr<const HttpResponseHeaders>) { |
| 12968 | FAIL() << "Callback should not be called unless request is sent"; |
| 12969 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12970 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12971 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12972 | EXPECT_TRUE(r->was_cached()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12973 | } |
| 12974 | } |
| 12975 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12976 | TEST_F(URLRequestTestHTTP, HeadersCallbacksWithRedirect) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12977 | ASSERT_TRUE(http_test_server()->Start()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12978 | HttpRawRequestHeaders raw_req_headers; |
| 12979 | scoped_refptr<const HttpResponseHeaders> raw_resp_headers; |
| 12980 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12981 | TestURLRequestContext context; |
| 12982 | TestDelegate delegate; |
| 12983 | HttpRequestHeaders extra_headers; |
| 12984 | extra_headers.SetHeader("X-Foo", "bar"); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12985 | GURL url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12986 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12987 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12988 | r->SetExtraRequestHeaders(extra_headers); |
| 12989 | r->SetRequestHeadersCallback(base::Bind(&HttpRawRequestHeaders::Assign, |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12990 | base::Unretained(&raw_req_headers))); |
| 12991 | r->SetResponseHeadersCallback(base::Bind( |
| 12992 | [](scoped_refptr<const HttpResponseHeaders>* left, |
| 12993 | scoped_refptr<const HttpResponseHeaders> right) { *left = right; }, |
| 12994 | base::Unretained(&raw_resp_headers))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12995 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 12996 | delegate.RunUntilRedirect(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12997 | |
| 12998 | ASSERT_EQ(1, delegate.received_redirect_count()); |
| 12999 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13000 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13001 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13002 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13003 | EXPECT_EQ("gzip, deflate", value); |
| 13004 | EXPECT_EQ(1, delegate.received_redirect_count()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13005 | EXPECT_EQ("GET /redirect-test.html HTTP/1.1\r\n", |
| 13006 | raw_req_headers.request_line()); |
| 13007 | EXPECT_TRUE(raw_resp_headers->HasHeader("Location")); |
| 13008 | EXPECT_EQ(302, raw_resp_headers->response_code()); |
| 13009 | EXPECT_EQ("Redirect", raw_resp_headers->GetStatusText()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13010 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13011 | raw_req_headers = HttpRawRequestHeaders(); |
| 13012 | raw_resp_headers = nullptr; |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 13013 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 13014 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 13015 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13016 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13017 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13018 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13019 | EXPECT_EQ("gzip, deflate", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13020 | EXPECT_EQ("GET /with-headers.html HTTP/1.1\r\n", |
| 13021 | raw_req_headers.request_line()); |
| 13022 | EXPECT_EQ(r->response_headers(), raw_resp_headers.get()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13023 | } |
| 13024 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13025 | TEST_F(URLRequestTest, HeadersCallbacksConnectFailed) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13026 | TestDelegate request_delegate; |
| 13027 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 13028 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13029 | GURL("http://127.0.0.1:9/"), DEFAULT_PRIORITY, &request_delegate, |
| 13030 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13031 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
| 13032 | FAIL() << "Callback should not be called unless request is sent"; |
| 13033 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13034 | r->SetResponseHeadersCallback( |
| 13035 | base::Bind([](scoped_refptr<const net::HttpResponseHeaders>) { |
| 13036 | FAIL() << "Callback should not be called unless request is sent"; |
| 13037 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13038 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 13039 | request_delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13040 | EXPECT_FALSE(r->is_pending()); |
| 13041 | } |
| 13042 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13043 | TEST_F(URLRequestTestHTTP, HeadersCallbacksAuthRetry) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13044 | ASSERT_TRUE(http_test_server()->Start()); |
| 13045 | GURL url(http_test_server()->GetURL("/auth-basic")); |
| 13046 | |
| 13047 | TestURLRequestContext context; |
| 13048 | TestDelegate delegate; |
| 13049 | |
| 13050 | delegate.set_credentials(AuthCredentials(kUser, kSecret)); |
| 13051 | HttpRequestHeaders extra_headers; |
| 13052 | extra_headers.SetHeader("X-Foo", "bar"); |
| 13053 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13054 | using ReqHeadersVector = std::vector<std::unique_ptr<HttpRawRequestHeaders>>; |
| 13055 | ReqHeadersVector raw_req_headers; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13056 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13057 | using RespHeadersVector = |
| 13058 | std::vector<scoped_refptr<const HttpResponseHeaders>>; |
| 13059 | RespHeadersVector raw_resp_headers; |
| 13060 | |
| 13061 | auto req_headers_callback = base::Bind( |
| 13062 | [](ReqHeadersVector* vec, HttpRawRequestHeaders headers) { |
| 13063 | vec->emplace_back(new HttpRawRequestHeaders(std::move(headers))); |
| 13064 | }, |
| 13065 | &raw_req_headers); |
| 13066 | auto resp_headers_callback = base::Bind( |
| 13067 | [](RespHeadersVector* vec, |
| 13068 | scoped_refptr<const HttpResponseHeaders> headers) { |
| 13069 | vec->push_back(headers); |
| 13070 | }, |
| 13071 | &raw_resp_headers); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13072 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 13073 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13074 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13075 | r->SetRequestHeadersCallback(req_headers_callback); |
| 13076 | r->SetResponseHeadersCallback(resp_headers_callback); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13077 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 13078 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13079 | EXPECT_FALSE(r->is_pending()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13080 | ASSERT_EQ(raw_req_headers.size(), 2u); |
| 13081 | ASSERT_EQ(raw_resp_headers.size(), 2u); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13082 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13083 | EXPECT_FALSE(raw_req_headers[0]->FindHeaderForTest("Authorization", &value)); |
| 13084 | EXPECT_TRUE(raw_req_headers[0]->FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13085 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13086 | EXPECT_TRUE(raw_req_headers[1]->FindHeaderForTest("Authorization", &value)); |
| 13087 | EXPECT_TRUE(raw_req_headers[1]->FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13088 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13089 | EXPECT_EQ(raw_resp_headers[1], r->response_headers()); |
| 13090 | EXPECT_NE(raw_resp_headers[0], raw_resp_headers[1]); |
| 13091 | EXPECT_EQ(401, raw_resp_headers[0]->response_code()); |
| 13092 | EXPECT_EQ("Unauthorized", raw_resp_headers[0]->GetStatusText()); |
| 13093 | |
| 13094 | std::unique_ptr<URLRequest> r2(context.CreateRequest( |
| 13095 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13096 | r2->SetExtraRequestHeaders(extra_headers); |
| 13097 | r2->SetRequestHeadersCallback(req_headers_callback); |
| 13098 | r2->SetResponseHeadersCallback(resp_headers_callback); |
| 13099 | r2->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 13100 | r2->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 13101 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13102 | EXPECT_FALSE(r2->is_pending()); |
| 13103 | ASSERT_EQ(raw_req_headers.size(), 3u); |
| 13104 | ASSERT_EQ(raw_resp_headers.size(), 3u); |
| 13105 | EXPECT_TRUE(raw_req_headers[2]->FindHeaderForTest("If-None-Match", &value)); |
| 13106 | EXPECT_NE(raw_resp_headers[2].get(), r2->response_headers()); |
| 13107 | EXPECT_EQ(304, raw_resp_headers[2]->response_code()); |
| 13108 | EXPECT_EQ("Not Modified", raw_resp_headers[2]->GetStatusText()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13109 | } |
| 13110 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13111 | TEST_F(URLRequestTest, HeadersCallbacksNonHTTP) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13112 | GURL data_url("data:text/html,<html><body>Hello!</body></html>"); |
| 13113 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 13114 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13115 | data_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13116 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13117 | FAIL() << "Callback should not be called for non-HTTP schemes"; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13118 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 13119 | r->SetResponseHeadersCallback( |
| 13120 | base::Bind([](scoped_refptr<const net::HttpResponseHeaders>) { |
| 13121 | FAIL() << "Callback should not be called for non-HTTP schemes"; |
| 13122 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13123 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 13124 | d.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 13125 | EXPECT_FALSE(r->is_pending()); |
| 13126 | } |
| 13127 | |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13128 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSet) { |
| 13129 | TestDelegate d; |
| 13130 | BlockingNetworkDelegate network_delegate( |
| 13131 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 13132 | const GURL kOriginalUrl("https://original.test"); |
| 13133 | const GURL kRedirectUrl("http://redirect.test"); |
| 13134 | network_delegate.set_redirect_url(kRedirectUrl); |
| 13135 | TestURLRequestContext context(true /* delay_initialization */); |
| 13136 | context.set_network_delegate(&network_delegate); |
| 13137 | context.Init(); |
| 13138 | |
| 13139 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 13140 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13141 | r->set_upgrade_if_insecure(true); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13142 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 13143 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13144 | GURL::Replacements replacements; |
| 13145 | // Check that the redirect URL was upgraded to HTTPS since upgrade_if_insecure |
| 13146 | // was set. |
| 13147 | replacements.SetSchemeStr("https"); |
| 13148 | EXPECT_EQ(kRedirectUrl.ReplaceComponents(replacements), |
| 13149 | d.redirect_info().new_url); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 13150 | EXPECT_TRUE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13151 | } |
| 13152 | |
| 13153 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSetExplicitPort80) { |
| 13154 | TestDelegate d; |
| 13155 | BlockingNetworkDelegate network_delegate( |
| 13156 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 13157 | const GURL kOriginalUrl("https://original.test"); |
| 13158 | const GURL kRedirectUrl("http://redirect.test:80"); |
| 13159 | network_delegate.set_redirect_url(kRedirectUrl); |
| 13160 | TestURLRequestContext context(true /* delay_initialization */); |
| 13161 | context.set_network_delegate(&network_delegate); |
| 13162 | context.Init(); |
| 13163 | |
| 13164 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 13165 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13166 | r->set_upgrade_if_insecure(true); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13167 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 13168 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13169 | GURL::Replacements replacements; |
| 13170 | // The URL host should have not been changed. |
| 13171 | EXPECT_EQ(d.redirect_info().new_url.host(), kRedirectUrl.host()); |
| 13172 | // The scheme should now be https, and the effective port should now be 443. |
| 13173 | EXPECT_TRUE(d.redirect_info().new_url.SchemeIs("https")); |
| 13174 | EXPECT_EQ(d.redirect_info().new_url.EffectiveIntPort(), 443); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 13175 | EXPECT_TRUE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13176 | } |
| 13177 | |
| 13178 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSetNonStandardPort) { |
| 13179 | TestDelegate d; |
| 13180 | BlockingNetworkDelegate network_delegate( |
| 13181 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 13182 | const GURL kOriginalUrl("https://original.test"); |
| 13183 | const GURL kRedirectUrl("http://redirect.test:1234"); |
| 13184 | network_delegate.set_redirect_url(kRedirectUrl); |
| 13185 | TestURLRequestContext context(true /* delay_initialization */); |
| 13186 | context.set_network_delegate(&network_delegate); |
| 13187 | context.Init(); |
| 13188 | |
| 13189 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 13190 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13191 | r->set_upgrade_if_insecure(true); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13192 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 13193 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13194 | GURL::Replacements replacements; |
| 13195 | // Check that the redirect URL was upgraded to HTTPS since upgrade_if_insecure |
| 13196 | // was set, nonstandard port should not have been modified. |
| 13197 | replacements.SetSchemeStr("https"); |
| 13198 | EXPECT_EQ(kRedirectUrl.ReplaceComponents(replacements), |
| 13199 | d.redirect_info().new_url); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 13200 | EXPECT_TRUE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13201 | } |
| 13202 | |
| 13203 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagNotSet) { |
| 13204 | TestDelegate d; |
| 13205 | BlockingNetworkDelegate network_delegate( |
| 13206 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 13207 | const GURL kOriginalUrl("https://original.test"); |
| 13208 | const GURL kRedirectUrl("http://redirect.test"); |
| 13209 | network_delegate.set_redirect_url(kRedirectUrl); |
| 13210 | TestURLRequestContext context(true /* delay_initialization */); |
| 13211 | context.set_network_delegate(&network_delegate); |
| 13212 | context.Init(); |
| 13213 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 13214 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13215 | r->set_upgrade_if_insecure(false); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13216 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 13217 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13218 | // The redirect URL should not be changed if the upgrade_if_insecure flag is |
| 13219 | // not set. |
| 13220 | EXPECT_EQ(kRedirectUrl, d.redirect_info().new_url); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 13221 | EXPECT_FALSE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13222 | } |
| 13223 | |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 13224 | // Test that URLRequests get properly tagged. |
| 13225 | #if defined(OS_ANDROID) |
| 13226 | TEST_F(URLRequestTestHTTP, TestTagging) { |
Peter Collingbourne | 8ccab72 | 2019-02-12 18:10:48 | [diff] [blame] | 13227 | if (!CanGetTaggedBytes()) { |
| 13228 | DVLOG(0) << "Skipping test - GetTaggedBytes unsupported."; |
| 13229 | return; |
| 13230 | } |
| 13231 | |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 13232 | ASSERT_TRUE(http_test_server()->Start()); |
| 13233 | |
| 13234 | // The tag under which the system reports untagged traffic. |
| 13235 | static const int32_t UNTAGGED_TAG = 0; |
| 13236 | |
| 13237 | uint64_t old_traffic = GetTaggedBytes(UNTAGGED_TAG); |
| 13238 | |
| 13239 | // Untagged traffic should be tagged with tag UNTAGGED_TAG. |
| 13240 | TestDelegate delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 13241 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 13242 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &delegate, |
| 13243 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13244 | EXPECT_EQ(SocketTag(), req->socket_tag()); |
| 13245 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 13246 | delegate.RunUntilComplete(); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 13247 | |
| 13248 | EXPECT_GT(GetTaggedBytes(UNTAGGED_TAG), old_traffic); |
| 13249 | |
| 13250 | int32_t tag_val1 = 0x12345678; |
| 13251 | SocketTag tag1(SocketTag::UNSET_UID, tag_val1); |
| 13252 | old_traffic = GetTaggedBytes(tag_val1); |
| 13253 | |
| 13254 | // Test specific tag value. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 13255 | req = default_context().CreateRequest(http_test_server()->GetURL("/"), |
| 13256 | DEFAULT_PRIORITY, &delegate, |
| 13257 | TRAFFIC_ANNOTATION_FOR_TESTS); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 13258 | req->set_socket_tag(tag1); |
| 13259 | EXPECT_EQ(tag1, req->socket_tag()); |
| 13260 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 13261 | delegate.RunUntilComplete(); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 13262 | |
| 13263 | EXPECT_GT(GetTaggedBytes(tag_val1), old_traffic); |
| 13264 | } |
| 13265 | #endif |
| 13266 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 13267 | // Provides a response to the 0RTT request indicating whether it was received |
| 13268 | // as early data, sending HTTP_TOO_EARLY if enabled. |
| 13269 | class ZeroRTTResponse : public test_server::BasicHttpResponse { |
| 13270 | public: |
| 13271 | ZeroRTTResponse(bool zero_rtt, bool send_too_early) |
| 13272 | : zero_rtt_(zero_rtt), send_too_early_(send_too_early) {} |
| 13273 | ~ZeroRTTResponse() override {} |
| 13274 | |
| 13275 | void SendResponse(const test_server::SendBytesCallback& send, |
| 13276 | const test_server::SendCompleteCallback& done) override { |
| 13277 | AddCustomHeader("Vary", "Early-Data"); |
| 13278 | set_content_type("text/plain"); |
| 13279 | AddCustomHeader("Cache-Control", "no-cache"); |
| 13280 | if (zero_rtt_) { |
| 13281 | if (send_too_early_) |
| 13282 | set_code(HTTP_TOO_EARLY); |
| 13283 | set_content("1"); |
| 13284 | } else { |
| 13285 | set_content("0"); |
| 13286 | } |
| 13287 | |
| 13288 | // Since the EmbeddedTestServer doesn't keep the socket open by default, |
| 13289 | // it is explicitly kept alive to allow the remaining leg of the 0RTT |
| 13290 | // handshake to be received after the early data. |
| 13291 | send.Run(ToResponseString(), base::DoNothing()); |
| 13292 | } |
| 13293 | |
| 13294 | private: |
| 13295 | bool zero_rtt_; |
| 13296 | bool send_too_early_; |
| 13297 | |
| 13298 | DISALLOW_COPY_AND_ASSIGN(ZeroRTTResponse); |
| 13299 | }; |
| 13300 | |
| 13301 | std::unique_ptr<test_server::HttpResponse> HandleZeroRTTRequest( |
| 13302 | const test_server::HttpRequest& request) { |
| 13303 | if (request.GetURL().path() != "/zerortt") |
| 13304 | return nullptr; |
| 13305 | auto iter = request.headers.find("Early-Data"); |
| 13306 | bool zero_rtt = iter != request.headers.end() && iter->second == "1"; |
| 13307 | return std::make_unique<ZeroRTTResponse>(zero_rtt, false); |
| 13308 | } |
| 13309 | |
| 13310 | class HTTPSEarlyDataTest : public TestWithScopedTaskEnvironment { |
| 13311 | public: |
| 13312 | HTTPSEarlyDataTest() |
| 13313 | : context_(true), test_server_(net::EmbeddedTestServer::TYPE_HTTPS) { |
| 13314 | auto params = std::make_unique<HttpNetworkSession::Params>(); |
| 13315 | params->enable_early_data = true; |
| 13316 | context_.set_http_network_session_params(std::move(params)); |
| 13317 | |
| 13318 | context_.set_network_delegate(&network_delegate_); |
| 13319 | cert_verifier_.set_default_result(OK); |
| 13320 | context_.set_cert_verifier(&cert_verifier_); |
| 13321 | |
David Benjamin | 151ec6b | 2019-08-02 19:38:52 | [diff] [blame^] | 13322 | SSLContextConfig config; |
| 13323 | config.version_max = SSL_PROTOCOL_VERSION_TLS1_3; |
| 13324 | ssl_config_service_ = std::make_unique<TestSSLConfigService>(config); |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 13325 | context_.set_ssl_config_service(ssl_config_service_.get()); |
| 13326 | |
| 13327 | context_.Init(); |
| 13328 | |
| 13329 | ssl_config_.version_max = SSL_PROTOCOL_VERSION_TLS1_3; |
| 13330 | ssl_config_.early_data_enabled = true; |
| 13331 | test_server_.SetSSLConfig(net::EmbeddedTestServer::CERT_OK, ssl_config_); |
| 13332 | test_server_.AddDefaultHandlers( |
| 13333 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 13334 | test_server_.RegisterRequestHandler( |
| 13335 | base::BindRepeating(&HandleZeroRTTRequest)); |
| 13336 | } |
| 13337 | |
| 13338 | ~HTTPSEarlyDataTest() override = default; |
| 13339 | |
| 13340 | void ResetSSLConfig(net::EmbeddedTestServer::ServerCertificate cert, |
| 13341 | uint16_t version) { |
| 13342 | ssl_config_.version_max = version; |
| 13343 | test_server_.ResetSSLConfig(cert, ssl_config_); |
| 13344 | } |
| 13345 | |
| 13346 | protected: |
| 13347 | MockCertVerifier cert_verifier_; |
| 13348 | TestNetworkDelegate network_delegate_; // Must outlive URLRequest. |
| 13349 | std::unique_ptr<TestSSLConfigService> ssl_config_service_; |
| 13350 | TestURLRequestContext context_; |
| 13351 | |
| 13352 | SSLServerConfig ssl_config_; |
| 13353 | EmbeddedTestServer test_server_; |
| 13354 | }; |
| 13355 | |
| 13356 | // TLSEarlyDataTest tests that we handle early data correctly. |
| 13357 | TEST_F(HTTPSEarlyDataTest, TLSEarlyDataTest) { |
| 13358 | ASSERT_TRUE(test_server_.Start()); |
| 13359 | context_.http_transaction_factory()->GetSession()->ClearSSLSessionCache(); |
| 13360 | |
| 13361 | { |
| 13362 | TestDelegate d; |
| 13363 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13364 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13365 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13366 | r->Start(); |
| 13367 | EXPECT_TRUE(r->is_pending()); |
| 13368 | |
| 13369 | base::RunLoop().Run(); |
| 13370 | |
| 13371 | EXPECT_EQ(1, d.response_started_count()); |
| 13372 | |
| 13373 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13374 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13375 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13376 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13377 | r->ssl_info().cert.get())); |
| 13378 | |
| 13379 | // The Early-Data header should be omitted in the initial request, and the |
| 13380 | // handler should return "0". |
| 13381 | EXPECT_EQ("0", d.data_received()); |
| 13382 | } |
| 13383 | |
| 13384 | context_.http_transaction_factory()->GetSession()->CloseAllConnections(); |
| 13385 | |
| 13386 | { |
| 13387 | TestDelegate d; |
| 13388 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13389 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13390 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13391 | |
| 13392 | r->Start(); |
| 13393 | EXPECT_TRUE(r->is_pending()); |
| 13394 | |
| 13395 | base::RunLoop().Run(); |
| 13396 | |
| 13397 | EXPECT_EQ(1, d.response_started_count()); |
| 13398 | |
| 13399 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13400 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13401 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13402 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13403 | r->ssl_info().cert.get())); |
| 13404 | |
| 13405 | // The Early-Data header should be a single '1' in the resumed request, and |
| 13406 | // the handler should return "1". |
| 13407 | EXPECT_EQ("1", d.data_received()); |
| 13408 | } |
| 13409 | } |
| 13410 | |
| 13411 | // TLSEarlyDataTest tests that we handle early data correctly for POST. |
| 13412 | TEST_F(HTTPSEarlyDataTest, TLSEarlyDataPOSTTest) { |
| 13413 | ASSERT_TRUE(test_server_.Start()); |
| 13414 | context_.http_transaction_factory()->GetSession()->ClearSSLSessionCache(); |
| 13415 | |
| 13416 | { |
| 13417 | TestDelegate d; |
| 13418 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13419 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13420 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13421 | r->Start(); |
| 13422 | EXPECT_TRUE(r->is_pending()); |
| 13423 | |
| 13424 | base::RunLoop().Run(); |
| 13425 | |
| 13426 | EXPECT_EQ(1, d.response_started_count()); |
| 13427 | |
| 13428 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13429 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13430 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13431 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13432 | r->ssl_info().cert.get())); |
| 13433 | |
| 13434 | // The Early-Data header should be omitted in the initial request, and the |
| 13435 | // handler should return "0". |
| 13436 | EXPECT_EQ("0", d.data_received()); |
| 13437 | } |
| 13438 | |
| 13439 | context_.http_transaction_factory()->GetSession()->CloseAllConnections(); |
| 13440 | |
| 13441 | { |
| 13442 | TestDelegate d; |
| 13443 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13444 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13445 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13446 | r->set_method("POST"); |
| 13447 | r->Start(); |
| 13448 | EXPECT_TRUE(r->is_pending()); |
| 13449 | |
| 13450 | base::RunLoop().Run(); |
| 13451 | |
| 13452 | EXPECT_EQ(1, d.response_started_count()); |
| 13453 | |
| 13454 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13455 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13456 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13457 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13458 | r->ssl_info().cert.get())); |
| 13459 | |
| 13460 | // The Early-Data header should be omitted in the request, since we don't |
| 13461 | // send POSTs over early data, and the handler should return "0". |
| 13462 | EXPECT_EQ("0", d.data_received()); |
| 13463 | } |
| 13464 | } |
| 13465 | |
| 13466 | std::unique_ptr<test_server::HttpResponse> HandleTooEarly( |
| 13467 | bool* sent_425, |
| 13468 | const test_server::HttpRequest& request) { |
| 13469 | if (request.GetURL().path() != "/tooearly") |
| 13470 | return nullptr; |
| 13471 | auto iter = request.headers.find("Early-Data"); |
| 13472 | bool zero_rtt = iter != request.headers.end() && iter->second == "1"; |
| 13473 | if (zero_rtt) |
| 13474 | *sent_425 = true; |
| 13475 | return std::make_unique<ZeroRTTResponse>(zero_rtt, true); |
| 13476 | } |
| 13477 | |
| 13478 | // Test that we handle 425 (Too Early) correctly. |
| 13479 | TEST_F(HTTPSEarlyDataTest, TLSEarlyDataTooEarlyTest) { |
| 13480 | bool sent_425 = false; |
| 13481 | test_server_.RegisterRequestHandler( |
| 13482 | base::BindRepeating(&HandleTooEarly, base::Unretained(&sent_425))); |
| 13483 | ASSERT_TRUE(test_server_.Start()); |
| 13484 | context_.http_transaction_factory()->GetSession()->ClearSSLSessionCache(); |
| 13485 | |
| 13486 | { |
| 13487 | TestDelegate d; |
| 13488 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13489 | test_server_.GetURL("/tooearly"), DEFAULT_PRIORITY, &d, |
| 13490 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13491 | r->Start(); |
| 13492 | EXPECT_TRUE(r->is_pending()); |
| 13493 | |
| 13494 | d.RunUntilComplete(); |
| 13495 | |
| 13496 | EXPECT_EQ(1, d.response_started_count()); |
| 13497 | |
| 13498 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13499 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13500 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13501 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13502 | r->ssl_info().cert.get())); |
| 13503 | |
| 13504 | // The Early-Data header should be omitted in the initial request, and the |
| 13505 | // handler should return "0". |
| 13506 | EXPECT_EQ("0", d.data_received()); |
| 13507 | EXPECT_FALSE(sent_425); |
| 13508 | } |
| 13509 | |
| 13510 | context_.http_transaction_factory()->GetSession()->CloseAllConnections(); |
| 13511 | |
| 13512 | { |
| 13513 | TestDelegate d; |
| 13514 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13515 | test_server_.GetURL("/tooearly"), DEFAULT_PRIORITY, &d, |
| 13516 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13517 | |
| 13518 | r->Start(); |
| 13519 | EXPECT_TRUE(r->is_pending()); |
| 13520 | |
| 13521 | d.RunUntilComplete(); |
| 13522 | |
| 13523 | EXPECT_EQ(1, d.response_started_count()); |
| 13524 | |
| 13525 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13526 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13527 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13528 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13529 | r->ssl_info().cert.get())); |
| 13530 | |
| 13531 | // The resumption request will encounter a 425 error and retry without early |
| 13532 | // data, and the handler should return "0". |
| 13533 | EXPECT_EQ("0", d.data_received()); |
| 13534 | EXPECT_TRUE(sent_425); |
| 13535 | } |
| 13536 | } |
| 13537 | |
| 13538 | // TLSEarlyDataRejectTest tests that we gracefully handle an early data reject |
| 13539 | // and retry without early data. |
| 13540 | TEST_F(HTTPSEarlyDataTest, TLSEarlyDataRejectTest) { |
| 13541 | ASSERT_TRUE(test_server_.Start()); |
| 13542 | context_.http_transaction_factory()->GetSession()->ClearSSLSessionCache(); |
| 13543 | |
| 13544 | { |
| 13545 | TestDelegate d; |
| 13546 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13547 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13548 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13549 | |
| 13550 | r->Start(); |
| 13551 | EXPECT_TRUE(r->is_pending()); |
| 13552 | |
| 13553 | d.RunUntilComplete(); |
| 13554 | |
| 13555 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13556 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13557 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13558 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13559 | r->ssl_info().cert.get())); |
| 13560 | |
| 13561 | // The Early-Data header should be omitted in the initial request, and the |
| 13562 | // handler should return "0". |
| 13563 | EXPECT_EQ("0", d.data_received()); |
| 13564 | } |
| 13565 | |
| 13566 | context_.http_transaction_factory()->GetSession()->CloseAllConnections(); |
| 13567 | |
| 13568 | // The certificate in the resumption is changed to confirm that the |
| 13569 | // certificate change is observed. |
| 13570 | scoped_refptr<X509Certificate> old_cert = test_server_.GetCertificate(); |
| 13571 | ResetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED, |
| 13572 | SSL_PROTOCOL_VERSION_TLS1_3); |
| 13573 | |
| 13574 | { |
| 13575 | TestDelegate d; |
| 13576 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13577 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13578 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13579 | |
| 13580 | r->Start(); |
| 13581 | EXPECT_TRUE(r->is_pending()); |
| 13582 | |
| 13583 | d.RunUntilComplete(); |
| 13584 | |
| 13585 | EXPECT_EQ(1, d.response_started_count()); |
| 13586 | |
| 13587 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13588 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13589 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13590 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13591 | r->ssl_info().cert.get())); |
| 13592 | EXPECT_FALSE(old_cert->EqualsIncludingChain(r->ssl_info().cert.get())); |
| 13593 | |
| 13594 | // The Early-Data header should be omitted in the rejected request, and the |
| 13595 | // handler should return "0". |
| 13596 | EXPECT_EQ("0", d.data_received()); |
| 13597 | } |
| 13598 | } |
| 13599 | |
| 13600 | // TLSEarlyDataTLS12RejectTest tests that we gracefully handle an early data |
| 13601 | // reject from a TLS 1.2 server and retry without early data. |
| 13602 | TEST_F(HTTPSEarlyDataTest, TLSEarlyDataTLS12RejectTest) { |
| 13603 | ASSERT_TRUE(test_server_.Start()); |
| 13604 | context_.http_transaction_factory()->GetSession()->ClearSSLSessionCache(); |
| 13605 | |
| 13606 | { |
| 13607 | TestDelegate d; |
| 13608 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13609 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13610 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13611 | |
| 13612 | r->Start(); |
| 13613 | EXPECT_TRUE(r->is_pending()); |
| 13614 | |
| 13615 | d.RunUntilComplete(); |
| 13616 | |
| 13617 | EXPECT_EQ(1, d.response_started_count()); |
| 13618 | |
| 13619 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13620 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13621 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13622 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13623 | r->ssl_info().cert.get())); |
| 13624 | |
| 13625 | // The Early-Data header should be omitted in the initial request, and the |
| 13626 | // handler should return "0". |
| 13627 | EXPECT_EQ("0", d.data_received()); |
| 13628 | } |
| 13629 | |
| 13630 | context_.http_transaction_factory()->GetSession()->CloseAllConnections(); |
| 13631 | |
| 13632 | // The certificate in the resumption is changed to confirm that the |
| 13633 | // certificate change is observed. |
| 13634 | scoped_refptr<X509Certificate> old_cert = test_server_.GetCertificate(); |
| 13635 | ResetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED, |
| 13636 | SSL_PROTOCOL_VERSION_TLS1_2); |
| 13637 | |
| 13638 | { |
| 13639 | TestDelegate d; |
| 13640 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13641 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13642 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13643 | |
| 13644 | r->Start(); |
| 13645 | EXPECT_TRUE(r->is_pending()); |
| 13646 | |
| 13647 | d.RunUntilComplete(); |
| 13648 | |
| 13649 | EXPECT_EQ(1, d.response_started_count()); |
| 13650 | |
| 13651 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_2, |
| 13652 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13653 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13654 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13655 | r->ssl_info().cert.get())); |
| 13656 | EXPECT_FALSE(old_cert->EqualsIncludingChain(r->ssl_info().cert.get())); |
| 13657 | |
| 13658 | // The Early-Data header should be omitted in the rejected request, and the |
| 13659 | // handler should return "0". |
| 13660 | EXPECT_EQ("0", d.data_received()); |
| 13661 | } |
| 13662 | } |
| 13663 | |
Emily Stark | 08f6c97 | 2019-05-28 17:24:27 | [diff] [blame] | 13664 | // Tests that AuthChallengeInfo is available on the request. |
| 13665 | TEST_F(URLRequestTestHTTP, AuthChallengeInfo) { |
| 13666 | ASSERT_TRUE(http_test_server()->Start()); |
| 13667 | GURL url(http_test_server()->GetURL("/auth-basic")); |
| 13668 | |
| 13669 | TestURLRequestContext context; |
| 13670 | TestDelegate delegate; |
| 13671 | |
| 13672 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 13673 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13674 | r->Start(); |
| 13675 | delegate.RunUntilComplete(); |
| 13676 | ASSERT_TRUE(r->auth_challenge_info().has_value()); |
| 13677 | EXPECT_FALSE(r->auth_challenge_info()->is_proxy); |
| 13678 | EXPECT_EQ(url::Origin::Create(url), r->auth_challenge_info()->challenger); |
| 13679 | EXPECT_EQ("basic", r->auth_challenge_info()->scheme); |
| 13680 | EXPECT_EQ("testrealm", r->auth_challenge_info()->realm); |
| 13681 | EXPECT_EQ("Basic realm=\"testrealm\"", r->auth_challenge_info()->challenge); |
| 13682 | EXPECT_EQ("/auth-basic", r->auth_challenge_info()->path); |
| 13683 | } |
| 13684 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 13685 | } // namespace net |