[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" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 37 | #include "base/power_monitor/power_monitor.h" |
| 38 | #include "base/power_monitor/power_monitor_source.h" |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 39 | #include "base/run_loop.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 40 | #include "base/single_thread_task_runner.h" |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 41 | #include "base/stl_util.h" |
[email protected] | 4dc3ad4f | 2013-06-11 07:15:50 | [diff] [blame] | 42 | #include "base/strings/string_number_conversions.h" |
[email protected] | d069c11a | 2013-04-13 00:01:55 | [diff] [blame] | 43 | #include "base/strings/string_piece.h" |
[email protected] | d778e042 | 2013-03-06 18:10:22 | [diff] [blame] | 44 | #include "base/strings/string_split.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 45 | #include "base/strings/string_util.h" |
| 46 | #include "base/strings/stringprintf.h" |
[email protected] | 750b2f3c | 2013-06-07 18:41:05 | [diff] [blame] | 47 | #include "base/strings/utf_string_conversions.h" |
Devlin Cronin | e4bcb40e | 2018-06-05 18:02:47 | [diff] [blame] | 48 | #include "base/test/metrics/histogram_tester.h" |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 49 | #include "base/test/scoped_feature_list.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 50 | #include "base/threading/thread_task_runner_handle.h" |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 51 | #include "base/values.h" |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 52 | #include "build/buildflag.h" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 53 | #include "net/base/chunked_upload_data_stream.h" |
tfarina | 43a416b | 2016-01-06 21:48:07 | [diff] [blame] | 54 | #include "net/base/directory_listing.h" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 55 | #include "net/base/elements_upload_data_stream.h" |
David Benjamin | a7fde61 | 2019-03-15 14:20:58 | [diff] [blame] | 56 | #include "net/base/escape.h" |
Mike West | fbd4411 | 2019-05-08 18:23:37 | [diff] [blame] | 57 | #include "net/base/features.h" |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 58 | #include "net/base/layered_network_delegate.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 59 | #include "net/base/load_flags.h" |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 60 | #include "net/base/load_timing_info.h" |
| 61 | #include "net/base/load_timing_info_test_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 62 | #include "net/base/net_errors.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 63 | #include "net/base/net_module.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 64 | #include "net/base/proxy_server.h" |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 65 | #include "net/base/request_priority.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 66 | #include "net/base/test_completion_callback.h" |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 67 | #include "net/base/upload_bytes_element_reader.h" |
| 68 | #include "net/base/upload_data_stream.h" |
| 69 | #include "net/base/upload_file_element_reader.h" |
tfarina | 7a4a7fd | 2016-01-20 14:23:44 | [diff] [blame] | 70 | #include "net/base/url_util.h" |
Matt Mueller | a4aaa9d | 2019-04-24 21:09:14 | [diff] [blame] | 71 | #include "net/cert/cert_net_fetcher.h" |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 72 | #include "net/cert/crl_set.h" |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 73 | #include "net/cert/ct_policy_enforcer.h" |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 74 | #include "net/cert/ct_policy_status.h" |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 75 | #include "net/cert/do_nothing_ct_verifier.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 76 | #include "net/cert/ev_root_ca_metadata.h" |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 77 | #include "net/cert/mock_cert_verifier.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 78 | #include "net/cert/multi_log_ct_verifier.h" |
eranm | dcec963 | 2016-10-10 14:16:10 | [diff] [blame] | 79 | #include "net/cert/signed_certificate_timestamp_and_status.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 80 | #include "net/cert/test_root_certs.h" |
Matt Mueller | a4aaa9d | 2019-04-24 21:09:14 | [diff] [blame] | 81 | #include "net/cert_net/cert_net_fetcher_impl.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" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 92 | #include "net/http/http_server_properties_impl.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_entry.h" |
| 98 | #include "net/log/test_net_log_util.h" |
Scott Violet | 0caaaf43 | 2018-03-24 00:43:59 | [diff] [blame] | 99 | #include "net/net_buildflags.h" |
Lily Houghton | ffe89daa0 | 2018-03-09 18:30:03 | [diff] [blame] | 100 | #include "net/proxy_resolution/proxy_resolution_service.h" |
Ryan Hamilton | a3ee93a7 | 2018-08-01 22:03:08 | [diff] [blame] | 101 | #include "net/quic/mock_crypto_client_stream_factory.h" |
| 102 | #include "net/quic/quic_server_info.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 103 | #include "net/socket/socket_test_util.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 104 | #include "net/socket/ssl_client_socket.h" |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 105 | #include "net/ssl/client_cert_identity_test_util.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 106 | #include "net/ssl/ssl_connection_status_flags.h" |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 107 | #include "net/ssl/ssl_private_key.h" |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 108 | #include "net/ssl/ssl_server_config.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 | |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 136 | #if defined(OS_FUCHSIA) |
| 137 | #define USE_BUILTIN_CERT_VERIFIER |
| 138 | #endif |
| 139 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 140 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 141 | #include "net/base/filename_util.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 142 | #include "net/url_request/file_protocol_handler.h" |
| 143 | #include "net/url_request/url_request_file_dir_job.h" |
| 144 | #endif |
| 145 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 146 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 147 | #include "net/ftp/ftp_network_layer.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 148 | #include "net/url_request/ftp_protocol_handler.h" |
| 149 | #endif |
| 150 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 151 | #if defined(OS_WIN) |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 152 | #include "base/win/scoped_com_initializer.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 153 | #endif |
| 154 | |
Matt Mueller | 7f60dc4 | 2019-04-26 22:47:10 | [diff] [blame] | 155 | #if defined(OS_MACOSX) |
| 156 | #include "base/mac/mac_util.h" |
| 157 | #endif |
| 158 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 159 | #if BUILDFLAG(ENABLE_REPORTING) |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 160 | #include "net/network_error_logging/network_error_logging_service.h" |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 161 | #include "net/network_error_logging/network_error_logging_test_util.h" |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 162 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 163 | |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 164 | #if defined(USE_NSS_CERTS) |
| 165 | #include "net/cert_net/nss_ocsp.h" |
| 166 | #endif |
| 167 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 168 | using net::test::IsError; |
| 169 | using net::test::IsOk; |
| 170 | |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 171 | using base::ASCIIToUTF16; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 172 | using base::Time; |
halton.huo | e4e4574 | 2014-12-08 07:55:46 | [diff] [blame] | 173 | using std::string; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 174 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 175 | namespace net { |
| 176 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 177 | namespace { |
| 178 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 179 | namespace test_default { |
| 180 | #include "net/http/transport_security_state_static_unittest_default.h" |
xunjieli | 815ad5b | 2017-07-18 15:51:35 | [diff] [blame] | 181 | } |
| 182 | |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 183 | const base::string16 kChrome(ASCIIToUTF16("chrome")); |
| 184 | const base::string16 kSecret(ASCIIToUTF16("secret")); |
| 185 | const base::string16 kUser(ASCIIToUTF16("user")); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 186 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 187 | const base::FilePath::CharType kTestFilePath[] = |
| 188 | FILE_PATH_LITERAL("net/data/url_request_unittest"); |
| 189 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 190 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 191 | !defined(OS_FUCHSIA) |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 192 | // Test file used in most FTP tests. |
| 193 | const char kFtpTestFile[] = "BullRunSpeech.txt"; |
| 194 | #endif |
| 195 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 196 | // Tests load timing information in the case a fresh connection was used, with |
| 197 | // no proxy. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 198 | void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info, |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 199 | int connect_timing_flags) { |
| 200 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 201 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 202 | |
| 203 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 204 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 205 | |
| 206 | EXPECT_LE(load_timing_info.request_start, |
| 207 | load_timing_info.connect_timing.connect_start); |
| 208 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 209 | connect_timing_flags); |
| 210 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 211 | load_timing_info.send_start); |
| 212 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 213 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_start); |
| 214 | EXPECT_LE(load_timing_info.receive_headers_start, |
| 215 | load_timing_info.receive_headers_end); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 216 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 217 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 218 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 219 | } |
| 220 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 221 | // Same as above, but with proxy times. |
| 222 | void TestLoadTimingNotReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 223 | const LoadTimingInfo& load_timing_info, |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 224 | int connect_timing_flags) { |
| 225 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 226 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 227 | |
| 228 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 229 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 230 | |
| 231 | EXPECT_LE(load_timing_info.request_start, |
| 232 | load_timing_info.proxy_resolve_start); |
| 233 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 234 | load_timing_info.proxy_resolve_end); |
| 235 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 236 | load_timing_info.connect_timing.connect_start); |
| 237 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 238 | connect_timing_flags); |
| 239 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 240 | load_timing_info.send_start); |
| 241 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 242 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_start); |
| 243 | EXPECT_LE(load_timing_info.receive_headers_start, |
| 244 | load_timing_info.receive_headers_end); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // Same as above, but with a reused socket and proxy times. |
| 248 | void TestLoadTimingReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 249 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 250 | EXPECT_TRUE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 251 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 252 | |
| 253 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 254 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 255 | |
| 256 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 257 | |
| 258 | EXPECT_LE(load_timing_info.request_start, |
| 259 | load_timing_info.proxy_resolve_start); |
| 260 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 261 | load_timing_info.proxy_resolve_end); |
| 262 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 263 | load_timing_info.send_start); |
| 264 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 265 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_start); |
| 266 | EXPECT_LE(load_timing_info.receive_headers_start, |
| 267 | load_timing_info.receive_headers_end); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 268 | } |
| 269 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 270 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 271 | // Tests load timing information in the case of a cache hit, when no cache |
| 272 | // validation request was sent over the wire. |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 273 | base::StringPiece TestNetResourceProvider(int key) { |
| 274 | return "header"; |
| 275 | } |
| 276 | |
| 277 | void FillBuffer(char* buffer, size_t len) { |
| 278 | static bool called = false; |
| 279 | if (!called) { |
| 280 | called = true; |
| 281 | int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 282 | srand(seed); |
| 283 | } |
| 284 | |
| 285 | for (size_t i = 0; i < len; i++) { |
| 286 | buffer[i] = static_cast<char>(rand()); |
| 287 | if (!buffer[i]) |
| 288 | buffer[i] = 'g'; |
| 289 | } |
| 290 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 291 | #endif |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 292 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 293 | void TestLoadTimingCacheHitNoNetwork( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 294 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 295 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 296 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 297 | |
| 298 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 299 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 300 | |
| 301 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 302 | EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start); |
| 303 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 304 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_start); |
| 305 | EXPECT_LE(load_timing_info.receive_headers_start, |
| 306 | load_timing_info.receive_headers_end); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 307 | |
| 308 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 309 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 310 | } |
| 311 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 312 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 313 | !defined(OS_FUCHSIA) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 314 | // Tests load timing in the case that there is no HTTP response. This can be |
| 315 | // used to test in the case of errors or non-HTTP requests. |
| 316 | void TestLoadTimingNoHttpResponse( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 317 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 318 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 319 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 320 | |
| 321 | // Only the request times should be non-null. |
| 322 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 323 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 324 | |
| 325 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 326 | |
| 327 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 328 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 329 | EXPECT_TRUE(load_timing_info.send_start.is_null()); |
| 330 | EXPECT_TRUE(load_timing_info.send_end.is_null()); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 331 | EXPECT_TRUE(load_timing_info.receive_headers_start.is_null()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 332 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 333 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 334 | #endif |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 335 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 336 | // Test power monitor source that can simulate entering suspend mode. Can't use |
| 337 | // the one in base/ because it insists on bringing its own MessageLoop. |
| 338 | class TestPowerMonitorSource : public base::PowerMonitorSource { |
| 339 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 340 | TestPowerMonitorSource() = default; |
| 341 | ~TestPowerMonitorSource() override = default; |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 342 | |
Colin Blundell | c46597f | 2018-06-14 16:43:10 | [diff] [blame] | 343 | void Shutdown() override {} |
| 344 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 345 | void Suspend() { ProcessPowerEvent(SUSPEND_EVENT); } |
| 346 | |
| 347 | void Resume() { ProcessPowerEvent(RESUME_EVENT); } |
| 348 | |
| 349 | bool IsOnBatteryPowerImpl() override { return false; } |
| 350 | |
| 351 | private: |
| 352 | DISALLOW_COPY_AND_ASSIGN(TestPowerMonitorSource); |
| 353 | }; |
| 354 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 355 | // Job that allows monitoring of its priority. |
| 356 | class PriorityMonitoringURLRequestJob : public URLRequestTestJob { |
| 357 | public: |
| 358 | // The latest priority of the job is always written to |request_priority_|. |
| 359 | PriorityMonitoringURLRequestJob(URLRequest* request, |
| 360 | NetworkDelegate* network_delegate, |
| 361 | RequestPriority* request_priority) |
| 362 | : URLRequestTestJob(request, network_delegate), |
| 363 | request_priority_(request_priority) { |
| 364 | *request_priority_ = DEFAULT_PRIORITY; |
| 365 | } |
| 366 | |
| 367 | void SetPriority(RequestPriority priority) override { |
| 368 | *request_priority_ = priority; |
| 369 | URLRequestTestJob::SetPriority(priority); |
| 370 | } |
| 371 | |
| 372 | private: |
| 373 | RequestPriority* const request_priority_; |
| 374 | }; |
| 375 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 376 | // Do a case-insensitive search through |haystack| for |needle|. |
| 377 | bool ContainsString(const std::string& haystack, const char* needle) { |
brettw | a2027fb | 2015-07-14 02:24:50 | [diff] [blame] | 378 | std::string::const_iterator it = std::search( |
| 379 | haystack.begin(), haystack.end(), needle, needle + strlen(needle), |
| 380 | base::CaseInsensitiveCompareASCII<char>()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 381 | return it != haystack.end(); |
| 382 | } |
| 383 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 384 | std::unique_ptr<UploadDataStream> CreateSimpleUploadData(const char* data) { |
| 385 | std::unique_ptr<UploadElementReader> reader( |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 386 | new UploadBytesElementReader(data, strlen(data))); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 387 | return ElementsUploadDataStream::CreateWithReader(std::move(reader), 0); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 388 | } |
| 389 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 390 | // Verify that the SSLInfo of a successful SSL connection has valid values. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 391 | void CheckSSLInfo(const SSLInfo& ssl_info) { |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 392 | // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated. |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 393 | uint16_t cipher_suite = |
| 394 | SSLConnectionStatusToCipherSuite(ssl_info.connection_status); |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 395 | EXPECT_NE(0U, cipher_suite); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 396 | } |
| 397 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 398 | void CheckFullRequestHeaders(const HttpRequestHeaders& headers, |
| 399 | const GURL& host_url) { |
| 400 | std::string sent_value; |
| 401 | |
| 402 | EXPECT_TRUE(headers.GetHeader("Host", &sent_value)); |
| 403 | EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value); |
| 404 | |
| 405 | EXPECT_TRUE(headers.GetHeader("Connection", &sent_value)); |
| 406 | EXPECT_EQ("keep-alive", sent_value); |
| 407 | } |
| 408 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 409 | // A network delegate that allows the user to choose a subset of request stages |
| 410 | // to block in. When blocking, the delegate can do one of the following: |
| 411 | // * synchronously return a pre-specified error code, or |
| 412 | // * asynchronously return that value via an automatically called callback, |
| 413 | // or |
| 414 | // * block and wait for the user to do a callback. |
| 415 | // Additionally, the user may also specify a redirect URL -- then each request |
| 416 | // with the current URL different from the redirect target will be redirected |
| 417 | // to that target, in the on-before-URL-request stage, independent of whether |
| 418 | // the delegate blocks in ON_BEFORE_URL_REQUEST or not. |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 419 | class BlockingNetworkDelegate : public TestNetworkDelegate { |
| 420 | public: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 421 | // Stages in which the delegate can block. |
| 422 | enum Stage { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 423 | NOT_BLOCKED = 0, |
| 424 | ON_BEFORE_URL_REQUEST = 1 << 0, |
| 425 | ON_BEFORE_SEND_HEADERS = 1 << 1, |
| 426 | ON_HEADERS_RECEIVED = 1 << 2, |
| 427 | ON_AUTH_REQUIRED = 1 << 3 |
| 428 | }; |
| 429 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 430 | // Behavior during blocked stages. During other stages, just |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 431 | // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 432 | enum BlockMode { |
| 433 | SYNCHRONOUS, // No callback, returns specified return values. |
| 434 | AUTO_CALLBACK, // |this| posts a task to run the callback using the |
| 435 | // specified return codes. |
| 436 | USER_CALLBACK, // User takes care of doing a callback. |retval_| and |
| 437 | // |auth_retval_| are ignored. In every blocking stage the |
| 438 | // message loop is quit. |
| 439 | }; |
| 440 | |
| 441 | // Creates a delegate which does not block at all. |
| 442 | explicit BlockingNetworkDelegate(BlockMode block_mode); |
| 443 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 444 | // Runs the message loop until the delegate blocks. |
| 445 | void RunUntilBlocked(); |
| 446 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 447 | // For users to trigger a callback returning |response|. |
| 448 | // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks. |
| 449 | // Only call if |block_mode_| == USER_CALLBACK. |
| 450 | void DoCallback(int response); |
| 451 | void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response); |
| 452 | |
| 453 | // Setters. |
| 454 | void set_retval(int retval) { |
| 455 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 456 | ASSERT_NE(ERR_IO_PENDING, retval); |
| 457 | ASSERT_NE(OK, retval); |
| 458 | retval_ = retval; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 459 | } |
| 460 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 461 | // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then |
| 462 | // |auth_credentials_| will be passed with the response. |
| 463 | void set_auth_retval(AuthRequiredResponse auth_retval) { |
| 464 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 465 | ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval); |
| 466 | auth_retval_ = auth_retval; |
| 467 | } |
| 468 | void set_auth_credentials(const AuthCredentials& auth_credentials) { |
| 469 | auth_credentials_ = auth_credentials; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 470 | } |
| 471 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 472 | void set_redirect_url(const GURL& url) { |
| 473 | redirect_url_ = url; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 474 | } |
| 475 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 476 | void set_block_on(int block_on) { |
| 477 | block_on_ = block_on; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 478 | } |
| 479 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 480 | // Allows the user to check in which state did we block. |
| 481 | Stage stage_blocked_for_callback() const { |
| 482 | EXPECT_EQ(USER_CALLBACK, block_mode_); |
| 483 | return stage_blocked_for_callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | private: |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 487 | void OnBlocked(); |
| 488 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 489 | void RunCallback(int response, CompletionOnceCallback callback); |
| 490 | void RunAuthCallback(AuthRequiredResponse response, AuthCallback callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 491 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 492 | // TestNetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 493 | int OnBeforeURLRequest(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 494 | CompletionOnceCallback callback, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 495 | GURL* new_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 496 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 497 | int OnBeforeStartTransaction(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 498 | CompletionOnceCallback callback, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 499 | HttpRequestHeaders* headers) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 500 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 501 | int OnHeadersReceived( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 502 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 503 | CompletionOnceCallback callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 504 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 505 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 506 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 507 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 508 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 509 | URLRequest* request, |
| 510 | const AuthChallengeInfo& auth_info, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 511 | AuthCallback callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 512 | AuthCredentials* credentials) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 513 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 514 | // Resets the callbacks and |stage_blocked_for_callback_|. |
| 515 | void Reset(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 516 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 517 | // Checks whether we should block in |stage|. If yes, returns an error code |
| 518 | // and optionally sets up callback based on |block_mode_|. If no, returns OK. |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 519 | int MaybeBlockStage(Stage stage, CompletionOnceCallback callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 520 | |
| 521 | // Configuration parameters, can be adjusted by public methods: |
| 522 | const BlockMode block_mode_; |
| 523 | |
| 524 | // Values returned on blocking stages when mode is SYNCHRONOUS or |
| 525 | // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING. |
| 526 | int retval_; // To be returned in non-auth stages. |
| 527 | AuthRequiredResponse auth_retval_; |
| 528 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 529 | GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 530 | int block_on_; // Bit mask: in which stages to block. |
| 531 | |
| 532 | // |auth_credentials_| will be copied to |*target_auth_credential_| on |
| 533 | // callback. |
| 534 | AuthCredentials auth_credentials_; |
| 535 | AuthCredentials* target_auth_credentials_; |
| 536 | |
| 537 | // Internal variables, not set by not the user: |
| 538 | // Last blocked stage waiting for user callback (unused if |block_mode_| != |
| 539 | // USER_CALLBACK). |
| 540 | Stage stage_blocked_for_callback_; |
| 541 | |
| 542 | // Callback objects stored during blocking stages. |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 543 | CompletionOnceCallback callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 544 | AuthCallback auth_callback_; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 545 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 546 | // Closure to run to exit RunUntilBlocked(). |
| 547 | base::OnceClosure on_blocked_; |
| 548 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 549 | base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; |
| 550 | |
| 551 | DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 552 | }; |
| 553 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 554 | BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode) |
| 555 | : block_mode_(block_mode), |
| 556 | retval_(OK), |
| 557 | auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION), |
| 558 | block_on_(0), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 559 | target_auth_credentials_(nullptr), |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 560 | stage_blocked_for_callback_(NOT_BLOCKED), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 561 | weak_factory_(this) {} |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 562 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 563 | void BlockingNetworkDelegate::RunUntilBlocked() { |
| 564 | base::RunLoop run_loop; |
| 565 | on_blocked_ = run_loop.QuitClosure(); |
| 566 | run_loop.Run(); |
| 567 | } |
| 568 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 569 | void BlockingNetworkDelegate::DoCallback(int response) { |
| 570 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 571 | ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 572 | ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 573 | CompletionOnceCallback callback = std::move(callback_); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 574 | Reset(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 575 | |
| 576 | // |callback| may trigger completion of a request, so post it as a task, so |
| 577 | // it will run under a subsequent TestDelegate::RunUntilComplete() loop. |
| 578 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 579 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::RunCallback, |
| 580 | weak_factory_.GetWeakPtr(), response, |
| 581 | std::move(callback))); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | void BlockingNetworkDelegate::DoAuthCallback( |
| 585 | NetworkDelegate::AuthRequiredResponse response) { |
| 586 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 587 | ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 588 | AuthCallback auth_callback = std::move(auth_callback_); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 589 | Reset(); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 590 | RunAuthCallback(response, std::move(auth_callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 591 | } |
| 592 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 593 | void BlockingNetworkDelegate::OnBlocked() { |
| 594 | // If this fails due to |on_blocked_| being null then OnBlocked() was run by |
| 595 | // a RunLoop other than RunUntilBlocked(), indicating a bug in the calling |
| 596 | // test. |
| 597 | std::move(on_blocked_).Run(); |
| 598 | } |
| 599 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 600 | void BlockingNetworkDelegate::RunCallback(int response, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 601 | CompletionOnceCallback callback) { |
| 602 | std::move(callback).Run(response); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 606 | AuthCallback callback) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 607 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 608 | ASSERT_TRUE(target_auth_credentials_ != nullptr); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 609 | *target_auth_credentials_ = auth_credentials_; |
| 610 | } |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 611 | std::move(callback).Run(response); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 612 | } |
| 613 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 614 | int BlockingNetworkDelegate::OnBeforeURLRequest(URLRequest* request, |
| 615 | CompletionOnceCallback callback, |
| 616 | GURL* new_url) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 617 | if (redirect_url_ == request->url()) |
| 618 | return OK; // We've already seen this request and redirected elsewhere. |
| 619 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 620 | // TestNetworkDelegate always completes synchronously. |
| 621 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeURLRequest( |
| 622 | request, base::NullCallback(), new_url)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 623 | |
| 624 | if (!redirect_url_.is_empty()) |
| 625 | *new_url = redirect_url_; |
| 626 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 627 | return MaybeBlockStage(ON_BEFORE_URL_REQUEST, std::move(callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 628 | } |
| 629 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 630 | int BlockingNetworkDelegate::OnBeforeStartTransaction( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 631 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 632 | CompletionOnceCallback callback, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 633 | HttpRequestHeaders* headers) { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 634 | // TestNetworkDelegate always completes synchronously. |
| 635 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeStartTransaction( |
| 636 | request, base::NullCallback(), headers)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 637 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 638 | return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, std::move(callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | int BlockingNetworkDelegate::OnHeadersReceived( |
| 642 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 643 | CompletionOnceCallback callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 644 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 645 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
| 646 | GURL* allowed_unsafe_redirect_url) { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 647 | // TestNetworkDelegate always completes synchronously. |
| 648 | CHECK_NE(ERR_IO_PENDING, |
| 649 | TestNetworkDelegate::OnHeadersReceived( |
| 650 | request, base::NullCallback(), original_response_headers, |
| 651 | override_response_headers, allowed_unsafe_redirect_url)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 652 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 653 | return MaybeBlockStage(ON_HEADERS_RECEIVED, std::move(callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired( |
| 657 | URLRequest* request, |
| 658 | const AuthChallengeInfo& auth_info, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 659 | AuthCallback callback, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 660 | AuthCredentials* credentials) { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 661 | // TestNetworkDelegate always completes synchronously. |
| 662 | CHECK_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, |
| 663 | TestNetworkDelegate::OnAuthRequired( |
| 664 | request, auth_info, base::NullCallback(), credentials)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 665 | // Check that the user has provided callback for the previous blocked stage. |
| 666 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 667 | |
| 668 | if ((block_on_ & ON_AUTH_REQUIRED) == 0) { |
| 669 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 670 | } |
| 671 | |
| 672 | target_auth_credentials_ = credentials; |
| 673 | |
| 674 | switch (block_mode_) { |
| 675 | case SYNCHRONOUS: |
| 676 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) |
| 677 | *target_auth_credentials_ = auth_credentials_; |
| 678 | return auth_retval_; |
| 679 | |
| 680 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 681 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 682 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::RunAuthCallback, |
| 683 | weak_factory_.GetWeakPtr(), auth_retval_, |
| 684 | std::move(callback))); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 685 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 686 | |
| 687 | case USER_CALLBACK: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 688 | auth_callback_ = std::move(callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 689 | stage_blocked_for_callback_ = ON_AUTH_REQUIRED; |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 690 | // We may reach here via a callback prior to RunUntilBlocked(), so post |
| 691 | // a task to fetch and run the |on_blocked_| closure. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 692 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 693 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::OnBlocked, |
| 694 | weak_factory_.GetWeakPtr())); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 695 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 696 | } |
| 697 | NOTREACHED(); |
| 698 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value. |
| 699 | } |
| 700 | |
| 701 | void BlockingNetworkDelegate::Reset() { |
| 702 | EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 703 | stage_blocked_for_callback_ = NOT_BLOCKED; |
| 704 | callback_.Reset(); |
| 705 | auth_callback_.Reset(); |
| 706 | } |
| 707 | |
| 708 | int BlockingNetworkDelegate::MaybeBlockStage( |
| 709 | BlockingNetworkDelegate::Stage stage, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 710 | CompletionOnceCallback callback) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 711 | // Check that the user has provided callback for the previous blocked stage. |
| 712 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 713 | |
| 714 | if ((block_on_ & stage) == 0) { |
| 715 | return OK; |
| 716 | } |
| 717 | |
| 718 | switch (block_mode_) { |
| 719 | case SYNCHRONOUS: |
| 720 | EXPECT_NE(OK, retval_); |
| 721 | return retval_; |
| 722 | |
| 723 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 724 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 725 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::RunCallback, |
| 726 | weak_factory_.GetWeakPtr(), retval_, |
| 727 | std::move(callback))); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 728 | return ERR_IO_PENDING; |
| 729 | |
| 730 | case USER_CALLBACK: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 731 | callback_ = std::move(callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 732 | stage_blocked_for_callback_ = stage; |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 733 | // We may reach here via a callback prior to RunUntilBlocked(), so post |
| 734 | // a task to fetch and run the |on_blocked_| closure. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 735 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 736 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::OnBlocked, |
| 737 | weak_factory_.GetWeakPtr())); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 738 | return ERR_IO_PENDING; |
| 739 | } |
| 740 | NOTREACHED(); |
| 741 | return 0; |
| 742 | } |
| 743 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 744 | class TestURLRequestContextWithProxy : public TestURLRequestContext { |
| 745 | public: |
| 746 | // Does not own |delegate|. |
| 747 | TestURLRequestContextWithProxy(const std::string& proxy, |
| 748 | NetworkDelegate* delegate) |
| 749 | : TestURLRequestContext(true) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 750 | context_storage_.set_proxy_resolution_service( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 751 | ProxyResolutionService::CreateFixed(proxy, |
| 752 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 753 | set_network_delegate(delegate); |
| 754 | Init(); |
| 755 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 756 | ~TestURLRequestContextWithProxy() override = default; |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 757 | }; |
| 758 | |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 759 | // A mock ReportSenderInterface that just remembers the latest report |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 760 | // URI and report to be sent. |
| 761 | class MockCertificateReportSender |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 762 | : public TransportSecurityState::ReportSenderInterface { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 763 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 764 | MockCertificateReportSender() = default; |
| 765 | ~MockCertificateReportSender() override = default; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 766 | |
meacer | 5d4dc5a | 2017-04-27 20:37:48 | [diff] [blame] | 767 | void Send(const GURL& report_uri, |
| 768 | base::StringPiece content_type, |
| 769 | base::StringPiece report, |
| 770 | const base::Callback<void()>& success_callback, |
| 771 | const base::Callback<void(const GURL&, int, int)>& error_callback) |
| 772 | override { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 773 | latest_report_uri_ = report_uri; |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 774 | report.CopyToString(&latest_report_); |
| 775 | content_type.CopyToString(&latest_content_type_); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 776 | } |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 777 | const GURL& latest_report_uri() { return latest_report_uri_; } |
| 778 | const std::string& latest_report() { return latest_report_; } |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 779 | const std::string& latest_content_type() { return latest_content_type_; } |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 780 | |
| 781 | private: |
| 782 | GURL latest_report_uri_; |
| 783 | std::string latest_report_; |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 784 | std::string latest_content_type_; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 785 | }; |
| 786 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 787 | // OCSPErrorTestDelegate caches the SSLInfo passed to OnSSLCertificateError. |
| 788 | // This is needed because after the certificate failure, the URLRequest will |
| 789 | // retry the connection, and return a partial SSLInfo with a cached cert status. |
| 790 | // The partial SSLInfo does not have the OCSP information filled out. |
| 791 | class OCSPErrorTestDelegate : public TestDelegate { |
| 792 | public: |
| 793 | void OnSSLCertificateError(URLRequest* request, |
Emily Stark | 79fba584 | 2019-04-25 04:59:36 | [diff] [blame] | 794 | int net_error, |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 795 | const SSLInfo& ssl_info, |
| 796 | bool fatal) override { |
| 797 | ssl_info_ = ssl_info; |
| 798 | on_ssl_certificate_error_called_ = true; |
Emily Stark | 79fba584 | 2019-04-25 04:59:36 | [diff] [blame] | 799 | TestDelegate::OnSSLCertificateError(request, net_error, ssl_info, fatal); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | bool on_ssl_certificate_error_called() { |
| 803 | return on_ssl_certificate_error_called_; |
| 804 | } |
| 805 | |
| 806 | SSLInfo ssl_info() { return ssl_info_; } |
| 807 | |
| 808 | private: |
| 809 | bool on_ssl_certificate_error_called_ = false; |
| 810 | SSLInfo ssl_info_; |
| 811 | }; |
| 812 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 813 | } // namespace |
| 814 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 815 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 816 | class URLRequestTest : public PlatformTest, public WithScopedTaskEnvironment { |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 817 | public: |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 818 | URLRequestTest() |
| 819 | : default_context_(std::make_unique<TestURLRequestContext>(true)) { |
| 820 | default_context_->set_network_delegate(&default_network_delegate_); |
| 821 | default_context_->set_net_log(&net_log_); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 822 | job_factory_impl_ = new URLRequestJobFactoryImpl(); |
| 823 | job_factory_.reset(job_factory_impl_); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 824 | } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 825 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 826 | ~URLRequestTest() override { |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 827 | // URLRequestJobs may post clean-up tasks on destruction. |
| 828 | base::RunLoop().RunUntilIdle(); |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 829 | |
| 830 | SetTransportSecurityStateSourceForTesting(nullptr); |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 831 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 832 | |
dcheng | 2339883c | 2014-12-23 00:23:05 | [diff] [blame] | 833 | void SetUp() override { |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 834 | SetUpFactory(); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 835 | default_context_->set_job_factory(job_factory_.get()); |
| 836 | default_context_->Init(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 837 | PlatformTest::SetUp(); |
| 838 | } |
| 839 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 840 | void TearDown() override { default_context_.reset(); } |
| 841 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 842 | virtual void SetUpFactory() { |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 843 | job_factory_impl_->SetProtocolHandler( |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 844 | "data", std::make_unique<DataProtocolHandler>()); |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 845 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 846 | job_factory_impl_->SetProtocolHandler( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 847 | "file", std::make_unique<FileProtocolHandler>( |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 848 | base::ThreadTaskRunnerHandle::Get())); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 849 | #endif |
| 850 | } |
| 851 | |
| 852 | TestNetworkDelegate* default_network_delegate() { |
| 853 | return &default_network_delegate_; |
| 854 | } |
| 855 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 856 | TestURLRequestContext& default_context() const { return *default_context_; } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 857 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 858 | // Adds the TestJobInterceptor to the default context. |
| 859 | TestJobInterceptor* AddTestInterceptor() { |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 860 | TestJobInterceptor* protocol_handler_ = new TestJobInterceptor(); |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 861 | job_factory_impl_->SetProtocolHandler("http", nullptr); |
| 862 | job_factory_impl_->SetProtocolHandler("http", |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 863 | base::WrapUnique(protocol_handler_)); |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 864 | return protocol_handler_; |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 865 | } |
| 866 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 867 | // Creates a temp test file and writes |data| to the file. The file will be |
| 868 | // deleted after the test completes. |
| 869 | void CreateTestFile(const char* data, |
| 870 | size_t data_size, |
| 871 | base::FilePath* test_file) { |
| 872 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 873 | // Get an absolute path since |temp_dir| can contain a symbolic link. As of |
| 874 | // now, Mac and Android bots return a path with a symbolic link. |
| 875 | base::FilePath absolute_temp_dir = |
| 876 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
| 877 | |
| 878 | ASSERT_TRUE(base::CreateTemporaryFileInDir(absolute_temp_dir, test_file)); |
| 879 | ASSERT_EQ(static_cast<int>(data_size), |
| 880 | base::WriteFile(*test_file, data, data_size)); |
| 881 | } |
| 882 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 883 | protected: |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 884 | TestNetLog net_log_; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 885 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 886 | URLRequestJobFactoryImpl* job_factory_impl_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 887 | std::unique_ptr<URLRequestJobFactory> job_factory_; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 888 | std::unique_ptr<TestURLRequestContext> default_context_; |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 889 | base::ScopedTempDir temp_dir_; |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 890 | }; |
| 891 | |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 892 | // This NetworkDelegate is picky about what files are accessible. Only |
| 893 | // whitelisted files are allowed. |
| 894 | class CookieBlockingNetworkDelegate : public TestNetworkDelegate { |
| 895 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 896 | CookieBlockingNetworkDelegate() = default; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 897 | |
| 898 | // Adds |directory| to the access white list. |
| 899 | void AddToWhitelist(const base::FilePath& directory) { |
| 900 | whitelist_.insert(directory); |
| 901 | } |
| 902 | |
| 903 | private: |
| 904 | // Returns true if |path| matches the white list. |
| 905 | bool OnCanAccessFileInternal(const base::FilePath& path) const { |
| 906 | for (const auto& directory : whitelist_) { |
| 907 | if (directory == path || directory.IsParent(path)) |
| 908 | return true; |
| 909 | } |
| 910 | return false; |
| 911 | } |
| 912 | |
| 913 | // Returns true only if both |original_path| and |absolute_path| match the |
| 914 | // white list. |
| 915 | bool OnCanAccessFile(const URLRequest& request, |
| 916 | const base::FilePath& original_path, |
| 917 | const base::FilePath& absolute_path) const override { |
| 918 | return (OnCanAccessFileInternal(original_path) && |
| 919 | OnCanAccessFileInternal(absolute_path)); |
| 920 | } |
| 921 | |
| 922 | std::set<base::FilePath> whitelist_; |
| 923 | |
| 924 | DISALLOW_COPY_AND_ASSIGN(CookieBlockingNetworkDelegate); |
| 925 | }; |
| 926 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 927 | TEST_F(URLRequestTest, AboutBlankTest) { |
| 928 | TestDelegate d; |
| 929 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 930 | std::unique_ptr<URLRequest> r( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 931 | default_context().CreateRequest(GURL("about:blank"), DEFAULT_PRIORITY, |
| 932 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 933 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 934 | r->Start(); |
| 935 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 936 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 937 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 938 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 939 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 940 | EXPECT_FALSE(d.received_data_before_response()); |
| 941 | EXPECT_EQ(d.bytes_received(), 0); |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 942 | EXPECT_TRUE(r->GetResponseRemoteEndpoint().address().empty()); |
| 943 | EXPECT_EQ(0, r->GetResponseRemoteEndpoint().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 944 | |
| 945 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 946 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 947 | } |
| 948 | } |
| 949 | |
| 950 | TEST_F(URLRequestTest, DataURLImageTest) { |
| 951 | TestDelegate d; |
| 952 | { |
| 953 | // Use our nice little Chrome logo. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 954 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 955 | GURL("data:image/png;base64," |
| 956 | "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUB" |
| 957 | "BjG3w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQ" |
| 958 | "xIEVxitD5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI" |
| 959 | "5MpCVdW0gO7tvNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKz" |
| 960 | "Mx1+fg9bAgK6zHq9cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7Ck" |
| 961 | "IMDxQpF7r/MWq12UctI1dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0" |
| 962 | "WYUqt57yWf2McHTObYPbVD+ZwbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2H" |
| 963 | "QQcm43XwmtoYM6vVKleh0uoWvnzW3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nk" |
| 964 | "DFHF9ZS+uYVjRUasMeHUmyLYtcklTvzWGFZnNOXczThvpKIzjcahSqIzkvDLayDq6" |
| 965 | "D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTbxXX1T5xsV9tm9r4TQwHLiZw/pdDZJ" |
| 966 | "ea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5MrVvMzNaI3+ERHfrFzPKQukrQ" |
| 967 | "GI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gVMd6q5c8GdosynKmSeRuGz" |
| 968 | "pjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq/uxxZKzNiZFGD0wRC" |
| 969 | "3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7FUx1qLnV7MGF40" |
| 970 | "smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufBJbsysjjW4" |
| 971 | "kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJMnlm2O" |
| 972 | "34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfmm" |
| 973 | "oRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV" |
| 974 | "5EnhORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 975 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 976 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 977 | r->Start(); |
| 978 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 979 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 980 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 981 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 982 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 983 | EXPECT_FALSE(d.received_data_before_response()); |
| 984 | EXPECT_EQ(d.bytes_received(), 911); |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 985 | EXPECT_TRUE(r->GetResponseRemoteEndpoint().address().empty()); |
| 986 | EXPECT_EQ(0, r->GetResponseRemoteEndpoint().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 987 | |
| 988 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 989 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 993 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 994 | TEST_F(URLRequestTest, FileTest) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 995 | const char kTestFileContent[] = "Hello"; |
| 996 | base::FilePath test_file; |
| 997 | ASSERT_NO_FATAL_FAILURE( |
| 998 | CreateTestFile(kTestFileContent, sizeof(kTestFileContent), &test_file)); |
kraush | 5a64582 | 2016-04-07 18:35:04 | [diff] [blame] | 999 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1000 | GURL test_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1001 | |
| 1002 | TestDelegate d; |
| 1003 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1004 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1005 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1006 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1007 | r->Start(); |
| 1008 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1009 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1010 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1011 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1012 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1013 | EXPECT_EQ(1, d.response_started_count()); |
| 1014 | EXPECT_FALSE(d.received_data_before_response()); |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1015 | EXPECT_EQ(d.bytes_received(), static_cast<int>(sizeof(kTestFileContent))); |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 1016 | EXPECT_TRUE(r->GetResponseRemoteEndpoint().address().empty()); |
| 1017 | EXPECT_EQ(0, r->GetResponseRemoteEndpoint().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 1018 | |
| 1019 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1020 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1024 | TEST_F(URLRequestTest, FileTestCancel) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1025 | const char kTestFileContent[] = "Hello"; |
| 1026 | base::FilePath test_file; |
| 1027 | ASSERT_NO_FATAL_FAILURE( |
| 1028 | CreateTestFile(kTestFileContent, sizeof(kTestFileContent), &test_file)); |
| 1029 | |
| 1030 | GURL test_url = FilePathToFileURL(test_file); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1031 | |
| 1032 | TestDelegate d; |
| 1033 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1034 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1035 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1036 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1037 | r->Start(); |
| 1038 | EXPECT_TRUE(r->is_pending()); |
| 1039 | r->Cancel(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1040 | } |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 1041 | // Async cancellation should be safe even when URLRequest has been already |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1042 | // destroyed. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1043 | base::RunLoop().RunUntilIdle(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1044 | } |
| 1045 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1046 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 1047 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1048 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1049 | FillBuffer(buffer.get(), buffer_size); |
| 1050 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1051 | base::FilePath test_file; |
| 1052 | ASSERT_NO_FATAL_FAILURE( |
| 1053 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1054 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1055 | |
| 1056 | const size_t first_byte_position = 500; |
| 1057 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 1058 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 1059 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 1060 | buffer.get() + last_byte_position + 1); |
| 1061 | |
| 1062 | TestDelegate d; |
| 1063 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1064 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1065 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1066 | |
| 1067 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1068 | headers.SetHeader( |
| 1069 | HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1070 | HttpByteRange::Bounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1071 | first_byte_position, last_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1072 | r->SetExtraRequestHeaders(headers); |
| 1073 | r->Start(); |
| 1074 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1075 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1076 | d.RunUntilComplete(); |
| 1077 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1078 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1079 | EXPECT_EQ(1, d.response_started_count()); |
| 1080 | EXPECT_FALSE(d.received_data_before_response()); |
| 1081 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 1082 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 1083 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 1084 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 1088 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1089 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1090 | FillBuffer(buffer.get(), buffer_size); |
| 1091 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1092 | base::FilePath test_file; |
| 1093 | ASSERT_NO_FATAL_FAILURE( |
| 1094 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1095 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1096 | |
| 1097 | const size_t first_byte_position = 500; |
| 1098 | const size_t last_byte_position = buffer_size - 1; |
| 1099 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 1100 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 1101 | buffer.get() + last_byte_position + 1); |
| 1102 | |
| 1103 | TestDelegate d; |
| 1104 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1105 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1106 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1107 | |
| 1108 | HttpRequestHeaders headers; |
| 1109 | headers.SetHeader(HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1110 | HttpByteRange::RightUnbounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1111 | first_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1112 | r->SetExtraRequestHeaders(headers); |
| 1113 | r->Start(); |
| 1114 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1115 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1116 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1117 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1118 | EXPECT_EQ(1, d.response_started_count()); |
| 1119 | EXPECT_FALSE(d.received_data_before_response()); |
| 1120 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 1121 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 1122 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 1123 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 1127 | const size_t buffer_size = 400000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1128 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1129 | FillBuffer(buffer.get(), buffer_size); |
| 1130 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1131 | base::FilePath test_file; |
| 1132 | ASSERT_NO_FATAL_FAILURE( |
| 1133 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1134 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1135 | |
| 1136 | TestDelegate d; |
| 1137 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1138 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1139 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1140 | |
| 1141 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1142 | headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1143 | r->SetExtraRequestHeaders(headers); |
| 1144 | r->Start(); |
| 1145 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1146 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1147 | d.RunUntilComplete(); |
| 1148 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1149 | EXPECT_TRUE(d.request_failed()); |
| 1150 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1151 | } |
| 1152 | |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1153 | TEST_F(URLRequestTest, AllowFileURLs) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1154 | std::string test_data("monkey"); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1155 | base::FilePath test_file; |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1156 | ASSERT_NO_FATAL_FAILURE( |
| 1157 | CreateTestFile(test_data.data(), test_data.size(), &test_file)); |
| 1158 | |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1159 | // The directory part of the path returned from CreateTemporaryFileInDir() |
| 1160 | // can be slightly different from |absolute_temp_dir| on Windows. |
| 1161 | // Example: C:\\Users\\CHROME~2 -> C:\\Users\\chrome-bot |
| 1162 | // Hence the test should use the directory name of |test_file|, rather than |
| 1163 | // |absolute_temp_dir|, for whitelisting. |
| 1164 | base::FilePath real_temp_dir = test_file.DirName(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1165 | GURL test_file_url = FilePathToFileURL(test_file); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1166 | { |
| 1167 | TestDelegate d; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1168 | CookieBlockingNetworkDelegate network_delegate; |
| 1169 | network_delegate.AddToWhitelist(real_temp_dir); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1170 | default_context().set_network_delegate(&network_delegate); |
| 1171 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1172 | test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1173 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1174 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1175 | // This should be allowed as the file path is whitelisted. |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1176 | EXPECT_FALSE(d.request_failed()); |
| 1177 | EXPECT_EQ(test_data, d.data_received()); |
| 1178 | } |
| 1179 | |
| 1180 | { |
| 1181 | TestDelegate d; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1182 | CookieBlockingNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1183 | default_context().set_network_delegate(&network_delegate); |
| 1184 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1185 | test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1186 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1187 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1188 | // This should be rejected as the file path is not whitelisted. |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1189 | EXPECT_TRUE(d.request_failed()); |
| 1190 | EXPECT_EQ("", d.data_received()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1191 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1192 | } |
| 1193 | } |
| 1194 | |
Wez | 3553913 | 2018-07-17 11:26:05 | [diff] [blame] | 1195 | #if defined(OS_POSIX) // Because of symbolic links. |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1196 | |
| 1197 | TEST_F(URLRequestTest, SymlinksToFiles) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1198 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1199 | // Get an absolute path since temp_dir can contain a symbolic link. |
| 1200 | base::FilePath absolute_temp_dir = |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1201 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1202 | |
| 1203 | // Create a good directory (will be whitelisted) and a good file. |
| 1204 | base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); |
| 1205 | ASSERT_TRUE(base::CreateDirectory(good_dir)); |
| 1206 | base::FilePath good_file; |
| 1207 | ASSERT_TRUE(base::CreateTemporaryFileInDir(good_dir, &good_file)); |
| 1208 | std::string good_data("good"); |
| 1209 | base::WriteFile(good_file, good_data.data(), good_data.size()); |
| 1210 | // See the comment in AllowFileURLs() for why this is done. |
| 1211 | base::FilePath real_good_dir = good_file.DirName(); |
| 1212 | |
| 1213 | // Create a bad directory (will not be whitelisted) and a bad file. |
| 1214 | base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); |
| 1215 | ASSERT_TRUE(base::CreateDirectory(bad_dir)); |
| 1216 | base::FilePath bad_file; |
| 1217 | ASSERT_TRUE(base::CreateTemporaryFileInDir(bad_dir, &bad_file)); |
| 1218 | std::string bad_data("bad"); |
| 1219 | base::WriteFile(bad_file, bad_data.data(), bad_data.size()); |
| 1220 | |
| 1221 | // This symlink will point to the good file. Access to the symlink will be |
| 1222 | // allowed as both the symlink and the destination file are in the same |
| 1223 | // good directory. |
| 1224 | base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); |
| 1225 | ASSERT_TRUE(base::CreateSymbolicLink(good_file, good_symlink)); |
| 1226 | GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1227 | // This symlink will point to the bad file. Even though the symlink is in |
| 1228 | // the good directory, access to the symlink will be rejected since it |
| 1229 | // points to the bad file. |
| 1230 | base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); |
| 1231 | ASSERT_TRUE(base::CreateSymbolicLink(bad_file, bad_symlink)); |
| 1232 | GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1233 | |
| 1234 | CookieBlockingNetworkDelegate network_delegate; |
| 1235 | network_delegate.AddToWhitelist(real_good_dir); |
| 1236 | { |
| 1237 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1238 | default_context().set_network_delegate(&network_delegate); |
| 1239 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1240 | good_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1241 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1242 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1243 | // good_file_url should be allowed. |
| 1244 | EXPECT_FALSE(d.request_failed()); |
| 1245 | EXPECT_EQ(good_data, d.data_received()); |
| 1246 | } |
| 1247 | |
| 1248 | { |
| 1249 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1250 | default_context().set_network_delegate(&network_delegate); |
| 1251 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1252 | bad_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1253 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1254 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1255 | // bad_file_url should be rejected. |
| 1256 | EXPECT_TRUE(d.request_failed()); |
| 1257 | EXPECT_EQ("", d.data_received()); |
| 1258 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
| 1259 | } |
| 1260 | } |
| 1261 | |
| 1262 | TEST_F(URLRequestTest, SymlinksToDirs) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1263 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1264 | // Get an absolute path since temp_dir can contain a symbolic link. |
| 1265 | base::FilePath absolute_temp_dir = |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1266 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1267 | |
| 1268 | // Create a good directory (will be whitelisted). |
| 1269 | base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); |
| 1270 | ASSERT_TRUE(base::CreateDirectory(good_dir)); |
| 1271 | |
| 1272 | // Create a bad directory (will not be whitelisted). |
| 1273 | base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); |
| 1274 | ASSERT_TRUE(base::CreateDirectory(bad_dir)); |
| 1275 | |
| 1276 | // This symlink will point to the good directory. Access to the symlink |
| 1277 | // will be allowed as the symlink is in the good dir that'll be white |
| 1278 | // listed. |
| 1279 | base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); |
| 1280 | ASSERT_TRUE(base::CreateSymbolicLink(good_dir, good_symlink)); |
| 1281 | GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1282 | // This symlink will point to the bad directory. Even though the symlink is |
| 1283 | // in the good directory, access to the symlink will be rejected since it |
| 1284 | // points to the bad directory. |
| 1285 | base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); |
| 1286 | ASSERT_TRUE(base::CreateSymbolicLink(bad_dir, bad_symlink)); |
| 1287 | GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1288 | |
| 1289 | CookieBlockingNetworkDelegate network_delegate; |
| 1290 | network_delegate.AddToWhitelist(good_dir); |
| 1291 | { |
| 1292 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1293 | default_context().set_network_delegate(&network_delegate); |
| 1294 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1295 | good_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1296 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1297 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1298 | // good_file_url should be allowed. |
| 1299 | EXPECT_FALSE(d.request_failed()); |
| 1300 | ASSERT_NE(d.data_received().find("good_symlink"), std::string::npos); |
| 1301 | } |
| 1302 | |
| 1303 | { |
| 1304 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1305 | default_context().set_network_delegate(&network_delegate); |
| 1306 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1307 | bad_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1308 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1309 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1310 | // bad_file_url should be rejected. |
| 1311 | EXPECT_TRUE(d.request_failed()); |
| 1312 | EXPECT_EQ("", d.data_received()); |
| 1313 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
| 1314 | } |
| 1315 | } |
| 1316 | |
Wez | 3553913 | 2018-07-17 11:26:05 | [diff] [blame] | 1317 | #endif // defined(OS_POSIX) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1318 | |
| 1319 | TEST_F(URLRequestTest, FileDirCancelTest) { |
| 1320 | // Put in mock resource provider. |
| 1321 | NetModule::SetResourceProvider(TestNetResourceProvider); |
| 1322 | |
| 1323 | TestDelegate d; |
| 1324 | { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1325 | base::FilePath file_path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1326 | base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1327 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 1328 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
| 1329 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1330 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1331 | FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d, |
| 1332 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1333 | req->Start(); |
| 1334 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1335 | |
| 1336 | d.set_cancel_in_received_data_pending(true); |
| 1337 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1338 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | // Take out mock resource provider. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1342 | NetModule::SetResourceProvider(nullptr); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1343 | } |
| 1344 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1345 | TEST_F(URLRequestTest, FileDirOutputSanity) { |
| 1346 | // Verify the general sanity of the the output of the file: |
| 1347 | // directory lister by checking for the output of a known existing |
| 1348 | // file. |
| 1349 | const char sentinel_name[] = "filedir-sentinel"; |
| 1350 | |
| 1351 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1352 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1353 | path = path.Append(kTestFilePath); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1354 | |
| 1355 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1356 | std::unique_ptr<URLRequest> req( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1357 | default_context().CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 1358 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1359 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1360 | d.RunUntilComplete(); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1361 | |
| 1362 | // Generate entry for the sentinel file. |
| 1363 | base::FilePath sentinel_path = path.AppendASCII(sentinel_name); |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 1364 | base::File::Info info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 1365 | EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1366 | EXPECT_GT(info.size, 0); |
| 1367 | std::string sentinel_output = GetDirectoryListingEntry( |
| 1368 | base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1369 | std::string(sentinel_name), false /* is_dir */, info.size, |
| 1370 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1371 | info.last_modified); |
| 1372 | |
| 1373 | ASSERT_LT(0, d.bytes_received()); |
| 1374 | ASSERT_FALSE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1375 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1376 | // Check for the entry generated for the "sentinel" file. |
| 1377 | const std::string& data = d.data_received(); |
| 1378 | ASSERT_NE(data.find(sentinel_output), std::string::npos); |
| 1379 | } |
| 1380 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1381 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 1382 | // There is an implicit redirect when loading a file path that matches a |
| 1383 | // directory and does not end with a slash. Ensure that following such |
| 1384 | // redirects does not crash. See http://crbug.com/18686. |
| 1385 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1386 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1387 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1388 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1389 | |
| 1390 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1391 | std::unique_ptr<URLRequest> req( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1392 | default_context().CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 1393 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1394 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1395 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1396 | |
| 1397 | ASSERT_EQ(1, d.received_redirect_count()); |
| 1398 | ASSERT_LT(0, d.bytes_received()); |
| 1399 | ASSERT_FALSE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1400 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | #if defined(OS_WIN) |
| 1404 | // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
| 1405 | TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
| 1406 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1407 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1408 | GURL("file:///"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1409 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1410 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1411 | |
| 1412 | ASSERT_EQ(1, d.received_redirect_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1413 | EXPECT_NE(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1414 | } |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1415 | #endif // defined(OS_WIN) |
| 1416 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 1417 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1418 | |
| 1419 | TEST_F(URLRequestTest, InvalidUrlTest) { |
| 1420 | TestDelegate d; |
| 1421 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1422 | std::unique_ptr<URLRequest> r( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1423 | default_context().CreateRequest(GURL("invalid url"), DEFAULT_PRIORITY, |
| 1424 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1425 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1426 | r->Start(); |
| 1427 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1428 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1429 | d.RunUntilComplete(); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1430 | EXPECT_TRUE(d.request_failed()); |
| 1431 | } |
| 1432 | } |
| 1433 | |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1434 | TEST_F(URLRequestTest, InvalidReferrerTest) { |
| 1435 | TestURLRequestContext context; |
| 1436 | TestNetworkDelegate network_delegate; |
| 1437 | network_delegate.set_cancel_request_with_policy_violating_referrer(true); |
| 1438 | context.set_network_delegate(&network_delegate); |
| 1439 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1440 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1441 | context.CreateRequest(GURL("http://localhost/"), DEFAULT_PRIORITY, &d, |
| 1442 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1443 | req->SetReferrer("https://somewhere.com/"); |
| 1444 | |
| 1445 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1446 | d.RunUntilComplete(); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1447 | EXPECT_TRUE(d.request_failed()); |
| 1448 | } |
| 1449 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1450 | #if defined(OS_WIN) |
| 1451 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
| 1452 | base::FilePath app_path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1453 | base::PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1454 | app_path = app_path.Append(kTestFilePath); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1455 | app_path = app_path.AppendASCII("with-headers.html"); |
| 1456 | |
jdoerrie | 6312bf6 | 2019-02-01 22:03:42 | [diff] [blame] | 1457 | base::string16 lnk_path = app_path.value() + FILE_PATH_LITERAL(".lnk"); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1458 | |
| 1459 | base::win::ScopedCOMInitializer com_initializer; |
| 1460 | |
| 1461 | // Temporarily create a shortcut for test |
| 1462 | { |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 1463 | Microsoft::WRL::ComPtr<IShellLink> shell; |
robliao | eb9bfd64 | 2017-05-18 17:35:16 | [diff] [blame] | 1464 | ASSERT_TRUE(SUCCEEDED(::CoCreateInstance( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1465 | CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shell)))); |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 1466 | Microsoft::WRL::ComPtr<IPersistFile> persist; |
robliao | 310fa98b | 2017-05-11 17:14:00 | [diff] [blame] | 1467 | ASSERT_TRUE(SUCCEEDED(shell.CopyTo(persist.GetAddressOf()))); |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1468 | EXPECT_TRUE(SUCCEEDED(shell->SetPath(base::as_wcstr(app_path.value())))); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1469 | EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1470 | EXPECT_TRUE(SUCCEEDED(persist->Save(base::as_wcstr(lnk_path), TRUE))); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | TestDelegate d; |
| 1474 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1475 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1476 | FilePathToFileURL(base::FilePath(lnk_path)), DEFAULT_PRIORITY, &d, |
| 1477 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1478 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1479 | r->Start(); |
| 1480 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1481 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1482 | d.RunUntilComplete(); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1483 | |
| 1484 | WIN32_FILE_ATTRIBUTE_DATA data; |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1485 | GetFileAttributesEx(base::as_wcstr(app_path.value()), GetFileExInfoStandard, |
jdoerrie | 6312bf6 | 2019-02-01 22:03:42 | [diff] [blame] | 1486 | &data); |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1487 | HANDLE file = CreateFile(base::as_wcstr(app_path.value()), GENERIC_READ, |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1488 | FILE_SHARE_READ, nullptr, OPEN_EXISTING, |
| 1489 | FILE_ATTRIBUTE_NORMAL, nullptr); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1490 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1491 | std::unique_ptr<char[]> buffer(new char[data.nFileSizeLow]); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1492 | DWORD read_size; |
| 1493 | BOOL result; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1494 | result = |
| 1495 | ReadFile(file, buffer.get(), data.nFileSizeLow, &read_size, nullptr); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1496 | std::string content(buffer.get(), read_size); |
| 1497 | CloseHandle(file); |
| 1498 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1499 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1500 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1501 | EXPECT_EQ(content, d.data_received()); |
| 1502 | } |
| 1503 | |
| 1504 | // Clean the shortcut |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1505 | DeleteFile(base::as_wcstr(lnk_path)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1506 | } |
| 1507 | #endif // defined(OS_WIN) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1508 | |
| 1509 | // Custom URLRequestJobs for use with interceptor tests |
| 1510 | class RestartTestJob : public URLRequestTestJob { |
| 1511 | public: |
| 1512 | RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1513 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1514 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1515 | void StartAsync() override { this->NotifyRestartRequired(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1516 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1517 | ~RestartTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1518 | }; |
| 1519 | |
| 1520 | class CancelTestJob : public URLRequestTestJob { |
| 1521 | public: |
| 1522 | explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1523 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1524 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1525 | void StartAsync() override { request_->Cancel(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1526 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1527 | ~CancelTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1528 | }; |
| 1529 | |
| 1530 | class CancelThenRestartTestJob : public URLRequestTestJob { |
| 1531 | public: |
| 1532 | explicit CancelThenRestartTestJob(URLRequest* request, |
| 1533 | NetworkDelegate* network_delegate) |
| 1534 | : URLRequestTestJob(request, network_delegate, true) { |
| 1535 | } |
| 1536 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1537 | void StartAsync() override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1538 | request_->Cancel(); |
| 1539 | this->NotifyRestartRequired(); |
| 1540 | } |
| 1541 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1542 | ~CancelThenRestartTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1543 | }; |
| 1544 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1545 | // An Interceptor for use with interceptor tests. |
| 1546 | class MockURLRequestInterceptor : public URLRequestInterceptor { |
| 1547 | public: |
| 1548 | // Static getters for canned response header and data strings. |
| 1549 | static std::string ok_data() { |
| 1550 | return URLRequestTestJob::test_data_1(); |
| 1551 | } |
| 1552 | |
| 1553 | static std::string ok_headers() { |
| 1554 | return URLRequestTestJob::test_headers(); |
| 1555 | } |
| 1556 | |
| 1557 | static std::string redirect_data() { |
| 1558 | return std::string(); |
| 1559 | } |
| 1560 | |
| 1561 | static std::string redirect_headers() { |
| 1562 | return URLRequestTestJob::test_redirect_headers(); |
| 1563 | } |
| 1564 | |
| 1565 | static std::string error_data() { |
| 1566 | return std::string("ohhh nooooo mr. bill!"); |
| 1567 | } |
| 1568 | |
| 1569 | static std::string error_headers() { |
| 1570 | return URLRequestTestJob::test_error_headers(); |
| 1571 | } |
| 1572 | |
| 1573 | MockURLRequestInterceptor() |
| 1574 | : intercept_main_request_(false), restart_main_request_(false), |
| 1575 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 1576 | simulate_main_network_error_(false), |
| 1577 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 1578 | intercept_final_response_(false), cancel_final_request_(false), |
| 1579 | use_url_request_http_job_(false), |
| 1580 | did_intercept_main_(false), did_restart_main_(false), |
| 1581 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 1582 | did_simulate_error_main_(false), |
| 1583 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 1584 | did_intercept_final_(false), did_cancel_final_(false) { |
| 1585 | } |
| 1586 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1587 | ~MockURLRequestInterceptor() override = default; |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1588 | |
| 1589 | // URLRequestInterceptor implementation: |
| 1590 | URLRequestJob* MaybeInterceptRequest( |
| 1591 | URLRequest* request, |
| 1592 | NetworkDelegate* network_delegate) const override { |
| 1593 | if (restart_main_request_) { |
| 1594 | restart_main_request_ = false; |
| 1595 | did_restart_main_ = true; |
| 1596 | return new RestartTestJob(request, network_delegate); |
| 1597 | } |
| 1598 | if (cancel_main_request_) { |
| 1599 | cancel_main_request_ = false; |
| 1600 | did_cancel_main_ = true; |
| 1601 | return new CancelTestJob(request, network_delegate); |
| 1602 | } |
| 1603 | if (cancel_then_restart_main_request_) { |
| 1604 | cancel_then_restart_main_request_ = false; |
| 1605 | did_cancel_then_restart_main_ = true; |
| 1606 | return new CancelThenRestartTestJob(request, network_delegate); |
| 1607 | } |
| 1608 | if (simulate_main_network_error_) { |
| 1609 | simulate_main_network_error_ = false; |
| 1610 | did_simulate_error_main_ = true; |
| 1611 | if (use_url_request_http_job_) { |
| 1612 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1613 | } |
| 1614 | // This job will result in error since the requested URL is not one of the |
| 1615 | // URLs supported by these tests. |
| 1616 | return new URLRequestTestJob(request, network_delegate, true); |
| 1617 | } |
| 1618 | if (!intercept_main_request_) |
| 1619 | return nullptr; |
| 1620 | intercept_main_request_ = false; |
| 1621 | did_intercept_main_ = true; |
| 1622 | URLRequestTestJob* job = new URLRequestTestJob(request, |
| 1623 | network_delegate, |
| 1624 | main_headers_, |
| 1625 | main_data_, |
| 1626 | true); |
| 1627 | job->set_load_timing_info(main_request_load_timing_info_); |
| 1628 | return job; |
| 1629 | } |
| 1630 | |
| 1631 | URLRequestJob* MaybeInterceptRedirect(URLRequest* request, |
| 1632 | NetworkDelegate* network_delegate, |
| 1633 | const GURL& location) const override { |
| 1634 | if (cancel_redirect_request_) { |
| 1635 | cancel_redirect_request_ = false; |
| 1636 | did_cancel_redirect_ = true; |
| 1637 | return new CancelTestJob(request, network_delegate); |
| 1638 | } |
| 1639 | if (!intercept_redirect_) |
| 1640 | return nullptr; |
| 1641 | intercept_redirect_ = false; |
| 1642 | did_intercept_redirect_ = true; |
| 1643 | if (use_url_request_http_job_) { |
| 1644 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1645 | } |
| 1646 | return new URLRequestTestJob(request, |
| 1647 | network_delegate, |
| 1648 | redirect_headers_, |
| 1649 | redirect_data_, |
| 1650 | true); |
| 1651 | } |
| 1652 | |
| 1653 | URLRequestJob* MaybeInterceptResponse( |
| 1654 | URLRequest* request, |
| 1655 | NetworkDelegate* network_delegate) const override { |
| 1656 | if (cancel_final_request_) { |
| 1657 | cancel_final_request_ = false; |
| 1658 | did_cancel_final_ = true; |
| 1659 | return new CancelTestJob(request, network_delegate); |
| 1660 | } |
| 1661 | if (!intercept_final_response_) |
| 1662 | return nullptr; |
| 1663 | intercept_final_response_ = false; |
| 1664 | did_intercept_final_ = true; |
| 1665 | if (use_url_request_http_job_) { |
| 1666 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1667 | } |
| 1668 | return new URLRequestTestJob(request, |
| 1669 | network_delegate, |
| 1670 | final_headers_, |
| 1671 | final_data_, |
| 1672 | true); |
| 1673 | } |
| 1674 | |
| 1675 | void set_intercept_main_request(bool intercept_main_request) { |
| 1676 | intercept_main_request_ = intercept_main_request; |
| 1677 | } |
| 1678 | |
| 1679 | void set_main_headers(const std::string& main_headers) { |
| 1680 | main_headers_ = main_headers; |
| 1681 | } |
| 1682 | |
| 1683 | void set_main_data(const std::string& main_data) { |
| 1684 | main_data_ = main_data; |
| 1685 | } |
| 1686 | |
| 1687 | void set_main_request_load_timing_info( |
| 1688 | const LoadTimingInfo& main_request_load_timing_info) { |
| 1689 | main_request_load_timing_info_ = main_request_load_timing_info; |
| 1690 | } |
| 1691 | |
| 1692 | void set_restart_main_request(bool restart_main_request) { |
| 1693 | restart_main_request_ = restart_main_request; |
| 1694 | } |
| 1695 | |
| 1696 | void set_cancel_main_request(bool cancel_main_request) { |
| 1697 | cancel_main_request_ = cancel_main_request; |
| 1698 | } |
| 1699 | |
| 1700 | void set_cancel_then_restart_main_request( |
| 1701 | bool cancel_then_restart_main_request) { |
| 1702 | cancel_then_restart_main_request_ = cancel_then_restart_main_request; |
| 1703 | } |
| 1704 | |
| 1705 | void set_simulate_main_network_error(bool simulate_main_network_error) { |
| 1706 | simulate_main_network_error_ = simulate_main_network_error; |
| 1707 | } |
| 1708 | |
| 1709 | void set_intercept_redirect(bool intercept_redirect) { |
| 1710 | intercept_redirect_ = intercept_redirect; |
| 1711 | } |
| 1712 | |
| 1713 | void set_redirect_headers(const std::string& redirect_headers) { |
| 1714 | redirect_headers_ = redirect_headers; |
| 1715 | } |
| 1716 | |
| 1717 | void set_redirect_data(const std::string& redirect_data) { |
| 1718 | redirect_data_ = redirect_data; |
| 1719 | } |
| 1720 | |
| 1721 | void set_cancel_redirect_request(bool cancel_redirect_request) { |
| 1722 | cancel_redirect_request_ = cancel_redirect_request; |
| 1723 | } |
| 1724 | |
| 1725 | void set_intercept_final_response(bool intercept_final_response) { |
| 1726 | intercept_final_response_ = intercept_final_response; |
| 1727 | } |
| 1728 | |
| 1729 | void set_final_headers(const std::string& final_headers) { |
| 1730 | final_headers_ = final_headers; |
| 1731 | } |
| 1732 | |
| 1733 | void set_final_data(const std::string& final_data) { |
| 1734 | final_data_ = final_data; |
| 1735 | } |
| 1736 | |
| 1737 | void set_cancel_final_request(bool cancel_final_request) { |
| 1738 | cancel_final_request_ = cancel_final_request; |
| 1739 | } |
| 1740 | |
| 1741 | void set_use_url_request_http_job(bool use_url_request_http_job) { |
| 1742 | use_url_request_http_job_ = use_url_request_http_job; |
| 1743 | } |
| 1744 | |
| 1745 | bool did_intercept_main() const { |
| 1746 | return did_intercept_main_; |
| 1747 | } |
| 1748 | |
| 1749 | bool did_restart_main() const { |
| 1750 | return did_restart_main_; |
| 1751 | } |
| 1752 | |
| 1753 | bool did_cancel_main() const { |
| 1754 | return did_cancel_main_; |
| 1755 | } |
| 1756 | |
| 1757 | bool did_cancel_then_restart_main() const { |
| 1758 | return did_cancel_then_restart_main_; |
| 1759 | } |
| 1760 | |
| 1761 | bool did_simulate_error_main() const { |
| 1762 | return did_simulate_error_main_; |
| 1763 | } |
| 1764 | |
| 1765 | bool did_intercept_redirect() const { |
| 1766 | return did_intercept_redirect_; |
| 1767 | } |
| 1768 | |
| 1769 | bool did_cancel_redirect() const { |
| 1770 | return did_cancel_redirect_; |
| 1771 | } |
| 1772 | |
| 1773 | bool did_intercept_final() const { |
| 1774 | return did_intercept_final_; |
| 1775 | } |
| 1776 | |
| 1777 | bool did_cancel_final() const { |
| 1778 | return did_cancel_final_; |
| 1779 | } |
| 1780 | |
| 1781 | private: |
| 1782 | // Indicate whether to intercept the main request, and if so specify the |
| 1783 | // response to return and the LoadTimingInfo to use. |
| 1784 | mutable bool intercept_main_request_; |
| 1785 | mutable std::string main_headers_; |
| 1786 | mutable std::string main_data_; |
| 1787 | mutable LoadTimingInfo main_request_load_timing_info_; |
| 1788 | |
| 1789 | // These indicate actions that can be taken within MaybeInterceptRequest. |
| 1790 | mutable bool restart_main_request_; |
| 1791 | mutable bool cancel_main_request_; |
| 1792 | mutable bool cancel_then_restart_main_request_; |
| 1793 | mutable bool simulate_main_network_error_; |
| 1794 | |
| 1795 | // Indicate whether to intercept redirects, and if so specify the response to |
| 1796 | // return. |
| 1797 | mutable bool intercept_redirect_; |
| 1798 | mutable std::string redirect_headers_; |
| 1799 | mutable std::string redirect_data_; |
| 1800 | |
| 1801 | // Cancel the request within MaybeInterceptRedirect. |
| 1802 | mutable bool cancel_redirect_request_; |
| 1803 | |
| 1804 | // Indicate whether to intercept the final response, and if so specify the |
| 1805 | // response to return. |
| 1806 | mutable bool intercept_final_response_; |
| 1807 | mutable std::string final_headers_; |
| 1808 | mutable std::string final_data_; |
| 1809 | |
| 1810 | // Cancel the final request within MaybeInterceptResponse. |
| 1811 | mutable bool cancel_final_request_; |
| 1812 | |
| 1813 | // Instruct the interceptor to use a real URLRequestHTTPJob. |
| 1814 | mutable bool use_url_request_http_job_; |
| 1815 | |
| 1816 | // These indicate if the interceptor did something or not. |
| 1817 | mutable bool did_intercept_main_; |
| 1818 | mutable bool did_restart_main_; |
| 1819 | mutable bool did_cancel_main_; |
| 1820 | mutable bool did_cancel_then_restart_main_; |
| 1821 | mutable bool did_simulate_error_main_; |
| 1822 | mutable bool did_intercept_redirect_; |
| 1823 | mutable bool did_cancel_redirect_; |
| 1824 | mutable bool did_intercept_final_; |
| 1825 | mutable bool did_cancel_final_; |
| 1826 | }; |
| 1827 | |
| 1828 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
| 1829 | class URLRequestInterceptorTest : public URLRequestTest { |
| 1830 | public: |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1831 | URLRequestInterceptorTest() : URLRequestTest(), interceptor_(nullptr) {} |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1832 | |
| 1833 | ~URLRequestInterceptorTest() override { |
| 1834 | // URLRequestJobs may post clean-up tasks on destruction. |
| 1835 | base::RunLoop().RunUntilIdle(); |
| 1836 | } |
| 1837 | |
| 1838 | void SetUpFactory() override { |
| 1839 | interceptor_ = new MockURLRequestInterceptor(); |
| 1840 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1841 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | MockURLRequestInterceptor* interceptor() const { |
| 1845 | return interceptor_; |
| 1846 | } |
| 1847 | |
| 1848 | private: |
| 1849 | MockURLRequestInterceptor* interceptor_; |
| 1850 | }; |
| 1851 | |
| 1852 | TEST_F(URLRequestInterceptorTest, Intercept) { |
| 1853 | // Intercept the main request and respond with a simple response. |
| 1854 | interceptor()->set_intercept_main_request(true); |
| 1855 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1856 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1857 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1858 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1859 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1860 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1861 | base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); |
| 1862 | base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); |
| 1863 | base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); |
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 1864 | req->SetUserData(&user_data0, base::WrapUnique(user_data0)); |
avi | 0d99147 | 2017-04-27 07:04:04 | [diff] [blame] | 1865 | req->SetUserData(&user_data1, base::WrapUnique(user_data1)); |
| 1866 | req->SetUserData(&user_data2, base::WrapUnique(user_data2)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1867 | req->set_method("GET"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1868 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1869 | d.RunUntilComplete(); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1870 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1871 | // Make sure we can retrieve our specific user data. |
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 1872 | EXPECT_EQ(user_data0, req->GetUserData(&user_data0)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1873 | EXPECT_EQ(user_data1, req->GetUserData(&user_data1)); |
| 1874 | EXPECT_EQ(user_data2, req->GetUserData(&user_data2)); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1875 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1876 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1877 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1878 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1879 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1880 | EXPECT_EQ(1, d.response_started_count()); |
| 1881 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1882 | } |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1883 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1884 | TEST_F(URLRequestInterceptorTest, InterceptRedirect) { |
| 1885 | // Intercept the main request and respond with a redirect. |
| 1886 | interceptor()->set_intercept_main_request(true); |
| 1887 | interceptor()->set_main_headers( |
| 1888 | MockURLRequestInterceptor::redirect_headers()); |
| 1889 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 1890 | |
| 1891 | // Intercept that redirect and respond with a final OK response. |
| 1892 | interceptor()->set_intercept_redirect(true); |
| 1893 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 1894 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 1895 | |
| 1896 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1897 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1898 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1899 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1900 | req->set_method("GET"); |
| 1901 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1902 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1903 | |
| 1904 | // Check that the interceptor got called as expected. |
| 1905 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1906 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 1907 | |
| 1908 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1909 | int status = d.request_status(); |
| 1910 | EXPECT_EQ(OK, status); |
| 1911 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1912 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1913 | |
| 1914 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1915 | EXPECT_EQ(1, d.response_started_count()); |
| 1916 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1917 | } |
| 1918 | |
| 1919 | TEST_F(URLRequestInterceptorTest, InterceptServerError) { |
| 1920 | // Intercept the main request to generate a server error response. |
| 1921 | interceptor()->set_intercept_main_request(true); |
| 1922 | interceptor()->set_main_headers(MockURLRequestInterceptor::error_headers()); |
| 1923 | interceptor()->set_main_data(MockURLRequestInterceptor::error_data()); |
| 1924 | |
| 1925 | // Intercept that error and respond with an OK response. |
| 1926 | interceptor()->set_intercept_final_response(true); |
| 1927 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1928 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1929 | |
| 1930 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1931 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1932 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1933 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1934 | req->set_method("GET"); |
| 1935 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1936 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1937 | |
| 1938 | // Check that the interceptor got called as expected. |
| 1939 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1940 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1941 | |
| 1942 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1943 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1944 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1945 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1946 | EXPECT_EQ(1, d.response_started_count()); |
| 1947 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1948 | } |
| 1949 | |
| 1950 | TEST_F(URLRequestInterceptorTest, InterceptNetworkError) { |
| 1951 | // Intercept the main request to simulate a network error. |
| 1952 | interceptor()->set_simulate_main_network_error(true); |
| 1953 | |
| 1954 | // Intercept that error and respond with an OK response. |
| 1955 | interceptor()->set_intercept_final_response(true); |
| 1956 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1957 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1958 | |
| 1959 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1960 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1961 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1962 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1963 | req->set_method("GET"); |
| 1964 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1965 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1966 | |
| 1967 | // Check that the interceptor got called as expected. |
| 1968 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 1969 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1970 | |
| 1971 | // Check that we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1972 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1973 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1974 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1975 | EXPECT_EQ(1, d.response_started_count()); |
| 1976 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1977 | } |
| 1978 | |
| 1979 | TEST_F(URLRequestInterceptorTest, InterceptRestartRequired) { |
| 1980 | // Restart the main request. |
| 1981 | interceptor()->set_restart_main_request(true); |
| 1982 | |
| 1983 | // then intercept the new main request and respond with an OK response |
| 1984 | interceptor()->set_intercept_main_request(true); |
| 1985 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1986 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
| 1987 | |
| 1988 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1989 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1990 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1991 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1992 | req->set_method("GET"); |
| 1993 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1994 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1995 | |
| 1996 | // Check that the interceptor got called as expected. |
| 1997 | EXPECT_TRUE(interceptor()->did_restart_main()); |
| 1998 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1999 | |
| 2000 | // Check that we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2001 | int status = d.request_status(); |
| 2002 | EXPECT_EQ(OK, status); |
| 2003 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2004 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 2005 | |
| 2006 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 2007 | EXPECT_EQ(1, d.response_started_count()); |
| 2008 | EXPECT_EQ(0, d.received_redirect_count()); |
| 2009 | } |
| 2010 | |
| 2011 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelMain) { |
| 2012 | // Intercept the main request and cancel from within the restarted job. |
| 2013 | interceptor()->set_cancel_main_request(true); |
| 2014 | |
| 2015 | // Set up to intercept the final response and override it with an OK response. |
| 2016 | interceptor()->set_intercept_final_response(true); |
| 2017 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2018 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 2019 | |
| 2020 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2021 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2022 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2023 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2024 | req->set_method("GET"); |
| 2025 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2026 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2027 | |
| 2028 | // Check that the interceptor got called as expected. |
| 2029 | EXPECT_TRUE(interceptor()->did_cancel_main()); |
| 2030 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2031 | |
| 2032 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2033 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelRedirect) { |
| 2037 | // Intercept the main request and respond with a redirect. |
| 2038 | interceptor()->set_intercept_main_request(true); |
| 2039 | interceptor()->set_main_headers( |
| 2040 | MockURLRequestInterceptor::redirect_headers()); |
| 2041 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 2042 | |
| 2043 | // Intercept the redirect and cancel from within that job. |
| 2044 | interceptor()->set_cancel_redirect_request(true); |
| 2045 | |
| 2046 | // Set up to intercept the final response and override it with an OK response. |
| 2047 | interceptor()->set_intercept_final_response(true); |
| 2048 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2049 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 2050 | |
| 2051 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2052 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2053 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2054 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2055 | req->set_method("GET"); |
| 2056 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2057 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2058 | |
| 2059 | // Check that the interceptor got called as expected. |
| 2060 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 2061 | EXPECT_TRUE(interceptor()->did_cancel_redirect()); |
| 2062 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2063 | |
| 2064 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2065 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelFinal) { |
| 2069 | // Intercept the main request to simulate a network error. |
| 2070 | interceptor()->set_simulate_main_network_error(true); |
| 2071 | |
| 2072 | // Set up to intercept final the response and cancel from within that job. |
| 2073 | interceptor()->set_cancel_final_request(true); |
| 2074 | |
| 2075 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2076 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2077 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2078 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2079 | req->set_method("GET"); |
| 2080 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2081 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2082 | |
| 2083 | // Check that the interceptor got called as expected. |
| 2084 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 2085 | EXPECT_TRUE(interceptor()->did_cancel_final()); |
| 2086 | |
| 2087 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2088 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2089 | } |
| 2090 | |
| 2091 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelInRestart) { |
| 2092 | // Intercept the main request and cancel then restart from within that job. |
| 2093 | interceptor()->set_cancel_then_restart_main_request(true); |
| 2094 | |
| 2095 | // Set up to intercept the final response and override it with an OK response. |
| 2096 | interceptor()->set_intercept_final_response(true); |
bengr | b50d631e | 2014-11-17 22:50:50 | [diff] [blame] | 2097 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2098 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2099 | |
| 2100 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2101 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2102 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2103 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2104 | req->set_method("GET"); |
| 2105 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2106 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2107 | |
| 2108 | // Check that the interceptor got called as expected. |
| 2109 | EXPECT_TRUE(interceptor()->did_cancel_then_restart_main()); |
| 2110 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2111 | |
| 2112 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2113 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2114 | } |
| 2115 | |
| 2116 | // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not |
| 2117 | // reused. |connect_time_flags| is used to indicate if there should be dns |
| 2118 | // or SSL times, and |used_proxy| is used for proxy times. |
| 2119 | LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now, |
| 2120 | int connect_time_flags, |
| 2121 | bool used_proxy) { |
| 2122 | LoadTimingInfo load_timing; |
| 2123 | load_timing.socket_log_id = 1; |
| 2124 | |
| 2125 | if (used_proxy) { |
| 2126 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 2127 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 2128 | } |
| 2129 | |
| 2130 | LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing; |
| 2131 | if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) { |
| 2132 | connect_timing.dns_start = now + base::TimeDelta::FromDays(3); |
| 2133 | connect_timing.dns_end = now + base::TimeDelta::FromDays(4); |
| 2134 | } |
| 2135 | connect_timing.connect_start = now + base::TimeDelta::FromDays(5); |
| 2136 | if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) { |
| 2137 | connect_timing.ssl_start = now + base::TimeDelta::FromDays(6); |
| 2138 | connect_timing.ssl_end = now + base::TimeDelta::FromDays(7); |
| 2139 | } |
| 2140 | connect_timing.connect_end = now + base::TimeDelta::FromDays(8); |
| 2141 | |
| 2142 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 2143 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 2144 | load_timing.receive_headers_start = now + base::TimeDelta::FromDays(11); |
| 2145 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(12); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2146 | return load_timing; |
| 2147 | } |
| 2148 | |
| 2149 | // Same as above, but in the case of a reused socket. |
| 2150 | LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now, |
| 2151 | bool used_proxy) { |
| 2152 | LoadTimingInfo load_timing; |
| 2153 | load_timing.socket_log_id = 1; |
| 2154 | load_timing.socket_reused = true; |
| 2155 | |
| 2156 | if (used_proxy) { |
| 2157 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 2158 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 2159 | } |
| 2160 | |
| 2161 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 2162 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 2163 | load_timing.receive_headers_start = now + base::TimeDelta::FromDays(11); |
| 2164 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(12); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2165 | return load_timing; |
| 2166 | } |
| 2167 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2168 | LoadTimingInfo RunURLRequestInterceptorLoadTimingTest( |
| 2169 | const LoadTimingInfo& job_load_timing, |
| 2170 | const URLRequestContext& context, |
| 2171 | MockURLRequestInterceptor* interceptor) { |
| 2172 | interceptor->set_intercept_main_request(true); |
| 2173 | interceptor->set_main_request_load_timing_info(job_load_timing); |
| 2174 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2175 | std::unique_ptr<URLRequest> req( |
| 2176 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 2177 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2178 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2179 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2180 | |
| 2181 | LoadTimingInfo resulting_load_timing; |
| 2182 | req->GetLoadTimingInfo(&resulting_load_timing); |
| 2183 | |
| 2184 | // None of these should be modified by the URLRequest. |
| 2185 | EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); |
| 2186 | EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); |
| 2187 | EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); |
| 2188 | EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 2189 | EXPECT_EQ(job_load_timing.receive_headers_start, |
| 2190 | resulting_load_timing.receive_headers_start); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2191 | EXPECT_EQ(job_load_timing.receive_headers_end, |
| 2192 | resulting_load_timing.receive_headers_end); |
caseq | e8340bc9 | 2016-04-20 00:02:57 | [diff] [blame] | 2193 | EXPECT_EQ(job_load_timing.push_start, resulting_load_timing.push_start); |
| 2194 | EXPECT_EQ(job_load_timing.push_end, resulting_load_timing.push_end); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2195 | |
| 2196 | return resulting_load_timing; |
| 2197 | } |
| 2198 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2199 | // Basic test that the intercept + load timing tests work. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2200 | TEST_F(URLRequestInterceptorTest, InterceptLoadTiming) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2201 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2202 | LoadTimingInfo job_load_timing = |
| 2203 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); |
| 2204 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2205 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2206 | RunURLRequestInterceptorLoadTimingTest( |
| 2207 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2208 | |
| 2209 | // Nothing should have been changed by the URLRequest. |
| 2210 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 2211 | load_timing_result.proxy_resolve_start); |
| 2212 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 2213 | load_timing_result.proxy_resolve_end); |
| 2214 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 2215 | load_timing_result.connect_timing.dns_start); |
| 2216 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 2217 | load_timing_result.connect_timing.dns_end); |
| 2218 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 2219 | load_timing_result.connect_timing.connect_start); |
| 2220 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 2221 | load_timing_result.connect_timing.connect_end); |
| 2222 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 2223 | load_timing_result.connect_timing.ssl_start); |
| 2224 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 2225 | load_timing_result.connect_timing.ssl_end); |
| 2226 | |
| 2227 | // Redundant sanity check. |
| 2228 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2229 | } |
| 2230 | |
| 2231 | // Another basic test, with proxy and SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2232 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2233 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2234 | LoadTimingInfo job_load_timing = |
| 2235 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); |
| 2236 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2237 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2238 | RunURLRequestInterceptorLoadTimingTest( |
| 2239 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2240 | |
| 2241 | // Nothing should have been changed by the URLRequest. |
| 2242 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 2243 | load_timing_result.proxy_resolve_start); |
| 2244 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 2245 | load_timing_result.proxy_resolve_end); |
| 2246 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 2247 | load_timing_result.connect_timing.dns_start); |
| 2248 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 2249 | load_timing_result.connect_timing.dns_end); |
| 2250 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 2251 | load_timing_result.connect_timing.connect_start); |
| 2252 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 2253 | load_timing_result.connect_timing.connect_end); |
| 2254 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 2255 | load_timing_result.connect_timing.ssl_start); |
| 2256 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 2257 | load_timing_result.connect_timing.ssl_end); |
| 2258 | |
| 2259 | // Redundant sanity check. |
| 2260 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2261 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 2262 | } |
| 2263 | |
| 2264 | // Make sure that URLRequest correctly adjusts proxy times when they're before |
| 2265 | // |request_start|, due to already having a connected socket. This happens in |
[email protected] | cf4cae3 | 2014-05-27 00:39:10 | [diff] [blame] | 2266 | // the case of reusing a SPDY session. The connected socket is not considered |
| 2267 | // reused in this test (May be a preconnect). |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2268 | // |
| 2269 | // 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] | 2270 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyProxyResolution) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2271 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2272 | LoadTimingInfo job_load_timing = |
| 2273 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); |
| 2274 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); |
| 2275 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); |
| 2276 | job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); |
| 2277 | job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); |
| 2278 | job_load_timing.connect_timing.connect_start = |
| 2279 | now - base::TimeDelta::FromDays(2); |
| 2280 | job_load_timing.connect_timing.connect_end = |
| 2281 | now - base::TimeDelta::FromDays(1); |
| 2282 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2283 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2284 | RunURLRequestInterceptorLoadTimingTest( |
| 2285 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2286 | |
| 2287 | // Proxy times, connect times, and DNS times should all be replaced with |
| 2288 | // request_start. |
| 2289 | EXPECT_EQ(load_timing_result.request_start, |
| 2290 | load_timing_result.proxy_resolve_start); |
| 2291 | EXPECT_EQ(load_timing_result.request_start, |
| 2292 | load_timing_result.proxy_resolve_end); |
| 2293 | EXPECT_EQ(load_timing_result.request_start, |
| 2294 | load_timing_result.connect_timing.dns_start); |
| 2295 | EXPECT_EQ(load_timing_result.request_start, |
| 2296 | load_timing_result.connect_timing.dns_end); |
| 2297 | EXPECT_EQ(load_timing_result.request_start, |
| 2298 | load_timing_result.connect_timing.connect_start); |
| 2299 | EXPECT_EQ(load_timing_result.request_start, |
| 2300 | load_timing_result.connect_timing.connect_end); |
| 2301 | |
| 2302 | // Other times should have been left null. |
| 2303 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2304 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 2305 | } |
| 2306 | |
| 2307 | // Same as above, but in the reused case. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2308 | TEST_F(URLRequestInterceptorTest, |
| 2309 | InterceptLoadTimingEarlyProxyResolutionReused) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2310 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2311 | LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); |
| 2312 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); |
| 2313 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); |
| 2314 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2315 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2316 | RunURLRequestInterceptorLoadTimingTest( |
| 2317 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2318 | |
| 2319 | // Proxy times and connect times should all be replaced with request_start. |
| 2320 | EXPECT_EQ(load_timing_result.request_start, |
| 2321 | load_timing_result.proxy_resolve_start); |
| 2322 | EXPECT_EQ(load_timing_result.request_start, |
| 2323 | load_timing_result.proxy_resolve_end); |
| 2324 | |
| 2325 | // Other times should have been left null. |
| 2326 | TestLoadTimingReusedWithProxy(load_timing_result); |
| 2327 | } |
| 2328 | |
| 2329 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2330 | // |request_start|, due to reusing a connected socket. The connected socket is |
| 2331 | // not considered reused in this test (May be a preconnect). |
| 2332 | // |
| 2333 | // To mix things up, the request has SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2334 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnect) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2335 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2336 | LoadTimingInfo job_load_timing = |
| 2337 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); |
| 2338 | job_load_timing.connect_timing.connect_start = |
| 2339 | now - base::TimeDelta::FromDays(1); |
| 2340 | job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); |
| 2341 | job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); |
| 2342 | job_load_timing.connect_timing.connect_end = |
| 2343 | now - base::TimeDelta::FromDays(4); |
| 2344 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2345 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2346 | RunURLRequestInterceptorLoadTimingTest( |
| 2347 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2348 | |
| 2349 | // Connect times, and SSL times should be replaced with request_start. |
| 2350 | EXPECT_EQ(load_timing_result.request_start, |
| 2351 | load_timing_result.connect_timing.connect_start); |
| 2352 | EXPECT_EQ(load_timing_result.request_start, |
| 2353 | load_timing_result.connect_timing.ssl_start); |
| 2354 | EXPECT_EQ(load_timing_result.request_start, |
| 2355 | load_timing_result.connect_timing.ssl_end); |
| 2356 | EXPECT_EQ(load_timing_result.request_start, |
| 2357 | load_timing_result.connect_timing.connect_end); |
| 2358 | |
| 2359 | // Other times should have been left null. |
| 2360 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); |
| 2361 | } |
| 2362 | |
| 2363 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2364 | // |request_start|, due to reusing a connected socket in the case that there |
| 2365 | // are also proxy times. The connected socket is not considered reused in this |
| 2366 | // test (May be a preconnect). |
| 2367 | // |
| 2368 | // In this test, there are no SSL or DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2369 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnectWithProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2370 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2371 | LoadTimingInfo job_load_timing = |
| 2372 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); |
| 2373 | job_load_timing.connect_timing.connect_start = |
| 2374 | now - base::TimeDelta::FromDays(1); |
| 2375 | job_load_timing.connect_timing.connect_end = |
| 2376 | now - base::TimeDelta::FromDays(2); |
| 2377 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2378 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2379 | RunURLRequestInterceptorLoadTimingTest( |
| 2380 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2381 | |
| 2382 | // Connect times should be replaced with proxy_resolve_end. |
| 2383 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2384 | load_timing_result.connect_timing.connect_start); |
| 2385 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2386 | load_timing_result.connect_timing.connect_end); |
| 2387 | |
| 2388 | // Other times should have been left null. |
| 2389 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2390 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 2391 | } |
| 2392 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2393 | // Check that two different URL requests have different identifiers. |
| 2394 | TEST_F(URLRequestTest, Identifiers) { |
| 2395 | TestDelegate d; |
| 2396 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2397 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2398 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2399 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2400 | std::unique_ptr<URLRequest> other_req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2401 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2402 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2403 | |
mmenke | 19378d2 | 2014-09-09 04:12:59 | [diff] [blame] | 2404 | ASSERT_NE(req->identifier(), other_req->identifier()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2405 | } |
| 2406 | |
blundell | b8163592f | 2015-12-16 14:22:42 | [diff] [blame] | 2407 | #if defined(OS_IOS) |
| 2408 | // TODO(droger): Check that a failure to connect to the proxy is reported to |
| 2409 | // the network delegate. crbug.com/496743 |
| 2410 | #define MAYBE_NetworkDelegateProxyError DISABLED_NetworkDelegateProxyError |
| 2411 | #else |
| 2412 | #define MAYBE_NetworkDelegateProxyError NetworkDelegateProxyError |
| 2413 | #endif |
| 2414 | TEST_F(URLRequestTest, MAYBE_NetworkDelegateProxyError) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2415 | MockHostResolver host_resolver; |
| 2416 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 2417 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2418 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2419 | TestURLRequestContextWithProxy context("myproxy:70", &network_delegate); |
| 2420 | |
| 2421 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2422 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2423 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2424 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2425 | req->set_method("GET"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2426 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2427 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2428 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2429 | |
| 2430 | // Check we see a failed request. |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 2431 | // The proxy server should be set before failure. |
| 2432 | EXPECT_EQ(ProxyServer::FromPacString("PROXY myproxy:70"), |
| 2433 | req->proxy_server()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2434 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2435 | |
| 2436 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2437 | EXPECT_THAT(network_delegate.last_error(), |
| 2438 | IsError(ERR_PROXY_CONNECTION_FAILED)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2439 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2440 | } |
| 2441 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2442 | // Make sure that NetworkDelegate::NotifyCompleted is called if |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2443 | // content is empty. |
| 2444 | TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) { |
| 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("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2448 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2449 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2450 | EXPECT_EQ("", d.data_received()); |
| 2451 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 2452 | } |
| 2453 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2454 | // Make sure that SetPriority actually sets the URLRequest's priority |
| 2455 | // correctly, both before and after start. |
| 2456 | TEST_F(URLRequestTest, SetPriorityBasic) { |
| 2457 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2458 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2459 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2460 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2461 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2462 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2463 | req->SetPriority(LOW); |
| 2464 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2465 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2466 | req->Start(); |
| 2467 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2468 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2469 | req->SetPriority(MEDIUM); |
| 2470 | EXPECT_EQ(MEDIUM, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | // Make sure that URLRequest calls SetPriority on a job before calling |
| 2474 | // Start on it. |
| 2475 | TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) { |
| 2476 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2477 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2478 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2479 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2480 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2481 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2482 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2483 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2484 | req.get(), &default_network_delegate_, &job_priority)); |
| 2485 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
| 2486 | EXPECT_EQ(DEFAULT_PRIORITY, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2487 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2488 | req->SetPriority(LOW); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2489 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2490 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2491 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2492 | } |
| 2493 | |
| 2494 | // Make sure that URLRequest passes on its priority updates to its |
| 2495 | // job. |
| 2496 | TEST_F(URLRequestTest, SetJobPriority) { |
| 2497 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2498 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2499 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2500 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2501 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2502 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2503 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2504 | req.get(), &default_network_delegate_, &job_priority)); |
| 2505 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2506 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2507 | req->SetPriority(LOW); |
| 2508 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2509 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2510 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2511 | req->SetPriority(MEDIUM); |
| 2512 | EXPECT_EQ(MEDIUM, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2513 | EXPECT_EQ(MEDIUM, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2514 | } |
| 2515 | |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2516 | // Setting the IGNORE_LIMITS load flag should be okay if the priority |
| 2517 | // is MAXIMUM_PRIORITY. |
| 2518 | TEST_F(URLRequestTest, PriorityIgnoreLimits) { |
| 2519 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2520 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2521 | GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY, &d, |
| 2522 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2523 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2524 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2525 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2526 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2527 | req.get(), &default_network_delegate_, &job_priority)); |
| 2528 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2529 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2530 | req->SetLoadFlags(LOAD_IGNORE_LIMITS); |
| 2531 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2532 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2533 | req->SetPriority(MAXIMUM_PRIORITY); |
| 2534 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2535 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2536 | req->Start(); |
| 2537 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2538 | EXPECT_EQ(MAXIMUM_PRIORITY, job_priority); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2539 | } |
| 2540 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2541 | namespace { |
| 2542 | |
| 2543 | // Less verbose way of running a simple testserver for the tests below. |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2544 | class HttpTestServer : public EmbeddedTestServer { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2545 | public: |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2546 | explicit HttpTestServer(const base::FilePath& document_root) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2547 | AddDefaultHandlers(document_root); |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2548 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2549 | |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2550 | HttpTestServer() { AddDefaultHandlers(base::FilePath()); } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2551 | }; |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2552 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2553 | } // namespace |
| 2554 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2555 | TEST_F(URLRequestTest, DelayedCookieCallback) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2556 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2557 | ASSERT_TRUE(test_server.Start()); |
| 2558 | |
| 2559 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2560 | std::unique_ptr<DelayedCookieMonster> delayed_cm(new DelayedCookieMonster()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2561 | context.set_cookie_store(delayed_cm.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2562 | |
| 2563 | // Set up a cookie. |
| 2564 | { |
| 2565 | TestNetworkDelegate network_delegate; |
| 2566 | context.set_network_delegate(&network_delegate); |
| 2567 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2568 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2569 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2570 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2571 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2572 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2573 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2574 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2575 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2576 | } |
| 2577 | |
| 2578 | // Verify that the cookie is set. |
| 2579 | { |
| 2580 | TestNetworkDelegate network_delegate; |
| 2581 | context.set_network_delegate(&network_delegate); |
| 2582 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2583 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2584 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2585 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2586 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2587 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2588 | |
| 2589 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2590 | != std::string::npos); |
| 2591 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2592 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2593 | } |
| 2594 | } |
| 2595 | |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 2596 | class FilteringTestLayeredNetworkDelegate : public LayeredNetworkDelegate { |
| 2597 | public: |
| 2598 | FilteringTestLayeredNetworkDelegate( |
| 2599 | std::unique_ptr<NetworkDelegate> network_delegate) |
| 2600 | : LayeredNetworkDelegate(std::move((network_delegate))), |
| 2601 | set_cookie_called_count_(0), |
| 2602 | blocked_set_cookie_count_(0) {} |
| 2603 | ~FilteringTestLayeredNetworkDelegate() override = default; |
| 2604 | |
| 2605 | bool OnCanSetCookieInternal(const URLRequest& request, |
| 2606 | const net::CanonicalCookie& cookie, |
| 2607 | CookieOptions* options, |
| 2608 | bool allowed_from_caller) override { |
| 2609 | // Filter out cookies with the same name as |cookie_name_filter_| and |
| 2610 | // combine with |allowed_from_caller|. |
| 2611 | bool allowed = |
| 2612 | allowed_from_caller && !(cookie.Name() == cookie_name_filter_); |
| 2613 | |
| 2614 | ++set_cookie_called_count_; |
| 2615 | |
| 2616 | if (!allowed) |
| 2617 | ++blocked_set_cookie_count_; |
| 2618 | |
| 2619 | return allowed; |
| 2620 | } |
| 2621 | |
| 2622 | void SetCookieFilter(std::string filter) { |
| 2623 | cookie_name_filter_ = std::move(filter); |
| 2624 | } |
| 2625 | |
| 2626 | int set_cookie_called_count() { return set_cookie_called_count_; } |
| 2627 | |
| 2628 | int blocked_set_cookie_count() { return blocked_set_cookie_count_; } |
| 2629 | |
| 2630 | void ResetSetCookieCalledCount() { set_cookie_called_count_ = 0; } |
| 2631 | |
| 2632 | void ResetBlockedSetCookieCount() { blocked_set_cookie_count_ = 0; } |
| 2633 | |
| 2634 | private: |
| 2635 | std::string cookie_name_filter_; |
| 2636 | int set_cookie_called_count_; |
| 2637 | int blocked_set_cookie_count_; |
| 2638 | }; |
| 2639 | |
| 2640 | TEST_F(URLRequestTest, DelayedCookieCallbackAsync) { |
| 2641 | HttpTestServer test_server; |
| 2642 | ASSERT_TRUE(test_server.Start()); |
| 2643 | |
| 2644 | TestURLRequestContext async_context; |
| 2645 | std::unique_ptr<DelayedCookieMonster> delayed_cm = |
| 2646 | std::make_unique<DelayedCookieMonster>(); |
| 2647 | async_context.set_cookie_store(delayed_cm.get()); |
| 2648 | FilteringTestLayeredNetworkDelegate async_filter_network_delegate( |
| 2649 | std::make_unique<TestNetworkDelegate>()); |
| 2650 | async_filter_network_delegate.SetCookieFilter("CookieBlockedOnCanGetCookie"); |
| 2651 | async_context.set_network_delegate(&async_filter_network_delegate); |
| 2652 | TestDelegate async_delegate; |
| 2653 | |
| 2654 | TestURLRequestContext sync_context; |
| 2655 | std::unique_ptr<CookieMonster> cm = |
Nick Harper | 57142b1c | 2019-03-14 21:03:59 | [diff] [blame] | 2656 | std::make_unique<CookieMonster>(nullptr, nullptr); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 2657 | sync_context.set_cookie_store(cm.get()); |
| 2658 | FilteringTestLayeredNetworkDelegate sync_filter_network_delegate( |
| 2659 | std::make_unique<TestNetworkDelegate>()); |
| 2660 | sync_filter_network_delegate.SetCookieFilter("CookieBlockedOnCanGetCookie"); |
| 2661 | sync_context.set_network_delegate(&sync_filter_network_delegate); |
| 2662 | TestDelegate sync_delegate; |
| 2663 | |
| 2664 | // Add a secure cookie so we can try to set an insecure cookie and have |
| 2665 | // SetCanonicalCookie (and therefore SetCookieWithOptions) fail. |
| 2666 | GURL::Replacements replace_scheme; |
| 2667 | replace_scheme.SetSchemeStr("https"); |
| 2668 | GURL url = test_server.base_url().ReplaceComponents(replace_scheme); |
| 2669 | |
| 2670 | delayed_cm->SetCookieWithOptionsAsync(url, "AlreadySetCookie=1;Secure", |
| 2671 | CookieOptions(), |
| 2672 | CookieStore::SetCookiesCallback()); |
| 2673 | cm->SetCookieWithOptionsAsync(url, "AlreadySetCookie=1;Secure", |
| 2674 | CookieOptions(), |
| 2675 | CookieStore::SetCookiesCallback()); |
| 2676 | |
| 2677 | std::vector<std::string> cookie_lines( |
| 2678 | {// Fails on CanonicalCookie::Create for trying to create a secure cookie |
| 2679 | // on an insecure host. |
| 2680 | "CookieNotSet=1;Secure", |
| 2681 | // Fail in FilteringTestLayeredNetworkDelegate::CanGetCookie. |
| 2682 | "CookieBlockedOnCanGetCookie=1", |
| 2683 | // Fails in SetCanonicalCookie for trying to overwrite a secure cookie |
| 2684 | // with an insecure cookie. |
| 2685 | "AlreadySetCookie=1", |
| 2686 | // Succeeds and added cookie to store. Delayed (which makes the callback |
| 2687 | // run asynchronously) in DelayedCookieMonster. |
| 2688 | "CookieSet=1"}); |
| 2689 | |
| 2690 | for (auto first_cookie_line : cookie_lines) { |
| 2691 | for (auto second_cookie_line : cookie_lines) { |
| 2692 | // Run with the delayed cookie monster. |
| 2693 | std::unique_ptr<URLRequest> request = async_context.CreateRequest( |
| 2694 | test_server.GetURL("/set-cookie?" + first_cookie_line + "&" + |
| 2695 | second_cookie_line), |
| 2696 | DEFAULT_PRIORITY, &async_delegate, TRAFFIC_ANNOTATION_FOR_TESTS); |
| 2697 | |
| 2698 | request->Start(); |
| 2699 | async_delegate.RunUntilComplete(); |
| 2700 | EXPECT_THAT(async_delegate.request_status(), IsOk()); |
| 2701 | |
| 2702 | // Run with the regular cookie monster. |
| 2703 | request = sync_context.CreateRequest( |
| 2704 | test_server.GetURL("/set-cookie?" + first_cookie_line + "&" + |
| 2705 | second_cookie_line), |
| 2706 | DEFAULT_PRIORITY, &sync_delegate, TRAFFIC_ANNOTATION_FOR_TESTS); |
| 2707 | |
| 2708 | request->Start(); |
| 2709 | sync_delegate.RunUntilComplete(); |
| 2710 | EXPECT_THAT(sync_delegate.request_status(), IsOk()); |
| 2711 | |
| 2712 | int expected_set_cookie_count = 0; |
| 2713 | int expected_blocked_cookie_count = 0; |
| 2714 | |
| 2715 | if (first_cookie_line != "CookieNotSet=1;Secure") |
| 2716 | ++expected_set_cookie_count; |
| 2717 | if (second_cookie_line != "CookieNotSet=1;Secure") |
| 2718 | ++expected_set_cookie_count; |
| 2719 | |
| 2720 | if (first_cookie_line == "CookieBlockedOnCanGetCookie=1") |
| 2721 | ++expected_blocked_cookie_count; |
| 2722 | if (second_cookie_line == "CookieBlockedOnCanGetCookie=1") |
| 2723 | ++expected_blocked_cookie_count; |
| 2724 | |
| 2725 | EXPECT_EQ(expected_set_cookie_count, |
| 2726 | async_filter_network_delegate.set_cookie_called_count()); |
| 2727 | EXPECT_EQ(expected_blocked_cookie_count, |
| 2728 | async_filter_network_delegate.blocked_set_cookie_count()); |
| 2729 | |
| 2730 | EXPECT_EQ(expected_set_cookie_count, |
| 2731 | sync_filter_network_delegate.set_cookie_called_count()); |
| 2732 | EXPECT_EQ(expected_blocked_cookie_count, |
| 2733 | sync_filter_network_delegate.blocked_set_cookie_count()); |
| 2734 | |
| 2735 | async_filter_network_delegate.ResetSetCookieCalledCount(); |
| 2736 | async_filter_network_delegate.ResetBlockedSetCookieCount(); |
| 2737 | |
| 2738 | sync_filter_network_delegate.ResetSetCookieCalledCount(); |
| 2739 | sync_filter_network_delegate.ResetBlockedSetCookieCount(); |
| 2740 | } |
| 2741 | } |
| 2742 | } |
| 2743 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2744 | TEST_F(URLRequestTest, DoNotSendCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2745 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2746 | ASSERT_TRUE(test_server.Start()); |
| 2747 | |
| 2748 | // Set up a cookie. |
| 2749 | { |
| 2750 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2751 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2752 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2753 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2754 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2755 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2756 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2757 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2758 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2759 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2760 | } |
| 2761 | |
| 2762 | // Verify that the cookie is set. |
| 2763 | { |
| 2764 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2765 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2766 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2767 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2768 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2769 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2770 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2771 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2772 | |
| 2773 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2774 | != std::string::npos); |
| 2775 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2776 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2777 | } |
| 2778 | |
| 2779 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 2780 | { |
| 2781 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2782 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2783 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2784 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2785 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2786 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2787 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
| 2788 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2789 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2790 | |
| 2791 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2792 | == std::string::npos); |
| 2793 | |
| 2794 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
| 2795 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2796 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2797 | } |
| 2798 | } |
| 2799 | |
| 2800 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2801 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2802 | ASSERT_TRUE(test_server.Start()); |
| 2803 | |
| 2804 | // Set up a cookie. |
| 2805 | { |
| 2806 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2807 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2808 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2809 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2810 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2811 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2812 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2813 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2814 | |
| 2815 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2816 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2817 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2818 | } |
| 2819 | |
| 2820 | // Try to set-up another cookie and update the previous cookie. |
| 2821 | { |
| 2822 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2823 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2824 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2825 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2826 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2827 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2828 | req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES); |
| 2829 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2830 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2831 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2832 | |
| 2833 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
| 2834 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2835 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2836 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2837 | } |
| 2838 | |
| 2839 | // Verify the cookies weren't saved or updated. |
| 2840 | { |
| 2841 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2842 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2843 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2844 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2845 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2846 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2847 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2848 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2849 | |
| 2850 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2851 | == std::string::npos); |
| 2852 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2853 | != std::string::npos); |
| 2854 | |
| 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 | |
| 2861 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2862 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2863 | ASSERT_TRUE(test_server.Start()); |
| 2864 | |
| 2865 | // Set up a cookie. |
| 2866 | { |
| 2867 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2868 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2869 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2870 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2871 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2872 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2873 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2874 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2875 | |
| 2876 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2877 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2878 | } |
| 2879 | |
| 2880 | // Verify that the cookie is set. |
| 2881 | { |
| 2882 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2883 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2884 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2885 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2886 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2887 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2888 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2889 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2890 | |
| 2891 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2892 | != std::string::npos); |
| 2893 | |
| 2894 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2895 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2896 | TestNetLogEntry::List entries; |
| 2897 | net_log_.GetEntries(&entries); |
| 2898 | for (const auto& entry : entries) { |
| 2899 | EXPECT_NE(entry.type, |
| 2900 | NetLogEventType::COOKIE_GET_BLOCKED_BY_NETWORK_DELEGATE); |
| 2901 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2902 | } |
| 2903 | |
| 2904 | // Verify that the cookie isn't sent. |
| 2905 | { |
| 2906 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2907 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2908 | TestDelegate d; |
| 2909 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2910 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2911 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2912 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2913 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2914 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2915 | |
| 2916 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2917 | == std::string::npos); |
| 2918 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2919 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2920 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2921 | TestNetLogEntry::List entries; |
| 2922 | net_log_.GetEntries(&entries); |
| 2923 | ExpectLogContainsSomewhereAfter( |
| 2924 | entries, 0, NetLogEventType::COOKIE_GET_BLOCKED_BY_NETWORK_DELEGATE, |
| 2925 | NetLogEventPhase::NONE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2926 | } |
| 2927 | } |
| 2928 | |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 2929 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 2930 | #if defined(OS_IOS) |
| 2931 | #define MAYBE_DoNotSaveCookies_ViaPolicy FLAKY_DoNotSaveCookies_ViaPolicy |
| 2932 | #else |
| 2933 | #define MAYBE_DoNotSaveCookies_ViaPolicy DoNotSaveCookies_ViaPolicy |
| 2934 | #endif |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2935 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2936 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2937 | ASSERT_TRUE(test_server.Start()); |
| 2938 | |
| 2939 | // Set up a cookie. |
| 2940 | { |
| 2941 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2942 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2943 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2944 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2945 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2946 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2947 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2948 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2949 | |
| 2950 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2951 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2952 | TestNetLogEntry::List entries; |
| 2953 | net_log_.GetEntries(&entries); |
| 2954 | for (const auto& entry : entries) { |
| 2955 | EXPECT_NE(entry.type, |
| 2956 | NetLogEventType::COOKIE_SET_BLOCKED_BY_NETWORK_DELEGATE); |
| 2957 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2958 | } |
| 2959 | |
| 2960 | // Try to set-up another cookie and update the previous cookie. |
| 2961 | { |
| 2962 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2963 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2964 | TestDelegate d; |
| 2965 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2966 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2967 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2968 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2969 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2970 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2971 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2972 | |
| 2973 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2974 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2975 | TestNetLogEntry::List entries; |
| 2976 | net_log_.GetEntries(&entries); |
| 2977 | ExpectLogContainsSomewhereAfter( |
| 2978 | entries, 0, NetLogEventType::COOKIE_SET_BLOCKED_BY_NETWORK_DELEGATE, |
| 2979 | NetLogEventPhase::NONE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2980 | } |
| 2981 | |
| 2982 | // Verify the cookies weren't saved or updated. |
| 2983 | { |
| 2984 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2985 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2986 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2987 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2988 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2989 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2990 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2991 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2992 | |
| 2993 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2994 | == std::string::npos); |
| 2995 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2996 | != std::string::npos); |
| 2997 | |
| 2998 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2999 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3000 | } |
| 3001 | } |
| 3002 | |
| 3003 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3004 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3005 | ASSERT_TRUE(test_server.Start()); |
| 3006 | |
| 3007 | // Set up an empty cookie. |
| 3008 | { |
| 3009 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3010 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3011 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3012 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3013 | test_server.GetURL("/set-cookie"), DEFAULT_PRIORITY, &d, |
| 3014 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3015 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3016 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3017 | |
| 3018 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3019 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3020 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 3021 | } |
| 3022 | } |
| 3023 | |
| 3024 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3025 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3026 | ASSERT_TRUE(test_server.Start()); |
| 3027 | |
| 3028 | // Set up a cookie. |
| 3029 | { |
| 3030 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3031 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3032 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3033 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3034 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3035 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3036 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3037 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3038 | |
| 3039 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3040 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3041 | } |
| 3042 | |
| 3043 | // Verify that the cookie is set. |
| 3044 | { |
| 3045 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3046 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3047 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3048 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3049 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3050 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3051 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3052 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3053 | |
| 3054 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 3055 | != std::string::npos); |
| 3056 | |
| 3057 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3058 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3059 | } |
| 3060 | |
| 3061 | // Verify that the cookie isn't sent. |
| 3062 | { |
| 3063 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3064 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3065 | TestDelegate d; |
| 3066 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3067 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3068 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3069 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3070 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3071 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3072 | |
| 3073 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 3074 | == std::string::npos); |
| 3075 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 3076 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3077 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3078 | } |
| 3079 | } |
| 3080 | |
| 3081 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3082 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3083 | ASSERT_TRUE(test_server.Start()); |
| 3084 | |
| 3085 | // Set up a cookie. |
| 3086 | { |
| 3087 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3088 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3089 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3090 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3091 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3092 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3093 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3094 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3095 | |
| 3096 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3097 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3098 | } |
| 3099 | |
| 3100 | // Try to set-up another cookie and update the previous cookie. |
| 3101 | { |
| 3102 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3103 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3104 | TestDelegate d; |
| 3105 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3106 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3107 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3108 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3109 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3110 | |
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(2, network_delegate.blocked_set_cookie_count()); |
| 3115 | } |
| 3116 | |
| 3117 | // Verify the cookies weren't saved or updated. |
| 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; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3122 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3123 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3124 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3125 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3126 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3127 | |
| 3128 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 3129 | == std::string::npos); |
| 3130 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 3131 | != std::string::npos); |
| 3132 | |
| 3133 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3134 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3135 | } |
| 3136 | } |
| 3137 | |
mkwst | c5fa776 | 2016-03-28 09:28:23 | [diff] [blame] | 3138 | TEST_F(URLRequestTest, SameSiteCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3139 | HttpTestServer test_server; |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3140 | ASSERT_TRUE(test_server.Start()); |
| 3141 | |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3142 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3143 | default_context().set_network_delegate(&network_delegate); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3144 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3145 | const std::string kHost = "example.test"; |
| 3146 | const std::string kSubHost = "subdomain.example.test"; |
| 3147 | const std::string kCrossHost = "cross-origin.test"; |
| 3148 | |
| 3149 | // Set up two 'SameSite' cookies on 'example.test' |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3150 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3151 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3152 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3153 | test_server.GetURL(kHost, |
| 3154 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 3155 | "LaxSameSiteCookie=1;SameSite=Lax"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3156 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3157 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
| 3158 | req->set_initiator(url::Origin::Create(test_server.GetURL(kHost, "/"))); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3159 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3160 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3161 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3162 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3163 | EXPECT_EQ(2, network_delegate.set_cookie_count()); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3164 | } |
| 3165 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3166 | // Verify that both cookies are sent for same-site requests. |
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( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3170 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3171 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3172 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3173 | req->set_initiator(url::Origin::Create(test_server.GetURL(kHost, "/"))); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3174 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3175 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3176 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3177 | EXPECT_NE(std::string::npos, |
| 3178 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3179 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3180 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3181 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3182 | } |
| 3183 | |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3184 | // Verify that both cookies are sent when the request has no initiator (can |
| 3185 | // happen for main frame browser-initiated navigations). |
| 3186 | { |
| 3187 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3188 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3189 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3190 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3191 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3192 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3193 | d.RunUntilComplete(); |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3194 | |
| 3195 | EXPECT_NE(std::string::npos, |
| 3196 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3197 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 3198 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3199 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3200 | } |
| 3201 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3202 | // Verify that both cookies are sent for same-registrable-domain requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3203 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 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(kSubHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3209 | req->set_initiator(url::Origin::Create(test_server.GetURL(kSubHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3210 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3211 | d.RunUntilComplete(); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3212 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3213 | EXPECT_NE(std::string::npos, |
| 3214 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3215 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3216 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3217 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3218 | } |
| 3219 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3220 | // Verify that neither cookie is not sent for cross-site requests. |
| 3221 | { |
| 3222 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3223 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3224 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3225 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3226 | req->set_site_for_cookies(test_server.GetURL(kCrossHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3227 | req->set_initiator( |
| 3228 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3229 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3230 | d.RunUntilComplete(); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3231 | |
| 3232 | EXPECT_EQ(std::string::npos, |
| 3233 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3234 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 3235 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3236 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3237 | } |
| 3238 | |
| 3239 | // Verify that the lax cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3240 | // method is "safe". |
| 3241 | { |
| 3242 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3243 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3244 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3245 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3246 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3247 | req->set_initiator( |
| 3248 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3249 | req->set_method("GET"); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3250 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3251 | d.RunUntilComplete(); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3252 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3253 | EXPECT_EQ(std::string::npos, |
| 3254 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3255 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3256 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3257 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3258 | } |
| 3259 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3260 | // Verify that neither cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3261 | // method is unsafe (e.g. POST). |
| 3262 | { |
| 3263 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3264 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3265 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3266 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3267 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3268 | req->set_initiator( |
| 3269 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3270 | req->set_method("POST"); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3271 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3272 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3273 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3274 | EXPECT_EQ(std::string::npos, |
| 3275 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3276 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3277 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3278 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3279 | } |
| 3280 | } |
| 3281 | |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3282 | TEST_F(URLRequestTest, SettingSameSiteCookies) { |
| 3283 | HttpTestServer test_server; |
| 3284 | ASSERT_TRUE(test_server.Start()); |
| 3285 | |
| 3286 | TestNetworkDelegate network_delegate; |
| 3287 | default_context().set_network_delegate(&network_delegate); |
| 3288 | |
| 3289 | const std::string kHost = "example.test"; |
| 3290 | const std::string kSubHost = "subdomain.example.test"; |
| 3291 | const std::string kCrossHost = "cross-origin.test"; |
| 3292 | |
| 3293 | int expected_cookies = 0; |
| 3294 | |
| 3295 | { |
| 3296 | TestDelegate d; |
| 3297 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 3298 | test_server.GetURL(kHost, |
| 3299 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 3300 | "LaxSameSiteCookie=1;SameSite=Lax"), |
| 3301 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3302 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
| 3303 | req->set_initiator(url::Origin::Create(test_server.GetURL(kHost, "/"))); |
| 3304 | |
| 3305 | // 'SameSite' cookies are settable from strict same-site contexts |
| 3306 | // (same-origin site_for_cookies, same-origin initiator), so this request |
| 3307 | // should result in two cookies being created. |
| 3308 | expected_cookies += 2; |
| 3309 | |
| 3310 | req->Start(); |
| 3311 | d.RunUntilComplete(); |
| 3312 | EXPECT_EQ(expected_cookies, network_delegate.set_cookie_count()); |
| 3313 | } |
| 3314 | |
| 3315 | { |
| 3316 | TestDelegate d; |
| 3317 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 3318 | test_server.GetURL(kHost, |
| 3319 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 3320 | "LaxSameSiteCookie=1;SameSite=Lax"), |
| 3321 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3322 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
| 3323 | req->set_initiator( |
| 3324 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
| 3325 | |
| 3326 | // 'SameSite' cookies are settable from lax same-site contexts (same-origin |
| 3327 | // site_for_cookies, cross-site initiator), so this request should result in |
| 3328 | // two cookies being created. |
| 3329 | expected_cookies += 2; |
| 3330 | |
| 3331 | req->Start(); |
| 3332 | d.RunUntilComplete(); |
| 3333 | EXPECT_EQ(expected_cookies, network_delegate.set_cookie_count()); |
| 3334 | } |
| 3335 | |
| 3336 | { |
| 3337 | TestDelegate d; |
| 3338 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 3339 | test_server.GetURL(kHost, |
| 3340 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 3341 | "LaxSameSiteCookie=1;SameSite=Lax"), |
| 3342 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3343 | req->set_site_for_cookies(test_server.GetURL(kSubHost, "/")); |
| 3344 | req->set_initiator( |
| 3345 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
| 3346 | |
| 3347 | // 'SameSite' cookies are settable from lax same-site contexts (same-site |
| 3348 | // site_for_cookies, cross-site initiator), so this request should result in |
| 3349 | // two cookies being created. |
| 3350 | expected_cookies += 2; |
| 3351 | |
| 3352 | req->Start(); |
| 3353 | d.RunUntilComplete(); |
| 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, |
| 3361 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 3362 | "LaxSameSiteCookie=1;SameSite=Lax"), |
| 3363 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3364 | req->set_site_for_cookies(test_server.GetURL(kSubHost, "/")); |
| 3365 | |
| 3366 | // 'SameSite' cookies are settable from strict same-site contexts (same-site |
| 3367 | // site_for_cookies, no initiator), so this request should result in two |
| 3368 | // cookies being created. |
| 3369 | expected_cookies += 2; |
| 3370 | |
| 3371 | req->Start(); |
| 3372 | d.RunUntilComplete(); |
| 3373 | EXPECT_EQ(expected_cookies, network_delegate.set_cookie_count()); |
| 3374 | } |
| 3375 | |
| 3376 | { |
| 3377 | TestDelegate d; |
| 3378 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 3379 | test_server.GetURL(kHost, |
| 3380 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 3381 | "LaxSameSiteCookie=1;SameSite=Lax"), |
| 3382 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3383 | req->set_site_for_cookies(test_server.GetURL(kCrossHost, "/")); |
| 3384 | req->set_initiator( |
| 3385 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
| 3386 | |
| 3387 | // 'SameSite' cookies are not settable from cross-site contexts, so this |
| 3388 | // should not result in any new cookies being created. |
| 3389 | expected_cookies += 0; |
| 3390 | |
| 3391 | req->Start(); |
| 3392 | d.RunUntilComplete(); |
| 3393 | EXPECT_EQ(expected_cookies, network_delegate.set_cookie_count()); |
| 3394 | } |
| 3395 | } |
| 3396 | |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3397 | // Tests that __Secure- cookies can't be set on non-secure origins. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3398 | TEST_F(URLRequestTest, SecureCookiePrefixOnNonsecureOrigin) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3399 | EmbeddedTestServer http_server; |
| 3400 | http_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3401 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3402 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3403 | https_server.AddDefaultHandlers( |
| 3404 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3405 | ASSERT_TRUE(http_server.Start()); |
| 3406 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3407 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3408 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3409 | TestURLRequestContext context(true); |
| 3410 | context.set_network_delegate(&network_delegate); |
| 3411 | context.Init(); |
| 3412 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3413 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3414 | { |
| 3415 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3416 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3417 | http_server.GetURL("/set-cookie?__Secure-nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3418 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3419 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3420 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3421 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3422 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3423 | } |
| 3424 | |
| 3425 | // Verify that the cookie is not set. |
| 3426 | { |
| 3427 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3428 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3429 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3430 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3431 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3432 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3433 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3434 | EXPECT_EQ(d.data_received().find("__Secure-nonsecure-origin=1"), |
| 3435 | std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3436 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3437 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3438 | } |
| 3439 | } |
| 3440 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3441 | TEST_F(URLRequestTest, SecureCookiePrefixNonsecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3442 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3443 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3444 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3445 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3446 | |
| 3447 | TestNetworkDelegate network_delegate; |
| 3448 | TestURLRequestContext context(true); |
| 3449 | context.set_network_delegate(&network_delegate); |
| 3450 | context.Init(); |
| 3451 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3452 | // Try to set a non-Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3453 | { |
| 3454 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3455 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 3456 | https_server.GetURL("/set-cookie?__Secure-foo=1"), DEFAULT_PRIORITY, &d, |
| 3457 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3458 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3459 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3460 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3461 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3462 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3463 | } |
| 3464 | |
| 3465 | // Verify that the cookie is not set. |
| 3466 | { |
| 3467 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3468 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3469 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3470 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3471 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3472 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3473 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3474 | EXPECT_EQ(d.data_received().find("__Secure-foo=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3475 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3476 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3477 | } |
| 3478 | } |
| 3479 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3480 | TEST_F(URLRequestTest, SecureCookiePrefixSecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3481 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3482 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3483 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3484 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3485 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3486 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3487 | TestURLRequestContext context(true); |
| 3488 | context.set_network_delegate(&network_delegate); |
| 3489 | context.Init(); |
| 3490 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3491 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3492 | { |
| 3493 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3494 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3495 | https_server.GetURL("/set-cookie?__Secure-bar=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3496 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3497 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3498 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3499 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3500 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3501 | } |
| 3502 | |
| 3503 | // Verify that the cookie is set. |
| 3504 | { |
| 3505 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3506 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3507 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3508 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3509 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3510 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3511 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3512 | EXPECT_NE(d.data_received().find("__Secure-bar=1"), std::string::npos); |
| 3513 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3514 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3515 | } |
| 3516 | } |
| 3517 | |
| 3518 | // Tests that secure cookies can't be set on non-secure origins if strict secure |
| 3519 | // cookies are enabled. |
| 3520 | TEST_F(URLRequestTest, StrictSecureCookiesOnNonsecureOrigin) { |
| 3521 | EmbeddedTestServer http_server; |
| 3522 | http_server.AddDefaultHandlers( |
| 3523 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3524 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3525 | https_server.AddDefaultHandlers( |
| 3526 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3527 | ASSERT_TRUE(http_server.Start()); |
| 3528 | ASSERT_TRUE(https_server.Start()); |
| 3529 | |
Matt Menke | be9b6eb | 2018-10-18 12:11:44 | [diff] [blame] | 3530 | TestNetworkDelegate network_delegate; |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3531 | TestURLRequestContext context(true); |
| 3532 | context.set_network_delegate(&network_delegate); |
| 3533 | context.Init(); |
| 3534 | |
| 3535 | // Try to set a Secure cookie, with experimental features enabled. |
| 3536 | { |
| 3537 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3538 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3539 | http_server.GetURL("/set-cookie?nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3540 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3541 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3542 | d.RunUntilComplete(); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3543 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3544 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3545 | } |
| 3546 | |
| 3547 | // Verify that the cookie is not set. |
| 3548 | { |
| 3549 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3550 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3551 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3552 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3553 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3554 | d.RunUntilComplete(); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3555 | |
| 3556 | EXPECT_EQ(d.data_received().find("nonsecure-origin=1"), std::string::npos); |
| 3557 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3558 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3559 | } |
| 3560 | } |
| 3561 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3562 | // The parameter is true for same-site and false for cross-site requests. |
| 3563 | class URLRequestTestParameterizedSameSite |
| 3564 | : public URLRequestTest, |
| 3565 | public ::testing::WithParamInterface<bool> { |
| 3566 | protected: |
| 3567 | URLRequestTestParameterizedSameSite() { |
| 3568 | auto params = std::make_unique<HttpNetworkSession::Params>(); |
| 3569 | params->ignore_certificate_errors = true; |
| 3570 | context_.set_http_network_session_params(std::move(params)); |
| 3571 | context_.set_network_delegate(&network_delegate_); |
| 3572 | https_server_.AddDefaultHandlers( |
| 3573 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3574 | EXPECT_TRUE(https_server_.Start()); |
| 3575 | } |
| 3576 | |
| 3577 | // To be called after configuration of |context_| has been finalized. |
| 3578 | void InitContext() { context_.Init(); } |
| 3579 | |
| 3580 | const std::string kHost_ = "example.test"; |
| 3581 | const std::string kCrossHost_ = "cross-site.test"; |
| 3582 | TestURLRequestContext context_{true}; |
| 3583 | TestNetworkDelegate network_delegate_; |
| 3584 | base::HistogramTester histograms_; |
| 3585 | EmbeddedTestServer https_server_{EmbeddedTestServer::TYPE_HTTPS}; |
| 3586 | }; |
| 3587 | |
Victor Costan | 8fb98f6f | 2019-02-01 17:08:29 | [diff] [blame] | 3588 | INSTANTIATE_TEST_SUITE_P(URLRequestTest, |
| 3589 | URLRequestTestParameterizedSameSite, |
| 3590 | ::testing::Bool()); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3591 | |
| 3592 | TEST_P(URLRequestTestParameterizedSameSite, CookieAgeMetrics) { |
| 3593 | const bool same_site = GetParam(); |
| 3594 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3595 | InitContext(); |
| 3596 | |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3597 | EmbeddedTestServer http_server; |
| 3598 | http_server.AddDefaultHandlers( |
| 3599 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3600 | ASSERT_TRUE(http_server.Start()); |
| 3601 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3602 | // Set two test cookies. |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3603 | { |
| 3604 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3605 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3606 | http_server.GetURL(kHost_, "/set-cookie?cookie=value&cookie2=value2"), |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3607 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3608 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3609 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3610 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3611 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3612 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 0); |
| 3613 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", 0); |
| 3614 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", 0); |
| 3615 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3616 | 0); |
| 3617 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3618 | 0); |
| 3619 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", 0); |
| 3620 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", 0); |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3621 | } |
| 3622 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3623 | // Make a secure request. |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3624 | { |
| 3625 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3626 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3627 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3628 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3629 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3630 | req->set_initiator( |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3631 | url::Origin::Create(https_server_.GetURL(kInitiatingHost, "/"))); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3632 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3633 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3634 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3635 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 0); |
| 3636 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", |
| 3637 | !same_site); |
| 3638 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", |
| 3639 | same_site); |
| 3640 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3641 | 0); |
| 3642 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3643 | 0); |
| 3644 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", |
| 3645 | same_site ? 0 : 2); |
| 3646 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", |
| 3647 | same_site ? 2 : 0); |
| 3648 | EXPECT_TRUE(d.data_received().find("cookie=value") != std::string::npos); |
| 3649 | EXPECT_TRUE(d.data_received().find("cookie2=value2") != std::string::npos); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3650 | } |
| 3651 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3652 | // Make a non-secure request. |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3653 | { |
| 3654 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3655 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3656 | http_server.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3657 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3658 | req->set_site_for_cookies(http_server.GetURL(kInitiatingHost, "/")); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3659 | req->set_initiator( |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3660 | url::Origin::Create(http_server.GetURL(kInitiatingHost, "/"))); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3661 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3662 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3663 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", |
| 3664 | !same_site); |
| 3665 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", |
| 3666 | same_site); |
| 3667 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", |
| 3668 | !same_site); |
| 3669 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", |
| 3670 | same_site); |
| 3671 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3672 | same_site ? 0 : 2); |
| 3673 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3674 | same_site ? 2 : 0); |
| 3675 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", |
| 3676 | same_site ? 0 : 2); |
| 3677 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", |
| 3678 | same_site ? 2 : 0); |
| 3679 | EXPECT_TRUE(d.data_received().find("cookie=value") != std::string::npos); |
| 3680 | EXPECT_TRUE(d.data_received().find("cookie2=value2") != std::string::npos); |
| 3681 | } |
| 3682 | } |
| 3683 | |
| 3684 | // Cookies with secure attribute (no HSTS) --> k1pSecureAttribute |
| 3685 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3686 | CookieNetworkSecurityMetricSecureAttribute) { |
| 3687 | const bool same_site = GetParam(); |
| 3688 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3689 | InitContext(); |
| 3690 | |
| 3691 | // Set cookies. |
| 3692 | { |
| 3693 | TestDelegate d; |
| 3694 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3695 | https_server_.GetURL(kHost_, |
| 3696 | "/set-cookie?session-cookie=value;Secure&" |
| 3697 | "longlived-cookie=value;Secure;domain=" + |
| 3698 | kHost_ + ";Max-Age=360000"), |
| 3699 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3700 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3701 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3702 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3703 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3704 | } |
| 3705 | |
| 3706 | // Verify that the cookies fall into the correct metrics bucket. |
| 3707 | { |
| 3708 | TestDelegate d; |
| 3709 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3710 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3711 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3712 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3713 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3714 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3715 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3716 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3717 | // Static cast of boolean required for MSVC 1911. |
| 3718 | histograms_.ExpectBucketCount( |
| 3719 | "Cookie.NetworkSecurity", |
| 3720 | static_cast<int>(CookieNetworkSecurity::k1pSecureAttribute) | |
| 3721 | static_cast<int>(!same_site), |
| 3722 | 2); |
| 3723 | } |
| 3724 | } |
| 3725 | |
| 3726 | // Short-lived host cookie --> k1pHSTSHostCookie |
| 3727 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3728 | CookieNetworkSecurityMetricShortlivedHostCookie) { |
| 3729 | const bool same_site = GetParam(); |
| 3730 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3731 | |
| 3732 | TransportSecurityState transport_security_state; |
| 3733 | transport_security_state.AddHSTS( |
| 3734 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3735 | false /* include_subdomains */); |
| 3736 | context_.set_transport_security_state(&transport_security_state); |
| 3737 | InitContext(); |
| 3738 | |
| 3739 | // Set cookie. |
| 3740 | { |
| 3741 | TestDelegate d; |
| 3742 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3743 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;Max-Age=3600"), |
| 3744 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3745 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3746 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3747 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3748 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3749 | } |
| 3750 | |
| 3751 | // Verify that the cookie falls into the correct metrics bucket. |
| 3752 | { |
| 3753 | TestDelegate d; |
| 3754 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3755 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3756 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3757 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3758 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3759 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3760 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3761 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3762 | // Static cast of boolean required for MSVC 1911. |
| 3763 | histograms_.ExpectBucketCount( |
| 3764 | "Cookie.NetworkSecurity", |
| 3765 | static_cast<int>(CookieNetworkSecurity::k1pHSTSHostCookie) | |
| 3766 | static_cast<int>(!same_site), |
| 3767 | 1); |
| 3768 | } |
| 3769 | } |
| 3770 | |
| 3771 | // Long-lived (either due to expiry or due to being a session cookie) host |
| 3772 | // cookies --> k1pExpiringHSTSHostCookie |
| 3773 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3774 | CookieNetworkSecurityMetricLonglivedHostCookie) { |
| 3775 | const bool same_site = GetParam(); |
| 3776 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3777 | |
| 3778 | TransportSecurityState transport_security_state; |
| 3779 | transport_security_state.AddHSTS( |
| 3780 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3781 | false /* include_subdomains */); |
| 3782 | context_.set_transport_security_state(&transport_security_state); |
| 3783 | InitContext(); |
| 3784 | |
| 3785 | // Set cookies. |
| 3786 | { |
| 3787 | TestDelegate d; |
| 3788 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3789 | https_server_.GetURL(kHost_, |
| 3790 | "/set-cookie?session-cookie=value&" |
| 3791 | "longlived-cookie=value;Max-Age=360000"), |
| 3792 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3793 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3794 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3795 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3796 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3797 | } |
| 3798 | |
| 3799 | // Verify that the cookies fall into the correct metrics bucket. |
| 3800 | { |
| 3801 | TestDelegate d; |
| 3802 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3803 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3804 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3805 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3806 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3807 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3808 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3809 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3810 | // Static cast of boolean required for MSVC 1911. |
| 3811 | histograms_.ExpectBucketCount( |
| 3812 | "Cookie.NetworkSecurity", |
| 3813 | static_cast<int>(CookieNetworkSecurity::k1pExpiringHSTSHostCookie) | |
| 3814 | static_cast<int>(!same_site), |
| 3815 | 2); |
| 3816 | } |
| 3817 | } |
| 3818 | |
| 3819 | // Domain cookie with HSTS subdomains with cookie expiry before HSTS expiry --> |
| 3820 | // k1pHSTSSubdomainsIncluded |
| 3821 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3822 | CookieNetworkSecurityMetricShortlivedDomainCookie) { |
| 3823 | const bool same_site = GetParam(); |
| 3824 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3825 | |
| 3826 | TransportSecurityState transport_security_state; |
| 3827 | transport_security_state.AddHSTS( |
| 3828 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3829 | true /* include_subdomains */); |
| 3830 | context_.set_transport_security_state(&transport_security_state); |
| 3831 | InitContext(); |
| 3832 | |
| 3833 | // Set cookie. |
| 3834 | { |
| 3835 | TestDelegate d; |
| 3836 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3837 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;domain=" + |
| 3838 | kHost_ + ";Max-Age=3600"), |
| 3839 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3840 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3841 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3842 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3843 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3844 | } |
| 3845 | |
| 3846 | // Verify that the cookie falls into the correct metrics bucket. |
| 3847 | { |
| 3848 | TestDelegate d; |
| 3849 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3850 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3851 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3852 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3853 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3854 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3855 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3856 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3857 | // Static cast of boolean required for MSVC 1911. |
| 3858 | histograms_.ExpectBucketCount( |
| 3859 | "Cookie.NetworkSecurity", |
| 3860 | static_cast<int>(CookieNetworkSecurity::k1pHSTSSubdomainsIncluded) | |
| 3861 | static_cast<int>(!same_site), |
| 3862 | 1); |
| 3863 | } |
| 3864 | } |
| 3865 | |
| 3866 | // Long-lived (either due to expiry or due to being a session cookie) domain |
| 3867 | // cookies with HSTS subdomains --> k1pExpiringHSTSSubdomainsIncluded |
| 3868 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3869 | CookieNetworkSecurityMetricLonglivedDomainCookie) { |
| 3870 | const bool same_site = GetParam(); |
| 3871 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3872 | |
| 3873 | TransportSecurityState transport_security_state; |
| 3874 | transport_security_state.AddHSTS( |
| 3875 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3876 | true /* include_subdomains */); |
| 3877 | context_.set_transport_security_state(&transport_security_state); |
| 3878 | InitContext(); |
| 3879 | |
| 3880 | // Set cookies. |
| 3881 | { |
| 3882 | TestDelegate d; |
| 3883 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3884 | https_server_.GetURL( |
| 3885 | kHost_, "/set-cookie?session-cookie=value;domain=" + kHost_ + "&" + |
| 3886 | "longlived-cookie=value;domain=" + kHost_ + |
| 3887 | ";Max-Age=360000"), |
| 3888 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3889 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3890 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3891 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3892 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3893 | } |
| 3894 | |
| 3895 | // Verify that the cookies fall into the correct metrics bucket. |
| 3896 | { |
| 3897 | TestDelegate d; |
| 3898 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3899 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3900 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3901 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3902 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3903 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3904 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3905 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3906 | // Static cast of boolean required for MSVC 1911. |
| 3907 | histograms_.ExpectBucketCount( |
| 3908 | "Cookie.NetworkSecurity", |
| 3909 | static_cast<int>( |
| 3910 | CookieNetworkSecurity::k1pExpiringHSTSSubdomainsIncluded) | |
| 3911 | static_cast<int>(!same_site), |
| 3912 | 2); |
| 3913 | } |
| 3914 | } |
| 3915 | |
| 3916 | // Domain cookie with HSTS subdomains not included --> k1pHSTSSpoofable |
| 3917 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3918 | CookieNetworkSecurityMetricSpoofableDomainCookie) { |
| 3919 | const bool same_site = GetParam(); |
| 3920 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3921 | |
| 3922 | TransportSecurityState transport_security_state; |
| 3923 | transport_security_state.AddHSTS( |
| 3924 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3925 | false /* include_subdomains */); |
| 3926 | context_.set_transport_security_state(&transport_security_state); |
| 3927 | InitContext(); |
| 3928 | |
| 3929 | // Set cookie. |
| 3930 | { |
| 3931 | TestDelegate d; |
| 3932 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3933 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;domain=" + |
| 3934 | kHost_ + ";Max-Age=3600"), |
| 3935 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3936 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3937 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3938 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3939 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3940 | } |
| 3941 | |
| 3942 | // Verify that the cookie falls into the correct metrics bucket. |
| 3943 | { |
| 3944 | TestDelegate d; |
| 3945 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3946 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3947 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3948 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3949 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3950 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3951 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3952 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3953 | // Static cast of boolean required for MSVC 1911. |
| 3954 | histograms_.ExpectBucketCount( |
| 3955 | "Cookie.NetworkSecurity", |
| 3956 | static_cast<int>(CookieNetworkSecurity::k1pHSTSSpoofable) | |
| 3957 | static_cast<int>(!same_site), |
| 3958 | 1); |
| 3959 | } |
| 3960 | } |
| 3961 | |
| 3962 | // Cookie without HSTS --> k1p(Non)SecureConnection |
| 3963 | TEST_P(URLRequestTestParameterizedSameSite, CookieNetworkSecurityMetricNoHSTS) { |
| 3964 | const bool same_site = GetParam(); |
| 3965 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3966 | InitContext(); |
| 3967 | |
| 3968 | EmbeddedTestServer http_server; |
| 3969 | http_server.AddDefaultHandlers( |
| 3970 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3971 | ASSERT_TRUE(http_server.Start()); |
| 3972 | |
| 3973 | // Set cookies. |
| 3974 | { |
| 3975 | TestDelegate d; |
| 3976 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3977 | https_server_.GetURL(kHost_, |
| 3978 | "/set-cookie?cookie=value;domain=" + kHost_ + |
| 3979 | ";Max-Age=3600&host-cookie=value"), |
| 3980 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3981 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3982 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3983 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3984 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3985 | } |
| 3986 | |
| 3987 | // Verify that the cookie falls into the correct metrics bucket. |
| 3988 | { |
| 3989 | TestDelegate d; |
| 3990 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3991 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3992 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3993 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3994 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3995 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3996 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3997 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3998 | // Static cast of boolean required for MSVC 1911. |
| 3999 | histograms_.ExpectBucketCount( |
| 4000 | "Cookie.NetworkSecurity", |
| 4001 | static_cast<int>(CookieNetworkSecurity::k1pSecureConnection) | |
| 4002 | static_cast<int>(!same_site), |
| 4003 | 2); |
| 4004 | } |
| 4005 | |
| 4006 | // Verify that the cookie falls into the correct metrics bucket. |
| 4007 | { |
| 4008 | TestDelegate d; |
| 4009 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 4010 | http_server.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 4011 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 4012 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 4013 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 4014 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4015 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 4016 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 4); |
| 4017 | // Static cast of boolean required for MSVC 1911. |
| 4018 | histograms_.ExpectBucketCount( |
| 4019 | "Cookie.NetworkSecurity", |
| 4020 | static_cast<int>(CookieNetworkSecurity::k1pSecureConnection) | |
| 4021 | static_cast<int>(!same_site), |
| 4022 | 2); |
| 4023 | // Static cast of boolean required for MSVC 1911. |
| 4024 | histograms_.ExpectBucketCount( |
| 4025 | "Cookie.NetworkSecurity", |
| 4026 | static_cast<int>(CookieNetworkSecurity::k1pNonsecureConnection) | |
| 4027 | static_cast<int>(!same_site), |
| 4028 | 2); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 4029 | } |
| 4030 | } |
| 4031 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 4032 | // Tests that a request is cancelled while entering suspend mode. Uses mocks |
| 4033 | // rather than a spawned test server because the connection used to talk to |
| 4034 | // the test server is affected by entering suspend mode on Android. |
| 4035 | TEST_F(URLRequestTest, CancelOnSuspend) { |
| 4036 | TestPowerMonitorSource* power_monitor_source = new TestPowerMonitorSource(); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4037 | base::PowerMonitor power_monitor(base::WrapUnique(power_monitor_source)); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 4038 | |
| 4039 | URLRequestFailedJob::AddUrlHandler(); |
| 4040 | |
| 4041 | TestDelegate d; |
| 4042 | // Request that just hangs. |
| 4043 | GURL url(URLRequestFailedJob::GetMockHttpUrl(ERR_IO_PENDING)); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4044 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4045 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 4046 | r->Start(); |
| 4047 | |
| 4048 | power_monitor_source->Suspend(); |
| 4049 | // Wait for the suspend notification to cause the request to fail. |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4050 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4051 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 4052 | EXPECT_TRUE(d.request_failed()); |
| 4053 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 4054 | |
| 4055 | URLRequestFilter::GetInstance()->ClearHandlers(); |
| 4056 | |
| 4057 | // Shouldn't be needed, but just in case. |
| 4058 | power_monitor_source->Resume(); |
| 4059 | } |
| 4060 | |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4061 | // FixedDateNetworkDelegate swaps out the server's HTTP Date response header |
| 4062 | // value for the |fixed_date| argument given to the constructor. |
| 4063 | class FixedDateNetworkDelegate : public TestNetworkDelegate { |
| 4064 | public: |
| 4065 | explicit FixedDateNetworkDelegate(const std::string& fixed_date) |
| 4066 | : fixed_date_(fixed_date) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 4067 | ~FixedDateNetworkDelegate() override = default; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4068 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4069 | // NetworkDelegate implementation |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4070 | int OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4071 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 4072 | CompletionOnceCallback callback, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4073 | const HttpResponseHeaders* original_response_headers, |
| 4074 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 4075 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4076 | |
| 4077 | private: |
| 4078 | std::string fixed_date_; |
| 4079 | |
| 4080 | DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); |
| 4081 | }; |
| 4082 | |
| 4083 | int FixedDateNetworkDelegate::OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4084 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 4085 | CompletionOnceCallback callback, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4086 | const HttpResponseHeaders* original_response_headers, |
| 4087 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4088 | GURL* allowed_unsafe_redirect_url) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4089 | HttpResponseHeaders* new_response_headers = |
| 4090 | new HttpResponseHeaders(original_response_headers->raw_headers()); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4091 | |
| 4092 | new_response_headers->RemoveHeader("Date"); |
| 4093 | new_response_headers->AddHeader("Date: " + fixed_date_); |
| 4094 | |
| 4095 | *override_response_headers = new_response_headers; |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 4096 | return TestNetworkDelegate::OnHeadersReceived( |
| 4097 | request, std::move(callback), original_response_headers, |
| 4098 | override_response_headers, allowed_unsafe_redirect_url); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4099 | } |
| 4100 | |
| 4101 | // Test that cookie expiration times are adjusted for server/client clock |
| 4102 | // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date |
| 4103 | // headers by defaulting to GMT. (crbug.com/135131) |
| 4104 | TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4105 | HttpTestServer test_server; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4106 | ASSERT_TRUE(test_server.Start()); |
| 4107 | |
| 4108 | // Set up an expired cookie. |
| 4109 | { |
| 4110 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4111 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4112 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4113 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4114 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4115 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4116 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4117 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4118 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4119 | } |
| 4120 | // Verify that the cookie is not set. |
| 4121 | { |
| 4122 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4123 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4124 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4125 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4126 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 4127 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4128 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4129 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4130 | |
| 4131 | EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos); |
| 4132 | } |
| 4133 | // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier. |
| 4134 | { |
| 4135 | FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC"); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4136 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4137 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4138 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4139 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4140 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4141 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4142 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4143 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4144 | } |
| 4145 | // Verify that the cookie is set. |
| 4146 | { |
| 4147 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4148 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4149 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4150 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4151 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 4152 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4153 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4154 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4155 | |
| 4156 | EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos); |
| 4157 | } |
| 4158 | } |
| 4159 | |
| 4160 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4161 | // Check that it is impossible to change the referrer in the extra headers of |
| 4162 | // an URLRequest. |
| 4163 | TEST_F(URLRequestTest, DoNotOverrideReferrer) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4164 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4165 | ASSERT_TRUE(test_server.Start()); |
| 4166 | |
| 4167 | // If extra headers contain referer and the request contains a referer, |
| 4168 | // only the latter shall be respected. |
| 4169 | { |
| 4170 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4171 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4172 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 4173 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4174 | req->SetReferrer("http://foo.com/"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4175 | |
| 4176 | HttpRequestHeaders headers; |
| 4177 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4178 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4179 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4180 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4181 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4182 | |
| 4183 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 4184 | } |
| 4185 | |
| 4186 | // If extra headers contain a referer but the request does not, no referer |
| 4187 | // shall be sent in the header. |
| 4188 | { |
| 4189 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4190 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4191 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 4192 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4193 | |
| 4194 | HttpRequestHeaders headers; |
| 4195 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4196 | req->SetExtraRequestHeaders(headers); |
| 4197 | req->SetLoadFlags(LOAD_VALIDATE_CACHE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4198 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4199 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4200 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4201 | |
| 4202 | EXPECT_EQ("None", d.data_received()); |
| 4203 | } |
| 4204 | } |
| 4205 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4206 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4207 | public: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 4208 | URLRequestTestHTTP() : test_server_(base::FilePath(kTestFilePath)) {} |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4209 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4210 | protected: |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4211 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 4212 | // |request_method| is the method to use for the initial request. |
| 4213 | // |redirect_method| is the method that is expected to be used for the second |
| 4214 | // request, after redirection. |
| 4215 | // If |include_data| is true, data is uploaded with the request. The |
| 4216 | // response body is expected to match it exactly, if and only if |
| 4217 | // |request_method| == |redirect_method|. |
| 4218 | void HTTPRedirectMethodTest(const GURL& redirect_url, |
| 4219 | const std::string& request_method, |
| 4220 | const std::string& redirect_method, |
| 4221 | bool include_data) { |
| 4222 | static const char kData[] = "hello world"; |
| 4223 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4224 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4225 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4226 | req->set_method(request_method); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4227 | if (include_data) { |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4228 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4229 | HttpRequestHeaders headers; |
| 4230 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 4231 | base::NumberToString(base::size(kData) - 1)); |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 4232 | headers.SetHeader(HttpRequestHeaders::kContentType, "text/plain"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4233 | req->SetExtraRequestHeaders(headers); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4234 | } |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4235 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4236 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4237 | EXPECT_EQ(redirect_method, req->method()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4238 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4239 | if (include_data) { |
| 4240 | if (request_method == redirect_method) { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 4241 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 4242 | HttpRequestHeaders::kContentLength)); |
| 4243 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 4244 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4245 | EXPECT_EQ(kData, d.data_received()); |
| 4246 | } else { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 4247 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 4248 | HttpRequestHeaders::kContentLength)); |
| 4249 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 4250 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4251 | EXPECT_NE(kData, d.data_received()); |
| 4252 | } |
| 4253 | } |
| 4254 | if (HasFailure()) |
| 4255 | LOG(WARNING) << "Request method was: " << request_method; |
| 4256 | } |
| 4257 | |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4258 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. It's also |
| 4259 | // used as the initial origin. |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4260 | // |request_method| is the method to use for the initial request. |
| 4261 | // |redirect_method| is the method that is expected to be used for the second |
| 4262 | // request, after redirection. |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4263 | // |expected_origin_value| is the expected value for the Origin header after |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4264 | // redirection. If empty, expects that there will be no Origin header. |
| 4265 | void HTTPRedirectOriginHeaderTest(const GURL& redirect_url, |
| 4266 | const std::string& request_method, |
| 4267 | const std::string& redirect_method, |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4268 | const std::string& expected_origin_value) { |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4269 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4270 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4271 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4272 | req->set_method(request_method); |
| 4273 | req->SetExtraRequestHeaderByName(HttpRequestHeaders::kOrigin, |
| 4274 | redirect_url.GetOrigin().spec(), false); |
| 4275 | req->Start(); |
| 4276 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4277 | d.RunUntilComplete(); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4278 | |
| 4279 | EXPECT_EQ(redirect_method, req->method()); |
| 4280 | // Note that there is no check for request success here because, for |
| 4281 | // purposes of testing, the request very well may fail. For example, if the |
| 4282 | // test redirects to an HTTPS server from an HTTP origin, thus it is cross |
| 4283 | // origin, there is not an HTTPS server in this unit test framework, so the |
| 4284 | // request would fail. However, that's fine, as long as the request headers |
| 4285 | // are in order and pass the checks below. |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4286 | if (expected_origin_value.empty()) { |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4287 | EXPECT_FALSE( |
| 4288 | req->extra_request_headers().HasHeader(HttpRequestHeaders::kOrigin)); |
| 4289 | } else { |
| 4290 | std::string origin_header; |
| 4291 | EXPECT_TRUE(req->extra_request_headers().GetHeader( |
| 4292 | HttpRequestHeaders::kOrigin, &origin_header)); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4293 | EXPECT_EQ(expected_origin_value, origin_header); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4294 | } |
| 4295 | } |
| 4296 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4297 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4298 | const int kMsgSize = 20000; // multiple of 10 |
| 4299 | const int kIterations = 50; |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 4300 | char* uploadBytes = new char[kMsgSize+1]; |
| 4301 | char* ptr = uploadBytes; |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4302 | char marker = 'a'; |
| 4303 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 4304 | memcpy(ptr, "----------", 10); |
| 4305 | ptr += 10; |
| 4306 | if (idx % 100 == 0) { |
| 4307 | ptr--; |
| 4308 | *ptr++ = marker; |
| 4309 | if (++marker > 'z') |
| 4310 | marker = 'a'; |
| 4311 | } |
| 4312 | } |
| 4313 | uploadBytes[kMsgSize] = '\0'; |
| 4314 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4315 | for (int i = 0; i < kIterations; ++i) { |
| 4316 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4317 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4318 | test_server_.GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 4319 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4320 | r->set_method(method.c_str()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4321 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4322 | r->set_upload(CreateSimpleUploadData(uploadBytes)); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4323 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4324 | r->Start(); |
| 4325 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4326 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4327 | d.RunUntilComplete(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4328 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4329 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 4330 | << d.request_status(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4331 | |
| 4332 | EXPECT_FALSE(d.received_data_before_response()); |
| 4333 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4334 | } |
| 4335 | delete[] uploadBytes; |
| 4336 | } |
| 4337 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 4338 | bool DoManyCookiesRequest(int num_cookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4339 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4340 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4341 | test_server_.GetURL("/set-many-cookies?" + |
Raul Tambre | 8c1981d | 2019-02-08 02:22:26 | [diff] [blame] | 4342 | base::NumberToString(num_cookies)), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4343 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4344 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4345 | r->Start(); |
| 4346 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4347 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4348 | d.RunUntilComplete(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4349 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4350 | if (d.request_status() != OK) { |
| 4351 | EXPECT_EQ(ERR_RESPONSE_HEADERS_TOO_BIG, d.request_status()); |
| 4352 | return false; |
| 4353 | } |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4354 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4355 | return true; |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4356 | } |
| 4357 | |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4358 | HttpTestServer* http_test_server() { return &test_server_; } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 4359 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4360 | private: |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4361 | HttpTestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4362 | }; |
| 4363 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4364 | namespace { |
| 4365 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4366 | std::unique_ptr<test_server::HttpResponse> HandleRedirectConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4367 | const test_server::HttpRequest& request) { |
| 4368 | if (request.headers.find("Host") == request.headers.end() || |
| 4369 | request.headers.at("Host") != "www.redirect.com" || |
| 4370 | request.method != test_server::METHOD_CONNECT) { |
| 4371 | return nullptr; |
| 4372 | } |
| 4373 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4374 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4375 | new test_server::BasicHttpResponse); |
| 4376 | http_response->set_code(HTTP_FOUND); |
| 4377 | http_response->AddCustomHeader("Location", |
| 4378 | "http://www.destination.com/foo.js"); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 4379 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4380 | } |
| 4381 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4382 | class TestSSLConfigService : public SSLConfigService { |
| 4383 | public: |
Nick Harper | 2243e800 | 2018-09-28 20:33:26 | [diff] [blame] | 4384 | TestSSLConfigService() |
| 4385 | : min_version_(kDefaultSSLVersionMin), |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4386 | max_version_(kDefaultSSLVersionMax) {} |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4387 | ~TestSSLConfigService() override = default; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4388 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4389 | void set_max_version(uint16_t version) { max_version_ = version; } |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4390 | void set_min_version(uint16_t version) { min_version_ = version; } |
| 4391 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4392 | // SSLConfigService: |
| 4393 | void GetSSLConfig(SSLConfig* config) override { |
| 4394 | *config = SSLConfig(); |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4395 | config->version_min = min_version_; |
| 4396 | config->version_max = max_version_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4397 | } |
| 4398 | |
Nick Harper | 89bc721 | 2018-07-31 19:07:57 | [diff] [blame] | 4399 | bool CanShareConnectionWithClientCerts( |
| 4400 | const std::string& hostname) const override { |
| 4401 | return false; |
| 4402 | } |
| 4403 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4404 | private: |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4405 | uint16_t min_version_; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4406 | uint16_t max_version_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4407 | }; |
| 4408 | |
Nick Harper | 89bc721 | 2018-07-31 19:07:57 | [diff] [blame] | 4409 | } // namespace |
| 4410 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4411 | // 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] | 4412 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4413 | // The EmbeddedTestServer will return a 302 response, which we should not |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4414 | // follow. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 4415 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4416 | http_test_server()->RegisterRequestHandler( |
Matt Menke | 2b2a6047 | 2018-02-07 02:12:12 | [diff] [blame] | 4417 | base::BindRepeating(&HandleRedirectConnect)); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4418 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4419 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4420 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4421 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4422 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4423 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4424 | TestDelegate d; |
| 4425 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4426 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4427 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 4428 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4429 | r->Start(); |
| 4430 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4431 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4432 | d.RunUntilComplete(); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4433 | |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 4434 | // The proxy server should be set before failure. |
| 4435 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4436 | http_test_server()->host_port_pair()), |
| 4437 | r->proxy_server()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4438 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4439 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4440 | // We should not have followed the redirect. |
| 4441 | EXPECT_EQ(0, d.received_redirect_count()); |
| 4442 | } |
| 4443 | } |
| 4444 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4445 | // This is the same as the previous test, but checks that the network delegate |
| 4446 | // registers the error. |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 4447 | TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4448 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4449 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4450 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4451 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4452 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4453 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4454 | TestDelegate d; |
| 4455 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4456 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4457 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 4458 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4459 | r->Start(); |
| 4460 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4461 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4462 | d.RunUntilComplete(); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4463 | |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 4464 | // The proxy server should be set before failure. |
| 4465 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4466 | http_test_server()->host_port_pair()), |
| 4467 | r->proxy_server()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4468 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4469 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4470 | // We should not have followed the redirect. |
| 4471 | EXPECT_EQ(0, d.received_redirect_count()); |
| 4472 | |
| 4473 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4474 | EXPECT_THAT(network_delegate.last_error(), |
| 4475 | IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4476 | } |
| 4477 | } |
| 4478 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4479 | // Tests that we can block and asynchronously return OK in various stages. |
| 4480 | TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) { |
| 4481 | static const BlockingNetworkDelegate::Stage blocking_stages[] = { |
| 4482 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 4483 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 4484 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED |
| 4485 | }; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 4486 | static const size_t blocking_stages_length = base::size(blocking_stages); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4487 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4488 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4489 | |
| 4490 | TestDelegate d; |
| 4491 | BlockingNetworkDelegate network_delegate( |
| 4492 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4493 | network_delegate.set_block_on( |
| 4494 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST | |
| 4495 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS | |
| 4496 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 4497 | |
| 4498 | TestURLRequestContext context(true); |
| 4499 | context.set_network_delegate(&network_delegate); |
| 4500 | context.Init(); |
| 4501 | |
| 4502 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4503 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4504 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4505 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4506 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4507 | r->Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4508 | for (size_t i = 0; i < blocking_stages_length; ++i) { |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4509 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4510 | EXPECT_EQ(blocking_stages[i], |
| 4511 | network_delegate.stage_blocked_for_callback()); |
| 4512 | network_delegate.DoCallback(OK); |
| 4513 | } |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4514 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4515 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4516 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4517 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4518 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4519 | } |
| 4520 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4521 | } |
| 4522 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4523 | // Tests that the network delegate can block and cancel a request. |
| 4524 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4525 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4526 | |
| 4527 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4528 | BlockingNetworkDelegate network_delegate( |
| 4529 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4530 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 4531 | network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4532 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4533 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4534 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4535 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4536 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4537 | std::unique_ptr<URLRequest> r( |
| 4538 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4539 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4540 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4541 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4542 | d.RunUntilComplete(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4543 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4544 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4545 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4546 | EXPECT_EQ(ERR_EMPTY_RESPONSE, d.request_status()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4547 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4548 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4549 | } |
| 4550 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4551 | } |
| 4552 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4553 | // Helper function for NetworkDelegateCancelRequestAsynchronously and |
| 4554 | // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network |
| 4555 | // delegate operating in |block_mode| and a request for |url|. It blocks the |
| 4556 | // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT. |
| 4557 | void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode, |
| 4558 | BlockingNetworkDelegate::Stage stage, |
| 4559 | const GURL& url) { |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4560 | TestDelegate d; |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4561 | BlockingNetworkDelegate network_delegate(block_mode); |
| 4562 | network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT); |
| 4563 | network_delegate.set_block_on(stage); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4564 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4565 | TestURLRequestContext context(true); |
| 4566 | context.set_network_delegate(&network_delegate); |
| 4567 | context.Init(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4568 | |
| 4569 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4570 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4571 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4572 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4573 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4574 | d.RunUntilComplete(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4575 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4576 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4577 | if (stage == BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST || |
| 4578 | stage == BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS) { |
| 4579 | EXPECT_FALSE(r->proxy_server().is_valid()); |
| 4580 | } else if (stage == BlockingNetworkDelegate::ON_HEADERS_RECEIVED) { |
| 4581 | EXPECT_TRUE(r->proxy_server().is_direct()); |
| 4582 | } else { |
| 4583 | NOTREACHED(); |
| 4584 | } |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4585 | EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, d.request_status()); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4586 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4587 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4588 | } |
| 4589 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4590 | } |
| 4591 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4592 | // The following 3 tests check that the network delegate can cancel a request |
| 4593 | // synchronously in various stages of the request. |
| 4594 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4595 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4596 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4597 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4598 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4599 | } |
| 4600 | |
| 4601 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4602 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4603 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4604 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4605 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4606 | } |
| 4607 | |
| 4608 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4609 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4610 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4611 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4612 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4613 | } |
| 4614 | |
| 4615 | // The following 3 tests check that the network delegate can cancel a request |
| 4616 | // asynchronously in various stages of the request. |
| 4617 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4618 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4619 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4620 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4621 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4622 | } |
| 4623 | |
| 4624 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4625 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4626 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4627 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4628 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4632 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4633 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4634 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4635 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4636 | } |
| 4637 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4638 | // Tests that the network delegate can block and redirect a request to a new |
| 4639 | // URL. |
| 4640 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4641 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4642 | |
| 4643 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4644 | BlockingNetworkDelegate network_delegate( |
| 4645 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4646 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4647 | GURL redirect_url("http://does.not.resolve.test/simple.html"); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4648 | network_delegate.set_redirect_url(redirect_url); |
| 4649 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4650 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4651 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4652 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4653 | { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4654 | GURL original_url("http://does.not.resolve.test/defaultresponse"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4655 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4656 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4657 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4658 | // Quit after hitting the redirect, so can check the headers. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4659 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 4660 | d.RunUntilRedirect(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4661 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4662 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4663 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4664 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4665 | std::string location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4666 | ASSERT_TRUE( |
| 4667 | r->response_headers()->EnumerateHeader(nullptr, "Location", &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4668 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4669 | |
| 4670 | // Let the request finish. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 4671 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 4672 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4673 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4674 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4675 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4676 | http_test_server()->host_port_pair()), |
| 4677 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4678 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4679 | // through an untunneled proxy. |
| 4680 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4681 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4682 | http_test_server()->host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 4683 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4684 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4685 | EXPECT_EQ(redirect_url, r->url()); |
| 4686 | EXPECT_EQ(original_url, r->original_url()); |
| 4687 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4688 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4689 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4690 | } |
| 4691 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4692 | } |
| 4693 | |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4694 | // Tests that the network delegate can block and redirect a request to a new |
| 4695 | // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 4696 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4697 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4698 | |
| 4699 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4700 | BlockingNetworkDelegate network_delegate( |
| 4701 | BlockingNetworkDelegate::SYNCHRONOUS); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4702 | GURL redirect_url("http://does.not.resolve.test/simple.html"); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4703 | network_delegate.set_redirect_url(redirect_url); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4704 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4705 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4706 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4707 | |
| 4708 | { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4709 | GURL original_url("http://does.not.resolve.test/defaultresponse"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4710 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4711 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4712 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4713 | // Quit after hitting the redirect, so can check the headers. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4714 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 4715 | d.RunUntilRedirect(); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4716 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4717 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4718 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4719 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4720 | std::string location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4721 | ASSERT_TRUE( |
| 4722 | r->response_headers()->EnumerateHeader(nullptr, "Location", &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4723 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4724 | |
| 4725 | // Let the request finish. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 4726 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 4727 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4728 | d.RunUntilComplete(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4729 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4730 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4731 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4732 | http_test_server()->host_port_pair()), |
| 4733 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4734 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4735 | // through an untunneled proxy. |
| 4736 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4737 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4738 | http_test_server()->host_port_pair())); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4739 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4740 | EXPECT_EQ(redirect_url, r->url()); |
| 4741 | EXPECT_EQ(original_url, r->original_url()); |
| 4742 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4743 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4744 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4745 | } |
| 4746 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4747 | } |
| 4748 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4749 | // Tests that redirects caused by the network delegate preserve POST data. |
| 4750 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4751 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4752 | |
| 4753 | const char kData[] = "hello world"; |
| 4754 | |
| 4755 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4756 | BlockingNetworkDelegate network_delegate( |
| 4757 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4758 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4759 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4760 | network_delegate.set_redirect_url(redirect_url); |
| 4761 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4762 | TestURLRequestContext context(true); |
| 4763 | context.set_network_delegate(&network_delegate); |
| 4764 | context.Init(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4765 | |
| 4766 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4767 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4768 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4769 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4770 | r->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4771 | r->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4772 | HttpRequestHeaders headers; |
| 4773 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 4774 | base::NumberToString(base::size(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4775 | r->SetExtraRequestHeaders(headers); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4776 | |
| 4777 | // Quit after hitting the redirect, so can check the headers. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4778 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 4779 | d.RunUntilRedirect(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4780 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4781 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4782 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4783 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4784 | std::string location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4785 | ASSERT_TRUE( |
| 4786 | r->response_headers()->EnumerateHeader(nullptr, "Location", &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4787 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4788 | |
| 4789 | // Let the request finish. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 4790 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 4791 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4792 | d.RunUntilComplete(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4793 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4794 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4795 | EXPECT_EQ(redirect_url, r->url()); |
| 4796 | EXPECT_EQ(original_url, r->original_url()); |
| 4797 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4798 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4799 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4800 | EXPECT_EQ("POST", r->method()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4801 | EXPECT_EQ(kData, d.data_received()); |
| 4802 | } |
| 4803 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4804 | } |
| 4805 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4806 | // Tests that the network delegate can block and redirect a request to a new |
| 4807 | // URL during OnHeadersReceived. |
| 4808 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4809 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4810 | |
| 4811 | TestDelegate d; |
| 4812 | BlockingNetworkDelegate network_delegate( |
| 4813 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4814 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4815 | GURL redirect_url("http://does.not.resolve.test/simple.html"); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4816 | network_delegate.set_redirect_on_headers_received_url(redirect_url); |
| 4817 | |
| 4818 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4819 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4820 | |
| 4821 | { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4822 | GURL original_url("http://does.not.resolve.test/defaultresponse"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4823 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4824 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4825 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4826 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4827 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4828 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4829 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4830 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4831 | http_test_server()->host_port_pair()), |
| 4832 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4833 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4834 | // through an untunneled proxy. |
| 4835 | EXPECT_EQ(2, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4836 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4837 | http_test_server()->host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4838 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4839 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4840 | EXPECT_EQ(redirect_url, r->url()); |
| 4841 | EXPECT_EQ(original_url, r->original_url()); |
| 4842 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4843 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 4844 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4845 | } |
| 4846 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4847 | } |
| 4848 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4849 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4850 | // by taking no action. This indicates that the NetworkDelegate does not want to |
| 4851 | // handle the challenge, and is passing the buck along to the |
| 4852 | // URLRequest::Delegate. |
| 4853 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4854 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4855 | |
| 4856 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4857 | BlockingNetworkDelegate network_delegate( |
| 4858 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4859 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4860 | TestURLRequestContext context(true); |
| 4861 | context.set_network_delegate(&network_delegate); |
| 4862 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4863 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4864 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4865 | |
| 4866 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4867 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4868 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4869 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4870 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4871 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4872 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4873 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4874 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4875 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4876 | EXPECT_TRUE(d.auth_required_called()); |
| 4877 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4878 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4879 | } |
| 4880 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4881 | } |
| 4882 | |
| 4883 | TEST_F(URLRequestTestHTTP, |
| 4884 | NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4885 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4886 | |
| 4887 | TestDelegate d; |
| 4888 | BlockingNetworkDelegate network_delegate( |
| 4889 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4890 | |
| 4891 | TestURLRequestContext context(true); |
| 4892 | context.set_network_delegate(&network_delegate); |
| 4893 | context.Init(); |
| 4894 | |
| 4895 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 4896 | |
| 4897 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4898 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4899 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4900 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4901 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4902 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4903 | d.RunUntilComplete(); |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4904 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4905 | { |
| 4906 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4907 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4908 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4909 | } |
| 4910 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4911 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4912 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4913 | EXPECT_TRUE(d.auth_required_called()); |
| 4914 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4915 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4916 | } |
| 4917 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4918 | } |
| 4919 | |
| 4920 | // Tests that the network delegate can synchronously complete OnAuthRequired |
[email protected] | 1e110eae | 2013-05-10 22:02:40 | [diff] [blame] | 4921 | // by setting credentials. |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4922 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4923 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4924 | |
| 4925 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4926 | BlockingNetworkDelegate network_delegate( |
| 4927 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4928 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4929 | network_delegate.set_auth_retval( |
| 4930 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4931 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4932 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4933 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4934 | TestURLRequestContext context(true); |
| 4935 | context.set_network_delegate(&network_delegate); |
| 4936 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4937 | |
| 4938 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4939 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4940 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4941 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4942 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4943 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4944 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4945 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4946 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4947 | EXPECT_FALSE(d.auth_required_called()); |
| 4948 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4949 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4950 | } |
| 4951 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4952 | } |
| 4953 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4954 | // Same as above, but also tests that GetFullRequestHeaders returns the proper |
| 4955 | // headers (for the first or second request) when called at the proper times. |
| 4956 | TEST_F(URLRequestTestHTTP, |
| 4957 | NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4958 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4959 | |
| 4960 | TestDelegate d; |
| 4961 | BlockingNetworkDelegate network_delegate( |
| 4962 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4963 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 4964 | network_delegate.set_auth_retval( |
| 4965 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4966 | |
| 4967 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
| 4968 | |
| 4969 | TestURLRequestContext context(true); |
| 4970 | context.set_network_delegate(&network_delegate); |
| 4971 | context.Init(); |
| 4972 | |
| 4973 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4974 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4975 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4976 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4977 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4978 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4979 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4980 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4981 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4982 | EXPECT_FALSE(d.auth_required_called()); |
| 4983 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4984 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4985 | |
| 4986 | { |
| 4987 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4988 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4989 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
| 4990 | } |
| 4991 | } |
| 4992 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4993 | } |
| 4994 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4995 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4996 | // by cancelling authentication. |
| 4997 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4998 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4999 | |
| 5000 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5001 | BlockingNetworkDelegate network_delegate( |
| 5002 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 5003 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5004 | network_delegate.set_auth_retval( |
| 5005 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 5006 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5007 | TestURLRequestContext context(true); |
| 5008 | context.set_network_delegate(&network_delegate); |
| 5009 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5010 | |
| 5011 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5012 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5013 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5014 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5015 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5016 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5017 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5018 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5019 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5020 | EXPECT_FALSE(d.auth_required_called()); |
| 5021 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5022 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5023 | } |
| 5024 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5025 | } |
| 5026 | |
| 5027 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 5028 | // by taking no action. This indicates that the NetworkDelegate does not want |
| 5029 | // to handle the challenge, and is passing the buck along to the |
| 5030 | // URLRequest::Delegate. |
| 5031 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5032 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5033 | |
| 5034 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5035 | BlockingNetworkDelegate network_delegate( |
| 5036 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 5037 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5038 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5039 | TestURLRequestContext context(true); |
| 5040 | context.set_network_delegate(&network_delegate); |
| 5041 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5042 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 5043 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5044 | |
| 5045 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5046 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5047 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5048 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5049 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5050 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5051 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5052 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5053 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5054 | EXPECT_TRUE(d.auth_required_called()); |
| 5055 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5056 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5057 | } |
| 5058 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5059 | } |
| 5060 | |
| 5061 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 5062 | // by setting credentials. |
| 5063 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5064 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5065 | |
| 5066 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5067 | BlockingNetworkDelegate network_delegate( |
| 5068 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 5069 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5070 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5071 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 5072 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 5073 | AuthCredentials auth_credentials(kUser, kSecret); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5074 | network_delegate.set_auth_credentials(auth_credentials); |
| 5075 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5076 | TestURLRequestContext context(true); |
| 5077 | context.set_network_delegate(&network_delegate); |
| 5078 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5079 | |
| 5080 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5081 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5082 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5083 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5084 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5085 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5086 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5087 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5088 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5089 | EXPECT_FALSE(d.auth_required_called()); |
| 5090 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5091 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5092 | } |
| 5093 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5094 | } |
| 5095 | |
| 5096 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 5097 | // by cancelling authentication. |
| 5098 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5099 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5100 | |
| 5101 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5102 | BlockingNetworkDelegate network_delegate( |
| 5103 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 5104 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5105 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5106 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 5107 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5108 | TestURLRequestContext context(true); |
| 5109 | context.set_network_delegate(&network_delegate); |
| 5110 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5111 | |
| 5112 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5113 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5114 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5115 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5116 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5117 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5118 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5119 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5120 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5121 | EXPECT_FALSE(d.auth_required_called()); |
| 5122 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5123 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5124 | } |
| 5125 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5126 | } |
| 5127 | |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5128 | // Tests that NetworkDelegate header overrides from the 401 response do not |
| 5129 | // affect the 200 response. This is a regression test for |
| 5130 | // https://crbug.com/801237. |
| 5131 | TEST_F(URLRequestTestHTTP, NetworkDelegateOverrideHeadersWithAuth) { |
| 5132 | ASSERT_TRUE(http_test_server()->Start()); |
| 5133 | |
| 5134 | TestDelegate d; |
| 5135 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 5136 | default_network_delegate_.set_add_header_to_first_response(true); |
| 5137 | |
| 5138 | { |
| 5139 | GURL url(http_test_server()->GetURL("/auth-basic")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5140 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5141 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 5142 | r->Start(); |
| 5143 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5144 | d.RunUntilComplete(); |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5145 | |
| 5146 | EXPECT_EQ(OK, d.request_status()); |
| 5147 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5148 | EXPECT_TRUE(d.auth_required_called()); |
| 5149 | EXPECT_FALSE(r->response_headers()->HasHeader("X-Network-Delegate")); |
| 5150 | } |
| 5151 | |
| 5152 | { |
| 5153 | GURL url(http_test_server()->GetURL("/defaultresponse")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5154 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5155 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 5156 | r->Start(); |
| 5157 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5158 | d.RunUntilComplete(); |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5159 | |
| 5160 | // Check that set_add_header_to_first_response normally adds a header. |
| 5161 | EXPECT_EQ(OK, d.request_status()); |
| 5162 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5163 | EXPECT_TRUE(r->response_headers()->HasHeader("X-Network-Delegate")); |
| 5164 | } |
| 5165 | } |
| 5166 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5167 | // Tests that we can handle when a network request was canceled while we were |
| 5168 | // waiting for the network delegate. |
| 5169 | // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 5170 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5171 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5172 | |
| 5173 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5174 | BlockingNetworkDelegate network_delegate( |
| 5175 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5176 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5177 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5178 | TestURLRequestContext context(true); |
| 5179 | context.set_network_delegate(&network_delegate); |
| 5180 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5181 | |
| 5182 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5183 | std::unique_ptr<URLRequest> r( |
| 5184 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 5185 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5186 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5187 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5188 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5189 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 5190 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5191 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5192 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5193 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5194 | // Ensure that network delegate is notified. |
| 5195 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5196 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5197 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5198 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5199 | } |
| 5200 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5201 | } |
| 5202 | |
| 5203 | // Tests that we can handle when a network request was canceled while we were |
| 5204 | // waiting for the network delegate. |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5205 | // Part 2: Request is cancelled while waiting for OnBeforeStartTransaction |
| 5206 | // callback. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5207 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5208 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5209 | |
| 5210 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5211 | BlockingNetworkDelegate network_delegate( |
| 5212 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5213 | network_delegate.set_block_on( |
| 5214 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5215 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5216 | TestURLRequestContext context(true); |
| 5217 | context.set_network_delegate(&network_delegate); |
| 5218 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5219 | |
| 5220 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5221 | std::unique_ptr<URLRequest> r( |
| 5222 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 5223 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5224 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5225 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5226 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5227 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 5228 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5229 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5230 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5231 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5232 | // Ensure that network delegate is notified. |
| 5233 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5234 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5235 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5236 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5237 | } |
| 5238 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5239 | } |
| 5240 | |
| 5241 | // Tests that we can handle when a network request was canceled while we were |
| 5242 | // waiting for the network delegate. |
| 5243 | // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 5244 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5245 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5246 | |
| 5247 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5248 | BlockingNetworkDelegate network_delegate( |
| 5249 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5250 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5251 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5252 | TestURLRequestContext context(true); |
| 5253 | context.set_network_delegate(&network_delegate); |
| 5254 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5255 | |
| 5256 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5257 | std::unique_ptr<URLRequest> r( |
| 5258 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 5259 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5260 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5261 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5262 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5263 | EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 5264 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5265 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5266 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5267 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5268 | // Ensure that network delegate is notified. |
| 5269 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5270 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5271 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5272 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5273 | } |
| 5274 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5275 | } |
| 5276 | |
| 5277 | // Tests that we can handle when a network request was canceled while we were |
| 5278 | // waiting for the network delegate. |
| 5279 | // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
[email protected] | bfe8b30 | 2013-02-15 12:16:02 | [diff] [blame] | 5280 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5281 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5282 | |
| 5283 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5284 | BlockingNetworkDelegate network_delegate( |
| 5285 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5286 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5287 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5288 | TestURLRequestContext context(true); |
| 5289 | context.set_network_delegate(&network_delegate); |
| 5290 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5291 | |
| 5292 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5293 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5294 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 5295 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5296 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5297 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5298 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5299 | EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED, |
| 5300 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5301 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5302 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5303 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5304 | // Ensure that network delegate is notified. |
| 5305 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5306 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5307 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5308 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5309 | } |
| 5310 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5311 | } |
| 5312 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5313 | namespace { |
| 5314 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5315 | std::unique_ptr<test_server::HttpResponse> HandleServerAuthConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5316 | const test_server::HttpRequest& request) { |
| 5317 | if (request.headers.find("Host") == request.headers.end() || |
| 5318 | request.headers.at("Host") != "www.server-auth.com" || |
| 5319 | request.method != test_server::METHOD_CONNECT) { |
| 5320 | return nullptr; |
| 5321 | } |
| 5322 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5323 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5324 | new test_server::BasicHttpResponse); |
| 5325 | http_response->set_code(HTTP_UNAUTHORIZED); |
| 5326 | http_response->AddCustomHeader("WWW-Authenticate", |
| 5327 | "Basic realm=\"WallyWorld\""); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 5328 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5329 | } |
| 5330 | |
| 5331 | } // namespace |
| 5332 | |
| 5333 | // 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] | 5334 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5335 | // The EmbeddedTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5336 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5337 | http_test_server()->RegisterRequestHandler( |
Matt Menke | 2b2a6047 | 2018-02-07 02:12:12 | [diff] [blame] | 5338 | base::BindRepeating(&HandleServerAuthConnect)); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5339 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5340 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5341 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 5342 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5343 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 5344 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5345 | TestDelegate d; |
| 5346 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5347 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5348 | GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d, |
| 5349 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5350 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5351 | r->Start(); |
| 5352 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5353 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5354 | d.RunUntilComplete(); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5355 | |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 5356 | // The proxy server should be set before failure. |
| 5357 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 5358 | http_test_server()->host_port_pair()), |
| 5359 | r->proxy_server()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5360 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5361 | } |
| 5362 | } |
| 5363 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5364 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5365 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5366 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5367 | TestDelegate d; |
| 5368 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5369 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5370 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5371 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5372 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5373 | r->Start(); |
| 5374 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5375 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5376 | d.RunUntilComplete(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5377 | |
| 5378 | EXPECT_EQ(1, d.response_started_count()); |
| 5379 | EXPECT_FALSE(d.received_data_before_response()); |
| 5380 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5381 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5382 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5383 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5384 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 5385 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5386 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5387 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5388 | } |
| 5389 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5390 | // This test has the server send a large number of cookies to the client. |
| 5391 | // To ensure that no number of cookies causes a crash, a galloping binary |
| 5392 | // search is used to estimate that maximum number of cookies that are accepted |
| 5393 | // by the browser. Beyond the maximum number, the request will fail with |
| 5394 | // ERR_RESPONSE_HEADERS_TOO_BIG. |
Sergey Ulanov | 0fb900c | 2017-09-20 00:27:31 | [diff] [blame] | 5395 | #if defined(OS_WIN) || defined(OS_FUCHSIA) |
[email protected] | 69dd6fe | 2013-02-23 23:15:30 | [diff] [blame] | 5396 | // http://crbug.com/177916 |
| 5397 | #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies |
| 5398 | #else |
| 5399 | #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies |
| 5400 | #endif // defined(OS_WIN) |
| 5401 | TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5402 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5403 | |
| 5404 | int lower_bound = 0; |
| 5405 | int upper_bound = 1; |
| 5406 | |
| 5407 | // Double the number of cookies until the response header limits are |
| 5408 | // exceeded. |
| 5409 | while (DoManyCookiesRequest(upper_bound)) { |
| 5410 | lower_bound = upper_bound; |
| 5411 | upper_bound *= 2; |
| 5412 | ASSERT_LT(upper_bound, 1000000); |
| 5413 | } |
| 5414 | |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 5415 | int tolerance = static_cast<int>(upper_bound * 0.005); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5416 | if (tolerance < 2) |
| 5417 | tolerance = 2; |
| 5418 | |
| 5419 | // Perform a binary search to find the highest possible number of cookies, |
| 5420 | // within the desired tolerance. |
| 5421 | while (upper_bound - lower_bound >= tolerance) { |
| 5422 | int num_cookies = (lower_bound + upper_bound) / 2; |
| 5423 | |
| 5424 | if (DoManyCookiesRequest(num_cookies)) |
| 5425 | lower_bound = num_cookies; |
| 5426 | else |
| 5427 | upper_bound = num_cookies; |
| 5428 | } |
| 5429 | // Success: the test did not crash. |
| 5430 | } |
| 5431 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5432 | TEST_F(URLRequestTestHTTP, GetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5433 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5434 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5435 | TestDelegate d; |
| 5436 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5437 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5438 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5439 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5440 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5441 | r->Start(); |
| 5442 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5443 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5444 | d.RunUntilComplete(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5445 | |
| 5446 | EXPECT_EQ(1, d.response_started_count()); |
| 5447 | EXPECT_FALSE(d.received_data_before_response()); |
| 5448 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5449 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5450 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5451 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5452 | r->GetResponseRemoteEndpoint().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5453 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 5454 | } |
| 5455 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5456 | TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5457 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5458 | |
| 5459 | TestDelegate d; |
| 5460 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5461 | GURL test_url(http_test_server()->GetURL("/defaultresponse")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5462 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5463 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5464 | |
| 5465 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5466 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5467 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5468 | r->Start(); |
| 5469 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5470 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5471 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5472 | |
| 5473 | EXPECT_EQ(1, d.response_started_count()); |
| 5474 | EXPECT_FALSE(d.received_data_before_response()); |
| 5475 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5476 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5477 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5478 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5479 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5480 | |
| 5481 | EXPECT_TRUE(d.have_full_request_headers()); |
| 5482 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 5483 | } |
| 5484 | } |
| 5485 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5486 | TEST_F(URLRequestTestHTTP, GetTestLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5487 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5488 | |
| 5489 | TestDelegate d; |
| 5490 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5491 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5492 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5493 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5494 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5495 | r->Start(); |
| 5496 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5497 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5498 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5499 | |
| 5500 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5501 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5502 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5503 | |
| 5504 | EXPECT_EQ(1, d.response_started_count()); |
| 5505 | EXPECT_FALSE(d.received_data_before_response()); |
| 5506 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5507 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5508 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5509 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5510 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5511 | } |
| 5512 | } |
| 5513 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5514 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 5515 | #if !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5516 | TEST_F(URLRequestTestHTTP, GetZippedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5517 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5518 | base::FilePath(kTestFilePath)); |
| 5519 | |
| 5520 | ASSERT_TRUE(test_server.Start()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5521 | |
| 5522 | // Parameter that specifies the Content-Length field in the response: |
| 5523 | // C - Compressed length. |
| 5524 | // U - Uncompressed length. |
| 5525 | // L - Large length (larger than both C & U). |
| 5526 | // M - Medium length (between C & U). |
| 5527 | // S - Small length (smaller than both C & U). |
| 5528 | const char test_parameters[] = "CULMS"; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 5529 | const int num_tests = base::size(test_parameters) - 1; // Skip NULL. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5530 | // C & U should be OK. |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 5531 | // L & M are larger than the data sent, and show an error. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5532 | // S has too little data, but we seem to accept it. |
| 5533 | const bool test_expect_success[num_tests] = |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 5534 | { true, true, false, false, true }; |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5535 | |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5536 | base::FilePath file_path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 5537 | base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5538 | file_path = file_path.Append(kTestFilePath); |
| 5539 | file_path = file_path.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); |
| 5540 | std::string expected_content; |
| 5541 | ASSERT_TRUE(base::ReadFileToString(file_path, &expected_content)); |
| 5542 | |
| 5543 | for (int i = 0; i < num_tests; i++) { |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5544 | TestDelegate d; |
| 5545 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5546 | std::string test_file = base::StringPrintf( |
| 5547 | "compressedfiles/BullRunSpeech.txt?%c", test_parameters[i]); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5548 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5549 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5550 | TestURLRequestContext context(true); |
| 5551 | context.set_network_delegate(&network_delegate); |
| 5552 | context.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 5553 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5554 | std::unique_ptr<URLRequest> r( |
| 5555 | context.CreateRequest(test_server.GetURL(test_file), DEFAULT_PRIORITY, |
| 5556 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5557 | r->Start(); |
| 5558 | EXPECT_TRUE(r->is_pending()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5559 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5560 | d.RunUntilComplete(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5561 | |
| 5562 | EXPECT_EQ(1, d.response_started_count()); |
| 5563 | EXPECT_FALSE(d.received_data_before_response()); |
| 5564 | VLOG(1) << " Received " << d.bytes_received() << " bytes" |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5565 | << " error = " << d.request_status(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5566 | if (test_expect_success[i]) { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5567 | EXPECT_EQ(OK, d.request_status()) << " Parameter = \"" << test_file |
| 5568 | << "\""; |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5569 | if (test_parameters[i] == 'S') { |
| 5570 | // When content length is smaller than both compressed length and |
| 5571 | // uncompressed length, HttpStreamParser might not read the full |
| 5572 | // response body. |
| 5573 | continue; |
| 5574 | } |
| 5575 | EXPECT_EQ(expected_content, d.data_received()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5576 | } else { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5577 | EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, d.request_status()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5578 | << " Parameter = \"" << test_file << "\""; |
| 5579 | } |
| 5580 | } |
| 5581 | } |
| 5582 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5583 | #endif // !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5584 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5585 | TEST_F(URLRequestTestHTTP, RedirectLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5586 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5587 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5588 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5589 | GURL original_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5590 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5591 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5592 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5593 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5594 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5595 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5596 | |
| 5597 | EXPECT_EQ(1, d.response_started_count()); |
| 5598 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5599 | EXPECT_EQ(destination_url, req->url()); |
| 5600 | EXPECT_EQ(original_url, req->original_url()); |
| 5601 | ASSERT_EQ(2U, req->url_chain().size()); |
| 5602 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5603 | EXPECT_EQ(destination_url, req->url_chain()[1]); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5604 | |
| 5605 | LoadTimingInfo load_timing_info_before_redirect; |
| 5606 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect( |
| 5607 | &load_timing_info_before_redirect)); |
| 5608 | TestLoadTimingNotReused(load_timing_info_before_redirect, |
| 5609 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 5610 | |
| 5611 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5612 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5613 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5614 | |
| 5615 | // Check that a new socket was used on redirect, since the server does not |
| 5616 | // supposed keep-alive sockets, and that the times before the redirect are |
| 5617 | // before the ones recorded for the second request. |
| 5618 | EXPECT_NE(load_timing_info_before_redirect.socket_log_id, |
| 5619 | load_timing_info.socket_log_id); |
| 5620 | EXPECT_LE(load_timing_info_before_redirect.receive_headers_end, |
| 5621 | load_timing_info.connect_timing.connect_start); |
| 5622 | } |
| 5623 | |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5624 | TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5625 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5626 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5627 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5628 | GURL middle_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5629 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
| 5630 | GURL original_url = http_test_server()->GetURL("/server-redirect?" + |
| 5631 | middle_redirect_url.spec()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5632 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5633 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5634 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5635 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5636 | d.RunUntilComplete(); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5637 | |
| 5638 | EXPECT_EQ(1, d.response_started_count()); |
| 5639 | EXPECT_EQ(2, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5640 | EXPECT_EQ(destination_url, req->url()); |
| 5641 | EXPECT_EQ(original_url, req->original_url()); |
| 5642 | ASSERT_EQ(3U, req->url_chain().size()); |
| 5643 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5644 | EXPECT_EQ(middle_redirect_url, req->url_chain()[1]); |
| 5645 | EXPECT_EQ(destination_url, req->url_chain()[2]); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5646 | } |
| 5647 | |
David Benjamin | a7fde61 | 2019-03-15 14:20:58 | [diff] [blame] | 5648 | // This is a regression test for https://crbug.com/942073. |
| 5649 | TEST_F(URLRequestTestHTTP, RedirectEscaping) { |
| 5650 | ASSERT_TRUE(http_test_server()->Start()); |
| 5651 | |
| 5652 | // Assemble the destination URL as a string so it is not escaped by GURL. |
| 5653 | GURL destination_base = http_test_server()->GetURL("/defaultresponse"); |
| 5654 | // Add a URL fragment of U+2603 unescaped, U+2603 escaped, and then a UTF-8 |
| 5655 | // encoding error. |
| 5656 | std::string destination_url = |
| 5657 | destination_base.spec() + "#\xE2\x98\x83_%E2%98%83_\xE0\xE0"; |
| 5658 | // Redirect resolution should percent-escape bytes and preserve the UTF-8 |
| 5659 | // error at the end. |
| 5660 | std::string destination_escaped = |
| 5661 | destination_base.spec() + "#%E2%98%83_%E2%98%83_%E0%E0"; |
| 5662 | GURL original_url = http_test_server()->GetURL( |
| 5663 | "/server-redirect?" + EscapeQueryParamValue(destination_url, false)); |
| 5664 | TestDelegate d; |
| 5665 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 5666 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 5667 | req->Start(); |
| 5668 | d.RunUntilComplete(); |
| 5669 | |
| 5670 | EXPECT_EQ(1, d.response_started_count()); |
| 5671 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5672 | EXPECT_EQ(destination_escaped, req->url().spec()); |
| 5673 | EXPECT_EQ(original_url, req->original_url()); |
| 5674 | ASSERT_EQ(2U, req->url_chain().size()); |
| 5675 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5676 | EXPECT_EQ(destination_escaped, req->url_chain()[1].spec()); |
| 5677 | } |
| 5678 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5679 | // First and second pieces of information logged by delegates to URLRequests. |
| 5680 | const char kFirstDelegateInfo[] = "Wonderful delegate"; |
| 5681 | const char kSecondDelegateInfo[] = "Exciting delegate"; |
| 5682 | |
| 5683 | // Logs delegate information to a URLRequest. The first string is logged |
| 5684 | // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is |
| 5685 | // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then |
| 5686 | // another asynchronous call is used to clear the delegate information |
| 5687 | // before calling a callback. The object then deletes itself. |
| 5688 | class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> { |
| 5689 | public: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5690 | using Callback = base::OnceCallback<void()>; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5691 | |
| 5692 | // Each time delegate information is added to the URLRequest, the resulting |
| 5693 | // load state is checked. The expected load state after each request is |
| 5694 | // passed in as an argument. |
| 5695 | static void Run(URLRequest* url_request, |
| 5696 | LoadState expected_first_load_state, |
| 5697 | LoadState expected_second_load_state, |
| 5698 | LoadState expected_third_load_state, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5699 | Callback callback) { |
tzik | 38f920a | 2018-08-01 04:41:20 | [diff] [blame] | 5700 | // base::MakeRefCounted<AsyncDelegateLogger> is unavailable here, since the |
| 5701 | // constructor of AsyncDelegateLogger is private. |
| 5702 | auto logger = base::WrapRefCounted(new AsyncDelegateLogger( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5703 | url_request, expected_first_load_state, expected_second_load_state, |
tzik | 38f920a | 2018-08-01 04:41:20 | [diff] [blame] | 5704 | expected_third_load_state, std::move(callback))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5705 | logger->Start(); |
| 5706 | } |
| 5707 | |
| 5708 | // Checks that the log entries, starting with log_position, contain the |
| 5709 | // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have |
| 5710 | // recorded. Returns the index of entry after the expected number of |
| 5711 | // events this logged, or entries.size() if there aren't enough entries. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5712 | static size_t CheckDelegateInfo(const TestNetLogEntry::List& entries, |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 5713 | size_t log_position) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5714 | // There should be 4 DELEGATE_INFO events: Two begins and two ends. |
| 5715 | if (log_position + 3 >= entries.size()) { |
| 5716 | ADD_FAILURE() << "Not enough log entries"; |
| 5717 | return entries.size(); |
| 5718 | } |
| 5719 | std::string delegate_info; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5720 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5721 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
rdsmith | 37a0dde | 2017-01-04 00:12:07 | [diff] [blame] | 5722 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_blocked_by", |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5723 | &delegate_info)); |
| 5724 | EXPECT_EQ(kFirstDelegateInfo, delegate_info); |
| 5725 | |
| 5726 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5727 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5728 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5729 | |
| 5730 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5731 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5732 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
rdsmith | 37a0dde | 2017-01-04 00:12:07 | [diff] [blame] | 5733 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_blocked_by", |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5734 | &delegate_info)); |
| 5735 | EXPECT_EQ(kSecondDelegateInfo, delegate_info); |
| 5736 | |
| 5737 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5738 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5739 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5740 | |
| 5741 | return log_position + 1; |
| 5742 | } |
| 5743 | |
| 5744 | private: |
| 5745 | friend class base::RefCounted<AsyncDelegateLogger>; |
| 5746 | |
| 5747 | AsyncDelegateLogger(URLRequest* url_request, |
| 5748 | LoadState expected_first_load_state, |
| 5749 | LoadState expected_second_load_state, |
| 5750 | LoadState expected_third_load_state, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5751 | Callback callback) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5752 | : url_request_(url_request), |
| 5753 | expected_first_load_state_(expected_first_load_state), |
| 5754 | expected_second_load_state_(expected_second_load_state), |
| 5755 | expected_third_load_state_(expected_third_load_state), |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5756 | callback_(std::move(callback)) {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5757 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5758 | ~AsyncDelegateLogger() = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5759 | |
| 5760 | void Start() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5761 | url_request_->LogBlockedBy(kFirstDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5762 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5763 | EXPECT_EQ(expected_first_load_state_, load_state.state); |
| 5764 | EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5765 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5766 | FROM_HERE, |
| 5767 | base::BindOnce(&AsyncDelegateLogger::LogSecondDelegate, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5768 | } |
| 5769 | |
| 5770 | void LogSecondDelegate() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5771 | url_request_->LogAndReportBlockedBy(kSecondDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5772 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5773 | EXPECT_EQ(expected_second_load_state_, load_state.state); |
| 5774 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) { |
| 5775 | EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5776 | } else { |
| 5777 | EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5778 | } |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5779 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5780 | FROM_HERE, base::BindOnce(&AsyncDelegateLogger::LogComplete, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5781 | } |
| 5782 | |
| 5783 | void LogComplete() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5784 | url_request_->LogUnblocked(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5785 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5786 | EXPECT_EQ(expected_third_load_state_, load_state.state); |
| 5787 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5788 | EXPECT_EQ(base::string16(), load_state.param); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5789 | std::move(callback_).Run(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5790 | } |
| 5791 | |
| 5792 | URLRequest* url_request_; |
| 5793 | const int expected_first_load_state_; |
| 5794 | const int expected_second_load_state_; |
| 5795 | const int expected_third_load_state_; |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5796 | Callback callback_; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5797 | |
| 5798 | DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger); |
| 5799 | }; |
| 5800 | |
| 5801 | // NetworkDelegate that logs delegate information before a request is started, |
| 5802 | // before headers are sent, when headers are read, and when auth information |
| 5803 | // is requested. Uses AsyncDelegateLogger. |
| 5804 | class AsyncLoggingNetworkDelegate : public TestNetworkDelegate { |
| 5805 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5806 | AsyncLoggingNetworkDelegate() = default; |
| 5807 | ~AsyncLoggingNetworkDelegate() override = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5808 | |
| 5809 | // NetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5810 | int OnBeforeURLRequest(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5811 | CompletionOnceCallback callback, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5812 | GURL* new_url) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5813 | // TestNetworkDelegate always completes synchronously. |
| 5814 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeURLRequest( |
| 5815 | request, base::NullCallback(), new_url)); |
| 5816 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5817 | } |
| 5818 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5819 | int OnBeforeStartTransaction(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5820 | CompletionOnceCallback callback, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5821 | HttpRequestHeaders* headers) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5822 | // TestNetworkDelegate always completes synchronously. |
| 5823 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeStartTransaction( |
| 5824 | request, base::NullCallback(), headers)); |
| 5825 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5826 | } |
| 5827 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5828 | int OnHeadersReceived( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5829 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5830 | CompletionOnceCallback callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5831 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5832 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5833 | GURL* allowed_unsafe_redirect_url) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5834 | // TestNetworkDelegate always completes synchronously. |
| 5835 | CHECK_NE(ERR_IO_PENDING, |
| 5836 | TestNetworkDelegate::OnHeadersReceived( |
| 5837 | request, base::NullCallback(), original_response_headers, |
| 5838 | override_response_headers, allowed_unsafe_redirect_url)); |
| 5839 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5840 | } |
| 5841 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5842 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5843 | URLRequest* request, |
| 5844 | const AuthChallengeInfo& auth_info, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5845 | AuthCallback callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5846 | AuthCredentials* credentials) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5847 | AsyncDelegateLogger::Run( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5848 | request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5849 | LOAD_STATE_WAITING_FOR_DELEGATE, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5850 | base::BindOnce(&AsyncLoggingNetworkDelegate::SetAuthAndResume, |
| 5851 | std::move(callback), credentials)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5852 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 5853 | } |
| 5854 | |
| 5855 | private: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5856 | static int RunCallbackAsynchronously(URLRequest* request, |
| 5857 | CompletionOnceCallback callback) { |
| 5858 | AsyncDelegateLogger::Run(request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5859 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5860 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5861 | base::BindOnce(std::move(callback), OK)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5862 | return ERR_IO_PENDING; |
| 5863 | } |
| 5864 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5865 | static void SetAuthAndResume(AuthCallback callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5866 | AuthCredentials* credentials) { |
| 5867 | *credentials = AuthCredentials(kUser, kSecret); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5868 | std::move(callback).Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5869 | } |
| 5870 | |
| 5871 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate); |
| 5872 | }; |
| 5873 | |
| 5874 | // URLRequest::Delegate that logs delegate information when the headers |
| 5875 | // are received, when each read completes, and during redirects. Uses |
| 5876 | // AsyncDelegateLogger. Can optionally cancel a request in any phase. |
| 5877 | // |
| 5878 | // Inherits from TestDelegate to reuse the TestDelegate code to handle |
| 5879 | // advancing to the next step in most cases, as well as cancellation. |
| 5880 | class AsyncLoggingUrlRequestDelegate : public TestDelegate { |
| 5881 | public: |
| 5882 | enum CancelStage { |
| 5883 | NO_CANCEL = 0, |
| 5884 | CANCEL_ON_RECEIVED_REDIRECT, |
| 5885 | CANCEL_ON_RESPONSE_STARTED, |
| 5886 | CANCEL_ON_READ_COMPLETED |
| 5887 | }; |
| 5888 | |
| 5889 | explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage) |
| 5890 | : cancel_stage_(cancel_stage) { |
| 5891 | if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT) |
| 5892 | set_cancel_in_received_redirect(true); |
| 5893 | else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED) |
| 5894 | set_cancel_in_response_started(true); |
| 5895 | else if (cancel_stage == CANCEL_ON_READ_COMPLETED) |
| 5896 | set_cancel_in_received_data(true); |
| 5897 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5898 | ~AsyncLoggingUrlRequestDelegate() override = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5899 | |
| 5900 | // URLRequest::Delegate implementation: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5901 | void OnReceivedRedirect(URLRequest* request, |
| 5902 | const RedirectInfo& redirect_info, |
| 5903 | bool* defer_redirect) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5904 | *defer_redirect = true; |
| 5905 | AsyncDelegateLogger::Run( |
| 5906 | request, |
| 5907 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5908 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5909 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5910 | base::Bind( |
| 5911 | &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5912 | base::Unretained(this), request, redirect_info)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5913 | } |
| 5914 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5915 | void OnResponseStarted(URLRequest* request, int net_error) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5916 | AsyncDelegateLogger::Run( |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5917 | request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5918 | LOAD_STATE_WAITING_FOR_DELEGATE, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5919 | base::Bind( |
| 5920 | &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete, |
| 5921 | base::Unretained(this), request, net_error)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5922 | } |
| 5923 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5924 | void OnReadCompleted(URLRequest* request, int bytes_read) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5925 | AsyncDelegateLogger::Run( |
| 5926 | request, |
| 5927 | LOAD_STATE_IDLE, |
| 5928 | LOAD_STATE_IDLE, |
| 5929 | LOAD_STATE_IDLE, |
| 5930 | base::Bind( |
| 5931 | &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete, |
| 5932 | base::Unretained(this), request, bytes_read)); |
| 5933 | } |
| 5934 | |
| 5935 | private: |
| 5936 | void OnReceivedRedirectLoggingComplete(URLRequest* request, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5937 | const RedirectInfo& redirect_info) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5938 | bool defer_redirect = false; |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5939 | TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5940 | // FollowDeferredRedirect should not be called after cancellation. |
| 5941 | if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT) |
| 5942 | return; |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 5943 | if (!defer_redirect) { |
| 5944 | request->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 5945 | base::nullopt /* modified_headers */); |
| 5946 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5947 | } |
| 5948 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5949 | void OnResponseStartedLoggingComplete(URLRequest* request, int net_error) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5950 | // The parent class continues the request. |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5951 | TestDelegate::OnResponseStarted(request, net_error); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5952 | } |
| 5953 | |
| 5954 | void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) { |
| 5955 | // The parent class continues the request. |
| 5956 | TestDelegate::OnReadCompleted(request, bytes_read); |
| 5957 | } |
| 5958 | |
| 5959 | const CancelStage cancel_stage_; |
| 5960 | |
| 5961 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate); |
| 5962 | }; |
| 5963 | |
| 5964 | // Tests handling of delegate info before a request starts. |
| 5965 | TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5966 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5967 | |
| 5968 | TestDelegate request_delegate; |
| 5969 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 5970 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5971 | context.set_net_log(&net_log_); |
| 5972 | context.Init(); |
| 5973 | |
| 5974 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5975 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5976 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, |
| 5977 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5978 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5979 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5980 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5981 | |
| 5982 | AsyncDelegateLogger::Run( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5983 | r.get(), |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5984 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5985 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5986 | LOAD_STATE_IDLE, |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5987 | base::Bind(&URLRequest::Start, base::Unretained(r.get()))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5988 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5989 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5990 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5991 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5992 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5993 | } |
| 5994 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5995 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5996 | net_log_.GetEntries(&entries); |
| 5997 | size_t log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5998 | entries, 0, NetLogEventType::DELEGATE_INFO, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5999 | |
| 6000 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position); |
| 6001 | |
| 6002 | // Nothing else should add any delegate info to the request. |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6003 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6004 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6005 | } |
| 6006 | |
| 6007 | // Tests handling of delegate info from a network delegate. |
| 6008 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6009 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6010 | |
| 6011 | TestDelegate request_delegate; |
| 6012 | AsyncLoggingNetworkDelegate network_delegate; |
| 6013 | TestURLRequestContext context(true); |
| 6014 | context.set_network_delegate(&network_delegate); |
| 6015 | context.set_net_log(&net_log_); |
| 6016 | context.Init(); |
| 6017 | |
| 6018 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6019 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 6020 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, |
| 6021 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6022 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6023 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 6024 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6025 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6026 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6027 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6028 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6029 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6030 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6031 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 6032 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 6033 | } |
| 6034 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 6035 | |
| 6036 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6037 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6038 | net_log_.GetEntries(&entries); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6039 | static const NetLogEventType kExpectedEvents[] = { |
| 6040 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 6041 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6042 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6043 | }; |
| 6044 | for (NetLogEventType event : kExpectedEvents) { |
| 6045 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6046 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6047 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6048 | |
| 6049 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 6050 | log_position + 1); |
| 6051 | |
| 6052 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6053 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6054 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6055 | } |
| 6056 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6057 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6058 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6059 | } |
| 6060 | |
| 6061 | // Tests handling of delegate info from a network delegate in the case of an |
| 6062 | // HTTP redirect. |
| 6063 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6064 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6065 | |
| 6066 | TestDelegate request_delegate; |
| 6067 | AsyncLoggingNetworkDelegate network_delegate; |
| 6068 | TestURLRequestContext context(true); |
| 6069 | context.set_network_delegate(&network_delegate); |
| 6070 | context.set_net_log(&net_log_); |
| 6071 | context.Init(); |
| 6072 | |
| 6073 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6074 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6075 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6076 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6077 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6078 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 6079 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6080 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6081 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6082 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6083 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6084 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6085 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6086 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 6087 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 6088 | } |
| 6089 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 6090 | |
| 6091 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6092 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6093 | net_log_.GetEntries(&entries); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6094 | static const NetLogEventType kExpectedEvents[] = { |
| 6095 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 6096 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6097 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6098 | }; |
| 6099 | for (NetLogEventType event : kExpectedEvents) { |
| 6100 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6101 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6102 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6103 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6104 | log_position = |
| 6105 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6106 | |
| 6107 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6108 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6109 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6110 | } |
| 6111 | |
| 6112 | // The URLRequest::Delegate then gets informed about the redirect. |
| 6113 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6114 | entries, log_position + 1, |
| 6115 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6116 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6117 | |
| 6118 | // The NetworkDelegate logged information in the same three events as before. |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6119 | for (NetLogEventType event : kExpectedEvents) { |
| 6120 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6121 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6122 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6123 | |
| 6124 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 6125 | log_position + 1); |
| 6126 | |
| 6127 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6128 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6129 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6130 | } |
| 6131 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6132 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6133 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6134 | } |
| 6135 | |
| 6136 | // Tests handling of delegate info from a network delegate in the case of HTTP |
| 6137 | // AUTH. |
| 6138 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6139 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6140 | |
| 6141 | TestDelegate request_delegate; |
| 6142 | AsyncLoggingNetworkDelegate network_delegate; |
| 6143 | TestURLRequestContext context(true); |
| 6144 | context.set_network_delegate(&network_delegate); |
| 6145 | context.set_net_log(&net_log_); |
| 6146 | context.Init(); |
| 6147 | |
| 6148 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6149 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 6150 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, |
| 6151 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6152 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6153 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 6154 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6155 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6156 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6157 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6158 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6159 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6160 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6161 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 6162 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 6163 | } |
| 6164 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 6165 | |
| 6166 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6167 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6168 | net_log_.GetEntries(&entries); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6169 | static const NetLogEventType kExpectedEvents[] = { |
| 6170 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 6171 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6172 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6173 | NetLogEventType::NETWORK_DELEGATE_AUTH_REQUIRED, |
| 6174 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6175 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6176 | }; |
| 6177 | for (NetLogEventType event : kExpectedEvents) { |
| 6178 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6179 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6180 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6181 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6182 | log_position = |
| 6183 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6184 | |
| 6185 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6186 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6187 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6188 | } |
| 6189 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6190 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6191 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6192 | } |
| 6193 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6194 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 6195 | #if !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6196 | // Tests handling of delegate info from a URLRequest::Delegate. |
| 6197 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6198 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6199 | base::FilePath(kTestFilePath)); |
| 6200 | |
| 6201 | ASSERT_TRUE(test_server.Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6202 | |
| 6203 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 6204 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 6205 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6206 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6207 | context.set_net_log(&net_log_); |
| 6208 | context.Init(); |
| 6209 | |
| 6210 | { |
| 6211 | // A chunked response with delays between chunks is used to make sure that |
| 6212 | // attempts by the URLRequest delegate to log information while reading the |
| 6213 | // body are ignored. Since they are ignored, this test is robust against |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6214 | // the possibility of multiple reads being combined in the unlikely event |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6215 | // that it occurs. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6216 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6217 | test_server.GetURL("/chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6218 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6219 | LoadStateWithParam load_state = r->GetLoadState(); |
| 6220 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6221 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6222 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6223 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6224 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6225 | } |
| 6226 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6227 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6228 | net_log_.GetEntries(&entries); |
| 6229 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6230 | size_t log_position = 0; |
| 6231 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6232 | // The delegate info should only have been logged on header complete. Other |
| 6233 | // times it should silently be ignored. |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6234 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 6235 | entries, 0, NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST)); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6236 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6237 | entries, log_position + 1, |
| 6238 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6239 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6240 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6241 | log_position = |
| 6242 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6243 | |
| 6244 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6245 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 6246 | entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6247 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6248 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6249 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6250 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6251 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6252 | entries, log_position + 1, |
| 6253 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6254 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6255 | #endif // !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6256 | |
| 6257 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 6258 | // an HTTP redirect. |
| 6259 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6260 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6261 | |
| 6262 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 6263 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 6264 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6265 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6266 | context.set_net_log(&net_log_); |
| 6267 | context.Init(); |
| 6268 | |
| 6269 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6270 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6271 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6272 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6273 | LoadStateWithParam load_state = r->GetLoadState(); |
| 6274 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6275 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6276 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6277 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6278 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6279 | } |
| 6280 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6281 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6282 | net_log_.GetEntries(&entries); |
| 6283 | |
| 6284 | // Delegate info should only have been logged in OnReceivedRedirect and |
| 6285 | // OnResponseStarted. |
| 6286 | size_t log_position = 0; |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6287 | static const NetLogEventType kExpectedEvents[] = { |
| 6288 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
| 6289 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 6290 | }; |
| 6291 | for (NetLogEventType event : kExpectedEvents) { |
| 6292 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
| 6293 | log_position = ExpectLogContainsSomewhereAfter(entries, log_position, event, |
| 6294 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6295 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6296 | log_position = |
| 6297 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6298 | |
| 6299 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6300 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6301 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6302 | } |
| 6303 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6304 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6305 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6306 | } |
| 6307 | |
| 6308 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 6309 | // an HTTP redirect, with cancellation at various points. |
| 6310 | TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6311 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6312 | |
| 6313 | const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = { |
| 6314 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT, |
| 6315 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED, |
| 6316 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED, |
| 6317 | }; |
| 6318 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6319 | for (auto cancel_stage : kCancelStages) { |
| 6320 | AsyncLoggingUrlRequestDelegate request_delegate(cancel_stage); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 6321 | TestNetLog net_log; |
krasin | 0bfeb6b | 2017-01-13 21:48:04 | [diff] [blame] | 6322 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6323 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6324 | context.set_net_log(&net_log); |
| 6325 | context.Init(); |
| 6326 | |
| 6327 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6328 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6329 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6330 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6331 | LoadStateWithParam load_state = r->GetLoadState(); |
| 6332 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6333 | request_delegate.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6334 | EXPECT_EQ(ERR_ABORTED, request_delegate.request_status()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6335 | |
| 6336 | // Spin the message loop to run AsyncDelegateLogger task(s) posted after |
| 6337 | // the |request_delegate| completion task. |
| 6338 | base::RunLoop().RunUntilIdle(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6339 | } |
| 6340 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6341 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6342 | net_log.GetEntries(&entries); |
| 6343 | |
| 6344 | // Delegate info is always logged in both OnReceivedRedirect and |
| 6345 | // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the |
| 6346 | // OnResponseStarted delegate call is after cancellation, but logging is |
| 6347 | // still currently supported in that call. |
| 6348 | size_t log_position = 0; |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6349 | static const NetLogEventType kExpectedEvents[] = { |
| 6350 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
| 6351 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 6352 | }; |
| 6353 | for (NetLogEventType event : kExpectedEvents) { |
| 6354 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6355 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6356 | entries, log_position, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6357 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6358 | log_position = |
| 6359 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6360 | |
| 6361 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6362 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6363 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6364 | } |
| 6365 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6366 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6367 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6368 | } |
| 6369 | } |
| 6370 | |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6371 | namespace { |
| 6372 | |
| 6373 | const char kExtraHeader[] = "Allow-Snafu"; |
| 6374 | const char kExtraValue[] = "fubar"; |
| 6375 | |
| 6376 | class RedirectWithAdditionalHeadersDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 6377 | void OnReceivedRedirect(URLRequest* request, |
| 6378 | const RedirectInfo& redirect_info, |
| 6379 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6380 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6381 | request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false); |
| 6382 | } |
| 6383 | }; |
| 6384 | |
| 6385 | } // namespace |
| 6386 | |
| 6387 | TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6388 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6389 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6390 | GURL destination_url = |
| 6391 | http_test_server()->GetURL("/echoheader?" + std::string(kExtraHeader)); |
| 6392 | GURL original_url = |
| 6393 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6394 | RedirectWithAdditionalHeadersDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6395 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6396 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6397 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6398 | d.RunUntilComplete(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6399 | |
| 6400 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6401 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6402 | EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value)); |
| 6403 | EXPECT_EQ(kExtraValue, value); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6404 | EXPECT_FALSE(req->is_pending()); |
| 6405 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6406 | EXPECT_EQ(kExtraValue, d.data_received()); |
| 6407 | } |
| 6408 | |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6409 | namespace { |
| 6410 | |
| 6411 | const char kExtraHeaderToRemove[] = "To-Be-Removed"; |
| 6412 | |
| 6413 | class RedirectWithHeaderRemovalDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 6414 | void OnReceivedRedirect(URLRequest* request, |
| 6415 | const RedirectInfo& redirect_info, |
| 6416 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6417 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6418 | request->RemoveRequestHeaderByName(kExtraHeaderToRemove); |
| 6419 | } |
| 6420 | }; |
| 6421 | |
| 6422 | } // namespace |
| 6423 | |
| 6424 | TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6425 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6426 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6427 | GURL destination_url = http_test_server()->GetURL( |
| 6428 | "/echoheader?" + std::string(kExtraHeaderToRemove)); |
| 6429 | GURL original_url = |
| 6430 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6431 | RedirectWithHeaderRemovalDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6432 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6433 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6434 | req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false); |
| 6435 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6436 | d.RunUntilComplete(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6437 | |
| 6438 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6439 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6440 | EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6441 | EXPECT_FALSE(req->is_pending()); |
| 6442 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6443 | EXPECT_EQ("None", d.data_received()); |
| 6444 | } |
| 6445 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6446 | TEST_F(URLRequestTestHTTP, CancelAfterStart) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6447 | TestDelegate d; |
| 6448 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6449 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6450 | GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d, |
| 6451 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6452 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6453 | r->Start(); |
| 6454 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6455 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6456 | r->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6457 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6458 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6459 | |
| 6460 | // We expect to receive OnResponseStarted even though the request has been |
| 6461 | // cancelled. |
| 6462 | EXPECT_EQ(1, d.response_started_count()); |
| 6463 | EXPECT_EQ(0, d.bytes_received()); |
| 6464 | EXPECT_FALSE(d.received_data_before_response()); |
| 6465 | } |
| 6466 | } |
| 6467 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6468 | TEST_F(URLRequestTestHTTP, CancelInResponseStarted) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6469 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6470 | |
| 6471 | TestDelegate d; |
| 6472 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6473 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6474 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6475 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6476 | |
| 6477 | d.set_cancel_in_response_started(true); |
| 6478 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6479 | r->Start(); |
| 6480 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6481 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6482 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6483 | |
| 6484 | EXPECT_EQ(1, d.response_started_count()); |
| 6485 | EXPECT_EQ(0, d.bytes_received()); |
| 6486 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6487 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6488 | } |
| 6489 | } |
| 6490 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6491 | TEST_F(URLRequestTestHTTP, CancelOnDataReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6492 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6493 | |
| 6494 | TestDelegate d; |
| 6495 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6496 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6497 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 6498 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6499 | |
| 6500 | d.set_cancel_in_received_data(true); |
| 6501 | |
| 6502 | r->Start(); |
| 6503 | EXPECT_TRUE(r->is_pending()); |
| 6504 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6505 | d.RunUntilComplete(); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6506 | |
| 6507 | EXPECT_EQ(1, d.response_started_count()); |
| 6508 | EXPECT_NE(0, d.received_bytes_count()); |
| 6509 | EXPECT_FALSE(d.received_data_before_response()); |
| 6510 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
| 6511 | } |
| 6512 | } |
| 6513 | |
| 6514 | TEST_F(URLRequestTestHTTP, CancelDuringEofRead) { |
| 6515 | ASSERT_TRUE(http_test_server()->Start()); |
| 6516 | |
| 6517 | TestDelegate d; |
| 6518 | { |
| 6519 | // This returns an empty response (With headers). |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6520 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6521 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6522 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6523 | |
| 6524 | d.set_cancel_in_received_data(true); |
| 6525 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6526 | r->Start(); |
| 6527 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6528 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6529 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6530 | |
| 6531 | EXPECT_EQ(1, d.response_started_count()); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6532 | EXPECT_EQ(0, d.received_bytes_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6533 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6534 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6535 | } |
| 6536 | } |
| 6537 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6538 | TEST_F(URLRequestTestHTTP, CancelByDestroyingAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6539 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6540 | |
| 6541 | TestDelegate d; |
| 6542 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6543 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6544 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6545 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6546 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6547 | r->Start(); |
| 6548 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6549 | |
| 6550 | // The request will be implicitly canceled when it is destroyed. The |
| 6551 | // test delegate must not post a quit message when this happens because |
| 6552 | // this test doesn't actually have a message loop. The quit message would |
| 6553 | // get put on this thread's message queue and the next test would exit |
| 6554 | // early, causing problems. |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 6555 | d.set_on_complete(base::DoNothing()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6556 | } |
| 6557 | // expect things to just cleanup properly. |
| 6558 | |
kimwjdalsl | 2bb4ff0 | 2015-12-16 22:06:02 | [diff] [blame] | 6559 | // 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] | 6560 | // message loop |
| 6561 | EXPECT_FALSE(d.received_data_before_response()); |
| 6562 | EXPECT_EQ(0, d.bytes_received()); |
| 6563 | } |
| 6564 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6565 | TEST_F(URLRequestTestHTTP, CancelWhileReadingFromCache) { |
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 | |
| 6568 | // populate cache |
| 6569 | { |
| 6570 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6571 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6572 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6573 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6574 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6575 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6576 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6577 | } |
| 6578 | |
| 6579 | // cancel read from cache (see bug 990242) |
| 6580 | { |
| 6581 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6582 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6583 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6584 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6585 | r->Start(); |
| 6586 | r->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6587 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6588 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6589 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6590 | EXPECT_EQ(1, d.response_started_count()); |
| 6591 | EXPECT_EQ(0, d.bytes_received()); |
| 6592 | EXPECT_FALSE(d.received_data_before_response()); |
| 6593 | } |
| 6594 | } |
| 6595 | |
| 6596 | TEST_F(URLRequestTestHTTP, PostTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6597 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6598 | HTTPUploadDataOperationTest("POST"); |
| 6599 | } |
| 6600 | |
| 6601 | TEST_F(URLRequestTestHTTP, PutTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6602 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6603 | HTTPUploadDataOperationTest("PUT"); |
| 6604 | } |
| 6605 | |
| 6606 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6607 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6608 | |
| 6609 | TestDelegate d; |
| 6610 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6611 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6612 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6613 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6614 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6615 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6616 | r->Start(); |
| 6617 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6618 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6619 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6620 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6621 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6622 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6623 | |
| 6624 | EXPECT_FALSE(d.received_data_before_response()); |
| 6625 | EXPECT_TRUE(d.data_received().empty()); |
| 6626 | } |
| 6627 | } |
| 6628 | |
| 6629 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6630 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6631 | |
| 6632 | TestDelegate d; |
| 6633 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6634 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6635 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6636 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6637 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6638 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6639 | base::FilePath dir; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 6640 | base::PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 6641 | base::SetCurrentDirectory(dir); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6642 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6643 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6644 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6645 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 6646 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 6647 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6648 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6649 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6650 | base::ThreadTaskRunnerHandle::Get().get(), path, 0, |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6651 | std::numeric_limits<uint64_t>::max(), base::Time())); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6652 | r->set_upload(std::make_unique<ElementsUploadDataStream>( |
| 6653 | std::move(element_readers), 0)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6654 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6655 | r->Start(); |
| 6656 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6657 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6658 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6659 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6660 | int64_t size64 = 0; |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 6661 | ASSERT_EQ(true, base::GetFileSize(path, &size64)); |
| 6662 | ASSERT_LE(size64, std::numeric_limits<int>::max()); |
| 6663 | int size = static_cast<int>(size64); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6664 | std::unique_ptr<char[]> buf(new char[size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6665 | |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 6666 | ASSERT_EQ(size, base::ReadFile(path, buf.get(), size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6667 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6668 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6669 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6670 | |
| 6671 | EXPECT_FALSE(d.received_data_before_response()); |
| 6672 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6673 | EXPECT_EQ(size, d.bytes_received()); |
| 6674 | EXPECT_EQ(std::string(&buf[0], size), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6675 | } |
| 6676 | } |
| 6677 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6678 | TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6679 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6680 | |
| 6681 | TestDelegate d; |
| 6682 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6683 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6684 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6685 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6686 | r->set_method("POST"); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6687 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6688 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6689 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6690 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 6691 | base::ThreadTaskRunnerHandle::Get().get(), |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6692 | base::FilePath(FILE_PATH_LITERAL( |
| 6693 | "c:\\path\\to\\non\\existant\\file.randomness.12345")), |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6694 | 0, std::numeric_limits<uint64_t>::max(), base::Time())); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6695 | r->set_upload(std::make_unique<ElementsUploadDataStream>( |
| 6696 | std::move(element_readers), 0)); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6697 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6698 | r->Start(); |
| 6699 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6700 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6701 | d.RunUntilComplete(); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6702 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6703 | EXPECT_TRUE(d.request_failed()); |
| 6704 | EXPECT_FALSE(d.received_data_before_response()); |
| 6705 | EXPECT_EQ(0, d.bytes_received()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6706 | EXPECT_EQ(ERR_FILE_NOT_FOUND, d.request_status()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6707 | } |
| 6708 | } |
| 6709 | |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6710 | namespace { |
| 6711 | |
| 6712 | // Adds a standard set of data to an upload for chunked upload integration |
| 6713 | // tests. |
| 6714 | void AddDataToUpload(ChunkedUploadDataStream::Writer* writer) { |
| 6715 | writer->AppendData("a", 1, false); |
| 6716 | writer->AppendData("bcd", 3, false); |
| 6717 | writer->AppendData("this is a longer chunk than before.", 35, false); |
| 6718 | writer->AppendData("\r\n\r\n", 4, false); |
| 6719 | writer->AppendData("0", 1, false); |
| 6720 | writer->AppendData("2323", 4, true); |
| 6721 | } |
| 6722 | |
| 6723 | // Checks that the upload data added in AddChunksToUpload() was echoed back from |
| 6724 | // the server. |
| 6725 | void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { |
| 6726 | // This should match the chunks sent by AddChunksToUpload(). |
| 6727 | const std::string expected_data = |
| 6728 | "abcdthis is a longer chunk than before.\r\n\r\n02323"; |
| 6729 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6730 | ASSERT_EQ(1, d->response_started_count()) << "request failed. Error: " |
| 6731 | << d->request_status(); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6732 | |
| 6733 | EXPECT_FALSE(d->received_data_before_response()); |
| 6734 | |
| 6735 | EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); |
| 6736 | EXPECT_EQ(expected_data, d->data_received()); |
| 6737 | } |
| 6738 | |
| 6739 | } // namespace |
| 6740 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6741 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6742 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6743 | |
| 6744 | TestDelegate d; |
| 6745 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6746 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6747 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6748 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6749 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6750 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6751 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6752 | upload_data_stream->CreateWriter(); |
| 6753 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6754 | r->set_method("POST"); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6755 | AddDataToUpload(writer.get()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6756 | r->Start(); |
| 6757 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6758 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6759 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6760 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6761 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6762 | } |
| 6763 | } |
| 6764 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6765 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6766 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6767 | |
| 6768 | TestDelegate d; |
| 6769 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6770 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6771 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6772 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6773 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6774 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6775 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6776 | upload_data_stream->CreateWriter(); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6777 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6778 | r->set_method("POST"); |
| 6779 | r->Start(); |
| 6780 | EXPECT_TRUE(r->is_pending()); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6781 | AddDataToUpload(writer.get()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6782 | d.RunUntilComplete(); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6783 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6784 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6785 | } |
| 6786 | } |
| 6787 | |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6788 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6789 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6790 | |
| 6791 | TestDelegate d; |
| 6792 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6793 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6794 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6795 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6796 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6797 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6798 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6799 | upload_data_stream->CreateWriter(); |
| 6800 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6801 | r->set_method("POST"); |
| 6802 | r->Start(); |
| 6803 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6804 | |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6805 | // Pump messages until we start sending headers.. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6806 | base::RunLoop().RunUntilIdle(); |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6807 | |
| 6808 | // And now wait for completion. |
| 6809 | base::RunLoop run_loop; |
| 6810 | d.set_on_complete(run_loop.QuitClosure()); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6811 | AddDataToUpload(writer.get()); |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6812 | run_loop.Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6813 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6814 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6815 | } |
| 6816 | } |
| 6817 | |
| 6818 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6819 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6820 | |
| 6821 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6822 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6823 | http_test_server()->GetURL("/with-headers.html"), DEFAULT_PRIORITY, &d, |
| 6824 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6825 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6826 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6827 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6828 | const HttpResponseHeaders* headers = req->response_headers(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6829 | |
| 6830 | // Simple sanity check that response_info() accesses the same data. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6831 | EXPECT_EQ(headers, req->response_info().headers.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6832 | |
| 6833 | std::string header; |
| 6834 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 6835 | EXPECT_EQ("private", header); |
| 6836 | |
| 6837 | header.clear(); |
| 6838 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 6839 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 6840 | |
| 6841 | // The response has two "X-Multiple-Entries" headers. |
| 6842 | // This verfies our output has them concatenated together. |
| 6843 | header.clear(); |
| 6844 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 6845 | EXPECT_EQ("a, b", header); |
| 6846 | } |
| 6847 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6848 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 6849 | // security state. (see http://crbug.com/550977). |
| 6850 | #if !defined(OS_IOS) |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6851 | TEST_F(URLRequestTestHTTP, ProcessSTS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6852 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6853 | https_test_server.SetSSLConfig( |
| 6854 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6855 | https_test_server.ServeFilesFromSourceDirectory( |
| 6856 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6857 | ASSERT_TRUE(https_test_server.Start()); |
| 6858 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6859 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6860 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6861 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6862 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6863 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6864 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6865 | d.RunUntilComplete(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6866 | |
| 6867 | TransportSecurityState* security_state = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6868 | default_context().transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6869 | TransportSecurityState::STSState sts_state; |
| 6870 | TransportSecurityState::PKPState pkp_state; |
| 6871 | EXPECT_TRUE( |
| 6872 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6873 | EXPECT_FALSE( |
| 6874 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6875 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6876 | sts_state.upgrade_mode); |
| 6877 | EXPECT_TRUE(sts_state.include_subdomains); |
| 6878 | EXPECT_FALSE(pkp_state.include_subdomains); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6879 | #if defined(OS_ANDROID) |
| 6880 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6881 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6882 | EXPECT_FALSE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6883 | #endif |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6884 | } |
| 6885 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6886 | TEST_F(URLRequestTestHTTP, STSNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6887 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6888 | https_test_server.ServeFilesFromSourceDirectory( |
| 6889 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6890 | ASSERT_TRUE(https_test_server.Start()); |
| 6891 | // Make sure this test fails if the test server is changed to not |
| 6892 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6893 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 6894 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6895 | |
| 6896 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6897 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6898 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6899 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6900 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6901 | d.RunUntilComplete(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6902 | TransportSecurityState* security_state = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6903 | default_context().transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6904 | TransportSecurityState::STSState sts_state; |
| 6905 | EXPECT_FALSE( |
| 6906 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6907 | } |
| 6908 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6909 | namespace { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 6910 | const char kExpectCTStaticHostname[] = "expect-ct.preloaded.test"; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6911 | const char kPKPReportUri[] = "http://report-uri.preloaded.test/pkp"; |
| 6912 | const char kPKPHost[] = "with-report-uri-pkp.preloaded.test"; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6913 | } // namespace |
| 6914 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6915 | // 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] | 6916 | TEST_F(URLRequestTestHTTP, ProcessPKPAndSendReport) { |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6917 | GURL report_uri(kPKPReportUri); |
Hiroki Nakagawa | 4e4967bcb | 2018-10-11 04:09:51 | [diff] [blame] | 6918 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6919 | https_test_server.SetSSLConfig( |
| 6920 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6921 | https_test_server.ServeFilesFromSourceDirectory( |
| 6922 | base::FilePath(kTestFilePath)); |
| 6923 | ASSERT_TRUE(https_test_server.Start()); |
| 6924 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6925 | std::string test_server_hostname = kPKPHost; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6926 | |
| 6927 | // Set up a pin for |test_server_hostname|. |
| 6928 | TransportSecurityState security_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6929 | security_state.EnableStaticPinsForTesting(); |
| 6930 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6931 | |
| 6932 | MockCertificateReportSender mock_report_sender; |
| 6933 | security_state.SetReportSender(&mock_report_sender); |
| 6934 | |
| 6935 | // Set up a MockCertVerifier to trigger a violation of the previously |
| 6936 | // set pin. |
| 6937 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6938 | ASSERT_TRUE(cert); |
| 6939 | |
| 6940 | MockCertVerifier cert_verifier; |
| 6941 | CertVerifyResult verify_result; |
| 6942 | verify_result.verified_cert = cert; |
| 6943 | verify_result.is_issued_by_known_root = true; |
| 6944 | HashValue hash3; |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6945 | ASSERT_TRUE( |
| 6946 | hash3.FromString("sha256/3333333333333333333333333333333333333333333=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6947 | verify_result.public_key_hashes.push_back(hash3); |
| 6948 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6949 | |
| 6950 | TestNetworkDelegate network_delegate; |
| 6951 | TestURLRequestContext context(true); |
| 6952 | context.set_transport_security_state(&security_state); |
| 6953 | context.set_network_delegate(&network_delegate); |
| 6954 | context.set_cert_verifier(&cert_verifier); |
| 6955 | context.Init(); |
| 6956 | |
| 6957 | // Now send a request to trigger the violation. |
| 6958 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6959 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6960 | https_test_server.GetURL(test_server_hostname, "/simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6961 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6962 | violating_request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6963 | d.RunUntilComplete(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6964 | |
| 6965 | // Check that a report was sent. |
| 6966 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 6967 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 6968 | EXPECT_EQ("application/json; charset=utf-8", |
| 6969 | mock_report_sender.latest_content_type()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6970 | std::unique_ptr<base::Value> value( |
Lei Zhang | a8b4c5fb | 2019-02-16 03:02:03 | [diff] [blame] | 6971 | base::JSONReader::ReadDeprecated(mock_report_sender.latest_report())); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6972 | ASSERT_TRUE(value); |
jdoerrie | e48b26a | 2017-12-09 14:19:08 | [diff] [blame] | 6973 | ASSERT_TRUE(value->is_dict()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6974 | base::DictionaryValue* report_dict; |
| 6975 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 6976 | std::string report_hostname; |
| 6977 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 6978 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 6979 | } |
| 6980 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6981 | // Tests that reports do not get sent on requests to static pkp hosts that |
| 6982 | // don't have pin violations. |
| 6983 | TEST_F(URLRequestTestHTTP, ProcessPKPWithNoViolation) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6984 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6985 | https_test_server.SetSSLConfig( |
| 6986 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6987 | https_test_server.ServeFilesFromSourceDirectory( |
| 6988 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6989 | ASSERT_TRUE(https_test_server.Start()); |
| 6990 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6991 | std::string test_server_hostname = kPKPHost; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6992 | |
| 6993 | TransportSecurityState security_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6994 | security_state.EnableStaticPinsForTesting(); |
| 6995 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6996 | MockCertificateReportSender mock_report_sender; |
| 6997 | security_state.SetReportSender(&mock_report_sender); |
| 6998 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6999 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7000 | ASSERT_TRUE(cert); |
Hiroki Nakagawa | 4e4967bcb | 2018-10-11 04:09:51 | [diff] [blame] | 7001 | MockCertVerifier mock_cert_verifier; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7002 | CertVerifyResult verify_result; |
| 7003 | verify_result.verified_cert = cert; |
| 7004 | verify_result.is_issued_by_known_root = true; |
| 7005 | HashValue hash; |
| 7006 | // The expected value of GoodPin1 used by |test_default::kHSTSSource|. |
| 7007 | ASSERT_TRUE( |
| 7008 | hash.FromString("sha256/Nn8jk5By4Vkq6BeOVZ7R7AC6XUUBZsWmUbJR1f1Y5FY=")); |
| 7009 | verify_result.public_key_hashes.push_back(hash); |
| 7010 | mock_cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7011 | |
| 7012 | TestNetworkDelegate network_delegate; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 7013 | TestURLRequestContext context(true); |
| 7014 | context.set_transport_security_state(&security_state); |
| 7015 | context.set_network_delegate(&network_delegate); |
| 7016 | context.set_cert_verifier(&mock_cert_verifier); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 7017 | context.Init(); |
| 7018 | |
| 7019 | // Now send a request that does not trigger the violation. |
| 7020 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7021 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7022 | https_test_server.GetURL(test_server_hostname, "/simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7023 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 7024 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7025 | d.RunUntilComplete(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 7026 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7027 | // Check that the request succeeded, a report was not sent and the pkp was |
| 7028 | // not bypassed. |
| 7029 | EXPECT_EQ(OK, d.request_status()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 7030 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 7031 | EXPECT_EQ(std::string(), mock_report_sender.latest_report()); |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7032 | TransportSecurityState::STSState sts_state; |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 7033 | TransportSecurityState::PKPState pkp_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7034 | EXPECT_TRUE(security_state.GetStaticDomainState(test_server_hostname, |
| 7035 | &sts_state, &pkp_state)); |
| 7036 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
| 7037 | EXPECT_FALSE(request->ssl_info().pkp_bypassed); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 7038 | } |
| 7039 | |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7040 | TEST_F(URLRequestTestHTTP, PKPBypassRecorded) { |
| 7041 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7042 | https_test_server.SetSSLConfig( |
| 7043 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7044 | https_test_server.ServeFilesFromSourceDirectory( |
| 7045 | base::FilePath(kTestFilePath)); |
| 7046 | ASSERT_TRUE(https_test_server.Start()); |
| 7047 | |
| 7048 | // Set up a MockCertVerifier to be a local root that violates the pin |
| 7049 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7050 | ASSERT_TRUE(cert); |
| 7051 | |
| 7052 | MockCertVerifier cert_verifier; |
| 7053 | CertVerifyResult verify_result; |
| 7054 | verify_result.verified_cert = cert; |
| 7055 | verify_result.is_issued_by_known_root = false; |
| 7056 | HashValue hash; |
| 7057 | ASSERT_TRUE( |
| 7058 | hash.FromString("sha256/1111111111111111111111111111111111111111111=")); |
| 7059 | verify_result.public_key_hashes.push_back(hash); |
| 7060 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7061 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7062 | std::string test_server_hostname = kPKPHost; |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7063 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7064 | // Set up PKP |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7065 | TransportSecurityState security_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7066 | security_state.EnableStaticPinsForTesting(); |
| 7067 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 7068 | MockCertificateReportSender mock_report_sender; |
| 7069 | security_state.SetReportSender(&mock_report_sender); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7070 | |
| 7071 | TestNetworkDelegate network_delegate; |
| 7072 | TestURLRequestContext context(true); |
| 7073 | context.set_transport_security_state(&security_state); |
| 7074 | context.set_network_delegate(&network_delegate); |
| 7075 | context.set_cert_verifier(&cert_verifier); |
| 7076 | context.Init(); |
| 7077 | |
| 7078 | TestDelegate d; |
| 7079 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7080 | https_test_server.GetURL(test_server_hostname, "/simple.html"), |
| 7081 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7082 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7083 | d.RunUntilComplete(); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7084 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7085 | // Check that the request succeeded, a report was not sent and the PKP was |
| 7086 | // bypassed. |
| 7087 | EXPECT_EQ(OK, d.request_status()); |
| 7088 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 7089 | EXPECT_EQ(std::string(), mock_report_sender.latest_report()); |
| 7090 | TransportSecurityState::STSState sts_state; |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7091 | TransportSecurityState::PKPState pkp_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7092 | EXPECT_TRUE(security_state.GetStaticDomainState(test_server_hostname, |
| 7093 | &sts_state, &pkp_state)); |
| 7094 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7095 | EXPECT_TRUE(request->ssl_info().pkp_bypassed); |
| 7096 | } |
| 7097 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7098 | TEST_F(URLRequestTestHTTP, ProcessSTSOnce) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7099 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7100 | https_test_server.SetSSLConfig( |
| 7101 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7102 | https_test_server.ServeFilesFromSourceDirectory( |
| 7103 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7104 | ASSERT_TRUE(https_test_server.Start()); |
| 7105 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7106 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 7107 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7108 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7109 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7110 | https_test_server.GetURL("/hsts-multiple-headers.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7111 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7112 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7113 | d.RunUntilComplete(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7114 | |
| 7115 | // We should have set parameters from the first header, not the second. |
| 7116 | TransportSecurityState* security_state = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7117 | default_context().transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 7118 | TransportSecurityState::STSState sts_state; |
| 7119 | EXPECT_TRUE( |
| 7120 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 7121 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 7122 | sts_state.upgrade_mode); |
| 7123 | EXPECT_FALSE(sts_state.include_subdomains); |
| 7124 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7125 | } |
| 7126 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7127 | // An ExpectCTReporter that records the number of times OnExpectCTFailed() was |
| 7128 | // called. |
| 7129 | class MockExpectCTReporter : public TransportSecurityState::ExpectCTReporter { |
| 7130 | public: |
| 7131 | MockExpectCTReporter() : num_failures_(0) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 7132 | ~MockExpectCTReporter() override = default; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7133 | |
| 7134 | void OnExpectCTFailed(const HostPortPair& host_port_pair, |
| 7135 | const GURL& report_uri, |
estark | ae028b46 | 2017-06-20 23:25:01 | [diff] [blame] | 7136 | base::Time expiration, |
estark | bf1b5296 | 2017-05-05 17:05:25 | [diff] [blame] | 7137 | const X509Certificate* validated_certificate_chain, |
| 7138 | const X509Certificate* served_certificate_chain, |
| 7139 | const SignedCertificateTimestampAndStatusList& |
| 7140 | signed_certificate_timestamps) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7141 | num_failures_++; |
| 7142 | } |
| 7143 | |
| 7144 | uint32_t num_failures() { return num_failures_; } |
| 7145 | |
| 7146 | private: |
| 7147 | uint32_t num_failures_; |
| 7148 | }; |
| 7149 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7150 | // A CTPolicyEnforcer that returns a default CTPolicyCompliance value |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7151 | // for every certificate. |
| 7152 | class MockCTPolicyEnforcer : public CTPolicyEnforcer { |
| 7153 | public: |
| 7154 | MockCTPolicyEnforcer() |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7155 | : default_result_(ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 7156 | ~MockCTPolicyEnforcer() override = default; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7157 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7158 | ct::CTPolicyCompliance CheckCompliance( |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7159 | X509Certificate* cert, |
Ryan Sleevi | 8a9c9c1 | 2018-05-09 02:36:23 | [diff] [blame] | 7160 | const ct::SCTList& verified_scts, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 7161 | const NetLogWithSource& net_log) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7162 | return default_result_; |
| 7163 | } |
| 7164 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7165 | void set_default_result(ct::CTPolicyCompliance default_result) { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7166 | default_result_ = default_result; |
| 7167 | } |
| 7168 | |
| 7169 | private: |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7170 | ct::CTPolicyCompliance default_result_; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7171 | }; |
| 7172 | |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7173 | // Tests that Expect CT headers for the preload list are processed correctly. |
| 7174 | TEST_F(URLRequestTestHTTP, PreloadExpectCTHeader) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 7175 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 7176 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7177 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7178 | https_test_server.SetSSLConfig( |
| 7179 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7180 | https_test_server.ServeFilesFromSourceDirectory( |
| 7181 | base::FilePath(kTestFilePath)); |
| 7182 | ASSERT_TRUE(https_test_server.Start()); |
| 7183 | |
| 7184 | MockExpectCTReporter reporter; |
| 7185 | TransportSecurityState transport_security_state; |
| 7186 | transport_security_state.enable_static_expect_ct_ = true; |
| 7187 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7188 | |
| 7189 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7190 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7191 | ASSERT_TRUE(cert); |
| 7192 | MockCertVerifier cert_verifier; |
| 7193 | CertVerifyResult verify_result; |
| 7194 | verify_result.verified_cert = cert; |
| 7195 | verify_result.is_issued_by_known_root = true; |
| 7196 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7197 | |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 7198 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to trigger an Expect |
| 7199 | // CT violation. |
| 7200 | DoNothingCTVerifier ct_verifier; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7201 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7202 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7203 | ct::CTPolicyCompliance::CT_POLICY_NOT_ENOUGH_SCTS); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7204 | |
| 7205 | TestNetworkDelegate network_delegate; |
| 7206 | // Use a MockHostResolver (which by default maps all hosts to |
| 7207 | // 127.0.0.1) so that the request can be sent to a site on the Expect |
| 7208 | // CT preload list. |
| 7209 | MockHostResolver host_resolver; |
| 7210 | TestURLRequestContext context(true); |
| 7211 | context.set_host_resolver(&host_resolver); |
| 7212 | context.set_transport_security_state(&transport_security_state); |
| 7213 | context.set_network_delegate(&network_delegate); |
| 7214 | context.set_cert_verifier(&cert_verifier); |
| 7215 | context.set_cert_transparency_verifier(&ct_verifier); |
vabr | 13d0074 | 2017-06-05 10:31:46 | [diff] [blame] | 7216 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7217 | context.Init(); |
| 7218 | |
| 7219 | // Now send a request to trigger the violation. |
| 7220 | TestDelegate d; |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7221 | GURL url = https_test_server.GetURL("/expect-ct-header-preload.html"); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7222 | GURL::Replacements replace_host; |
| 7223 | replace_host.SetHostStr(kExpectCTStaticHostname); |
| 7224 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7225 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
| 7226 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7227 | violating_request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7228 | d.RunUntilComplete(); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7229 | |
| 7230 | EXPECT_EQ(1u, reporter.num_failures()); |
| 7231 | } |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7232 | |
| 7233 | // Tests that Expect CT HTTP headers are processed correctly. |
| 7234 | TEST_F(URLRequestTestHTTP, ExpectCTHeader) { |
| 7235 | base::test::ScopedFeatureList feature_list; |
| 7236 | feature_list.InitAndEnableFeature( |
| 7237 | TransportSecurityState::kDynamicExpectCTFeature); |
| 7238 | |
| 7239 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7240 | https_test_server.SetSSLConfig( |
| 7241 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7242 | https_test_server.ServeFilesFromSourceDirectory( |
| 7243 | base::FilePath(kTestFilePath)); |
| 7244 | ASSERT_TRUE(https_test_server.Start()); |
| 7245 | |
| 7246 | MockExpectCTReporter reporter; |
| 7247 | TransportSecurityState transport_security_state; |
| 7248 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7249 | |
| 7250 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7251 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7252 | ASSERT_TRUE(cert); |
| 7253 | MockCertVerifier cert_verifier; |
| 7254 | CertVerifyResult verify_result; |
| 7255 | verify_result.verified_cert = cert; |
| 7256 | verify_result.is_issued_by_known_root = true; |
| 7257 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7258 | |
| 7259 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 7260 | // compliance. |
| 7261 | DoNothingCTVerifier ct_verifier; |
| 7262 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7263 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7264 | ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7265 | |
| 7266 | TestNetworkDelegate network_delegate; |
| 7267 | // Use a MockHostResolver (which by default maps all hosts to |
| 7268 | // 127.0.0.1). |
| 7269 | MockHostResolver host_resolver; |
| 7270 | TestURLRequestContext context(true); |
| 7271 | context.set_host_resolver(&host_resolver); |
| 7272 | context.set_transport_security_state(&transport_security_state); |
| 7273 | context.set_network_delegate(&network_delegate); |
| 7274 | context.set_cert_verifier(&cert_verifier); |
| 7275 | context.set_cert_transparency_verifier(&ct_verifier); |
| 7276 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 7277 | context.Init(); |
| 7278 | |
| 7279 | // Now send a request to trigger the header processing. |
| 7280 | TestDelegate d; |
| 7281 | GURL url = https_test_server.GetURL("/expect-ct-header.html"); |
| 7282 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7283 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7284 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7285 | d.RunUntilComplete(); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7286 | |
| 7287 | TransportSecurityState::ExpectCTState state; |
| 7288 | ASSERT_TRUE( |
| 7289 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 7290 | EXPECT_TRUE(state.enforce); |
| 7291 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 7292 | } |
| 7293 | |
| 7294 | // Tests that if multiple Expect CT HTTP headers are sent, they are all |
| 7295 | // processed. |
| 7296 | TEST_F(URLRequestTestHTTP, MultipleExpectCTHeaders) { |
| 7297 | base::test::ScopedFeatureList feature_list; |
| 7298 | feature_list.InitAndEnableFeature( |
| 7299 | TransportSecurityState::kDynamicExpectCTFeature); |
| 7300 | |
| 7301 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7302 | https_test_server.SetSSLConfig( |
| 7303 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7304 | https_test_server.ServeFilesFromSourceDirectory( |
| 7305 | base::FilePath(kTestFilePath)); |
| 7306 | ASSERT_TRUE(https_test_server.Start()); |
| 7307 | |
| 7308 | MockExpectCTReporter reporter; |
| 7309 | TransportSecurityState transport_security_state; |
| 7310 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7311 | |
| 7312 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7313 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7314 | ASSERT_TRUE(cert); |
| 7315 | MockCertVerifier cert_verifier; |
| 7316 | CertVerifyResult verify_result; |
| 7317 | verify_result.verified_cert = cert; |
| 7318 | verify_result.is_issued_by_known_root = true; |
| 7319 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7320 | |
| 7321 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 7322 | // compliance. |
| 7323 | DoNothingCTVerifier ct_verifier; |
| 7324 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7325 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7326 | ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7327 | |
| 7328 | TestNetworkDelegate network_delegate; |
| 7329 | // Use a MockHostResolver (which by default maps all hosts to |
| 7330 | // 127.0.0.1). |
| 7331 | MockHostResolver host_resolver; |
| 7332 | TestURLRequestContext context(true); |
| 7333 | context.set_host_resolver(&host_resolver); |
| 7334 | context.set_transport_security_state(&transport_security_state); |
| 7335 | context.set_network_delegate(&network_delegate); |
| 7336 | context.set_cert_verifier(&cert_verifier); |
| 7337 | context.set_cert_transparency_verifier(&ct_verifier); |
| 7338 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 7339 | context.Init(); |
| 7340 | |
| 7341 | // Now send a request to trigger the header processing. |
| 7342 | TestDelegate d; |
| 7343 | GURL url = https_test_server.GetURL("/expect-ct-header-multiple.html"); |
| 7344 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7345 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7346 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7347 | d.RunUntilComplete(); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7348 | |
| 7349 | TransportSecurityState::ExpectCTState state; |
| 7350 | ASSERT_TRUE( |
| 7351 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 7352 | EXPECT_TRUE(state.enforce); |
| 7353 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 7354 | } |
| 7355 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 7356 | #endif // !defined(OS_IOS) |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7357 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7358 | #if BUILDFLAG(ENABLE_REPORTING) |
| 7359 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7360 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_DontReportIfNetworkNotAccessed) { |
| 7361 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7362 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7363 | ASSERT_TRUE(https_test_server.Start()); |
| 7364 | GURL request_url = https_test_server.GetURL("/cachetime"); |
| 7365 | |
| 7366 | TestNetworkErrorLoggingService nel_service; |
| 7367 | TestURLRequestContext context(true); |
| 7368 | context.set_network_error_logging_service(&nel_service); |
| 7369 | context.Init(); |
| 7370 | |
| 7371 | // Populate the cache. |
| 7372 | TestDelegate d; |
| 7373 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7374 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7375 | request->Start(); |
| 7376 | d.RunUntilComplete(); |
| 7377 | |
| 7378 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7379 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7380 | nel_service.errors()[0]; |
| 7381 | EXPECT_EQ(request_url, error.uri); |
| 7382 | EXPECT_EQ(200, error.status_code); |
| 7383 | EXPECT_EQ(OK, error.type); |
| 7384 | |
| 7385 | request = context.CreateRequest(request_url, DEFAULT_PRIORITY, &d, |
| 7386 | TRAFFIC_ANNOTATION_FOR_TESTS); |
| 7387 | request->Start(); |
| 7388 | d.RunUntilComplete(); |
| 7389 | |
| 7390 | EXPECT_FALSE(request->response_info().network_accessed); |
| 7391 | EXPECT_TRUE(request->response_info().was_cached); |
| 7392 | // No additional NEL report was generated. |
| 7393 | EXPECT_EQ(1u, nel_service.errors().size()); |
| 7394 | } |
| 7395 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7396 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_BasicSuccess) { |
| 7397 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7398 | https_test_server.ServeFilesFromSourceDirectory( |
| 7399 | base::FilePath(kTestFilePath)); |
| 7400 | ASSERT_TRUE(https_test_server.Start()); |
| 7401 | GURL request_url = https_test_server.GetURL("/simple.html"); |
| 7402 | |
| 7403 | TestNetworkErrorLoggingService nel_service; |
| 7404 | TestURLRequestContext context(true); |
| 7405 | context.set_network_error_logging_service(&nel_service); |
| 7406 | context.Init(); |
| 7407 | |
| 7408 | TestDelegate d; |
| 7409 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7410 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7411 | request->Start(); |
| 7412 | d.RunUntilComplete(); |
| 7413 | |
| 7414 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7415 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7416 | nel_service.errors()[0]; |
| 7417 | EXPECT_EQ(request_url, error.uri); |
| 7418 | EXPECT_EQ(200, error.status_code); |
| 7419 | EXPECT_EQ(OK, error.type); |
| 7420 | } |
| 7421 | |
| 7422 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_BasicError) { |
| 7423 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7424 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7425 | ASSERT_TRUE(https_test_server.Start()); |
| 7426 | GURL request_url = https_test_server.GetURL("/close-socket"); |
| 7427 | |
| 7428 | TestNetworkErrorLoggingService nel_service; |
| 7429 | TestURLRequestContext context(true); |
| 7430 | context.set_network_error_logging_service(&nel_service); |
| 7431 | context.Init(); |
| 7432 | |
| 7433 | TestDelegate d; |
| 7434 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7435 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7436 | request->Start(); |
| 7437 | d.RunUntilComplete(); |
| 7438 | |
| 7439 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7440 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7441 | nel_service.errors()[0]; |
| 7442 | EXPECT_EQ(request_url, error.uri); |
| 7443 | EXPECT_EQ(0, error.status_code); |
| 7444 | EXPECT_EQ(ERR_EMPTY_RESPONSE, error.type); |
| 7445 | } |
| 7446 | |
| 7447 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_Redirect) { |
| 7448 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7449 | https_test_server.ServeFilesFromSourceDirectory( |
| 7450 | base::FilePath(kTestFilePath)); |
| 7451 | ASSERT_TRUE(https_test_server.Start()); |
| 7452 | GURL request_url = https_test_server.GetURL("/redirect-test.html"); |
| 7453 | GURL redirect_url = https_test_server.GetURL("/with-headers.html"); |
| 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(2u, nel_service.errors().size()); |
| 7467 | const TestNetworkErrorLoggingService::RequestDetails& error1 = |
| 7468 | nel_service.errors()[0]; |
| 7469 | EXPECT_EQ(request_url, error1.uri); |
| 7470 | EXPECT_EQ(302, error1.status_code); |
| 7471 | EXPECT_EQ(OK, error1.type); |
| 7472 | const TestNetworkErrorLoggingService::RequestDetails& error2 = |
| 7473 | nel_service.errors()[1]; |
| 7474 | EXPECT_EQ(redirect_url, error2.uri); |
| 7475 | EXPECT_EQ(200, error2.status_code); |
| 7476 | EXPECT_EQ(OK, error2.type); |
| 7477 | } |
| 7478 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7479 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_RedirectWithoutLocationHeader) { |
| 7480 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7481 | https_test_server.ServeFilesFromSourceDirectory( |
| 7482 | base::FilePath(kTestFilePath)); |
| 7483 | ASSERT_TRUE(https_test_server.Start()); |
| 7484 | GURL request_url = https_test_server.GetURL("/308-without-location-header"); |
| 7485 | |
| 7486 | TestNetworkErrorLoggingService nel_service; |
| 7487 | TestURLRequestContext context(true); |
| 7488 | context.set_network_error_logging_service(&nel_service); |
| 7489 | context.Init(); |
| 7490 | |
| 7491 | TestDelegate d; |
| 7492 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7493 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7494 | request->Start(); |
| 7495 | d.RunUntilComplete(); |
| 7496 | |
| 7497 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7498 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7499 | nel_service.errors()[0]; |
| 7500 | EXPECT_EQ(request_url, error.uri); |
| 7501 | EXPECT_EQ(308, error.status_code); |
| 7502 | // The body of the response was successfully read. |
| 7503 | EXPECT_EQ(OK, error.type); |
| 7504 | } |
| 7505 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7506 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_Auth) { |
| 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 | TestDelegate d; |
| 7518 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7519 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7520 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7521 | request->Start(); |
| 7522 | d.RunUntilComplete(); |
| 7523 | |
| 7524 | ASSERT_EQ(2u, nel_service.errors().size()); |
| 7525 | const TestNetworkErrorLoggingService::RequestDetails& error1 = |
| 7526 | nel_service.errors()[0]; |
| 7527 | EXPECT_EQ(request_url, error1.uri); |
| 7528 | EXPECT_EQ(401, error1.status_code); |
| 7529 | EXPECT_EQ(OK, error1.type); |
| 7530 | const TestNetworkErrorLoggingService::RequestDetails& error2 = |
| 7531 | nel_service.errors()[1]; |
| 7532 | EXPECT_EQ(request_url, error2.uri); |
| 7533 | EXPECT_EQ(200, error2.status_code); |
| 7534 | EXPECT_EQ(OK, error2.type); |
| 7535 | } |
| 7536 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7537 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_304Response) { |
| 7538 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7539 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7540 | ASSERT_TRUE(https_test_server.Start()); |
| 7541 | GURL request_url = https_test_server.GetURL("/auth-basic"); |
| 7542 | |
| 7543 | TestNetworkErrorLoggingService nel_service; |
| 7544 | TestURLRequestContext context(true); |
| 7545 | context.set_network_error_logging_service(&nel_service); |
| 7546 | context.Init(); |
| 7547 | |
| 7548 | // populate the cache |
| 7549 | { |
| 7550 | TestDelegate d; |
| 7551 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7552 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 7553 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7554 | r->Start(); |
| 7555 | d.RunUntilComplete(); |
| 7556 | } |
| 7557 | ASSERT_EQ(2u, nel_service.errors().size()); |
| 7558 | const TestNetworkErrorLoggingService::RequestDetails& error1 = |
| 7559 | nel_service.errors()[0]; |
| 7560 | EXPECT_EQ(request_url, error1.uri); |
| 7561 | EXPECT_EQ(401, error1.status_code); |
| 7562 | EXPECT_EQ(OK, error1.type); |
| 7563 | const TestNetworkErrorLoggingService::RequestDetails& error2 = |
| 7564 | nel_service.errors()[1]; |
| 7565 | EXPECT_EQ(request_url, error2.uri); |
| 7566 | EXPECT_EQ(200, error2.status_code); |
| 7567 | EXPECT_EQ(OK, error2.type); |
| 7568 | |
| 7569 | // repeat request with end-to-end validation. since auth-basic results in a |
| 7570 | // cachable page, we expect this test to result in a 304. in which case, the |
| 7571 | // response should be fetched from the cache. |
| 7572 | { |
| 7573 | TestDelegate d; |
| 7574 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7575 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 7576 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7577 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 7578 | r->Start(); |
| 7579 | d.RunUntilComplete(); |
| 7580 | |
| 7581 | // Should be the same cached document. |
| 7582 | EXPECT_TRUE(r->was_cached()); |
| 7583 | } |
| 7584 | ASSERT_EQ(3u, nel_service.errors().size()); |
| 7585 | const TestNetworkErrorLoggingService::RequestDetails& error3 = |
| 7586 | nel_service.errors()[2]; |
| 7587 | EXPECT_EQ(request_url, error3.uri); |
| 7588 | EXPECT_EQ(304, error3.status_code); |
| 7589 | EXPECT_EQ(OK, error3.type); |
| 7590 | } |
| 7591 | |
| 7592 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_CancelInResponseStarted) { |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7593 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7594 | https_test_server.ServeFilesFromSourceDirectory( |
| 7595 | base::FilePath(kTestFilePath)); |
| 7596 | ASSERT_TRUE(https_test_server.Start()); |
| 7597 | GURL request_url = https_test_server.GetURL("/simple.html"); |
| 7598 | |
| 7599 | TestNetworkErrorLoggingService nel_service; |
| 7600 | TestURLRequestContext context(true); |
| 7601 | context.set_network_error_logging_service(&nel_service); |
| 7602 | context.Init(); |
| 7603 | |
| 7604 | TestDelegate d; |
| 7605 | d.set_cancel_in_response_started(true); |
| 7606 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7607 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7608 | request->Start(); |
| 7609 | d.RunUntilComplete(); |
| 7610 | |
| 7611 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7612 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7613 | nel_service.errors()[0]; |
| 7614 | EXPECT_EQ(request_url, error.uri); |
| 7615 | EXPECT_EQ(200, error.status_code); |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7616 | // Headers were received and the body should have been read but was not. |
| 7617 | EXPECT_EQ(ERR_ABORTED, error.type); |
| 7618 | } |
| 7619 | |
| 7620 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_CancelOnDataReceived) { |
| 7621 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7622 | https_test_server.ServeFilesFromSourceDirectory( |
| 7623 | base::FilePath(kTestFilePath)); |
| 7624 | ASSERT_TRUE(https_test_server.Start()); |
| 7625 | GURL request_url = https_test_server.GetURL("/simple.html"); |
| 7626 | |
| 7627 | TestNetworkErrorLoggingService nel_service; |
| 7628 | TestURLRequestContext context(true); |
| 7629 | context.set_network_error_logging_service(&nel_service); |
| 7630 | context.Init(); |
| 7631 | |
| 7632 | TestDelegate d; |
| 7633 | d.set_cancel_in_received_data(true); |
| 7634 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7635 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7636 | request->Start(); |
| 7637 | d.RunUntilComplete(); |
| 7638 | |
| 7639 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7640 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7641 | nel_service.errors()[0]; |
| 7642 | EXPECT_EQ(request_url, error.uri); |
| 7643 | EXPECT_EQ(200, error.status_code); |
| 7644 | // Data was received but the body was not completely read. |
| 7645 | EXPECT_EQ(ERR_ABORTED, error.type); |
| 7646 | } |
| 7647 | |
| 7648 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_CancelRedirect) { |
| 7649 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7650 | https_test_server.ServeFilesFromSourceDirectory( |
| 7651 | base::FilePath(kTestFilePath)); |
| 7652 | ASSERT_TRUE(https_test_server.Start()); |
| 7653 | GURL request_url = https_test_server.GetURL("/redirect-test.html"); |
| 7654 | |
| 7655 | TestNetworkErrorLoggingService nel_service; |
| 7656 | TestURLRequestContext context(true); |
| 7657 | context.set_network_error_logging_service(&nel_service); |
| 7658 | context.Init(); |
| 7659 | |
| 7660 | TestDelegate d; |
| 7661 | d.set_cancel_in_received_redirect(true); |
| 7662 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7663 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7664 | request->Start(); |
| 7665 | d.RunUntilComplete(); |
| 7666 | |
| 7667 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7668 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7669 | nel_service.errors()[0]; |
| 7670 | EXPECT_EQ(request_url, error.uri); |
| 7671 | EXPECT_EQ(302, error.status_code); |
| 7672 | // A valid HTTP response was received, even though the request was cancelled. |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7673 | EXPECT_EQ(OK, error.type); |
| 7674 | } |
| 7675 | |
| 7676 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 7677 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7678 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7679 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7680 | |
| 7681 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7682 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7683 | http_test_server()->GetURL("/content-type-normalization.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7684 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7685 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7686 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7687 | |
| 7688 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7689 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7690 | EXPECT_EQ("text/html", mime_type); |
| 7691 | |
| 7692 | std::string charset; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7693 | req->GetCharset(&charset); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7694 | EXPECT_EQ("utf-8", charset); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7695 | req->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7696 | } |
| 7697 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7698 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) { |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7699 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7700 | GURL data_url("data:,foo"); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7701 | DataProtocolHandler data_protocol_handler; |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7702 | EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7703 | |
| 7704 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7705 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7706 | } |
| 7707 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7708 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7709 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) { |
| 7710 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
| 7711 | GURL file_url("file:///foo.txt"); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 7712 | FileProtocolHandler file_protocol_handler( |
| 7713 | base::ThreadTaskRunnerHandle::Get()); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7714 | EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url)); |
| 7715 | |
| 7716 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7717 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(file_url)); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7718 | } |
| 7719 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7720 | TEST_F(URLRequestTestHTTP, RestrictFileRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7721 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7722 | |
| 7723 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7724 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7725 | http_test_server()->GetURL("/redirect-to-file.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7726 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7727 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7728 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7729 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7730 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7731 | |
| 7732 | // The redirect should have been rejected before reporting it to the caller. |
| 7733 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7734 | } |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7735 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7736 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7737 | TEST_F(URLRequestTestHTTP, RestrictDataRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7738 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7739 | |
| 7740 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7741 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7742 | http_test_server()->GetURL("/redirect-to-data.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7743 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7744 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7745 | d.RunUntilComplete(); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7746 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7747 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7748 | |
| 7749 | // The redirect should have been rejected before reporting it to the |
| 7750 | // caller. See https://crbug.com/723796 |
| 7751 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7752 | } |
| 7753 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7754 | // Test that redirects to invalid URLs are rejected. See |
| 7755 | // https://crbug.com/462272. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7756 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7757 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7758 | |
| 7759 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7760 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7761 | http_test_server()->GetURL("/redirect-to-invalid-url.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7762 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7763 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7764 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7765 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7766 | EXPECT_EQ(1, d.response_started_count()); |
| 7767 | EXPECT_EQ(ERR_INVALID_REDIRECT, d.request_status()); |
| 7768 | |
| 7769 | // The redirect should have been rejected before reporting it to the caller. |
| 7770 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7771 | } |
| 7772 | |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7773 | // Make sure redirects are cached, despite not reading their bodies. |
| 7774 | TEST_F(URLRequestTestHTTP, CacheRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7775 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7776 | GURL redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7777 | http_test_server()->GetURL("/redirect302-to-echo-cacheable"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7778 | |
| 7779 | { |
| 7780 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7781 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7782 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7783 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7784 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7785 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7786 | EXPECT_EQ(1, d.received_redirect_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7787 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7788 | } |
| 7789 | |
| 7790 | { |
| 7791 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7792 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7793 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7794 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 7795 | d.RunUntilRedirect(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7796 | |
| 7797 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7798 | EXPECT_EQ(0, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7799 | EXPECT_TRUE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7800 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 7801 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 7802 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7803 | d.RunUntilComplete(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7804 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7805 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7806 | EXPECT_EQ(OK, d.request_status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7807 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7808 | } |
| 7809 | } |
| 7810 | |
| 7811 | // Make sure a request isn't cached when a NetworkDelegate forces a redirect |
| 7812 | // when the headers are read, since the body won't have been read. |
| 7813 | TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7814 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7815 | // URL that is normally cached. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7816 | GURL initial_url = http_test_server()->GetURL("/cachetime"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7817 | |
| 7818 | { |
| 7819 | // Set up the TestNetworkDelegate tp force a redirect. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7820 | GURL redirect_to_url = http_test_server()->GetURL("/echo"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7821 | default_network_delegate_.set_redirect_on_headers_received_url( |
| 7822 | redirect_to_url); |
| 7823 | |
| 7824 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7825 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7826 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7827 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7828 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7829 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7830 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7831 | EXPECT_EQ(redirect_to_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7832 | } |
| 7833 | |
| 7834 | { |
| 7835 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7836 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7837 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7838 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7839 | d.RunUntilComplete(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7840 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7841 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7842 | EXPECT_FALSE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7843 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7844 | EXPECT_EQ(initial_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7845 | } |
| 7846 | } |
| 7847 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7848 | // Tests that redirection to an unsafe URL is allowed when it has been marked as |
| 7849 | // safe. |
| 7850 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7851 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7852 | |
| 7853 | GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url"); |
| 7854 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7855 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7856 | |
| 7857 | TestDelegate d; |
| 7858 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7859 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7860 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 7861 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7862 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7863 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7864 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7865 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7866 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7867 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7868 | EXPECT_EQ(unsafe_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7869 | EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received()); |
| 7870 | } |
| 7871 | } |
| 7872 | |
| 7873 | // Tests that a redirect to a different unsafe URL is blocked, even after adding |
| 7874 | // some other URL to the whitelist. |
| 7875 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7876 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7877 | |
| 7878 | GURL unsafe_url("data:text/html,something"); |
| 7879 | GURL different_unsafe_url("data:text/html,something-else"); |
| 7880 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7881 | default_network_delegate_.set_allowed_unsafe_redirect_url( |
| 7882 | different_unsafe_url); |
| 7883 | |
| 7884 | TestDelegate d; |
| 7885 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7886 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7887 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 7888 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7889 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7890 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7891 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7892 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7893 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7894 | |
| 7895 | // The redirect should have been rejected before reporting it to the caller. |
| 7896 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7897 | } |
| 7898 | } |
| 7899 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7900 | // Redirects from an URL with fragment to an unsafe URL with fragment should |
| 7901 | // be allowed, and the reference fragment of the target URL should be preserved. |
| 7902 | TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7903 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7904 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7905 | GURL original_url(http_test_server()->GetURL("/original#fragment1")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7906 | GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 7907 | GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 7908 | |
| 7909 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7910 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7911 | |
| 7912 | TestDelegate d; |
| 7913 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7914 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7915 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7916 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7917 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7918 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7919 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7920 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7921 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7922 | EXPECT_EQ(original_url, r->original_url()); |
| 7923 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7924 | } |
| 7925 | } |
| 7926 | |
| 7927 | // When a delegate has specified a safe redirect URL, but it does not match the |
| 7928 | // redirect target, then do not prevent the reference fragment from being added. |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7929 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7930 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7931 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7932 | GURL original_url(http_test_server()->GetURL("/original#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7933 | GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7934 | GURL redirect_url(http_test_server()->GetURL("/target")); |
| 7935 | GURL expected_redirect_url( |
| 7936 | http_test_server()->GetURL("/target#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7937 | |
| 7938 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 7939 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7940 | |
| 7941 | TestDelegate d; |
| 7942 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7943 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7944 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7945 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7946 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7947 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7948 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7949 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7950 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7951 | EXPECT_EQ(original_url, r->original_url()); |
| 7952 | EXPECT_EQ(expected_redirect_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7953 | } |
| 7954 | } |
| 7955 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7956 | // When a delegate has specified a safe redirect URL, assume that the redirect |
| 7957 | // URL should not be changed. In particular, the reference fragment should not |
| 7958 | // be modified. |
| 7959 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7960 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7961 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7962 | GURL original_url( |
| 7963 | http_test_server()->GetURL("/original#should-not-be-appended")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7964 | GURL redirect_url("data:text/html,expect-no-reference-fragment"); |
| 7965 | |
| 7966 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 7967 | default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url); |
| 7968 | |
| 7969 | TestDelegate d; |
| 7970 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7971 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7972 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7973 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7974 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7975 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7976 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7977 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7978 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7979 | EXPECT_EQ(original_url, r->original_url()); |
| 7980 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7981 | } |
| 7982 | } |
| 7983 | |
| 7984 | // When a URLRequestRedirectJob is created, the redirection must be followed and |
| 7985 | // the reference fragment of the target URL must not be modified. |
| 7986 | TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7987 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7988 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7989 | GURL original_url( |
| 7990 | http_test_server()->GetURL("/original#should-not-be-appended")); |
| 7991 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7992 | |
| 7993 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7994 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7995 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7996 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7997 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7998 | r.get(), &default_network_delegate_, redirect_url, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7999 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 8000 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8001 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8002 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8003 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8004 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8005 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8006 | EXPECT_EQ(original_url, r->original_url()); |
| 8007 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8008 | } |
| 8009 | |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 8010 | TEST_F(URLRequestTestHTTP, UnsupportedReferrerScheme) { |
| 8011 | ASSERT_TRUE(http_test_server()->Start()); |
| 8012 | |
| 8013 | const std::string referrer("foobar://totally.legit.referrer"); |
| 8014 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8015 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8016 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8017 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 8018 | req->SetReferrer(referrer); |
| 8019 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8020 | d.RunUntilComplete(); |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 8021 | |
| 8022 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 8023 | } |
| 8024 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8025 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8026 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8027 | |
| 8028 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8029 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8030 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8031 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8032 | req->SetReferrer("http://user:[email protected]/"); |
| 8033 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8034 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8035 | |
| 8036 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 8037 | } |
| 8038 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8039 | TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8040 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8041 | |
| 8042 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8043 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8044 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8045 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8046 | req->SetReferrer("http://foo.com/test#fragment"); |
| 8047 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8048 | d.RunUntilComplete(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8049 | |
| 8050 | EXPECT_EQ(std::string("http://foo.com/test"), d.data_received()); |
| 8051 | } |
| 8052 | |
| 8053 | TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8054 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8055 | |
| 8056 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8057 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8058 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8059 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8060 | req->SetReferrer("http://foo.com/test#fragment"); |
| 8061 | req->SetReferrer(""); |
| 8062 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8063 | d.RunUntilComplete(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8064 | |
| 8065 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 8066 | } |
| 8067 | |
Mike West | fbd4411 | 2019-05-08 18:23:37 | [diff] [blame] | 8068 | TEST_F(URLRequestTestHTTP, CapRefererDisabled) { |
| 8069 | ASSERT_TRUE(http_test_server()->Start()); |
| 8070 | |
| 8071 | // Create a string, and pad it out to ~10k with a very exciting path. |
| 8072 | std::string long_referer_header = "http://foo.com/"; |
| 8073 | long_referer_header.resize(10000, 'a'); |
| 8074 | |
| 8075 | // If the feature isn't enabled, a long `referer` will remain long. |
| 8076 | TestDelegate d; |
| 8077 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8078 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8079 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8080 | req->SetReferrer(long_referer_header); |
| 8081 | req->Start(); |
| 8082 | d.RunUntilComplete(); |
| 8083 | |
| 8084 | EXPECT_EQ(long_referer_header, d.data_received()); |
| 8085 | } |
| 8086 | |
| 8087 | TEST_F(URLRequestTestHTTP, CapRefererHeaderLengthEnabled) { |
| 8088 | ASSERT_TRUE(http_test_server()->Start()); |
| 8089 | |
| 8090 | // Create a string, and pad it out to ~10k with a very exciting path. |
| 8091 | std::string long_referer_header = "http://foo.com/"; |
| 8092 | long_referer_header.resize(10000, 'a'); |
| 8093 | |
| 8094 | // If the feature is enabled without params, a `referer` longer than 4096 |
| 8095 | // bytes will be shortened. |
| 8096 | { |
| 8097 | TestDelegate d; |
| 8098 | base::test::ScopedFeatureList feature_list; |
| 8099 | feature_list.InitAndEnableFeature(features::kCapRefererHeaderLength); |
| 8100 | |
| 8101 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8102 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8103 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8104 | req->SetReferrer(long_referer_header); |
| 8105 | req->Start(); |
| 8106 | d.RunUntilComplete(); |
| 8107 | |
| 8108 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 8109 | } |
| 8110 | |
| 8111 | // If the feature is enabled with params, they will govern the shortening |
| 8112 | // behavior as expected. The following three tests verify behavior for a |
| 8113 | // param larger than the referrer length, exactly the same as the string |
| 8114 | // length, and shorter than the string length. |
| 8115 | { |
| 8116 | TestDelegate d; |
| 8117 | std::map<std::string, std::string> params; |
| 8118 | params["MaxRefererHeaderLength"] = |
| 8119 | base::NumberToString(long_referer_header.length() + 1); |
| 8120 | |
| 8121 | base::test::ScopedFeatureList feature_list; |
| 8122 | feature_list.InitAndEnableFeatureWithParameters( |
| 8123 | features::kCapRefererHeaderLength, params); |
| 8124 | |
| 8125 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8126 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8127 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8128 | req->SetReferrer(long_referer_header); |
| 8129 | req->Start(); |
| 8130 | d.RunUntilComplete(); |
| 8131 | |
| 8132 | EXPECT_EQ(long_referer_header, d.data_received()); |
| 8133 | } |
| 8134 | |
| 8135 | { |
| 8136 | TestDelegate d; |
| 8137 | std::map<std::string, std::string> params; |
| 8138 | params["MaxRefererHeaderLength"] = |
| 8139 | base::NumberToString(long_referer_header.length()); |
| 8140 | |
| 8141 | base::test::ScopedFeatureList feature_list; |
| 8142 | feature_list.InitAndEnableFeatureWithParameters( |
| 8143 | features::kCapRefererHeaderLength, params); |
| 8144 | |
| 8145 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8146 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8147 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8148 | req->SetReferrer(long_referer_header); |
| 8149 | req->Start(); |
| 8150 | d.RunUntilComplete(); |
| 8151 | |
| 8152 | EXPECT_EQ(long_referer_header, d.data_received()); |
| 8153 | } |
| 8154 | |
| 8155 | { |
| 8156 | TestDelegate d; |
| 8157 | std::map<std::string, std::string> params; |
| 8158 | params["MaxRefererHeaderLength"] = |
| 8159 | base::NumberToString(long_referer_header.length() - 1); |
| 8160 | |
| 8161 | base::test::ScopedFeatureList feature_list; |
| 8162 | feature_list.InitAndEnableFeatureWithParameters( |
| 8163 | features::kCapRefererHeaderLength, params); |
| 8164 | |
| 8165 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8166 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8167 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8168 | req->SetReferrer(long_referer_header); |
| 8169 | req->Start(); |
| 8170 | d.RunUntilComplete(); |
| 8171 | |
| 8172 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 8173 | } |
| 8174 | } |
| 8175 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8176 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8177 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8178 | |
| 8179 | TestDelegate d; |
| 8180 | { |
| 8181 | d.set_cancel_in_received_redirect(true); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8182 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8183 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 8184 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8185 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8186 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8187 | |
| 8188 | EXPECT_EQ(1, d.response_started_count()); |
| 8189 | EXPECT_EQ(0, d.bytes_received()); |
| 8190 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8191 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8192 | } |
| 8193 | } |
| 8194 | |
| 8195 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8196 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8197 | |
| 8198 | TestDelegate d; |
| 8199 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8200 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8201 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8202 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8203 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8204 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 8205 | d.RunUntilRedirect(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8206 | |
| 8207 | EXPECT_EQ(1, d.received_redirect_count()); |
Shivani Sharma | c18f976 | 2017-10-23 16:43:23 | [diff] [blame] | 8208 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8209 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 8210 | d.ClearFullRequestHeaders(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8211 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8212 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 8213 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8214 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8215 | |
| 8216 | EXPECT_EQ(1, d.response_started_count()); |
| 8217 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8218 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8219 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 8220 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 8221 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 8222 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8223 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 8224 | |
| 8225 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 8226 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8227 | EXPECT_EQ(contents, d.data_received()); |
| 8228 | } |
| 8229 | } |
| 8230 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8231 | TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8232 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8233 | |
| 8234 | TestDelegate d; |
| 8235 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8236 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8237 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8238 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8239 | |
| 8240 | EXPECT_FALSE(d.have_full_request_headers()); |
| 8241 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8242 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 8243 | d.RunUntilRedirect(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8244 | |
| 8245 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8246 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8247 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 8248 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8249 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8250 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8251 | GURL target_url(http_test_server()->GetURL("/with-headers.html")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8252 | EXPECT_EQ(1, d.response_started_count()); |
| 8253 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8254 | CheckFullRequestHeaders(d.full_request_headers(), target_url); |
| 8255 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8256 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8257 | |
| 8258 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 8259 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 8260 | path = path.Append(kTestFilePath); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8261 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 8262 | |
| 8263 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 8264 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8265 | EXPECT_EQ(contents, d.data_received()); |
| 8266 | } |
| 8267 | } |
| 8268 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8269 | TEST_F(URLRequestTestHTTP, DeferredRedirect_ModifiedHeaders) { |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8270 | ASSERT_TRUE(http_test_server()->Start()); |
| 8271 | |
| 8272 | TestDelegate d; |
| 8273 | { |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8274 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8275 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8276 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8277 | |
| 8278 | // Set initial headers for the request. |
| 8279 | req->SetExtraRequestHeaderByName("Header1", "Value1", true /* overwrite */); |
| 8280 | req->SetExtraRequestHeaderByName("Header2", "Value2", true /* overwrite */); |
| 8281 | |
| 8282 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 8283 | d.RunUntilRedirect(); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8284 | |
| 8285 | // Initial request should only have initial headers. |
| 8286 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8287 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8288 | const HttpRequestHeaders& sent_headers1 = d.full_request_headers(); |
| 8289 | std::string sent_value; |
| 8290 | EXPECT_TRUE(sent_headers1.GetHeader("Header1", &sent_value)); |
| 8291 | EXPECT_EQ("Value1", sent_value); |
| 8292 | EXPECT_TRUE(sent_headers1.GetHeader("Header2", &sent_value)); |
| 8293 | EXPECT_EQ("Value2", sent_value); |
| 8294 | EXPECT_FALSE(sent_headers1.GetHeader("Header3", &sent_value)); |
| 8295 | d.ClearFullRequestHeaders(); |
| 8296 | |
| 8297 | // Overwrite Header2 and add Header3. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8298 | net::HttpRequestHeaders modified_headers; |
| 8299 | modified_headers.SetHeader("Header2", ""); |
| 8300 | modified_headers.SetHeader("Header3", "Value3"); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8301 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8302 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 8303 | modified_headers); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8304 | d.RunUntilComplete(); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8305 | |
| 8306 | EXPECT_EQ(1, d.response_started_count()); |
| 8307 | EXPECT_FALSE(d.received_data_before_response()); |
| 8308 | EXPECT_EQ(OK, d.request_status()); |
| 8309 | |
| 8310 | // Redirected request should also have modified headers. |
| 8311 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8312 | const HttpRequestHeaders& sent_headers2 = d.full_request_headers(); |
| 8313 | EXPECT_TRUE(sent_headers2.GetHeader("Header1", &sent_value)); |
| 8314 | EXPECT_EQ("Value1", sent_value); |
| 8315 | EXPECT_TRUE(sent_headers2.GetHeader("Header2", &sent_value)); |
| 8316 | EXPECT_EQ("", sent_value); |
| 8317 | EXPECT_TRUE(sent_headers2.GetHeader("Header3", &sent_value)); |
| 8318 | EXPECT_EQ("Value3", sent_value); |
| 8319 | } |
| 8320 | } |
| 8321 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8322 | TEST_F(URLRequestTestHTTP, DeferredRedirect_RemovedHeaders) { |
| 8323 | ASSERT_TRUE(http_test_server()->Start()); |
| 8324 | |
| 8325 | TestDelegate d; |
| 8326 | { |
| 8327 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
| 8328 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8329 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8330 | |
| 8331 | // Set initial headers for the request. |
| 8332 | req->SetExtraRequestHeaderByName("Header1", "Value1", true /* overwrite */); |
| 8333 | req->SetExtraRequestHeaderByName("Header2", "Value2", true /* overwrite */); |
| 8334 | |
| 8335 | req->Start(); |
| 8336 | d.RunUntilRedirect(); |
| 8337 | |
| 8338 | // Initial request should have initial headers. |
| 8339 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8340 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8341 | const HttpRequestHeaders& sent_headers1 = d.full_request_headers(); |
| 8342 | std::string sent_value; |
| 8343 | EXPECT_TRUE(sent_headers1.GetHeader("Header1", &sent_value)); |
| 8344 | EXPECT_EQ("Value1", sent_value); |
| 8345 | EXPECT_TRUE(sent_headers1.GetHeader("Header2", &sent_value)); |
| 8346 | EXPECT_EQ("Value2", sent_value); |
| 8347 | d.ClearFullRequestHeaders(); |
| 8348 | |
| 8349 | // Keep Header1 and remove Header2. |
| 8350 | std::vector<std::string> removed_headers({"Header2"}); |
| 8351 | req->FollowDeferredRedirect(removed_headers, |
| 8352 | base::nullopt /* modified_headers */); |
| 8353 | d.RunUntilComplete(); |
| 8354 | |
| 8355 | EXPECT_EQ(1, d.response_started_count()); |
| 8356 | EXPECT_FALSE(d.received_data_before_response()); |
| 8357 | EXPECT_EQ(OK, d.request_status()); |
| 8358 | |
| 8359 | // Redirected request should also have |
| 8360 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8361 | const HttpRequestHeaders& sent_headers2 = d.full_request_headers(); |
| 8362 | EXPECT_TRUE(sent_headers2.GetHeader("Header1", &sent_value)); |
| 8363 | EXPECT_EQ("Value1", sent_value); |
| 8364 | EXPECT_FALSE(sent_headers2.GetHeader("Header2", &sent_value)); |
| 8365 | } |
| 8366 | } |
| 8367 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8368 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8369 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8370 | |
| 8371 | TestDelegate d; |
| 8372 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8373 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8374 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 8375 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8376 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 8377 | d.RunUntilRedirect(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8378 | |
| 8379 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8380 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8381 | req->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8382 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8383 | |
| 8384 | EXPECT_EQ(1, d.response_started_count()); |
| 8385 | EXPECT_EQ(0, d.bytes_received()); |
| 8386 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8387 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8388 | } |
| 8389 | } |
| 8390 | |
| 8391 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8392 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8393 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8394 | // Populate the cache. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8395 | { |
| 8396 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8397 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8398 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8399 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8400 | HttpRequestHeaders headers; |
| 8401 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8402 | req->SetExtraRequestHeaders(headers); |
| 8403 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8404 | d.RunUntilComplete(); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8405 | |
| 8406 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8407 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8408 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8409 | } |
| 8410 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8411 | // Expect a cache hit. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8412 | { |
| 8413 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8414 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8415 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8416 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8417 | HttpRequestHeaders headers; |
| 8418 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8419 | req->SetExtraRequestHeaders(headers); |
| 8420 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8421 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8422 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8423 | EXPECT_TRUE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8424 | |
| 8425 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8426 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8427 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8428 | } |
| 8429 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8430 | // Expect a cache miss. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8431 | { |
| 8432 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8433 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8434 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8435 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8436 | HttpRequestHeaders headers; |
| 8437 | headers.SetHeader("foo", "2"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8438 | req->SetExtraRequestHeaders(headers); |
| 8439 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8440 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8441 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8442 | EXPECT_FALSE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8443 | |
| 8444 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8445 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8446 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8447 | } |
| 8448 | } |
| 8449 | |
| 8450 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8451 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8452 | |
| 8453 | // populate the cache |
| 8454 | { |
| 8455 | TestDelegate d; |
| 8456 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8457 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8458 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8459 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8460 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8461 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8462 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8463 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8464 | |
| 8465 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8466 | } |
| 8467 | |
| 8468 | // repeat request with end-to-end validation. since auth-basic results in a |
| 8469 | // cachable page, we expect this test to result in a 304. in which case, the |
| 8470 | // response should be fetched from the cache. |
| 8471 | { |
| 8472 | TestDelegate d; |
| 8473 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8474 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8475 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8476 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8477 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8478 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 8479 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8480 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8481 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8482 | |
| 8483 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8484 | |
| 8485 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8486 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8487 | } |
| 8488 | } |
| 8489 | |
| 8490 | // Check that Set-Cookie headers in 401 responses are respected. |
| 8491 | // http://crbug.com/6450 |
| 8492 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8493 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8494 | |
| 8495 | GURL url_requiring_auth = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8496 | http_test_server()->GetURL("/auth-basic?set-cookie-if-challenged"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8497 | |
| 8498 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 8499 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 8500 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8501 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8502 | TestURLRequestContext context(true); |
| 8503 | context.set_network_delegate(&network_delegate); |
| 8504 | context.Init(); |
| 8505 | |
| 8506 | TestDelegate d; |
| 8507 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8508 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8509 | std::unique_ptr<URLRequest> r( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8510 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &d, |
| 8511 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8512 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8513 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8514 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8515 | |
| 8516 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8517 | |
| 8518 | // Make sure we sent the cookie in the restarted transaction. |
| 8519 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 8520 | != std::string::npos); |
| 8521 | } |
| 8522 | |
| 8523 | // Same test as above, except this time the restart is initiated earlier |
| 8524 | // (without user intervention since identity is embedded in the URL). |
| 8525 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8526 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8527 | TestURLRequestContext context(true); |
| 8528 | context.set_network_delegate(&network_delegate); |
| 8529 | context.Init(); |
| 8530 | |
| 8531 | TestDelegate d; |
| 8532 | |
| 8533 | GURL::Replacements replacements; |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 8534 | replacements.SetUsernameStr("user2"); |
| 8535 | replacements.SetPasswordStr("secret"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8536 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 8537 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8538 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 8539 | url_with_identity, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8540 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8541 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8542 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8543 | |
| 8544 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 8545 | |
| 8546 | // Make sure we sent the cookie in the restarted transaction. |
| 8547 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 8548 | != std::string::npos); |
| 8549 | } |
| 8550 | } |
| 8551 | |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8552 | TEST_F(URLRequestTestHTTP, AuthChallengeWithFilteredCookie) { |
| 8553 | ASSERT_TRUE(http_test_server()->Start()); |
| 8554 | |
| 8555 | GURL url_requiring_auth = http_test_server()->GetURL( |
| 8556 | "/auth-basic?set-cookie-if-not-challenged&set-cookie-if-challenged"); |
| 8557 | |
| 8558 | FilteringTestLayeredNetworkDelegate filtering_network_delegate( |
| 8559 | std::make_unique<TestNetworkDelegate>()); // Must outlive URLRequest. |
| 8560 | filtering_network_delegate.SetCookieFilter( |
| 8561 | "got_challenged"); // Filter the cookie auth-basic sets |
| 8562 | TestURLRequestContext context(true); |
| 8563 | context.set_network_delegate(&filtering_network_delegate); |
| 8564 | context.Init(); |
| 8565 | |
| 8566 | TestDelegate delegate; |
| 8567 | |
| 8568 | delegate.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8569 | |
| 8570 | std::unique_ptr<URLRequest> request( |
| 8571 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &delegate, |
| 8572 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8573 | request->Start(); |
| 8574 | |
| 8575 | delegate.RunUntilComplete(); |
| 8576 | EXPECT_THAT(delegate.request_status(), IsOk()); |
| 8577 | |
| 8578 | // Make sure the cookie was actually filtered. |
| 8579 | EXPECT_EQ(std::string::npos, |
| 8580 | delegate.data_received().find("Cookie: got_challenged=true")); |
| 8581 | // Make sure it was blocked twice. |
| 8582 | EXPECT_EQ(2, filtering_network_delegate.blocked_set_cookie_count()); |
| 8583 | } |
| 8584 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8585 | // Tests that load timing works as expected with auth and the cache. |
| 8586 | TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8587 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8588 | |
| 8589 | // populate the cache |
| 8590 | { |
| 8591 | TestDelegate d; |
| 8592 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8593 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8594 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8595 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8596 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8597 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8598 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8599 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8600 | |
| 8601 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8602 | |
| 8603 | LoadTimingInfo load_timing_info_before_auth; |
| 8604 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth( |
| 8605 | &load_timing_info_before_auth)); |
| 8606 | TestLoadTimingNotReused(load_timing_info_before_auth, |
| 8607 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 8608 | |
| 8609 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8610 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8611 | // The test server does not support keep alive sockets, so the second |
| 8612 | // request with auth should use a new socket. |
| 8613 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 8614 | EXPECT_NE(load_timing_info_before_auth.socket_log_id, |
| 8615 | load_timing_info.socket_log_id); |
| 8616 | EXPECT_LE(load_timing_info_before_auth.receive_headers_end, |
| 8617 | load_timing_info.connect_timing.connect_start); |
| 8618 | } |
| 8619 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8620 | // Repeat request with end-to-end validation. Since auth-basic results in a |
| 8621 | // 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] | 8622 | // response should be fetched from the cache. |
| 8623 | { |
| 8624 | TestDelegate d; |
| 8625 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8626 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8627 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8628 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8629 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8630 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 8631 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8632 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8633 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8634 | |
| 8635 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8636 | |
| 8637 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8638 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8639 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8640 | // Since there was a request that went over the wire, the load timing |
| 8641 | // information should include connection times. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8642 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8643 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8644 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8645 | } |
| 8646 | } |
| 8647 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8648 | // In this test, we do a POST which the server will 302 redirect. |
| 8649 | // The subsequent transaction should use GET, and should not send the |
| 8650 | // Content-Type header. |
| 8651 | // http://code.google.com/p/chromium/issues/detail?id=843 |
| 8652 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8653 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8654 | |
| 8655 | const char kData[] = "hello world"; |
| 8656 | |
| 8657 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8658 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8659 | http_test_server()->GetURL("/redirect-to-echoall"), DEFAULT_PRIORITY, &d, |
| 8660 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8661 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8662 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8663 | |
| 8664 | // Set headers (some of which are specific to the POST). |
| 8665 | HttpRequestHeaders headers; |
Yeol | 45b9e0a | 2018-07-27 02:03:29 | [diff] [blame] | 8666 | headers.SetHeader("Content-Type", |
| 8667 | "multipart/form-data;" |
| 8668 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ"); |
| 8669 | headers.SetHeader("Accept", |
| 8670 | "text/xml,application/xml,application/xhtml+xml," |
| 8671 | "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); |
| 8672 | headers.SetHeader("Accept-Language", "en-US,en"); |
| 8673 | headers.SetHeader("Accept-Charset", "ISO-8859-1,*,utf-8"); |
| 8674 | headers.SetHeader("Content-Length", "11"); |
| 8675 | headers.SetHeader("Origin", "http://localhost:1337/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8676 | req->SetExtraRequestHeaders(headers); |
| 8677 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8678 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8679 | |
| 8680 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8681 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8682 | EXPECT_EQ("text/html", mime_type); |
| 8683 | |
| 8684 | const std::string& data = d.data_received(); |
| 8685 | |
| 8686 | // Check that the post-specific headers were stripped: |
| 8687 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 8688 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8689 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8690 | |
| 8691 | // These extra request headers should not have been stripped. |
| 8692 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 8693 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 8694 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 8695 | } |
| 8696 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8697 | // The following tests check that we handle mutating the request for HTTP |
| 8698 | // redirects as expected. |
| 8699 | // See https://crbug.com/56373, https://crbug.com/102130, and |
| 8700 | // https://crbug.com/465517. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8701 | |
| 8702 | TEST_F(URLRequestTestHTTP, Redirect301Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8703 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8704 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8705 | const GURL url = http_test_server()->GetURL("/redirect301-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8706 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8707 | http_test_server()->GetURL("/redirect301-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8708 | |
| 8709 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8710 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8711 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8712 | |
| 8713 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8714 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8715 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8716 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8717 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8718 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8719 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8720 | } |
| 8721 | |
| 8722 | TEST_F(URLRequestTestHTTP, Redirect302Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8723 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8724 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8725 | const GURL url = http_test_server()->GetURL("/redirect302-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8726 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8727 | http_test_server()->GetURL("/redirect302-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8728 | |
| 8729 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8730 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8731 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8732 | |
| 8733 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8734 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8735 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8736 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8737 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8738 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8739 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8740 | } |
| 8741 | |
| 8742 | TEST_F(URLRequestTestHTTP, Redirect303Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8743 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8744 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8745 | const GURL url = http_test_server()->GetURL("/redirect303-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8746 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8747 | http_test_server()->GetURL("/redirect303-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8748 | |
| 8749 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8750 | HTTPRedirectMethodTest(url, "PUT", "GET", true); |
| 8751 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8752 | |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8753 | HTTPRedirectOriginHeaderTest(url, "CONNECT", "GET", std::string()); |
| 8754 | HTTPRedirectOriginHeaderTest(https_redirect_url, "CONNECT", "GET", |
| 8755 | std::string()); |
| 8756 | HTTPRedirectOriginHeaderTest(url, "DELETE", "GET", std::string()); |
| 8757 | HTTPRedirectOriginHeaderTest(https_redirect_url, "DELETE", "GET", |
| 8758 | std::string()); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8759 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8760 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8761 | HTTPRedirectOriginHeaderTest(url, "HEAD", "HEAD", url.GetOrigin().spec()); |
| 8762 | HTTPRedirectOriginHeaderTest(https_redirect_url, "HEAD", "HEAD", "null"); |
| 8763 | HTTPRedirectOriginHeaderTest(url, "OPTIONS", "GET", std::string()); |
| 8764 | HTTPRedirectOriginHeaderTest(https_redirect_url, "OPTIONS", "GET", |
| 8765 | std::string()); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8766 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8767 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8768 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8769 | HTTPRedirectOriginHeaderTest(url, "PUT", "GET", std::string()); |
| 8770 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "GET", std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8771 | } |
| 8772 | |
| 8773 | TEST_F(URLRequestTestHTTP, Redirect307Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8774 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8775 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8776 | const GURL url = http_test_server()->GetURL("/redirect307-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8777 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8778 | http_test_server()->GetURL("/redirect307-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8779 | |
| 8780 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 8781 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8782 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8783 | |
| 8784 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8785 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 8786 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 8787 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8788 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8789 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8790 | } |
| 8791 | |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8792 | TEST_F(URLRequestTestHTTP, Redirect308Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8793 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8794 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8795 | const GURL url = http_test_server()->GetURL("/redirect308-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8796 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8797 | http_test_server()->GetURL("/redirect308-to-https"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8798 | |
| 8799 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 8800 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8801 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8802 | |
| 8803 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8804 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 8805 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 8806 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8807 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8808 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8809 | } |
| 8810 | |
| 8811 | // Make sure that 308 responses without bodies are not treated as redirects. |
| 8812 | // Certain legacy apis that pre-date the response code expect this behavior |
| 8813 | // (Like Google Drive). |
| 8814 | TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8815 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8816 | |
| 8817 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8818 | const GURL url = http_test_server()->GetURL("/308-without-location-header"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8819 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8820 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8821 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8822 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8823 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8824 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8825 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8826 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8827 | EXPECT_EQ(308, request->response_headers()->response_code()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8828 | EXPECT_EQ("This is not a redirect.", d.data_received()); |
| 8829 | } |
| 8830 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8831 | TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8832 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8833 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8834 | GURL original_url( |
| 8835 | http_test_server()->GetURL("/redirect302-to-echo#fragment")); |
| 8836 | GURL expected_url(http_test_server()->GetURL("/echo#fragment")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8837 | |
| 8838 | TestDelegate d; |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8839 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8840 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8841 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8842 | r->Start(); |
| 8843 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8844 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8845 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8846 | EXPECT_EQ(OK, d.request_status()); |
| 8847 | EXPECT_EQ(original_url, r->original_url()); |
| 8848 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8849 | } |
| 8850 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8851 | TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8852 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8853 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8854 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8855 | GURL first_party_url("http://example.com"); |
| 8856 | |
| 8857 | TestDelegate d; |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8858 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8859 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8860 | r->set_site_for_cookies(first_party_url); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8861 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8862 | r->Start(); |
| 8863 | d.RunUntilComplete(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8864 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8865 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8866 | EXPECT_EQ(OK, d.request_status()); |
| 8867 | EXPECT_EQ(first_party_url, r->site_for_cookies()); |
| 8868 | } |
| 8869 | |
| 8870 | TEST_F(URLRequestTestHTTP, RedirectPreserveTopFrameOrigin) { |
| 8871 | ASSERT_TRUE(http_test_server()->Start()); |
| 8872 | |
| 8873 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 8874 | url::Origin top_frame_origin = |
| 8875 | url::Origin::Create(GURL("http://example.com")); |
| 8876 | TestDelegate d; |
| 8877 | |
| 8878 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8879 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8880 | r->set_top_frame_origin(top_frame_origin); |
| 8881 | |
| 8882 | r->Start(); |
| 8883 | d.RunUntilComplete(); |
| 8884 | |
| 8885 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8886 | EXPECT_EQ(OK, d.request_status()); |
| 8887 | EXPECT_EQ(top_frame_origin, *r->top_frame_origin()); |
| 8888 | } |
| 8889 | |
| 8890 | TEST_F(URLRequestTestHTTP, RedirectPreserveUnsetTopFrameOrigin) { |
| 8891 | ASSERT_TRUE(http_test_server()->Start()); |
| 8892 | |
| 8893 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 8894 | TestDelegate d; |
| 8895 | |
| 8896 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8897 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8898 | // Since we're not setting a top frame origin, we wouldn't expect one after |
| 8899 | // the redirect. |
| 8900 | r->Start(); |
| 8901 | d.RunUntilComplete(); |
| 8902 | |
| 8903 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8904 | EXPECT_EQ(OK, d.request_status()); |
| 8905 | EXPECT_FALSE(r->top_frame_origin()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8906 | } |
| 8907 | |
Josh Karlin | f3caee164 | 2018-12-10 22:54:27 | [diff] [blame] | 8908 | TEST_F(URLRequestTestHTTP, RedirectPreserveTopFrameURL) { |
| 8909 | ASSERT_TRUE(http_test_server()->Start()); |
| 8910 | |
| 8911 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 8912 | url::Origin top_frame_origin = |
| 8913 | url::Origin::Create(GURL("http://example.com")); |
| 8914 | TestDelegate d; |
| 8915 | { |
| 8916 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8917 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8918 | r->set_top_frame_origin(top_frame_origin); |
| 8919 | |
| 8920 | r->Start(); |
| 8921 | d.RunUntilComplete(); |
| 8922 | |
| 8923 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8924 | EXPECT_EQ(OK, d.request_status()); |
| 8925 | EXPECT_EQ(top_frame_origin, *r->top_frame_origin()); |
| 8926 | } |
| 8927 | } |
| 8928 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8929 | TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8930 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8931 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8932 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8933 | GURL original_first_party_url("http://example.com"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8934 | GURL expected_first_party_url(http_test_server()->GetURL("/echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8935 | |
| 8936 | TestDelegate d; |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8937 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8938 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8939 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8940 | r->set_site_for_cookies(original_first_party_url); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8941 | r->set_first_party_url_policy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8942 | URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 8943 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8944 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8945 | d.RunUntilComplete(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8946 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8947 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8948 | EXPECT_EQ(OK, d.request_status()); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8949 | EXPECT_EQ(expected_first_party_url, r->site_for_cookies()); |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8950 | } |
| 8951 | |
| 8952 | TEST_F(URLRequestTestHTTP, RedirectUpdateTopFrameOrigin) { |
| 8953 | ASSERT_TRUE(http_test_server()->Start()); |
| 8954 | |
| 8955 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 8956 | url::Origin original_top_frame_origin = |
| 8957 | url::Origin::Create(GURL("http://example.com")); |
| 8958 | url::Origin expected_top_frame_origin = |
| 8959 | url::Origin::Create(GURL(http_test_server()->GetURL("/echo"))); |
| 8960 | |
| 8961 | TestDelegate d; |
| 8962 | |
| 8963 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8964 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8965 | r->set_top_frame_origin(original_top_frame_origin); |
| 8966 | r->set_first_party_url_policy(URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 8967 | |
| 8968 | r->Start(); |
| 8969 | d.RunUntilComplete(); |
| 8970 | |
| 8971 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8972 | EXPECT_EQ(OK, d.request_status()); |
| 8973 | EXPECT_EQ(expected_top_frame_origin, *r->top_frame_origin()); |
| 8974 | } |
| 8975 | |
| 8976 | TEST_F(URLRequestTestHTTP, RedirectIgnoreUnsetTopFrameOrigin) { |
| 8977 | ASSERT_TRUE(http_test_server()->Start()); |
| 8978 | |
| 8979 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 8980 | TestDelegate d; |
| 8981 | |
| 8982 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8983 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8984 | // Since we're not setting a top frame origin, we wouldn't expect one after |
| 8985 | // the redirect. |
| 8986 | r->set_first_party_url_policy(URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 8987 | |
| 8988 | r->Start(); |
| 8989 | d.RunUntilComplete(); |
| 8990 | |
| 8991 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8992 | EXPECT_EQ(OK, d.request_status()); |
| 8993 | EXPECT_FALSE(r->top_frame_origin()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8994 | } |
| 8995 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8996 | TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8997 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8998 | |
| 8999 | const char kData[] = "hello world"; |
| 9000 | |
| 9001 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9002 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9003 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9004 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9005 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 9006 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9007 | HttpRequestHeaders headers; |
| 9008 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 9009 | base::NumberToString(base::size(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9010 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9011 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9012 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9013 | req.get(), &default_network_delegate_, |
| 9014 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9015 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 9016 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9017 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9018 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9019 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9020 | EXPECT_EQ("GET", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9021 | } |
| 9022 | |
| 9023 | TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9024 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9025 | |
| 9026 | const char kData[] = "hello world"; |
| 9027 | |
| 9028 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9029 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9030 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9031 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9032 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 9033 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9034 | HttpRequestHeaders headers; |
| 9035 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 9036 | base::NumberToString(base::size(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9037 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9038 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9039 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9040 | req.get(), &default_network_delegate_, |
| 9041 | http_test_server()->GetURL("/echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 9042 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9043 | "Very Good Reason")); |
| 9044 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9045 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9046 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9047 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9048 | EXPECT_EQ("POST", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9049 | EXPECT_EQ(kData, d.data_received()); |
| 9050 | } |
| 9051 | |
| 9052 | // Check that default A-L header is sent. |
| 9053 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9054 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9055 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 9056 | StaticHttpUserAgentSettings settings("en", std::string()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9057 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9058 | TestURLRequestContext context(true); |
| 9059 | context.set_network_delegate(&network_delegate); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9060 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9061 | context.Init(); |
| 9062 | |
| 9063 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9064 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9065 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9066 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9067 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9068 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9069 | EXPECT_EQ("en", d.data_received()); |
| 9070 | } |
| 9071 | |
| 9072 | // Check that an empty A-L header is not sent. http://crbug.com/77365. |
| 9073 | TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9074 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9075 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 9076 | std::string empty_string; // Avoid most vexing parse on line below. |
| 9077 | StaticHttpUserAgentSettings settings(empty_string, empty_string); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9078 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9079 | TestURLRequestContext context(true); |
| 9080 | context.set_network_delegate(&network_delegate); |
| 9081 | context.Init(); |
| 9082 | // We override the language after initialization because empty entries |
| 9083 | // get overridden by Init(). |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9084 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9085 | |
| 9086 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9087 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9088 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9089 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9090 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9091 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9092 | EXPECT_EQ("None", d.data_received()); |
| 9093 | } |
| 9094 | |
| 9095 | // Check that if request overrides the A-L header, the default is not appended. |
| 9096 | // See http://crbug.com/20894 |
| 9097 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9098 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9099 | |
| 9100 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9101 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9102 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9103 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9104 | HttpRequestHeaders headers; |
| 9105 | headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9106 | req->SetExtraRequestHeaders(headers); |
| 9107 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9108 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9109 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 9110 | } |
| 9111 | |
| 9112 | // Check that default A-E header is sent. |
| 9113 | TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9114 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9115 | |
| 9116 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9117 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9118 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9119 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9120 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9121 | req->SetExtraRequestHeaders(headers); |
| 9122 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9123 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9124 | EXPECT_TRUE(ContainsString(d.data_received(), "gzip")); |
| 9125 | } |
| 9126 | |
| 9127 | // Check that if request overrides the A-E header, the default is not appended. |
| 9128 | // See http://crbug.com/47381 |
| 9129 | TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9130 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9131 | |
| 9132 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9133 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9134 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9135 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9136 | HttpRequestHeaders headers; |
| 9137 | headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9138 | req->SetExtraRequestHeaders(headers); |
| 9139 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9140 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9141 | EXPECT_FALSE(ContainsString(d.data_received(), "gzip")); |
| 9142 | EXPECT_TRUE(ContainsString(d.data_received(), "identity")); |
| 9143 | } |
| 9144 | |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 9145 | // Check that setting the A-C header sends the proper header. |
| 9146 | TEST_F(URLRequestTestHTTP, SetAcceptCharset) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9147 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9148 | |
| 9149 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9150 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9151 | http_test_server()->GetURL("/echoheader?Accept-Charset"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9152 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9153 | HttpRequestHeaders headers; |
| 9154 | headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9155 | req->SetExtraRequestHeaders(headers); |
| 9156 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9157 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9158 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 9159 | } |
| 9160 | |
| 9161 | // Check that default User-Agent header is sent. |
| 9162 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9163 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9164 | |
| 9165 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9166 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9167 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9168 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9169 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9170 | d.RunUntilComplete(); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9171 | EXPECT_EQ(default_context().http_user_agent_settings()->GetUserAgent(), |
[email protected] | 051a4b3 | 2014-01-09 04:02:37 | [diff] [blame] | 9172 | d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9173 | } |
| 9174 | |
| 9175 | // Check that if request overrides the User-Agent header, |
| 9176 | // the default is not appended. |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 9177 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 9178 | #if defined(OS_IOS) |
| 9179 | #define MAYBE_OverrideUserAgent FLAKY_OverrideUserAgent |
| 9180 | #else |
| 9181 | #define MAYBE_OverrideUserAgent OverrideUserAgent |
| 9182 | #endif |
| 9183 | TEST_F(URLRequestTestHTTP, MAYBE_OverrideUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9184 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9185 | |
| 9186 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9187 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9188 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9189 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9190 | HttpRequestHeaders headers; |
| 9191 | headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9192 | req->SetExtraRequestHeaders(headers); |
| 9193 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9194 | d.RunUntilComplete(); |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 9195 | EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9196 | } |
| 9197 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9198 | // Check that a NULL HttpUserAgentSettings causes the corresponding empty |
| 9199 | // User-Agent header to be sent but does not send the Accept-Language and |
| 9200 | // Accept-Charset headers. |
| 9201 | TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9202 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9203 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9204 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9205 | TestURLRequestContext context(true); |
| 9206 | context.set_network_delegate(&network_delegate); |
| 9207 | context.Init(); |
| 9208 | // We override the HttpUserAgentSettings after initialization because empty |
| 9209 | // entries get overridden by Init(). |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 9210 | context.set_http_user_agent_settings(nullptr); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9211 | |
| 9212 | struct { |
| 9213 | const char* request; |
| 9214 | const char* expected_response; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9215 | } tests[] = {{"/echoheader?Accept-Language", "None"}, |
| 9216 | {"/echoheader?Accept-Charset", "None"}, |
| 9217 | {"/echoheader?User-Agent", ""}}; |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9218 | |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 9219 | for (size_t i = 0; i < base::size(tests); i++) { |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9220 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9221 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9222 | http_test_server()->GetURL(tests[i].request), DEFAULT_PRIORITY, &d, |
| 9223 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9224 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9225 | d.RunUntilComplete(); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9226 | EXPECT_EQ(tests[i].expected_response, d.data_received()) |
| 9227 | << " Request = \"" << tests[i].request << "\""; |
| 9228 | } |
| 9229 | } |
| 9230 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9231 | // Make sure that URLRequest passes on its priority updates to |
| 9232 | // newly-created jobs after the first one. |
| 9233 | TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9234 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9235 | |
| 9236 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9237 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9238 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9239 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9240 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9241 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9242 | std::unique_ptr<URLRequestRedirectJob> redirect_job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9243 | req.get(), &default_network_delegate_, |
| 9244 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9245 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 9246 | AddTestInterceptor()->set_main_intercept_job(std::move(redirect_job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9247 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9248 | req->SetPriority(LOW); |
| 9249 | req->Start(); |
| 9250 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9251 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9252 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9253 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9254 | req.get(), &default_network_delegate_, &job_priority)); |
| 9255 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9256 | |
| 9257 | // Should trigger |job| to be started. |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9258 | d.RunUntilComplete(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9259 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9260 | } |
| 9261 | |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9262 | // Check that creating a network request while entering/exiting suspend mode |
| 9263 | // fails as it should. This is the only case where an HttpTransactionFactory |
| 9264 | // does not return an HttpTransaction. |
| 9265 | TEST_F(URLRequestTestHTTP, NetworkSuspendTest) { |
| 9266 | // Create a new HttpNetworkLayer that thinks it's suspended. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9267 | std::unique_ptr<HttpNetworkLayer> network_layer(new HttpNetworkLayer( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9268 | default_context().http_transaction_factory()->GetSession())); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9269 | network_layer->OnSuspend(); |
| 9270 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 9271 | HttpCache http_cache(std::move(network_layer), |
zhongyi | 3ceab9f | 2017-02-04 02:06:03 | [diff] [blame] | 9272 | HttpCache::DefaultBackend::InMemory(0), |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 9273 | false /* is_main_cache */); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9274 | |
| 9275 | TestURLRequestContext context(true); |
| 9276 | context.set_http_transaction_factory(&http_cache); |
| 9277 | context.Init(); |
| 9278 | |
| 9279 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9280 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9281 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 9282 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9283 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9284 | d.RunUntilComplete(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9285 | |
| 9286 | EXPECT_TRUE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9287 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, d.request_status()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9288 | } |
| 9289 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9290 | namespace { |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9291 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9292 | // HttpTransactionFactory that synchronously fails to create transactions. |
| 9293 | class FailingHttpTransactionFactory : public HttpTransactionFactory { |
| 9294 | public: |
| 9295 | explicit FailingHttpTransactionFactory(HttpNetworkSession* network_session) |
| 9296 | : network_session_(network_session) {} |
| 9297 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 9298 | ~FailingHttpTransactionFactory() override = default; |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9299 | |
| 9300 | // HttpTransactionFactory methods: |
| 9301 | int CreateTransaction(RequestPriority priority, |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9302 | std::unique_ptr<HttpTransaction>* trans) override { |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9303 | return ERR_FAILED; |
| 9304 | } |
| 9305 | |
| 9306 | HttpCache* GetCache() override { return nullptr; } |
| 9307 | |
| 9308 | HttpNetworkSession* GetSession() override { return network_session_; } |
| 9309 | |
| 9310 | private: |
| 9311 | HttpNetworkSession* network_session_; |
| 9312 | |
| 9313 | DISALLOW_COPY_AND_ASSIGN(FailingHttpTransactionFactory); |
| 9314 | }; |
| 9315 | |
| 9316 | } // namespace |
| 9317 | |
| 9318 | // Check that when a request that fails to create an HttpTransaction can be |
| 9319 | // cancelled while the failure notification is pending, and doesn't send two |
| 9320 | // failure notifications. |
| 9321 | // |
| 9322 | // This currently only happens when in suspend mode and there's no cache, but |
| 9323 | // just use a special HttpTransactionFactory, to avoid depending on those |
| 9324 | // behaviors. |
| 9325 | TEST_F(URLRequestTestHTTP, NetworkCancelAfterCreateTransactionFailsTest) { |
| 9326 | FailingHttpTransactionFactory http_transaction_factory( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9327 | default_context().http_transaction_factory()->GetSession()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9328 | TestURLRequestContext context(true); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9329 | context.set_http_transaction_factory(&http_transaction_factory); |
| 9330 | context.set_network_delegate(default_network_delegate()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9331 | context.Init(); |
| 9332 | |
| 9333 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9334 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9335 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 9336 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9337 | // Don't send cookies (Collecting cookies is asynchronous, and need request to |
| 9338 | // try to create an HttpNetworkTransaction synchronously on start). |
| 9339 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9340 | req->Start(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9341 | req->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9342 | d.RunUntilComplete(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9343 | // Run pending error task, if there is one. |
| 9344 | base::RunLoop().RunUntilIdle(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9345 | |
| 9346 | EXPECT_TRUE(d.request_failed()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9347 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9348 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9349 | |
| 9350 | // NetworkDelegate should see the cancellation, but not the error. |
| 9351 | EXPECT_EQ(1, default_network_delegate()->canceled_requests()); |
| 9352 | EXPECT_EQ(0, default_network_delegate()->error_count()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9353 | } |
| 9354 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9355 | TEST_F(URLRequestTestHTTP, NetworkAccessedSetOnNetworkRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9356 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9357 | |
| 9358 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9359 | GURL test_url(http_test_server()->GetURL("/")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9360 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9361 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9362 | |
| 9363 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9364 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9365 | |
| 9366 | EXPECT_TRUE(req->response_info().network_accessed); |
| 9367 | } |
| 9368 | |
| 9369 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnCachedResponse) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9370 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9371 | |
| 9372 | // Populate the cache. |
| 9373 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9374 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9375 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 9376 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9377 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9378 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9379 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9380 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9381 | EXPECT_TRUE(req->response_info().network_accessed); |
| 9382 | EXPECT_FALSE(req->response_info().was_cached); |
| 9383 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9384 | req = default_context().CreateRequest( |
| 9385 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 9386 | TRAFFIC_ANNOTATION_FOR_TESTS); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9387 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9388 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9389 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9390 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9391 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9392 | EXPECT_TRUE(req->response_info().was_cached); |
| 9393 | } |
| 9394 | |
| 9395 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnLoadOnlyFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9396 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9397 | |
| 9398 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9399 | GURL test_url(http_test_server()->GetURL("/")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9400 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9401 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jkarlin | a067deed | 2016-10-27 14:48:33 | [diff] [blame] | 9402 | req->SetLoadFlags(LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9403 | |
| 9404 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9405 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9406 | |
| 9407 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9408 | } |
| 9409 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 9410 | // Test that a single job with a THROTTLED priority completes |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 9411 | // correctly in the absence of contention. |
| 9412 | TEST_F(URLRequestTestHTTP, ThrottledPriority) { |
| 9413 | ASSERT_TRUE(http_test_server()->Start()); |
| 9414 | |
| 9415 | TestDelegate d; |
| 9416 | GURL test_url(http_test_server()->GetURL("/")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9417 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9418 | test_url, THROTTLED, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 9419 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9420 | d.RunUntilComplete(); |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 9421 | |
| 9422 | EXPECT_TRUE(req->status().is_success()); |
| 9423 | } |
| 9424 | |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9425 | TEST_F(URLRequestTestHTTP, RawBodyBytesNoContentEncoding) { |
| 9426 | ASSERT_TRUE(http_test_server()->Start()); |
| 9427 | |
| 9428 | TestDelegate d; |
| 9429 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9430 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 9431 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9432 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9433 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9434 | |
| 9435 | EXPECT_EQ(5, req->GetRawBodyBytes()); |
| 9436 | } |
| 9437 | |
| 9438 | TEST_F(URLRequestTestHTTP, RawBodyBytesGzipEncoding) { |
| 9439 | ASSERT_TRUE(http_test_server()->Start()); |
| 9440 | |
| 9441 | TestDelegate d; |
| 9442 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9443 | http_test_server()->GetURL("/gzip-encoded"), DEFAULT_PRIORITY, &d, |
| 9444 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9445 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9446 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9447 | |
| 9448 | EXPECT_EQ(30, req->GetRawBodyBytes()); |
| 9449 | } |
| 9450 | |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9451 | // Check that if NetworkDelegate::OnBeforeStartTransaction returns an error, |
| 9452 | // the delegate isn't called back synchronously. |
| 9453 | TEST_F(URLRequestTestHTTP, TesBeforeStartTransactionFails) { |
| 9454 | ASSERT_TRUE(http_test_server()->Start()); |
| 9455 | default_network_delegate_.set_before_start_transaction_fails(); |
| 9456 | |
| 9457 | TestDelegate d; |
| 9458 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9459 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 9460 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9461 | req->Start(); |
| 9462 | DCHECK(!d.response_completed()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9463 | d.RunUntilComplete(); |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9464 | DCHECK(d.response_completed()); |
| 9465 | EXPECT_EQ(ERR_FAILED, d.request_status()); |
| 9466 | } |
| 9467 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9468 | class URLRequestInterceptorTestHTTP : public URLRequestTestHTTP { |
| 9469 | public: |
| 9470 | // TODO(bengr): Merge this with the URLRequestInterceptorHTTPTest fixture, |
| 9471 | // ideally remove the dependency on URLRequestTestJob, and maybe move these |
| 9472 | // tests into the factory tests. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 9473 | URLRequestInterceptorTestHTTP() |
| 9474 | : URLRequestTestHTTP(), interceptor_(nullptr) {} |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9475 | |
| 9476 | void SetUpFactory() override { |
| 9477 | interceptor_ = new MockURLRequestInterceptor(); |
| 9478 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9479 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9480 | } |
| 9481 | |
| 9482 | MockURLRequestInterceptor* interceptor() const { |
| 9483 | return interceptor_; |
| 9484 | } |
| 9485 | |
| 9486 | private: |
| 9487 | MockURLRequestInterceptor* interceptor_; |
| 9488 | }; |
| 9489 | |
| 9490 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9491 | NetworkDelegateNotificationOnRedirectIntercept) { |
| 9492 | interceptor()->set_intercept_redirect(true); |
| 9493 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 9494 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 9495 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9496 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9497 | |
| 9498 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9499 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9500 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 9501 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9502 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9503 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9504 | |
| 9505 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 9506 | // Check we got one good response |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9507 | int status = d.request_status(); |
| 9508 | EXPECT_EQ(OK, status); |
| 9509 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9510 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9511 | |
| 9512 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 9513 | EXPECT_EQ(1, d.response_started_count()); |
| 9514 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9515 | |
| 9516 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9517 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9518 | EXPECT_EQ(1, default_network_delegate()->headers_received_count()); |
| 9519 | } |
| 9520 | |
| 9521 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9522 | NetworkDelegateNotificationOnErrorIntercept) { |
| 9523 | // Intercept that error and respond with an OK response. |
| 9524 | interceptor()->set_intercept_final_response(true); |
| 9525 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 9526 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 9527 | default_network_delegate()->set_can_be_intercepted_on_error(true); |
| 9528 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9529 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9530 | |
| 9531 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9532 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9533 | http_test_server()->GetURL("/two-content-lengths.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9534 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9535 | req->set_method("GET"); |
| 9536 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9537 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9538 | |
| 9539 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 9540 | |
| 9541 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9542 | int status = d.request_status(); |
| 9543 | EXPECT_EQ(OK, status); |
| 9544 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9545 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9546 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 9547 | EXPECT_EQ(1, d.response_started_count()); |
| 9548 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9549 | |
| 9550 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9551 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9552 | EXPECT_EQ(0, default_network_delegate()->headers_received_count()); |
| 9553 | } |
| 9554 | |
| 9555 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9556 | NetworkDelegateNotificationOnResponseIntercept) { |
| 9557 | // Intercept that error and respond with an OK response. |
| 9558 | interceptor()->set_intercept_final_response(true); |
| 9559 | |
| 9560 | // Intercept with a real URLRequestHttpJob. |
| 9561 | interceptor()->set_use_url_request_http_job(true); |
| 9562 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9563 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9564 | |
| 9565 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9566 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9567 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 9568 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9569 | req->set_method("GET"); |
| 9570 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9571 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9572 | |
| 9573 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 9574 | |
| 9575 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9576 | int status = d.request_status(); |
| 9577 | EXPECT_EQ(OK, status); |
| 9578 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9579 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9580 | EXPECT_EQ("hello", d.data_received()); |
| 9581 | EXPECT_EQ(1, d.response_started_count()); |
| 9582 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9583 | |
| 9584 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9585 | EXPECT_EQ(2, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9586 | EXPECT_EQ(2, default_network_delegate()->headers_received_count()); |
| 9587 | } |
| 9588 | |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9589 | class URLRequestTestReferrerPolicy : public URLRequestTest { |
| 9590 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 9591 | URLRequestTestReferrerPolicy() = default; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9592 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9593 | void InstantiateSameOriginServers(net::EmbeddedTestServer::Type type) { |
| 9594 | origin_server_.reset(new EmbeddedTestServer(type)); |
| 9595 | if (type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9596 | origin_server_->AddDefaultHandlers( |
| 9597 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9598 | } else { |
| 9599 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9600 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9601 | ASSERT_TRUE(origin_server_->Start()); |
| 9602 | } |
| 9603 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9604 | void InstantiateCrossOriginServers(net::EmbeddedTestServer::Type origin_type, |
| 9605 | net::EmbeddedTestServer::Type dest_type) { |
| 9606 | origin_server_.reset(new EmbeddedTestServer(origin_type)); |
| 9607 | if (origin_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9608 | origin_server_->AddDefaultHandlers( |
| 9609 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9610 | } else { |
| 9611 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9612 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9613 | ASSERT_TRUE(origin_server_->Start()); |
| 9614 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9615 | destination_server_.reset(new EmbeddedTestServer(dest_type)); |
| 9616 | if (dest_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9617 | destination_server_->AddDefaultHandlers( |
| 9618 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9619 | } else { |
| 9620 | destination_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9621 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9622 | ASSERT_TRUE(destination_server_->Start()); |
| 9623 | } |
| 9624 | |
| 9625 | void VerifyReferrerAfterRedirect(URLRequest::ReferrerPolicy policy, |
| 9626 | const GURL& referrer, |
| 9627 | const GURL& expected) { |
| 9628 | // Create and execute the request: we'll only have a |destination_server_| |
| 9629 | // if the origins are meant to be distinct. Otherwise, we'll use the |
| 9630 | // |origin_server_| for both endpoints. |
| 9631 | GURL destination_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9632 | destination_server_ ? destination_server_->GetURL("/echoheader?Referer") |
| 9633 | : origin_server_->GetURL("/echoheader?Referer"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9634 | GURL origin_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9635 | origin_server_->GetURL("/server-redirect?" + destination_url.spec()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9636 | |
| 9637 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9638 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9639 | origin_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9640 | req->set_referrer_policy(policy); |
| 9641 | req->SetReferrer(referrer.spec()); |
| 9642 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9643 | d.RunUntilComplete(); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9644 | |
| 9645 | EXPECT_EQ(1, d.response_started_count()); |
| 9646 | EXPECT_EQ(1, d.received_redirect_count()); |
| 9647 | EXPECT_EQ(destination_url, req->url()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9648 | EXPECT_EQ(OK, d.request_status()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9649 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9650 | |
| 9651 | EXPECT_EQ(expected.spec(), req->referrer()); |
| 9652 | if (expected.is_empty()) |
| 9653 | EXPECT_EQ("None", d.data_received()); |
| 9654 | else |
| 9655 | EXPECT_EQ(expected.spec(), d.data_received()); |
| 9656 | } |
| 9657 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9658 | EmbeddedTestServer* origin_server() const { return origin_server_.get(); } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9659 | |
| 9660 | private: |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9661 | std::unique_ptr<EmbeddedTestServer> origin_server_; |
| 9662 | std::unique_ptr<EmbeddedTestServer> destination_server_; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9663 | }; |
| 9664 | |
| 9665 | TEST_F(URLRequestTestReferrerPolicy, HTTPToSameOriginHTTP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9666 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTP); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9667 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9668 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9669 | VerifyReferrerAfterRedirect( |
| 9670 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9671 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9672 | |
| 9673 | VerifyReferrerAfterRedirect( |
| 9674 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9675 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9676 | |
| 9677 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9678 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9679 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9680 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9681 | referrer); |
| 9682 | |
| 9683 | // The original referrer set on the request is expected to obey the referrer |
| 9684 | // policy and already be stripped to the origin; thus this test case just |
| 9685 | // checks that this policy doesn't cause the referrer to change when following |
| 9686 | // a redirect. |
| 9687 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9688 | referrer.GetOrigin()); |
| 9689 | |
| 9690 | VerifyReferrerAfterRedirect( |
| 9691 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9692 | referrer); |
| 9693 | |
| 9694 | // The original referrer set on the request is expected to obey the referrer |
| 9695 | // policy and already be stripped to the origin; thus this test case just |
| 9696 | // checks that this policy doesn't cause the referrer to change when following |
| 9697 | // a redirect. |
| 9698 | VerifyReferrerAfterRedirect( |
| 9699 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9700 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9701 | |
| 9702 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9703 | } |
| 9704 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9705 | TEST_F(URLRequestTestReferrerPolicy, HTTPToCrossOriginHTTP) { |
| 9706 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 9707 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9708 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9709 | |
| 9710 | VerifyReferrerAfterRedirect( |
| 9711 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9712 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9713 | |
| 9714 | VerifyReferrerAfterRedirect( |
| 9715 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9716 | referrer, referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9717 | |
| 9718 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9719 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9720 | referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9721 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9722 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9723 | referrer); |
| 9724 | |
| 9725 | // The original referrer set on the request is expected to obey the referrer |
| 9726 | // policy and already be stripped to the origin; thus this test case just |
| 9727 | // checks that this policy doesn't cause the referrer to change when following |
| 9728 | // a redirect. |
| 9729 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9730 | referrer.GetOrigin()); |
| 9731 | |
| 9732 | VerifyReferrerAfterRedirect( |
| 9733 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9734 | |
| 9735 | // The original referrer set on the request is expected to obey the referrer |
| 9736 | // policy and already be stripped to the origin; thus this test case just |
| 9737 | // checks that this policy doesn't cause the referrer to change when following |
| 9738 | // a redirect. |
| 9739 | VerifyReferrerAfterRedirect( |
| 9740 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9741 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9742 | |
| 9743 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9744 | } |
| 9745 | |
| 9746 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToSameOriginHTTPS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9747 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9748 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9749 | |
| 9750 | VerifyReferrerAfterRedirect( |
| 9751 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9752 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9753 | |
| 9754 | VerifyReferrerAfterRedirect( |
| 9755 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9756 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9757 | |
| 9758 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9759 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9760 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9761 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9762 | referrer); |
| 9763 | |
| 9764 | // The original referrer set on the request is expected to obey the referrer |
| 9765 | // policy and already be stripped to the origin; thus this test case just |
| 9766 | // checks that this policy doesn't cause the referrer to change when following |
| 9767 | // a redirect. |
| 9768 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9769 | referrer.GetOrigin()); |
| 9770 | |
| 9771 | VerifyReferrerAfterRedirect( |
| 9772 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9773 | referrer); |
| 9774 | |
| 9775 | // The original referrer set on the request is expected to obey the referrer |
| 9776 | // policy and already be stripped to the origin; thus this test case just |
| 9777 | // checks that this policy doesn't cause the referrer to change when following |
| 9778 | // a redirect. |
| 9779 | VerifyReferrerAfterRedirect( |
| 9780 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9781 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9782 | |
| 9783 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9784 | } |
| 9785 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9786 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToCrossOriginHTTPS) { |
| 9787 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 9788 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9789 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9790 | |
| 9791 | VerifyReferrerAfterRedirect( |
| 9792 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9793 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9794 | |
| 9795 | VerifyReferrerAfterRedirect( |
| 9796 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9797 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9798 | |
| 9799 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9800 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9801 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9802 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9803 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9804 | referrer); |
| 9805 | |
| 9806 | // The original referrer set on the request is expected to obey the referrer |
| 9807 | // policy and already be stripped to the origin; thus this test case just |
| 9808 | // checks that this policy doesn't cause the referrer to change when following |
| 9809 | // a redirect. |
| 9810 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9811 | referrer.GetOrigin()); |
| 9812 | |
| 9813 | VerifyReferrerAfterRedirect( |
| 9814 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9815 | |
| 9816 | // The original referrer set on the request is expected to obey the referrer |
| 9817 | // policy and already be stripped to the origin; thus this test case just |
| 9818 | // checks that this policy doesn't cause the referrer to change when following |
| 9819 | // a redirect. |
| 9820 | VerifyReferrerAfterRedirect( |
| 9821 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9822 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9823 | |
| 9824 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9825 | } |
| 9826 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9827 | TEST_F(URLRequestTestReferrerPolicy, HTTPToHTTPS) { |
| 9828 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 9829 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9830 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9831 | |
| 9832 | VerifyReferrerAfterRedirect( |
| 9833 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9834 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9835 | |
| 9836 | VerifyReferrerAfterRedirect( |
| 9837 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9838 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9839 | |
| 9840 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9841 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9842 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9843 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9844 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9845 | referrer); |
| 9846 | |
| 9847 | // The original referrer set on the request is expected to obey the referrer |
| 9848 | // policy and already be stripped to the origin; thus this test case just |
| 9849 | // checks that this policy doesn't cause the referrer to change when following |
| 9850 | // a redirect. |
| 9851 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9852 | referrer.GetOrigin()); |
| 9853 | |
| 9854 | VerifyReferrerAfterRedirect( |
| 9855 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9856 | |
| 9857 | // The original referrer set on the request is expected to obey the referrer |
| 9858 | // policy and already be stripped to the origin; thus this test case just |
| 9859 | // checks that this policy doesn't cause the referrer to change when following |
| 9860 | // a redirect. |
| 9861 | VerifyReferrerAfterRedirect( |
| 9862 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9863 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9864 | |
| 9865 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9866 | } |
| 9867 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9868 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToHTTP) { |
| 9869 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 9870 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9871 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9872 | |
| 9873 | VerifyReferrerAfterRedirect( |
| 9874 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9875 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9876 | |
| 9877 | VerifyReferrerAfterRedirect( |
| 9878 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9879 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9880 | |
| 9881 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9882 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9883 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9884 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9885 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9886 | referrer); |
| 9887 | |
| 9888 | // The original referrer set on the request is expected to obey the referrer |
| 9889 | // policy and already be stripped to the origin; thus this test case just |
| 9890 | // checks that this policy doesn't cause the referrer to change when following |
| 9891 | // a redirect. |
| 9892 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9893 | referrer.GetOrigin()); |
| 9894 | |
| 9895 | VerifyReferrerAfterRedirect( |
| 9896 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9897 | |
| 9898 | // The original referrer set on the request is expected to obey the referrer |
| 9899 | // policy and already be stripped to the origin, though it should be |
| 9900 | // subsequently cleared during the downgrading redirect. |
| 9901 | VerifyReferrerAfterRedirect( |
| 9902 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9903 | referrer.GetOrigin(), GURL()); |
| 9904 | |
| 9905 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9906 | } |
| 9907 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 9908 | class HTTPSRequestTest : public TestWithScopedTaskEnvironment { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9909 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 9910 | HTTPSRequestTest() : default_context_(true) { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9911 | default_context_.set_network_delegate(&default_network_delegate_); |
| 9912 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9913 | } |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9914 | ~HTTPSRequestTest() override { |
| 9915 | SetTransportSecurityStateSourceForTesting(nullptr); |
| 9916 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9917 | |
| 9918 | protected: |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9919 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9920 | TestURLRequestContext default_context_; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9921 | }; |
| 9922 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 9923 | TEST_F(HTTPSRequestTest, HTTPSGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9924 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9925 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9926 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9927 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9928 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9929 | TestDelegate d; |
| 9930 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9931 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9932 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9933 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9934 | r->Start(); |
| 9935 | EXPECT_TRUE(r->is_pending()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9936 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9937 | d.RunUntilComplete(); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9938 | |
| 9939 | EXPECT_EQ(1, d.response_started_count()); |
| 9940 | EXPECT_FALSE(d.received_data_before_response()); |
| 9941 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9942 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 9943 | EXPECT_EQ(test_server.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 9944 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 9945 | EXPECT_EQ(test_server.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 9946 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9947 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9948 | } |
| 9949 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 9950 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9951 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9952 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9953 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9954 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9955 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9956 | |
| 9957 | bool err_allowed = true; |
| 9958 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 9959 | TestDelegate d; |
| 9960 | { |
| 9961 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9962 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9963 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9964 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9965 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9966 | r->Start(); |
| 9967 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9968 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9969 | d.RunUntilComplete(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9970 | |
| 9971 | EXPECT_EQ(1, d.response_started_count()); |
| 9972 | EXPECT_FALSE(d.received_data_before_response()); |
| 9973 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9974 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9975 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9976 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9977 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9978 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9979 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9980 | } |
| 9981 | } |
| 9982 | } |
| 9983 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 9984 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9985 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9986 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED); |
| 9987 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9988 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9989 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9990 | |
| 9991 | // Iterate from false to true, just so that we do the opposite of the |
| 9992 | // previous test in order to increase test coverage. |
| 9993 | bool err_allowed = false; |
| 9994 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 9995 | TestDelegate d; |
| 9996 | { |
| 9997 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9998 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9999 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10000 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10001 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10002 | r->Start(); |
| 10003 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10004 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10005 | d.RunUntilComplete(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10006 | |
| 10007 | EXPECT_EQ(1, d.response_started_count()); |
| 10008 | EXPECT_FALSE(d.received_data_before_response()); |
| 10009 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 10010 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10011 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10012 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 10013 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10014 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 10015 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10016 | } |
| 10017 | } |
| 10018 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 10019 | |
Emily Stark | 79fba584 | 2019-04-25 04:59:36 | [diff] [blame] | 10020 | // A TestDelegate used to test that an appropriate net error code is provided |
| 10021 | // when an SSL certificate error occurs. |
| 10022 | class SSLNetErrorTestDelegate : public TestDelegate { |
| 10023 | public: |
| 10024 | void OnSSLCertificateError(URLRequest* request, |
| 10025 | int net_error, |
| 10026 | const SSLInfo& ssl_info, |
| 10027 | bool fatal) override { |
| 10028 | net_error_ = net_error; |
| 10029 | on_ssl_certificate_error_called_ = true; |
| 10030 | TestDelegate::OnSSLCertificateError(request, net_error, ssl_info, fatal); |
| 10031 | } |
| 10032 | |
| 10033 | bool on_ssl_certificate_error_called() { |
| 10034 | return on_ssl_certificate_error_called_; |
| 10035 | } |
| 10036 | |
| 10037 | int net_error() { return net_error_; } |
| 10038 | |
| 10039 | private: |
| 10040 | bool on_ssl_certificate_error_called_ = false; |
| 10041 | int net_error_ = net::OK; |
| 10042 | }; |
| 10043 | |
| 10044 | // Tests that the URLRequest::Delegate receives an appropriate net error code |
| 10045 | // when an SSL certificate error occurs. |
| 10046 | TEST_F(HTTPSRequestTest, SSLNetErrorReportedToDelegate) { |
| 10047 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10048 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED); |
| 10049 | test_server.AddDefaultHandlers( |
| 10050 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10051 | ASSERT_TRUE(test_server.Start()); |
| 10052 | |
| 10053 | SSLNetErrorTestDelegate d; |
| 10054 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10055 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10056 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10057 | r->Start(); |
| 10058 | EXPECT_TRUE(r->is_pending()); |
| 10059 | d.RunUntilComplete(); |
| 10060 | |
| 10061 | EXPECT_TRUE(d.on_ssl_certificate_error_called()); |
| 10062 | EXPECT_EQ(net::ERR_CERT_DATE_INVALID, d.net_error()); |
| 10063 | } |
| 10064 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10065 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 10066 | // security state. (see http://crbug.com/550977). |
| 10067 | #if !defined(OS_IOS) |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10068 | // This tests that a load of a domain with preloaded HSTS and HPKP with a |
| 10069 | // certificate error sets the |certificate_errors_are_fatal| flag correctly. |
| 10070 | // This flag will cause the interstitial to be fatal. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10071 | TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10072 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 10073 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10074 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10075 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 10076 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10077 | ASSERT_TRUE(test_server.Start()); |
| 10078 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10079 | // We require that the URL be hsts-hpkp-preloaded.test. This is a test domain |
| 10080 | // that has a preloaded HSTS+HPKP entry in the TransportSecurityState. This |
| 10081 | // means that we have to use a MockHostResolver in order to direct |
| 10082 | // hsts-hpkp-preloaded.test to the testserver. By default, MockHostResolver |
| 10083 | // maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10084 | |
| 10085 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 10086 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10087 | TestURLRequestContext context(true); |
| 10088 | context.set_network_delegate(&network_delegate); |
| 10089 | context.set_host_resolver(&host_resolver); |
| 10090 | TransportSecurityState transport_security_state; |
| 10091 | context.set_transport_security_state(&transport_security_state); |
| 10092 | context.Init(); |
| 10093 | |
| 10094 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10095 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10096 | GURL(base::StringPrintf("https://hsts-hpkp-preloaded.test:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 10097 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10098 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10099 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10100 | r->Start(); |
| 10101 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10102 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10103 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10104 | |
| 10105 | EXPECT_EQ(1, d.response_started_count()); |
| 10106 | EXPECT_FALSE(d.received_data_before_response()); |
| 10107 | EXPECT_TRUE(d.have_certificate_errors()); |
| 10108 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 10109 | } |
| 10110 | |
| 10111 | // This tests that cached HTTPS page loads do not cause any updates to the |
| 10112 | // TransportSecurityState. |
| 10113 | TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10114 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 10115 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10116 | // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't |
| 10117 | // matter. It just has to be any error. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10118 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10119 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 10120 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10121 | ASSERT_TRUE(test_server.Start()); |
| 10122 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10123 | // We require that the URL be hsts-hpkp-preloaded.test. This is a test domain |
| 10124 | // that has a preloaded HSTS+HPKP entry in the TransportSecurityState. This |
| 10125 | // means that we have to use a MockHostResolver in order to direct |
| 10126 | // hsts-hpkp-preloaded.test to the testserver. By default, MockHostResolver |
| 10127 | // maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10128 | |
| 10129 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 10130 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10131 | TestURLRequestContext context(true); |
| 10132 | context.set_network_delegate(&network_delegate); |
| 10133 | context.set_host_resolver(&host_resolver); |
| 10134 | TransportSecurityState transport_security_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10135 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10136 | TransportSecurityState::STSState static_sts_state; |
| 10137 | TransportSecurityState::PKPState static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10138 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10139 | "hsts-hpkp-preloaded.test", &static_sts_state, &static_pkp_state)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10140 | context.set_transport_security_state(&transport_security_state); |
| 10141 | context.Init(); |
| 10142 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10143 | TransportSecurityState::STSState dynamic_sts_state; |
| 10144 | TransportSecurityState::PKPState dynamic_pkp_state; |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10145 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
| 10146 | "hsts-hpkp-preloaded.test", &dynamic_sts_state)); |
| 10147 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
| 10148 | "hsts-hpkp-preloaded.test", &dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10149 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10150 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10151 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10152 | GURL(base::StringPrintf("https://hsts-hpkp-preloaded.test:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 10153 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10154 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10155 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10156 | r->Start(); |
| 10157 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10158 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10159 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10160 | |
| 10161 | EXPECT_EQ(1, d.response_started_count()); |
| 10162 | EXPECT_FALSE(d.received_data_before_response()); |
| 10163 | EXPECT_TRUE(d.have_certificate_errors()); |
| 10164 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 10165 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10166 | // Get a fresh copy of the states, and check that they haven't changed. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10167 | TransportSecurityState::STSState new_static_sts_state; |
| 10168 | TransportSecurityState::PKPState new_static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10169 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10170 | "hsts-hpkp-preloaded.test", &new_static_sts_state, |
| 10171 | &new_static_pkp_state)); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10172 | TransportSecurityState::STSState new_dynamic_sts_state; |
| 10173 | TransportSecurityState::PKPState new_dynamic_pkp_state; |
| 10174 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10175 | "hsts-hpkp-preloaded.test", &new_dynamic_sts_state)); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10176 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10177 | "hsts-hpkp-preloaded.test", &new_dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10178 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10179 | EXPECT_EQ(new_static_sts_state.upgrade_mode, static_sts_state.upgrade_mode); |
| 10180 | EXPECT_EQ(new_static_sts_state.include_subdomains, |
| 10181 | static_sts_state.include_subdomains); |
| 10182 | EXPECT_EQ(new_static_pkp_state.include_subdomains, |
| 10183 | static_pkp_state.include_subdomains); |
rsleevi | 91d4c9c | 2016-05-14 20:28:48 | [diff] [blame] | 10184 | EXPECT_EQ(new_static_pkp_state.spki_hashes, static_pkp_state.spki_hashes); |
| 10185 | EXPECT_EQ(new_static_pkp_state.bad_spki_hashes, |
| 10186 | static_pkp_state.bad_spki_hashes); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10187 | } |
| 10188 | |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10189 | // Make sure HSTS preserves a POST request's method and body. |
| 10190 | TEST_F(HTTPSRequestTest, HSTSPreservesPosts) { |
| 10191 | static const char kData[] = "hello world"; |
| 10192 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10193 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10194 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10195 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10196 | ASSERT_TRUE(test_server.Start()); |
| 10197 | |
| 10198 | |
| 10199 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 10200 | // address, so a MockHostResolver is needed to redirect www.somewhere.com to |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10201 | // the EmbeddedTestServer. By default, MockHostResolver maps all hosts |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10202 | // to 127.0.0.1. |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10203 | MockHostResolver host_resolver; |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10204 | |
| 10205 | // Force https for www.somewhere.com. |
| 10206 | TransportSecurityState transport_security_state; |
[email protected] | 474f079e | 2013-03-02 19:11:20 | [diff] [blame] | 10207 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 10208 | bool include_subdomains = false; |
| 10209 | transport_security_state.AddHSTS("www.somewhere.com", expiry, |
| 10210 | include_subdomains); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10211 | |
| 10212 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 10213 | |
| 10214 | TestURLRequestContext context(true); |
| 10215 | context.set_host_resolver(&host_resolver); |
| 10216 | context.set_transport_security_state(&transport_security_state); |
| 10217 | context.set_network_delegate(&network_delegate); |
| 10218 | context.Init(); |
| 10219 | |
| 10220 | TestDelegate d; |
| 10221 | // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will |
| 10222 | // cause a certificate error. Ignore the error. |
| 10223 | d.set_allow_certificate_errors(true); |
| 10224 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10225 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10226 | GURL(base::StringPrintf("http://www.somewhere.com:%d/echo", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 10227 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10228 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10229 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 10230 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10231 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10232 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10233 | d.RunUntilComplete(); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10234 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10235 | EXPECT_EQ("https", req->url().scheme()); |
| 10236 | EXPECT_EQ("POST", req->method()); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10237 | EXPECT_EQ(kData, d.data_received()); |
[email protected] | 07d9bfd | 2013-06-27 14:16:40 | [diff] [blame] | 10238 | |
| 10239 | LoadTimingInfo load_timing_info; |
| 10240 | network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info); |
| 10241 | // LoadTimingInfo of HSTS redirects is similar to that of network cache hits |
| 10242 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10243 | } |
| 10244 | |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10245 | // Make sure that the CORS headers are added to cross-origin HSTS redirects. |
| 10246 | TEST_F(HTTPSRequestTest, HSTSCrossOriginAddHeaders) { |
| 10247 | static const char kOriginHeaderValue[] = "http://www.example.com"; |
| 10248 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10249 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10250 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10251 | ASSERT_TRUE(test_server.Start()); |
| 10252 | |
| 10253 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 10254 | // address, so a MockHostResolver is needed to redirect example.net to the |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10255 | // EmbeddedTestServer. MockHostResolver maps all hosts to 127.0.0.1 by |
| 10256 | // default. |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10257 | MockHostResolver host_resolver; |
| 10258 | |
| 10259 | TransportSecurityState transport_security_state; |
| 10260 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 10261 | bool include_subdomains = false; |
| 10262 | transport_security_state.AddHSTS("example.net", expiry, include_subdomains); |
| 10263 | |
| 10264 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 10265 | |
| 10266 | MockCertVerifier cert_verifier; |
| 10267 | cert_verifier.set_default_result(OK); |
| 10268 | |
| 10269 | TestURLRequestContext context(true); |
| 10270 | context.set_host_resolver(&host_resolver); |
| 10271 | context.set_transport_security_state(&transport_security_state); |
| 10272 | context.set_network_delegate(&network_delegate); |
| 10273 | context.set_cert_verifier(&cert_verifier); |
| 10274 | context.Init(); |
| 10275 | |
| 10276 | GURL hsts_http_url(base::StringPrintf("http://example.net:%d/somehstssite", |
| 10277 | test_server.host_port_pair().port())); |
| 10278 | url::Replacements<char> replacements; |
| 10279 | const char kNewScheme[] = "https"; |
| 10280 | replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme))); |
| 10281 | GURL hsts_https_url = hsts_http_url.ReplaceComponents(replacements); |
| 10282 | |
| 10283 | TestDelegate d; |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10284 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10285 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 10286 | hsts_http_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10287 | // Set Origin header to simulate a cross-origin request. |
| 10288 | HttpRequestHeaders request_headers; |
| 10289 | request_headers.SetHeader("Origin", kOriginHeaderValue); |
| 10290 | req->SetExtraRequestHeaders(request_headers); |
| 10291 | |
| 10292 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10293 | d.RunUntilRedirect(); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10294 | |
| 10295 | EXPECT_EQ(1, d.received_redirect_count()); |
| 10296 | |
| 10297 | const HttpResponseHeaders* headers = req->response_headers(); |
| 10298 | std::string redirect_location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 10299 | EXPECT_TRUE( |
| 10300 | headers->EnumerateHeader(nullptr, "Location", &redirect_location)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10301 | EXPECT_EQ(hsts_https_url.spec(), redirect_location); |
| 10302 | |
| 10303 | std::string received_cors_header; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 10304 | EXPECT_TRUE(headers->EnumerateHeader(nullptr, "Access-Control-Allow-Origin", |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10305 | &received_cors_header)); |
| 10306 | EXPECT_EQ(kOriginHeaderValue, received_cors_header); |
| 10307 | } |
| 10308 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10309 | namespace { |
| 10310 | |
| 10311 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 10312 | public: |
| 10313 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10314 | set_on_complete(base::DoNothing()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10315 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 10316 | void OnCertificateRequested(URLRequest* request, |
| 10317 | SSLCertRequestInfo* cert_request_info) override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10318 | on_certificate_requested_count_++; |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10319 | std::move(on_certificate_requested_).Run(); |
| 10320 | } |
| 10321 | void RunUntilCertificateRequested() { |
| 10322 | base::RunLoop run_loop; |
| 10323 | on_certificate_requested_ = run_loop.QuitClosure(); |
| 10324 | run_loop.Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10325 | } |
| 10326 | int on_certificate_requested_count() { |
| 10327 | return on_certificate_requested_count_; |
| 10328 | } |
| 10329 | private: |
| 10330 | int on_certificate_requested_count_; |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10331 | base::OnceClosure on_certificate_requested_; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10332 | }; |
| 10333 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10334 | class TestSSLPrivateKey : public SSLPrivateKey { |
| 10335 | public: |
| 10336 | explicit TestSSLPrivateKey(scoped_refptr<SSLPrivateKey> key) |
| 10337 | : key_(std::move(key)) {} |
| 10338 | |
| 10339 | void set_fail_signing(bool fail_signing) { fail_signing_ = fail_signing; } |
| 10340 | int sign_count() const { return sign_count_; } |
| 10341 | |
David Benjamin | b65b073 | 2018-11-09 20:33:53 | [diff] [blame] | 10342 | std::string GetProviderName() override { return key_->GetProviderName(); } |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10343 | std::vector<uint16_t> GetAlgorithmPreferences() override { |
| 10344 | return key_->GetAlgorithmPreferences(); |
| 10345 | } |
| 10346 | void Sign(uint16_t algorithm, |
| 10347 | base::span<const uint8_t> input, |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 10348 | SignCallback callback) override { |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10349 | sign_count_++; |
| 10350 | if (fail_signing_) { |
| 10351 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 10352 | FROM_HERE, base::BindOnce(std::move(callback), |
| 10353 | ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED, |
| 10354 | std::vector<uint8_t>())); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10355 | } else { |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 10356 | key_->Sign(algorithm, input, std::move(callback)); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10357 | } |
| 10358 | } |
| 10359 | |
| 10360 | private: |
| 10361 | ~TestSSLPrivateKey() override = default; |
| 10362 | |
| 10363 | scoped_refptr<SSLPrivateKey> key_; |
| 10364 | bool fail_signing_ = false; |
| 10365 | int sign_count_ = 0; |
| 10366 | }; |
| 10367 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10368 | } // namespace |
| 10369 | |
| 10370 | // TODO(davidben): Test the rest of the code. Specifically, |
| 10371 | // - Filtering which certificates to select. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10372 | // - Getting a certificate request in an SSL renegotiation sending the |
| 10373 | // HTTP request. |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10374 | TEST_F(HTTPSRequestTest, ClientAuthNoCertificate) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10375 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10376 | net::SSLServerConfig ssl_config; |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 10377 | ssl_config.client_cert_type = |
| 10378 | SSLServerConfig::ClientCertType::OPTIONAL_CLIENT_CERT; |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10379 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10380 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10381 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10382 | ASSERT_TRUE(test_server.Start()); |
| 10383 | |
| 10384 | SSLClientAuthTestDelegate d; |
| 10385 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10386 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10387 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10388 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10389 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10390 | r->Start(); |
| 10391 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10392 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10393 | d.RunUntilCertificateRequested(); |
| 10394 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10395 | |
| 10396 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10397 | EXPECT_FALSE(d.received_data_before_response()); |
| 10398 | EXPECT_EQ(0, d.bytes_received()); |
| 10399 | |
| 10400 | // Send no certificate. |
| 10401 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 10402 | // all platforms so we can test sending a cert as well. |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 10403 | r->ContinueWithCertificate(nullptr, nullptr); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10404 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10405 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10406 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10407 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10408 | EXPECT_EQ(1, d.response_started_count()); |
| 10409 | EXPECT_FALSE(d.received_data_before_response()); |
| 10410 | EXPECT_NE(0, d.bytes_received()); |
| 10411 | } |
| 10412 | } |
| 10413 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10414 | TEST_F(HTTPSRequestTest, ClientAuth) { |
| 10415 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 10416 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 10417 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 10418 | ASSERT_TRUE(identity); |
| 10419 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 10420 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 10421 | |
| 10422 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10423 | net::SSLServerConfig ssl_config; |
| 10424 | ssl_config.client_cert_type = |
| 10425 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 10426 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 10427 | test_server.AddDefaultHandlers( |
| 10428 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10429 | ASSERT_TRUE(test_server.Start()); |
| 10430 | |
| 10431 | { |
| 10432 | SSLClientAuthTestDelegate d; |
| 10433 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10434 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10435 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10436 | |
| 10437 | r->Start(); |
| 10438 | EXPECT_TRUE(r->is_pending()); |
| 10439 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10440 | d.RunUntilCertificateRequested(); |
| 10441 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10442 | |
| 10443 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10444 | EXPECT_FALSE(d.received_data_before_response()); |
| 10445 | EXPECT_EQ(0, d.bytes_received()); |
| 10446 | |
| 10447 | // Send a certificate. |
| 10448 | r->ContinueWithCertificate(identity->certificate(), private_key); |
| 10449 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10450 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10451 | |
| 10452 | EXPECT_EQ(OK, d.request_status()); |
| 10453 | EXPECT_EQ(1, d.response_started_count()); |
| 10454 | EXPECT_FALSE(d.received_data_before_response()); |
| 10455 | EXPECT_NE(0, d.bytes_received()); |
| 10456 | |
| 10457 | // The private key should have been used. |
| 10458 | EXPECT_EQ(1, private_key->sign_count()); |
| 10459 | } |
| 10460 | |
| 10461 | // Close all connections and clear the session cache to force a new handshake. |
| 10462 | default_context_.http_transaction_factory() |
| 10463 | ->GetSession() |
| 10464 | ->CloseAllConnections(); |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10465 | default_context_.http_transaction_factory() |
| 10466 | ->GetSession() |
| 10467 | ->ClearSSLSessionCache(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10468 | |
| 10469 | // Connecting again should not call OnCertificateRequested. The identity is |
| 10470 | // taken from the client auth cache. |
| 10471 | { |
| 10472 | SSLClientAuthTestDelegate d; |
| 10473 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10474 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10475 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10476 | |
| 10477 | r->Start(); |
| 10478 | EXPECT_TRUE(r->is_pending()); |
| 10479 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10480 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10481 | |
| 10482 | EXPECT_EQ(OK, d.request_status()); |
| 10483 | EXPECT_EQ(0, d.on_certificate_requested_count()); |
| 10484 | EXPECT_FALSE(d.received_data_before_response()); |
| 10485 | EXPECT_EQ(1, d.response_started_count()); |
| 10486 | EXPECT_FALSE(d.received_data_before_response()); |
| 10487 | EXPECT_NE(0, d.bytes_received()); |
| 10488 | |
| 10489 | // The private key should have been used. |
| 10490 | EXPECT_EQ(2, private_key->sign_count()); |
| 10491 | } |
| 10492 | } |
| 10493 | |
| 10494 | // Test that private keys that fail to sign anything get evicted from the cache. |
| 10495 | TEST_F(HTTPSRequestTest, ClientAuthFailSigning) { |
| 10496 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 10497 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 10498 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 10499 | ASSERT_TRUE(identity); |
| 10500 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 10501 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 10502 | private_key->set_fail_signing(true); |
| 10503 | |
| 10504 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10505 | net::SSLServerConfig ssl_config; |
| 10506 | ssl_config.client_cert_type = |
| 10507 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 10508 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 10509 | test_server.AddDefaultHandlers( |
| 10510 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10511 | ASSERT_TRUE(test_server.Start()); |
| 10512 | |
| 10513 | { |
| 10514 | SSLClientAuthTestDelegate d; |
| 10515 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10516 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10517 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10518 | |
| 10519 | r->Start(); |
| 10520 | EXPECT_TRUE(r->is_pending()); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10521 | |
| 10522 | d.RunUntilCertificateRequested(); |
| 10523 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10524 | |
| 10525 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10526 | EXPECT_FALSE(d.received_data_before_response()); |
| 10527 | EXPECT_EQ(0, d.bytes_received()); |
| 10528 | |
| 10529 | // Send a certificate. |
| 10530 | r->ContinueWithCertificate(identity->certificate(), private_key); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10531 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10532 | |
| 10533 | // The private key cannot sign anything, so we report an error. |
| 10534 | EXPECT_EQ(ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED, d.request_status()); |
| 10535 | EXPECT_EQ(1, d.response_started_count()); |
| 10536 | EXPECT_FALSE(d.received_data_before_response()); |
| 10537 | EXPECT_EQ(0, d.bytes_received()); |
| 10538 | |
| 10539 | // The private key should have been used. |
| 10540 | EXPECT_EQ(1, private_key->sign_count()); |
| 10541 | } |
| 10542 | |
| 10543 | // Close all connections and clear the session cache to force a new handshake. |
| 10544 | default_context_.http_transaction_factory() |
| 10545 | ->GetSession() |
| 10546 | ->CloseAllConnections(); |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10547 | default_context_.http_transaction_factory() |
| 10548 | ->GetSession() |
| 10549 | ->ClearSSLSessionCache(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10550 | |
| 10551 | // The bad identity should have been evicted from the cache, so connecting |
| 10552 | // again should call OnCertificateRequested again. |
| 10553 | { |
| 10554 | SSLClientAuthTestDelegate d; |
| 10555 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10556 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10557 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10558 | |
| 10559 | r->Start(); |
| 10560 | EXPECT_TRUE(r->is_pending()); |
| 10561 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10562 | d.RunUntilCertificateRequested(); |
| 10563 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10564 | |
| 10565 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10566 | EXPECT_FALSE(d.received_data_before_response()); |
| 10567 | EXPECT_EQ(0, d.bytes_received()); |
| 10568 | |
| 10569 | // There should have been no additional uses of the private key. |
| 10570 | EXPECT_EQ(1, private_key->sign_count()); |
| 10571 | } |
| 10572 | } |
| 10573 | |
| 10574 | // Test that cached private keys that fail to sign anything trigger a |
| 10575 | // retry. This is so we handle unplugged smartcards |
| 10576 | // gracefully. https://crbug.com/813022. |
| 10577 | TEST_F(HTTPSRequestTest, ClientAuthFailSigningRetry) { |
| 10578 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 10579 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 10580 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 10581 | ASSERT_TRUE(identity); |
| 10582 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 10583 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 10584 | |
| 10585 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10586 | net::SSLServerConfig ssl_config; |
| 10587 | ssl_config.client_cert_type = |
| 10588 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 10589 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 10590 | test_server.AddDefaultHandlers( |
| 10591 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10592 | ASSERT_TRUE(test_server.Start()); |
| 10593 | |
| 10594 | // Connect with a client certificate to put it in the client auth cache. |
| 10595 | { |
| 10596 | SSLClientAuthTestDelegate d; |
| 10597 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10598 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10599 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10600 | |
| 10601 | r->Start(); |
| 10602 | EXPECT_TRUE(r->is_pending()); |
| 10603 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10604 | d.RunUntilCertificateRequested(); |
| 10605 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10606 | |
| 10607 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10608 | EXPECT_FALSE(d.received_data_before_response()); |
| 10609 | EXPECT_EQ(0, d.bytes_received()); |
| 10610 | |
| 10611 | r->ContinueWithCertificate(identity->certificate(), private_key); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10612 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10613 | |
| 10614 | EXPECT_EQ(OK, d.request_status()); |
| 10615 | EXPECT_EQ(1, d.response_started_count()); |
| 10616 | EXPECT_FALSE(d.received_data_before_response()); |
| 10617 | EXPECT_NE(0, d.bytes_received()); |
| 10618 | |
| 10619 | // The private key should have been used. |
| 10620 | EXPECT_EQ(1, private_key->sign_count()); |
| 10621 | } |
| 10622 | |
| 10623 | // Close all connections and clear the session cache to force a new handshake. |
| 10624 | default_context_.http_transaction_factory() |
| 10625 | ->GetSession() |
| 10626 | ->CloseAllConnections(); |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10627 | default_context_.http_transaction_factory() |
| 10628 | ->GetSession() |
| 10629 | ->ClearSSLSessionCache(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10630 | |
| 10631 | // Cause the private key to fail. Connecting again should attempt to use it, |
| 10632 | // notice the failure, and then request a new identity via |
| 10633 | // OnCertificateRequested. |
| 10634 | private_key->set_fail_signing(true); |
| 10635 | |
| 10636 | { |
| 10637 | SSLClientAuthTestDelegate d; |
| 10638 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10639 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10640 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10641 | |
| 10642 | r->Start(); |
| 10643 | EXPECT_TRUE(r->is_pending()); |
| 10644 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10645 | d.RunUntilCertificateRequested(); |
| 10646 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10647 | |
| 10648 | // There was an additional signing call on the private key (the one which |
| 10649 | // failed). |
| 10650 | EXPECT_EQ(2, private_key->sign_count()); |
| 10651 | |
| 10652 | // That caused another OnCertificateRequested call. |
| 10653 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10654 | EXPECT_FALSE(d.received_data_before_response()); |
| 10655 | EXPECT_EQ(0, d.bytes_received()); |
| 10656 | } |
| 10657 | } |
| 10658 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10659 | TEST_F(HTTPSRequestTest, ResumeTest) { |
| 10660 | // Test that we attempt a session resume when making two connections to the |
| 10661 | // same host. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10662 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10663 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10664 | SpawnedTestServer test_server( |
| 10665 | SpawnedTestServer::TYPE_HTTPS, |
| 10666 | ssl_options, |
| 10667 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10668 | ASSERT_TRUE(test_server.Start()); |
| 10669 | |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10670 | default_context_.http_transaction_factory() |
| 10671 | ->GetSession() |
| 10672 | ->ClearSSLSessionCache(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10673 | |
| 10674 | { |
| 10675 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10676 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10677 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10678 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10679 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10680 | r->Start(); |
| 10681 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10682 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10683 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10684 | |
| 10685 | EXPECT_EQ(1, d.response_started_count()); |
| 10686 | } |
| 10687 | |
| 10688 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 10689 | CloseAllConnections(); |
| 10690 | |
| 10691 | { |
| 10692 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10693 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10694 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10695 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10696 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10697 | r->Start(); |
| 10698 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10699 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10700 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10701 | |
| 10702 | // The response will look like; |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10703 | // lookup uvw (TLS 1.3's compatibility session ID) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10704 | // insert abc |
| 10705 | // lookup abc |
| 10706 | // insert xyz |
| 10707 | // |
| 10708 | // With a newline at the end which makes the split think that there are |
| 10709 | // four lines. |
| 10710 | |
| 10711 | EXPECT_EQ(1, d.response_started_count()); |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 10712 | std::vector<std::string> lines = base::SplitString( |
| 10713 | d.data_received(), "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10714 | ASSERT_EQ(5u, lines.size()) << d.data_received(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10715 | |
| 10716 | std::string session_id; |
| 10717 | |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10718 | for (size_t i = 0; i < 3; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 10719 | std::vector<std::string> parts = base::SplitString( |
| 10720 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10721 | ASSERT_EQ(2u, parts.size()); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10722 | if (i % 2 == 1) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10723 | EXPECT_EQ("insert", parts[0]); |
| 10724 | session_id = parts[1]; |
| 10725 | } else { |
| 10726 | EXPECT_EQ("lookup", parts[0]); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10727 | if (i != 0) |
| 10728 | EXPECT_EQ(session_id, parts[1]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10729 | } |
| 10730 | } |
| 10731 | } |
| 10732 | } |
| 10733 | |
| 10734 | TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { |
| 10735 | // Test that sessions aren't resumed when the value of ssl_session_cache_shard |
| 10736 | // differs. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10737 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10738 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10739 | SpawnedTestServer test_server( |
| 10740 | SpawnedTestServer::TYPE_HTTPS, |
| 10741 | ssl_options, |
| 10742 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10743 | ASSERT_TRUE(test_server.Start()); |
| 10744 | |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10745 | default_context_.http_transaction_factory() |
| 10746 | ->GetSession() |
| 10747 | ->ClearSSLSessionCache(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10748 | |
| 10749 | { |
| 10750 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10751 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 10752 | test_server.GetURL("/"), DEFAULT_PRIORITY, &d, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10753 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10754 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10755 | r->Start(); |
| 10756 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10757 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10758 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10759 | |
| 10760 | EXPECT_EQ(1, d.response_started_count()); |
| 10761 | } |
| 10762 | |
| 10763 | // Now create a new HttpCache with a different ssl_session_cache_shard value. |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10764 | HttpNetworkSession::Context session_context; |
| 10765 | session_context.host_resolver = default_context_.host_resolver(); |
| 10766 | session_context.cert_verifier = default_context_.cert_verifier(); |
| 10767 | session_context.transport_security_state = |
| 10768 | default_context_.transport_security_state(); |
| 10769 | session_context.cert_transparency_verifier = |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10770 | default_context_.cert_transparency_verifier(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10771 | session_context.ct_policy_enforcer = default_context_.ct_policy_enforcer(); |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 10772 | session_context.proxy_resolution_service = default_context_.proxy_resolution_service(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10773 | session_context.ssl_config_service = default_context_.ssl_config_service(); |
| 10774 | session_context.http_auth_handler_factory = |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10775 | default_context_.http_auth_handler_factory(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10776 | session_context.http_server_properties = |
| 10777 | default_context_.http_server_properties(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10778 | |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10779 | HttpNetworkSession network_session(HttpNetworkSession::Params(), |
| 10780 | session_context); |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 10781 | std::unique_ptr<HttpCache> cache( |
| 10782 | new HttpCache(&network_session, HttpCache::DefaultBackend::InMemory(0), |
| 10783 | false /* is_main_cache */)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10784 | |
| 10785 | default_context_.set_http_transaction_factory(cache.get()); |
| 10786 | |
| 10787 | { |
| 10788 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10789 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10790 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10791 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10792 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10793 | r->Start(); |
| 10794 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10795 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10796 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10797 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10798 | EXPECT_EQ(1, d.response_started_count()); |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 10799 | EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, r->ssl_info().handshake_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10800 | } |
| 10801 | } |
| 10802 | |
Daniel McArdle | da3fa94 | 2019-02-15 16:41:21 | [diff] [blame] | 10803 | // Test that sessions started with privacy mode enabled cannot be resumed when |
| 10804 | // it is disabled, and vice versa. |
| 10805 | TEST_F(HTTPSRequestTest, NoSessionResumptionBetweenPrivacyModes) { |
| 10806 | // Start a server. |
| 10807 | SpawnedTestServer test_server( |
| 10808 | SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::SSLOptions(), |
| 10809 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10810 | ASSERT_TRUE(test_server.Start()); |
| 10811 | const auto url = test_server.GetURL("/"); |
| 10812 | |
| 10813 | auto ConnectAndCheckHandshake = [this, url](bool allow_credentials, |
| 10814 | auto expected_handshake) { |
| 10815 | // Construct request and indirectly set the privacy mode. |
| 10816 | TestDelegate d; |
| 10817 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10818 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10819 | r->set_allow_credentials(allow_credentials); |
| 10820 | |
| 10821 | // Start the request and check the SSL handshake type. |
| 10822 | r->Start(); |
| 10823 | EXPECT_TRUE(r->is_pending()); |
| 10824 | d.RunUntilComplete(); |
| 10825 | EXPECT_EQ(1, d.response_started_count()); |
| 10826 | EXPECT_EQ(expected_handshake, r->ssl_info().handshake_type); |
| 10827 | }; |
| 10828 | |
| 10829 | // Exhaustively check all pairs of privacy mode values. Note that we are using |
| 10830 | // allow_credentials to indirectly enable/disable privacy mode. |
| 10831 | const bool kAllowCredentialsValues[] = {false, true}; |
| 10832 | for (const auto allow_creds_1 : kAllowCredentialsValues) { |
| 10833 | for (const auto allow_creds_2 : kAllowCredentialsValues) { |
| 10834 | SCOPED_TRACE(base::StringPrintf("allow_creds_1=%d, allow_creds_2=%d", |
| 10835 | allow_creds_1, allow_creds_2)); |
| 10836 | |
| 10837 | // The session cache starts off empty, so we expect a full handshake. |
| 10838 | ConnectAndCheckHandshake(allow_creds_1, SSLInfo::HANDSHAKE_FULL); |
| 10839 | |
| 10840 | // The second handshake depends on whether we are using the same session |
| 10841 | // cache as the first request. |
| 10842 | ConnectAndCheckHandshake(allow_creds_2, allow_creds_1 == allow_creds_2 |
| 10843 | ? SSLInfo::HANDSHAKE_RESUME |
| 10844 | : SSLInfo::HANDSHAKE_FULL); |
| 10845 | // Flush both session caches. |
| 10846 | auto* network_session = |
| 10847 | default_context_.http_transaction_factory()->GetSession(); |
| 10848 | network_session->ClearSSLSessionCache(); |
| 10849 | } |
| 10850 | } |
| 10851 | } |
| 10852 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 10853 | class HTTPSFallbackTest : public TestWithScopedTaskEnvironment { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10854 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 10855 | HTTPSFallbackTest() : context_(true) { |
Nick Harper | 2243e800 | 2018-09-28 20:33:26 | [diff] [blame] | 10856 | ssl_config_service_ = std::make_unique<TestSSLConfigService>(); |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10857 | context_.set_ssl_config_service(ssl_config_service_.get()); |
| 10858 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 10859 | ~HTTPSFallbackTest() override = default; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10860 | |
| 10861 | protected: |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10862 | TestSSLConfigService* ssl_config_service() { |
| 10863 | return ssl_config_service_.get(); |
| 10864 | } |
| 10865 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10866 | void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) { |
| 10867 | DCHECK(!request_); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10868 | context_.Init(); |
| 10869 | delegate_.set_allow_certificate_errors(true); |
| 10870 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10871 | SpawnedTestServer test_server( |
| 10872 | SpawnedTestServer::TYPE_HTTPS, |
| 10873 | ssl_options, |
| 10874 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10875 | ASSERT_TRUE(test_server.Start()); |
| 10876 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10877 | request_ = context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10878 | &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10879 | request_->Start(); |
| 10880 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10881 | delegate_.RunUntilComplete(); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10882 | } |
| 10883 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10884 | void ExpectConnection(int version) { |
| 10885 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 10886 | EXPECT_NE(0, delegate_.bytes_received()); |
| 10887 | EXPECT_EQ(version, SSLConnectionStatusToVersion( |
| 10888 | request_->ssl_info().connection_status)); |
| 10889 | } |
| 10890 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10891 | void ExpectFailure(int error) { |
| 10892 | EXPECT_EQ(1, delegate_.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 10893 | EXPECT_EQ(error, delegate_.request_status()); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10894 | } |
| 10895 | |
| 10896 | private: |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 10897 | std::unique_ptr<TestSSLConfigService> ssl_config_service_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10898 | TestDelegate delegate_; |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 10899 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10900 | std::unique_ptr<URLRequest> request_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10901 | }; |
| 10902 | |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10903 | // Tests the TLS 1.0 fallback doesn't happen. |
| 10904 | TEST_F(HTTPSFallbackTest, TLSv1NoFallback) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10905 | SpawnedTestServer::SSLOptions ssl_options( |
| 10906 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10907 | ssl_options.tls_intolerant = |
| 10908 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 10909 | |
| 10910 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 10911 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10912 | } |
| 10913 | |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 10914 | // Tests the TLS 1.1 fallback doesn't happen. |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 10915 | TEST_F(HTTPSFallbackTest, TLSv1_1NoFallback) { |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10916 | SpawnedTestServer::SSLOptions ssl_options( |
| 10917 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10918 | ssl_options.tls_intolerant = |
| 10919 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2; |
| 10920 | |
| 10921 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10922 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
| 10923 | } |
| 10924 | |
David Benjamin | d61bd53 | 2019-04-23 21:11:37 | [diff] [blame] | 10925 | // Tests the TLS 1.2 fallback doesn't happen. |
| 10926 | TEST_F(HTTPSFallbackTest, TLSv1_2NoFallback) { |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10927 | SpawnedTestServer::SSLOptions ssl_options( |
| 10928 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10929 | ssl_options.tls_intolerant = |
| 10930 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_3; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10931 | |
| 10932 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
David Benjamin | d61bd53 | 2019-04-23 21:11:37 | [diff] [blame] | 10933 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10934 | } |
| 10935 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 10936 | class HTTPSSessionTest : public TestWithScopedTaskEnvironment { |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10937 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 10938 | HTTPSSessionTest() : default_context_(true) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10939 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10940 | |
| 10941 | default_context_.set_network_delegate(&default_network_delegate_); |
| 10942 | default_context_.set_cert_verifier(&cert_verifier_); |
| 10943 | default_context_.Init(); |
| 10944 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 10945 | ~HTTPSSessionTest() override = default; |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10946 | |
| 10947 | protected: |
| 10948 | MockCertVerifier cert_verifier_; |
| 10949 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 10950 | TestURLRequestContext default_context_; |
| 10951 | }; |
| 10952 | |
| 10953 | // Tests that session resumption is not attempted if an invalid certificate |
| 10954 | // is presented. |
| 10955 | TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) { |
| 10956 | SpawnedTestServer::SSLOptions ssl_options; |
| 10957 | ssl_options.record_resume = true; |
| 10958 | SpawnedTestServer test_server( |
| 10959 | SpawnedTestServer::TYPE_HTTPS, |
| 10960 | ssl_options, |
| 10961 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10962 | ASSERT_TRUE(test_server.Start()); |
| 10963 | |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10964 | default_context_.http_transaction_factory() |
| 10965 | ->GetSession() |
| 10966 | ->ClearSSLSessionCache(); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10967 | |
| 10968 | // Simulate the certificate being expired and attempt a connection. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10969 | cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10970 | { |
| 10971 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10972 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 10973 | test_server.GetURL("/"), DEFAULT_PRIORITY, &d, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10974 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10975 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10976 | r->Start(); |
| 10977 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10978 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10979 | d.RunUntilComplete(); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10980 | |
| 10981 | EXPECT_EQ(1, d.response_started_count()); |
| 10982 | } |
| 10983 | |
| 10984 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 10985 | CloseAllConnections(); |
| 10986 | |
| 10987 | // Now change the certificate to be acceptable (so that the response is |
| 10988 | // loaded), and ensure that no session id is presented to the peer. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10989 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10990 | { |
| 10991 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10992 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10993 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10994 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10995 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10996 | r->Start(); |
| 10997 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10998 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10999 | d.RunUntilComplete(); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11000 | |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11001 | EXPECT_EQ(1, d.response_started_count()); |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 11002 | EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, r->ssl_info().handshake_type); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11003 | } |
| 11004 | } |
| 11005 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11006 | // This the fingerprint of the "Testing CA" certificate used by the testserver. |
| 11007 | // See net/data/ssl/certificates/ocsp-test-root.pem. |
David Benjamin | 9cedc3a5 | 2017-08-20 21:30:58 | [diff] [blame] | 11008 | static const SHA256HashValue kOCSPTestCertFingerprint = {{ |
| 11009 | 0x0c, 0xa9, 0x05, 0x11, 0xb0, 0xa2, 0xc0, 0x1d, 0x40, 0x6a, 0x99, |
| 11010 | 0x04, 0x21, 0x36, 0x45, 0x3f, 0x59, 0x12, 0x5c, 0x80, 0x64, 0x2d, |
| 11011 | 0x46, 0x6a, 0x3b, 0x78, 0x9e, 0x84, 0xea, 0x54, 0x0f, 0x8b, |
mattm | 3b4376f1 | 2016-10-03 21:07:15 | [diff] [blame] | 11012 | }}; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11013 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11014 | // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the |
| 11015 | // testserver. |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11016 | static const SHA256HashValue kOCSPTestCertSPKI = {{ |
| 11017 | 0x05, 0xa8, 0xf6, 0xfd, 0x8e, 0x10, 0xfe, 0x92, 0x2f, 0x22, 0x75, |
| 11018 | 0x46, 0x40, 0xf4, 0xc4, 0x57, 0x06, 0x0d, 0x95, 0xfd, 0x60, 0x31, |
| 11019 | 0x3b, 0xf3, 0xfc, 0x12, 0x47, 0xe7, 0x66, 0x1a, 0x82, 0xa3, |
| 11020 | }}; |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11021 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11022 | // This is the policy OID contained in the certificates that testserver |
| 11023 | // generates. |
| 11024 | static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1"; |
| 11025 | |
| 11026 | class HTTPSOCSPTest : public HTTPSRequestTest { |
| 11027 | public: |
| 11028 | HTTPSOCSPTest() |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 11029 | : context_(true), |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11030 | ev_test_policy_( |
| 11031 | new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(), |
| 11032 | kOCSPTestCertFingerprint, |
| 11033 | kOCSPTestCertPolicy)) { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11034 | } |
| 11035 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 11036 | void SetUp() override { |
Matt Mueller | a4aaa9d | 2019-04-24 21:09:14 | [diff] [blame] | 11037 | cert_net_fetcher_ = base::MakeRefCounted<CertNetFetcherImpl>(); |
| 11038 | cert_verifier_ = CertVerifier::CreateDefault(cert_net_fetcher_); |
| 11039 | context_.set_cert_verifier(cert_verifier_.get()); |
Ryan Sleevi | 8a9c9c1 | 2018-05-09 02:36:23 | [diff] [blame] | 11040 | context_.SetCTPolicyEnforcer(std::make_unique<DefaultCTPolicyEnforcer>()); |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 11041 | context_.Init(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11042 | |
Matt Mueller | a4aaa9d | 2019-04-24 21:09:14 | [diff] [blame] | 11043 | cert_net_fetcher_->SetURLRequestContext(&context_); |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11044 | context_.cert_verifier()->SetConfig(GetCertVerifierConfig()); |
| 11045 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 11046 | scoped_refptr<X509Certificate> root_cert = |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11047 | ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
Matt Mueller | 09c8fd9a | 2019-05-04 00:37:24 | [diff] [blame] | 11048 | ASSERT_TRUE(root_cert); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 11049 | test_root_.reset(new ScopedTestRoot(root_cert.get())); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11050 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 11051 | #if defined(USE_NSS_CERTS) |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 11052 | SetURLRequestContextForNSSHttpIO(&context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11053 | #endif |
| 11054 | } |
| 11055 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11056 | void DoConnectionWithDelegate( |
| 11057 | const SpawnedTestServer::SSLOptions& ssl_options, |
| 11058 | TestDelegate* delegate, |
| 11059 | SSLInfo* out_ssl_info) { |
| 11060 | // Always overwrite |out_ssl_info|. |
| 11061 | out_ssl_info->Reset(); |
| 11062 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11063 | SpawnedTestServer test_server( |
| 11064 | SpawnedTestServer::TYPE_HTTPS, |
| 11065 | ssl_options, |
| 11066 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11067 | ASSERT_TRUE(test_server.Start()); |
| 11068 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11069 | delegate->set_allow_certificate_errors(true); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11070 | std::unique_ptr<URLRequest> r( |
| 11071 | context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
| 11072 | delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11073 | r->Start(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11074 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11075 | delegate->RunUntilComplete(); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11076 | EXPECT_EQ(1, delegate->response_started_count()); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11077 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11078 | *out_ssl_info = r->ssl_info(); |
| 11079 | } |
| 11080 | |
| 11081 | void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options, |
| 11082 | CertStatus* out_cert_status) { |
| 11083 | // Always overwrite |out_cert_status|. |
| 11084 | *out_cert_status = 0; |
| 11085 | |
| 11086 | TestDelegate d; |
| 11087 | SSLInfo ssl_info; |
| 11088 | ASSERT_NO_FATAL_FAILURE( |
| 11089 | DoConnectionWithDelegate(ssl_options, &d, &ssl_info)); |
| 11090 | |
| 11091 | *out_cert_status = ssl_info.cert_status; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11092 | } |
| 11093 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 11094 | ~HTTPSOCSPTest() override { |
Matt Mueller | a4aaa9d | 2019-04-24 21:09:14 | [diff] [blame] | 11095 | cert_net_fetcher_->Shutdown(); |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 11096 | #if defined(USE_NSS_CERTS) |
Matt Mueller | a17ffaf | 2018-03-28 20:08:18 | [diff] [blame] | 11097 | SetURLRequestContextForNSSHttpIO(nullptr); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11098 | #endif |
| 11099 | } |
| 11100 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11101 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11102 | // GetCertVerifierConfig() configures the URLRequestContext that will be used |
| 11103 | // for making connections to the testserver. This can be overridden in test |
| 11104 | // subclasses for different behaviour. |
| 11105 | virtual CertVerifier::Config GetCertVerifierConfig() { |
| 11106 | CertVerifier::Config config; |
| 11107 | config.enable_rev_checking = true; |
| 11108 | return config; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11109 | } |
| 11110 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11111 | std::unique_ptr<ScopedTestRoot> test_root_; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11112 | std::unique_ptr<TestSSLConfigService> ssl_config_service_; |
Matt Mueller | a4aaa9d | 2019-04-24 21:09:14 | [diff] [blame] | 11113 | scoped_refptr<CertNetFetcherImpl> cert_net_fetcher_; |
| 11114 | std::unique_ptr<CertVerifier> cert_verifier_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 11115 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11116 | std::unique_ptr<ScopedTestEVPolicy> ev_test_policy_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11117 | }; |
| 11118 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11119 | static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() { |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11120 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11121 | // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't |
| 11122 | // have that ability on other platforms. |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11123 | // TODO(eroman): Should this also be the return value for |
| 11124 | // CertVerifyProcBuiltin? |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11125 | return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 11126 | #else |
| 11127 | return 0; |
| 11128 | #endif |
| 11129 | } |
| 11130 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11131 | // SystemSupportsHardFailRevocationChecking returns true iff the current |
| 11132 | // operating system supports revocation checking and can distinguish between |
| 11133 | // situations where a given certificate lacks any revocation information (eg: |
| 11134 | // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when |
| 11135 | // revocation information cannot be obtained (eg: the CRL was unreachable). |
| 11136 | // If it does not, then tests which rely on 'hard fail' behaviour should be |
| 11137 | // skipped. |
| 11138 | static bool SystemSupportsHardFailRevocationChecking() { |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11139 | #if defined(OS_WIN) || defined(USE_NSS_CERTS) || \ |
| 11140 | defined(USE_BUILTIN_CERT_VERIFIER) |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11141 | return true; |
| 11142 | #else |
| 11143 | return false; |
| 11144 | #endif |
| 11145 | } |
| 11146 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11147 | // SystemUsesChromiumEVMetadata returns true iff the current operating system |
| 11148 | // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then |
| 11149 | // several tests are effected because our testing EV certificate won't be |
| 11150 | // recognised as EV. |
| 11151 | static bool SystemUsesChromiumEVMetadata() { |
Eric Roman | 5f8d9d2 | 2017-10-17 02:32:52 | [diff] [blame] | 11152 | #if defined(PLATFORM_USES_CHROMIUM_EV_METADATA) |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 11153 | return true; |
Eric Roman | 5f8d9d2 | 2017-10-17 02:32:52 | [diff] [blame] | 11154 | #else |
| 11155 | return false; |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 11156 | #endif |
| 11157 | } |
| 11158 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11159 | // Returns the expected CertStatus for tests that expect an online revocation |
| 11160 | // check failure as a result of checking a test EV cert, which will not |
| 11161 | // actually trigger an online revocation check on some platforms. |
| 11162 | static CertStatus ExpectedCertStatusForFailedOnlineEVRevocationCheck() { |
| 11163 | if (SystemUsesChromiumEVMetadata()) { |
| 11164 | return ExpectedCertStatusForFailedOnlineRevocationCheck(); |
| 11165 | } else { |
| 11166 | // If SystemUsesChromiumEVMetadata is false, revocation checking will not |
| 11167 | // be enabled, and thus there will not be a revocation check to fail. |
| 11168 | return 0u; |
| 11169 | } |
| 11170 | } |
| 11171 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11172 | static bool SystemSupportsOCSP() { |
Sergey Ulanov | ee7c8db | 2017-12-08 00:18:39 | [diff] [blame] | 11173 | #if defined(OS_ANDROID) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11174 | // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. |
| 11175 | return false; |
| 11176 | #else |
| 11177 | return true; |
| 11178 | #endif |
| 11179 | } |
| 11180 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11181 | static bool SystemSupportsOCSPStapling() { |
Matt Mueller | 7f60dc4 | 2019-04-26 22:47:10 | [diff] [blame] | 11182 | #if defined(OS_ANDROID) |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11183 | return false; |
Matt Mueller | 7f60dc4 | 2019-04-26 22:47:10 | [diff] [blame] | 11184 | #elif defined(OS_MACOSX) |
| 11185 | // The SecTrustSetOCSPResponse function exists since macOS 10.9+, but does |
| 11186 | // not actually do anything until 10.12. |
| 11187 | if (base::mac::IsAtLeastOS10_12()) |
| 11188 | return true; |
| 11189 | return false; |
| 11190 | #else |
| 11191 | return true; |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11192 | #endif |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11193 | } |
| 11194 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11195 | TEST_F(HTTPSOCSPTest, Valid) { |
| 11196 | if (!SystemSupportsOCSP()) { |
| 11197 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11198 | return; |
| 11199 | } |
| 11200 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11201 | SpawnedTestServer::SSLOptions ssl_options( |
| 11202 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11203 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11204 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11205 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11206 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11207 | |
| 11208 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11209 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11210 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11211 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11212 | |
| 11213 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11214 | } |
| 11215 | |
| 11216 | TEST_F(HTTPSOCSPTest, Revoked) { |
| 11217 | if (!SystemSupportsOCSP()) { |
| 11218 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11219 | return; |
| 11220 | } |
| 11221 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11222 | SpawnedTestServer::SSLOptions ssl_options( |
| 11223 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11224 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11225 | |
| 11226 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11227 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11228 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11229 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11230 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11231 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11232 | } |
| 11233 | |
| 11234 | TEST_F(HTTPSOCSPTest, Invalid) { |
| 11235 | if (!SystemSupportsOCSP()) { |
| 11236 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11237 | return; |
| 11238 | } |
| 11239 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11240 | SpawnedTestServer::SSLOptions ssl_options( |
| 11241 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11242 | ssl_options.ocsp_status = |
| 11243 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11244 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11245 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11246 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11247 | |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11248 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 11249 | // TODO(649017): This test uses soft-fail revocation checking, but returns an |
| 11250 | // invalid OCSP response (can't parse). CertVerifyProcBuiltin currently |
| 11251 | // doesn't consider this a candidate for soft-fail (only considers |
| 11252 | // network-level failures as skippable). |
| 11253 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 11254 | cert_status & CERT_STATUS_UNABLE_TO_CHECK_REVOCATION); |
| 11255 | #else |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11256 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11257 | cert_status & CERT_STATUS_ALL_ERRORS); |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11258 | #endif |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11259 | |
| 11260 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 11261 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11262 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11263 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11264 | |
Matt Mueller | 5339d86 | 2018-05-02 18:53:57 | [diff] [blame] | 11265 | TEST_F(HTTPSOCSPTest, IntermediateValid) { |
| 11266 | if (!SystemSupportsOCSP()) { |
| 11267 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11268 | return; |
| 11269 | } |
| 11270 | |
| 11271 | SpawnedTestServer::SSLOptions ssl_options( |
| 11272 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11273 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11274 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11275 | |
| 11276 | CertStatus cert_status; |
| 11277 | DoConnection(ssl_options, &cert_status); |
| 11278 | |
| 11279 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11280 | |
| 11281 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11282 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11283 | |
| 11284 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11285 | } |
| 11286 | |
| 11287 | TEST_F(HTTPSOCSPTest, IntermediateResponseOldButStillValid) { |
| 11288 | if (!SystemSupportsOCSP()) { |
| 11289 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11290 | return; |
| 11291 | } |
| 11292 | |
| 11293 | SpawnedTestServer::SSLOptions ssl_options( |
| 11294 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11295 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11296 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11297 | // Use an OCSP response for the intermediate that would be too old for a leaf |
| 11298 | // cert, but is still valid for an intermediate. |
| 11299 | ssl_options.ocsp_intermediate_date = |
| 11300 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG; |
| 11301 | |
| 11302 | CertStatus cert_status; |
| 11303 | DoConnection(ssl_options, &cert_status); |
| 11304 | |
| 11305 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11306 | |
| 11307 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11308 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11309 | |
| 11310 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11311 | } |
| 11312 | |
| 11313 | TEST_F(HTTPSOCSPTest, IntermediateResponseTooOld) { |
| 11314 | if (!SystemSupportsOCSP()) { |
| 11315 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11316 | return; |
| 11317 | } |
| 11318 | |
| 11319 | SpawnedTestServer::SSLOptions ssl_options( |
| 11320 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11321 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11322 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11323 | ssl_options.ocsp_intermediate_date = |
| 11324 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONGER; |
| 11325 | |
| 11326 | CertStatus cert_status; |
| 11327 | DoConnection(ssl_options, &cert_status); |
| 11328 | |
| 11329 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 11330 | // The builtin verifier enforces the baseline requirements for max age of an |
| 11331 | // intermediate's OCSP response. |
| 11332 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 11333 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11334 | EXPECT_EQ(0u, cert_status & CERT_STATUS_IS_EV); |
| 11335 | #else |
| 11336 | // The platform verifiers are more lenient. |
| 11337 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11338 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11339 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11340 | #endif |
| 11341 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11342 | } |
| 11343 | |
| 11344 | TEST_F(HTTPSOCSPTest, IntermediateRevoked) { |
| 11345 | if (!SystemSupportsOCSP()) { |
| 11346 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11347 | return; |
| 11348 | } |
| 11349 | |
| 11350 | SpawnedTestServer::SSLOptions ssl_options( |
| 11351 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11352 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11353 | ssl_options.ocsp_intermediate_status = |
| 11354 | SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 11355 | |
| 11356 | CertStatus cert_status; |
| 11357 | DoConnection(ssl_options, &cert_status); |
| 11358 | |
| 11359 | #if defined(OS_WIN) |
| 11360 | // TODO(mattm): why does CertVerifyProcWin accept this? |
| 11361 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11362 | #else |
| 11363 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11364 | #endif |
| 11365 | EXPECT_EQ(0u, cert_status & CERT_STATUS_IS_EV); |
| 11366 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11367 | } |
| 11368 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11369 | TEST_F(HTTPSOCSPTest, ValidStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11370 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11371 | LOG(WARNING) |
| 11372 | << "Skipping test because system doesn't support OCSP stapling"; |
| 11373 | return; |
| 11374 | } |
| 11375 | |
| 11376 | SpawnedTestServer::SSLOptions ssl_options( |
| 11377 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11378 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11379 | ssl_options.staple_ocsp_response = true; |
| 11380 | ssl_options.ocsp_server_unavailable = true; |
| 11381 | |
| 11382 | CertStatus cert_status; |
| 11383 | DoConnection(ssl_options, &cert_status); |
| 11384 | |
| 11385 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11386 | |
| 11387 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11388 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11389 | |
| 11390 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11391 | } |
| 11392 | |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 11393 | // Disabled on NSS ports. See https://crbug.com/431716. |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 11394 | #if defined(USE_NSS_CERTS) |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 11395 | #define MAYBE_RevokedStapled DISABLED_RevokedStapled |
| 11396 | #else |
| 11397 | #define MAYBE_RevokedStapled RevokedStapled |
| 11398 | #endif |
| 11399 | TEST_F(HTTPSOCSPTest, MAYBE_RevokedStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11400 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11401 | LOG(WARNING) |
| 11402 | << "Skipping test because system doesn't support OCSP stapling"; |
| 11403 | return; |
| 11404 | } |
| 11405 | |
| 11406 | SpawnedTestServer::SSLOptions ssl_options( |
| 11407 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11408 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 11409 | ssl_options.staple_ocsp_response = true; |
| 11410 | ssl_options.ocsp_server_unavailable = true; |
| 11411 | |
| 11412 | CertStatus cert_status; |
| 11413 | DoConnection(ssl_options, &cert_status); |
| 11414 | |
| 11415 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11416 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11417 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11418 | } |
| 11419 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11420 | static const struct OCSPVerifyTestData { |
| 11421 | std::vector<SpawnedTestServer::SSLOptions::OCSPSingleResponse> ocsp_responses; |
| 11422 | SpawnedTestServer::SSLOptions::OCSPProduced ocsp_produced; |
| 11423 | OCSPVerifyResult::ResponseStatus response_status; |
| 11424 | bool has_revocation_status; |
| 11425 | OCSPRevocationStatus cert_status; |
| 11426 | } kOCSPVerifyData[] = { |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11427 | // 0 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11428 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11429 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11430 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11431 | OCSPVerifyResult::PROVIDED, |
| 11432 | true, |
| 11433 | OCSPRevocationStatus::GOOD}, |
| 11434 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11435 | // 1 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11436 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11437 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 11438 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11439 | OCSPVerifyResult::INVALID_DATE, |
| 11440 | false, |
| 11441 | OCSPRevocationStatus::UNKNOWN}, |
| 11442 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11443 | // 2 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11444 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11445 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11446 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11447 | OCSPVerifyResult::INVALID_DATE, |
| 11448 | false, |
| 11449 | OCSPRevocationStatus::UNKNOWN}, |
| 11450 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11451 | // 3 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11452 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11453 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11454 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11455 | OCSPVerifyResult::INVALID_DATE, |
| 11456 | false, |
| 11457 | OCSPRevocationStatus::UNKNOWN}, |
| 11458 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11459 | // 4 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11460 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11461 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11462 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11463 | OCSPVerifyResult::INVALID_DATE, |
| 11464 | false, |
| 11465 | OCSPRevocationStatus::UNKNOWN}, |
| 11466 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11467 | // 5 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11468 | {{{SpawnedTestServer::SSLOptions::OCSP_TRY_LATER, |
| 11469 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11470 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11471 | OCSPVerifyResult::ERROR_RESPONSE, |
| 11472 | false, |
| 11473 | OCSPRevocationStatus::UNKNOWN}, |
| 11474 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11475 | // 6 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11476 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE, |
| 11477 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11478 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11479 | OCSPVerifyResult::PARSE_RESPONSE_ERROR, |
| 11480 | false, |
| 11481 | OCSPRevocationStatus::UNKNOWN}, |
| 11482 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11483 | // 7 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11484 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE_DATA, |
| 11485 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11486 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11487 | OCSPVerifyResult::PARSE_RESPONSE_DATA_ERROR, |
| 11488 | false, |
| 11489 | OCSPRevocationStatus::UNKNOWN}, |
| 11490 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11491 | // 8 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11492 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11493 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11494 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11495 | OCSPVerifyResult::INVALID_DATE, |
| 11496 | false, |
| 11497 | OCSPRevocationStatus::UNKNOWN}, |
| 11498 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11499 | // 9 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11500 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11501 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11502 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11503 | OCSPVerifyResult::PROVIDED, |
| 11504 | true, |
| 11505 | OCSPRevocationStatus::UNKNOWN}, |
| 11506 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11507 | // 10 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11508 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11509 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 11510 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11511 | OCSPVerifyResult::INVALID_DATE, |
| 11512 | false, |
| 11513 | OCSPRevocationStatus::UNKNOWN}, |
| 11514 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11515 | // 11 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11516 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11517 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11518 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11519 | OCSPVerifyResult::INVALID_DATE, |
| 11520 | false, |
| 11521 | OCSPRevocationStatus::UNKNOWN}, |
| 11522 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11523 | // 12 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11524 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11525 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11526 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_BEFORE_CERT, |
| 11527 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11528 | false, |
| 11529 | OCSPRevocationStatus::UNKNOWN}, |
| 11530 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11531 | // 13 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11532 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11533 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11534 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 11535 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11536 | false, |
| 11537 | OCSPRevocationStatus::UNKNOWN}, |
| 11538 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11539 | // 14 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11540 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11541 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11542 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 11543 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11544 | false, |
| 11545 | OCSPRevocationStatus::UNKNOWN}, |
| 11546 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11547 | // 15 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11548 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11549 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11550 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11551 | OCSPVerifyResult::PROVIDED, |
| 11552 | true, |
| 11553 | OCSPRevocationStatus::GOOD}, |
| 11554 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11555 | // 16 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11556 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11557 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 11558 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11559 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11560 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11561 | OCSPVerifyResult::PROVIDED, |
| 11562 | true, |
| 11563 | OCSPRevocationStatus::GOOD}, |
| 11564 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11565 | // 17 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11566 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11567 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 11568 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11569 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11570 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11571 | OCSPVerifyResult::PROVIDED, |
| 11572 | true, |
| 11573 | OCSPRevocationStatus::GOOD}, |
| 11574 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11575 | // 18 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11576 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11577 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 11578 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11579 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11580 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11581 | OCSPVerifyResult::PROVIDED, |
| 11582 | true, |
| 11583 | OCSPRevocationStatus::GOOD}, |
| 11584 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11585 | // 19 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11586 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11587 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 11588 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11589 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 11590 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11591 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11592 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11593 | OCSPVerifyResult::INVALID_DATE, |
| 11594 | false, |
| 11595 | OCSPRevocationStatus::UNKNOWN}, |
| 11596 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11597 | // 20 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11598 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11599 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11600 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11601 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11602 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11603 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11604 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11605 | OCSPVerifyResult::PROVIDED, |
| 11606 | true, |
| 11607 | OCSPRevocationStatus::REVOKED}, |
| 11608 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11609 | // 21 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11610 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11611 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11612 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11613 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11614 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11615 | OCSPVerifyResult::PROVIDED, |
| 11616 | true, |
| 11617 | OCSPRevocationStatus::UNKNOWN}, |
| 11618 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11619 | // 22 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11620 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11621 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11622 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11623 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 11624 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11625 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11626 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11627 | OCSPVerifyResult::PROVIDED, |
| 11628 | true, |
| 11629 | OCSPRevocationStatus::UNKNOWN}, |
| 11630 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11631 | // 23 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11632 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 11633 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11634 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11635 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 11636 | false, |
| 11637 | OCSPRevocationStatus::UNKNOWN}, |
| 11638 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11639 | // 24 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11640 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 11641 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11642 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11643 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 11644 | false, |
| 11645 | OCSPRevocationStatus::UNKNOWN}, |
| 11646 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11647 | // These tests fail when using NSS for certificate verification, as NSS fails |
| 11648 | // and doesn't return the partial path. As a result the OCSP checks being done |
| 11649 | // at the CertVerifyProc layer cannot access the issuer certificate. |
| 11650 | #if !defined(USE_NSS_CERTS) |
| 11651 | // 25 |
| 11652 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11653 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11654 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11655 | OCSPVerifyResult::PROVIDED, |
| 11656 | true, |
| 11657 | OCSPRevocationStatus::REVOKED}, |
| 11658 | |
| 11659 | // 26 |
| 11660 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11661 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 11662 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11663 | OCSPVerifyResult::INVALID_DATE, |
| 11664 | false, |
| 11665 | OCSPRevocationStatus::UNKNOWN}, |
| 11666 | |
| 11667 | // 27 |
| 11668 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11669 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11670 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11671 | OCSPVerifyResult::INVALID_DATE, |
| 11672 | false, |
| 11673 | OCSPRevocationStatus::UNKNOWN}, |
| 11674 | #endif |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11675 | }; |
| 11676 | |
| 11677 | class HTTPSOCSPVerifyTest |
| 11678 | : public HTTPSOCSPTest, |
| 11679 | public testing::WithParamInterface<OCSPVerifyTestData> {}; |
| 11680 | |
Christos Froussios | 09e6ac4 | 2019-04-05 14:08:20 | [diff] [blame] | 11681 | // TODO(crbug.com/949958): The test is flaky on Mac |
| 11682 | #if defined(OS_MACOSX) |
| 11683 | #define MAYBE_VerifyResult DISABLED_VerifyResult |
| 11684 | #else |
| 11685 | #define MAYBE_VerifyResult VerifyResult |
| 11686 | #endif |
| 11687 | TEST_P(HTTPSOCSPVerifyTest, MAYBE_VerifyResult) { |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11688 | SpawnedTestServer::SSLOptions ssl_options( |
| 11689 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11690 | OCSPVerifyTestData test = GetParam(); |
| 11691 | |
| 11692 | ssl_options.ocsp_responses = test.ocsp_responses; |
| 11693 | ssl_options.ocsp_produced = test.ocsp_produced; |
| 11694 | ssl_options.staple_ocsp_response = true; |
| 11695 | |
| 11696 | SSLInfo ssl_info; |
| 11697 | OCSPErrorTestDelegate delegate; |
| 11698 | ASSERT_NO_FATAL_FAILURE( |
| 11699 | DoConnectionWithDelegate(ssl_options, &delegate, &ssl_info)); |
| 11700 | |
| 11701 | // The SSLInfo must be extracted from |delegate| on error, due to how |
| 11702 | // URLRequest caches certificate errors. |
| 11703 | if (delegate.have_certificate_errors()) { |
| 11704 | ASSERT_TRUE(delegate.on_ssl_certificate_error_called()); |
| 11705 | ssl_info = delegate.ssl_info(); |
| 11706 | } |
| 11707 | |
| 11708 | EXPECT_EQ(test.response_status, ssl_info.ocsp_result.response_status); |
| 11709 | |
| 11710 | if (test.has_revocation_status) |
| 11711 | EXPECT_EQ(test.cert_status, ssl_info.ocsp_result.revocation_status); |
| 11712 | } |
| 11713 | |
Victor Costan | 8fb98f6f | 2019-02-01 17:08:29 | [diff] [blame] | 11714 | INSTANTIATE_TEST_SUITE_P(OCSPVerify, |
| 11715 | HTTPSOCSPVerifyTest, |
| 11716 | testing::ValuesIn(kOCSPVerifyData)); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11717 | |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11718 | class HTTPSAIATest : public HTTPSOCSPTest { |
| 11719 | public: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11720 | CertVerifier::Config GetCertVerifierConfig() override { |
| 11721 | CertVerifier::Config config; |
| 11722 | return config; |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11723 | } |
| 11724 | }; |
| 11725 | |
| 11726 | TEST_F(HTTPSAIATest, AIAFetching) { |
| 11727 | SpawnedTestServer::SSLOptions ssl_options( |
| 11728 | SpawnedTestServer::SSLOptions::CERT_AUTO_AIA_INTERMEDIATE); |
| 11729 | SpawnedTestServer test_server( |
| 11730 | SpawnedTestServer::TYPE_HTTPS, ssl_options, |
| 11731 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 11732 | ASSERT_TRUE(test_server.Start()); |
| 11733 | |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11734 | // Unmark the certificate's OID as EV, which will disable revocation |
| 11735 | // checking. |
| 11736 | ev_test_policy_.reset(); |
| 11737 | |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11738 | TestDelegate d; |
| 11739 | d.set_allow_certificate_errors(true); |
| 11740 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11741 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 11742 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11743 | |
| 11744 | r->Start(); |
| 11745 | EXPECT_TRUE(r->is_pending()); |
| 11746 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11747 | d.RunUntilComplete(); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11748 | |
| 11749 | EXPECT_EQ(1, d.response_started_count()); |
| 11750 | |
| 11751 | CertStatus cert_status = r->ssl_info().cert_status; |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 11752 | EXPECT_EQ(OK, d.request_status()); |
| 11753 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11754 | ASSERT_TRUE(r->ssl_info().cert); |
| 11755 | EXPECT_EQ(2u, r->ssl_info().cert->intermediate_buffers().size()); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11756 | ASSERT_TRUE(r->ssl_info().unverified_cert); |
Matt Mueller | a419327 | 2017-12-07 00:23:34 | [diff] [blame] | 11757 | EXPECT_EQ(0u, r->ssl_info().unverified_cert->intermediate_buffers().size()); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11758 | } |
| 11759 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11760 | class HTTPSHardFailTest : public HTTPSOCSPTest { |
| 11761 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11762 | CertVerifier::Config GetCertVerifierConfig() override { |
| 11763 | CertVerifier::Config config; |
| 11764 | config.require_rev_checking_local_anchors = true; |
| 11765 | return config; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11766 | } |
| 11767 | }; |
| 11768 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11769 | TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) { |
| 11770 | if (!SystemSupportsOCSP()) { |
| 11771 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11772 | return; |
| 11773 | } |
| 11774 | |
| 11775 | if (!SystemSupportsHardFailRevocationChecking()) { |
| 11776 | LOG(WARNING) << "Skipping test because system doesn't support hard fail " |
| 11777 | << "revocation checking"; |
| 11778 | return; |
| 11779 | } |
| 11780 | |
| 11781 | SpawnedTestServer::SSLOptions ssl_options( |
| 11782 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11783 | ssl_options.ocsp_status = |
| 11784 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11785 | |
| 11786 | CertStatus cert_status; |
| 11787 | DoConnection(ssl_options, &cert_status); |
| 11788 | |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11789 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 11790 | // TODO(crbug.com/649017): Should we consider invalid response as |
| 11791 | // affirmatively revoked? |
| 11792 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 11793 | cert_status & CERT_STATUS_UNABLE_TO_CHECK_REVOCATION); |
| 11794 | #else |
| 11795 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_REVOKED); |
| 11796 | #endif |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11797 | |
| 11798 | // Without a positive OCSP response, we shouldn't show the EV status. |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11799 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11800 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11801 | } |
| 11802 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11803 | class HTTPSEVCRLSetTest : public HTTPSOCSPTest { |
| 11804 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11805 | CertVerifier::Config GetCertVerifierConfig() override { |
| 11806 | CertVerifier::Config config; |
| 11807 | return config; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11808 | } |
| 11809 | }; |
| 11810 | |
| 11811 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) { |
| 11812 | if (!SystemSupportsOCSP()) { |
| 11813 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11814 | return; |
| 11815 | } |
| 11816 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11817 | SpawnedTestServer::SSLOptions ssl_options( |
| 11818 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11819 | ssl_options.ocsp_status = |
| 11820 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11821 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11822 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11823 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11824 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11825 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11826 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11827 | |
| 11828 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11829 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11830 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11831 | } |
| 11832 | |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11833 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) { |
| 11834 | if (!SystemSupportsOCSP()) { |
| 11835 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11836 | return; |
| 11837 | } |
| 11838 | |
| 11839 | SpawnedTestServer::SSLOptions ssl_options( |
| 11840 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11841 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11842 | |
| 11843 | CertStatus cert_status; |
| 11844 | DoConnection(ssl_options, &cert_status); |
| 11845 | |
mattm | 1a282f5 | 2016-11-10 21:49:42 | [diff] [blame] | 11846 | // Currently only works for Windows and OS X. When using NSS, it's not |
| 11847 | // possible to determine whether the check failed because of actual |
| 11848 | // revocation or because there was an OCSP failure. |
| 11849 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11850 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11851 | #else |
| 11852 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11853 | #endif |
| 11854 | |
| 11855 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11856 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11857 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 11858 | } |
| 11859 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11860 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) { |
| 11861 | if (!SystemSupportsOCSP()) { |
| 11862 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11863 | return; |
| 11864 | } |
| 11865 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11866 | SpawnedTestServer::SSLOptions ssl_options( |
| 11867 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11868 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11869 | |
| 11870 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11871 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11872 | |
| 11873 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11874 | |
| 11875 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11876 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11877 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11878 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11879 | } |
| 11880 | |
| 11881 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) { |
| 11882 | if (!SystemSupportsOCSP()) { |
| 11883 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11884 | return; |
| 11885 | } |
| 11886 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11887 | SpawnedTestServer::SSLOptions ssl_options( |
| 11888 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11889 | ssl_options.ocsp_status = |
| 11890 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11891 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11892 | cert_verifier_config.crl_set = CRLSet::ExpiredCRLSetForTesting(); |
| 11893 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11894 | |
| 11895 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11896 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11897 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11898 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11899 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11900 | |
| 11901 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11902 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11903 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11904 | } |
| 11905 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11906 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) { |
| 11907 | if (!SystemSupportsOCSP()) { |
| 11908 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11909 | return; |
| 11910 | } |
| 11911 | |
| 11912 | SpawnedTestServer::SSLOptions ssl_options( |
| 11913 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11914 | ssl_options.ocsp_status = |
| 11915 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11916 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11917 | cert_verifier_config.crl_set = |
| 11918 | CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "", "", {}); |
| 11919 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11920 | |
| 11921 | CertStatus cert_status; |
| 11922 | DoConnection(ssl_options, &cert_status); |
| 11923 | |
| 11924 | // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a |
| 11925 | // revocation check for EV. |
| 11926 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11927 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11928 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11929 | EXPECT_FALSE( |
| 11930 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 11931 | } |
| 11932 | |
| 11933 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) { |
| 11934 | if (!SystemSupportsOCSP()) { |
| 11935 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11936 | return; |
| 11937 | } |
| 11938 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11939 | SpawnedTestServer::SSLOptions ssl_options( |
| 11940 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11941 | ssl_options.ocsp_status = |
| 11942 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11943 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11944 | cert_verifier_config.crl_set = CRLSet::EmptyCRLSetForTesting(); |
| 11945 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11946 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11947 | CertStatus cert_status = 0; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11948 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11949 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11950 | // Even with a fresh CRLSet, we should still do online revocation checks when |
| 11951 | // the certificate chain isn't covered by the CRLSet, which it isn't in this |
| 11952 | // test. |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11953 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11954 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11955 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11956 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11957 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11958 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11959 | } |
| 11960 | |
| 11961 | class HTTPSCRLSetTest : public HTTPSOCSPTest { |
| 11962 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11963 | CertVerifier::Config GetCertVerifierConfig() override { |
| 11964 | CertVerifier::Config config; |
| 11965 | return config; |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11966 | } |
| 11967 | |
| 11968 | void SetUp() override { |
| 11969 | HTTPSOCSPTest::SetUp(); |
| 11970 | |
| 11971 | // Unmark the certificate's OID as EV, which should disable revocation |
| 11972 | // checking (as per the user preference). |
| 11973 | ev_test_policy_.reset(); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11974 | } |
| 11975 | }; |
| 11976 | |
| 11977 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11978 | SpawnedTestServer::SSLOptions ssl_options( |
| 11979 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11980 | ssl_options.ocsp_status = |
| 11981 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11982 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11983 | cert_verifier_config.crl_set = CRLSet::ExpiredCRLSetForTesting(); |
| 11984 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11985 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11986 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11987 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11988 | |
| 11989 | // If we're not trying EV verification then, even if the CRLSet has expired, |
| 11990 | // we don't fall back to online revocation checks. |
| 11991 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11992 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11993 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11994 | } |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11995 | |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11996 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSetAndRevoked) { |
| 11997 | // Test that when online revocation checking is disabled, and the leaf |
| 11998 | // certificate is not EV, that no revocation checking actually happens. |
| 11999 | if (!SystemSupportsOCSP()) { |
| 12000 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12001 | return; |
| 12002 | } |
| 12003 | |
| 12004 | SpawnedTestServer::SSLOptions ssl_options( |
| 12005 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12006 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12007 | |
| 12008 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12009 | cert_verifier_config.crl_set = CRLSet::ExpiredCRLSetForTesting(); |
| 12010 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12011 | |
| 12012 | CertStatus cert_status; |
| 12013 | DoConnection(ssl_options, &cert_status); |
| 12014 | |
| 12015 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12016 | |
| 12017 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 12018 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 12019 | } |
| 12020 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12021 | TEST_F(HTTPSCRLSetTest, CRLSetRevoked) { |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 12022 | #if defined(OS_ANDROID) |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12023 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 12024 | return; |
| 12025 | #endif |
| 12026 | |
| 12027 | SpawnedTestServer::SSLOptions ssl_options( |
| 12028 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12029 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 12030 | ssl_options.cert_serial = 10; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12031 | |
| 12032 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12033 | cert_verifier_config.crl_set = |
| 12034 | CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "\x0a", "", {}); |
| 12035 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12036 | |
| 12037 | CertStatus cert_status = 0; |
| 12038 | DoConnection(ssl_options, &cert_status); |
| 12039 | |
| 12040 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 12041 | // reflected without online revocation checking. |
| 12042 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12043 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12044 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12045 | } |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 12046 | |
| 12047 | TEST_F(HTTPSCRLSetTest, CRLSetRevokedBySubject) { |
| 12048 | #if defined(OS_ANDROID) |
| 12049 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 12050 | return; |
| 12051 | #endif |
| 12052 | |
| 12053 | SpawnedTestServer::SSLOptions ssl_options( |
| 12054 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12055 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 12056 | static const char kCommonName[] = "Test CN"; |
| 12057 | ssl_options.cert_common_name = kCommonName; |
| 12058 | |
| 12059 | { |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12060 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12061 | cert_verifier_config.crl_set = |
| 12062 | CRLSet::ForTesting(false, nullptr, "", kCommonName, {}); |
| 12063 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 12064 | |
| 12065 | CertStatus cert_status = 0; |
| 12066 | DoConnection(ssl_options, &cert_status); |
| 12067 | |
| 12068 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 12069 | // reflected without online revocation checking. |
| 12070 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12071 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12072 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 12073 | } |
| 12074 | |
| 12075 | const uint8_t kTestServerSPKISHA256[32] = { |
| 12076 | 0xb3, 0x91, 0xac, 0x73, 0x32, 0x54, 0x7f, 0x7b, 0x8a, 0x62, 0x77, |
| 12077 | 0x73, 0x1d, 0x45, 0x7b, 0x23, 0x46, 0x69, 0xef, 0x6f, 0x05, 0x3d, |
| 12078 | 0x07, 0x22, 0x15, 0x18, 0xd6, 0x10, 0x8b, 0xa1, 0x49, 0x33, |
| 12079 | }; |
| 12080 | const std::string spki_hash( |
| 12081 | reinterpret_cast<const char*>(kTestServerSPKISHA256), |
| 12082 | sizeof(kTestServerSPKISHA256)); |
| 12083 | |
| 12084 | { |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12085 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12086 | cert_verifier_config.crl_set = |
| 12087 | CRLSet::ForTesting(false, nullptr, "", kCommonName, {spki_hash}); |
| 12088 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 12089 | |
| 12090 | CertStatus cert_status = 0; |
| 12091 | DoConnection(ssl_options, &cert_status); |
| 12092 | |
| 12093 | // When the correct SPKI hash is specified, the connection should succeed |
| 12094 | // even though the subject is listed in the CRLSet. |
| 12095 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12096 | } |
| 12097 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 12098 | #endif // !defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 12099 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 12100 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 12101 | !defined(OS_FUCHSIA) |
| 12102 | // FTP uses a second TCP connection with the port number allocated dynamically |
| 12103 | // on the server side, so it would be hard to make RemoteTestServer proxy FTP |
| 12104 | // connections reliably. FTP tests are disabled on platforms that use |
| 12105 | // RemoteTestServer. See http://crbug.com/495220 |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 12106 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12107 | public: |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 12108 | URLRequestTestFTP() |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 12109 | : ftp_test_server_(SpawnedTestServer::TYPE_FTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12110 | base::FilePath(kTestFilePath)) { |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12111 | // Can't use |default_context_|'s HostResolver to set up the |
| 12112 | // FTPTransactionFactory because it hasn't been created yet. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12113 | default_context().set_host_resolver(&host_resolver_); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12114 | } |
| 12115 | |
| 12116 | // URLRequestTest interface: |
| 12117 | void SetUpFactory() override { |
| 12118 | // Add FTP support to the default URLRequestContext. |
| 12119 | job_factory_impl_->SetProtocolHandler( |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 12120 | "ftp", FtpProtocolHandler::Create(&host_resolver_)); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12121 | } |
| 12122 | |
| 12123 | std::string GetTestFileContents() { |
| 12124 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 12125 | EXPECT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &path)); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12126 | path = path.Append(kTestFilePath); |
| 12127 | path = path.AppendASCII(kFtpTestFile); |
| 12128 | std::string contents; |
| 12129 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
| 12130 | return contents; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12131 | } |
| 12132 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 12133 | protected: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12134 | MockHostResolver host_resolver_; |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12135 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12136 | SpawnedTestServer ftp_test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 12137 | }; |
| 12138 | |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12139 | // Make sure an FTP request using an unsafe ports fails. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 12140 | TEST_F(URLRequestTestFTP, UnsafePort) { |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12141 | GURL url("ftp://127.0.0.1:7"); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12142 | |
| 12143 | TestDelegate d; |
| 12144 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12145 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12146 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12147 | r->Start(); |
| 12148 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12149 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12150 | d.RunUntilComplete(); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12151 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12152 | EXPECT_FALSE(r->is_pending()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 12153 | EXPECT_EQ(ERR_UNSAFE_PORT, d.request_status()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12154 | } |
| 12155 | } |
| 12156 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12157 | TEST_F(URLRequestTestFTP, FTPDirectoryListing) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12158 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12159 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12160 | TestDelegate d; |
| 12161 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12162 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12163 | ftp_test_server_.GetURL("/"), DEFAULT_PRIORITY, &d, |
| 12164 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12165 | r->Start(); |
| 12166 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12167 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12168 | d.RunUntilComplete(); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12169 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12170 | EXPECT_FALSE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12171 | EXPECT_EQ(1, d.response_started_count()); |
| 12172 | EXPECT_FALSE(d.received_data_before_response()); |
| 12173 | EXPECT_LT(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12174 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12175 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12176 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12177 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12178 | } |
| 12179 | } |
| 12180 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12181 | TEST_F(URLRequestTestFTP, FTPGetTestAnonymous) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12182 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12183 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12184 | TestDelegate d; |
| 12185 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12186 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12187 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, &d, |
| 12188 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12189 | r->Start(); |
| 12190 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12191 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12192 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12193 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12194 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12195 | EXPECT_EQ(1, d.response_started_count()); |
| 12196 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12197 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12198 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12199 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12200 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12201 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12202 | } |
| 12203 | } |
| 12204 | |
Mike West | 80462a1 | 2018-11-27 16:05:06 | [diff] [blame] | 12205 | TEST_F(URLRequestTestFTP, FTPMimeType) { |
| 12206 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 12207 | |
| 12208 | struct { |
| 12209 | const char* path; |
| 12210 | const char* mime; |
| 12211 | } test_cases[] = { |
| 12212 | {"/", "text/vnd.chromium.ftp-dir"}, |
| 12213 | {kFtpTestFile, "application/octet-stream"}, |
| 12214 | }; |
| 12215 | |
| 12216 | for (const auto test : test_cases) { |
| 12217 | TestDelegate d; |
| 12218 | |
| 12219 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 12220 | ftp_test_server_.GetURL(test.path), DEFAULT_PRIORITY, &d, |
| 12221 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12222 | r->Start(); |
| 12223 | EXPECT_TRUE(r->is_pending()); |
| 12224 | |
| 12225 | d.RunUntilComplete(); |
| 12226 | |
| 12227 | std::string mime; |
| 12228 | r->GetMimeType(&mime); |
| 12229 | EXPECT_EQ(test.mime, mime); |
| 12230 | } |
| 12231 | } |
| 12232 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12233 | TEST_F(URLRequestTestFTP, FTPGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12234 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12235 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12236 | TestDelegate d; |
| 12237 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12238 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12239 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12240 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12241 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12242 | r->Start(); |
| 12243 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12244 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12245 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12246 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12247 | EXPECT_FALSE(r->is_pending()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12248 | EXPECT_EQ(1, d.response_started_count()); |
| 12249 | EXPECT_FALSE(d.received_data_before_response()); |
| 12250 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12251 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12252 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12253 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12254 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 12255 | |
| 12256 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12257 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 12258 | TestLoadTimingNoHttpResponse(load_timing_info); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12259 | } |
| 12260 | } |
| 12261 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12262 | TEST_F(URLRequestTestFTP, FTPCheckWrongPassword) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12263 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12264 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12265 | TestDelegate d; |
| 12266 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12267 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12268 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12269 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12270 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12271 | r->Start(); |
| 12272 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12273 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12274 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12275 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12276 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12277 | EXPECT_EQ(1, d.response_started_count()); |
| 12278 | EXPECT_FALSE(d.received_data_before_response()); |
| 12279 | EXPECT_EQ(d.bytes_received(), 0); |
| 12280 | } |
| 12281 | } |
| 12282 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12283 | TEST_F(URLRequestTestFTP, FTPCheckWrongPasswordRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12284 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12285 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12286 | TestDelegate d; |
| 12287 | // Set correct login credentials. The delegate will be asked for them when |
| 12288 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 12289 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12290 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12291 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12292 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12293 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12294 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12295 | r->Start(); |
| 12296 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12297 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12298 | d.RunUntilComplete(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12299 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12300 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12301 | EXPECT_EQ(1, d.response_started_count()); |
| 12302 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12303 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12304 | } |
| 12305 | } |
| 12306 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12307 | TEST_F(URLRequestTestFTP, FTPCheckWrongUser) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12308 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12309 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12310 | TestDelegate d; |
| 12311 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12312 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12313 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 12314 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12315 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12316 | r->Start(); |
| 12317 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12318 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12319 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12320 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12321 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12322 | EXPECT_EQ(1, d.response_started_count()); |
| 12323 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12324 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12325 | } |
| 12326 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12327 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12328 | TEST_F(URLRequestTestFTP, FTPCheckWrongUserRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12329 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12330 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12331 | TestDelegate d; |
| 12332 | // Set correct login credentials. The delegate will be asked for them when |
| 12333 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 12334 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12335 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12336 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12337 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 12338 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12339 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12340 | r->Start(); |
| 12341 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12342 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12343 | d.RunUntilComplete(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12344 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12345 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12346 | EXPECT_EQ(1, d.response_started_count()); |
| 12347 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12348 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12349 | } |
| 12350 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12351 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12352 | TEST_F(URLRequestTestFTP, FTPCacheURLCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12353 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12354 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12355 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12356 | { |
| 12357 | // Pass correct login identity in the URL. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12358 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12359 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12360 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12361 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12362 | r->Start(); |
| 12363 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12364 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12365 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12366 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12367 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12368 | EXPECT_EQ(1, d->response_started_count()); |
| 12369 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12370 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12371 | } |
| 12372 | |
| 12373 | d.reset(new TestDelegate); |
| 12374 | { |
| 12375 | // This request should use cached identity from previous request. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12376 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12377 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 12378 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12379 | r->Start(); |
| 12380 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12381 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12382 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12383 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12384 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12385 | EXPECT_EQ(1, d->response_started_count()); |
| 12386 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12387 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12388 | } |
| 12389 | } |
| 12390 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12391 | TEST_F(URLRequestTestFTP, FTPCacheLoginBoxCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12392 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12393 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12394 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12395 | // Set correct login credentials. The delegate will be asked for them when |
| 12396 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 12397 | d->set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12398 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12399 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12400 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12401 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12402 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12403 | r->Start(); |
| 12404 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12405 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12406 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12407 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12408 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12409 | EXPECT_EQ(1, d->response_started_count()); |
| 12410 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12411 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12412 | } |
| 12413 | |
| 12414 | // Use a new delegate without explicit credentials. The cached ones should be |
| 12415 | // used. |
| 12416 | d.reset(new TestDelegate); |
| 12417 | { |
| 12418 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 12419 | // ones. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12420 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12421 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 12422 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12423 | r->Start(); |
| 12424 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12425 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12426 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12427 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12428 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12429 | EXPECT_EQ(1, d->response_started_count()); |
| 12430 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12431 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12432 | } |
| 12433 | } |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 12434 | |
| 12435 | TEST_F(URLRequestTestFTP, RawBodyBytes) { |
| 12436 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 12437 | |
| 12438 | TestDelegate d; |
| 12439 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12440 | ftp_test_server_.GetURL("simple.html"), DEFAULT_PRIORITY, &d, |
| 12441 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 12442 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12443 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 12444 | |
| 12445 | EXPECT_EQ(6, req->GetRawBodyBytes()); |
| 12446 | } |
| 12447 | |
Matt Menke | f1b97e0 | 2019-05-13 23:52:41 | [diff] [blame^] | 12448 | class URLRequestTestFTPOverHttpProxy : public URLRequestTestFTP { |
| 12449 | public: |
| 12450 | // Test interface: |
| 12451 | void SetUp() override { |
| 12452 | proxy_resolution_service_ = ProxyResolutionService::CreateFixed( |
| 12453 | "localhost", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 12454 | default_context_->set_proxy_resolution_service( |
| 12455 | proxy_resolution_service_.get()); |
| 12456 | URLRequestTestFTP::SetUp(); |
| 12457 | } |
| 12458 | |
| 12459 | private: |
| 12460 | std::unique_ptr<ProxyResolutionService> proxy_resolution_service_; |
| 12461 | }; |
| 12462 | |
| 12463 | // Check that FTP is not supported over an HTTP proxy. |
| 12464 | TEST_F(URLRequestTestFTPOverHttpProxy, Fails) { |
| 12465 | TestDelegate delegate; |
| 12466 | std::unique_ptr<URLRequest> request( |
| 12467 | default_context_->CreateRequest(GURL("ftp://foo.test/"), DEFAULT_PRIORITY, |
| 12468 | &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12469 | request->Start(); |
| 12470 | delegate.RunUntilComplete(); |
| 12471 | |
| 12472 | EXPECT_THAT(delegate.request_status(), IsError(ERR_NO_SUPPORTED_PROXIES)); |
| 12473 | } |
| 12474 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 12475 | #endif // !BUILDFLAG(DISABLE_FTP_SUPPORT) |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 12476 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12477 | TEST_F(URLRequestTest, NetworkAccessedClearOnDataRequest) { |
| 12478 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12479 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12480 | GURL("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12481 | |
| 12482 | EXPECT_FALSE(req->response_info().network_accessed); |
| 12483 | |
| 12484 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12485 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12486 | |
| 12487 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 12488 | EXPECT_FALSE(req->response_info().network_accessed); |
| 12489 | } |
| 12490 | |
| 12491 | TEST_F(URLRequestTest, NetworkAccessedSetOnHostResolutionFailure) { |
| 12492 | MockHostResolver host_resolver; |
| 12493 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 12494 | TestURLRequestContext context(true); |
| 12495 | context.set_network_delegate(&network_delegate); |
| 12496 | context.set_host_resolver(&host_resolver); |
| 12497 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 12498 | context.Init(); |
| 12499 | |
| 12500 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12501 | std::unique_ptr<URLRequest> req( |
| 12502 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 12503 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12504 | |
| 12505 | EXPECT_FALSE(req->response_info().network_accessed); |
| 12506 | |
| 12507 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12508 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12509 | EXPECT_TRUE(req->response_info().network_accessed); |
| 12510 | } |
| 12511 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 12512 | // Test that URLRequest is canceled correctly. |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12513 | // See http://crbug.com/508900 |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 12514 | TEST_F(URLRequestTest, URLRequestRedirectJobCancelRequest) { |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12515 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12516 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12517 | GURL("http://not-a-real-domain/"), DEFAULT_PRIORITY, &d, |
| 12518 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12519 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12520 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12521 | req.get(), &default_network_delegate_, |
| 12522 | GURL("http://this-should-never-be-navigated-to/"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 12523 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Jumbo shrimp")); |
| 12524 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12525 | |
| 12526 | req->Start(); |
| 12527 | req->Cancel(); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12528 | base::RunLoop().RunUntilIdle(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 12529 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12530 | EXPECT_EQ(0, d.received_redirect_count()); |
| 12531 | } |
| 12532 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12533 | TEST_F(URLRequestTestHTTP, HeadersCallbacks) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12534 | ASSERT_TRUE(http_test_server()->Start()); |
| 12535 | TestURLRequestContext context; |
| 12536 | GURL url(http_test_server()->GetURL("/cachetime")); |
| 12537 | TestDelegate delegate; |
| 12538 | HttpRequestHeaders extra_headers; |
| 12539 | extra_headers.SetHeader("X-Foo", "bar"); |
| 12540 | |
| 12541 | { |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12542 | HttpRawRequestHeaders raw_req_headers; |
| 12543 | scoped_refptr<const HttpResponseHeaders> raw_resp_headers; |
| 12544 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12545 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12546 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12547 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12548 | r->SetRequestHeadersCallback(base::Bind( |
| 12549 | &HttpRawRequestHeaders::Assign, base::Unretained(&raw_req_headers))); |
| 12550 | r->SetResponseHeadersCallback(base::Bind( |
| 12551 | [](scoped_refptr<const HttpResponseHeaders>* left, |
| 12552 | scoped_refptr<const HttpResponseHeaders> right) { *left = right; }, |
| 12553 | base::Unretained(&raw_resp_headers))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12554 | r->Start(); |
| 12555 | while (!delegate.response_started_count()) |
| 12556 | base::RunLoop().RunUntilIdle(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12557 | EXPECT_FALSE(raw_req_headers.headers().empty()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12558 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12559 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12560 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12561 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12562 | EXPECT_EQ("gzip, deflate", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12563 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Connection", &value)); |
| 12564 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Host", &value)); |
| 12565 | EXPECT_EQ("GET /cachetime HTTP/1.1\r\n", raw_req_headers.request_line()); |
| 12566 | EXPECT_EQ(raw_resp_headers.get(), r->response_headers()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12567 | } |
| 12568 | { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12569 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12570 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12571 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12572 | r->SetRequestHeadersCallback(base::Bind([](HttpRawRequestHeaders) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12573 | FAIL() << "Callback should not be called unless request is sent"; |
| 12574 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12575 | r->SetResponseHeadersCallback( |
| 12576 | base::Bind([](scoped_refptr<const HttpResponseHeaders>) { |
| 12577 | FAIL() << "Callback should not be called unless request is sent"; |
| 12578 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12579 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12580 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12581 | EXPECT_TRUE(r->was_cached()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12582 | } |
| 12583 | } |
| 12584 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12585 | TEST_F(URLRequestTestHTTP, HeadersCallbacksWithRedirect) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12586 | ASSERT_TRUE(http_test_server()->Start()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12587 | HttpRawRequestHeaders raw_req_headers; |
| 12588 | scoped_refptr<const HttpResponseHeaders> raw_resp_headers; |
| 12589 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12590 | TestURLRequestContext context; |
| 12591 | TestDelegate delegate; |
| 12592 | HttpRequestHeaders extra_headers; |
| 12593 | extra_headers.SetHeader("X-Foo", "bar"); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12594 | GURL url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12595 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12596 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12597 | r->SetExtraRequestHeaders(extra_headers); |
| 12598 | r->SetRequestHeadersCallback(base::Bind(&HttpRawRequestHeaders::Assign, |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12599 | base::Unretained(&raw_req_headers))); |
| 12600 | r->SetResponseHeadersCallback(base::Bind( |
| 12601 | [](scoped_refptr<const HttpResponseHeaders>* left, |
| 12602 | scoped_refptr<const HttpResponseHeaders> right) { *left = right; }, |
| 12603 | base::Unretained(&raw_resp_headers))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12604 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 12605 | delegate.RunUntilRedirect(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12606 | |
| 12607 | ASSERT_EQ(1, delegate.received_redirect_count()); |
| 12608 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12609 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12610 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12611 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12612 | EXPECT_EQ("gzip, deflate", value); |
| 12613 | EXPECT_EQ(1, delegate.received_redirect_count()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12614 | EXPECT_EQ("GET /redirect-test.html HTTP/1.1\r\n", |
| 12615 | raw_req_headers.request_line()); |
| 12616 | EXPECT_TRUE(raw_resp_headers->HasHeader("Location")); |
| 12617 | EXPECT_EQ(302, raw_resp_headers->response_code()); |
| 12618 | EXPECT_EQ("Redirect", raw_resp_headers->GetStatusText()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12619 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12620 | raw_req_headers = HttpRawRequestHeaders(); |
| 12621 | raw_resp_headers = nullptr; |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 12622 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 12623 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12624 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12625 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12626 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12627 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12628 | EXPECT_EQ("gzip, deflate", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12629 | EXPECT_EQ("GET /with-headers.html HTTP/1.1\r\n", |
| 12630 | raw_req_headers.request_line()); |
| 12631 | EXPECT_EQ(r->response_headers(), raw_resp_headers.get()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12632 | } |
| 12633 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12634 | TEST_F(URLRequestTest, HeadersCallbacksConnectFailed) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12635 | TestDelegate request_delegate; |
| 12636 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12637 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12638 | GURL("http://127.0.0.1:9/"), DEFAULT_PRIORITY, &request_delegate, |
| 12639 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12640 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
| 12641 | FAIL() << "Callback should not be called unless request is sent"; |
| 12642 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12643 | r->SetResponseHeadersCallback( |
| 12644 | base::Bind([](scoped_refptr<const net::HttpResponseHeaders>) { |
| 12645 | FAIL() << "Callback should not be called unless request is sent"; |
| 12646 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12647 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12648 | request_delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12649 | EXPECT_FALSE(r->is_pending()); |
| 12650 | } |
| 12651 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12652 | TEST_F(URLRequestTestHTTP, HeadersCallbacksAuthRetry) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12653 | ASSERT_TRUE(http_test_server()->Start()); |
| 12654 | GURL url(http_test_server()->GetURL("/auth-basic")); |
| 12655 | |
| 12656 | TestURLRequestContext context; |
| 12657 | TestDelegate delegate; |
| 12658 | |
| 12659 | delegate.set_credentials(AuthCredentials(kUser, kSecret)); |
| 12660 | HttpRequestHeaders extra_headers; |
| 12661 | extra_headers.SetHeader("X-Foo", "bar"); |
| 12662 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12663 | using ReqHeadersVector = std::vector<std::unique_ptr<HttpRawRequestHeaders>>; |
| 12664 | ReqHeadersVector raw_req_headers; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12665 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12666 | using RespHeadersVector = |
| 12667 | std::vector<scoped_refptr<const HttpResponseHeaders>>; |
| 12668 | RespHeadersVector raw_resp_headers; |
| 12669 | |
| 12670 | auto req_headers_callback = base::Bind( |
| 12671 | [](ReqHeadersVector* vec, HttpRawRequestHeaders headers) { |
| 12672 | vec->emplace_back(new HttpRawRequestHeaders(std::move(headers))); |
| 12673 | }, |
| 12674 | &raw_req_headers); |
| 12675 | auto resp_headers_callback = base::Bind( |
| 12676 | [](RespHeadersVector* vec, |
| 12677 | scoped_refptr<const HttpResponseHeaders> headers) { |
| 12678 | vec->push_back(headers); |
| 12679 | }, |
| 12680 | &raw_resp_headers); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12681 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12682 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12683 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12684 | r->SetRequestHeadersCallback(req_headers_callback); |
| 12685 | r->SetResponseHeadersCallback(resp_headers_callback); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12686 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12687 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12688 | EXPECT_FALSE(r->is_pending()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12689 | ASSERT_EQ(raw_req_headers.size(), 2u); |
| 12690 | ASSERT_EQ(raw_resp_headers.size(), 2u); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12691 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12692 | EXPECT_FALSE(raw_req_headers[0]->FindHeaderForTest("Authorization", &value)); |
| 12693 | EXPECT_TRUE(raw_req_headers[0]->FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12694 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12695 | EXPECT_TRUE(raw_req_headers[1]->FindHeaderForTest("Authorization", &value)); |
| 12696 | EXPECT_TRUE(raw_req_headers[1]->FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12697 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12698 | EXPECT_EQ(raw_resp_headers[1], r->response_headers()); |
| 12699 | EXPECT_NE(raw_resp_headers[0], raw_resp_headers[1]); |
| 12700 | EXPECT_EQ(401, raw_resp_headers[0]->response_code()); |
| 12701 | EXPECT_EQ("Unauthorized", raw_resp_headers[0]->GetStatusText()); |
| 12702 | |
| 12703 | std::unique_ptr<URLRequest> r2(context.CreateRequest( |
| 12704 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12705 | r2->SetExtraRequestHeaders(extra_headers); |
| 12706 | r2->SetRequestHeadersCallback(req_headers_callback); |
| 12707 | r2->SetResponseHeadersCallback(resp_headers_callback); |
| 12708 | r2->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 12709 | r2->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12710 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12711 | EXPECT_FALSE(r2->is_pending()); |
| 12712 | ASSERT_EQ(raw_req_headers.size(), 3u); |
| 12713 | ASSERT_EQ(raw_resp_headers.size(), 3u); |
| 12714 | EXPECT_TRUE(raw_req_headers[2]->FindHeaderForTest("If-None-Match", &value)); |
| 12715 | EXPECT_NE(raw_resp_headers[2].get(), r2->response_headers()); |
| 12716 | EXPECT_EQ(304, raw_resp_headers[2]->response_code()); |
| 12717 | EXPECT_EQ("Not Modified", raw_resp_headers[2]->GetStatusText()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12718 | } |
| 12719 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12720 | TEST_F(URLRequestTest, HeadersCallbacksNonHTTP) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12721 | GURL data_url("data:text/html,<html><body>Hello!</body></html>"); |
| 12722 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12723 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12724 | data_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12725 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12726 | FAIL() << "Callback should not be called for non-HTTP schemes"; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12727 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12728 | r->SetResponseHeadersCallback( |
| 12729 | base::Bind([](scoped_refptr<const net::HttpResponseHeaders>) { |
| 12730 | FAIL() << "Callback should not be called for non-HTTP schemes"; |
| 12731 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12732 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12733 | d.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12734 | EXPECT_FALSE(r->is_pending()); |
| 12735 | } |
| 12736 | |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12737 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSet) { |
| 12738 | TestDelegate d; |
| 12739 | BlockingNetworkDelegate network_delegate( |
| 12740 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 12741 | const GURL kOriginalUrl("https://original.test"); |
| 12742 | const GURL kRedirectUrl("http://redirect.test"); |
| 12743 | network_delegate.set_redirect_url(kRedirectUrl); |
| 12744 | TestURLRequestContext context(true /* delay_initialization */); |
| 12745 | context.set_network_delegate(&network_delegate); |
| 12746 | context.Init(); |
| 12747 | |
| 12748 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12749 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12750 | r->set_upgrade_if_insecure(true); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12751 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 12752 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12753 | GURL::Replacements replacements; |
| 12754 | // Check that the redirect URL was upgraded to HTTPS since upgrade_if_insecure |
| 12755 | // was set. |
| 12756 | replacements.SetSchemeStr("https"); |
| 12757 | EXPECT_EQ(kRedirectUrl.ReplaceComponents(replacements), |
| 12758 | d.redirect_info().new_url); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 12759 | EXPECT_TRUE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12760 | } |
| 12761 | |
| 12762 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSetExplicitPort80) { |
| 12763 | TestDelegate d; |
| 12764 | BlockingNetworkDelegate network_delegate( |
| 12765 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 12766 | const GURL kOriginalUrl("https://original.test"); |
| 12767 | const GURL kRedirectUrl("http://redirect.test:80"); |
| 12768 | network_delegate.set_redirect_url(kRedirectUrl); |
| 12769 | TestURLRequestContext context(true /* delay_initialization */); |
| 12770 | context.set_network_delegate(&network_delegate); |
| 12771 | context.Init(); |
| 12772 | |
| 12773 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12774 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12775 | r->set_upgrade_if_insecure(true); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12776 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 12777 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12778 | GURL::Replacements replacements; |
| 12779 | // The URL host should have not been changed. |
| 12780 | EXPECT_EQ(d.redirect_info().new_url.host(), kRedirectUrl.host()); |
| 12781 | // The scheme should now be https, and the effective port should now be 443. |
| 12782 | EXPECT_TRUE(d.redirect_info().new_url.SchemeIs("https")); |
| 12783 | EXPECT_EQ(d.redirect_info().new_url.EffectiveIntPort(), 443); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 12784 | EXPECT_TRUE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12785 | } |
| 12786 | |
| 12787 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSetNonStandardPort) { |
| 12788 | TestDelegate d; |
| 12789 | BlockingNetworkDelegate network_delegate( |
| 12790 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 12791 | const GURL kOriginalUrl("https://original.test"); |
| 12792 | const GURL kRedirectUrl("http://redirect.test:1234"); |
| 12793 | network_delegate.set_redirect_url(kRedirectUrl); |
| 12794 | TestURLRequestContext context(true /* delay_initialization */); |
| 12795 | context.set_network_delegate(&network_delegate); |
| 12796 | context.Init(); |
| 12797 | |
| 12798 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12799 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12800 | r->set_upgrade_if_insecure(true); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12801 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 12802 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12803 | GURL::Replacements replacements; |
| 12804 | // Check that the redirect URL was upgraded to HTTPS since upgrade_if_insecure |
| 12805 | // was set, nonstandard port should not have been modified. |
| 12806 | replacements.SetSchemeStr("https"); |
| 12807 | EXPECT_EQ(kRedirectUrl.ReplaceComponents(replacements), |
| 12808 | d.redirect_info().new_url); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 12809 | EXPECT_TRUE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12810 | } |
| 12811 | |
| 12812 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagNotSet) { |
| 12813 | TestDelegate d; |
| 12814 | BlockingNetworkDelegate network_delegate( |
| 12815 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 12816 | const GURL kOriginalUrl("https://original.test"); |
| 12817 | const GURL kRedirectUrl("http://redirect.test"); |
| 12818 | network_delegate.set_redirect_url(kRedirectUrl); |
| 12819 | TestURLRequestContext context(true /* delay_initialization */); |
| 12820 | context.set_network_delegate(&network_delegate); |
| 12821 | context.Init(); |
| 12822 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12823 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12824 | r->set_upgrade_if_insecure(false); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12825 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 12826 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12827 | // The redirect URL should not be changed if the upgrade_if_insecure flag is |
| 12828 | // not set. |
| 12829 | EXPECT_EQ(kRedirectUrl, d.redirect_info().new_url); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 12830 | EXPECT_FALSE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12831 | } |
| 12832 | |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12833 | // Test that URLRequests get properly tagged. |
| 12834 | #if defined(OS_ANDROID) |
| 12835 | TEST_F(URLRequestTestHTTP, TestTagging) { |
Peter Collingbourne | 8ccab72 | 2019-02-12 18:10:48 | [diff] [blame] | 12836 | if (!CanGetTaggedBytes()) { |
| 12837 | DVLOG(0) << "Skipping test - GetTaggedBytes unsupported."; |
| 12838 | return; |
| 12839 | } |
| 12840 | |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12841 | ASSERT_TRUE(http_test_server()->Start()); |
| 12842 | |
| 12843 | // The tag under which the system reports untagged traffic. |
| 12844 | static const int32_t UNTAGGED_TAG = 0; |
| 12845 | |
| 12846 | uint64_t old_traffic = GetTaggedBytes(UNTAGGED_TAG); |
| 12847 | |
| 12848 | // Untagged traffic should be tagged with tag UNTAGGED_TAG. |
| 12849 | TestDelegate delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12850 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12851 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &delegate, |
| 12852 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12853 | EXPECT_EQ(SocketTag(), req->socket_tag()); |
| 12854 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12855 | delegate.RunUntilComplete(); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12856 | |
| 12857 | EXPECT_GT(GetTaggedBytes(UNTAGGED_TAG), old_traffic); |
| 12858 | |
| 12859 | int32_t tag_val1 = 0x12345678; |
| 12860 | SocketTag tag1(SocketTag::UNSET_UID, tag_val1); |
| 12861 | old_traffic = GetTaggedBytes(tag_val1); |
| 12862 | |
| 12863 | // Test specific tag value. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12864 | req = default_context().CreateRequest(http_test_server()->GetURL("/"), |
| 12865 | DEFAULT_PRIORITY, &delegate, |
| 12866 | TRAFFIC_ANNOTATION_FOR_TESTS); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12867 | req->set_socket_tag(tag1); |
| 12868 | EXPECT_EQ(tag1, req->socket_tag()); |
| 12869 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12870 | delegate.RunUntilComplete(); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12871 | |
| 12872 | EXPECT_GT(GetTaggedBytes(tag_val1), old_traffic); |
| 12873 | } |
| 12874 | #endif |
| 12875 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 12876 | // Provides a response to the 0RTT request indicating whether it was received |
| 12877 | // as early data, sending HTTP_TOO_EARLY if enabled. |
| 12878 | class ZeroRTTResponse : public test_server::BasicHttpResponse { |
| 12879 | public: |
| 12880 | ZeroRTTResponse(bool zero_rtt, bool send_too_early) |
| 12881 | : zero_rtt_(zero_rtt), send_too_early_(send_too_early) {} |
| 12882 | ~ZeroRTTResponse() override {} |
| 12883 | |
| 12884 | void SendResponse(const test_server::SendBytesCallback& send, |
| 12885 | const test_server::SendCompleteCallback& done) override { |
| 12886 | AddCustomHeader("Vary", "Early-Data"); |
| 12887 | set_content_type("text/plain"); |
| 12888 | AddCustomHeader("Cache-Control", "no-cache"); |
| 12889 | if (zero_rtt_) { |
| 12890 | if (send_too_early_) |
| 12891 | set_code(HTTP_TOO_EARLY); |
| 12892 | set_content("1"); |
| 12893 | } else { |
| 12894 | set_content("0"); |
| 12895 | } |
| 12896 | |
| 12897 | // Since the EmbeddedTestServer doesn't keep the socket open by default, |
| 12898 | // it is explicitly kept alive to allow the remaining leg of the 0RTT |
| 12899 | // handshake to be received after the early data. |
| 12900 | send.Run(ToResponseString(), base::DoNothing()); |
| 12901 | } |
| 12902 | |
| 12903 | private: |
| 12904 | bool zero_rtt_; |
| 12905 | bool send_too_early_; |
| 12906 | |
| 12907 | DISALLOW_COPY_AND_ASSIGN(ZeroRTTResponse); |
| 12908 | }; |
| 12909 | |
| 12910 | std::unique_ptr<test_server::HttpResponse> HandleZeroRTTRequest( |
| 12911 | const test_server::HttpRequest& request) { |
| 12912 | if (request.GetURL().path() != "/zerortt") |
| 12913 | return nullptr; |
| 12914 | auto iter = request.headers.find("Early-Data"); |
| 12915 | bool zero_rtt = iter != request.headers.end() && iter->second == "1"; |
| 12916 | return std::make_unique<ZeroRTTResponse>(zero_rtt, false); |
| 12917 | } |
| 12918 | |
| 12919 | class HTTPSEarlyDataTest : public TestWithScopedTaskEnvironment { |
| 12920 | public: |
| 12921 | HTTPSEarlyDataTest() |
| 12922 | : context_(true), test_server_(net::EmbeddedTestServer::TYPE_HTTPS) { |
| 12923 | auto params = std::make_unique<HttpNetworkSession::Params>(); |
| 12924 | params->enable_early_data = true; |
| 12925 | context_.set_http_network_session_params(std::move(params)); |
| 12926 | |
| 12927 | context_.set_network_delegate(&network_delegate_); |
| 12928 | cert_verifier_.set_default_result(OK); |
| 12929 | context_.set_cert_verifier(&cert_verifier_); |
| 12930 | |
| 12931 | ssl_config_service_ = std::make_unique<TestSSLConfigService>(); |
| 12932 | ssl_config_service_->set_max_version(SSL_PROTOCOL_VERSION_TLS1_3); |
| 12933 | context_.set_ssl_config_service(ssl_config_service_.get()); |
| 12934 | |
| 12935 | context_.Init(); |
| 12936 | |
| 12937 | ssl_config_.version_max = SSL_PROTOCOL_VERSION_TLS1_3; |
| 12938 | ssl_config_.early_data_enabled = true; |
| 12939 | test_server_.SetSSLConfig(net::EmbeddedTestServer::CERT_OK, ssl_config_); |
| 12940 | test_server_.AddDefaultHandlers( |
| 12941 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 12942 | test_server_.RegisterRequestHandler( |
| 12943 | base::BindRepeating(&HandleZeroRTTRequest)); |
| 12944 | } |
| 12945 | |
| 12946 | ~HTTPSEarlyDataTest() override = default; |
| 12947 | |
| 12948 | void ResetSSLConfig(net::EmbeddedTestServer::ServerCertificate cert, |
| 12949 | uint16_t version) { |
| 12950 | ssl_config_.version_max = version; |
| 12951 | test_server_.ResetSSLConfig(cert, ssl_config_); |
| 12952 | } |
| 12953 | |
| 12954 | protected: |
| 12955 | MockCertVerifier cert_verifier_; |
| 12956 | TestNetworkDelegate network_delegate_; // Must outlive URLRequest. |
| 12957 | std::unique_ptr<TestSSLConfigService> ssl_config_service_; |
| 12958 | TestURLRequestContext context_; |
| 12959 | |
| 12960 | SSLServerConfig ssl_config_; |
| 12961 | EmbeddedTestServer test_server_; |
| 12962 | }; |
| 12963 | |
| 12964 | // TLSEarlyDataTest tests that we handle early data correctly. |
| 12965 | TEST_F(HTTPSEarlyDataTest, TLSEarlyDataTest) { |
| 12966 | ASSERT_TRUE(test_server_.Start()); |
| 12967 | context_.http_transaction_factory()->GetSession()->ClearSSLSessionCache(); |
| 12968 | |
| 12969 | { |
| 12970 | TestDelegate d; |
| 12971 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 12972 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 12973 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12974 | r->Start(); |
| 12975 | EXPECT_TRUE(r->is_pending()); |
| 12976 | |
| 12977 | base::RunLoop().Run(); |
| 12978 | |
| 12979 | EXPECT_EQ(1, d.response_started_count()); |
| 12980 | |
| 12981 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 12982 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 12983 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 12984 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 12985 | r->ssl_info().cert.get())); |
| 12986 | |
| 12987 | // The Early-Data header should be omitted in the initial request, and the |
| 12988 | // handler should return "0". |
| 12989 | EXPECT_EQ("0", d.data_received()); |
| 12990 | } |
| 12991 | |
| 12992 | context_.http_transaction_factory()->GetSession()->CloseAllConnections(); |
| 12993 | |
| 12994 | { |
| 12995 | TestDelegate d; |
| 12996 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 12997 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 12998 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12999 | |
| 13000 | r->Start(); |
| 13001 | EXPECT_TRUE(r->is_pending()); |
| 13002 | |
| 13003 | base::RunLoop().Run(); |
| 13004 | |
| 13005 | EXPECT_EQ(1, d.response_started_count()); |
| 13006 | |
| 13007 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13008 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13009 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13010 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13011 | r->ssl_info().cert.get())); |
| 13012 | |
| 13013 | // The Early-Data header should be a single '1' in the resumed request, and |
| 13014 | // the handler should return "1". |
| 13015 | EXPECT_EQ("1", d.data_received()); |
| 13016 | } |
| 13017 | } |
| 13018 | |
| 13019 | // TLSEarlyDataTest tests that we handle early data correctly for POST. |
| 13020 | TEST_F(HTTPSEarlyDataTest, TLSEarlyDataPOSTTest) { |
| 13021 | ASSERT_TRUE(test_server_.Start()); |
| 13022 | context_.http_transaction_factory()->GetSession()->ClearSSLSessionCache(); |
| 13023 | |
| 13024 | { |
| 13025 | TestDelegate d; |
| 13026 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13027 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13028 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13029 | r->Start(); |
| 13030 | EXPECT_TRUE(r->is_pending()); |
| 13031 | |
| 13032 | base::RunLoop().Run(); |
| 13033 | |
| 13034 | EXPECT_EQ(1, d.response_started_count()); |
| 13035 | |
| 13036 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13037 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13038 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13039 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13040 | r->ssl_info().cert.get())); |
| 13041 | |
| 13042 | // The Early-Data header should be omitted in the initial request, and the |
| 13043 | // handler should return "0". |
| 13044 | EXPECT_EQ("0", d.data_received()); |
| 13045 | } |
| 13046 | |
| 13047 | context_.http_transaction_factory()->GetSession()->CloseAllConnections(); |
| 13048 | |
| 13049 | { |
| 13050 | TestDelegate d; |
| 13051 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13052 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13053 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13054 | r->set_method("POST"); |
| 13055 | r->Start(); |
| 13056 | EXPECT_TRUE(r->is_pending()); |
| 13057 | |
| 13058 | base::RunLoop().Run(); |
| 13059 | |
| 13060 | EXPECT_EQ(1, d.response_started_count()); |
| 13061 | |
| 13062 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13063 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13064 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13065 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13066 | r->ssl_info().cert.get())); |
| 13067 | |
| 13068 | // The Early-Data header should be omitted in the request, since we don't |
| 13069 | // send POSTs over early data, and the handler should return "0". |
| 13070 | EXPECT_EQ("0", d.data_received()); |
| 13071 | } |
| 13072 | } |
| 13073 | |
| 13074 | std::unique_ptr<test_server::HttpResponse> HandleTooEarly( |
| 13075 | bool* sent_425, |
| 13076 | const test_server::HttpRequest& request) { |
| 13077 | if (request.GetURL().path() != "/tooearly") |
| 13078 | return nullptr; |
| 13079 | auto iter = request.headers.find("Early-Data"); |
| 13080 | bool zero_rtt = iter != request.headers.end() && iter->second == "1"; |
| 13081 | if (zero_rtt) |
| 13082 | *sent_425 = true; |
| 13083 | return std::make_unique<ZeroRTTResponse>(zero_rtt, true); |
| 13084 | } |
| 13085 | |
| 13086 | // Test that we handle 425 (Too Early) correctly. |
| 13087 | TEST_F(HTTPSEarlyDataTest, TLSEarlyDataTooEarlyTest) { |
| 13088 | bool sent_425 = false; |
| 13089 | test_server_.RegisterRequestHandler( |
| 13090 | base::BindRepeating(&HandleTooEarly, base::Unretained(&sent_425))); |
| 13091 | ASSERT_TRUE(test_server_.Start()); |
| 13092 | context_.http_transaction_factory()->GetSession()->ClearSSLSessionCache(); |
| 13093 | |
| 13094 | { |
| 13095 | TestDelegate d; |
| 13096 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13097 | test_server_.GetURL("/tooearly"), DEFAULT_PRIORITY, &d, |
| 13098 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13099 | r->Start(); |
| 13100 | EXPECT_TRUE(r->is_pending()); |
| 13101 | |
| 13102 | d.RunUntilComplete(); |
| 13103 | |
| 13104 | EXPECT_EQ(1, d.response_started_count()); |
| 13105 | |
| 13106 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13107 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13108 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13109 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13110 | r->ssl_info().cert.get())); |
| 13111 | |
| 13112 | // The Early-Data header should be omitted in the initial request, and the |
| 13113 | // handler should return "0". |
| 13114 | EXPECT_EQ("0", d.data_received()); |
| 13115 | EXPECT_FALSE(sent_425); |
| 13116 | } |
| 13117 | |
| 13118 | context_.http_transaction_factory()->GetSession()->CloseAllConnections(); |
| 13119 | |
| 13120 | { |
| 13121 | TestDelegate d; |
| 13122 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13123 | test_server_.GetURL("/tooearly"), DEFAULT_PRIORITY, &d, |
| 13124 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13125 | |
| 13126 | r->Start(); |
| 13127 | EXPECT_TRUE(r->is_pending()); |
| 13128 | |
| 13129 | d.RunUntilComplete(); |
| 13130 | |
| 13131 | EXPECT_EQ(1, d.response_started_count()); |
| 13132 | |
| 13133 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13134 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13135 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13136 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13137 | r->ssl_info().cert.get())); |
| 13138 | |
| 13139 | // The resumption request will encounter a 425 error and retry without early |
| 13140 | // data, and the handler should return "0". |
| 13141 | EXPECT_EQ("0", d.data_received()); |
| 13142 | EXPECT_TRUE(sent_425); |
| 13143 | } |
| 13144 | } |
| 13145 | |
| 13146 | // TLSEarlyDataRejectTest tests that we gracefully handle an early data reject |
| 13147 | // and retry without early data. |
| 13148 | TEST_F(HTTPSEarlyDataTest, TLSEarlyDataRejectTest) { |
| 13149 | ASSERT_TRUE(test_server_.Start()); |
| 13150 | context_.http_transaction_factory()->GetSession()->ClearSSLSessionCache(); |
| 13151 | |
| 13152 | { |
| 13153 | TestDelegate d; |
| 13154 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13155 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13156 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13157 | |
| 13158 | r->Start(); |
| 13159 | EXPECT_TRUE(r->is_pending()); |
| 13160 | |
| 13161 | d.RunUntilComplete(); |
| 13162 | |
| 13163 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13164 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13165 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13166 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13167 | r->ssl_info().cert.get())); |
| 13168 | |
| 13169 | // The Early-Data header should be omitted in the initial request, and the |
| 13170 | // handler should return "0". |
| 13171 | EXPECT_EQ("0", d.data_received()); |
| 13172 | } |
| 13173 | |
| 13174 | context_.http_transaction_factory()->GetSession()->CloseAllConnections(); |
| 13175 | |
| 13176 | // The certificate in the resumption is changed to confirm that the |
| 13177 | // certificate change is observed. |
| 13178 | scoped_refptr<X509Certificate> old_cert = test_server_.GetCertificate(); |
| 13179 | ResetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED, |
| 13180 | SSL_PROTOCOL_VERSION_TLS1_3); |
| 13181 | |
| 13182 | { |
| 13183 | TestDelegate d; |
| 13184 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13185 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13186 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13187 | |
| 13188 | r->Start(); |
| 13189 | EXPECT_TRUE(r->is_pending()); |
| 13190 | |
| 13191 | d.RunUntilComplete(); |
| 13192 | |
| 13193 | EXPECT_EQ(1, d.response_started_count()); |
| 13194 | |
| 13195 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13196 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13197 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13198 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13199 | r->ssl_info().cert.get())); |
| 13200 | EXPECT_FALSE(old_cert->EqualsIncludingChain(r->ssl_info().cert.get())); |
| 13201 | |
| 13202 | // The Early-Data header should be omitted in the rejected request, and the |
| 13203 | // handler should return "0". |
| 13204 | EXPECT_EQ("0", d.data_received()); |
| 13205 | } |
| 13206 | } |
| 13207 | |
| 13208 | // TLSEarlyDataTLS12RejectTest tests that we gracefully handle an early data |
| 13209 | // reject from a TLS 1.2 server and retry without early data. |
| 13210 | TEST_F(HTTPSEarlyDataTest, TLSEarlyDataTLS12RejectTest) { |
| 13211 | ASSERT_TRUE(test_server_.Start()); |
| 13212 | context_.http_transaction_factory()->GetSession()->ClearSSLSessionCache(); |
| 13213 | |
| 13214 | { |
| 13215 | TestDelegate d; |
| 13216 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13217 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13218 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13219 | |
| 13220 | r->Start(); |
| 13221 | EXPECT_TRUE(r->is_pending()); |
| 13222 | |
| 13223 | d.RunUntilComplete(); |
| 13224 | |
| 13225 | EXPECT_EQ(1, d.response_started_count()); |
| 13226 | |
| 13227 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13228 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13229 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13230 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13231 | r->ssl_info().cert.get())); |
| 13232 | |
| 13233 | // The Early-Data header should be omitted in the initial request, and the |
| 13234 | // handler should return "0". |
| 13235 | EXPECT_EQ("0", d.data_received()); |
| 13236 | } |
| 13237 | |
| 13238 | context_.http_transaction_factory()->GetSession()->CloseAllConnections(); |
| 13239 | |
| 13240 | // The certificate in the resumption is changed to confirm that the |
| 13241 | // certificate change is observed. |
| 13242 | scoped_refptr<X509Certificate> old_cert = test_server_.GetCertificate(); |
| 13243 | ResetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED, |
| 13244 | SSL_PROTOCOL_VERSION_TLS1_2); |
| 13245 | |
| 13246 | { |
| 13247 | TestDelegate d; |
| 13248 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13249 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13250 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13251 | |
| 13252 | r->Start(); |
| 13253 | EXPECT_TRUE(r->is_pending()); |
| 13254 | |
| 13255 | d.RunUntilComplete(); |
| 13256 | |
| 13257 | EXPECT_EQ(1, d.response_started_count()); |
| 13258 | |
| 13259 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_2, |
| 13260 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13261 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13262 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13263 | r->ssl_info().cert.get())); |
| 13264 | EXPECT_FALSE(old_cert->EqualsIncludingChain(r->ssl_info().cert.get())); |
| 13265 | |
| 13266 | // The Early-Data header should be omitted in the rejected request, and the |
| 13267 | // handler should return "0". |
| 13268 | EXPECT_EQ("0", d.data_received()); |
| 13269 | } |
| 13270 | } |
| 13271 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 13272 | } // namespace net |