[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" |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 57 | #include "net/base/layered_network_delegate.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 58 | #include "net/base/load_flags.h" |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 59 | #include "net/base/load_timing_info.h" |
| 60 | #include "net/base/load_timing_info_test_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 61 | #include "net/base/net_errors.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 62 | #include "net/base/net_module.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 63 | #include "net/base/proxy_server.h" |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 64 | #include "net/base/request_priority.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 65 | #include "net/base/test_completion_callback.h" |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 66 | #include "net/base/upload_bytes_element_reader.h" |
| 67 | #include "net/base/upload_data_stream.h" |
| 68 | #include "net/base/upload_file_element_reader.h" |
tfarina | 7a4a7fd | 2016-01-20 14:23:44 | [diff] [blame] | 69 | #include "net/base/url_util.h" |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 70 | #include "net/cert/crl_set.h" |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 71 | #include "net/cert/ct_policy_enforcer.h" |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 72 | #include "net/cert/ct_policy_status.h" |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 73 | #include "net/cert/do_nothing_ct_verifier.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 74 | #include "net/cert/ev_root_ca_metadata.h" |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 75 | #include "net/cert/mock_cert_verifier.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 76 | #include "net/cert/multi_log_ct_verifier.h" |
eranm | dcec963 | 2016-10-10 14:16:10 | [diff] [blame] | 77 | #include "net/cert/signed_certificate_timestamp_and_status.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 78 | #include "net/cert/test_root_certs.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 79 | #include "net/cookies/cookie_monster.h" |
| 80 | #include "net/cookies/cookie_store_test_helpers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 81 | #include "net/disk_cache/disk_cache.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 82 | #include "net/dns/mock_host_resolver.h" |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 83 | #include "net/http/http_byte_range.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 84 | #include "net/http/http_cache.h" |
| 85 | #include "net/http/http_network_layer.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 86 | #include "net/http/http_network_session.h" |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 87 | #include "net/http/http_request_headers.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 88 | #include "net/http/http_response_headers.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 89 | #include "net/http/http_server_properties_impl.h" |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 90 | #include "net/http/http_util.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 91 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 92 | #include "net/log/net_log_source.h" |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 93 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 94 | #include "net/log/test_net_log_entry.h" |
| 95 | #include "net/log/test_net_log_util.h" |
Scott Violet | 0caaaf43 | 2018-03-24 00:43:59 | [diff] [blame] | 96 | #include "net/net_buildflags.h" |
Lily Houghton | ffe89daa0 | 2018-03-09 18:30:03 | [diff] [blame] | 97 | #include "net/proxy_resolution/proxy_resolution_service.h" |
Ryan Hamilton | a3ee93a7 | 2018-08-01 22:03:08 | [diff] [blame] | 98 | #include "net/quic/mock_crypto_client_stream_factory.h" |
| 99 | #include "net/quic/quic_server_info.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 100 | #include "net/socket/socket_test_util.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 101 | #include "net/socket/ssl_client_socket.h" |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 102 | #include "net/ssl/channel_id_service.h" |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 103 | #include "net/ssl/client_cert_identity_test_util.h" |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 104 | #include "net/ssl/default_channel_id_store.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 105 | #include "net/ssl/ssl_connection_status_flags.h" |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 106 | #include "net/ssl/ssl_private_key.h" |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 107 | #include "net/ssl/ssl_server_config.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 108 | #include "net/test/cert_test_util.h" |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 109 | #include "net/test/embedded_test_server/embedded_test_server.h" |
| 110 | #include "net/test/embedded_test_server/http_request.h" |
| 111 | #include "net/test/embedded_test_server/http_response.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 112 | #include "net/test/gtest_util.h" |
[email protected] | 89b3252 | 2013-05-07 20:04:21 | [diff] [blame] | 113 | #include "net/test/spawned_test_server/spawned_test_server.h" |
rsleevi | a69c79a | 2016-06-22 03:28:43 | [diff] [blame] | 114 | #include "net/test/test_data_directory.h" |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 115 | #include "net/test/test_with_scoped_task_environment.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 116 | #include "net/test/url_request/url_request_failed_job.h" |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 117 | #include "net/test/url_request/url_request_mock_http_job.h" |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 118 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 119 | #include "net/url_request/data_protocol_handler.h" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 120 | #include "net/url_request/static_http_user_agent_settings.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 121 | #include "net/url_request/url_request.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 122 | #include "net/url_request/url_request_filter.h" |
[email protected] | bcb84f8b | 2009-08-31 16:20:14 | [diff] [blame] | 123 | #include "net/url_request/url_request_http_job.h" |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 124 | #include "net/url_request/url_request_http_job_histogram.h" |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 125 | #include "net/url_request/url_request_intercepting_job_factory.h" |
| 126 | #include "net/url_request/url_request_interceptor.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 127 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 128 | #include "net/url_request/url_request_redirect_job.h" |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 129 | #include "net/url_request/url_request_test_job.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 130 | #include "net/url_request/url_request_test_util.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 131 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 132 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 133 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 134 | |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 135 | #if defined(OS_FUCHSIA) |
| 136 | #define USE_BUILTIN_CERT_VERIFIER |
| 137 | #endif |
| 138 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 139 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 140 | #include "net/base/filename_util.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 141 | #include "net/url_request/file_protocol_handler.h" |
| 142 | #include "net/url_request/url_request_file_dir_job.h" |
| 143 | #endif |
| 144 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 145 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 146 | #include "net/ftp/ftp_network_layer.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 147 | #include "net/url_request/ftp_protocol_handler.h" |
| 148 | #endif |
| 149 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 150 | #if defined(OS_WIN) |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 151 | #include "base/win/scoped_com_initializer.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 152 | #endif |
| 153 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 154 | #if BUILDFLAG(ENABLE_REPORTING) |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 155 | #include "net/network_error_logging/network_error_logging_service.h" |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 156 | #include "net/network_error_logging/network_error_logging_test_util.h" |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 157 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 158 | |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 159 | #if defined(OS_ANDROID) || defined(USE_BUILTIN_CERT_VERIFIER) |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 160 | #include "net/cert/cert_net_fetcher.h" |
| 161 | #include "net/cert_net/cert_net_fetcher_impl.h" |
| 162 | #endif |
| 163 | |
| 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, |
| 794 | const SSLInfo& ssl_info, |
| 795 | bool fatal) override { |
| 796 | ssl_info_ = ssl_info; |
| 797 | on_ssl_certificate_error_called_ = true; |
| 798 | TestDelegate::OnSSLCertificateError(request, ssl_info, fatal); |
| 799 | } |
| 800 | |
| 801 | bool on_ssl_certificate_error_called() { |
| 802 | return on_ssl_certificate_error_called_; |
| 803 | } |
| 804 | |
| 805 | SSLInfo ssl_info() { return ssl_info_; } |
| 806 | |
| 807 | private: |
| 808 | bool on_ssl_certificate_error_called_ = false; |
| 809 | SSLInfo ssl_info_; |
| 810 | }; |
| 811 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 812 | } // namespace |
| 813 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 814 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 815 | class URLRequestTest : public PlatformTest, public WithScopedTaskEnvironment { |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 816 | public: |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 817 | URLRequestTest() |
| 818 | : default_context_(std::make_unique<TestURLRequestContext>(true)) { |
| 819 | default_context_->set_network_delegate(&default_network_delegate_); |
| 820 | default_context_->set_net_log(&net_log_); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 821 | job_factory_impl_ = new URLRequestJobFactoryImpl(); |
| 822 | job_factory_.reset(job_factory_impl_); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 823 | } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 824 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 825 | ~URLRequestTest() override { |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 826 | // URLRequestJobs may post clean-up tasks on destruction. |
| 827 | base::RunLoop().RunUntilIdle(); |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 828 | |
| 829 | SetTransportSecurityStateSourceForTesting(nullptr); |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 830 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 831 | |
dcheng | 2339883c | 2014-12-23 00:23:05 | [diff] [blame] | 832 | void SetUp() override { |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 833 | SetUpFactory(); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 834 | default_context_->set_job_factory(job_factory_.get()); |
| 835 | default_context_->Init(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 836 | PlatformTest::SetUp(); |
| 837 | } |
| 838 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 839 | void TearDown() override { default_context_.reset(); } |
| 840 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 841 | virtual void SetUpFactory() { |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 842 | job_factory_impl_->SetProtocolHandler( |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 843 | "data", std::make_unique<DataProtocolHandler>()); |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 844 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 845 | job_factory_impl_->SetProtocolHandler( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 846 | "file", std::make_unique<FileProtocolHandler>( |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 847 | base::ThreadTaskRunnerHandle::Get())); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 848 | #endif |
| 849 | } |
| 850 | |
| 851 | TestNetworkDelegate* default_network_delegate() { |
| 852 | return &default_network_delegate_; |
| 853 | } |
| 854 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 855 | TestURLRequestContext& default_context() const { return *default_context_; } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 856 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 857 | // Adds the TestJobInterceptor to the default context. |
| 858 | TestJobInterceptor* AddTestInterceptor() { |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 859 | TestJobInterceptor* protocol_handler_ = new TestJobInterceptor(); |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 860 | job_factory_impl_->SetProtocolHandler("http", nullptr); |
| 861 | job_factory_impl_->SetProtocolHandler("http", |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 862 | base::WrapUnique(protocol_handler_)); |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 863 | return protocol_handler_; |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 864 | } |
| 865 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 866 | // Creates a temp test file and writes |data| to the file. The file will be |
| 867 | // deleted after the test completes. |
| 868 | void CreateTestFile(const char* data, |
| 869 | size_t data_size, |
| 870 | base::FilePath* test_file) { |
| 871 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 872 | // Get an absolute path since |temp_dir| can contain a symbolic link. As of |
| 873 | // now, Mac and Android bots return a path with a symbolic link. |
| 874 | base::FilePath absolute_temp_dir = |
| 875 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
| 876 | |
| 877 | ASSERT_TRUE(base::CreateTemporaryFileInDir(absolute_temp_dir, test_file)); |
| 878 | ASSERT_EQ(static_cast<int>(data_size), |
| 879 | base::WriteFile(*test_file, data, data_size)); |
| 880 | } |
| 881 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 882 | protected: |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 883 | TestNetLog net_log_; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 884 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 885 | URLRequestJobFactoryImpl* job_factory_impl_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 886 | std::unique_ptr<URLRequestJobFactory> job_factory_; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 887 | std::unique_ptr<TestURLRequestContext> default_context_; |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 888 | base::ScopedTempDir temp_dir_; |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 889 | }; |
| 890 | |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 891 | // This NetworkDelegate is picky about what files are accessible. Only |
| 892 | // whitelisted files are allowed. |
| 893 | class CookieBlockingNetworkDelegate : public TestNetworkDelegate { |
| 894 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 895 | CookieBlockingNetworkDelegate() = default; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 896 | |
| 897 | // Adds |directory| to the access white list. |
| 898 | void AddToWhitelist(const base::FilePath& directory) { |
| 899 | whitelist_.insert(directory); |
| 900 | } |
| 901 | |
| 902 | private: |
| 903 | // Returns true if |path| matches the white list. |
| 904 | bool OnCanAccessFileInternal(const base::FilePath& path) const { |
| 905 | for (const auto& directory : whitelist_) { |
| 906 | if (directory == path || directory.IsParent(path)) |
| 907 | return true; |
| 908 | } |
| 909 | return false; |
| 910 | } |
| 911 | |
| 912 | // Returns true only if both |original_path| and |absolute_path| match the |
| 913 | // white list. |
| 914 | bool OnCanAccessFile(const URLRequest& request, |
| 915 | const base::FilePath& original_path, |
| 916 | const base::FilePath& absolute_path) const override { |
| 917 | return (OnCanAccessFileInternal(original_path) && |
| 918 | OnCanAccessFileInternal(absolute_path)); |
| 919 | } |
| 920 | |
| 921 | std::set<base::FilePath> whitelist_; |
| 922 | |
| 923 | DISALLOW_COPY_AND_ASSIGN(CookieBlockingNetworkDelegate); |
| 924 | }; |
| 925 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 926 | TEST_F(URLRequestTest, AboutBlankTest) { |
| 927 | TestDelegate d; |
| 928 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 929 | std::unique_ptr<URLRequest> r( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 930 | default_context().CreateRequest(GURL("about:blank"), DEFAULT_PRIORITY, |
| 931 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 932 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 933 | r->Start(); |
| 934 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 935 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 936 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 937 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 938 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 939 | EXPECT_FALSE(d.received_data_before_response()); |
| 940 | EXPECT_EQ(d.bytes_received(), 0); |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 941 | EXPECT_TRUE(r->GetResponseRemoteEndpoint().address().empty()); |
| 942 | EXPECT_EQ(0, r->GetResponseRemoteEndpoint().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 943 | |
| 944 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 945 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 946 | } |
| 947 | } |
| 948 | |
| 949 | TEST_F(URLRequestTest, DataURLImageTest) { |
| 950 | TestDelegate d; |
| 951 | { |
| 952 | // Use our nice little Chrome logo. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 953 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 954 | GURL("data:image/png;base64," |
| 955 | "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUB" |
| 956 | "BjG3w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQ" |
| 957 | "xIEVxitD5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI" |
| 958 | "5MpCVdW0gO7tvNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKz" |
| 959 | "Mx1+fg9bAgK6zHq9cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7Ck" |
| 960 | "IMDxQpF7r/MWq12UctI1dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0" |
| 961 | "WYUqt57yWf2McHTObYPbVD+ZwbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2H" |
| 962 | "QQcm43XwmtoYM6vVKleh0uoWvnzW3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nk" |
| 963 | "DFHF9ZS+uYVjRUasMeHUmyLYtcklTvzWGFZnNOXczThvpKIzjcahSqIzkvDLayDq6" |
| 964 | "D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTbxXX1T5xsV9tm9r4TQwHLiZw/pdDZJ" |
| 965 | "ea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5MrVvMzNaI3+ERHfrFzPKQukrQ" |
| 966 | "GI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gVMd6q5c8GdosynKmSeRuGz" |
| 967 | "pjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq/uxxZKzNiZFGD0wRC" |
| 968 | "3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7FUx1qLnV7MGF40" |
| 969 | "smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufBJbsysjjW4" |
| 970 | "kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJMnlm2O" |
| 971 | "34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfmm" |
| 972 | "oRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV" |
| 973 | "5EnhORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 974 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 975 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 976 | r->Start(); |
| 977 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 978 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 979 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 980 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 981 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 982 | EXPECT_FALSE(d.received_data_before_response()); |
| 983 | EXPECT_EQ(d.bytes_received(), 911); |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 984 | EXPECT_TRUE(r->GetResponseRemoteEndpoint().address().empty()); |
| 985 | EXPECT_EQ(0, r->GetResponseRemoteEndpoint().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 986 | |
| 987 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 988 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 992 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 993 | TEST_F(URLRequestTest, FileTest) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 994 | const char kTestFileContent[] = "Hello"; |
| 995 | base::FilePath test_file; |
| 996 | ASSERT_NO_FATAL_FAILURE( |
| 997 | CreateTestFile(kTestFileContent, sizeof(kTestFileContent), &test_file)); |
kraush | 5a64582 | 2016-04-07 18:35:04 | [diff] [blame] | 998 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 999 | GURL test_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1000 | |
| 1001 | TestDelegate d; |
| 1002 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1003 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1004 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1005 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1006 | r->Start(); |
| 1007 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1008 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1009 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1010 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1011 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1012 | EXPECT_EQ(1, d.response_started_count()); |
| 1013 | EXPECT_FALSE(d.received_data_before_response()); |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1014 | EXPECT_EQ(d.bytes_received(), static_cast<int>(sizeof(kTestFileContent))); |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 1015 | EXPECT_TRUE(r->GetResponseRemoteEndpoint().address().empty()); |
| 1016 | EXPECT_EQ(0, r->GetResponseRemoteEndpoint().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 1017 | |
| 1018 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1019 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1020 | } |
| 1021 | } |
| 1022 | |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1023 | TEST_F(URLRequestTest, FileTestCancel) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1024 | const char kTestFileContent[] = "Hello"; |
| 1025 | base::FilePath test_file; |
| 1026 | ASSERT_NO_FATAL_FAILURE( |
| 1027 | CreateTestFile(kTestFileContent, sizeof(kTestFileContent), &test_file)); |
| 1028 | |
| 1029 | GURL test_url = FilePathToFileURL(test_file); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1030 | |
| 1031 | TestDelegate d; |
| 1032 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1033 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1034 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1035 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1036 | r->Start(); |
| 1037 | EXPECT_TRUE(r->is_pending()); |
| 1038 | r->Cancel(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1039 | } |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 1040 | // Async cancellation should be safe even when URLRequest has been already |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1041 | // destroyed. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1042 | base::RunLoop().RunUntilIdle(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1043 | } |
| 1044 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1045 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 1046 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1047 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1048 | FillBuffer(buffer.get(), buffer_size); |
| 1049 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1050 | base::FilePath test_file; |
| 1051 | ASSERT_NO_FATAL_FAILURE( |
| 1052 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1053 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1054 | |
| 1055 | const size_t first_byte_position = 500; |
| 1056 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 1057 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 1058 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 1059 | buffer.get() + last_byte_position + 1); |
| 1060 | |
| 1061 | TestDelegate d; |
| 1062 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1063 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1064 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1065 | |
| 1066 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1067 | headers.SetHeader( |
| 1068 | HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1069 | HttpByteRange::Bounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1070 | first_byte_position, last_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1071 | r->SetExtraRequestHeaders(headers); |
| 1072 | r->Start(); |
| 1073 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1074 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1075 | d.RunUntilComplete(); |
| 1076 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1077 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1078 | EXPECT_EQ(1, d.response_started_count()); |
| 1079 | EXPECT_FALSE(d.received_data_before_response()); |
| 1080 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 1081 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 1082 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 1083 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 1087 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1088 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1089 | FillBuffer(buffer.get(), buffer_size); |
| 1090 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1091 | base::FilePath test_file; |
| 1092 | ASSERT_NO_FATAL_FAILURE( |
| 1093 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1094 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1095 | |
| 1096 | const size_t first_byte_position = 500; |
| 1097 | const size_t last_byte_position = buffer_size - 1; |
| 1098 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 1099 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 1100 | buffer.get() + last_byte_position + 1); |
| 1101 | |
| 1102 | TestDelegate d; |
| 1103 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1104 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1105 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1106 | |
| 1107 | HttpRequestHeaders headers; |
| 1108 | headers.SetHeader(HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1109 | HttpByteRange::RightUnbounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1110 | first_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1111 | r->SetExtraRequestHeaders(headers); |
| 1112 | r->Start(); |
| 1113 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1114 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1115 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1116 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1117 | EXPECT_EQ(1, d.response_started_count()); |
| 1118 | EXPECT_FALSE(d.received_data_before_response()); |
| 1119 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 1120 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 1121 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 1122 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 1126 | const size_t buffer_size = 400000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1127 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1128 | FillBuffer(buffer.get(), buffer_size); |
| 1129 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1130 | base::FilePath test_file; |
| 1131 | ASSERT_NO_FATAL_FAILURE( |
| 1132 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1133 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1134 | |
| 1135 | TestDelegate d; |
| 1136 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1137 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1138 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1139 | |
| 1140 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1141 | headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1142 | r->SetExtraRequestHeaders(headers); |
| 1143 | r->Start(); |
| 1144 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1145 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1146 | d.RunUntilComplete(); |
| 1147 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1148 | EXPECT_TRUE(d.request_failed()); |
| 1149 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1150 | } |
| 1151 | |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1152 | TEST_F(URLRequestTest, AllowFileURLs) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1153 | std::string test_data("monkey"); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1154 | base::FilePath test_file; |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1155 | ASSERT_NO_FATAL_FAILURE( |
| 1156 | CreateTestFile(test_data.data(), test_data.size(), &test_file)); |
| 1157 | |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1158 | // The directory part of the path returned from CreateTemporaryFileInDir() |
| 1159 | // can be slightly different from |absolute_temp_dir| on Windows. |
| 1160 | // Example: C:\\Users\\CHROME~2 -> C:\\Users\\chrome-bot |
| 1161 | // Hence the test should use the directory name of |test_file|, rather than |
| 1162 | // |absolute_temp_dir|, for whitelisting. |
| 1163 | base::FilePath real_temp_dir = test_file.DirName(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1164 | GURL test_file_url = FilePathToFileURL(test_file); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1165 | { |
| 1166 | TestDelegate d; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1167 | CookieBlockingNetworkDelegate network_delegate; |
| 1168 | network_delegate.AddToWhitelist(real_temp_dir); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1169 | default_context().set_network_delegate(&network_delegate); |
| 1170 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1171 | test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1172 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1173 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1174 | // This should be allowed as the file path is whitelisted. |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1175 | EXPECT_FALSE(d.request_failed()); |
| 1176 | EXPECT_EQ(test_data, d.data_received()); |
| 1177 | } |
| 1178 | |
| 1179 | { |
| 1180 | TestDelegate d; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1181 | CookieBlockingNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1182 | default_context().set_network_delegate(&network_delegate); |
| 1183 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1184 | test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1185 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1186 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1187 | // This should be rejected as the file path is not whitelisted. |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1188 | EXPECT_TRUE(d.request_failed()); |
| 1189 | EXPECT_EQ("", d.data_received()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1190 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1191 | } |
| 1192 | } |
| 1193 | |
Wez | 3553913 | 2018-07-17 11:26:05 | [diff] [blame] | 1194 | #if defined(OS_POSIX) // Because of symbolic links. |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1195 | |
| 1196 | TEST_F(URLRequestTest, SymlinksToFiles) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1197 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1198 | // Get an absolute path since temp_dir can contain a symbolic link. |
| 1199 | base::FilePath absolute_temp_dir = |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1200 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1201 | |
| 1202 | // Create a good directory (will be whitelisted) and a good file. |
| 1203 | base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); |
| 1204 | ASSERT_TRUE(base::CreateDirectory(good_dir)); |
| 1205 | base::FilePath good_file; |
| 1206 | ASSERT_TRUE(base::CreateTemporaryFileInDir(good_dir, &good_file)); |
| 1207 | std::string good_data("good"); |
| 1208 | base::WriteFile(good_file, good_data.data(), good_data.size()); |
| 1209 | // See the comment in AllowFileURLs() for why this is done. |
| 1210 | base::FilePath real_good_dir = good_file.DirName(); |
| 1211 | |
| 1212 | // Create a bad directory (will not be whitelisted) and a bad file. |
| 1213 | base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); |
| 1214 | ASSERT_TRUE(base::CreateDirectory(bad_dir)); |
| 1215 | base::FilePath bad_file; |
| 1216 | ASSERT_TRUE(base::CreateTemporaryFileInDir(bad_dir, &bad_file)); |
| 1217 | std::string bad_data("bad"); |
| 1218 | base::WriteFile(bad_file, bad_data.data(), bad_data.size()); |
| 1219 | |
| 1220 | // This symlink will point to the good file. Access to the symlink will be |
| 1221 | // allowed as both the symlink and the destination file are in the same |
| 1222 | // good directory. |
| 1223 | base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); |
| 1224 | ASSERT_TRUE(base::CreateSymbolicLink(good_file, good_symlink)); |
| 1225 | GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1226 | // This symlink will point to the bad file. Even though the symlink is in |
| 1227 | // the good directory, access to the symlink will be rejected since it |
| 1228 | // points to the bad file. |
| 1229 | base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); |
| 1230 | ASSERT_TRUE(base::CreateSymbolicLink(bad_file, bad_symlink)); |
| 1231 | GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1232 | |
| 1233 | CookieBlockingNetworkDelegate network_delegate; |
| 1234 | network_delegate.AddToWhitelist(real_good_dir); |
| 1235 | { |
| 1236 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1237 | default_context().set_network_delegate(&network_delegate); |
| 1238 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1239 | good_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1240 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1241 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1242 | // good_file_url should be allowed. |
| 1243 | EXPECT_FALSE(d.request_failed()); |
| 1244 | EXPECT_EQ(good_data, d.data_received()); |
| 1245 | } |
| 1246 | |
| 1247 | { |
| 1248 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1249 | default_context().set_network_delegate(&network_delegate); |
| 1250 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1251 | bad_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1252 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1253 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1254 | // bad_file_url should be rejected. |
| 1255 | EXPECT_TRUE(d.request_failed()); |
| 1256 | EXPECT_EQ("", d.data_received()); |
| 1257 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | TEST_F(URLRequestTest, SymlinksToDirs) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1262 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1263 | // Get an absolute path since temp_dir can contain a symbolic link. |
| 1264 | base::FilePath absolute_temp_dir = |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1265 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1266 | |
| 1267 | // Create a good directory (will be whitelisted). |
| 1268 | base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); |
| 1269 | ASSERT_TRUE(base::CreateDirectory(good_dir)); |
| 1270 | |
| 1271 | // Create a bad directory (will not be whitelisted). |
| 1272 | base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); |
| 1273 | ASSERT_TRUE(base::CreateDirectory(bad_dir)); |
| 1274 | |
| 1275 | // This symlink will point to the good directory. Access to the symlink |
| 1276 | // will be allowed as the symlink is in the good dir that'll be white |
| 1277 | // listed. |
| 1278 | base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); |
| 1279 | ASSERT_TRUE(base::CreateSymbolicLink(good_dir, good_symlink)); |
| 1280 | GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1281 | // This symlink will point to the bad directory. Even though the symlink is |
| 1282 | // in the good directory, access to the symlink will be rejected since it |
| 1283 | // points to the bad directory. |
| 1284 | base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); |
| 1285 | ASSERT_TRUE(base::CreateSymbolicLink(bad_dir, bad_symlink)); |
| 1286 | GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1287 | |
| 1288 | CookieBlockingNetworkDelegate network_delegate; |
| 1289 | network_delegate.AddToWhitelist(good_dir); |
| 1290 | { |
| 1291 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1292 | default_context().set_network_delegate(&network_delegate); |
| 1293 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1294 | good_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1295 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1296 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1297 | // good_file_url should be allowed. |
| 1298 | EXPECT_FALSE(d.request_failed()); |
| 1299 | ASSERT_NE(d.data_received().find("good_symlink"), std::string::npos); |
| 1300 | } |
| 1301 | |
| 1302 | { |
| 1303 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1304 | default_context().set_network_delegate(&network_delegate); |
| 1305 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1306 | bad_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1307 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1308 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1309 | // bad_file_url should be rejected. |
| 1310 | EXPECT_TRUE(d.request_failed()); |
| 1311 | EXPECT_EQ("", d.data_received()); |
| 1312 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
| 1313 | } |
| 1314 | } |
| 1315 | |
Wez | 3553913 | 2018-07-17 11:26:05 | [diff] [blame] | 1316 | #endif // defined(OS_POSIX) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1317 | |
| 1318 | TEST_F(URLRequestTest, FileDirCancelTest) { |
| 1319 | // Put in mock resource provider. |
| 1320 | NetModule::SetResourceProvider(TestNetResourceProvider); |
| 1321 | |
| 1322 | TestDelegate d; |
| 1323 | { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1324 | base::FilePath file_path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1325 | base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1326 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 1327 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
| 1328 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1329 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1330 | FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d, |
| 1331 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1332 | req->Start(); |
| 1333 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1334 | |
| 1335 | d.set_cancel_in_received_data_pending(true); |
| 1336 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1337 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | // Take out mock resource provider. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1341 | NetModule::SetResourceProvider(nullptr); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1342 | } |
| 1343 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1344 | TEST_F(URLRequestTest, FileDirOutputSanity) { |
| 1345 | // Verify the general sanity of the the output of the file: |
| 1346 | // directory lister by checking for the output of a known existing |
| 1347 | // file. |
| 1348 | const char sentinel_name[] = "filedir-sentinel"; |
| 1349 | |
| 1350 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1351 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1352 | path = path.Append(kTestFilePath); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1353 | |
| 1354 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1355 | std::unique_ptr<URLRequest> req( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1356 | default_context().CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 1357 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1358 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1359 | d.RunUntilComplete(); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1360 | |
| 1361 | // Generate entry for the sentinel file. |
| 1362 | base::FilePath sentinel_path = path.AppendASCII(sentinel_name); |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 1363 | base::File::Info info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 1364 | EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1365 | EXPECT_GT(info.size, 0); |
| 1366 | std::string sentinel_output = GetDirectoryListingEntry( |
| 1367 | base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1368 | std::string(sentinel_name), false /* is_dir */, info.size, |
| 1369 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1370 | info.last_modified); |
| 1371 | |
| 1372 | ASSERT_LT(0, d.bytes_received()); |
| 1373 | ASSERT_FALSE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1374 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1375 | // Check for the entry generated for the "sentinel" file. |
| 1376 | const std::string& data = d.data_received(); |
| 1377 | ASSERT_NE(data.find(sentinel_output), std::string::npos); |
| 1378 | } |
| 1379 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1380 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 1381 | // There is an implicit redirect when loading a file path that matches a |
| 1382 | // directory and does not end with a slash. Ensure that following such |
| 1383 | // redirects does not crash. See http://crbug.com/18686. |
| 1384 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1385 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1386 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1387 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1388 | |
| 1389 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1390 | std::unique_ptr<URLRequest> req( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1391 | default_context().CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 1392 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1393 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1394 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1395 | |
| 1396 | ASSERT_EQ(1, d.received_redirect_count()); |
| 1397 | ASSERT_LT(0, d.bytes_received()); |
| 1398 | ASSERT_FALSE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1399 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | #if defined(OS_WIN) |
| 1403 | // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
| 1404 | TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
| 1405 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1406 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1407 | GURL("file:///"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1408 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1409 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1410 | |
| 1411 | ASSERT_EQ(1, d.received_redirect_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1412 | EXPECT_NE(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1413 | } |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1414 | #endif // defined(OS_WIN) |
| 1415 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 1416 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1417 | |
| 1418 | TEST_F(URLRequestTest, InvalidUrlTest) { |
| 1419 | TestDelegate d; |
| 1420 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1421 | std::unique_ptr<URLRequest> r( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1422 | default_context().CreateRequest(GURL("invalid url"), DEFAULT_PRIORITY, |
| 1423 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1424 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1425 | r->Start(); |
| 1426 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1427 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1428 | d.RunUntilComplete(); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1429 | EXPECT_TRUE(d.request_failed()); |
| 1430 | } |
| 1431 | } |
| 1432 | |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1433 | TEST_F(URLRequestTest, InvalidReferrerTest) { |
| 1434 | TestURLRequestContext context; |
| 1435 | TestNetworkDelegate network_delegate; |
| 1436 | network_delegate.set_cancel_request_with_policy_violating_referrer(true); |
| 1437 | context.set_network_delegate(&network_delegate); |
| 1438 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1439 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1440 | context.CreateRequest(GURL("http://localhost/"), DEFAULT_PRIORITY, &d, |
| 1441 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1442 | req->SetReferrer("https://somewhere.com/"); |
| 1443 | |
| 1444 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1445 | d.RunUntilComplete(); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1446 | EXPECT_TRUE(d.request_failed()); |
| 1447 | } |
| 1448 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1449 | #if defined(OS_WIN) |
| 1450 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
| 1451 | base::FilePath app_path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1452 | base::PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1453 | app_path = app_path.Append(kTestFilePath); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1454 | app_path = app_path.AppendASCII("with-headers.html"); |
| 1455 | |
jdoerrie | 6312bf6 | 2019-02-01 22:03:42 | [diff] [blame] | 1456 | base::string16 lnk_path = app_path.value() + FILE_PATH_LITERAL(".lnk"); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1457 | |
| 1458 | base::win::ScopedCOMInitializer com_initializer; |
| 1459 | |
| 1460 | // Temporarily create a shortcut for test |
| 1461 | { |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 1462 | Microsoft::WRL::ComPtr<IShellLink> shell; |
robliao | eb9bfd64 | 2017-05-18 17:35:16 | [diff] [blame] | 1463 | ASSERT_TRUE(SUCCEEDED(::CoCreateInstance( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1464 | CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shell)))); |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 1465 | Microsoft::WRL::ComPtr<IPersistFile> persist; |
robliao | 310fa98b | 2017-05-11 17:14:00 | [diff] [blame] | 1466 | ASSERT_TRUE(SUCCEEDED(shell.CopyTo(persist.GetAddressOf()))); |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1467 | EXPECT_TRUE(SUCCEEDED(shell->SetPath(base::as_wcstr(app_path.value())))); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1468 | EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1469 | EXPECT_TRUE(SUCCEEDED(persist->Save(base::as_wcstr(lnk_path), TRUE))); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | TestDelegate d; |
| 1473 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1474 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1475 | FilePathToFileURL(base::FilePath(lnk_path)), DEFAULT_PRIORITY, &d, |
| 1476 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1477 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1478 | r->Start(); |
| 1479 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1480 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1481 | d.RunUntilComplete(); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1482 | |
| 1483 | WIN32_FILE_ATTRIBUTE_DATA data; |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1484 | GetFileAttributesEx(base::as_wcstr(app_path.value()), GetFileExInfoStandard, |
jdoerrie | 6312bf6 | 2019-02-01 22:03:42 | [diff] [blame] | 1485 | &data); |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1486 | HANDLE file = CreateFile(base::as_wcstr(app_path.value()), GENERIC_READ, |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1487 | FILE_SHARE_READ, nullptr, OPEN_EXISTING, |
| 1488 | FILE_ATTRIBUTE_NORMAL, nullptr); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1489 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1490 | std::unique_ptr<char[]> buffer(new char[data.nFileSizeLow]); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1491 | DWORD read_size; |
| 1492 | BOOL result; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1493 | result = |
| 1494 | ReadFile(file, buffer.get(), data.nFileSizeLow, &read_size, nullptr); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1495 | std::string content(buffer.get(), read_size); |
| 1496 | CloseHandle(file); |
| 1497 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1498 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1499 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1500 | EXPECT_EQ(content, d.data_received()); |
| 1501 | } |
| 1502 | |
| 1503 | // Clean the shortcut |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1504 | DeleteFile(base::as_wcstr(lnk_path)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1505 | } |
| 1506 | #endif // defined(OS_WIN) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1507 | |
| 1508 | // Custom URLRequestJobs for use with interceptor tests |
| 1509 | class RestartTestJob : public URLRequestTestJob { |
| 1510 | public: |
| 1511 | RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1512 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1513 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1514 | void StartAsync() override { this->NotifyRestartRequired(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1515 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1516 | ~RestartTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1517 | }; |
| 1518 | |
| 1519 | class CancelTestJob : public URLRequestTestJob { |
| 1520 | public: |
| 1521 | explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1522 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1523 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1524 | void StartAsync() override { request_->Cancel(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1525 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1526 | ~CancelTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1527 | }; |
| 1528 | |
| 1529 | class CancelThenRestartTestJob : public URLRequestTestJob { |
| 1530 | public: |
| 1531 | explicit CancelThenRestartTestJob(URLRequest* request, |
| 1532 | NetworkDelegate* network_delegate) |
| 1533 | : URLRequestTestJob(request, network_delegate, true) { |
| 1534 | } |
| 1535 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1536 | void StartAsync() override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1537 | request_->Cancel(); |
| 1538 | this->NotifyRestartRequired(); |
| 1539 | } |
| 1540 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1541 | ~CancelThenRestartTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1542 | }; |
| 1543 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1544 | // An Interceptor for use with interceptor tests. |
| 1545 | class MockURLRequestInterceptor : public URLRequestInterceptor { |
| 1546 | public: |
| 1547 | // Static getters for canned response header and data strings. |
| 1548 | static std::string ok_data() { |
| 1549 | return URLRequestTestJob::test_data_1(); |
| 1550 | } |
| 1551 | |
| 1552 | static std::string ok_headers() { |
| 1553 | return URLRequestTestJob::test_headers(); |
| 1554 | } |
| 1555 | |
| 1556 | static std::string redirect_data() { |
| 1557 | return std::string(); |
| 1558 | } |
| 1559 | |
| 1560 | static std::string redirect_headers() { |
| 1561 | return URLRequestTestJob::test_redirect_headers(); |
| 1562 | } |
| 1563 | |
| 1564 | static std::string error_data() { |
| 1565 | return std::string("ohhh nooooo mr. bill!"); |
| 1566 | } |
| 1567 | |
| 1568 | static std::string error_headers() { |
| 1569 | return URLRequestTestJob::test_error_headers(); |
| 1570 | } |
| 1571 | |
| 1572 | MockURLRequestInterceptor() |
| 1573 | : intercept_main_request_(false), restart_main_request_(false), |
| 1574 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 1575 | simulate_main_network_error_(false), |
| 1576 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 1577 | intercept_final_response_(false), cancel_final_request_(false), |
| 1578 | use_url_request_http_job_(false), |
| 1579 | did_intercept_main_(false), did_restart_main_(false), |
| 1580 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 1581 | did_simulate_error_main_(false), |
| 1582 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 1583 | did_intercept_final_(false), did_cancel_final_(false) { |
| 1584 | } |
| 1585 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1586 | ~MockURLRequestInterceptor() override = default; |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1587 | |
| 1588 | // URLRequestInterceptor implementation: |
| 1589 | URLRequestJob* MaybeInterceptRequest( |
| 1590 | URLRequest* request, |
| 1591 | NetworkDelegate* network_delegate) const override { |
| 1592 | if (restart_main_request_) { |
| 1593 | restart_main_request_ = false; |
| 1594 | did_restart_main_ = true; |
| 1595 | return new RestartTestJob(request, network_delegate); |
| 1596 | } |
| 1597 | if (cancel_main_request_) { |
| 1598 | cancel_main_request_ = false; |
| 1599 | did_cancel_main_ = true; |
| 1600 | return new CancelTestJob(request, network_delegate); |
| 1601 | } |
| 1602 | if (cancel_then_restart_main_request_) { |
| 1603 | cancel_then_restart_main_request_ = false; |
| 1604 | did_cancel_then_restart_main_ = true; |
| 1605 | return new CancelThenRestartTestJob(request, network_delegate); |
| 1606 | } |
| 1607 | if (simulate_main_network_error_) { |
| 1608 | simulate_main_network_error_ = false; |
| 1609 | did_simulate_error_main_ = true; |
| 1610 | if (use_url_request_http_job_) { |
| 1611 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1612 | } |
| 1613 | // This job will result in error since the requested URL is not one of the |
| 1614 | // URLs supported by these tests. |
| 1615 | return new URLRequestTestJob(request, network_delegate, true); |
| 1616 | } |
| 1617 | if (!intercept_main_request_) |
| 1618 | return nullptr; |
| 1619 | intercept_main_request_ = false; |
| 1620 | did_intercept_main_ = true; |
| 1621 | URLRequestTestJob* job = new URLRequestTestJob(request, |
| 1622 | network_delegate, |
| 1623 | main_headers_, |
| 1624 | main_data_, |
| 1625 | true); |
| 1626 | job->set_load_timing_info(main_request_load_timing_info_); |
| 1627 | return job; |
| 1628 | } |
| 1629 | |
| 1630 | URLRequestJob* MaybeInterceptRedirect(URLRequest* request, |
| 1631 | NetworkDelegate* network_delegate, |
| 1632 | const GURL& location) const override { |
| 1633 | if (cancel_redirect_request_) { |
| 1634 | cancel_redirect_request_ = false; |
| 1635 | did_cancel_redirect_ = true; |
| 1636 | return new CancelTestJob(request, network_delegate); |
| 1637 | } |
| 1638 | if (!intercept_redirect_) |
| 1639 | return nullptr; |
| 1640 | intercept_redirect_ = false; |
| 1641 | did_intercept_redirect_ = true; |
| 1642 | if (use_url_request_http_job_) { |
| 1643 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1644 | } |
| 1645 | return new URLRequestTestJob(request, |
| 1646 | network_delegate, |
| 1647 | redirect_headers_, |
| 1648 | redirect_data_, |
| 1649 | true); |
| 1650 | } |
| 1651 | |
| 1652 | URLRequestJob* MaybeInterceptResponse( |
| 1653 | URLRequest* request, |
| 1654 | NetworkDelegate* network_delegate) const override { |
| 1655 | if (cancel_final_request_) { |
| 1656 | cancel_final_request_ = false; |
| 1657 | did_cancel_final_ = true; |
| 1658 | return new CancelTestJob(request, network_delegate); |
| 1659 | } |
| 1660 | if (!intercept_final_response_) |
| 1661 | return nullptr; |
| 1662 | intercept_final_response_ = false; |
| 1663 | did_intercept_final_ = true; |
| 1664 | if (use_url_request_http_job_) { |
| 1665 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1666 | } |
| 1667 | return new URLRequestTestJob(request, |
| 1668 | network_delegate, |
| 1669 | final_headers_, |
| 1670 | final_data_, |
| 1671 | true); |
| 1672 | } |
| 1673 | |
| 1674 | void set_intercept_main_request(bool intercept_main_request) { |
| 1675 | intercept_main_request_ = intercept_main_request; |
| 1676 | } |
| 1677 | |
| 1678 | void set_main_headers(const std::string& main_headers) { |
| 1679 | main_headers_ = main_headers; |
| 1680 | } |
| 1681 | |
| 1682 | void set_main_data(const std::string& main_data) { |
| 1683 | main_data_ = main_data; |
| 1684 | } |
| 1685 | |
| 1686 | void set_main_request_load_timing_info( |
| 1687 | const LoadTimingInfo& main_request_load_timing_info) { |
| 1688 | main_request_load_timing_info_ = main_request_load_timing_info; |
| 1689 | } |
| 1690 | |
| 1691 | void set_restart_main_request(bool restart_main_request) { |
| 1692 | restart_main_request_ = restart_main_request; |
| 1693 | } |
| 1694 | |
| 1695 | void set_cancel_main_request(bool cancel_main_request) { |
| 1696 | cancel_main_request_ = cancel_main_request; |
| 1697 | } |
| 1698 | |
| 1699 | void set_cancel_then_restart_main_request( |
| 1700 | bool cancel_then_restart_main_request) { |
| 1701 | cancel_then_restart_main_request_ = cancel_then_restart_main_request; |
| 1702 | } |
| 1703 | |
| 1704 | void set_simulate_main_network_error(bool simulate_main_network_error) { |
| 1705 | simulate_main_network_error_ = simulate_main_network_error; |
| 1706 | } |
| 1707 | |
| 1708 | void set_intercept_redirect(bool intercept_redirect) { |
| 1709 | intercept_redirect_ = intercept_redirect; |
| 1710 | } |
| 1711 | |
| 1712 | void set_redirect_headers(const std::string& redirect_headers) { |
| 1713 | redirect_headers_ = redirect_headers; |
| 1714 | } |
| 1715 | |
| 1716 | void set_redirect_data(const std::string& redirect_data) { |
| 1717 | redirect_data_ = redirect_data; |
| 1718 | } |
| 1719 | |
| 1720 | void set_cancel_redirect_request(bool cancel_redirect_request) { |
| 1721 | cancel_redirect_request_ = cancel_redirect_request; |
| 1722 | } |
| 1723 | |
| 1724 | void set_intercept_final_response(bool intercept_final_response) { |
| 1725 | intercept_final_response_ = intercept_final_response; |
| 1726 | } |
| 1727 | |
| 1728 | void set_final_headers(const std::string& final_headers) { |
| 1729 | final_headers_ = final_headers; |
| 1730 | } |
| 1731 | |
| 1732 | void set_final_data(const std::string& final_data) { |
| 1733 | final_data_ = final_data; |
| 1734 | } |
| 1735 | |
| 1736 | void set_cancel_final_request(bool cancel_final_request) { |
| 1737 | cancel_final_request_ = cancel_final_request; |
| 1738 | } |
| 1739 | |
| 1740 | void set_use_url_request_http_job(bool use_url_request_http_job) { |
| 1741 | use_url_request_http_job_ = use_url_request_http_job; |
| 1742 | } |
| 1743 | |
| 1744 | bool did_intercept_main() const { |
| 1745 | return did_intercept_main_; |
| 1746 | } |
| 1747 | |
| 1748 | bool did_restart_main() const { |
| 1749 | return did_restart_main_; |
| 1750 | } |
| 1751 | |
| 1752 | bool did_cancel_main() const { |
| 1753 | return did_cancel_main_; |
| 1754 | } |
| 1755 | |
| 1756 | bool did_cancel_then_restart_main() const { |
| 1757 | return did_cancel_then_restart_main_; |
| 1758 | } |
| 1759 | |
| 1760 | bool did_simulate_error_main() const { |
| 1761 | return did_simulate_error_main_; |
| 1762 | } |
| 1763 | |
| 1764 | bool did_intercept_redirect() const { |
| 1765 | return did_intercept_redirect_; |
| 1766 | } |
| 1767 | |
| 1768 | bool did_cancel_redirect() const { |
| 1769 | return did_cancel_redirect_; |
| 1770 | } |
| 1771 | |
| 1772 | bool did_intercept_final() const { |
| 1773 | return did_intercept_final_; |
| 1774 | } |
| 1775 | |
| 1776 | bool did_cancel_final() const { |
| 1777 | return did_cancel_final_; |
| 1778 | } |
| 1779 | |
| 1780 | private: |
| 1781 | // Indicate whether to intercept the main request, and if so specify the |
| 1782 | // response to return and the LoadTimingInfo to use. |
| 1783 | mutable bool intercept_main_request_; |
| 1784 | mutable std::string main_headers_; |
| 1785 | mutable std::string main_data_; |
| 1786 | mutable LoadTimingInfo main_request_load_timing_info_; |
| 1787 | |
| 1788 | // These indicate actions that can be taken within MaybeInterceptRequest. |
| 1789 | mutable bool restart_main_request_; |
| 1790 | mutable bool cancel_main_request_; |
| 1791 | mutable bool cancel_then_restart_main_request_; |
| 1792 | mutable bool simulate_main_network_error_; |
| 1793 | |
| 1794 | // Indicate whether to intercept redirects, and if so specify the response to |
| 1795 | // return. |
| 1796 | mutable bool intercept_redirect_; |
| 1797 | mutable std::string redirect_headers_; |
| 1798 | mutable std::string redirect_data_; |
| 1799 | |
| 1800 | // Cancel the request within MaybeInterceptRedirect. |
| 1801 | mutable bool cancel_redirect_request_; |
| 1802 | |
| 1803 | // Indicate whether to intercept the final response, and if so specify the |
| 1804 | // response to return. |
| 1805 | mutable bool intercept_final_response_; |
| 1806 | mutable std::string final_headers_; |
| 1807 | mutable std::string final_data_; |
| 1808 | |
| 1809 | // Cancel the final request within MaybeInterceptResponse. |
| 1810 | mutable bool cancel_final_request_; |
| 1811 | |
| 1812 | // Instruct the interceptor to use a real URLRequestHTTPJob. |
| 1813 | mutable bool use_url_request_http_job_; |
| 1814 | |
| 1815 | // These indicate if the interceptor did something or not. |
| 1816 | mutable bool did_intercept_main_; |
| 1817 | mutable bool did_restart_main_; |
| 1818 | mutable bool did_cancel_main_; |
| 1819 | mutable bool did_cancel_then_restart_main_; |
| 1820 | mutable bool did_simulate_error_main_; |
| 1821 | mutable bool did_intercept_redirect_; |
| 1822 | mutable bool did_cancel_redirect_; |
| 1823 | mutable bool did_intercept_final_; |
| 1824 | mutable bool did_cancel_final_; |
| 1825 | }; |
| 1826 | |
| 1827 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
| 1828 | class URLRequestInterceptorTest : public URLRequestTest { |
| 1829 | public: |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1830 | URLRequestInterceptorTest() : URLRequestTest(), interceptor_(nullptr) {} |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1831 | |
| 1832 | ~URLRequestInterceptorTest() override { |
| 1833 | // URLRequestJobs may post clean-up tasks on destruction. |
| 1834 | base::RunLoop().RunUntilIdle(); |
| 1835 | } |
| 1836 | |
| 1837 | void SetUpFactory() override { |
| 1838 | interceptor_ = new MockURLRequestInterceptor(); |
| 1839 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1840 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | MockURLRequestInterceptor* interceptor() const { |
| 1844 | return interceptor_; |
| 1845 | } |
| 1846 | |
| 1847 | private: |
| 1848 | MockURLRequestInterceptor* interceptor_; |
| 1849 | }; |
| 1850 | |
| 1851 | TEST_F(URLRequestInterceptorTest, Intercept) { |
| 1852 | // Intercept the main request and respond with a simple response. |
| 1853 | interceptor()->set_intercept_main_request(true); |
| 1854 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1855 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1856 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1857 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1858 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1859 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1860 | base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); |
| 1861 | base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); |
| 1862 | base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); |
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 1863 | req->SetUserData(&user_data0, base::WrapUnique(user_data0)); |
avi | 0d99147 | 2017-04-27 07:04:04 | [diff] [blame] | 1864 | req->SetUserData(&user_data1, base::WrapUnique(user_data1)); |
| 1865 | req->SetUserData(&user_data2, base::WrapUnique(user_data2)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1866 | req->set_method("GET"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1867 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1868 | d.RunUntilComplete(); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1869 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1870 | // Make sure we can retrieve our specific user data. |
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 1871 | EXPECT_EQ(user_data0, req->GetUserData(&user_data0)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1872 | EXPECT_EQ(user_data1, req->GetUserData(&user_data1)); |
| 1873 | EXPECT_EQ(user_data2, req->GetUserData(&user_data2)); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1874 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1875 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1876 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1877 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1878 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1879 | EXPECT_EQ(1, d.response_started_count()); |
| 1880 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1881 | } |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1882 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1883 | TEST_F(URLRequestInterceptorTest, InterceptRedirect) { |
| 1884 | // Intercept the main request and respond with a redirect. |
| 1885 | interceptor()->set_intercept_main_request(true); |
| 1886 | interceptor()->set_main_headers( |
| 1887 | MockURLRequestInterceptor::redirect_headers()); |
| 1888 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 1889 | |
| 1890 | // Intercept that redirect and respond with a final OK response. |
| 1891 | interceptor()->set_intercept_redirect(true); |
| 1892 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 1893 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 1894 | |
| 1895 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1896 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1897 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1898 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1899 | req->set_method("GET"); |
| 1900 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1901 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1902 | |
| 1903 | // Check that the interceptor got called as expected. |
| 1904 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1905 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 1906 | |
| 1907 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1908 | int status = d.request_status(); |
| 1909 | EXPECT_EQ(OK, status); |
| 1910 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1911 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1912 | |
| 1913 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1914 | EXPECT_EQ(1, d.response_started_count()); |
| 1915 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1916 | } |
| 1917 | |
| 1918 | TEST_F(URLRequestInterceptorTest, InterceptServerError) { |
| 1919 | // Intercept the main request to generate a server error response. |
| 1920 | interceptor()->set_intercept_main_request(true); |
| 1921 | interceptor()->set_main_headers(MockURLRequestInterceptor::error_headers()); |
| 1922 | interceptor()->set_main_data(MockURLRequestInterceptor::error_data()); |
| 1923 | |
| 1924 | // Intercept that error and respond with an OK response. |
| 1925 | interceptor()->set_intercept_final_response(true); |
| 1926 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1927 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1928 | |
| 1929 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1930 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1931 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1932 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1933 | req->set_method("GET"); |
| 1934 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1935 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1936 | |
| 1937 | // Check that the interceptor got called as expected. |
| 1938 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1939 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1940 | |
| 1941 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1942 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1943 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1944 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1945 | EXPECT_EQ(1, d.response_started_count()); |
| 1946 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1947 | } |
| 1948 | |
| 1949 | TEST_F(URLRequestInterceptorTest, InterceptNetworkError) { |
| 1950 | // Intercept the main request to simulate a network error. |
| 1951 | interceptor()->set_simulate_main_network_error(true); |
| 1952 | |
| 1953 | // Intercept that error and respond with an OK response. |
| 1954 | interceptor()->set_intercept_final_response(true); |
| 1955 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1956 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1957 | |
| 1958 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1959 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1960 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1961 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1962 | req->set_method("GET"); |
| 1963 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1964 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1965 | |
| 1966 | // Check that the interceptor got called as expected. |
| 1967 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 1968 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1969 | |
| 1970 | // Check that we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1971 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1972 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1973 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1974 | EXPECT_EQ(1, d.response_started_count()); |
| 1975 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1976 | } |
| 1977 | |
| 1978 | TEST_F(URLRequestInterceptorTest, InterceptRestartRequired) { |
| 1979 | // Restart the main request. |
| 1980 | interceptor()->set_restart_main_request(true); |
| 1981 | |
| 1982 | // then intercept the new main request and respond with an OK response |
| 1983 | interceptor()->set_intercept_main_request(true); |
| 1984 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1985 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
| 1986 | |
| 1987 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1988 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1989 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1990 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1991 | req->set_method("GET"); |
| 1992 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1993 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1994 | |
| 1995 | // Check that the interceptor got called as expected. |
| 1996 | EXPECT_TRUE(interceptor()->did_restart_main()); |
| 1997 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1998 | |
| 1999 | // Check that we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2000 | int status = d.request_status(); |
| 2001 | EXPECT_EQ(OK, status); |
| 2002 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2003 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 2004 | |
| 2005 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 2006 | EXPECT_EQ(1, d.response_started_count()); |
| 2007 | EXPECT_EQ(0, d.received_redirect_count()); |
| 2008 | } |
| 2009 | |
| 2010 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelMain) { |
| 2011 | // Intercept the main request and cancel from within the restarted job. |
| 2012 | interceptor()->set_cancel_main_request(true); |
| 2013 | |
| 2014 | // Set up to intercept the final response and override it with an OK response. |
| 2015 | interceptor()->set_intercept_final_response(true); |
| 2016 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2017 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 2018 | |
| 2019 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2020 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2021 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2022 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2023 | req->set_method("GET"); |
| 2024 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2025 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2026 | |
| 2027 | // Check that the interceptor got called as expected. |
| 2028 | EXPECT_TRUE(interceptor()->did_cancel_main()); |
| 2029 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2030 | |
| 2031 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2032 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelRedirect) { |
| 2036 | // Intercept the main request and respond with a redirect. |
| 2037 | interceptor()->set_intercept_main_request(true); |
| 2038 | interceptor()->set_main_headers( |
| 2039 | MockURLRequestInterceptor::redirect_headers()); |
| 2040 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 2041 | |
| 2042 | // Intercept the redirect and cancel from within that job. |
| 2043 | interceptor()->set_cancel_redirect_request(true); |
| 2044 | |
| 2045 | // Set up to intercept the final response and override it with an OK response. |
| 2046 | interceptor()->set_intercept_final_response(true); |
| 2047 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2048 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 2049 | |
| 2050 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2051 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2052 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2053 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2054 | req->set_method("GET"); |
| 2055 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2056 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2057 | |
| 2058 | // Check that the interceptor got called as expected. |
| 2059 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 2060 | EXPECT_TRUE(interceptor()->did_cancel_redirect()); |
| 2061 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2062 | |
| 2063 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2064 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelFinal) { |
| 2068 | // Intercept the main request to simulate a network error. |
| 2069 | interceptor()->set_simulate_main_network_error(true); |
| 2070 | |
| 2071 | // Set up to intercept final the response and cancel from within that job. |
| 2072 | interceptor()->set_cancel_final_request(true); |
| 2073 | |
| 2074 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2075 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2076 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2077 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2078 | req->set_method("GET"); |
| 2079 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2080 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2081 | |
| 2082 | // Check that the interceptor got called as expected. |
| 2083 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 2084 | EXPECT_TRUE(interceptor()->did_cancel_final()); |
| 2085 | |
| 2086 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2087 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelInRestart) { |
| 2091 | // Intercept the main request and cancel then restart from within that job. |
| 2092 | interceptor()->set_cancel_then_restart_main_request(true); |
| 2093 | |
| 2094 | // Set up to intercept the final response and override it with an OK response. |
| 2095 | interceptor()->set_intercept_final_response(true); |
bengr | b50d631e | 2014-11-17 22:50:50 | [diff] [blame] | 2096 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2097 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2098 | |
| 2099 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2100 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2101 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2102 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2103 | req->set_method("GET"); |
| 2104 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2105 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2106 | |
| 2107 | // Check that the interceptor got called as expected. |
| 2108 | EXPECT_TRUE(interceptor()->did_cancel_then_restart_main()); |
| 2109 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2110 | |
| 2111 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2112 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not |
| 2116 | // reused. |connect_time_flags| is used to indicate if there should be dns |
| 2117 | // or SSL times, and |used_proxy| is used for proxy times. |
| 2118 | LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now, |
| 2119 | int connect_time_flags, |
| 2120 | bool used_proxy) { |
| 2121 | LoadTimingInfo load_timing; |
| 2122 | load_timing.socket_log_id = 1; |
| 2123 | |
| 2124 | if (used_proxy) { |
| 2125 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 2126 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 2127 | } |
| 2128 | |
| 2129 | LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing; |
| 2130 | if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) { |
| 2131 | connect_timing.dns_start = now + base::TimeDelta::FromDays(3); |
| 2132 | connect_timing.dns_end = now + base::TimeDelta::FromDays(4); |
| 2133 | } |
| 2134 | connect_timing.connect_start = now + base::TimeDelta::FromDays(5); |
| 2135 | if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) { |
| 2136 | connect_timing.ssl_start = now + base::TimeDelta::FromDays(6); |
| 2137 | connect_timing.ssl_end = now + base::TimeDelta::FromDays(7); |
| 2138 | } |
| 2139 | connect_timing.connect_end = now + base::TimeDelta::FromDays(8); |
| 2140 | |
| 2141 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 2142 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 2143 | load_timing.receive_headers_start = now + base::TimeDelta::FromDays(11); |
| 2144 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(12); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2145 | return load_timing; |
| 2146 | } |
| 2147 | |
| 2148 | // Same as above, but in the case of a reused socket. |
| 2149 | LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now, |
| 2150 | bool used_proxy) { |
| 2151 | LoadTimingInfo load_timing; |
| 2152 | load_timing.socket_log_id = 1; |
| 2153 | load_timing.socket_reused = true; |
| 2154 | |
| 2155 | if (used_proxy) { |
| 2156 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 2157 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 2158 | } |
| 2159 | |
| 2160 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 2161 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 2162 | load_timing.receive_headers_start = now + base::TimeDelta::FromDays(11); |
| 2163 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(12); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2164 | return load_timing; |
| 2165 | } |
| 2166 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2167 | LoadTimingInfo RunURLRequestInterceptorLoadTimingTest( |
| 2168 | const LoadTimingInfo& job_load_timing, |
| 2169 | const URLRequestContext& context, |
| 2170 | MockURLRequestInterceptor* interceptor) { |
| 2171 | interceptor->set_intercept_main_request(true); |
| 2172 | interceptor->set_main_request_load_timing_info(job_load_timing); |
| 2173 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2174 | std::unique_ptr<URLRequest> req( |
| 2175 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 2176 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2177 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2178 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2179 | |
| 2180 | LoadTimingInfo resulting_load_timing; |
| 2181 | req->GetLoadTimingInfo(&resulting_load_timing); |
| 2182 | |
| 2183 | // None of these should be modified by the URLRequest. |
| 2184 | EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); |
| 2185 | EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); |
| 2186 | EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); |
| 2187 | EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 2188 | EXPECT_EQ(job_load_timing.receive_headers_start, |
| 2189 | resulting_load_timing.receive_headers_start); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2190 | EXPECT_EQ(job_load_timing.receive_headers_end, |
| 2191 | resulting_load_timing.receive_headers_end); |
caseq | e8340bc9 | 2016-04-20 00:02:57 | [diff] [blame] | 2192 | EXPECT_EQ(job_load_timing.push_start, resulting_load_timing.push_start); |
| 2193 | EXPECT_EQ(job_load_timing.push_end, resulting_load_timing.push_end); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2194 | |
| 2195 | return resulting_load_timing; |
| 2196 | } |
| 2197 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2198 | // Basic test that the intercept + load timing tests work. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2199 | TEST_F(URLRequestInterceptorTest, InterceptLoadTiming) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2200 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2201 | LoadTimingInfo job_load_timing = |
| 2202 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); |
| 2203 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2204 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2205 | RunURLRequestInterceptorLoadTimingTest( |
| 2206 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2207 | |
| 2208 | // Nothing should have been changed by the URLRequest. |
| 2209 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 2210 | load_timing_result.proxy_resolve_start); |
| 2211 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 2212 | load_timing_result.proxy_resolve_end); |
| 2213 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 2214 | load_timing_result.connect_timing.dns_start); |
| 2215 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 2216 | load_timing_result.connect_timing.dns_end); |
| 2217 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 2218 | load_timing_result.connect_timing.connect_start); |
| 2219 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 2220 | load_timing_result.connect_timing.connect_end); |
| 2221 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 2222 | load_timing_result.connect_timing.ssl_start); |
| 2223 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 2224 | load_timing_result.connect_timing.ssl_end); |
| 2225 | |
| 2226 | // Redundant sanity check. |
| 2227 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2228 | } |
| 2229 | |
| 2230 | // Another basic test, with proxy and SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2231 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2232 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2233 | LoadTimingInfo job_load_timing = |
| 2234 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); |
| 2235 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2236 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2237 | RunURLRequestInterceptorLoadTimingTest( |
| 2238 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2239 | |
| 2240 | // Nothing should have been changed by the URLRequest. |
| 2241 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 2242 | load_timing_result.proxy_resolve_start); |
| 2243 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 2244 | load_timing_result.proxy_resolve_end); |
| 2245 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 2246 | load_timing_result.connect_timing.dns_start); |
| 2247 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 2248 | load_timing_result.connect_timing.dns_end); |
| 2249 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 2250 | load_timing_result.connect_timing.connect_start); |
| 2251 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 2252 | load_timing_result.connect_timing.connect_end); |
| 2253 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 2254 | load_timing_result.connect_timing.ssl_start); |
| 2255 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 2256 | load_timing_result.connect_timing.ssl_end); |
| 2257 | |
| 2258 | // Redundant sanity check. |
| 2259 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2260 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 2261 | } |
| 2262 | |
| 2263 | // Make sure that URLRequest correctly adjusts proxy times when they're before |
| 2264 | // |request_start|, due to already having a connected socket. This happens in |
[email protected] | cf4cae3 | 2014-05-27 00:39:10 | [diff] [blame] | 2265 | // the case of reusing a SPDY session. The connected socket is not considered |
| 2266 | // reused in this test (May be a preconnect). |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2267 | // |
| 2268 | // 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] | 2269 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyProxyResolution) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2270 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2271 | LoadTimingInfo job_load_timing = |
| 2272 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); |
| 2273 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); |
| 2274 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); |
| 2275 | job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); |
| 2276 | job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); |
| 2277 | job_load_timing.connect_timing.connect_start = |
| 2278 | now - base::TimeDelta::FromDays(2); |
| 2279 | job_load_timing.connect_timing.connect_end = |
| 2280 | now - base::TimeDelta::FromDays(1); |
| 2281 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2282 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2283 | RunURLRequestInterceptorLoadTimingTest( |
| 2284 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2285 | |
| 2286 | // Proxy times, connect times, and DNS times should all be replaced with |
| 2287 | // request_start. |
| 2288 | EXPECT_EQ(load_timing_result.request_start, |
| 2289 | load_timing_result.proxy_resolve_start); |
| 2290 | EXPECT_EQ(load_timing_result.request_start, |
| 2291 | load_timing_result.proxy_resolve_end); |
| 2292 | EXPECT_EQ(load_timing_result.request_start, |
| 2293 | load_timing_result.connect_timing.dns_start); |
| 2294 | EXPECT_EQ(load_timing_result.request_start, |
| 2295 | load_timing_result.connect_timing.dns_end); |
| 2296 | EXPECT_EQ(load_timing_result.request_start, |
| 2297 | load_timing_result.connect_timing.connect_start); |
| 2298 | EXPECT_EQ(load_timing_result.request_start, |
| 2299 | load_timing_result.connect_timing.connect_end); |
| 2300 | |
| 2301 | // Other times should have been left null. |
| 2302 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2303 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 2304 | } |
| 2305 | |
| 2306 | // Same as above, but in the reused case. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2307 | TEST_F(URLRequestInterceptorTest, |
| 2308 | InterceptLoadTimingEarlyProxyResolutionReused) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2309 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2310 | LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); |
| 2311 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); |
| 2312 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); |
| 2313 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2314 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2315 | RunURLRequestInterceptorLoadTimingTest( |
| 2316 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2317 | |
| 2318 | // Proxy times and connect times should all be replaced with request_start. |
| 2319 | EXPECT_EQ(load_timing_result.request_start, |
| 2320 | load_timing_result.proxy_resolve_start); |
| 2321 | EXPECT_EQ(load_timing_result.request_start, |
| 2322 | load_timing_result.proxy_resolve_end); |
| 2323 | |
| 2324 | // Other times should have been left null. |
| 2325 | TestLoadTimingReusedWithProxy(load_timing_result); |
| 2326 | } |
| 2327 | |
| 2328 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2329 | // |request_start|, due to reusing a connected socket. The connected socket is |
| 2330 | // not considered reused in this test (May be a preconnect). |
| 2331 | // |
| 2332 | // To mix things up, the request has SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2333 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnect) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2334 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2335 | LoadTimingInfo job_load_timing = |
| 2336 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); |
| 2337 | job_load_timing.connect_timing.connect_start = |
| 2338 | now - base::TimeDelta::FromDays(1); |
| 2339 | job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); |
| 2340 | job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); |
| 2341 | job_load_timing.connect_timing.connect_end = |
| 2342 | now - base::TimeDelta::FromDays(4); |
| 2343 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2344 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2345 | RunURLRequestInterceptorLoadTimingTest( |
| 2346 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2347 | |
| 2348 | // Connect times, and SSL times should be replaced with request_start. |
| 2349 | EXPECT_EQ(load_timing_result.request_start, |
| 2350 | load_timing_result.connect_timing.connect_start); |
| 2351 | EXPECT_EQ(load_timing_result.request_start, |
| 2352 | load_timing_result.connect_timing.ssl_start); |
| 2353 | EXPECT_EQ(load_timing_result.request_start, |
| 2354 | load_timing_result.connect_timing.ssl_end); |
| 2355 | EXPECT_EQ(load_timing_result.request_start, |
| 2356 | load_timing_result.connect_timing.connect_end); |
| 2357 | |
| 2358 | // Other times should have been left null. |
| 2359 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); |
| 2360 | } |
| 2361 | |
| 2362 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2363 | // |request_start|, due to reusing a connected socket in the case that there |
| 2364 | // are also proxy times. The connected socket is not considered reused in this |
| 2365 | // test (May be a preconnect). |
| 2366 | // |
| 2367 | // In this test, there are no SSL or DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2368 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnectWithProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2369 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2370 | LoadTimingInfo job_load_timing = |
| 2371 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); |
| 2372 | job_load_timing.connect_timing.connect_start = |
| 2373 | now - base::TimeDelta::FromDays(1); |
| 2374 | job_load_timing.connect_timing.connect_end = |
| 2375 | now - base::TimeDelta::FromDays(2); |
| 2376 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2377 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2378 | RunURLRequestInterceptorLoadTimingTest( |
| 2379 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2380 | |
| 2381 | // Connect times should be replaced with proxy_resolve_end. |
| 2382 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2383 | load_timing_result.connect_timing.connect_start); |
| 2384 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2385 | load_timing_result.connect_timing.connect_end); |
| 2386 | |
| 2387 | // Other times should have been left null. |
| 2388 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2389 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 2390 | } |
| 2391 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2392 | // Check that two different URL requests have different identifiers. |
| 2393 | TEST_F(URLRequestTest, Identifiers) { |
| 2394 | TestDelegate d; |
| 2395 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2396 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2397 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2398 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2399 | std::unique_ptr<URLRequest> other_req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2400 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2401 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2402 | |
mmenke | 19378d2 | 2014-09-09 04:12:59 | [diff] [blame] | 2403 | ASSERT_NE(req->identifier(), other_req->identifier()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2404 | } |
| 2405 | |
blundell | b8163592f | 2015-12-16 14:22:42 | [diff] [blame] | 2406 | #if defined(OS_IOS) |
| 2407 | // TODO(droger): Check that a failure to connect to the proxy is reported to |
| 2408 | // the network delegate. crbug.com/496743 |
| 2409 | #define MAYBE_NetworkDelegateProxyError DISABLED_NetworkDelegateProxyError |
| 2410 | #else |
| 2411 | #define MAYBE_NetworkDelegateProxyError NetworkDelegateProxyError |
| 2412 | #endif |
| 2413 | TEST_F(URLRequestTest, MAYBE_NetworkDelegateProxyError) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2414 | MockHostResolver host_resolver; |
| 2415 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 2416 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2417 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2418 | TestURLRequestContextWithProxy context("myproxy:70", &network_delegate); |
| 2419 | |
| 2420 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2421 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2422 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2423 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2424 | req->set_method("GET"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2425 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2426 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2427 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2428 | |
| 2429 | // Check we see a failed request. |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 2430 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 2431 | EXPECT_FALSE(req->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2432 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2433 | |
| 2434 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2435 | EXPECT_THAT(network_delegate.last_error(), |
| 2436 | IsError(ERR_PROXY_CONNECTION_FAILED)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2437 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2438 | } |
| 2439 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2440 | // Make sure that NetworkDelegate::NotifyCompleted is called if |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2441 | // content is empty. |
| 2442 | TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) { |
| 2443 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2444 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2445 | GURL("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2446 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2447 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2448 | EXPECT_EQ("", d.data_received()); |
| 2449 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 2450 | } |
| 2451 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2452 | // Make sure that SetPriority actually sets the URLRequest's priority |
| 2453 | // correctly, both before and after start. |
| 2454 | TEST_F(URLRequestTest, SetPriorityBasic) { |
| 2455 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2456 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2457 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2458 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2459 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2460 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2461 | req->SetPriority(LOW); |
| 2462 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2463 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2464 | req->Start(); |
| 2465 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2466 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2467 | req->SetPriority(MEDIUM); |
| 2468 | EXPECT_EQ(MEDIUM, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2469 | } |
| 2470 | |
| 2471 | // Make sure that URLRequest calls SetPriority on a job before calling |
| 2472 | // Start on it. |
| 2473 | TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) { |
| 2474 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2475 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2476 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2477 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2478 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2479 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2480 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2481 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2482 | req.get(), &default_network_delegate_, &job_priority)); |
| 2483 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
| 2484 | EXPECT_EQ(DEFAULT_PRIORITY, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2485 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2486 | req->SetPriority(LOW); |
[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->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2489 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2490 | } |
| 2491 | |
| 2492 | // Make sure that URLRequest passes on its priority updates to its |
| 2493 | // job. |
| 2494 | TEST_F(URLRequestTest, SetJobPriority) { |
| 2495 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2496 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2497 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2498 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2499 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2500 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2501 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2502 | req.get(), &default_network_delegate_, &job_priority)); |
| 2503 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2504 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2505 | req->SetPriority(LOW); |
| 2506 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2507 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2508 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2509 | req->SetPriority(MEDIUM); |
| 2510 | EXPECT_EQ(MEDIUM, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2511 | EXPECT_EQ(MEDIUM, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2512 | } |
| 2513 | |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2514 | // Setting the IGNORE_LIMITS load flag should be okay if the priority |
| 2515 | // is MAXIMUM_PRIORITY. |
| 2516 | TEST_F(URLRequestTest, PriorityIgnoreLimits) { |
| 2517 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2518 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2519 | GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY, &d, |
| 2520 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2521 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2522 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2523 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2524 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2525 | req.get(), &default_network_delegate_, &job_priority)); |
| 2526 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2527 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2528 | req->SetLoadFlags(LOAD_IGNORE_LIMITS); |
| 2529 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2530 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2531 | req->SetPriority(MAXIMUM_PRIORITY); |
| 2532 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2533 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2534 | req->Start(); |
| 2535 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2536 | EXPECT_EQ(MAXIMUM_PRIORITY, job_priority); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2537 | } |
| 2538 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2539 | namespace { |
| 2540 | |
| 2541 | // Less verbose way of running a simple testserver for the tests below. |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2542 | class HttpTestServer : public EmbeddedTestServer { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2543 | public: |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2544 | explicit HttpTestServer(const base::FilePath& document_root) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2545 | AddDefaultHandlers(document_root); |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2546 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2547 | |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2548 | HttpTestServer() { AddDefaultHandlers(base::FilePath()); } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2549 | }; |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2550 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2551 | } // namespace |
| 2552 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2553 | TEST_F(URLRequestTest, DelayedCookieCallback) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2554 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2555 | ASSERT_TRUE(test_server.Start()); |
| 2556 | |
| 2557 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2558 | std::unique_ptr<DelayedCookieMonster> delayed_cm(new DelayedCookieMonster()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2559 | context.set_cookie_store(delayed_cm.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2560 | |
| 2561 | // Set up a cookie. |
| 2562 | { |
| 2563 | TestNetworkDelegate network_delegate; |
| 2564 | context.set_network_delegate(&network_delegate); |
| 2565 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2566 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2567 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2568 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2569 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2570 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2571 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2572 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2573 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2574 | } |
| 2575 | |
| 2576 | // Verify that the cookie is set. |
| 2577 | { |
| 2578 | TestNetworkDelegate network_delegate; |
| 2579 | context.set_network_delegate(&network_delegate); |
| 2580 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2581 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2582 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2583 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2584 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2585 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2586 | |
| 2587 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2588 | != std::string::npos); |
| 2589 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2590 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2591 | } |
| 2592 | } |
| 2593 | |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 2594 | class FilteringTestLayeredNetworkDelegate : public LayeredNetworkDelegate { |
| 2595 | public: |
| 2596 | FilteringTestLayeredNetworkDelegate( |
| 2597 | std::unique_ptr<NetworkDelegate> network_delegate) |
| 2598 | : LayeredNetworkDelegate(std::move((network_delegate))), |
| 2599 | set_cookie_called_count_(0), |
| 2600 | blocked_set_cookie_count_(0) {} |
| 2601 | ~FilteringTestLayeredNetworkDelegate() override = default; |
| 2602 | |
| 2603 | bool OnCanSetCookieInternal(const URLRequest& request, |
| 2604 | const net::CanonicalCookie& cookie, |
| 2605 | CookieOptions* options, |
| 2606 | bool allowed_from_caller) override { |
| 2607 | // Filter out cookies with the same name as |cookie_name_filter_| and |
| 2608 | // combine with |allowed_from_caller|. |
| 2609 | bool allowed = |
| 2610 | allowed_from_caller && !(cookie.Name() == cookie_name_filter_); |
| 2611 | |
| 2612 | ++set_cookie_called_count_; |
| 2613 | |
| 2614 | if (!allowed) |
| 2615 | ++blocked_set_cookie_count_; |
| 2616 | |
| 2617 | return allowed; |
| 2618 | } |
| 2619 | |
| 2620 | void SetCookieFilter(std::string filter) { |
| 2621 | cookie_name_filter_ = std::move(filter); |
| 2622 | } |
| 2623 | |
| 2624 | int set_cookie_called_count() { return set_cookie_called_count_; } |
| 2625 | |
| 2626 | int blocked_set_cookie_count() { return blocked_set_cookie_count_; } |
| 2627 | |
| 2628 | void ResetSetCookieCalledCount() { set_cookie_called_count_ = 0; } |
| 2629 | |
| 2630 | void ResetBlockedSetCookieCount() { blocked_set_cookie_count_ = 0; } |
| 2631 | |
| 2632 | private: |
| 2633 | std::string cookie_name_filter_; |
| 2634 | int set_cookie_called_count_; |
| 2635 | int blocked_set_cookie_count_; |
| 2636 | }; |
| 2637 | |
| 2638 | TEST_F(URLRequestTest, DelayedCookieCallbackAsync) { |
| 2639 | HttpTestServer test_server; |
| 2640 | ASSERT_TRUE(test_server.Start()); |
| 2641 | |
| 2642 | TestURLRequestContext async_context; |
| 2643 | std::unique_ptr<DelayedCookieMonster> delayed_cm = |
| 2644 | std::make_unique<DelayedCookieMonster>(); |
| 2645 | async_context.set_cookie_store(delayed_cm.get()); |
| 2646 | FilteringTestLayeredNetworkDelegate async_filter_network_delegate( |
| 2647 | std::make_unique<TestNetworkDelegate>()); |
| 2648 | async_filter_network_delegate.SetCookieFilter("CookieBlockedOnCanGetCookie"); |
| 2649 | async_context.set_network_delegate(&async_filter_network_delegate); |
| 2650 | TestDelegate async_delegate; |
| 2651 | |
| 2652 | TestURLRequestContext sync_context; |
| 2653 | std::unique_ptr<CookieMonster> cm = |
Nick Harper | 57142b1c | 2019-03-14 21:03:59 | [diff] [blame] | 2654 | std::make_unique<CookieMonster>(nullptr, nullptr); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 2655 | sync_context.set_cookie_store(cm.get()); |
| 2656 | FilteringTestLayeredNetworkDelegate sync_filter_network_delegate( |
| 2657 | std::make_unique<TestNetworkDelegate>()); |
| 2658 | sync_filter_network_delegate.SetCookieFilter("CookieBlockedOnCanGetCookie"); |
| 2659 | sync_context.set_network_delegate(&sync_filter_network_delegate); |
| 2660 | TestDelegate sync_delegate; |
| 2661 | |
| 2662 | // Add a secure cookie so we can try to set an insecure cookie and have |
| 2663 | // SetCanonicalCookie (and therefore SetCookieWithOptions) fail. |
| 2664 | GURL::Replacements replace_scheme; |
| 2665 | replace_scheme.SetSchemeStr("https"); |
| 2666 | GURL url = test_server.base_url().ReplaceComponents(replace_scheme); |
| 2667 | |
| 2668 | delayed_cm->SetCookieWithOptionsAsync(url, "AlreadySetCookie=1;Secure", |
| 2669 | CookieOptions(), |
| 2670 | CookieStore::SetCookiesCallback()); |
| 2671 | cm->SetCookieWithOptionsAsync(url, "AlreadySetCookie=1;Secure", |
| 2672 | CookieOptions(), |
| 2673 | CookieStore::SetCookiesCallback()); |
| 2674 | |
| 2675 | std::vector<std::string> cookie_lines( |
| 2676 | {// Fails on CanonicalCookie::Create for trying to create a secure cookie |
| 2677 | // on an insecure host. |
| 2678 | "CookieNotSet=1;Secure", |
| 2679 | // Fail in FilteringTestLayeredNetworkDelegate::CanGetCookie. |
| 2680 | "CookieBlockedOnCanGetCookie=1", |
| 2681 | // Fails in SetCanonicalCookie for trying to overwrite a secure cookie |
| 2682 | // with an insecure cookie. |
| 2683 | "AlreadySetCookie=1", |
| 2684 | // Succeeds and added cookie to store. Delayed (which makes the callback |
| 2685 | // run asynchronously) in DelayedCookieMonster. |
| 2686 | "CookieSet=1"}); |
| 2687 | |
| 2688 | for (auto first_cookie_line : cookie_lines) { |
| 2689 | for (auto second_cookie_line : cookie_lines) { |
| 2690 | // Run with the delayed cookie monster. |
| 2691 | std::unique_ptr<URLRequest> request = async_context.CreateRequest( |
| 2692 | test_server.GetURL("/set-cookie?" + first_cookie_line + "&" + |
| 2693 | second_cookie_line), |
| 2694 | DEFAULT_PRIORITY, &async_delegate, TRAFFIC_ANNOTATION_FOR_TESTS); |
| 2695 | |
| 2696 | request->Start(); |
| 2697 | async_delegate.RunUntilComplete(); |
| 2698 | EXPECT_THAT(async_delegate.request_status(), IsOk()); |
| 2699 | |
| 2700 | // Run with the regular cookie monster. |
| 2701 | request = sync_context.CreateRequest( |
| 2702 | test_server.GetURL("/set-cookie?" + first_cookie_line + "&" + |
| 2703 | second_cookie_line), |
| 2704 | DEFAULT_PRIORITY, &sync_delegate, TRAFFIC_ANNOTATION_FOR_TESTS); |
| 2705 | |
| 2706 | request->Start(); |
| 2707 | sync_delegate.RunUntilComplete(); |
| 2708 | EXPECT_THAT(sync_delegate.request_status(), IsOk()); |
| 2709 | |
| 2710 | int expected_set_cookie_count = 0; |
| 2711 | int expected_blocked_cookie_count = 0; |
| 2712 | |
| 2713 | if (first_cookie_line != "CookieNotSet=1;Secure") |
| 2714 | ++expected_set_cookie_count; |
| 2715 | if (second_cookie_line != "CookieNotSet=1;Secure") |
| 2716 | ++expected_set_cookie_count; |
| 2717 | |
| 2718 | if (first_cookie_line == "CookieBlockedOnCanGetCookie=1") |
| 2719 | ++expected_blocked_cookie_count; |
| 2720 | if (second_cookie_line == "CookieBlockedOnCanGetCookie=1") |
| 2721 | ++expected_blocked_cookie_count; |
| 2722 | |
| 2723 | EXPECT_EQ(expected_set_cookie_count, |
| 2724 | async_filter_network_delegate.set_cookie_called_count()); |
| 2725 | EXPECT_EQ(expected_blocked_cookie_count, |
| 2726 | async_filter_network_delegate.blocked_set_cookie_count()); |
| 2727 | |
| 2728 | EXPECT_EQ(expected_set_cookie_count, |
| 2729 | sync_filter_network_delegate.set_cookie_called_count()); |
| 2730 | EXPECT_EQ(expected_blocked_cookie_count, |
| 2731 | sync_filter_network_delegate.blocked_set_cookie_count()); |
| 2732 | |
| 2733 | async_filter_network_delegate.ResetSetCookieCalledCount(); |
| 2734 | async_filter_network_delegate.ResetBlockedSetCookieCount(); |
| 2735 | |
| 2736 | sync_filter_network_delegate.ResetSetCookieCalledCount(); |
| 2737 | sync_filter_network_delegate.ResetBlockedSetCookieCount(); |
| 2738 | } |
| 2739 | } |
| 2740 | } |
| 2741 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2742 | TEST_F(URLRequestTest, DoNotSendCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2743 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2744 | ASSERT_TRUE(test_server.Start()); |
| 2745 | |
| 2746 | // Set up a cookie. |
| 2747 | { |
| 2748 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2749 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2750 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2751 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2752 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2753 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2754 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2755 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2756 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2757 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2758 | } |
| 2759 | |
| 2760 | // Verify that the cookie is set. |
| 2761 | { |
| 2762 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2763 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2764 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2765 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2766 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2767 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2768 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2769 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2770 | |
| 2771 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2772 | != std::string::npos); |
| 2773 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2774 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2775 | } |
| 2776 | |
| 2777 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 2778 | { |
| 2779 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2780 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2781 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2782 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2783 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2784 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2785 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
| 2786 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2787 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2788 | |
| 2789 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2790 | == std::string::npos); |
| 2791 | |
| 2792 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
| 2793 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2794 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2795 | } |
| 2796 | } |
| 2797 | |
| 2798 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2799 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2800 | ASSERT_TRUE(test_server.Start()); |
| 2801 | |
| 2802 | // Set up a cookie. |
| 2803 | { |
| 2804 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2805 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2806 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2807 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2808 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2809 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2810 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2811 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2812 | |
| 2813 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2814 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2815 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2816 | } |
| 2817 | |
| 2818 | // Try to set-up another cookie and update the previous cookie. |
| 2819 | { |
| 2820 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2821 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2822 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2823 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2824 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2825 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2826 | req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES); |
| 2827 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2828 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2829 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2830 | |
| 2831 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
| 2832 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2833 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2834 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2835 | } |
| 2836 | |
| 2837 | // Verify the cookies weren't saved or updated. |
| 2838 | { |
| 2839 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2840 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2841 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2842 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2843 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2844 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2845 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2846 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2847 | |
| 2848 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2849 | == std::string::npos); |
| 2850 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2851 | != std::string::npos); |
| 2852 | |
| 2853 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2854 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2855 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2856 | } |
| 2857 | } |
| 2858 | |
| 2859 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2860 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2861 | ASSERT_TRUE(test_server.Start()); |
| 2862 | |
| 2863 | // Set up a cookie. |
| 2864 | { |
| 2865 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2866 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2867 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2868 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2869 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2870 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2871 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2872 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2873 | |
| 2874 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2875 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2876 | } |
| 2877 | |
| 2878 | // Verify that the cookie is set. |
| 2879 | { |
| 2880 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2881 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2882 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2883 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2884 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2885 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2886 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2887 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2888 | |
| 2889 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2890 | != std::string::npos); |
| 2891 | |
| 2892 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2893 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2894 | TestNetLogEntry::List entries; |
| 2895 | net_log_.GetEntries(&entries); |
| 2896 | for (const auto& entry : entries) { |
| 2897 | EXPECT_NE(entry.type, |
| 2898 | NetLogEventType::COOKIE_GET_BLOCKED_BY_NETWORK_DELEGATE); |
| 2899 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2900 | } |
| 2901 | |
| 2902 | // Verify that the cookie isn't sent. |
| 2903 | { |
| 2904 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2905 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2906 | TestDelegate d; |
| 2907 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2908 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2909 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2910 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2911 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2912 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2913 | |
| 2914 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2915 | == std::string::npos); |
| 2916 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2917 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2918 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2919 | TestNetLogEntry::List entries; |
| 2920 | net_log_.GetEntries(&entries); |
| 2921 | ExpectLogContainsSomewhereAfter( |
| 2922 | entries, 0, NetLogEventType::COOKIE_GET_BLOCKED_BY_NETWORK_DELEGATE, |
| 2923 | NetLogEventPhase::NONE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2924 | } |
| 2925 | } |
| 2926 | |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 2927 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 2928 | #if defined(OS_IOS) |
| 2929 | #define MAYBE_DoNotSaveCookies_ViaPolicy FLAKY_DoNotSaveCookies_ViaPolicy |
| 2930 | #else |
| 2931 | #define MAYBE_DoNotSaveCookies_ViaPolicy DoNotSaveCookies_ViaPolicy |
| 2932 | #endif |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2933 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2934 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2935 | ASSERT_TRUE(test_server.Start()); |
| 2936 | |
| 2937 | // Set up a cookie. |
| 2938 | { |
| 2939 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2940 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2941 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2942 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2943 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2944 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2945 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2946 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2947 | |
| 2948 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2949 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2950 | TestNetLogEntry::List entries; |
| 2951 | net_log_.GetEntries(&entries); |
| 2952 | for (const auto& entry : entries) { |
| 2953 | EXPECT_NE(entry.type, |
| 2954 | NetLogEventType::COOKIE_SET_BLOCKED_BY_NETWORK_DELEGATE); |
| 2955 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2956 | } |
| 2957 | |
| 2958 | // Try to set-up another cookie and update the previous cookie. |
| 2959 | { |
| 2960 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2961 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2962 | TestDelegate d; |
| 2963 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2964 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2965 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2966 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2967 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2968 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2969 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2970 | |
| 2971 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2972 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2973 | TestNetLogEntry::List entries; |
| 2974 | net_log_.GetEntries(&entries); |
| 2975 | ExpectLogContainsSomewhereAfter( |
| 2976 | entries, 0, NetLogEventType::COOKIE_SET_BLOCKED_BY_NETWORK_DELEGATE, |
| 2977 | NetLogEventPhase::NONE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2978 | } |
| 2979 | |
| 2980 | // Verify the cookies weren't saved or updated. |
| 2981 | { |
| 2982 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2983 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2984 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2985 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2986 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2987 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2988 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2989 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2990 | |
| 2991 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2992 | == std::string::npos); |
| 2993 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2994 | != std::string::npos); |
| 2995 | |
| 2996 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2997 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2998 | } |
| 2999 | } |
| 3000 | |
| 3001 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3002 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3003 | ASSERT_TRUE(test_server.Start()); |
| 3004 | |
| 3005 | // Set up an empty cookie. |
| 3006 | { |
| 3007 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3008 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3009 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3010 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3011 | test_server.GetURL("/set-cookie"), DEFAULT_PRIORITY, &d, |
| 3012 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3013 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3014 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3015 | |
| 3016 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3017 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3018 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 3019 | } |
| 3020 | } |
| 3021 | |
| 3022 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3023 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3024 | ASSERT_TRUE(test_server.Start()); |
| 3025 | |
| 3026 | // Set up a cookie. |
| 3027 | { |
| 3028 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3029 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3030 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3031 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3032 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3033 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3034 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3035 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3036 | |
| 3037 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3038 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3039 | } |
| 3040 | |
| 3041 | // Verify that the cookie is set. |
| 3042 | { |
| 3043 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3044 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3045 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3046 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3047 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3048 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3049 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3050 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3051 | |
| 3052 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 3053 | != std::string::npos); |
| 3054 | |
| 3055 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3056 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3057 | } |
| 3058 | |
| 3059 | // Verify that the cookie isn't sent. |
| 3060 | { |
| 3061 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3062 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3063 | TestDelegate d; |
| 3064 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3065 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3066 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3067 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3068 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3069 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3070 | |
| 3071 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 3072 | == std::string::npos); |
| 3073 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 3074 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3075 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3076 | } |
| 3077 | } |
| 3078 | |
| 3079 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3080 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3081 | ASSERT_TRUE(test_server.Start()); |
| 3082 | |
| 3083 | // Set up a cookie. |
| 3084 | { |
| 3085 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3086 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3087 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3088 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3089 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3090 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3091 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3092 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3093 | |
| 3094 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3095 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3096 | } |
| 3097 | |
| 3098 | // Try to set-up another cookie and update the previous cookie. |
| 3099 | { |
| 3100 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3101 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3102 | TestDelegate d; |
| 3103 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3104 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3105 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3106 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3107 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3108 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3109 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3110 | |
| 3111 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3112 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 3113 | } |
| 3114 | |
| 3115 | // Verify the cookies weren't saved or updated. |
| 3116 | { |
| 3117 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3118 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3119 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3120 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3121 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3122 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3123 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3124 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3125 | |
| 3126 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 3127 | == std::string::npos); |
| 3128 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 3129 | != std::string::npos); |
| 3130 | |
| 3131 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3132 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3133 | } |
| 3134 | } |
| 3135 | |
mkwst | c5fa776 | 2016-03-28 09:28:23 | [diff] [blame] | 3136 | TEST_F(URLRequestTest, SameSiteCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3137 | HttpTestServer test_server; |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3138 | ASSERT_TRUE(test_server.Start()); |
| 3139 | |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3140 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3141 | default_context().set_network_delegate(&network_delegate); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3142 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3143 | const std::string kHost = "example.test"; |
| 3144 | const std::string kSubHost = "subdomain.example.test"; |
| 3145 | const std::string kCrossHost = "cross-origin.test"; |
| 3146 | |
| 3147 | // Set up two 'SameSite' cookies on 'example.test' |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3148 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3149 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3150 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3151 | test_server.GetURL(kHost, |
| 3152 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 3153 | "LaxSameSiteCookie=1;SameSite=Lax"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3154 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3155 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3156 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3157 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3158 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3159 | EXPECT_EQ(2, network_delegate.set_cookie_count()); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3160 | } |
| 3161 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3162 | // Verify that both cookies are sent for same-site requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3163 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3164 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3165 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3166 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3167 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3168 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3169 | req->set_initiator(url::Origin::Create(test_server.GetURL(kHost, "/"))); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3170 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3171 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3172 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3173 | EXPECT_NE(std::string::npos, |
| 3174 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3175 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3176 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3177 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3178 | } |
| 3179 | |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3180 | // Verify that both cookies are sent when the request has no initiator (can |
| 3181 | // happen for main frame browser-initiated navigations). |
| 3182 | { |
| 3183 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3184 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3185 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3186 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3187 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3188 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3189 | d.RunUntilComplete(); |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3190 | |
| 3191 | EXPECT_NE(std::string::npos, |
| 3192 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3193 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 3194 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3195 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3196 | } |
| 3197 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3198 | // Verify that both cookies are sent for same-registrable-domain requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3199 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3200 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3201 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3202 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3203 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3204 | req->set_site_for_cookies(test_server.GetURL(kSubHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3205 | req->set_initiator(url::Origin::Create(test_server.GetURL(kSubHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3206 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3207 | d.RunUntilComplete(); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3208 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3209 | EXPECT_NE(std::string::npos, |
| 3210 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3211 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3212 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3213 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3214 | } |
| 3215 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3216 | // Verify that neither cookie is not sent for cross-site requests. |
| 3217 | { |
| 3218 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3219 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3220 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3221 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3222 | req->set_site_for_cookies(test_server.GetURL(kCrossHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3223 | req->set_initiator( |
| 3224 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3225 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3226 | d.RunUntilComplete(); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3227 | |
| 3228 | EXPECT_EQ(std::string::npos, |
| 3229 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3230 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 3231 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3232 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3233 | } |
| 3234 | |
| 3235 | // Verify that the lax cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3236 | // method is "safe". |
| 3237 | { |
| 3238 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3239 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3240 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3241 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3242 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3243 | req->set_initiator( |
| 3244 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3245 | req->set_method("GET"); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3246 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3247 | d.RunUntilComplete(); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3248 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3249 | EXPECT_EQ(std::string::npos, |
| 3250 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3251 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3252 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3253 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3254 | } |
| 3255 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3256 | // Verify that neither cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3257 | // method is unsafe (e.g. POST). |
| 3258 | { |
| 3259 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3260 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3261 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3262 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3263 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3264 | req->set_initiator( |
| 3265 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3266 | req->set_method("POST"); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3267 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3268 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3269 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3270 | EXPECT_EQ(std::string::npos, |
| 3271 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3272 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3273 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3274 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3275 | } |
| 3276 | } |
| 3277 | |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3278 | // Tests that __Secure- cookies can't be set on non-secure origins. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3279 | TEST_F(URLRequestTest, SecureCookiePrefixOnNonsecureOrigin) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3280 | EmbeddedTestServer http_server; |
| 3281 | http_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3282 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3283 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3284 | https_server.AddDefaultHandlers( |
| 3285 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3286 | ASSERT_TRUE(http_server.Start()); |
| 3287 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3288 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3289 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3290 | TestURLRequestContext context(true); |
| 3291 | context.set_network_delegate(&network_delegate); |
| 3292 | context.Init(); |
| 3293 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3294 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3295 | { |
| 3296 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3297 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3298 | http_server.GetURL("/set-cookie?__Secure-nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3299 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3300 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3301 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3302 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3303 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3304 | } |
| 3305 | |
| 3306 | // Verify that the cookie is not set. |
| 3307 | { |
| 3308 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3309 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3310 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3311 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3312 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3313 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3314 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3315 | EXPECT_EQ(d.data_received().find("__Secure-nonsecure-origin=1"), |
| 3316 | std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3317 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3318 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3319 | } |
| 3320 | } |
| 3321 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3322 | TEST_F(URLRequestTest, SecureCookiePrefixNonsecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3323 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3324 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3325 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3326 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3327 | |
| 3328 | TestNetworkDelegate network_delegate; |
| 3329 | TestURLRequestContext context(true); |
| 3330 | context.set_network_delegate(&network_delegate); |
| 3331 | context.Init(); |
| 3332 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3333 | // Try to set a non-Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3334 | { |
| 3335 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3336 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 3337 | https_server.GetURL("/set-cookie?__Secure-foo=1"), DEFAULT_PRIORITY, &d, |
| 3338 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3339 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3340 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3341 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3342 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3343 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3344 | } |
| 3345 | |
| 3346 | // Verify that the cookie is not set. |
| 3347 | { |
| 3348 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3349 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3350 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3351 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3352 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3353 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3354 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3355 | EXPECT_EQ(d.data_received().find("__Secure-foo=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3356 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3357 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3358 | } |
| 3359 | } |
| 3360 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3361 | TEST_F(URLRequestTest, SecureCookiePrefixSecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3362 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3363 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3364 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3365 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3366 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3367 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3368 | TestURLRequestContext context(true); |
| 3369 | context.set_network_delegate(&network_delegate); |
| 3370 | context.Init(); |
| 3371 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3372 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3373 | { |
| 3374 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3375 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3376 | https_server.GetURL("/set-cookie?__Secure-bar=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3377 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3378 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3379 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3380 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3381 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3382 | } |
| 3383 | |
| 3384 | // Verify that the cookie is set. |
| 3385 | { |
| 3386 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3387 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3388 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3389 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3390 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3391 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3392 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3393 | EXPECT_NE(d.data_received().find("__Secure-bar=1"), std::string::npos); |
| 3394 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3395 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3396 | } |
| 3397 | } |
| 3398 | |
| 3399 | // Tests that secure cookies can't be set on non-secure origins if strict secure |
| 3400 | // cookies are enabled. |
| 3401 | TEST_F(URLRequestTest, StrictSecureCookiesOnNonsecureOrigin) { |
| 3402 | EmbeddedTestServer http_server; |
| 3403 | http_server.AddDefaultHandlers( |
| 3404 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3405 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3406 | https_server.AddDefaultHandlers( |
| 3407 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3408 | ASSERT_TRUE(http_server.Start()); |
| 3409 | ASSERT_TRUE(https_server.Start()); |
| 3410 | |
Matt Menke | be9b6eb | 2018-10-18 12:11:44 | [diff] [blame] | 3411 | TestNetworkDelegate network_delegate; |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3412 | TestURLRequestContext context(true); |
| 3413 | context.set_network_delegate(&network_delegate); |
| 3414 | context.Init(); |
| 3415 | |
| 3416 | // Try to set a Secure cookie, with experimental features enabled. |
| 3417 | { |
| 3418 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3419 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3420 | http_server.GetURL("/set-cookie?nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3421 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3422 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3423 | d.RunUntilComplete(); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3424 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3425 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3426 | } |
| 3427 | |
| 3428 | // Verify that the cookie is not set. |
| 3429 | { |
| 3430 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3431 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3432 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3433 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3434 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3435 | d.RunUntilComplete(); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3436 | |
| 3437 | EXPECT_EQ(d.data_received().find("nonsecure-origin=1"), std::string::npos); |
| 3438 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3439 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3440 | } |
| 3441 | } |
| 3442 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3443 | // The parameter is true for same-site and false for cross-site requests. |
| 3444 | class URLRequestTestParameterizedSameSite |
| 3445 | : public URLRequestTest, |
| 3446 | public ::testing::WithParamInterface<bool> { |
| 3447 | protected: |
| 3448 | URLRequestTestParameterizedSameSite() { |
| 3449 | auto params = std::make_unique<HttpNetworkSession::Params>(); |
| 3450 | params->ignore_certificate_errors = true; |
| 3451 | context_.set_http_network_session_params(std::move(params)); |
| 3452 | context_.set_network_delegate(&network_delegate_); |
| 3453 | https_server_.AddDefaultHandlers( |
| 3454 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3455 | EXPECT_TRUE(https_server_.Start()); |
| 3456 | } |
| 3457 | |
| 3458 | // To be called after configuration of |context_| has been finalized. |
| 3459 | void InitContext() { context_.Init(); } |
| 3460 | |
| 3461 | const std::string kHost_ = "example.test"; |
| 3462 | const std::string kCrossHost_ = "cross-site.test"; |
| 3463 | TestURLRequestContext context_{true}; |
| 3464 | TestNetworkDelegate network_delegate_; |
| 3465 | base::HistogramTester histograms_; |
| 3466 | EmbeddedTestServer https_server_{EmbeddedTestServer::TYPE_HTTPS}; |
| 3467 | }; |
| 3468 | |
Victor Costan | 8fb98f6f | 2019-02-01 17:08:29 | [diff] [blame] | 3469 | INSTANTIATE_TEST_SUITE_P(URLRequestTest, |
| 3470 | URLRequestTestParameterizedSameSite, |
| 3471 | ::testing::Bool()); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3472 | |
| 3473 | TEST_P(URLRequestTestParameterizedSameSite, CookieAgeMetrics) { |
| 3474 | const bool same_site = GetParam(); |
| 3475 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3476 | InitContext(); |
| 3477 | |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3478 | EmbeddedTestServer http_server; |
| 3479 | http_server.AddDefaultHandlers( |
| 3480 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3481 | ASSERT_TRUE(http_server.Start()); |
| 3482 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3483 | // Set two test cookies. |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3484 | { |
| 3485 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3486 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3487 | http_server.GetURL(kHost_, "/set-cookie?cookie=value&cookie2=value2"), |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3488 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3489 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3490 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3491 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3492 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3493 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 0); |
| 3494 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", 0); |
| 3495 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", 0); |
| 3496 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3497 | 0); |
| 3498 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3499 | 0); |
| 3500 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", 0); |
| 3501 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", 0); |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3502 | } |
| 3503 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3504 | // Make a secure request. |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3505 | { |
| 3506 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3507 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3508 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3509 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3510 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3511 | req->set_initiator( |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3512 | url::Origin::Create(https_server_.GetURL(kInitiatingHost, "/"))); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3513 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3514 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3515 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3516 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 0); |
| 3517 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", |
| 3518 | !same_site); |
| 3519 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", |
| 3520 | same_site); |
| 3521 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3522 | 0); |
| 3523 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3524 | 0); |
| 3525 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", |
| 3526 | same_site ? 0 : 2); |
| 3527 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", |
| 3528 | same_site ? 2 : 0); |
| 3529 | EXPECT_TRUE(d.data_received().find("cookie=value") != std::string::npos); |
| 3530 | EXPECT_TRUE(d.data_received().find("cookie2=value2") != std::string::npos); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3531 | } |
| 3532 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3533 | // Make a non-secure request. |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3534 | { |
| 3535 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3536 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3537 | http_server.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3538 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3539 | req->set_site_for_cookies(http_server.GetURL(kInitiatingHost, "/")); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3540 | req->set_initiator( |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3541 | url::Origin::Create(http_server.GetURL(kInitiatingHost, "/"))); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3542 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3543 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3544 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", |
| 3545 | !same_site); |
| 3546 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", |
| 3547 | same_site); |
| 3548 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", |
| 3549 | !same_site); |
| 3550 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", |
| 3551 | same_site); |
| 3552 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3553 | same_site ? 0 : 2); |
| 3554 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3555 | same_site ? 2 : 0); |
| 3556 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", |
| 3557 | same_site ? 0 : 2); |
| 3558 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", |
| 3559 | same_site ? 2 : 0); |
| 3560 | EXPECT_TRUE(d.data_received().find("cookie=value") != std::string::npos); |
| 3561 | EXPECT_TRUE(d.data_received().find("cookie2=value2") != std::string::npos); |
| 3562 | } |
| 3563 | } |
| 3564 | |
| 3565 | // Cookies with secure attribute (no HSTS) --> k1pSecureAttribute |
| 3566 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3567 | CookieNetworkSecurityMetricSecureAttribute) { |
| 3568 | const bool same_site = GetParam(); |
| 3569 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3570 | InitContext(); |
| 3571 | |
| 3572 | // Set cookies. |
| 3573 | { |
| 3574 | TestDelegate d; |
| 3575 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3576 | https_server_.GetURL(kHost_, |
| 3577 | "/set-cookie?session-cookie=value;Secure&" |
| 3578 | "longlived-cookie=value;Secure;domain=" + |
| 3579 | kHost_ + ";Max-Age=360000"), |
| 3580 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3581 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3582 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3583 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3584 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3585 | } |
| 3586 | |
| 3587 | // Verify that the cookies fall into the correct metrics bucket. |
| 3588 | { |
| 3589 | TestDelegate d; |
| 3590 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3591 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3592 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3593 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3594 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3595 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3596 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3597 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3598 | // Static cast of boolean required for MSVC 1911. |
| 3599 | histograms_.ExpectBucketCount( |
| 3600 | "Cookie.NetworkSecurity", |
| 3601 | static_cast<int>(CookieNetworkSecurity::k1pSecureAttribute) | |
| 3602 | static_cast<int>(!same_site), |
| 3603 | 2); |
| 3604 | } |
| 3605 | } |
| 3606 | |
| 3607 | // Short-lived host cookie --> k1pHSTSHostCookie |
| 3608 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3609 | CookieNetworkSecurityMetricShortlivedHostCookie) { |
| 3610 | const bool same_site = GetParam(); |
| 3611 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3612 | |
| 3613 | TransportSecurityState transport_security_state; |
| 3614 | transport_security_state.AddHSTS( |
| 3615 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3616 | false /* include_subdomains */); |
| 3617 | context_.set_transport_security_state(&transport_security_state); |
| 3618 | InitContext(); |
| 3619 | |
| 3620 | // Set cookie. |
| 3621 | { |
| 3622 | TestDelegate d; |
| 3623 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3624 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;Max-Age=3600"), |
| 3625 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3626 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3627 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3628 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3629 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3630 | } |
| 3631 | |
| 3632 | // Verify that the cookie falls into the correct metrics bucket. |
| 3633 | { |
| 3634 | TestDelegate d; |
| 3635 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3636 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3637 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3638 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3639 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3640 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3641 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3642 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3643 | // Static cast of boolean required for MSVC 1911. |
| 3644 | histograms_.ExpectBucketCount( |
| 3645 | "Cookie.NetworkSecurity", |
| 3646 | static_cast<int>(CookieNetworkSecurity::k1pHSTSHostCookie) | |
| 3647 | static_cast<int>(!same_site), |
| 3648 | 1); |
| 3649 | } |
| 3650 | } |
| 3651 | |
| 3652 | // Long-lived (either due to expiry or due to being a session cookie) host |
| 3653 | // cookies --> k1pExpiringHSTSHostCookie |
| 3654 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3655 | CookieNetworkSecurityMetricLonglivedHostCookie) { |
| 3656 | const bool same_site = GetParam(); |
| 3657 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3658 | |
| 3659 | TransportSecurityState transport_security_state; |
| 3660 | transport_security_state.AddHSTS( |
| 3661 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3662 | false /* include_subdomains */); |
| 3663 | context_.set_transport_security_state(&transport_security_state); |
| 3664 | InitContext(); |
| 3665 | |
| 3666 | // Set cookies. |
| 3667 | { |
| 3668 | TestDelegate d; |
| 3669 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3670 | https_server_.GetURL(kHost_, |
| 3671 | "/set-cookie?session-cookie=value&" |
| 3672 | "longlived-cookie=value;Max-Age=360000"), |
| 3673 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3674 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3675 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3676 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3677 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3678 | } |
| 3679 | |
| 3680 | // Verify that the cookies fall into the correct metrics bucket. |
| 3681 | { |
| 3682 | TestDelegate d; |
| 3683 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3684 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3685 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3686 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3687 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3688 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3689 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3690 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3691 | // Static cast of boolean required for MSVC 1911. |
| 3692 | histograms_.ExpectBucketCount( |
| 3693 | "Cookie.NetworkSecurity", |
| 3694 | static_cast<int>(CookieNetworkSecurity::k1pExpiringHSTSHostCookie) | |
| 3695 | static_cast<int>(!same_site), |
| 3696 | 2); |
| 3697 | } |
| 3698 | } |
| 3699 | |
| 3700 | // Domain cookie with HSTS subdomains with cookie expiry before HSTS expiry --> |
| 3701 | // k1pHSTSSubdomainsIncluded |
| 3702 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3703 | CookieNetworkSecurityMetricShortlivedDomainCookie) { |
| 3704 | const bool same_site = GetParam(); |
| 3705 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3706 | |
| 3707 | TransportSecurityState transport_security_state; |
| 3708 | transport_security_state.AddHSTS( |
| 3709 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3710 | true /* include_subdomains */); |
| 3711 | context_.set_transport_security_state(&transport_security_state); |
| 3712 | InitContext(); |
| 3713 | |
| 3714 | // Set cookie. |
| 3715 | { |
| 3716 | TestDelegate d; |
| 3717 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3718 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;domain=" + |
| 3719 | kHost_ + ";Max-Age=3600"), |
| 3720 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3721 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3722 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3723 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3724 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3725 | } |
| 3726 | |
| 3727 | // Verify that the cookie falls into the correct metrics bucket. |
| 3728 | { |
| 3729 | TestDelegate d; |
| 3730 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3731 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3732 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3733 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3734 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3735 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3736 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3737 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3738 | // Static cast of boolean required for MSVC 1911. |
| 3739 | histograms_.ExpectBucketCount( |
| 3740 | "Cookie.NetworkSecurity", |
| 3741 | static_cast<int>(CookieNetworkSecurity::k1pHSTSSubdomainsIncluded) | |
| 3742 | static_cast<int>(!same_site), |
| 3743 | 1); |
| 3744 | } |
| 3745 | } |
| 3746 | |
| 3747 | // Long-lived (either due to expiry or due to being a session cookie) domain |
| 3748 | // cookies with HSTS subdomains --> k1pExpiringHSTSSubdomainsIncluded |
| 3749 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3750 | CookieNetworkSecurityMetricLonglivedDomainCookie) { |
| 3751 | const bool same_site = GetParam(); |
| 3752 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3753 | |
| 3754 | TransportSecurityState transport_security_state; |
| 3755 | transport_security_state.AddHSTS( |
| 3756 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3757 | true /* include_subdomains */); |
| 3758 | context_.set_transport_security_state(&transport_security_state); |
| 3759 | InitContext(); |
| 3760 | |
| 3761 | // Set cookies. |
| 3762 | { |
| 3763 | TestDelegate d; |
| 3764 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3765 | https_server_.GetURL( |
| 3766 | kHost_, "/set-cookie?session-cookie=value;domain=" + kHost_ + "&" + |
| 3767 | "longlived-cookie=value;domain=" + kHost_ + |
| 3768 | ";Max-Age=360000"), |
| 3769 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3770 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3771 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3772 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3773 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3774 | } |
| 3775 | |
| 3776 | // Verify that the cookies fall into the correct metrics bucket. |
| 3777 | { |
| 3778 | TestDelegate d; |
| 3779 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3780 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3781 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3782 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3783 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3784 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3785 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3786 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3787 | // Static cast of boolean required for MSVC 1911. |
| 3788 | histograms_.ExpectBucketCount( |
| 3789 | "Cookie.NetworkSecurity", |
| 3790 | static_cast<int>( |
| 3791 | CookieNetworkSecurity::k1pExpiringHSTSSubdomainsIncluded) | |
| 3792 | static_cast<int>(!same_site), |
| 3793 | 2); |
| 3794 | } |
| 3795 | } |
| 3796 | |
| 3797 | // Domain cookie with HSTS subdomains not included --> k1pHSTSSpoofable |
| 3798 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3799 | CookieNetworkSecurityMetricSpoofableDomainCookie) { |
| 3800 | const bool same_site = GetParam(); |
| 3801 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3802 | |
| 3803 | TransportSecurityState transport_security_state; |
| 3804 | transport_security_state.AddHSTS( |
| 3805 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3806 | false /* include_subdomains */); |
| 3807 | context_.set_transport_security_state(&transport_security_state); |
| 3808 | InitContext(); |
| 3809 | |
| 3810 | // Set cookie. |
| 3811 | { |
| 3812 | TestDelegate d; |
| 3813 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3814 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;domain=" + |
| 3815 | kHost_ + ";Max-Age=3600"), |
| 3816 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3817 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3818 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3819 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3820 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3821 | } |
| 3822 | |
| 3823 | // Verify that the cookie falls into the correct metrics bucket. |
| 3824 | { |
| 3825 | TestDelegate d; |
| 3826 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3827 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3828 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3829 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3830 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3831 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3832 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3833 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3834 | // Static cast of boolean required for MSVC 1911. |
| 3835 | histograms_.ExpectBucketCount( |
| 3836 | "Cookie.NetworkSecurity", |
| 3837 | static_cast<int>(CookieNetworkSecurity::k1pHSTSSpoofable) | |
| 3838 | static_cast<int>(!same_site), |
| 3839 | 1); |
| 3840 | } |
| 3841 | } |
| 3842 | |
| 3843 | // Cookie without HSTS --> k1p(Non)SecureConnection |
| 3844 | TEST_P(URLRequestTestParameterizedSameSite, CookieNetworkSecurityMetricNoHSTS) { |
| 3845 | const bool same_site = GetParam(); |
| 3846 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3847 | InitContext(); |
| 3848 | |
| 3849 | EmbeddedTestServer http_server; |
| 3850 | http_server.AddDefaultHandlers( |
| 3851 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3852 | ASSERT_TRUE(http_server.Start()); |
| 3853 | |
| 3854 | // Set cookies. |
| 3855 | { |
| 3856 | TestDelegate d; |
| 3857 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3858 | https_server_.GetURL(kHost_, |
| 3859 | "/set-cookie?cookie=value;domain=" + kHost_ + |
| 3860 | ";Max-Age=3600&host-cookie=value"), |
| 3861 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3862 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3863 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3864 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3865 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3866 | } |
| 3867 | |
| 3868 | // Verify that the cookie falls into the correct metrics bucket. |
| 3869 | { |
| 3870 | TestDelegate d; |
| 3871 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3872 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3873 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3874 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3875 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3876 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3877 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3878 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3879 | // Static cast of boolean required for MSVC 1911. |
| 3880 | histograms_.ExpectBucketCount( |
| 3881 | "Cookie.NetworkSecurity", |
| 3882 | static_cast<int>(CookieNetworkSecurity::k1pSecureConnection) | |
| 3883 | static_cast<int>(!same_site), |
| 3884 | 2); |
| 3885 | } |
| 3886 | |
| 3887 | // Verify that the cookie falls into the correct metrics bucket. |
| 3888 | { |
| 3889 | TestDelegate d; |
| 3890 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3891 | http_server.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3892 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3893 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3894 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3895 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3896 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3897 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 4); |
| 3898 | // Static cast of boolean required for MSVC 1911. |
| 3899 | histograms_.ExpectBucketCount( |
| 3900 | "Cookie.NetworkSecurity", |
| 3901 | static_cast<int>(CookieNetworkSecurity::k1pSecureConnection) | |
| 3902 | static_cast<int>(!same_site), |
| 3903 | 2); |
| 3904 | // Static cast of boolean required for MSVC 1911. |
| 3905 | histograms_.ExpectBucketCount( |
| 3906 | "Cookie.NetworkSecurity", |
| 3907 | static_cast<int>(CookieNetworkSecurity::k1pNonsecureConnection) | |
| 3908 | static_cast<int>(!same_site), |
| 3909 | 2); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3910 | } |
| 3911 | } |
| 3912 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3913 | // Tests that a request is cancelled while entering suspend mode. Uses mocks |
| 3914 | // rather than a spawned test server because the connection used to talk to |
| 3915 | // the test server is affected by entering suspend mode on Android. |
| 3916 | TEST_F(URLRequestTest, CancelOnSuspend) { |
| 3917 | TestPowerMonitorSource* power_monitor_source = new TestPowerMonitorSource(); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3918 | base::PowerMonitor power_monitor(base::WrapUnique(power_monitor_source)); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3919 | |
| 3920 | URLRequestFailedJob::AddUrlHandler(); |
| 3921 | |
| 3922 | TestDelegate d; |
| 3923 | // Request that just hangs. |
| 3924 | GURL url(URLRequestFailedJob::GetMockHttpUrl(ERR_IO_PENDING)); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3925 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3926 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3927 | r->Start(); |
| 3928 | |
| 3929 | power_monitor_source->Suspend(); |
| 3930 | // Wait for the suspend notification to cause the request to fail. |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3931 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3932 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3933 | EXPECT_TRUE(d.request_failed()); |
| 3934 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 3935 | |
| 3936 | URLRequestFilter::GetInstance()->ClearHandlers(); |
| 3937 | |
| 3938 | // Shouldn't be needed, but just in case. |
| 3939 | power_monitor_source->Resume(); |
| 3940 | } |
| 3941 | |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3942 | // FixedDateNetworkDelegate swaps out the server's HTTP Date response header |
| 3943 | // value for the |fixed_date| argument given to the constructor. |
| 3944 | class FixedDateNetworkDelegate : public TestNetworkDelegate { |
| 3945 | public: |
| 3946 | explicit FixedDateNetworkDelegate(const std::string& fixed_date) |
| 3947 | : fixed_date_(fixed_date) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 3948 | ~FixedDateNetworkDelegate() override = default; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3949 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3950 | // NetworkDelegate implementation |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 3951 | int OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3952 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 3953 | CompletionOnceCallback callback, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3954 | const HttpResponseHeaders* original_response_headers, |
| 3955 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 3956 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3957 | |
| 3958 | private: |
| 3959 | std::string fixed_date_; |
| 3960 | |
| 3961 | DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); |
| 3962 | }; |
| 3963 | |
| 3964 | int FixedDateNetworkDelegate::OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3965 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 3966 | CompletionOnceCallback callback, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3967 | const HttpResponseHeaders* original_response_headers, |
| 3968 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3969 | GURL* allowed_unsafe_redirect_url) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3970 | HttpResponseHeaders* new_response_headers = |
| 3971 | new HttpResponseHeaders(original_response_headers->raw_headers()); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3972 | |
| 3973 | new_response_headers->RemoveHeader("Date"); |
| 3974 | new_response_headers->AddHeader("Date: " + fixed_date_); |
| 3975 | |
| 3976 | *override_response_headers = new_response_headers; |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 3977 | return TestNetworkDelegate::OnHeadersReceived( |
| 3978 | request, std::move(callback), original_response_headers, |
| 3979 | override_response_headers, allowed_unsafe_redirect_url); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3980 | } |
| 3981 | |
| 3982 | // Test that cookie expiration times are adjusted for server/client clock |
| 3983 | // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date |
| 3984 | // headers by defaulting to GMT. (crbug.com/135131) |
| 3985 | TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3986 | HttpTestServer test_server; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3987 | ASSERT_TRUE(test_server.Start()); |
| 3988 | |
| 3989 | // Set up an expired cookie. |
| 3990 | { |
| 3991 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3992 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3993 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3994 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3995 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3996 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3997 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3998 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3999 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4000 | } |
| 4001 | // Verify that the cookie is not set. |
| 4002 | { |
| 4003 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4004 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4005 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4006 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4007 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 4008 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4009 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4010 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4011 | |
| 4012 | EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos); |
| 4013 | } |
| 4014 | // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier. |
| 4015 | { |
| 4016 | FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC"); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4017 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4018 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4019 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4020 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4021 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4022 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4023 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4024 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4025 | } |
| 4026 | // Verify that the cookie is set. |
| 4027 | { |
| 4028 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4029 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4030 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4031 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4032 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 4033 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4034 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4035 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4036 | |
| 4037 | EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos); |
| 4038 | } |
| 4039 | } |
| 4040 | |
| 4041 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4042 | // Check that it is impossible to change the referrer in the extra headers of |
| 4043 | // an URLRequest. |
| 4044 | TEST_F(URLRequestTest, DoNotOverrideReferrer) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4045 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4046 | ASSERT_TRUE(test_server.Start()); |
| 4047 | |
| 4048 | // If extra headers contain referer and the request contains a referer, |
| 4049 | // only the latter shall be respected. |
| 4050 | { |
| 4051 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4052 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4053 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 4054 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4055 | req->SetReferrer("http://foo.com/"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4056 | |
| 4057 | HttpRequestHeaders headers; |
| 4058 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4059 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4060 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4061 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4062 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4063 | |
| 4064 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 4065 | } |
| 4066 | |
| 4067 | // If extra headers contain a referer but the request does not, no referer |
| 4068 | // shall be sent in the header. |
| 4069 | { |
| 4070 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4071 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4072 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 4073 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4074 | |
| 4075 | HttpRequestHeaders headers; |
| 4076 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4077 | req->SetExtraRequestHeaders(headers); |
| 4078 | req->SetLoadFlags(LOAD_VALIDATE_CACHE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4079 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4080 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4081 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4082 | |
| 4083 | EXPECT_EQ("None", d.data_received()); |
| 4084 | } |
| 4085 | } |
| 4086 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4087 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4088 | public: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 4089 | URLRequestTestHTTP() : test_server_(base::FilePath(kTestFilePath)) {} |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4090 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4091 | protected: |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4092 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 4093 | // |request_method| is the method to use for the initial request. |
| 4094 | // |redirect_method| is the method that is expected to be used for the second |
| 4095 | // request, after redirection. |
| 4096 | // If |include_data| is true, data is uploaded with the request. The |
| 4097 | // response body is expected to match it exactly, if and only if |
| 4098 | // |request_method| == |redirect_method|. |
| 4099 | void HTTPRedirectMethodTest(const GURL& redirect_url, |
| 4100 | const std::string& request_method, |
| 4101 | const std::string& redirect_method, |
| 4102 | bool include_data) { |
| 4103 | static const char kData[] = "hello world"; |
| 4104 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4105 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4106 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4107 | req->set_method(request_method); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4108 | if (include_data) { |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4109 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4110 | HttpRequestHeaders headers; |
| 4111 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 4112 | base::NumberToString(base::size(kData) - 1)); |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 4113 | headers.SetHeader(HttpRequestHeaders::kContentType, "text/plain"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4114 | req->SetExtraRequestHeaders(headers); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4115 | } |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4116 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4117 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4118 | EXPECT_EQ(redirect_method, req->method()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4119 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4120 | if (include_data) { |
| 4121 | if (request_method == redirect_method) { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 4122 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 4123 | HttpRequestHeaders::kContentLength)); |
| 4124 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 4125 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4126 | EXPECT_EQ(kData, d.data_received()); |
| 4127 | } else { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 4128 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 4129 | HttpRequestHeaders::kContentLength)); |
| 4130 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 4131 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4132 | EXPECT_NE(kData, d.data_received()); |
| 4133 | } |
| 4134 | } |
| 4135 | if (HasFailure()) |
| 4136 | LOG(WARNING) << "Request method was: " << request_method; |
| 4137 | } |
| 4138 | |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4139 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. It's also |
| 4140 | // used as the initial origin. |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4141 | // |request_method| is the method to use for the initial request. |
| 4142 | // |redirect_method| is the method that is expected to be used for the second |
| 4143 | // request, after redirection. |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4144 | // |expected_origin_value| is the expected value for the Origin header after |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4145 | // redirection. If empty, expects that there will be no Origin header. |
| 4146 | void HTTPRedirectOriginHeaderTest(const GURL& redirect_url, |
| 4147 | const std::string& request_method, |
| 4148 | const std::string& redirect_method, |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4149 | const std::string& expected_origin_value) { |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4150 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4151 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4152 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4153 | req->set_method(request_method); |
| 4154 | req->SetExtraRequestHeaderByName(HttpRequestHeaders::kOrigin, |
| 4155 | redirect_url.GetOrigin().spec(), false); |
| 4156 | req->Start(); |
| 4157 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4158 | d.RunUntilComplete(); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4159 | |
| 4160 | EXPECT_EQ(redirect_method, req->method()); |
| 4161 | // Note that there is no check for request success here because, for |
| 4162 | // purposes of testing, the request very well may fail. For example, if the |
| 4163 | // test redirects to an HTTPS server from an HTTP origin, thus it is cross |
| 4164 | // origin, there is not an HTTPS server in this unit test framework, so the |
| 4165 | // request would fail. However, that's fine, as long as the request headers |
| 4166 | // are in order and pass the checks below. |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4167 | if (expected_origin_value.empty()) { |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4168 | EXPECT_FALSE( |
| 4169 | req->extra_request_headers().HasHeader(HttpRequestHeaders::kOrigin)); |
| 4170 | } else { |
| 4171 | std::string origin_header; |
| 4172 | EXPECT_TRUE(req->extra_request_headers().GetHeader( |
| 4173 | HttpRequestHeaders::kOrigin, &origin_header)); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4174 | EXPECT_EQ(expected_origin_value, origin_header); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4175 | } |
| 4176 | } |
| 4177 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4178 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4179 | const int kMsgSize = 20000; // multiple of 10 |
| 4180 | const int kIterations = 50; |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 4181 | char* uploadBytes = new char[kMsgSize+1]; |
| 4182 | char* ptr = uploadBytes; |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4183 | char marker = 'a'; |
| 4184 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 4185 | memcpy(ptr, "----------", 10); |
| 4186 | ptr += 10; |
| 4187 | if (idx % 100 == 0) { |
| 4188 | ptr--; |
| 4189 | *ptr++ = marker; |
| 4190 | if (++marker > 'z') |
| 4191 | marker = 'a'; |
| 4192 | } |
| 4193 | } |
| 4194 | uploadBytes[kMsgSize] = '\0'; |
| 4195 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4196 | for (int i = 0; i < kIterations; ++i) { |
| 4197 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4198 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4199 | test_server_.GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 4200 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4201 | r->set_method(method.c_str()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4202 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4203 | r->set_upload(CreateSimpleUploadData(uploadBytes)); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4204 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4205 | r->Start(); |
| 4206 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4207 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4208 | d.RunUntilComplete(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4209 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4210 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 4211 | << d.request_status(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4212 | |
| 4213 | EXPECT_FALSE(d.received_data_before_response()); |
| 4214 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4215 | } |
| 4216 | delete[] uploadBytes; |
| 4217 | } |
| 4218 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 4219 | bool DoManyCookiesRequest(int num_cookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4220 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4221 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4222 | test_server_.GetURL("/set-many-cookies?" + |
Raul Tambre | 8c1981d | 2019-02-08 02:22:26 | [diff] [blame] | 4223 | base::NumberToString(num_cookies)), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4224 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4225 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4226 | r->Start(); |
| 4227 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4228 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4229 | d.RunUntilComplete(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4230 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4231 | if (d.request_status() != OK) { |
| 4232 | EXPECT_EQ(ERR_RESPONSE_HEADERS_TOO_BIG, d.request_status()); |
| 4233 | return false; |
| 4234 | } |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4235 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4236 | return true; |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4237 | } |
| 4238 | |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4239 | HttpTestServer* http_test_server() { return &test_server_; } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 4240 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4241 | private: |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4242 | HttpTestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4243 | }; |
| 4244 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4245 | namespace { |
| 4246 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4247 | std::unique_ptr<test_server::HttpResponse> HandleRedirectConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4248 | const test_server::HttpRequest& request) { |
| 4249 | if (request.headers.find("Host") == request.headers.end() || |
| 4250 | request.headers.at("Host") != "www.redirect.com" || |
| 4251 | request.method != test_server::METHOD_CONNECT) { |
| 4252 | return nullptr; |
| 4253 | } |
| 4254 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4255 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4256 | new test_server::BasicHttpResponse); |
| 4257 | http_response->set_code(HTTP_FOUND); |
| 4258 | http_response->AddCustomHeader("Location", |
| 4259 | "http://www.destination.com/foo.js"); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 4260 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4261 | } |
| 4262 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4263 | class TestSSLConfigService : public SSLConfigService { |
| 4264 | public: |
Nick Harper | 2243e800 | 2018-09-28 20:33:26 | [diff] [blame] | 4265 | TestSSLConfigService() |
| 4266 | : min_version_(kDefaultSSLVersionMin), |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4267 | max_version_(kDefaultSSLVersionMax) {} |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4268 | ~TestSSLConfigService() override = default; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4269 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4270 | void set_max_version(uint16_t version) { max_version_ = version; } |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4271 | void set_min_version(uint16_t version) { min_version_ = version; } |
| 4272 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4273 | // SSLConfigService: |
| 4274 | void GetSSLConfig(SSLConfig* config) override { |
| 4275 | *config = SSLConfig(); |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4276 | config->version_min = min_version_; |
| 4277 | config->version_max = max_version_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4278 | } |
| 4279 | |
Nick Harper | 89bc721 | 2018-07-31 19:07:57 | [diff] [blame] | 4280 | bool CanShareConnectionWithClientCerts( |
| 4281 | const std::string& hostname) const override { |
| 4282 | return false; |
| 4283 | } |
| 4284 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4285 | private: |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4286 | uint16_t min_version_; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4287 | uint16_t max_version_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4288 | }; |
| 4289 | |
Nick Harper | 89bc721 | 2018-07-31 19:07:57 | [diff] [blame] | 4290 | } // namespace |
| 4291 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4292 | // 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] | 4293 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4294 | // The EmbeddedTestServer will return a 302 response, which we should not |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4295 | // follow. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 4296 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4297 | http_test_server()->RegisterRequestHandler( |
Matt Menke | 2b2a6047 | 2018-02-07 02:12:12 | [diff] [blame] | 4298 | base::BindRepeating(&HandleRedirectConnect)); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4299 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4300 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4301 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4302 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4303 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4304 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4305 | TestDelegate d; |
| 4306 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4307 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4308 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 4309 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4310 | r->Start(); |
| 4311 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4312 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4313 | d.RunUntilComplete(); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4314 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4315 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4316 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4317 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4318 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4319 | // We should not have followed the redirect. |
| 4320 | EXPECT_EQ(0, d.received_redirect_count()); |
| 4321 | } |
| 4322 | } |
| 4323 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4324 | // This is the same as the previous test, but checks that the network delegate |
| 4325 | // registers the error. |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 4326 | TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4327 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4328 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4329 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4330 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4331 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4332 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4333 | TestDelegate d; |
| 4334 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4335 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4336 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 4337 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4338 | r->Start(); |
| 4339 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4340 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4341 | d.RunUntilComplete(); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4342 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4343 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4344 | EXPECT_FALSE(r->proxy_server().is_valid()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4345 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4346 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4347 | // We should not have followed the redirect. |
| 4348 | EXPECT_EQ(0, d.received_redirect_count()); |
| 4349 | |
| 4350 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4351 | EXPECT_THAT(network_delegate.last_error(), |
| 4352 | IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4353 | } |
| 4354 | } |
| 4355 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4356 | // Tests that we can block and asynchronously return OK in various stages. |
| 4357 | TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) { |
| 4358 | static const BlockingNetworkDelegate::Stage blocking_stages[] = { |
| 4359 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 4360 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 4361 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED |
| 4362 | }; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 4363 | static const size_t blocking_stages_length = base::size(blocking_stages); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4364 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4365 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4366 | |
| 4367 | TestDelegate d; |
| 4368 | BlockingNetworkDelegate network_delegate( |
| 4369 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4370 | network_delegate.set_block_on( |
| 4371 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST | |
| 4372 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS | |
| 4373 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 4374 | |
| 4375 | TestURLRequestContext context(true); |
| 4376 | context.set_network_delegate(&network_delegate); |
| 4377 | context.Init(); |
| 4378 | |
| 4379 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4380 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4381 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4382 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4383 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4384 | r->Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4385 | for (size_t i = 0; i < blocking_stages_length; ++i) { |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4386 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4387 | EXPECT_EQ(blocking_stages[i], |
| 4388 | network_delegate.stage_blocked_for_callback()); |
| 4389 | network_delegate.DoCallback(OK); |
| 4390 | } |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4391 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4392 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4393 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4394 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4395 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4396 | } |
| 4397 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4398 | } |
| 4399 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4400 | // Tests that the network delegate can block and cancel a request. |
| 4401 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4402 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4403 | |
| 4404 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4405 | BlockingNetworkDelegate network_delegate( |
| 4406 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4407 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 4408 | network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4409 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4410 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4411 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4412 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4413 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4414 | std::unique_ptr<URLRequest> r( |
| 4415 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4416 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4417 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4418 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4419 | d.RunUntilComplete(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4420 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4421 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4422 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4423 | EXPECT_EQ(ERR_EMPTY_RESPONSE, d.request_status()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4424 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4425 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4426 | } |
| 4427 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4428 | } |
| 4429 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4430 | // Helper function for NetworkDelegateCancelRequestAsynchronously and |
| 4431 | // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network |
| 4432 | // delegate operating in |block_mode| and a request for |url|. It blocks the |
| 4433 | // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT. |
| 4434 | void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode, |
| 4435 | BlockingNetworkDelegate::Stage stage, |
| 4436 | const GURL& url) { |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4437 | TestDelegate d; |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4438 | BlockingNetworkDelegate network_delegate(block_mode); |
| 4439 | network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT); |
| 4440 | network_delegate.set_block_on(stage); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4441 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4442 | TestURLRequestContext context(true); |
| 4443 | context.set_network_delegate(&network_delegate); |
| 4444 | context.Init(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4445 | |
| 4446 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4447 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4448 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4449 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4450 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4451 | d.RunUntilComplete(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4452 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4453 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4454 | if (stage == BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST || |
| 4455 | stage == BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS) { |
| 4456 | EXPECT_FALSE(r->proxy_server().is_valid()); |
| 4457 | } else if (stage == BlockingNetworkDelegate::ON_HEADERS_RECEIVED) { |
| 4458 | EXPECT_TRUE(r->proxy_server().is_direct()); |
| 4459 | } else { |
| 4460 | NOTREACHED(); |
| 4461 | } |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4462 | EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, d.request_status()); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4463 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4464 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4465 | } |
| 4466 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4467 | } |
| 4468 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4469 | // The following 3 tests check that the network delegate can cancel a request |
| 4470 | // synchronously in various stages of the request. |
| 4471 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4472 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4473 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4474 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4475 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4476 | } |
| 4477 | |
| 4478 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4479 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4480 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4481 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4482 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4483 | } |
| 4484 | |
| 4485 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4486 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4487 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4488 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4489 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4490 | } |
| 4491 | |
| 4492 | // The following 3 tests check that the network delegate can cancel a request |
| 4493 | // asynchronously in various stages of the request. |
| 4494 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4495 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4496 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4497 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4498 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4499 | } |
| 4500 | |
| 4501 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4502 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4503 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4504 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4505 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4506 | } |
| 4507 | |
| 4508 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4509 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4510 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4511 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4512 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4513 | } |
| 4514 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4515 | // Tests that the network delegate can block and redirect a request to a new |
| 4516 | // URL. |
| 4517 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4518 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4519 | |
| 4520 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4521 | BlockingNetworkDelegate network_delegate( |
| 4522 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4523 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4524 | GURL redirect_url("http://does.not.resolve.test/simple.html"); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4525 | network_delegate.set_redirect_url(redirect_url); |
| 4526 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4527 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4528 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4529 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4530 | { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4531 | GURL original_url("http://does.not.resolve.test/defaultresponse"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4532 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4533 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4534 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4535 | // Quit after hitting the redirect, so can check the headers. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4536 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 4537 | d.RunUntilRedirect(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4538 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4539 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4540 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4541 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4542 | std::string location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4543 | ASSERT_TRUE( |
| 4544 | r->response_headers()->EnumerateHeader(nullptr, "Location", &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4545 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4546 | |
| 4547 | // Let the request finish. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 4548 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 4549 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4550 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4551 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4552 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4553 | http_test_server()->host_port_pair()), |
| 4554 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4555 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4556 | // through an untunneled proxy. |
| 4557 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4558 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4559 | http_test_server()->host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 4560 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4561 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4562 | EXPECT_EQ(redirect_url, r->url()); |
| 4563 | EXPECT_EQ(original_url, r->original_url()); |
| 4564 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4565 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4566 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4567 | } |
| 4568 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4569 | } |
| 4570 | |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4571 | // Tests that the network delegate can block and redirect a request to a new |
| 4572 | // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 4573 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4574 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4575 | |
| 4576 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4577 | BlockingNetworkDelegate network_delegate( |
| 4578 | BlockingNetworkDelegate::SYNCHRONOUS); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4579 | GURL redirect_url("http://does.not.resolve.test/simple.html"); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4580 | network_delegate.set_redirect_url(redirect_url); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4581 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4582 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4583 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4584 | |
| 4585 | { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4586 | GURL original_url("http://does.not.resolve.test/defaultresponse"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4587 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4588 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4589 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4590 | // Quit after hitting the redirect, so can check the headers. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4591 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 4592 | d.RunUntilRedirect(); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4593 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4594 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4595 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4596 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4597 | std::string location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4598 | ASSERT_TRUE( |
| 4599 | r->response_headers()->EnumerateHeader(nullptr, "Location", &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4600 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4601 | |
| 4602 | // Let the request finish. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 4603 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 4604 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4605 | d.RunUntilComplete(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4606 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4607 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4608 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4609 | http_test_server()->host_port_pair()), |
| 4610 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4611 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4612 | // through an untunneled proxy. |
| 4613 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4614 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4615 | http_test_server()->host_port_pair())); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4616 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4617 | EXPECT_EQ(redirect_url, r->url()); |
| 4618 | EXPECT_EQ(original_url, r->original_url()); |
| 4619 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4620 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4621 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4622 | } |
| 4623 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4624 | } |
| 4625 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4626 | // Tests that redirects caused by the network delegate preserve POST data. |
| 4627 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4628 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4629 | |
| 4630 | const char kData[] = "hello world"; |
| 4631 | |
| 4632 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4633 | BlockingNetworkDelegate network_delegate( |
| 4634 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4635 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4636 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4637 | network_delegate.set_redirect_url(redirect_url); |
| 4638 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4639 | TestURLRequestContext context(true); |
| 4640 | context.set_network_delegate(&network_delegate); |
| 4641 | context.Init(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4642 | |
| 4643 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4644 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4645 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4646 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4647 | r->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4648 | r->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4649 | HttpRequestHeaders headers; |
| 4650 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 4651 | base::NumberToString(base::size(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4652 | r->SetExtraRequestHeaders(headers); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4653 | |
| 4654 | // Quit after hitting the redirect, so can check the headers. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4655 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 4656 | d.RunUntilRedirect(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4657 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4658 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4659 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4660 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4661 | std::string location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4662 | ASSERT_TRUE( |
| 4663 | r->response_headers()->EnumerateHeader(nullptr, "Location", &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4664 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4665 | |
| 4666 | // Let the request finish. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 4667 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 4668 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4669 | d.RunUntilComplete(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4670 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4671 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4672 | EXPECT_EQ(redirect_url, r->url()); |
| 4673 | EXPECT_EQ(original_url, r->original_url()); |
| 4674 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4675 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4676 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4677 | EXPECT_EQ("POST", r->method()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4678 | EXPECT_EQ(kData, d.data_received()); |
| 4679 | } |
| 4680 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4681 | } |
| 4682 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4683 | // Tests that the network delegate can block and redirect a request to a new |
| 4684 | // URL during OnHeadersReceived. |
| 4685 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4686 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4687 | |
| 4688 | TestDelegate d; |
| 4689 | BlockingNetworkDelegate network_delegate( |
| 4690 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4691 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4692 | GURL redirect_url("http://does.not.resolve.test/simple.html"); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4693 | network_delegate.set_redirect_on_headers_received_url(redirect_url); |
| 4694 | |
| 4695 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4696 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4697 | |
| 4698 | { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4699 | GURL original_url("http://does.not.resolve.test/defaultresponse"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4700 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4701 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4702 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4703 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4704 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4705 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4706 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4707 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4708 | http_test_server()->host_port_pair()), |
| 4709 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4710 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4711 | // through an untunneled proxy. |
| 4712 | EXPECT_EQ(2, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4713 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4714 | http_test_server()->host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4715 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4716 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4717 | EXPECT_EQ(redirect_url, r->url()); |
| 4718 | EXPECT_EQ(original_url, r->original_url()); |
| 4719 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4720 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 4721 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4722 | } |
| 4723 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4724 | } |
| 4725 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4726 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4727 | // by taking no action. This indicates that the NetworkDelegate does not want to |
| 4728 | // handle the challenge, and is passing the buck along to the |
| 4729 | // URLRequest::Delegate. |
| 4730 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4731 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4732 | |
| 4733 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4734 | BlockingNetworkDelegate network_delegate( |
| 4735 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4736 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4737 | TestURLRequestContext context(true); |
| 4738 | context.set_network_delegate(&network_delegate); |
| 4739 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4740 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4741 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4742 | |
| 4743 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4744 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4745 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4746 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4747 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4748 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4749 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4750 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4751 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4752 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4753 | EXPECT_TRUE(d.auth_required_called()); |
| 4754 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4755 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4756 | } |
| 4757 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4758 | } |
| 4759 | |
| 4760 | TEST_F(URLRequestTestHTTP, |
| 4761 | NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4762 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4763 | |
| 4764 | TestDelegate d; |
| 4765 | BlockingNetworkDelegate network_delegate( |
| 4766 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4767 | |
| 4768 | TestURLRequestContext context(true); |
| 4769 | context.set_network_delegate(&network_delegate); |
| 4770 | context.Init(); |
| 4771 | |
| 4772 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 4773 | |
| 4774 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4775 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4776 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4777 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4778 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4779 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4780 | d.RunUntilComplete(); |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4781 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4782 | { |
| 4783 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4784 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4785 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4786 | } |
| 4787 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4788 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4789 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4790 | EXPECT_TRUE(d.auth_required_called()); |
| 4791 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4792 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4793 | } |
| 4794 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4795 | } |
| 4796 | |
| 4797 | // Tests that the network delegate can synchronously complete OnAuthRequired |
[email protected] | 1e110eae | 2013-05-10 22:02:40 | [diff] [blame] | 4798 | // by setting credentials. |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4799 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4800 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4801 | |
| 4802 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4803 | BlockingNetworkDelegate network_delegate( |
| 4804 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4805 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4806 | network_delegate.set_auth_retval( |
| 4807 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4808 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4809 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4810 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4811 | TestURLRequestContext context(true); |
| 4812 | context.set_network_delegate(&network_delegate); |
| 4813 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4814 | |
| 4815 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4816 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4817 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4818 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4819 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4820 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4821 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4822 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4823 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4824 | EXPECT_FALSE(d.auth_required_called()); |
| 4825 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4826 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4827 | } |
| 4828 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4829 | } |
| 4830 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4831 | // Same as above, but also tests that GetFullRequestHeaders returns the proper |
| 4832 | // headers (for the first or second request) when called at the proper times. |
| 4833 | TEST_F(URLRequestTestHTTP, |
| 4834 | NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4835 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4836 | |
| 4837 | TestDelegate d; |
| 4838 | BlockingNetworkDelegate network_delegate( |
| 4839 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4840 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 4841 | network_delegate.set_auth_retval( |
| 4842 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4843 | |
| 4844 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
| 4845 | |
| 4846 | TestURLRequestContext context(true); |
| 4847 | context.set_network_delegate(&network_delegate); |
| 4848 | context.Init(); |
| 4849 | |
| 4850 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4851 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4852 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4853 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4854 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4855 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4856 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4857 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4858 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4859 | EXPECT_FALSE(d.auth_required_called()); |
| 4860 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4861 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4862 | |
| 4863 | { |
| 4864 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4865 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4866 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
| 4867 | } |
| 4868 | } |
| 4869 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4870 | } |
| 4871 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4872 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4873 | // by cancelling authentication. |
| 4874 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4875 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4876 | |
| 4877 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4878 | BlockingNetworkDelegate network_delegate( |
| 4879 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4880 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4881 | network_delegate.set_auth_retval( |
| 4882 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4883 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4884 | TestURLRequestContext context(true); |
| 4885 | context.set_network_delegate(&network_delegate); |
| 4886 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4887 | |
| 4888 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4889 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4890 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4891 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4892 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4893 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4894 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4895 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4896 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4897 | EXPECT_FALSE(d.auth_required_called()); |
| 4898 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4899 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4900 | } |
| 4901 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4902 | } |
| 4903 | |
| 4904 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4905 | // by taking no action. This indicates that the NetworkDelegate does not want |
| 4906 | // to handle the challenge, and is passing the buck along to the |
| 4907 | // URLRequest::Delegate. |
| 4908 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4909 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4910 | |
| 4911 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4912 | BlockingNetworkDelegate network_delegate( |
| 4913 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4914 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4915 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4916 | TestURLRequestContext context(true); |
| 4917 | context.set_network_delegate(&network_delegate); |
| 4918 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4919 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4920 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4921 | |
| 4922 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4923 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4924 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4925 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4926 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4927 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4928 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4929 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4930 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4931 | EXPECT_TRUE(d.auth_required_called()); |
| 4932 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4933 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4934 | } |
| 4935 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4936 | } |
| 4937 | |
| 4938 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4939 | // by setting credentials. |
| 4940 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4941 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4942 | |
| 4943 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4944 | BlockingNetworkDelegate network_delegate( |
| 4945 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4946 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4947 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4948 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4949 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4950 | AuthCredentials auth_credentials(kUser, kSecret); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4951 | network_delegate.set_auth_credentials(auth_credentials); |
| 4952 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4953 | TestURLRequestContext context(true); |
| 4954 | context.set_network_delegate(&network_delegate); |
| 4955 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4956 | |
| 4957 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4958 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4959 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4960 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4961 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4962 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4963 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4964 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4965 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4966 | EXPECT_FALSE(d.auth_required_called()); |
| 4967 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4968 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4969 | } |
| 4970 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4971 | } |
| 4972 | |
| 4973 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4974 | // by cancelling authentication. |
| 4975 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4976 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4977 | |
| 4978 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4979 | BlockingNetworkDelegate network_delegate( |
| 4980 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4981 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4982 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4983 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4984 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4985 | TestURLRequestContext context(true); |
| 4986 | context.set_network_delegate(&network_delegate); |
| 4987 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4988 | |
| 4989 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4990 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4991 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4992 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4993 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4994 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4995 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4996 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4997 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4998 | EXPECT_FALSE(d.auth_required_called()); |
| 4999 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5000 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5001 | } |
| 5002 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5003 | } |
| 5004 | |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5005 | // Tests that NetworkDelegate header overrides from the 401 response do not |
| 5006 | // affect the 200 response. This is a regression test for |
| 5007 | // https://crbug.com/801237. |
| 5008 | TEST_F(URLRequestTestHTTP, NetworkDelegateOverrideHeadersWithAuth) { |
| 5009 | ASSERT_TRUE(http_test_server()->Start()); |
| 5010 | |
| 5011 | TestDelegate d; |
| 5012 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 5013 | default_network_delegate_.set_add_header_to_first_response(true); |
| 5014 | |
| 5015 | { |
| 5016 | GURL url(http_test_server()->GetURL("/auth-basic")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5017 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5018 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 5019 | r->Start(); |
| 5020 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5021 | d.RunUntilComplete(); |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5022 | |
| 5023 | EXPECT_EQ(OK, d.request_status()); |
| 5024 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5025 | EXPECT_TRUE(d.auth_required_called()); |
| 5026 | EXPECT_FALSE(r->response_headers()->HasHeader("X-Network-Delegate")); |
| 5027 | } |
| 5028 | |
| 5029 | { |
| 5030 | GURL url(http_test_server()->GetURL("/defaultresponse")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5031 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5032 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 5033 | r->Start(); |
| 5034 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5035 | d.RunUntilComplete(); |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5036 | |
| 5037 | // Check that set_add_header_to_first_response normally adds a header. |
| 5038 | EXPECT_EQ(OK, d.request_status()); |
| 5039 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5040 | EXPECT_TRUE(r->response_headers()->HasHeader("X-Network-Delegate")); |
| 5041 | } |
| 5042 | } |
| 5043 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5044 | // Tests that we can handle when a network request was canceled while we were |
| 5045 | // waiting for the network delegate. |
| 5046 | // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 5047 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5048 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5049 | |
| 5050 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5051 | BlockingNetworkDelegate network_delegate( |
| 5052 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5053 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5054 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5055 | TestURLRequestContext context(true); |
| 5056 | context.set_network_delegate(&network_delegate); |
| 5057 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5058 | |
| 5059 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5060 | std::unique_ptr<URLRequest> r( |
| 5061 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 5062 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5063 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5064 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5065 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5066 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 5067 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5068 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5069 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5070 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5071 | // Ensure that network delegate is notified. |
| 5072 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5073 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5074 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5075 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5076 | } |
| 5077 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5078 | } |
| 5079 | |
| 5080 | // Tests that we can handle when a network request was canceled while we were |
| 5081 | // waiting for the network delegate. |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5082 | // Part 2: Request is cancelled while waiting for OnBeforeStartTransaction |
| 5083 | // callback. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5084 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5085 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5086 | |
| 5087 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5088 | BlockingNetworkDelegate network_delegate( |
| 5089 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5090 | network_delegate.set_block_on( |
| 5091 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5092 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5093 | TestURLRequestContext context(true); |
| 5094 | context.set_network_delegate(&network_delegate); |
| 5095 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5096 | |
| 5097 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5098 | std::unique_ptr<URLRequest> r( |
| 5099 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 5100 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5101 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5102 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5103 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5104 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 5105 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5106 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5107 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5108 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5109 | // Ensure that network delegate is notified. |
| 5110 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5111 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5112 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5113 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5114 | } |
| 5115 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5116 | } |
| 5117 | |
| 5118 | // Tests that we can handle when a network request was canceled while we were |
| 5119 | // waiting for the network delegate. |
| 5120 | // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 5121 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5122 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5123 | |
| 5124 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5125 | BlockingNetworkDelegate network_delegate( |
| 5126 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5127 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5128 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5129 | TestURLRequestContext context(true); |
| 5130 | context.set_network_delegate(&network_delegate); |
| 5131 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5132 | |
| 5133 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5134 | std::unique_ptr<URLRequest> r( |
| 5135 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 5136 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5137 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5138 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5139 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5140 | EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 5141 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5142 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5143 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5144 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5145 | // Ensure that network delegate is notified. |
| 5146 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5147 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5148 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5149 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5150 | } |
| 5151 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5152 | } |
| 5153 | |
| 5154 | // Tests that we can handle when a network request was canceled while we were |
| 5155 | // waiting for the network delegate. |
| 5156 | // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
[email protected] | bfe8b30 | 2013-02-15 12:16:02 | [diff] [blame] | 5157 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5158 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5159 | |
| 5160 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5161 | BlockingNetworkDelegate network_delegate( |
| 5162 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5163 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5164 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5165 | TestURLRequestContext context(true); |
| 5166 | context.set_network_delegate(&network_delegate); |
| 5167 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5168 | |
| 5169 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5170 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5171 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 5172 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5173 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5174 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5175 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5176 | EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED, |
| 5177 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5178 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5179 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5180 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5181 | // Ensure that network delegate is notified. |
| 5182 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5183 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5184 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5185 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5186 | } |
| 5187 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5188 | } |
| 5189 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5190 | namespace { |
| 5191 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5192 | std::unique_ptr<test_server::HttpResponse> HandleServerAuthConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5193 | const test_server::HttpRequest& request) { |
| 5194 | if (request.headers.find("Host") == request.headers.end() || |
| 5195 | request.headers.at("Host") != "www.server-auth.com" || |
| 5196 | request.method != test_server::METHOD_CONNECT) { |
| 5197 | return nullptr; |
| 5198 | } |
| 5199 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5200 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5201 | new test_server::BasicHttpResponse); |
| 5202 | http_response->set_code(HTTP_UNAUTHORIZED); |
| 5203 | http_response->AddCustomHeader("WWW-Authenticate", |
| 5204 | "Basic realm=\"WallyWorld\""); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 5205 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5206 | } |
| 5207 | |
| 5208 | } // namespace |
| 5209 | |
| 5210 | // 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] | 5211 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5212 | // The EmbeddedTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5213 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5214 | http_test_server()->RegisterRequestHandler( |
Matt Menke | 2b2a6047 | 2018-02-07 02:12:12 | [diff] [blame] | 5215 | base::BindRepeating(&HandleServerAuthConnect)); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5216 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5217 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5218 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 5219 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5220 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 5221 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5222 | TestDelegate d; |
| 5223 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5224 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5225 | GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d, |
| 5226 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5227 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5228 | r->Start(); |
| 5229 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5230 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5231 | d.RunUntilComplete(); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5232 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 5233 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 5234 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5235 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5236 | } |
| 5237 | } |
| 5238 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5239 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5240 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5241 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5242 | TestDelegate d; |
| 5243 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5244 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5245 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5246 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5247 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5248 | r->Start(); |
| 5249 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5250 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5251 | d.RunUntilComplete(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5252 | |
| 5253 | EXPECT_EQ(1, d.response_started_count()); |
| 5254 | EXPECT_FALSE(d.received_data_before_response()); |
| 5255 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5256 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5257 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5258 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5259 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 5260 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5261 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5262 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5263 | } |
| 5264 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5265 | // This test has the server send a large number of cookies to the client. |
| 5266 | // To ensure that no number of cookies causes a crash, a galloping binary |
| 5267 | // search is used to estimate that maximum number of cookies that are accepted |
| 5268 | // by the browser. Beyond the maximum number, the request will fail with |
| 5269 | // ERR_RESPONSE_HEADERS_TOO_BIG. |
Sergey Ulanov | 0fb900c | 2017-09-20 00:27:31 | [diff] [blame] | 5270 | #if defined(OS_WIN) || defined(OS_FUCHSIA) |
[email protected] | 69dd6fe | 2013-02-23 23:15:30 | [diff] [blame] | 5271 | // http://crbug.com/177916 |
| 5272 | #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies |
| 5273 | #else |
| 5274 | #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies |
| 5275 | #endif // defined(OS_WIN) |
| 5276 | TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5277 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5278 | |
| 5279 | int lower_bound = 0; |
| 5280 | int upper_bound = 1; |
| 5281 | |
| 5282 | // Double the number of cookies until the response header limits are |
| 5283 | // exceeded. |
| 5284 | while (DoManyCookiesRequest(upper_bound)) { |
| 5285 | lower_bound = upper_bound; |
| 5286 | upper_bound *= 2; |
| 5287 | ASSERT_LT(upper_bound, 1000000); |
| 5288 | } |
| 5289 | |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 5290 | int tolerance = static_cast<int>(upper_bound * 0.005); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5291 | if (tolerance < 2) |
| 5292 | tolerance = 2; |
| 5293 | |
| 5294 | // Perform a binary search to find the highest possible number of cookies, |
| 5295 | // within the desired tolerance. |
| 5296 | while (upper_bound - lower_bound >= tolerance) { |
| 5297 | int num_cookies = (lower_bound + upper_bound) / 2; |
| 5298 | |
| 5299 | if (DoManyCookiesRequest(num_cookies)) |
| 5300 | lower_bound = num_cookies; |
| 5301 | else |
| 5302 | upper_bound = num_cookies; |
| 5303 | } |
| 5304 | // Success: the test did not crash. |
| 5305 | } |
| 5306 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5307 | TEST_F(URLRequestTestHTTP, GetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5308 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5309 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5310 | TestDelegate d; |
| 5311 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5312 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5313 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5314 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5315 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5316 | r->Start(); |
| 5317 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5318 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5319 | d.RunUntilComplete(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5320 | |
| 5321 | EXPECT_EQ(1, d.response_started_count()); |
| 5322 | EXPECT_FALSE(d.received_data_before_response()); |
| 5323 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5324 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5325 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5326 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5327 | r->GetResponseRemoteEndpoint().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5328 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 5329 | } |
| 5330 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5331 | TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5332 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5333 | |
| 5334 | TestDelegate d; |
| 5335 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5336 | GURL test_url(http_test_server()->GetURL("/defaultresponse")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5337 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5338 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5339 | |
| 5340 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5341 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5342 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5343 | r->Start(); |
| 5344 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5345 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5346 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5347 | |
| 5348 | EXPECT_EQ(1, d.response_started_count()); |
| 5349 | EXPECT_FALSE(d.received_data_before_response()); |
| 5350 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5351 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5352 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5353 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5354 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5355 | |
| 5356 | EXPECT_TRUE(d.have_full_request_headers()); |
| 5357 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 5358 | } |
| 5359 | } |
| 5360 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5361 | TEST_F(URLRequestTestHTTP, GetTestLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5362 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5363 | |
| 5364 | TestDelegate d; |
| 5365 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5366 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5367 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5368 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5369 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5370 | r->Start(); |
| 5371 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5372 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5373 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5374 | |
| 5375 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5376 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5377 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5378 | |
| 5379 | EXPECT_EQ(1, d.response_started_count()); |
| 5380 | EXPECT_FALSE(d.received_data_before_response()); |
| 5381 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5382 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5383 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5384 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5385 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5386 | } |
| 5387 | } |
| 5388 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5389 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 5390 | #if !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5391 | TEST_F(URLRequestTestHTTP, GetZippedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5392 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5393 | base::FilePath(kTestFilePath)); |
| 5394 | |
| 5395 | ASSERT_TRUE(test_server.Start()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5396 | |
| 5397 | // Parameter that specifies the Content-Length field in the response: |
| 5398 | // C - Compressed length. |
| 5399 | // U - Uncompressed length. |
| 5400 | // L - Large length (larger than both C & U). |
| 5401 | // M - Medium length (between C & U). |
| 5402 | // S - Small length (smaller than both C & U). |
| 5403 | const char test_parameters[] = "CULMS"; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 5404 | const int num_tests = base::size(test_parameters) - 1; // Skip NULL. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5405 | // C & U should be OK. |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 5406 | // L & M are larger than the data sent, and show an error. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5407 | // S has too little data, but we seem to accept it. |
| 5408 | const bool test_expect_success[num_tests] = |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 5409 | { true, true, false, false, true }; |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5410 | |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5411 | base::FilePath file_path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 5412 | base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5413 | file_path = file_path.Append(kTestFilePath); |
| 5414 | file_path = file_path.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); |
| 5415 | std::string expected_content; |
| 5416 | ASSERT_TRUE(base::ReadFileToString(file_path, &expected_content)); |
| 5417 | |
| 5418 | for (int i = 0; i < num_tests; i++) { |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5419 | TestDelegate d; |
| 5420 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5421 | std::string test_file = base::StringPrintf( |
| 5422 | "compressedfiles/BullRunSpeech.txt?%c", test_parameters[i]); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5423 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5424 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5425 | TestURLRequestContext context(true); |
| 5426 | context.set_network_delegate(&network_delegate); |
| 5427 | context.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 5428 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5429 | std::unique_ptr<URLRequest> r( |
| 5430 | context.CreateRequest(test_server.GetURL(test_file), DEFAULT_PRIORITY, |
| 5431 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5432 | r->Start(); |
| 5433 | EXPECT_TRUE(r->is_pending()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5434 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5435 | d.RunUntilComplete(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5436 | |
| 5437 | EXPECT_EQ(1, d.response_started_count()); |
| 5438 | EXPECT_FALSE(d.received_data_before_response()); |
| 5439 | VLOG(1) << " Received " << d.bytes_received() << " bytes" |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5440 | << " error = " << d.request_status(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5441 | if (test_expect_success[i]) { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5442 | EXPECT_EQ(OK, d.request_status()) << " Parameter = \"" << test_file |
| 5443 | << "\""; |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5444 | if (test_parameters[i] == 'S') { |
| 5445 | // When content length is smaller than both compressed length and |
| 5446 | // uncompressed length, HttpStreamParser might not read the full |
| 5447 | // response body. |
| 5448 | continue; |
| 5449 | } |
| 5450 | EXPECT_EQ(expected_content, d.data_received()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5451 | } else { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5452 | EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, d.request_status()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5453 | << " Parameter = \"" << test_file << "\""; |
| 5454 | } |
| 5455 | } |
| 5456 | } |
| 5457 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5458 | #endif // !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5459 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5460 | TEST_F(URLRequestTestHTTP, RedirectLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5461 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5462 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5463 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5464 | GURL original_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5465 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5466 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5467 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5468 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5469 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5470 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5471 | |
| 5472 | EXPECT_EQ(1, d.response_started_count()); |
| 5473 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5474 | EXPECT_EQ(destination_url, req->url()); |
| 5475 | EXPECT_EQ(original_url, req->original_url()); |
| 5476 | ASSERT_EQ(2U, req->url_chain().size()); |
| 5477 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5478 | EXPECT_EQ(destination_url, req->url_chain()[1]); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5479 | |
| 5480 | LoadTimingInfo load_timing_info_before_redirect; |
| 5481 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect( |
| 5482 | &load_timing_info_before_redirect)); |
| 5483 | TestLoadTimingNotReused(load_timing_info_before_redirect, |
| 5484 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 5485 | |
| 5486 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5487 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5488 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5489 | |
| 5490 | // Check that a new socket was used on redirect, since the server does not |
| 5491 | // supposed keep-alive sockets, and that the times before the redirect are |
| 5492 | // before the ones recorded for the second request. |
| 5493 | EXPECT_NE(load_timing_info_before_redirect.socket_log_id, |
| 5494 | load_timing_info.socket_log_id); |
| 5495 | EXPECT_LE(load_timing_info_before_redirect.receive_headers_end, |
| 5496 | load_timing_info.connect_timing.connect_start); |
| 5497 | } |
| 5498 | |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5499 | TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5500 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5501 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5502 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5503 | GURL middle_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5504 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
| 5505 | GURL original_url = http_test_server()->GetURL("/server-redirect?" + |
| 5506 | middle_redirect_url.spec()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5507 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5508 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5509 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5510 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5511 | d.RunUntilComplete(); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5512 | |
| 5513 | EXPECT_EQ(1, d.response_started_count()); |
| 5514 | EXPECT_EQ(2, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5515 | EXPECT_EQ(destination_url, req->url()); |
| 5516 | EXPECT_EQ(original_url, req->original_url()); |
| 5517 | ASSERT_EQ(3U, req->url_chain().size()); |
| 5518 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5519 | EXPECT_EQ(middle_redirect_url, req->url_chain()[1]); |
| 5520 | EXPECT_EQ(destination_url, req->url_chain()[2]); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5521 | } |
| 5522 | |
David Benjamin | a7fde61 | 2019-03-15 14:20:58 | [diff] [blame^] | 5523 | // This is a regression test for https://crbug.com/942073. |
| 5524 | TEST_F(URLRequestTestHTTP, RedirectEscaping) { |
| 5525 | ASSERT_TRUE(http_test_server()->Start()); |
| 5526 | |
| 5527 | // Assemble the destination URL as a string so it is not escaped by GURL. |
| 5528 | GURL destination_base = http_test_server()->GetURL("/defaultresponse"); |
| 5529 | // Add a URL fragment of U+2603 unescaped, U+2603 escaped, and then a UTF-8 |
| 5530 | // encoding error. |
| 5531 | std::string destination_url = |
| 5532 | destination_base.spec() + "#\xE2\x98\x83_%E2%98%83_\xE0\xE0"; |
| 5533 | // Redirect resolution should percent-escape bytes and preserve the UTF-8 |
| 5534 | // error at the end. |
| 5535 | std::string destination_escaped = |
| 5536 | destination_base.spec() + "#%E2%98%83_%E2%98%83_%E0%E0"; |
| 5537 | GURL original_url = http_test_server()->GetURL( |
| 5538 | "/server-redirect?" + EscapeQueryParamValue(destination_url, false)); |
| 5539 | TestDelegate d; |
| 5540 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 5541 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 5542 | req->Start(); |
| 5543 | d.RunUntilComplete(); |
| 5544 | |
| 5545 | EXPECT_EQ(1, d.response_started_count()); |
| 5546 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5547 | EXPECT_EQ(destination_escaped, req->url().spec()); |
| 5548 | EXPECT_EQ(original_url, req->original_url()); |
| 5549 | ASSERT_EQ(2U, req->url_chain().size()); |
| 5550 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5551 | EXPECT_EQ(destination_escaped, req->url_chain()[1].spec()); |
| 5552 | } |
| 5553 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5554 | // First and second pieces of information logged by delegates to URLRequests. |
| 5555 | const char kFirstDelegateInfo[] = "Wonderful delegate"; |
| 5556 | const char kSecondDelegateInfo[] = "Exciting delegate"; |
| 5557 | |
| 5558 | // Logs delegate information to a URLRequest. The first string is logged |
| 5559 | // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is |
| 5560 | // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then |
| 5561 | // another asynchronous call is used to clear the delegate information |
| 5562 | // before calling a callback. The object then deletes itself. |
| 5563 | class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> { |
| 5564 | public: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5565 | using Callback = base::OnceCallback<void()>; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5566 | |
| 5567 | // Each time delegate information is added to the URLRequest, the resulting |
| 5568 | // load state is checked. The expected load state after each request is |
| 5569 | // passed in as an argument. |
| 5570 | static void Run(URLRequest* url_request, |
| 5571 | LoadState expected_first_load_state, |
| 5572 | LoadState expected_second_load_state, |
| 5573 | LoadState expected_third_load_state, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5574 | Callback callback) { |
tzik | 38f920a | 2018-08-01 04:41:20 | [diff] [blame] | 5575 | // base::MakeRefCounted<AsyncDelegateLogger> is unavailable here, since the |
| 5576 | // constructor of AsyncDelegateLogger is private. |
| 5577 | auto logger = base::WrapRefCounted(new AsyncDelegateLogger( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5578 | url_request, expected_first_load_state, expected_second_load_state, |
tzik | 38f920a | 2018-08-01 04:41:20 | [diff] [blame] | 5579 | expected_third_load_state, std::move(callback))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5580 | logger->Start(); |
| 5581 | } |
| 5582 | |
| 5583 | // Checks that the log entries, starting with log_position, contain the |
| 5584 | // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have |
| 5585 | // recorded. Returns the index of entry after the expected number of |
| 5586 | // events this logged, or entries.size() if there aren't enough entries. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5587 | static size_t CheckDelegateInfo(const TestNetLogEntry::List& entries, |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 5588 | size_t log_position) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5589 | // There should be 4 DELEGATE_INFO events: Two begins and two ends. |
| 5590 | if (log_position + 3 >= entries.size()) { |
| 5591 | ADD_FAILURE() << "Not enough log entries"; |
| 5592 | return entries.size(); |
| 5593 | } |
| 5594 | std::string delegate_info; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5595 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5596 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
rdsmith | 37a0dde | 2017-01-04 00:12:07 | [diff] [blame] | 5597 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_blocked_by", |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5598 | &delegate_info)); |
| 5599 | EXPECT_EQ(kFirstDelegateInfo, delegate_info); |
| 5600 | |
| 5601 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5602 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5603 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5604 | |
| 5605 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5606 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5607 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
rdsmith | 37a0dde | 2017-01-04 00:12:07 | [diff] [blame] | 5608 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_blocked_by", |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5609 | &delegate_info)); |
| 5610 | EXPECT_EQ(kSecondDelegateInfo, delegate_info); |
| 5611 | |
| 5612 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5613 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5614 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5615 | |
| 5616 | return log_position + 1; |
| 5617 | } |
| 5618 | |
| 5619 | private: |
| 5620 | friend class base::RefCounted<AsyncDelegateLogger>; |
| 5621 | |
| 5622 | AsyncDelegateLogger(URLRequest* url_request, |
| 5623 | LoadState expected_first_load_state, |
| 5624 | LoadState expected_second_load_state, |
| 5625 | LoadState expected_third_load_state, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5626 | Callback callback) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5627 | : url_request_(url_request), |
| 5628 | expected_first_load_state_(expected_first_load_state), |
| 5629 | expected_second_load_state_(expected_second_load_state), |
| 5630 | expected_third_load_state_(expected_third_load_state), |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5631 | callback_(std::move(callback)) {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5632 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5633 | ~AsyncDelegateLogger() = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5634 | |
| 5635 | void Start() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5636 | url_request_->LogBlockedBy(kFirstDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5637 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5638 | EXPECT_EQ(expected_first_load_state_, load_state.state); |
| 5639 | EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5640 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5641 | FROM_HERE, |
| 5642 | base::BindOnce(&AsyncDelegateLogger::LogSecondDelegate, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5643 | } |
| 5644 | |
| 5645 | void LogSecondDelegate() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5646 | url_request_->LogAndReportBlockedBy(kSecondDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5647 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5648 | EXPECT_EQ(expected_second_load_state_, load_state.state); |
| 5649 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) { |
| 5650 | EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5651 | } else { |
| 5652 | EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5653 | } |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5654 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5655 | FROM_HERE, base::BindOnce(&AsyncDelegateLogger::LogComplete, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5656 | } |
| 5657 | |
| 5658 | void LogComplete() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5659 | url_request_->LogUnblocked(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5660 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5661 | EXPECT_EQ(expected_third_load_state_, load_state.state); |
| 5662 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5663 | EXPECT_EQ(base::string16(), load_state.param); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5664 | std::move(callback_).Run(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5665 | } |
| 5666 | |
| 5667 | URLRequest* url_request_; |
| 5668 | const int expected_first_load_state_; |
| 5669 | const int expected_second_load_state_; |
| 5670 | const int expected_third_load_state_; |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5671 | Callback callback_; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5672 | |
| 5673 | DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger); |
| 5674 | }; |
| 5675 | |
| 5676 | // NetworkDelegate that logs delegate information before a request is started, |
| 5677 | // before headers are sent, when headers are read, and when auth information |
| 5678 | // is requested. Uses AsyncDelegateLogger. |
| 5679 | class AsyncLoggingNetworkDelegate : public TestNetworkDelegate { |
| 5680 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5681 | AsyncLoggingNetworkDelegate() = default; |
| 5682 | ~AsyncLoggingNetworkDelegate() override = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5683 | |
| 5684 | // NetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5685 | int OnBeforeURLRequest(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5686 | CompletionOnceCallback callback, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5687 | GURL* new_url) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5688 | // TestNetworkDelegate always completes synchronously. |
| 5689 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeURLRequest( |
| 5690 | request, base::NullCallback(), new_url)); |
| 5691 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5692 | } |
| 5693 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5694 | int OnBeforeStartTransaction(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5695 | CompletionOnceCallback callback, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5696 | HttpRequestHeaders* headers) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5697 | // TestNetworkDelegate always completes synchronously. |
| 5698 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeStartTransaction( |
| 5699 | request, base::NullCallback(), headers)); |
| 5700 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5701 | } |
| 5702 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5703 | int OnHeadersReceived( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5704 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5705 | CompletionOnceCallback callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5706 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5707 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5708 | GURL* allowed_unsafe_redirect_url) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5709 | // TestNetworkDelegate always completes synchronously. |
| 5710 | CHECK_NE(ERR_IO_PENDING, |
| 5711 | TestNetworkDelegate::OnHeadersReceived( |
| 5712 | request, base::NullCallback(), original_response_headers, |
| 5713 | override_response_headers, allowed_unsafe_redirect_url)); |
| 5714 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5715 | } |
| 5716 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5717 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5718 | URLRequest* request, |
| 5719 | const AuthChallengeInfo& auth_info, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5720 | AuthCallback callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5721 | AuthCredentials* credentials) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5722 | AsyncDelegateLogger::Run( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5723 | request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5724 | LOAD_STATE_WAITING_FOR_DELEGATE, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5725 | base::BindOnce(&AsyncLoggingNetworkDelegate::SetAuthAndResume, |
| 5726 | std::move(callback), credentials)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5727 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 5728 | } |
| 5729 | |
| 5730 | private: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5731 | static int RunCallbackAsynchronously(URLRequest* request, |
| 5732 | CompletionOnceCallback callback) { |
| 5733 | AsyncDelegateLogger::Run(request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5734 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5735 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5736 | base::BindOnce(std::move(callback), OK)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5737 | return ERR_IO_PENDING; |
| 5738 | } |
| 5739 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5740 | static void SetAuthAndResume(AuthCallback callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5741 | AuthCredentials* credentials) { |
| 5742 | *credentials = AuthCredentials(kUser, kSecret); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5743 | std::move(callback).Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5744 | } |
| 5745 | |
| 5746 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate); |
| 5747 | }; |
| 5748 | |
| 5749 | // URLRequest::Delegate that logs delegate information when the headers |
| 5750 | // are received, when each read completes, and during redirects. Uses |
| 5751 | // AsyncDelegateLogger. Can optionally cancel a request in any phase. |
| 5752 | // |
| 5753 | // Inherits from TestDelegate to reuse the TestDelegate code to handle |
| 5754 | // advancing to the next step in most cases, as well as cancellation. |
| 5755 | class AsyncLoggingUrlRequestDelegate : public TestDelegate { |
| 5756 | public: |
| 5757 | enum CancelStage { |
| 5758 | NO_CANCEL = 0, |
| 5759 | CANCEL_ON_RECEIVED_REDIRECT, |
| 5760 | CANCEL_ON_RESPONSE_STARTED, |
| 5761 | CANCEL_ON_READ_COMPLETED |
| 5762 | }; |
| 5763 | |
| 5764 | explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage) |
| 5765 | : cancel_stage_(cancel_stage) { |
| 5766 | if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT) |
| 5767 | set_cancel_in_received_redirect(true); |
| 5768 | else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED) |
| 5769 | set_cancel_in_response_started(true); |
| 5770 | else if (cancel_stage == CANCEL_ON_READ_COMPLETED) |
| 5771 | set_cancel_in_received_data(true); |
| 5772 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5773 | ~AsyncLoggingUrlRequestDelegate() override = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5774 | |
| 5775 | // URLRequest::Delegate implementation: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5776 | void OnReceivedRedirect(URLRequest* request, |
| 5777 | const RedirectInfo& redirect_info, |
| 5778 | bool* defer_redirect) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5779 | *defer_redirect = true; |
| 5780 | AsyncDelegateLogger::Run( |
| 5781 | request, |
| 5782 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5783 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5784 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5785 | base::Bind( |
| 5786 | &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5787 | base::Unretained(this), request, redirect_info)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5788 | } |
| 5789 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5790 | void OnResponseStarted(URLRequest* request, int net_error) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5791 | AsyncDelegateLogger::Run( |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5792 | request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5793 | LOAD_STATE_WAITING_FOR_DELEGATE, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5794 | base::Bind( |
| 5795 | &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete, |
| 5796 | base::Unretained(this), request, net_error)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5797 | } |
| 5798 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5799 | void OnReadCompleted(URLRequest* request, int bytes_read) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5800 | AsyncDelegateLogger::Run( |
| 5801 | request, |
| 5802 | LOAD_STATE_IDLE, |
| 5803 | LOAD_STATE_IDLE, |
| 5804 | LOAD_STATE_IDLE, |
| 5805 | base::Bind( |
| 5806 | &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete, |
| 5807 | base::Unretained(this), request, bytes_read)); |
| 5808 | } |
| 5809 | |
| 5810 | private: |
| 5811 | void OnReceivedRedirectLoggingComplete(URLRequest* request, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5812 | const RedirectInfo& redirect_info) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5813 | bool defer_redirect = false; |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5814 | TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5815 | // FollowDeferredRedirect should not be called after cancellation. |
| 5816 | if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT) |
| 5817 | return; |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 5818 | if (!defer_redirect) { |
| 5819 | request->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 5820 | base::nullopt /* modified_headers */); |
| 5821 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5822 | } |
| 5823 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5824 | void OnResponseStartedLoggingComplete(URLRequest* request, int net_error) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5825 | // The parent class continues the request. |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5826 | TestDelegate::OnResponseStarted(request, net_error); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5827 | } |
| 5828 | |
| 5829 | void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) { |
| 5830 | // The parent class continues the request. |
| 5831 | TestDelegate::OnReadCompleted(request, bytes_read); |
| 5832 | } |
| 5833 | |
| 5834 | const CancelStage cancel_stage_; |
| 5835 | |
| 5836 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate); |
| 5837 | }; |
| 5838 | |
| 5839 | // Tests handling of delegate info before a request starts. |
| 5840 | TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5841 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5842 | |
| 5843 | TestDelegate request_delegate; |
| 5844 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 5845 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5846 | context.set_net_log(&net_log_); |
| 5847 | context.Init(); |
| 5848 | |
| 5849 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5850 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5851 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, |
| 5852 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5853 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5854 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5855 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5856 | |
| 5857 | AsyncDelegateLogger::Run( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5858 | r.get(), |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5859 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5860 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5861 | LOAD_STATE_IDLE, |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5862 | base::Bind(&URLRequest::Start, base::Unretained(r.get()))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5863 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5864 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5865 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5866 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5867 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5868 | } |
| 5869 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5870 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5871 | net_log_.GetEntries(&entries); |
| 5872 | size_t log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5873 | entries, 0, NetLogEventType::DELEGATE_INFO, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5874 | |
| 5875 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position); |
| 5876 | |
| 5877 | // Nothing else should add any delegate info to the request. |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5878 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5879 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5880 | } |
| 5881 | |
| 5882 | // Tests handling of delegate info from a network delegate. |
| 5883 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5884 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5885 | |
| 5886 | TestDelegate request_delegate; |
| 5887 | AsyncLoggingNetworkDelegate network_delegate; |
| 5888 | TestURLRequestContext context(true); |
| 5889 | context.set_network_delegate(&network_delegate); |
| 5890 | context.set_net_log(&net_log_); |
| 5891 | context.Init(); |
| 5892 | |
| 5893 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5894 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5895 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, |
| 5896 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5897 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5898 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5899 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5900 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5901 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5902 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5903 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5904 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5905 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5906 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5907 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5908 | } |
| 5909 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5910 | |
| 5911 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5912 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5913 | net_log_.GetEntries(&entries); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5914 | static const NetLogEventType kExpectedEvents[] = { |
| 5915 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 5916 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 5917 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 5918 | }; |
| 5919 | for (NetLogEventType event : kExpectedEvents) { |
| 5920 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5921 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5922 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5923 | |
| 5924 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5925 | log_position + 1); |
| 5926 | |
| 5927 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5928 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5929 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5930 | } |
| 5931 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5932 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5933 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5934 | } |
| 5935 | |
| 5936 | // Tests handling of delegate info from a network delegate in the case of an |
| 5937 | // HTTP redirect. |
| 5938 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5939 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5940 | |
| 5941 | TestDelegate request_delegate; |
| 5942 | AsyncLoggingNetworkDelegate network_delegate; |
| 5943 | TestURLRequestContext context(true); |
| 5944 | context.set_network_delegate(&network_delegate); |
| 5945 | context.set_net_log(&net_log_); |
| 5946 | context.Init(); |
| 5947 | |
| 5948 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5949 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5950 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5951 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5952 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5953 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5954 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5955 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5956 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5957 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5958 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5959 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5960 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5961 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 5962 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5963 | } |
| 5964 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5965 | |
| 5966 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5967 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5968 | net_log_.GetEntries(&entries); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5969 | static const NetLogEventType kExpectedEvents[] = { |
| 5970 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 5971 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 5972 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 5973 | }; |
| 5974 | for (NetLogEventType event : kExpectedEvents) { |
| 5975 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5976 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5977 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5978 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5979 | log_position = |
| 5980 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5981 | |
| 5982 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5983 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5984 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5985 | } |
| 5986 | |
| 5987 | // The URLRequest::Delegate then gets informed about the redirect. |
| 5988 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5989 | entries, log_position + 1, |
| 5990 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5991 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5992 | |
| 5993 | // The NetworkDelegate logged information in the same three events as before. |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5994 | for (NetLogEventType event : kExpectedEvents) { |
| 5995 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5996 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5997 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5998 | |
| 5999 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 6000 | log_position + 1); |
| 6001 | |
| 6002 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6003 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6004 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6005 | } |
| 6006 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6007 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6008 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6009 | } |
| 6010 | |
| 6011 | // Tests handling of delegate info from a network delegate in the case of HTTP |
| 6012 | // AUTH. |
| 6013 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6014 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6015 | |
| 6016 | TestDelegate request_delegate; |
| 6017 | AsyncLoggingNetworkDelegate network_delegate; |
| 6018 | TestURLRequestContext context(true); |
| 6019 | context.set_network_delegate(&network_delegate); |
| 6020 | context.set_net_log(&net_log_); |
| 6021 | context.Init(); |
| 6022 | |
| 6023 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6024 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 6025 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, |
| 6026 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6027 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6028 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 6029 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6030 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6031 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6032 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6033 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6034 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6035 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6036 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 6037 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 6038 | } |
| 6039 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 6040 | |
| 6041 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6042 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6043 | net_log_.GetEntries(&entries); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6044 | static const NetLogEventType kExpectedEvents[] = { |
| 6045 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 6046 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6047 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6048 | NetLogEventType::NETWORK_DELEGATE_AUTH_REQUIRED, |
| 6049 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6050 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6051 | }; |
| 6052 | for (NetLogEventType event : kExpectedEvents) { |
| 6053 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6054 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6055 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6056 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6057 | log_position = |
| 6058 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6059 | |
| 6060 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6061 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6062 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6063 | } |
| 6064 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6065 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6066 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6067 | } |
| 6068 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6069 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 6070 | #if !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6071 | // Tests handling of delegate info from a URLRequest::Delegate. |
| 6072 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6073 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6074 | base::FilePath(kTestFilePath)); |
| 6075 | |
| 6076 | ASSERT_TRUE(test_server.Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6077 | |
| 6078 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 6079 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 6080 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6081 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6082 | context.set_net_log(&net_log_); |
| 6083 | context.Init(); |
| 6084 | |
| 6085 | { |
| 6086 | // A chunked response with delays between chunks is used to make sure that |
| 6087 | // attempts by the URLRequest delegate to log information while reading the |
| 6088 | // body are ignored. Since they are ignored, this test is robust against |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6089 | // the possibility of multiple reads being combined in the unlikely event |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6090 | // that it occurs. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6091 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6092 | test_server.GetURL("/chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6093 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6094 | LoadStateWithParam load_state = r->GetLoadState(); |
| 6095 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6096 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6097 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6098 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6099 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6100 | } |
| 6101 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6102 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6103 | net_log_.GetEntries(&entries); |
| 6104 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6105 | size_t log_position = 0; |
| 6106 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6107 | // The delegate info should only have been logged on header complete. Other |
| 6108 | // times it should silently be ignored. |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6109 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 6110 | entries, 0, NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST)); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6111 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6112 | entries, log_position + 1, |
| 6113 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6114 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6115 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6116 | log_position = |
| 6117 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6118 | |
| 6119 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6120 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 6121 | entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6122 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6123 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6124 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6125 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6126 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6127 | entries, log_position + 1, |
| 6128 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6129 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6130 | #endif // !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6131 | |
| 6132 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 6133 | // an HTTP redirect. |
| 6134 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6135 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6136 | |
| 6137 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 6138 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 6139 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6140 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6141 | context.set_net_log(&net_log_); |
| 6142 | context.Init(); |
| 6143 | |
| 6144 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6145 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6146 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6147 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6148 | LoadStateWithParam load_state = r->GetLoadState(); |
| 6149 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6150 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6151 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6152 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6153 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6154 | } |
| 6155 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6156 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6157 | net_log_.GetEntries(&entries); |
| 6158 | |
| 6159 | // Delegate info should only have been logged in OnReceivedRedirect and |
| 6160 | // OnResponseStarted. |
| 6161 | size_t log_position = 0; |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6162 | static const NetLogEventType kExpectedEvents[] = { |
| 6163 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
| 6164 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 6165 | }; |
| 6166 | for (NetLogEventType event : kExpectedEvents) { |
| 6167 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
| 6168 | log_position = ExpectLogContainsSomewhereAfter(entries, log_position, event, |
| 6169 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6170 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6171 | log_position = |
| 6172 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6173 | |
| 6174 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6175 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6176 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6177 | } |
| 6178 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6179 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6180 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6181 | } |
| 6182 | |
| 6183 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 6184 | // an HTTP redirect, with cancellation at various points. |
| 6185 | TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6186 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6187 | |
| 6188 | const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = { |
| 6189 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT, |
| 6190 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED, |
| 6191 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED, |
| 6192 | }; |
| 6193 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6194 | for (auto cancel_stage : kCancelStages) { |
| 6195 | AsyncLoggingUrlRequestDelegate request_delegate(cancel_stage); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 6196 | TestNetLog net_log; |
krasin | 0bfeb6b | 2017-01-13 21:48:04 | [diff] [blame] | 6197 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6198 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6199 | context.set_net_log(&net_log); |
| 6200 | context.Init(); |
| 6201 | |
| 6202 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6203 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6204 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6205 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6206 | LoadStateWithParam load_state = r->GetLoadState(); |
| 6207 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6208 | request_delegate.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6209 | EXPECT_EQ(ERR_ABORTED, request_delegate.request_status()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6210 | |
| 6211 | // Spin the message loop to run AsyncDelegateLogger task(s) posted after |
| 6212 | // the |request_delegate| completion task. |
| 6213 | base::RunLoop().RunUntilIdle(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6214 | } |
| 6215 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6216 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6217 | net_log.GetEntries(&entries); |
| 6218 | |
| 6219 | // Delegate info is always logged in both OnReceivedRedirect and |
| 6220 | // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the |
| 6221 | // OnResponseStarted delegate call is after cancellation, but logging is |
| 6222 | // still currently supported in that call. |
| 6223 | size_t log_position = 0; |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6224 | static const NetLogEventType kExpectedEvents[] = { |
| 6225 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
| 6226 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 6227 | }; |
| 6228 | for (NetLogEventType event : kExpectedEvents) { |
| 6229 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6230 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6231 | entries, log_position, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6232 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6233 | log_position = |
| 6234 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6235 | |
| 6236 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6237 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6238 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6239 | } |
| 6240 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6241 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6242 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6243 | } |
| 6244 | } |
| 6245 | |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6246 | namespace { |
| 6247 | |
| 6248 | const char kExtraHeader[] = "Allow-Snafu"; |
| 6249 | const char kExtraValue[] = "fubar"; |
| 6250 | |
| 6251 | class RedirectWithAdditionalHeadersDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 6252 | void OnReceivedRedirect(URLRequest* request, |
| 6253 | const RedirectInfo& redirect_info, |
| 6254 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6255 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6256 | request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false); |
| 6257 | } |
| 6258 | }; |
| 6259 | |
| 6260 | } // namespace |
| 6261 | |
| 6262 | TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6263 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6264 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6265 | GURL destination_url = |
| 6266 | http_test_server()->GetURL("/echoheader?" + std::string(kExtraHeader)); |
| 6267 | GURL original_url = |
| 6268 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6269 | RedirectWithAdditionalHeadersDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6270 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6271 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6272 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6273 | d.RunUntilComplete(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6274 | |
| 6275 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6276 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6277 | EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value)); |
| 6278 | EXPECT_EQ(kExtraValue, value); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6279 | EXPECT_FALSE(req->is_pending()); |
| 6280 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6281 | EXPECT_EQ(kExtraValue, d.data_received()); |
| 6282 | } |
| 6283 | |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6284 | namespace { |
| 6285 | |
| 6286 | const char kExtraHeaderToRemove[] = "To-Be-Removed"; |
| 6287 | |
| 6288 | class RedirectWithHeaderRemovalDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 6289 | void OnReceivedRedirect(URLRequest* request, |
| 6290 | const RedirectInfo& redirect_info, |
| 6291 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6292 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6293 | request->RemoveRequestHeaderByName(kExtraHeaderToRemove); |
| 6294 | } |
| 6295 | }; |
| 6296 | |
| 6297 | } // namespace |
| 6298 | |
| 6299 | TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6300 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6301 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6302 | GURL destination_url = http_test_server()->GetURL( |
| 6303 | "/echoheader?" + std::string(kExtraHeaderToRemove)); |
| 6304 | GURL original_url = |
| 6305 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6306 | RedirectWithHeaderRemovalDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6307 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6308 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6309 | req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false); |
| 6310 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6311 | d.RunUntilComplete(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6312 | |
| 6313 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6314 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6315 | EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6316 | EXPECT_FALSE(req->is_pending()); |
| 6317 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6318 | EXPECT_EQ("None", d.data_received()); |
| 6319 | } |
| 6320 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6321 | TEST_F(URLRequestTestHTTP, CancelAfterStart) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6322 | TestDelegate d; |
| 6323 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6324 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6325 | GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d, |
| 6326 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6327 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6328 | r->Start(); |
| 6329 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6330 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6331 | r->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6332 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6333 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6334 | |
| 6335 | // We expect to receive OnResponseStarted even though the request has been |
| 6336 | // cancelled. |
| 6337 | EXPECT_EQ(1, d.response_started_count()); |
| 6338 | EXPECT_EQ(0, d.bytes_received()); |
| 6339 | EXPECT_FALSE(d.received_data_before_response()); |
| 6340 | } |
| 6341 | } |
| 6342 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6343 | TEST_F(URLRequestTestHTTP, CancelInResponseStarted) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6344 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6345 | |
| 6346 | TestDelegate d; |
| 6347 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6348 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6349 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6350 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6351 | |
| 6352 | d.set_cancel_in_response_started(true); |
| 6353 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6354 | r->Start(); |
| 6355 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6356 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6357 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6358 | |
| 6359 | EXPECT_EQ(1, d.response_started_count()); |
| 6360 | EXPECT_EQ(0, d.bytes_received()); |
| 6361 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6362 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6363 | } |
| 6364 | } |
| 6365 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6366 | TEST_F(URLRequestTestHTTP, CancelOnDataReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6367 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6368 | |
| 6369 | TestDelegate d; |
| 6370 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6371 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6372 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 6373 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6374 | |
| 6375 | d.set_cancel_in_received_data(true); |
| 6376 | |
| 6377 | r->Start(); |
| 6378 | EXPECT_TRUE(r->is_pending()); |
| 6379 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6380 | d.RunUntilComplete(); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6381 | |
| 6382 | EXPECT_EQ(1, d.response_started_count()); |
| 6383 | EXPECT_NE(0, d.received_bytes_count()); |
| 6384 | EXPECT_FALSE(d.received_data_before_response()); |
| 6385 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
| 6386 | } |
| 6387 | } |
| 6388 | |
| 6389 | TEST_F(URLRequestTestHTTP, CancelDuringEofRead) { |
| 6390 | ASSERT_TRUE(http_test_server()->Start()); |
| 6391 | |
| 6392 | TestDelegate d; |
| 6393 | { |
| 6394 | // This returns an empty response (With headers). |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6395 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6396 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6397 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6398 | |
| 6399 | d.set_cancel_in_received_data(true); |
| 6400 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6401 | r->Start(); |
| 6402 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6403 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6404 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6405 | |
| 6406 | EXPECT_EQ(1, d.response_started_count()); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6407 | EXPECT_EQ(0, d.received_bytes_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6408 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6409 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6410 | } |
| 6411 | } |
| 6412 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6413 | TEST_F(URLRequestTestHTTP, CancelByDestroyingAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6414 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6415 | |
| 6416 | TestDelegate d; |
| 6417 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6418 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6419 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6420 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6421 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6422 | r->Start(); |
| 6423 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6424 | |
| 6425 | // The request will be implicitly canceled when it is destroyed. The |
| 6426 | // test delegate must not post a quit message when this happens because |
| 6427 | // this test doesn't actually have a message loop. The quit message would |
| 6428 | // get put on this thread's message queue and the next test would exit |
| 6429 | // early, causing problems. |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 6430 | d.set_on_complete(base::DoNothing()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6431 | } |
| 6432 | // expect things to just cleanup properly. |
| 6433 | |
kimwjdalsl | 2bb4ff0 | 2015-12-16 22:06:02 | [diff] [blame] | 6434 | // 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] | 6435 | // message loop |
| 6436 | EXPECT_FALSE(d.received_data_before_response()); |
| 6437 | EXPECT_EQ(0, d.bytes_received()); |
| 6438 | } |
| 6439 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6440 | TEST_F(URLRequestTestHTTP, CancelWhileReadingFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6441 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6442 | |
| 6443 | // populate cache |
| 6444 | { |
| 6445 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6446 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6447 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6448 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6449 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6450 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6451 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6452 | } |
| 6453 | |
| 6454 | // cancel read from cache (see bug 990242) |
| 6455 | { |
| 6456 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6457 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6458 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6459 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6460 | r->Start(); |
| 6461 | r->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6462 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6463 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6464 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6465 | EXPECT_EQ(1, d.response_started_count()); |
| 6466 | EXPECT_EQ(0, d.bytes_received()); |
| 6467 | EXPECT_FALSE(d.received_data_before_response()); |
| 6468 | } |
| 6469 | } |
| 6470 | |
| 6471 | TEST_F(URLRequestTestHTTP, PostTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6472 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6473 | HTTPUploadDataOperationTest("POST"); |
| 6474 | } |
| 6475 | |
| 6476 | TEST_F(URLRequestTestHTTP, PutTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6477 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6478 | HTTPUploadDataOperationTest("PUT"); |
| 6479 | } |
| 6480 | |
| 6481 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6482 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6483 | |
| 6484 | TestDelegate d; |
| 6485 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6486 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6487 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6488 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6489 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6490 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6491 | r->Start(); |
| 6492 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6493 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6494 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6495 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6496 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6497 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6498 | |
| 6499 | EXPECT_FALSE(d.received_data_before_response()); |
| 6500 | EXPECT_TRUE(d.data_received().empty()); |
| 6501 | } |
| 6502 | } |
| 6503 | |
| 6504 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6505 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6506 | |
| 6507 | TestDelegate d; |
| 6508 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6509 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6510 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6511 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6512 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6513 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6514 | base::FilePath dir; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 6515 | base::PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 6516 | base::SetCurrentDirectory(dir); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6517 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6518 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6519 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6520 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 6521 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 6522 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6523 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6524 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6525 | base::ThreadTaskRunnerHandle::Get().get(), path, 0, |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6526 | std::numeric_limits<uint64_t>::max(), base::Time())); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6527 | r->set_upload(std::make_unique<ElementsUploadDataStream>( |
| 6528 | std::move(element_readers), 0)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6529 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6530 | r->Start(); |
| 6531 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6532 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6533 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6534 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6535 | int64_t size64 = 0; |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 6536 | ASSERT_EQ(true, base::GetFileSize(path, &size64)); |
| 6537 | ASSERT_LE(size64, std::numeric_limits<int>::max()); |
| 6538 | int size = static_cast<int>(size64); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6539 | std::unique_ptr<char[]> buf(new char[size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6540 | |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 6541 | ASSERT_EQ(size, base::ReadFile(path, buf.get(), size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6542 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6543 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6544 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6545 | |
| 6546 | EXPECT_FALSE(d.received_data_before_response()); |
| 6547 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6548 | EXPECT_EQ(size, d.bytes_received()); |
| 6549 | EXPECT_EQ(std::string(&buf[0], size), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6550 | } |
| 6551 | } |
| 6552 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6553 | TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6554 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6555 | |
| 6556 | TestDelegate d; |
| 6557 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6558 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6559 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6560 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6561 | r->set_method("POST"); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6562 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6563 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6564 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6565 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 6566 | base::ThreadTaskRunnerHandle::Get().get(), |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6567 | base::FilePath(FILE_PATH_LITERAL( |
| 6568 | "c:\\path\\to\\non\\existant\\file.randomness.12345")), |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6569 | 0, std::numeric_limits<uint64_t>::max(), base::Time())); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6570 | r->set_upload(std::make_unique<ElementsUploadDataStream>( |
| 6571 | std::move(element_readers), 0)); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6572 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6573 | r->Start(); |
| 6574 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6575 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6576 | d.RunUntilComplete(); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6577 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6578 | EXPECT_TRUE(d.request_failed()); |
| 6579 | EXPECT_FALSE(d.received_data_before_response()); |
| 6580 | EXPECT_EQ(0, d.bytes_received()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6581 | EXPECT_EQ(ERR_FILE_NOT_FOUND, d.request_status()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6582 | } |
| 6583 | } |
| 6584 | |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6585 | namespace { |
| 6586 | |
| 6587 | // Adds a standard set of data to an upload for chunked upload integration |
| 6588 | // tests. |
| 6589 | void AddDataToUpload(ChunkedUploadDataStream::Writer* writer) { |
| 6590 | writer->AppendData("a", 1, false); |
| 6591 | writer->AppendData("bcd", 3, false); |
| 6592 | writer->AppendData("this is a longer chunk than before.", 35, false); |
| 6593 | writer->AppendData("\r\n\r\n", 4, false); |
| 6594 | writer->AppendData("0", 1, false); |
| 6595 | writer->AppendData("2323", 4, true); |
| 6596 | } |
| 6597 | |
| 6598 | // Checks that the upload data added in AddChunksToUpload() was echoed back from |
| 6599 | // the server. |
| 6600 | void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { |
| 6601 | // This should match the chunks sent by AddChunksToUpload(). |
| 6602 | const std::string expected_data = |
| 6603 | "abcdthis is a longer chunk than before.\r\n\r\n02323"; |
| 6604 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6605 | ASSERT_EQ(1, d->response_started_count()) << "request failed. Error: " |
| 6606 | << d->request_status(); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6607 | |
| 6608 | EXPECT_FALSE(d->received_data_before_response()); |
| 6609 | |
| 6610 | EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); |
| 6611 | EXPECT_EQ(expected_data, d->data_received()); |
| 6612 | } |
| 6613 | |
| 6614 | } // namespace |
| 6615 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6616 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6617 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6618 | |
| 6619 | TestDelegate d; |
| 6620 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6621 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6622 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6623 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6624 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6625 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6626 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6627 | upload_data_stream->CreateWriter(); |
| 6628 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6629 | r->set_method("POST"); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6630 | AddDataToUpload(writer.get()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6631 | r->Start(); |
| 6632 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6633 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6634 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6635 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6636 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6637 | } |
| 6638 | } |
| 6639 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6640 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6641 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6642 | |
| 6643 | TestDelegate d; |
| 6644 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6645 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6646 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6647 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6648 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6649 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6650 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6651 | upload_data_stream->CreateWriter(); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6652 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6653 | r->set_method("POST"); |
| 6654 | r->Start(); |
| 6655 | EXPECT_TRUE(r->is_pending()); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6656 | AddDataToUpload(writer.get()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6657 | d.RunUntilComplete(); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6658 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6659 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6660 | } |
| 6661 | } |
| 6662 | |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6663 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6664 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6665 | |
| 6666 | TestDelegate d; |
| 6667 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6668 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6669 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6670 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6671 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6672 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6673 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6674 | upload_data_stream->CreateWriter(); |
| 6675 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6676 | r->set_method("POST"); |
| 6677 | r->Start(); |
| 6678 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6679 | |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6680 | // Pump messages until we start sending headers.. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6681 | base::RunLoop().RunUntilIdle(); |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6682 | |
| 6683 | // And now wait for completion. |
| 6684 | base::RunLoop run_loop; |
| 6685 | d.set_on_complete(run_loop.QuitClosure()); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6686 | AddDataToUpload(writer.get()); |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6687 | run_loop.Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6688 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6689 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6690 | } |
| 6691 | } |
| 6692 | |
| 6693 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6694 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6695 | |
| 6696 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6697 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6698 | http_test_server()->GetURL("/with-headers.html"), DEFAULT_PRIORITY, &d, |
| 6699 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6700 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6701 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6702 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6703 | const HttpResponseHeaders* headers = req->response_headers(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6704 | |
| 6705 | // Simple sanity check that response_info() accesses the same data. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6706 | EXPECT_EQ(headers, req->response_info().headers.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6707 | |
| 6708 | std::string header; |
| 6709 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 6710 | EXPECT_EQ("private", header); |
| 6711 | |
| 6712 | header.clear(); |
| 6713 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 6714 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 6715 | |
| 6716 | // The response has two "X-Multiple-Entries" headers. |
| 6717 | // This verfies our output has them concatenated together. |
| 6718 | header.clear(); |
| 6719 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 6720 | EXPECT_EQ("a, b", header); |
| 6721 | } |
| 6722 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6723 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 6724 | // security state. (see http://crbug.com/550977). |
| 6725 | #if !defined(OS_IOS) |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6726 | TEST_F(URLRequestTestHTTP, ProcessSTS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6727 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6728 | https_test_server.SetSSLConfig( |
| 6729 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6730 | https_test_server.ServeFilesFromSourceDirectory( |
| 6731 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6732 | ASSERT_TRUE(https_test_server.Start()); |
| 6733 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6734 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6735 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6736 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6737 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6738 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6739 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6740 | d.RunUntilComplete(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6741 | |
| 6742 | TransportSecurityState* security_state = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6743 | default_context().transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6744 | TransportSecurityState::STSState sts_state; |
| 6745 | TransportSecurityState::PKPState pkp_state; |
| 6746 | EXPECT_TRUE( |
| 6747 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6748 | EXPECT_FALSE( |
| 6749 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6750 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6751 | sts_state.upgrade_mode); |
| 6752 | EXPECT_TRUE(sts_state.include_subdomains); |
| 6753 | EXPECT_FALSE(pkp_state.include_subdomains); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6754 | #if defined(OS_ANDROID) |
| 6755 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6756 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6757 | EXPECT_FALSE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6758 | #endif |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6759 | } |
| 6760 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6761 | TEST_F(URLRequestTestHTTP, STSNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6762 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6763 | https_test_server.ServeFilesFromSourceDirectory( |
| 6764 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6765 | ASSERT_TRUE(https_test_server.Start()); |
| 6766 | // Make sure this test fails if the test server is changed to not |
| 6767 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6768 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 6769 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6770 | |
| 6771 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6772 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6773 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6774 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6775 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6776 | d.RunUntilComplete(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6777 | TransportSecurityState* security_state = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6778 | default_context().transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6779 | TransportSecurityState::STSState sts_state; |
| 6780 | EXPECT_FALSE( |
| 6781 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6782 | } |
| 6783 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6784 | namespace { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 6785 | const char kExpectCTStaticHostname[] = "expect-ct.preloaded.test"; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6786 | const char kPKPReportUri[] = "http://report-uri.preloaded.test/pkp"; |
| 6787 | const char kPKPHost[] = "with-report-uri-pkp.preloaded.test"; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6788 | } // namespace |
| 6789 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6790 | // 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] | 6791 | TEST_F(URLRequestTestHTTP, ProcessPKPAndSendReport) { |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6792 | GURL report_uri(kPKPReportUri); |
Hiroki Nakagawa | 4e4967bcb | 2018-10-11 04:09:51 | [diff] [blame] | 6793 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6794 | https_test_server.SetSSLConfig( |
| 6795 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6796 | https_test_server.ServeFilesFromSourceDirectory( |
| 6797 | base::FilePath(kTestFilePath)); |
| 6798 | ASSERT_TRUE(https_test_server.Start()); |
| 6799 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6800 | std::string test_server_hostname = kPKPHost; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6801 | |
| 6802 | // Set up a pin for |test_server_hostname|. |
| 6803 | TransportSecurityState security_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6804 | security_state.EnableStaticPinsForTesting(); |
| 6805 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6806 | |
| 6807 | MockCertificateReportSender mock_report_sender; |
| 6808 | security_state.SetReportSender(&mock_report_sender); |
| 6809 | |
| 6810 | // Set up a MockCertVerifier to trigger a violation of the previously |
| 6811 | // set pin. |
| 6812 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6813 | ASSERT_TRUE(cert); |
| 6814 | |
| 6815 | MockCertVerifier cert_verifier; |
| 6816 | CertVerifyResult verify_result; |
| 6817 | verify_result.verified_cert = cert; |
| 6818 | verify_result.is_issued_by_known_root = true; |
| 6819 | HashValue hash3; |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6820 | ASSERT_TRUE( |
| 6821 | hash3.FromString("sha256/3333333333333333333333333333333333333333333=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6822 | verify_result.public_key_hashes.push_back(hash3); |
| 6823 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6824 | |
| 6825 | TestNetworkDelegate network_delegate; |
| 6826 | TestURLRequestContext context(true); |
| 6827 | context.set_transport_security_state(&security_state); |
| 6828 | context.set_network_delegate(&network_delegate); |
| 6829 | context.set_cert_verifier(&cert_verifier); |
| 6830 | context.Init(); |
| 6831 | |
| 6832 | // Now send a request to trigger the violation. |
| 6833 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6834 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6835 | https_test_server.GetURL(test_server_hostname, "/simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6836 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6837 | violating_request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6838 | d.RunUntilComplete(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6839 | |
| 6840 | // Check that a report was sent. |
| 6841 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 6842 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 6843 | EXPECT_EQ("application/json; charset=utf-8", |
| 6844 | mock_report_sender.latest_content_type()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6845 | std::unique_ptr<base::Value> value( |
Lei Zhang | a8b4c5fb | 2019-02-16 03:02:03 | [diff] [blame] | 6846 | base::JSONReader::ReadDeprecated(mock_report_sender.latest_report())); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6847 | ASSERT_TRUE(value); |
jdoerrie | e48b26a | 2017-12-09 14:19:08 | [diff] [blame] | 6848 | ASSERT_TRUE(value->is_dict()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6849 | base::DictionaryValue* report_dict; |
| 6850 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 6851 | std::string report_hostname; |
| 6852 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 6853 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 6854 | } |
| 6855 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6856 | // Tests that reports do not get sent on requests to static pkp hosts that |
| 6857 | // don't have pin violations. |
| 6858 | TEST_F(URLRequestTestHTTP, ProcessPKPWithNoViolation) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6859 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6860 | https_test_server.SetSSLConfig( |
| 6861 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6862 | https_test_server.ServeFilesFromSourceDirectory( |
| 6863 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6864 | ASSERT_TRUE(https_test_server.Start()); |
| 6865 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6866 | std::string test_server_hostname = kPKPHost; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6867 | |
| 6868 | TransportSecurityState security_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6869 | security_state.EnableStaticPinsForTesting(); |
| 6870 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6871 | MockCertificateReportSender mock_report_sender; |
| 6872 | security_state.SetReportSender(&mock_report_sender); |
| 6873 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6874 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6875 | ASSERT_TRUE(cert); |
Hiroki Nakagawa | 4e4967bcb | 2018-10-11 04:09:51 | [diff] [blame] | 6876 | MockCertVerifier mock_cert_verifier; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6877 | CertVerifyResult verify_result; |
| 6878 | verify_result.verified_cert = cert; |
| 6879 | verify_result.is_issued_by_known_root = true; |
| 6880 | HashValue hash; |
| 6881 | // The expected value of GoodPin1 used by |test_default::kHSTSSource|. |
| 6882 | ASSERT_TRUE( |
| 6883 | hash.FromString("sha256/Nn8jk5By4Vkq6BeOVZ7R7AC6XUUBZsWmUbJR1f1Y5FY=")); |
| 6884 | verify_result.public_key_hashes.push_back(hash); |
| 6885 | mock_cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6886 | |
| 6887 | TestNetworkDelegate network_delegate; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6888 | TestURLRequestContext context(true); |
| 6889 | context.set_transport_security_state(&security_state); |
| 6890 | context.set_network_delegate(&network_delegate); |
| 6891 | context.set_cert_verifier(&mock_cert_verifier); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6892 | context.Init(); |
| 6893 | |
| 6894 | // Now send a request that does not trigger the violation. |
| 6895 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6896 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6897 | https_test_server.GetURL(test_server_hostname, "/simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6898 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6899 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6900 | d.RunUntilComplete(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6901 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6902 | // Check that the request succeeded, a report was not sent and the pkp was |
| 6903 | // not bypassed. |
| 6904 | EXPECT_EQ(OK, d.request_status()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6905 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 6906 | EXPECT_EQ(std::string(), mock_report_sender.latest_report()); |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6907 | TransportSecurityState::STSState sts_state; |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6908 | TransportSecurityState::PKPState pkp_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6909 | EXPECT_TRUE(security_state.GetStaticDomainState(test_server_hostname, |
| 6910 | &sts_state, &pkp_state)); |
| 6911 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
| 6912 | EXPECT_FALSE(request->ssl_info().pkp_bypassed); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6913 | } |
| 6914 | |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6915 | TEST_F(URLRequestTestHTTP, PKPBypassRecorded) { |
| 6916 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6917 | https_test_server.SetSSLConfig( |
| 6918 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6919 | https_test_server.ServeFilesFromSourceDirectory( |
| 6920 | base::FilePath(kTestFilePath)); |
| 6921 | ASSERT_TRUE(https_test_server.Start()); |
| 6922 | |
| 6923 | // Set up a MockCertVerifier to be a local root that violates the pin |
| 6924 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6925 | ASSERT_TRUE(cert); |
| 6926 | |
| 6927 | MockCertVerifier cert_verifier; |
| 6928 | CertVerifyResult verify_result; |
| 6929 | verify_result.verified_cert = cert; |
| 6930 | verify_result.is_issued_by_known_root = false; |
| 6931 | HashValue hash; |
| 6932 | ASSERT_TRUE( |
| 6933 | hash.FromString("sha256/1111111111111111111111111111111111111111111=")); |
| 6934 | verify_result.public_key_hashes.push_back(hash); |
| 6935 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6936 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6937 | std::string test_server_hostname = kPKPHost; |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6938 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6939 | // Set up PKP |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6940 | TransportSecurityState security_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6941 | security_state.EnableStaticPinsForTesting(); |
| 6942 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 6943 | MockCertificateReportSender mock_report_sender; |
| 6944 | security_state.SetReportSender(&mock_report_sender); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6945 | |
| 6946 | TestNetworkDelegate network_delegate; |
| 6947 | TestURLRequestContext context(true); |
| 6948 | context.set_transport_security_state(&security_state); |
| 6949 | context.set_network_delegate(&network_delegate); |
| 6950 | context.set_cert_verifier(&cert_verifier); |
| 6951 | context.Init(); |
| 6952 | |
| 6953 | TestDelegate d; |
| 6954 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6955 | https_test_server.GetURL(test_server_hostname, "/simple.html"), |
| 6956 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6957 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6958 | d.RunUntilComplete(); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6959 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6960 | // Check that the request succeeded, a report was not sent and the PKP was |
| 6961 | // bypassed. |
| 6962 | EXPECT_EQ(OK, d.request_status()); |
| 6963 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 6964 | EXPECT_EQ(std::string(), mock_report_sender.latest_report()); |
| 6965 | TransportSecurityState::STSState sts_state; |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6966 | TransportSecurityState::PKPState pkp_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6967 | EXPECT_TRUE(security_state.GetStaticDomainState(test_server_hostname, |
| 6968 | &sts_state, &pkp_state)); |
| 6969 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6970 | EXPECT_TRUE(request->ssl_info().pkp_bypassed); |
| 6971 | } |
| 6972 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6973 | TEST_F(URLRequestTestHTTP, ProcessSTSOnce) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6974 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6975 | https_test_server.SetSSLConfig( |
| 6976 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6977 | https_test_server.ServeFilesFromSourceDirectory( |
| 6978 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6979 | ASSERT_TRUE(https_test_server.Start()); |
| 6980 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6981 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6982 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6983 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6984 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6985 | https_test_server.GetURL("/hsts-multiple-headers.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6986 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6987 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6988 | d.RunUntilComplete(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6989 | |
| 6990 | // We should have set parameters from the first header, not the second. |
| 6991 | TransportSecurityState* security_state = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6992 | default_context().transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6993 | TransportSecurityState::STSState sts_state; |
| 6994 | EXPECT_TRUE( |
| 6995 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6996 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6997 | sts_state.upgrade_mode); |
| 6998 | EXPECT_FALSE(sts_state.include_subdomains); |
| 6999 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7000 | } |
| 7001 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7002 | // An ExpectCTReporter that records the number of times OnExpectCTFailed() was |
| 7003 | // called. |
| 7004 | class MockExpectCTReporter : public TransportSecurityState::ExpectCTReporter { |
| 7005 | public: |
| 7006 | MockExpectCTReporter() : num_failures_(0) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 7007 | ~MockExpectCTReporter() override = default; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7008 | |
| 7009 | void OnExpectCTFailed(const HostPortPair& host_port_pair, |
| 7010 | const GURL& report_uri, |
estark | ae028b46 | 2017-06-20 23:25:01 | [diff] [blame] | 7011 | base::Time expiration, |
estark | bf1b5296 | 2017-05-05 17:05:25 | [diff] [blame] | 7012 | const X509Certificate* validated_certificate_chain, |
| 7013 | const X509Certificate* served_certificate_chain, |
| 7014 | const SignedCertificateTimestampAndStatusList& |
| 7015 | signed_certificate_timestamps) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7016 | num_failures_++; |
| 7017 | } |
| 7018 | |
| 7019 | uint32_t num_failures() { return num_failures_; } |
| 7020 | |
| 7021 | private: |
| 7022 | uint32_t num_failures_; |
| 7023 | }; |
| 7024 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7025 | // A CTPolicyEnforcer that returns a default CTPolicyCompliance value |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7026 | // for every certificate. |
| 7027 | class MockCTPolicyEnforcer : public CTPolicyEnforcer { |
| 7028 | public: |
| 7029 | MockCTPolicyEnforcer() |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7030 | : default_result_(ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 7031 | ~MockCTPolicyEnforcer() override = default; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7032 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7033 | ct::CTPolicyCompliance CheckCompliance( |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7034 | X509Certificate* cert, |
Ryan Sleevi | 8a9c9c1 | 2018-05-09 02:36:23 | [diff] [blame] | 7035 | const ct::SCTList& verified_scts, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 7036 | const NetLogWithSource& net_log) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7037 | return default_result_; |
| 7038 | } |
| 7039 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7040 | void set_default_result(ct::CTPolicyCompliance default_result) { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7041 | default_result_ = default_result; |
| 7042 | } |
| 7043 | |
| 7044 | private: |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7045 | ct::CTPolicyCompliance default_result_; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7046 | }; |
| 7047 | |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7048 | // Tests that Expect CT headers for the preload list are processed correctly. |
| 7049 | TEST_F(URLRequestTestHTTP, PreloadExpectCTHeader) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 7050 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 7051 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7052 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7053 | https_test_server.SetSSLConfig( |
| 7054 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7055 | https_test_server.ServeFilesFromSourceDirectory( |
| 7056 | base::FilePath(kTestFilePath)); |
| 7057 | ASSERT_TRUE(https_test_server.Start()); |
| 7058 | |
| 7059 | MockExpectCTReporter reporter; |
| 7060 | TransportSecurityState transport_security_state; |
| 7061 | transport_security_state.enable_static_expect_ct_ = true; |
| 7062 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7063 | |
| 7064 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7065 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7066 | ASSERT_TRUE(cert); |
| 7067 | MockCertVerifier cert_verifier; |
| 7068 | CertVerifyResult verify_result; |
| 7069 | verify_result.verified_cert = cert; |
| 7070 | verify_result.is_issued_by_known_root = true; |
| 7071 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7072 | |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 7073 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to trigger an Expect |
| 7074 | // CT violation. |
| 7075 | DoNothingCTVerifier ct_verifier; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7076 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7077 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7078 | ct::CTPolicyCompliance::CT_POLICY_NOT_ENOUGH_SCTS); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7079 | |
| 7080 | TestNetworkDelegate network_delegate; |
| 7081 | // Use a MockHostResolver (which by default maps all hosts to |
| 7082 | // 127.0.0.1) so that the request can be sent to a site on the Expect |
| 7083 | // CT preload list. |
| 7084 | MockHostResolver host_resolver; |
| 7085 | TestURLRequestContext context(true); |
| 7086 | context.set_host_resolver(&host_resolver); |
| 7087 | context.set_transport_security_state(&transport_security_state); |
| 7088 | context.set_network_delegate(&network_delegate); |
| 7089 | context.set_cert_verifier(&cert_verifier); |
| 7090 | context.set_cert_transparency_verifier(&ct_verifier); |
vabr | 13d0074 | 2017-06-05 10:31:46 | [diff] [blame] | 7091 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7092 | context.Init(); |
| 7093 | |
| 7094 | // Now send a request to trigger the violation. |
| 7095 | TestDelegate d; |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7096 | GURL url = https_test_server.GetURL("/expect-ct-header-preload.html"); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7097 | GURL::Replacements replace_host; |
| 7098 | replace_host.SetHostStr(kExpectCTStaticHostname); |
| 7099 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7100 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
| 7101 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7102 | violating_request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7103 | d.RunUntilComplete(); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7104 | |
| 7105 | EXPECT_EQ(1u, reporter.num_failures()); |
| 7106 | } |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7107 | |
| 7108 | // Tests that Expect CT HTTP headers are processed correctly. |
| 7109 | TEST_F(URLRequestTestHTTP, ExpectCTHeader) { |
| 7110 | base::test::ScopedFeatureList feature_list; |
| 7111 | feature_list.InitAndEnableFeature( |
| 7112 | TransportSecurityState::kDynamicExpectCTFeature); |
| 7113 | |
| 7114 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7115 | https_test_server.SetSSLConfig( |
| 7116 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7117 | https_test_server.ServeFilesFromSourceDirectory( |
| 7118 | base::FilePath(kTestFilePath)); |
| 7119 | ASSERT_TRUE(https_test_server.Start()); |
| 7120 | |
| 7121 | MockExpectCTReporter reporter; |
| 7122 | TransportSecurityState transport_security_state; |
| 7123 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7124 | |
| 7125 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7126 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7127 | ASSERT_TRUE(cert); |
| 7128 | MockCertVerifier cert_verifier; |
| 7129 | CertVerifyResult verify_result; |
| 7130 | verify_result.verified_cert = cert; |
| 7131 | verify_result.is_issued_by_known_root = true; |
| 7132 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7133 | |
| 7134 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 7135 | // compliance. |
| 7136 | DoNothingCTVerifier ct_verifier; |
| 7137 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7138 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7139 | ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7140 | |
| 7141 | TestNetworkDelegate network_delegate; |
| 7142 | // Use a MockHostResolver (which by default maps all hosts to |
| 7143 | // 127.0.0.1). |
| 7144 | MockHostResolver host_resolver; |
| 7145 | TestURLRequestContext context(true); |
| 7146 | context.set_host_resolver(&host_resolver); |
| 7147 | context.set_transport_security_state(&transport_security_state); |
| 7148 | context.set_network_delegate(&network_delegate); |
| 7149 | context.set_cert_verifier(&cert_verifier); |
| 7150 | context.set_cert_transparency_verifier(&ct_verifier); |
| 7151 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 7152 | context.Init(); |
| 7153 | |
| 7154 | // Now send a request to trigger the header processing. |
| 7155 | TestDelegate d; |
| 7156 | GURL url = https_test_server.GetURL("/expect-ct-header.html"); |
| 7157 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7158 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7159 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7160 | d.RunUntilComplete(); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7161 | |
| 7162 | TransportSecurityState::ExpectCTState state; |
| 7163 | ASSERT_TRUE( |
| 7164 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 7165 | EXPECT_TRUE(state.enforce); |
| 7166 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 7167 | } |
| 7168 | |
| 7169 | // Tests that if multiple Expect CT HTTP headers are sent, they are all |
| 7170 | // processed. |
| 7171 | TEST_F(URLRequestTestHTTP, MultipleExpectCTHeaders) { |
| 7172 | base::test::ScopedFeatureList feature_list; |
| 7173 | feature_list.InitAndEnableFeature( |
| 7174 | TransportSecurityState::kDynamicExpectCTFeature); |
| 7175 | |
| 7176 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7177 | https_test_server.SetSSLConfig( |
| 7178 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7179 | https_test_server.ServeFilesFromSourceDirectory( |
| 7180 | base::FilePath(kTestFilePath)); |
| 7181 | ASSERT_TRUE(https_test_server.Start()); |
| 7182 | |
| 7183 | MockExpectCTReporter reporter; |
| 7184 | TransportSecurityState transport_security_state; |
| 7185 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7186 | |
| 7187 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7188 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7189 | ASSERT_TRUE(cert); |
| 7190 | MockCertVerifier cert_verifier; |
| 7191 | CertVerifyResult verify_result; |
| 7192 | verify_result.verified_cert = cert; |
| 7193 | verify_result.is_issued_by_known_root = true; |
| 7194 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7195 | |
| 7196 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 7197 | // compliance. |
| 7198 | DoNothingCTVerifier ct_verifier; |
| 7199 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7200 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7201 | ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7202 | |
| 7203 | TestNetworkDelegate network_delegate; |
| 7204 | // Use a MockHostResolver (which by default maps all hosts to |
| 7205 | // 127.0.0.1). |
| 7206 | MockHostResolver host_resolver; |
| 7207 | TestURLRequestContext context(true); |
| 7208 | context.set_host_resolver(&host_resolver); |
| 7209 | context.set_transport_security_state(&transport_security_state); |
| 7210 | context.set_network_delegate(&network_delegate); |
| 7211 | context.set_cert_verifier(&cert_verifier); |
| 7212 | context.set_cert_transparency_verifier(&ct_verifier); |
| 7213 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 7214 | context.Init(); |
| 7215 | |
| 7216 | // Now send a request to trigger the header processing. |
| 7217 | TestDelegate d; |
| 7218 | GURL url = https_test_server.GetURL("/expect-ct-header-multiple.html"); |
| 7219 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7220 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7221 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7222 | d.RunUntilComplete(); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7223 | |
| 7224 | TransportSecurityState::ExpectCTState state; |
| 7225 | ASSERT_TRUE( |
| 7226 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 7227 | EXPECT_TRUE(state.enforce); |
| 7228 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 7229 | } |
| 7230 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 7231 | #endif // !defined(OS_IOS) |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7232 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7233 | #if BUILDFLAG(ENABLE_REPORTING) |
| 7234 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7235 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_DontReportIfNetworkNotAccessed) { |
| 7236 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7237 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7238 | ASSERT_TRUE(https_test_server.Start()); |
| 7239 | GURL request_url = https_test_server.GetURL("/cachetime"); |
| 7240 | |
| 7241 | TestNetworkErrorLoggingService nel_service; |
| 7242 | TestURLRequestContext context(true); |
| 7243 | context.set_network_error_logging_service(&nel_service); |
| 7244 | context.Init(); |
| 7245 | |
| 7246 | // Populate the cache. |
| 7247 | TestDelegate d; |
| 7248 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7249 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7250 | request->Start(); |
| 7251 | d.RunUntilComplete(); |
| 7252 | |
| 7253 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7254 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7255 | nel_service.errors()[0]; |
| 7256 | EXPECT_EQ(request_url, error.uri); |
| 7257 | EXPECT_EQ(200, error.status_code); |
| 7258 | EXPECT_EQ(OK, error.type); |
| 7259 | |
| 7260 | request = context.CreateRequest(request_url, DEFAULT_PRIORITY, &d, |
| 7261 | TRAFFIC_ANNOTATION_FOR_TESTS); |
| 7262 | request->Start(); |
| 7263 | d.RunUntilComplete(); |
| 7264 | |
| 7265 | EXPECT_FALSE(request->response_info().network_accessed); |
| 7266 | EXPECT_TRUE(request->response_info().was_cached); |
| 7267 | // No additional NEL report was generated. |
| 7268 | EXPECT_EQ(1u, nel_service.errors().size()); |
| 7269 | } |
| 7270 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7271 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_BasicSuccess) { |
| 7272 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7273 | https_test_server.ServeFilesFromSourceDirectory( |
| 7274 | base::FilePath(kTestFilePath)); |
| 7275 | ASSERT_TRUE(https_test_server.Start()); |
| 7276 | GURL request_url = https_test_server.GetURL("/simple.html"); |
| 7277 | |
| 7278 | TestNetworkErrorLoggingService nel_service; |
| 7279 | TestURLRequestContext context(true); |
| 7280 | context.set_network_error_logging_service(&nel_service); |
| 7281 | context.Init(); |
| 7282 | |
| 7283 | TestDelegate d; |
| 7284 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7285 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7286 | request->Start(); |
| 7287 | d.RunUntilComplete(); |
| 7288 | |
| 7289 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7290 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7291 | nel_service.errors()[0]; |
| 7292 | EXPECT_EQ(request_url, error.uri); |
| 7293 | EXPECT_EQ(200, error.status_code); |
| 7294 | EXPECT_EQ(OK, error.type); |
| 7295 | } |
| 7296 | |
| 7297 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_BasicError) { |
| 7298 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7299 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7300 | ASSERT_TRUE(https_test_server.Start()); |
| 7301 | GURL request_url = https_test_server.GetURL("/close-socket"); |
| 7302 | |
| 7303 | TestNetworkErrorLoggingService nel_service; |
| 7304 | TestURLRequestContext context(true); |
| 7305 | context.set_network_error_logging_service(&nel_service); |
| 7306 | context.Init(); |
| 7307 | |
| 7308 | TestDelegate d; |
| 7309 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7310 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7311 | request->Start(); |
| 7312 | d.RunUntilComplete(); |
| 7313 | |
| 7314 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7315 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7316 | nel_service.errors()[0]; |
| 7317 | EXPECT_EQ(request_url, error.uri); |
| 7318 | EXPECT_EQ(0, error.status_code); |
| 7319 | EXPECT_EQ(ERR_EMPTY_RESPONSE, error.type); |
| 7320 | } |
| 7321 | |
| 7322 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_Redirect) { |
| 7323 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7324 | https_test_server.ServeFilesFromSourceDirectory( |
| 7325 | base::FilePath(kTestFilePath)); |
| 7326 | ASSERT_TRUE(https_test_server.Start()); |
| 7327 | GURL request_url = https_test_server.GetURL("/redirect-test.html"); |
| 7328 | GURL redirect_url = https_test_server.GetURL("/with-headers.html"); |
| 7329 | |
| 7330 | TestNetworkErrorLoggingService nel_service; |
| 7331 | TestURLRequestContext context(true); |
| 7332 | context.set_network_error_logging_service(&nel_service); |
| 7333 | context.Init(); |
| 7334 | |
| 7335 | TestDelegate d; |
| 7336 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7337 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7338 | request->Start(); |
| 7339 | d.RunUntilComplete(); |
| 7340 | |
| 7341 | ASSERT_EQ(2u, nel_service.errors().size()); |
| 7342 | const TestNetworkErrorLoggingService::RequestDetails& error1 = |
| 7343 | nel_service.errors()[0]; |
| 7344 | EXPECT_EQ(request_url, error1.uri); |
| 7345 | EXPECT_EQ(302, error1.status_code); |
| 7346 | EXPECT_EQ(OK, error1.type); |
| 7347 | const TestNetworkErrorLoggingService::RequestDetails& error2 = |
| 7348 | nel_service.errors()[1]; |
| 7349 | EXPECT_EQ(redirect_url, error2.uri); |
| 7350 | EXPECT_EQ(200, error2.status_code); |
| 7351 | EXPECT_EQ(OK, error2.type); |
| 7352 | } |
| 7353 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7354 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_RedirectWithoutLocationHeader) { |
| 7355 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7356 | https_test_server.ServeFilesFromSourceDirectory( |
| 7357 | base::FilePath(kTestFilePath)); |
| 7358 | ASSERT_TRUE(https_test_server.Start()); |
| 7359 | GURL request_url = https_test_server.GetURL("/308-without-location-header"); |
| 7360 | |
| 7361 | TestNetworkErrorLoggingService nel_service; |
| 7362 | TestURLRequestContext context(true); |
| 7363 | context.set_network_error_logging_service(&nel_service); |
| 7364 | context.Init(); |
| 7365 | |
| 7366 | TestDelegate d; |
| 7367 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7368 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7369 | request->Start(); |
| 7370 | d.RunUntilComplete(); |
| 7371 | |
| 7372 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7373 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7374 | nel_service.errors()[0]; |
| 7375 | EXPECT_EQ(request_url, error.uri); |
| 7376 | EXPECT_EQ(308, error.status_code); |
| 7377 | // The body of the response was successfully read. |
| 7378 | EXPECT_EQ(OK, error.type); |
| 7379 | } |
| 7380 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7381 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_Auth) { |
| 7382 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7383 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7384 | ASSERT_TRUE(https_test_server.Start()); |
| 7385 | GURL request_url = https_test_server.GetURL("/auth-basic"); |
| 7386 | |
| 7387 | TestNetworkErrorLoggingService nel_service; |
| 7388 | TestURLRequestContext context(true); |
| 7389 | context.set_network_error_logging_service(&nel_service); |
| 7390 | context.Init(); |
| 7391 | |
| 7392 | TestDelegate d; |
| 7393 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7394 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7395 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7396 | request->Start(); |
| 7397 | d.RunUntilComplete(); |
| 7398 | |
| 7399 | ASSERT_EQ(2u, nel_service.errors().size()); |
| 7400 | const TestNetworkErrorLoggingService::RequestDetails& error1 = |
| 7401 | nel_service.errors()[0]; |
| 7402 | EXPECT_EQ(request_url, error1.uri); |
| 7403 | EXPECT_EQ(401, error1.status_code); |
| 7404 | EXPECT_EQ(OK, error1.type); |
| 7405 | const TestNetworkErrorLoggingService::RequestDetails& error2 = |
| 7406 | nel_service.errors()[1]; |
| 7407 | EXPECT_EQ(request_url, error2.uri); |
| 7408 | EXPECT_EQ(200, error2.status_code); |
| 7409 | EXPECT_EQ(OK, error2.type); |
| 7410 | } |
| 7411 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7412 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_304Response) { |
| 7413 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7414 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7415 | ASSERT_TRUE(https_test_server.Start()); |
| 7416 | GURL request_url = https_test_server.GetURL("/auth-basic"); |
| 7417 | |
| 7418 | TestNetworkErrorLoggingService nel_service; |
| 7419 | TestURLRequestContext context(true); |
| 7420 | context.set_network_error_logging_service(&nel_service); |
| 7421 | context.Init(); |
| 7422 | |
| 7423 | // populate the cache |
| 7424 | { |
| 7425 | TestDelegate d; |
| 7426 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7427 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 7428 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7429 | r->Start(); |
| 7430 | d.RunUntilComplete(); |
| 7431 | } |
| 7432 | ASSERT_EQ(2u, nel_service.errors().size()); |
| 7433 | const TestNetworkErrorLoggingService::RequestDetails& error1 = |
| 7434 | nel_service.errors()[0]; |
| 7435 | EXPECT_EQ(request_url, error1.uri); |
| 7436 | EXPECT_EQ(401, error1.status_code); |
| 7437 | EXPECT_EQ(OK, error1.type); |
| 7438 | const TestNetworkErrorLoggingService::RequestDetails& error2 = |
| 7439 | nel_service.errors()[1]; |
| 7440 | EXPECT_EQ(request_url, error2.uri); |
| 7441 | EXPECT_EQ(200, error2.status_code); |
| 7442 | EXPECT_EQ(OK, error2.type); |
| 7443 | |
| 7444 | // repeat request with end-to-end validation. since auth-basic results in a |
| 7445 | // cachable page, we expect this test to result in a 304. in which case, the |
| 7446 | // response should be fetched from the cache. |
| 7447 | { |
| 7448 | TestDelegate d; |
| 7449 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7450 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 7451 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7452 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 7453 | r->Start(); |
| 7454 | d.RunUntilComplete(); |
| 7455 | |
| 7456 | // Should be the same cached document. |
| 7457 | EXPECT_TRUE(r->was_cached()); |
| 7458 | } |
| 7459 | ASSERT_EQ(3u, nel_service.errors().size()); |
| 7460 | const TestNetworkErrorLoggingService::RequestDetails& error3 = |
| 7461 | nel_service.errors()[2]; |
| 7462 | EXPECT_EQ(request_url, error3.uri); |
| 7463 | EXPECT_EQ(304, error3.status_code); |
| 7464 | EXPECT_EQ(OK, error3.type); |
| 7465 | } |
| 7466 | |
| 7467 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_CancelInResponseStarted) { |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7468 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7469 | https_test_server.ServeFilesFromSourceDirectory( |
| 7470 | base::FilePath(kTestFilePath)); |
| 7471 | ASSERT_TRUE(https_test_server.Start()); |
| 7472 | GURL request_url = https_test_server.GetURL("/simple.html"); |
| 7473 | |
| 7474 | TestNetworkErrorLoggingService nel_service; |
| 7475 | TestURLRequestContext context(true); |
| 7476 | context.set_network_error_logging_service(&nel_service); |
| 7477 | context.Init(); |
| 7478 | |
| 7479 | TestDelegate d; |
| 7480 | d.set_cancel_in_response_started(true); |
| 7481 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7482 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7483 | request->Start(); |
| 7484 | d.RunUntilComplete(); |
| 7485 | |
| 7486 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7487 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7488 | nel_service.errors()[0]; |
| 7489 | EXPECT_EQ(request_url, error.uri); |
| 7490 | EXPECT_EQ(200, error.status_code); |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7491 | // Headers were received and the body should have been read but was not. |
| 7492 | EXPECT_EQ(ERR_ABORTED, error.type); |
| 7493 | } |
| 7494 | |
| 7495 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_CancelOnDataReceived) { |
| 7496 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7497 | https_test_server.ServeFilesFromSourceDirectory( |
| 7498 | base::FilePath(kTestFilePath)); |
| 7499 | ASSERT_TRUE(https_test_server.Start()); |
| 7500 | GURL request_url = https_test_server.GetURL("/simple.html"); |
| 7501 | |
| 7502 | TestNetworkErrorLoggingService nel_service; |
| 7503 | TestURLRequestContext context(true); |
| 7504 | context.set_network_error_logging_service(&nel_service); |
| 7505 | context.Init(); |
| 7506 | |
| 7507 | TestDelegate d; |
| 7508 | d.set_cancel_in_received_data(true); |
| 7509 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7510 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7511 | request->Start(); |
| 7512 | d.RunUntilComplete(); |
| 7513 | |
| 7514 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7515 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7516 | nel_service.errors()[0]; |
| 7517 | EXPECT_EQ(request_url, error.uri); |
| 7518 | EXPECT_EQ(200, error.status_code); |
| 7519 | // Data was received but the body was not completely read. |
| 7520 | EXPECT_EQ(ERR_ABORTED, error.type); |
| 7521 | } |
| 7522 | |
| 7523 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_CancelRedirect) { |
| 7524 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7525 | https_test_server.ServeFilesFromSourceDirectory( |
| 7526 | base::FilePath(kTestFilePath)); |
| 7527 | ASSERT_TRUE(https_test_server.Start()); |
| 7528 | GURL request_url = https_test_server.GetURL("/redirect-test.html"); |
| 7529 | |
| 7530 | TestNetworkErrorLoggingService nel_service; |
| 7531 | TestURLRequestContext context(true); |
| 7532 | context.set_network_error_logging_service(&nel_service); |
| 7533 | context.Init(); |
| 7534 | |
| 7535 | TestDelegate d; |
| 7536 | d.set_cancel_in_received_redirect(true); |
| 7537 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7538 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7539 | request->Start(); |
| 7540 | d.RunUntilComplete(); |
| 7541 | |
| 7542 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7543 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7544 | nel_service.errors()[0]; |
| 7545 | EXPECT_EQ(request_url, error.uri); |
| 7546 | EXPECT_EQ(302, error.status_code); |
| 7547 | // A valid HTTP response was received, even though the request was cancelled. |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7548 | EXPECT_EQ(OK, error.type); |
| 7549 | } |
| 7550 | |
| 7551 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 7552 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7553 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7554 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7555 | |
| 7556 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7557 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7558 | http_test_server()->GetURL("/content-type-normalization.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7559 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7560 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7561 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7562 | |
| 7563 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7564 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7565 | EXPECT_EQ("text/html", mime_type); |
| 7566 | |
| 7567 | std::string charset; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7568 | req->GetCharset(&charset); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7569 | EXPECT_EQ("utf-8", charset); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7570 | req->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7571 | } |
| 7572 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7573 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) { |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7574 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7575 | GURL data_url("data:,foo"); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7576 | DataProtocolHandler data_protocol_handler; |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7577 | EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7578 | |
| 7579 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7580 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7581 | } |
| 7582 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7583 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7584 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) { |
| 7585 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
| 7586 | GURL file_url("file:///foo.txt"); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 7587 | FileProtocolHandler file_protocol_handler( |
| 7588 | base::ThreadTaskRunnerHandle::Get()); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7589 | EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url)); |
| 7590 | |
| 7591 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7592 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(file_url)); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7593 | } |
| 7594 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7595 | TEST_F(URLRequestTestHTTP, RestrictFileRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7596 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7597 | |
| 7598 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7599 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7600 | http_test_server()->GetURL("/redirect-to-file.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7601 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7602 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7603 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7604 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7605 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7606 | |
| 7607 | // The redirect should have been rejected before reporting it to the caller. |
| 7608 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7609 | } |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7610 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7611 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7612 | TEST_F(URLRequestTestHTTP, RestrictDataRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7613 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7614 | |
| 7615 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7616 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7617 | http_test_server()->GetURL("/redirect-to-data.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7618 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7619 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7620 | d.RunUntilComplete(); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7621 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7622 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7623 | |
| 7624 | // The redirect should have been rejected before reporting it to the |
| 7625 | // caller. See https://crbug.com/723796 |
| 7626 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7627 | } |
| 7628 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7629 | // Test that redirects to invalid URLs are rejected. See |
| 7630 | // https://crbug.com/462272. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7631 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7632 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7633 | |
| 7634 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7635 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7636 | http_test_server()->GetURL("/redirect-to-invalid-url.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7637 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7638 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7639 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7640 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7641 | EXPECT_EQ(1, d.response_started_count()); |
| 7642 | EXPECT_EQ(ERR_INVALID_REDIRECT, d.request_status()); |
| 7643 | |
| 7644 | // The redirect should have been rejected before reporting it to the caller. |
| 7645 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7646 | } |
| 7647 | |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7648 | // Make sure redirects are cached, despite not reading their bodies. |
| 7649 | TEST_F(URLRequestTestHTTP, CacheRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7650 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7651 | GURL redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7652 | http_test_server()->GetURL("/redirect302-to-echo-cacheable"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7653 | |
| 7654 | { |
| 7655 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7656 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7657 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7658 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7659 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7660 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7661 | EXPECT_EQ(1, d.received_redirect_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7662 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7663 | } |
| 7664 | |
| 7665 | { |
| 7666 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7667 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7668 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7669 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 7670 | d.RunUntilRedirect(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7671 | |
| 7672 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7673 | EXPECT_EQ(0, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7674 | EXPECT_TRUE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7675 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 7676 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 7677 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7678 | d.RunUntilComplete(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7679 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7680 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7681 | EXPECT_EQ(OK, d.request_status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7682 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7683 | } |
| 7684 | } |
| 7685 | |
| 7686 | // Make sure a request isn't cached when a NetworkDelegate forces a redirect |
| 7687 | // when the headers are read, since the body won't have been read. |
| 7688 | TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7689 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7690 | // URL that is normally cached. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7691 | GURL initial_url = http_test_server()->GetURL("/cachetime"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7692 | |
| 7693 | { |
| 7694 | // Set up the TestNetworkDelegate tp force a redirect. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7695 | GURL redirect_to_url = http_test_server()->GetURL("/echo"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7696 | default_network_delegate_.set_redirect_on_headers_received_url( |
| 7697 | redirect_to_url); |
| 7698 | |
| 7699 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7700 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7701 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7702 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7703 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7704 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7705 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7706 | EXPECT_EQ(redirect_to_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7707 | } |
| 7708 | |
| 7709 | { |
| 7710 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7711 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7712 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7713 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7714 | d.RunUntilComplete(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7715 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7716 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7717 | EXPECT_FALSE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7718 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7719 | EXPECT_EQ(initial_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7720 | } |
| 7721 | } |
| 7722 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7723 | // Tests that redirection to an unsafe URL is allowed when it has been marked as |
| 7724 | // safe. |
| 7725 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7726 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7727 | |
| 7728 | GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url"); |
| 7729 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7730 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7731 | |
| 7732 | TestDelegate d; |
| 7733 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7734 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7735 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 7736 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7737 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7738 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7739 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7740 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7741 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7742 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7743 | EXPECT_EQ(unsafe_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7744 | EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received()); |
| 7745 | } |
| 7746 | } |
| 7747 | |
| 7748 | // Tests that a redirect to a different unsafe URL is blocked, even after adding |
| 7749 | // some other URL to the whitelist. |
| 7750 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7751 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7752 | |
| 7753 | GURL unsafe_url("data:text/html,something"); |
| 7754 | GURL different_unsafe_url("data:text/html,something-else"); |
| 7755 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7756 | default_network_delegate_.set_allowed_unsafe_redirect_url( |
| 7757 | different_unsafe_url); |
| 7758 | |
| 7759 | TestDelegate d; |
| 7760 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7761 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7762 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 7763 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7764 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7765 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7766 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7767 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7768 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7769 | |
| 7770 | // The redirect should have been rejected before reporting it to the caller. |
| 7771 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7772 | } |
| 7773 | } |
| 7774 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7775 | // Redirects from an URL with fragment to an unsafe URL with fragment should |
| 7776 | // be allowed, and the reference fragment of the target URL should be preserved. |
| 7777 | TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7778 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7779 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7780 | GURL original_url(http_test_server()->GetURL("/original#fragment1")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7781 | GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 7782 | GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 7783 | |
| 7784 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7785 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7786 | |
| 7787 | TestDelegate d; |
| 7788 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7789 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7790 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7791 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7792 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7793 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7794 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7795 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7796 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7797 | EXPECT_EQ(original_url, r->original_url()); |
| 7798 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7799 | } |
| 7800 | } |
| 7801 | |
| 7802 | // When a delegate has specified a safe redirect URL, but it does not match the |
| 7803 | // redirect target, then do not prevent the reference fragment from being added. |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7804 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7805 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7806 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7807 | GURL original_url(http_test_server()->GetURL("/original#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7808 | GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7809 | GURL redirect_url(http_test_server()->GetURL("/target")); |
| 7810 | GURL expected_redirect_url( |
| 7811 | http_test_server()->GetURL("/target#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7812 | |
| 7813 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 7814 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7815 | |
| 7816 | TestDelegate d; |
| 7817 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7818 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7819 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7820 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7821 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7822 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7823 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7824 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7825 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7826 | EXPECT_EQ(original_url, r->original_url()); |
| 7827 | EXPECT_EQ(expected_redirect_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7828 | } |
| 7829 | } |
| 7830 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7831 | // When a delegate has specified a safe redirect URL, assume that the redirect |
| 7832 | // URL should not be changed. In particular, the reference fragment should not |
| 7833 | // be modified. |
| 7834 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7835 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7836 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7837 | GURL original_url( |
| 7838 | http_test_server()->GetURL("/original#should-not-be-appended")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7839 | GURL redirect_url("data:text/html,expect-no-reference-fragment"); |
| 7840 | |
| 7841 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 7842 | default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url); |
| 7843 | |
| 7844 | TestDelegate d; |
| 7845 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7846 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7847 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7848 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7849 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7850 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7851 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7852 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7853 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7854 | EXPECT_EQ(original_url, r->original_url()); |
| 7855 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7856 | } |
| 7857 | } |
| 7858 | |
| 7859 | // When a URLRequestRedirectJob is created, the redirection must be followed and |
| 7860 | // the reference fragment of the target URL must not be modified. |
| 7861 | TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7862 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7863 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7864 | GURL original_url( |
| 7865 | http_test_server()->GetURL("/original#should-not-be-appended")); |
| 7866 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7867 | |
| 7868 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7869 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7870 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7871 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7872 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7873 | r.get(), &default_network_delegate_, redirect_url, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7874 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 7875 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7876 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7877 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7878 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7879 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7880 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7881 | EXPECT_EQ(original_url, r->original_url()); |
| 7882 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7883 | } |
| 7884 | |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7885 | TEST_F(URLRequestTestHTTP, UnsupportedReferrerScheme) { |
| 7886 | ASSERT_TRUE(http_test_server()->Start()); |
| 7887 | |
| 7888 | const std::string referrer("foobar://totally.legit.referrer"); |
| 7889 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7890 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7891 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7892 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7893 | req->SetReferrer(referrer); |
| 7894 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7895 | d.RunUntilComplete(); |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7896 | |
| 7897 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 7898 | } |
| 7899 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7900 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7901 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7902 | |
| 7903 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7904 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7905 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7906 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7907 | req->SetReferrer("http://user:[email protected]/"); |
| 7908 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7909 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7910 | |
| 7911 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 7912 | } |
| 7913 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7914 | TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7915 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7916 | |
| 7917 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7918 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7919 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7920 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7921 | req->SetReferrer("http://foo.com/test#fragment"); |
| 7922 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7923 | d.RunUntilComplete(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7924 | |
| 7925 | EXPECT_EQ(std::string("http://foo.com/test"), d.data_received()); |
| 7926 | } |
| 7927 | |
| 7928 | TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7929 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7930 | |
| 7931 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7932 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7933 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7934 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7935 | req->SetReferrer("http://foo.com/test#fragment"); |
| 7936 | req->SetReferrer(""); |
| 7937 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7938 | d.RunUntilComplete(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7939 | |
| 7940 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 7941 | } |
| 7942 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7943 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7944 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7945 | |
| 7946 | TestDelegate d; |
| 7947 | { |
| 7948 | d.set_cancel_in_received_redirect(true); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7949 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7950 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 7951 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7952 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7953 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7954 | |
| 7955 | EXPECT_EQ(1, d.response_started_count()); |
| 7956 | EXPECT_EQ(0, d.bytes_received()); |
| 7957 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7958 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7959 | } |
| 7960 | } |
| 7961 | |
| 7962 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7963 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7964 | |
| 7965 | TestDelegate d; |
| 7966 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7967 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7968 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7969 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7970 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7971 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 7972 | d.RunUntilRedirect(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7973 | |
| 7974 | EXPECT_EQ(1, d.received_redirect_count()); |
Shivani Sharma | c18f976 | 2017-10-23 16:43:23 | [diff] [blame] | 7975 | EXPECT_TRUE(d.have_full_request_headers()); |
| 7976 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 7977 | d.ClearFullRequestHeaders(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7978 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 7979 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 7980 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7981 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7982 | |
| 7983 | EXPECT_EQ(1, d.response_started_count()); |
| 7984 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7985 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7986 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7987 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 7988 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 7989 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7990 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 7991 | |
| 7992 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 7993 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7994 | EXPECT_EQ(contents, d.data_received()); |
| 7995 | } |
| 7996 | } |
| 7997 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7998 | TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7999 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8000 | |
| 8001 | TestDelegate d; |
| 8002 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8003 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8004 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8005 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8006 | |
| 8007 | EXPECT_FALSE(d.have_full_request_headers()); |
| 8008 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8009 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 8010 | d.RunUntilRedirect(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8011 | |
| 8012 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8013 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8014 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 8015 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8016 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8017 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8018 | GURL target_url(http_test_server()->GetURL("/with-headers.html")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8019 | EXPECT_EQ(1, d.response_started_count()); |
| 8020 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8021 | CheckFullRequestHeaders(d.full_request_headers(), target_url); |
| 8022 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8023 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8024 | |
| 8025 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 8026 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 8027 | path = path.Append(kTestFilePath); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8028 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 8029 | |
| 8030 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 8031 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8032 | EXPECT_EQ(contents, d.data_received()); |
| 8033 | } |
| 8034 | } |
| 8035 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8036 | TEST_F(URLRequestTestHTTP, DeferredRedirect_ModifiedHeaders) { |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8037 | ASSERT_TRUE(http_test_server()->Start()); |
| 8038 | |
| 8039 | TestDelegate d; |
| 8040 | { |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8041 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8042 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8043 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8044 | |
| 8045 | // Set initial headers for the request. |
| 8046 | req->SetExtraRequestHeaderByName("Header1", "Value1", true /* overwrite */); |
| 8047 | req->SetExtraRequestHeaderByName("Header2", "Value2", true /* overwrite */); |
| 8048 | |
| 8049 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 8050 | d.RunUntilRedirect(); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8051 | |
| 8052 | // Initial request should only have initial headers. |
| 8053 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8054 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8055 | const HttpRequestHeaders& sent_headers1 = d.full_request_headers(); |
| 8056 | std::string sent_value; |
| 8057 | EXPECT_TRUE(sent_headers1.GetHeader("Header1", &sent_value)); |
| 8058 | EXPECT_EQ("Value1", sent_value); |
| 8059 | EXPECT_TRUE(sent_headers1.GetHeader("Header2", &sent_value)); |
| 8060 | EXPECT_EQ("Value2", sent_value); |
| 8061 | EXPECT_FALSE(sent_headers1.GetHeader("Header3", &sent_value)); |
| 8062 | d.ClearFullRequestHeaders(); |
| 8063 | |
| 8064 | // Overwrite Header2 and add Header3. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8065 | net::HttpRequestHeaders modified_headers; |
| 8066 | modified_headers.SetHeader("Header2", ""); |
| 8067 | modified_headers.SetHeader("Header3", "Value3"); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8068 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8069 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 8070 | modified_headers); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8071 | d.RunUntilComplete(); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8072 | |
| 8073 | EXPECT_EQ(1, d.response_started_count()); |
| 8074 | EXPECT_FALSE(d.received_data_before_response()); |
| 8075 | EXPECT_EQ(OK, d.request_status()); |
| 8076 | |
| 8077 | // Redirected request should also have modified headers. |
| 8078 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8079 | const HttpRequestHeaders& sent_headers2 = d.full_request_headers(); |
| 8080 | EXPECT_TRUE(sent_headers2.GetHeader("Header1", &sent_value)); |
| 8081 | EXPECT_EQ("Value1", sent_value); |
| 8082 | EXPECT_TRUE(sent_headers2.GetHeader("Header2", &sent_value)); |
| 8083 | EXPECT_EQ("", sent_value); |
| 8084 | EXPECT_TRUE(sent_headers2.GetHeader("Header3", &sent_value)); |
| 8085 | EXPECT_EQ("Value3", sent_value); |
| 8086 | } |
| 8087 | } |
| 8088 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8089 | TEST_F(URLRequestTestHTTP, DeferredRedirect_RemovedHeaders) { |
| 8090 | ASSERT_TRUE(http_test_server()->Start()); |
| 8091 | |
| 8092 | TestDelegate d; |
| 8093 | { |
| 8094 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
| 8095 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8096 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8097 | |
| 8098 | // Set initial headers for the request. |
| 8099 | req->SetExtraRequestHeaderByName("Header1", "Value1", true /* overwrite */); |
| 8100 | req->SetExtraRequestHeaderByName("Header2", "Value2", true /* overwrite */); |
| 8101 | |
| 8102 | req->Start(); |
| 8103 | d.RunUntilRedirect(); |
| 8104 | |
| 8105 | // Initial request should have initial headers. |
| 8106 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8107 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8108 | const HttpRequestHeaders& sent_headers1 = d.full_request_headers(); |
| 8109 | std::string sent_value; |
| 8110 | EXPECT_TRUE(sent_headers1.GetHeader("Header1", &sent_value)); |
| 8111 | EXPECT_EQ("Value1", sent_value); |
| 8112 | EXPECT_TRUE(sent_headers1.GetHeader("Header2", &sent_value)); |
| 8113 | EXPECT_EQ("Value2", sent_value); |
| 8114 | d.ClearFullRequestHeaders(); |
| 8115 | |
| 8116 | // Keep Header1 and remove Header2. |
| 8117 | std::vector<std::string> removed_headers({"Header2"}); |
| 8118 | req->FollowDeferredRedirect(removed_headers, |
| 8119 | base::nullopt /* modified_headers */); |
| 8120 | d.RunUntilComplete(); |
| 8121 | |
| 8122 | EXPECT_EQ(1, d.response_started_count()); |
| 8123 | EXPECT_FALSE(d.received_data_before_response()); |
| 8124 | EXPECT_EQ(OK, d.request_status()); |
| 8125 | |
| 8126 | // Redirected request should also have |
| 8127 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8128 | const HttpRequestHeaders& sent_headers2 = d.full_request_headers(); |
| 8129 | EXPECT_TRUE(sent_headers2.GetHeader("Header1", &sent_value)); |
| 8130 | EXPECT_EQ("Value1", sent_value); |
| 8131 | EXPECT_FALSE(sent_headers2.GetHeader("Header2", &sent_value)); |
| 8132 | } |
| 8133 | } |
| 8134 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8135 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8136 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8137 | |
| 8138 | TestDelegate d; |
| 8139 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8140 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8141 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 8142 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8143 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 8144 | d.RunUntilRedirect(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8145 | |
| 8146 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8147 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8148 | req->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8149 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8150 | |
| 8151 | EXPECT_EQ(1, d.response_started_count()); |
| 8152 | EXPECT_EQ(0, d.bytes_received()); |
| 8153 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8154 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8155 | } |
| 8156 | } |
| 8157 | |
| 8158 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8159 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8160 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8161 | // Populate the cache. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8162 | { |
| 8163 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8164 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8165 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8166 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8167 | HttpRequestHeaders headers; |
| 8168 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8169 | req->SetExtraRequestHeaders(headers); |
| 8170 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8171 | d.RunUntilComplete(); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8172 | |
| 8173 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8174 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8175 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8176 | } |
| 8177 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8178 | // Expect a cache hit. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8179 | { |
| 8180 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8181 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8182 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8183 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8184 | HttpRequestHeaders headers; |
| 8185 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8186 | req->SetExtraRequestHeaders(headers); |
| 8187 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8188 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8189 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8190 | EXPECT_TRUE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8191 | |
| 8192 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8193 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8194 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8195 | } |
| 8196 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8197 | // Expect a cache miss. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8198 | { |
| 8199 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8200 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8201 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8202 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8203 | HttpRequestHeaders headers; |
| 8204 | headers.SetHeader("foo", "2"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8205 | req->SetExtraRequestHeaders(headers); |
| 8206 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8207 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8208 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8209 | EXPECT_FALSE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8210 | |
| 8211 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8212 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8213 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8214 | } |
| 8215 | } |
| 8216 | |
| 8217 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8218 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8219 | |
| 8220 | // populate the cache |
| 8221 | { |
| 8222 | TestDelegate d; |
| 8223 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8224 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8225 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8226 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8227 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8228 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8229 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8230 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8231 | |
| 8232 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8233 | } |
| 8234 | |
| 8235 | // repeat request with end-to-end validation. since auth-basic results in a |
| 8236 | // cachable page, we expect this test to result in a 304. in which case, the |
| 8237 | // response should be fetched from the cache. |
| 8238 | { |
| 8239 | TestDelegate d; |
| 8240 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8241 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8242 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8243 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8244 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8245 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 8246 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8247 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8248 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8249 | |
| 8250 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8251 | |
| 8252 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8253 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8254 | } |
| 8255 | } |
| 8256 | |
| 8257 | // Check that Set-Cookie headers in 401 responses are respected. |
| 8258 | // http://crbug.com/6450 |
| 8259 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8260 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8261 | |
| 8262 | GURL url_requiring_auth = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8263 | http_test_server()->GetURL("/auth-basic?set-cookie-if-challenged"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8264 | |
| 8265 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 8266 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 8267 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8268 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8269 | TestURLRequestContext context(true); |
| 8270 | context.set_network_delegate(&network_delegate); |
| 8271 | context.Init(); |
| 8272 | |
| 8273 | TestDelegate d; |
| 8274 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8275 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8276 | std::unique_ptr<URLRequest> r( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8277 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &d, |
| 8278 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8279 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8280 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8281 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8282 | |
| 8283 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8284 | |
| 8285 | // Make sure we sent the cookie in the restarted transaction. |
| 8286 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 8287 | != std::string::npos); |
| 8288 | } |
| 8289 | |
| 8290 | // Same test as above, except this time the restart is initiated earlier |
| 8291 | // (without user intervention since identity is embedded in the URL). |
| 8292 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8293 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8294 | TestURLRequestContext context(true); |
| 8295 | context.set_network_delegate(&network_delegate); |
| 8296 | context.Init(); |
| 8297 | |
| 8298 | TestDelegate d; |
| 8299 | |
| 8300 | GURL::Replacements replacements; |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 8301 | replacements.SetUsernameStr("user2"); |
| 8302 | replacements.SetPasswordStr("secret"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8303 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 8304 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8305 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 8306 | url_with_identity, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8307 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8308 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8309 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8310 | |
| 8311 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 8312 | |
| 8313 | // Make sure we sent the cookie in the restarted transaction. |
| 8314 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 8315 | != std::string::npos); |
| 8316 | } |
| 8317 | } |
| 8318 | |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8319 | TEST_F(URLRequestTestHTTP, AuthChallengeWithFilteredCookie) { |
| 8320 | ASSERT_TRUE(http_test_server()->Start()); |
| 8321 | |
| 8322 | GURL url_requiring_auth = http_test_server()->GetURL( |
| 8323 | "/auth-basic?set-cookie-if-not-challenged&set-cookie-if-challenged"); |
| 8324 | |
| 8325 | FilteringTestLayeredNetworkDelegate filtering_network_delegate( |
| 8326 | std::make_unique<TestNetworkDelegate>()); // Must outlive URLRequest. |
| 8327 | filtering_network_delegate.SetCookieFilter( |
| 8328 | "got_challenged"); // Filter the cookie auth-basic sets |
| 8329 | TestURLRequestContext context(true); |
| 8330 | context.set_network_delegate(&filtering_network_delegate); |
| 8331 | context.Init(); |
| 8332 | |
| 8333 | TestDelegate delegate; |
| 8334 | |
| 8335 | delegate.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8336 | |
| 8337 | std::unique_ptr<URLRequest> request( |
| 8338 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &delegate, |
| 8339 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8340 | request->Start(); |
| 8341 | |
| 8342 | delegate.RunUntilComplete(); |
| 8343 | EXPECT_THAT(delegate.request_status(), IsOk()); |
| 8344 | |
| 8345 | // Make sure the cookie was actually filtered. |
| 8346 | EXPECT_EQ(std::string::npos, |
| 8347 | delegate.data_received().find("Cookie: got_challenged=true")); |
| 8348 | // Make sure it was blocked twice. |
| 8349 | EXPECT_EQ(2, filtering_network_delegate.blocked_set_cookie_count()); |
| 8350 | } |
| 8351 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8352 | // Tests that load timing works as expected with auth and the cache. |
| 8353 | TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8354 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8355 | |
| 8356 | // populate the cache |
| 8357 | { |
| 8358 | TestDelegate d; |
| 8359 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8360 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8361 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8362 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8363 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8364 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8365 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8366 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8367 | |
| 8368 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8369 | |
| 8370 | LoadTimingInfo load_timing_info_before_auth; |
| 8371 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth( |
| 8372 | &load_timing_info_before_auth)); |
| 8373 | TestLoadTimingNotReused(load_timing_info_before_auth, |
| 8374 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 8375 | |
| 8376 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8377 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8378 | // The test server does not support keep alive sockets, so the second |
| 8379 | // request with auth should use a new socket. |
| 8380 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 8381 | EXPECT_NE(load_timing_info_before_auth.socket_log_id, |
| 8382 | load_timing_info.socket_log_id); |
| 8383 | EXPECT_LE(load_timing_info_before_auth.receive_headers_end, |
| 8384 | load_timing_info.connect_timing.connect_start); |
| 8385 | } |
| 8386 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8387 | // Repeat request with end-to-end validation. Since auth-basic results in a |
| 8388 | // 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] | 8389 | // response should be fetched from the cache. |
| 8390 | { |
| 8391 | TestDelegate d; |
| 8392 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8393 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8394 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8395 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8396 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8397 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 8398 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8399 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8400 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8401 | |
| 8402 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8403 | |
| 8404 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8405 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8406 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8407 | // Since there was a request that went over the wire, the load timing |
| 8408 | // information should include connection times. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8409 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8410 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8411 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8412 | } |
| 8413 | } |
| 8414 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8415 | // In this test, we do a POST which the server will 302 redirect. |
| 8416 | // The subsequent transaction should use GET, and should not send the |
| 8417 | // Content-Type header. |
| 8418 | // http://code.google.com/p/chromium/issues/detail?id=843 |
| 8419 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8420 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8421 | |
| 8422 | const char kData[] = "hello world"; |
| 8423 | |
| 8424 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8425 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8426 | http_test_server()->GetURL("/redirect-to-echoall"), DEFAULT_PRIORITY, &d, |
| 8427 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8428 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8429 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8430 | |
| 8431 | // Set headers (some of which are specific to the POST). |
| 8432 | HttpRequestHeaders headers; |
Yeol | 45b9e0a | 2018-07-27 02:03:29 | [diff] [blame] | 8433 | headers.SetHeader("Content-Type", |
| 8434 | "multipart/form-data;" |
| 8435 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ"); |
| 8436 | headers.SetHeader("Accept", |
| 8437 | "text/xml,application/xml,application/xhtml+xml," |
| 8438 | "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); |
| 8439 | headers.SetHeader("Accept-Language", "en-US,en"); |
| 8440 | headers.SetHeader("Accept-Charset", "ISO-8859-1,*,utf-8"); |
| 8441 | headers.SetHeader("Content-Length", "11"); |
| 8442 | headers.SetHeader("Origin", "http://localhost:1337/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8443 | req->SetExtraRequestHeaders(headers); |
| 8444 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8445 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8446 | |
| 8447 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8448 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8449 | EXPECT_EQ("text/html", mime_type); |
| 8450 | |
| 8451 | const std::string& data = d.data_received(); |
| 8452 | |
| 8453 | // Check that the post-specific headers were stripped: |
| 8454 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 8455 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8456 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8457 | |
| 8458 | // These extra request headers should not have been stripped. |
| 8459 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 8460 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 8461 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 8462 | } |
| 8463 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8464 | // The following tests check that we handle mutating the request for HTTP |
| 8465 | // redirects as expected. |
| 8466 | // See https://crbug.com/56373, https://crbug.com/102130, and |
| 8467 | // https://crbug.com/465517. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8468 | |
| 8469 | TEST_F(URLRequestTestHTTP, Redirect301Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8470 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8471 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8472 | const GURL url = http_test_server()->GetURL("/redirect301-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8473 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8474 | http_test_server()->GetURL("/redirect301-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8475 | |
| 8476 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8477 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8478 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8479 | |
| 8480 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8481 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8482 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8483 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8484 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8485 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8486 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8487 | } |
| 8488 | |
| 8489 | TEST_F(URLRequestTestHTTP, Redirect302Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8490 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8491 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8492 | const GURL url = http_test_server()->GetURL("/redirect302-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8493 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8494 | http_test_server()->GetURL("/redirect302-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8495 | |
| 8496 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8497 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8498 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8499 | |
| 8500 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8501 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8502 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8503 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8504 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8505 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8506 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8507 | } |
| 8508 | |
| 8509 | TEST_F(URLRequestTestHTTP, Redirect303Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8510 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8511 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8512 | const GURL url = http_test_server()->GetURL("/redirect303-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8513 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8514 | http_test_server()->GetURL("/redirect303-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8515 | |
| 8516 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8517 | HTTPRedirectMethodTest(url, "PUT", "GET", true); |
| 8518 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8519 | |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8520 | HTTPRedirectOriginHeaderTest(url, "CONNECT", "GET", std::string()); |
| 8521 | HTTPRedirectOriginHeaderTest(https_redirect_url, "CONNECT", "GET", |
| 8522 | std::string()); |
| 8523 | HTTPRedirectOriginHeaderTest(url, "DELETE", "GET", std::string()); |
| 8524 | HTTPRedirectOriginHeaderTest(https_redirect_url, "DELETE", "GET", |
| 8525 | std::string()); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8526 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8527 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8528 | HTTPRedirectOriginHeaderTest(url, "HEAD", "HEAD", url.GetOrigin().spec()); |
| 8529 | HTTPRedirectOriginHeaderTest(https_redirect_url, "HEAD", "HEAD", "null"); |
| 8530 | HTTPRedirectOriginHeaderTest(url, "OPTIONS", "GET", std::string()); |
| 8531 | HTTPRedirectOriginHeaderTest(https_redirect_url, "OPTIONS", "GET", |
| 8532 | std::string()); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8533 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8534 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8535 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8536 | HTTPRedirectOriginHeaderTest(url, "PUT", "GET", std::string()); |
| 8537 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "GET", std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8538 | } |
| 8539 | |
| 8540 | TEST_F(URLRequestTestHTTP, Redirect307Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8541 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8542 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8543 | const GURL url = http_test_server()->GetURL("/redirect307-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8544 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8545 | http_test_server()->GetURL("/redirect307-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8546 | |
| 8547 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 8548 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8549 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8550 | |
| 8551 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8552 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 8553 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 8554 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8555 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8556 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8557 | } |
| 8558 | |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8559 | TEST_F(URLRequestTestHTTP, Redirect308Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8560 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8561 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8562 | const GURL url = http_test_server()->GetURL("/redirect308-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8563 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8564 | http_test_server()->GetURL("/redirect308-to-https"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8565 | |
| 8566 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 8567 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8568 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8569 | |
| 8570 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8571 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 8572 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 8573 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8574 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8575 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8576 | } |
| 8577 | |
| 8578 | // Make sure that 308 responses without bodies are not treated as redirects. |
| 8579 | // Certain legacy apis that pre-date the response code expect this behavior |
| 8580 | // (Like Google Drive). |
| 8581 | TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8582 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8583 | |
| 8584 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8585 | const GURL url = http_test_server()->GetURL("/308-without-location-header"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8586 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8587 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8588 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8589 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8590 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8591 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8592 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8593 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8594 | EXPECT_EQ(308, request->response_headers()->response_code()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8595 | EXPECT_EQ("This is not a redirect.", d.data_received()); |
| 8596 | } |
| 8597 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8598 | TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8599 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8600 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8601 | GURL original_url( |
| 8602 | http_test_server()->GetURL("/redirect302-to-echo#fragment")); |
| 8603 | GURL expected_url(http_test_server()->GetURL("/echo#fragment")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8604 | |
| 8605 | TestDelegate d; |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8606 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8607 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8608 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8609 | r->Start(); |
| 8610 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8611 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8612 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8613 | EXPECT_EQ(OK, d.request_status()); |
| 8614 | EXPECT_EQ(original_url, r->original_url()); |
| 8615 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8616 | } |
| 8617 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8618 | TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8619 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8620 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8621 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8622 | GURL first_party_url("http://example.com"); |
| 8623 | |
| 8624 | TestDelegate d; |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8625 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8626 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8627 | r->set_site_for_cookies(first_party_url); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8628 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8629 | r->Start(); |
| 8630 | d.RunUntilComplete(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8631 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8632 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8633 | EXPECT_EQ(OK, d.request_status()); |
| 8634 | EXPECT_EQ(first_party_url, r->site_for_cookies()); |
| 8635 | } |
| 8636 | |
| 8637 | TEST_F(URLRequestTestHTTP, RedirectPreserveTopFrameOrigin) { |
| 8638 | ASSERT_TRUE(http_test_server()->Start()); |
| 8639 | |
| 8640 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 8641 | url::Origin top_frame_origin = |
| 8642 | url::Origin::Create(GURL("http://example.com")); |
| 8643 | TestDelegate d; |
| 8644 | |
| 8645 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8646 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8647 | r->set_top_frame_origin(top_frame_origin); |
| 8648 | |
| 8649 | r->Start(); |
| 8650 | d.RunUntilComplete(); |
| 8651 | |
| 8652 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8653 | EXPECT_EQ(OK, d.request_status()); |
| 8654 | EXPECT_EQ(top_frame_origin, *r->top_frame_origin()); |
| 8655 | } |
| 8656 | |
| 8657 | TEST_F(URLRequestTestHTTP, RedirectPreserveUnsetTopFrameOrigin) { |
| 8658 | ASSERT_TRUE(http_test_server()->Start()); |
| 8659 | |
| 8660 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 8661 | TestDelegate d; |
| 8662 | |
| 8663 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8664 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8665 | // Since we're not setting a top frame origin, we wouldn't expect one after |
| 8666 | // the redirect. |
| 8667 | r->Start(); |
| 8668 | d.RunUntilComplete(); |
| 8669 | |
| 8670 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8671 | EXPECT_EQ(OK, d.request_status()); |
| 8672 | EXPECT_FALSE(r->top_frame_origin()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8673 | } |
| 8674 | |
Josh Karlin | f3caee164 | 2018-12-10 22:54:27 | [diff] [blame] | 8675 | TEST_F(URLRequestTestHTTP, RedirectPreserveTopFrameURL) { |
| 8676 | ASSERT_TRUE(http_test_server()->Start()); |
| 8677 | |
| 8678 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 8679 | url::Origin top_frame_origin = |
| 8680 | url::Origin::Create(GURL("http://example.com")); |
| 8681 | TestDelegate d; |
| 8682 | { |
| 8683 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8684 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8685 | r->set_top_frame_origin(top_frame_origin); |
| 8686 | |
| 8687 | r->Start(); |
| 8688 | d.RunUntilComplete(); |
| 8689 | |
| 8690 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8691 | EXPECT_EQ(OK, d.request_status()); |
| 8692 | EXPECT_EQ(top_frame_origin, *r->top_frame_origin()); |
| 8693 | } |
| 8694 | } |
| 8695 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8696 | TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8697 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8698 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8699 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8700 | GURL original_first_party_url("http://example.com"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8701 | GURL expected_first_party_url(http_test_server()->GetURL("/echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8702 | |
| 8703 | TestDelegate d; |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8704 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8705 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8706 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8707 | r->set_site_for_cookies(original_first_party_url); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8708 | r->set_first_party_url_policy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8709 | URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 8710 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8711 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8712 | d.RunUntilComplete(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8713 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8714 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8715 | EXPECT_EQ(OK, d.request_status()); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8716 | EXPECT_EQ(expected_first_party_url, r->site_for_cookies()); |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8717 | } |
| 8718 | |
| 8719 | TEST_F(URLRequestTestHTTP, RedirectUpdateTopFrameOrigin) { |
| 8720 | ASSERT_TRUE(http_test_server()->Start()); |
| 8721 | |
| 8722 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 8723 | url::Origin original_top_frame_origin = |
| 8724 | url::Origin::Create(GURL("http://example.com")); |
| 8725 | url::Origin expected_top_frame_origin = |
| 8726 | url::Origin::Create(GURL(http_test_server()->GetURL("/echo"))); |
| 8727 | |
| 8728 | TestDelegate d; |
| 8729 | |
| 8730 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8731 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8732 | r->set_top_frame_origin(original_top_frame_origin); |
| 8733 | r->set_first_party_url_policy(URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 8734 | |
| 8735 | r->Start(); |
| 8736 | d.RunUntilComplete(); |
| 8737 | |
| 8738 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8739 | EXPECT_EQ(OK, d.request_status()); |
| 8740 | EXPECT_EQ(expected_top_frame_origin, *r->top_frame_origin()); |
| 8741 | } |
| 8742 | |
| 8743 | TEST_F(URLRequestTestHTTP, RedirectIgnoreUnsetTopFrameOrigin) { |
| 8744 | ASSERT_TRUE(http_test_server()->Start()); |
| 8745 | |
| 8746 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 8747 | TestDelegate d; |
| 8748 | |
| 8749 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8750 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8751 | // Since we're not setting a top frame origin, we wouldn't expect one after |
| 8752 | // the redirect. |
| 8753 | r->set_first_party_url_policy(URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 8754 | |
| 8755 | r->Start(); |
| 8756 | d.RunUntilComplete(); |
| 8757 | |
| 8758 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8759 | EXPECT_EQ(OK, d.request_status()); |
| 8760 | EXPECT_FALSE(r->top_frame_origin()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8761 | } |
| 8762 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8763 | TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8764 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8765 | |
| 8766 | const char kData[] = "hello world"; |
| 8767 | |
| 8768 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8769 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8770 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 8771 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8772 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8773 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8774 | HttpRequestHeaders headers; |
| 8775 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8776 | base::NumberToString(base::size(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8777 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8778 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8779 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8780 | req.get(), &default_network_delegate_, |
| 8781 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8782 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 8783 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8784 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8785 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8786 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8787 | EXPECT_EQ("GET", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8788 | } |
| 8789 | |
| 8790 | TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8791 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8792 | |
| 8793 | const char kData[] = "hello world"; |
| 8794 | |
| 8795 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8796 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8797 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 8798 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8799 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8800 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8801 | HttpRequestHeaders headers; |
| 8802 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8803 | base::NumberToString(base::size(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8804 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8805 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8806 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8807 | req.get(), &default_network_delegate_, |
| 8808 | http_test_server()->GetURL("/echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 8809 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8810 | "Very Good Reason")); |
| 8811 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8812 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8813 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8814 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8815 | EXPECT_EQ("POST", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8816 | EXPECT_EQ(kData, d.data_received()); |
| 8817 | } |
| 8818 | |
| 8819 | // Check that default A-L header is sent. |
| 8820 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8821 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8822 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 8823 | StaticHttpUserAgentSettings settings("en", std::string()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8824 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8825 | TestURLRequestContext context(true); |
| 8826 | context.set_network_delegate(&network_delegate); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8827 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8828 | context.Init(); |
| 8829 | |
| 8830 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8831 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8832 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8833 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8834 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8835 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8836 | EXPECT_EQ("en", d.data_received()); |
| 8837 | } |
| 8838 | |
| 8839 | // Check that an empty A-L header is not sent. http://crbug.com/77365. |
| 8840 | TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8841 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8842 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 8843 | std::string empty_string; // Avoid most vexing parse on line below. |
| 8844 | StaticHttpUserAgentSettings settings(empty_string, empty_string); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8845 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8846 | TestURLRequestContext context(true); |
| 8847 | context.set_network_delegate(&network_delegate); |
| 8848 | context.Init(); |
| 8849 | // We override the language after initialization because empty entries |
| 8850 | // get overridden by Init(). |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8851 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8852 | |
| 8853 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8854 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8855 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8856 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8857 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8858 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8859 | EXPECT_EQ("None", d.data_received()); |
| 8860 | } |
| 8861 | |
| 8862 | // Check that if request overrides the A-L header, the default is not appended. |
| 8863 | // See http://crbug.com/20894 |
| 8864 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8865 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8866 | |
| 8867 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8868 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8869 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8870 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8871 | HttpRequestHeaders headers; |
| 8872 | headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8873 | req->SetExtraRequestHeaders(headers); |
| 8874 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8875 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8876 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 8877 | } |
| 8878 | |
| 8879 | // Check that default A-E header is sent. |
| 8880 | TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8881 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8882 | |
| 8883 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8884 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8885 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8886 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8887 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8888 | req->SetExtraRequestHeaders(headers); |
| 8889 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8890 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8891 | EXPECT_TRUE(ContainsString(d.data_received(), "gzip")); |
| 8892 | } |
| 8893 | |
| 8894 | // Check that if request overrides the A-E header, the default is not appended. |
| 8895 | // See http://crbug.com/47381 |
| 8896 | TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8897 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8898 | |
| 8899 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8900 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8901 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8902 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8903 | HttpRequestHeaders headers; |
| 8904 | headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8905 | req->SetExtraRequestHeaders(headers); |
| 8906 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8907 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8908 | EXPECT_FALSE(ContainsString(d.data_received(), "gzip")); |
| 8909 | EXPECT_TRUE(ContainsString(d.data_received(), "identity")); |
| 8910 | } |
| 8911 | |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 8912 | // Check that setting the A-C header sends the proper header. |
| 8913 | TEST_F(URLRequestTestHTTP, SetAcceptCharset) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8914 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8915 | |
| 8916 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8917 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8918 | http_test_server()->GetURL("/echoheader?Accept-Charset"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8919 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8920 | HttpRequestHeaders headers; |
| 8921 | headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8922 | req->SetExtraRequestHeaders(headers); |
| 8923 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8924 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8925 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 8926 | } |
| 8927 | |
| 8928 | // Check that default User-Agent header is sent. |
| 8929 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8930 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8931 | |
| 8932 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8933 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8934 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8935 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8936 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8937 | d.RunUntilComplete(); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8938 | EXPECT_EQ(default_context().http_user_agent_settings()->GetUserAgent(), |
[email protected] | 051a4b3 | 2014-01-09 04:02:37 | [diff] [blame] | 8939 | d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8940 | } |
| 8941 | |
| 8942 | // Check that if request overrides the User-Agent header, |
| 8943 | // the default is not appended. |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 8944 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 8945 | #if defined(OS_IOS) |
| 8946 | #define MAYBE_OverrideUserAgent FLAKY_OverrideUserAgent |
| 8947 | #else |
| 8948 | #define MAYBE_OverrideUserAgent OverrideUserAgent |
| 8949 | #endif |
| 8950 | TEST_F(URLRequestTestHTTP, MAYBE_OverrideUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8951 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8952 | |
| 8953 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8954 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8955 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8956 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8957 | HttpRequestHeaders headers; |
| 8958 | headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8959 | req->SetExtraRequestHeaders(headers); |
| 8960 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8961 | d.RunUntilComplete(); |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 8962 | EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8963 | } |
| 8964 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8965 | // Check that a NULL HttpUserAgentSettings causes the corresponding empty |
| 8966 | // User-Agent header to be sent but does not send the Accept-Language and |
| 8967 | // Accept-Charset headers. |
| 8968 | TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8969 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8970 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8971 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8972 | TestURLRequestContext context(true); |
| 8973 | context.set_network_delegate(&network_delegate); |
| 8974 | context.Init(); |
| 8975 | // We override the HttpUserAgentSettings after initialization because empty |
| 8976 | // entries get overridden by Init(). |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 8977 | context.set_http_user_agent_settings(nullptr); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8978 | |
| 8979 | struct { |
| 8980 | const char* request; |
| 8981 | const char* expected_response; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8982 | } tests[] = {{"/echoheader?Accept-Language", "None"}, |
| 8983 | {"/echoheader?Accept-Charset", "None"}, |
| 8984 | {"/echoheader?User-Agent", ""}}; |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8985 | |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8986 | for (size_t i = 0; i < base::size(tests); i++) { |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8987 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8988 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8989 | http_test_server()->GetURL(tests[i].request), DEFAULT_PRIORITY, &d, |
| 8990 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8991 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8992 | d.RunUntilComplete(); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8993 | EXPECT_EQ(tests[i].expected_response, d.data_received()) |
| 8994 | << " Request = \"" << tests[i].request << "\""; |
| 8995 | } |
| 8996 | } |
| 8997 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8998 | // Make sure that URLRequest passes on its priority updates to |
| 8999 | // newly-created jobs after the first one. |
| 9000 | TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9001 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9002 | |
| 9003 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9004 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9005 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9006 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9007 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9008 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9009 | std::unique_ptr<URLRequestRedirectJob> redirect_job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9010 | req.get(), &default_network_delegate_, |
| 9011 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9012 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 9013 | AddTestInterceptor()->set_main_intercept_job(std::move(redirect_job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9014 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9015 | req->SetPriority(LOW); |
| 9016 | req->Start(); |
| 9017 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9018 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9019 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9020 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9021 | req.get(), &default_network_delegate_, &job_priority)); |
| 9022 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9023 | |
| 9024 | // Should trigger |job| to be started. |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9025 | d.RunUntilComplete(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9026 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9027 | } |
| 9028 | |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9029 | // Check that creating a network request while entering/exiting suspend mode |
| 9030 | // fails as it should. This is the only case where an HttpTransactionFactory |
| 9031 | // does not return an HttpTransaction. |
| 9032 | TEST_F(URLRequestTestHTTP, NetworkSuspendTest) { |
| 9033 | // Create a new HttpNetworkLayer that thinks it's suspended. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9034 | std::unique_ptr<HttpNetworkLayer> network_layer(new HttpNetworkLayer( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9035 | default_context().http_transaction_factory()->GetSession())); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9036 | network_layer->OnSuspend(); |
| 9037 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 9038 | HttpCache http_cache(std::move(network_layer), |
zhongyi | 3ceab9f | 2017-02-04 02:06:03 | [diff] [blame] | 9039 | HttpCache::DefaultBackend::InMemory(0), |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 9040 | false /* is_main_cache */); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9041 | |
| 9042 | TestURLRequestContext context(true); |
| 9043 | context.set_http_transaction_factory(&http_cache); |
| 9044 | context.Init(); |
| 9045 | |
| 9046 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9047 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9048 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 9049 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9050 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9051 | d.RunUntilComplete(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9052 | |
| 9053 | EXPECT_TRUE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9054 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, d.request_status()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9055 | } |
| 9056 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9057 | namespace { |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9058 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9059 | // HttpTransactionFactory that synchronously fails to create transactions. |
| 9060 | class FailingHttpTransactionFactory : public HttpTransactionFactory { |
| 9061 | public: |
| 9062 | explicit FailingHttpTransactionFactory(HttpNetworkSession* network_session) |
| 9063 | : network_session_(network_session) {} |
| 9064 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 9065 | ~FailingHttpTransactionFactory() override = default; |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9066 | |
| 9067 | // HttpTransactionFactory methods: |
| 9068 | int CreateTransaction(RequestPriority priority, |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9069 | std::unique_ptr<HttpTransaction>* trans) override { |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9070 | return ERR_FAILED; |
| 9071 | } |
| 9072 | |
| 9073 | HttpCache* GetCache() override { return nullptr; } |
| 9074 | |
| 9075 | HttpNetworkSession* GetSession() override { return network_session_; } |
| 9076 | |
| 9077 | private: |
| 9078 | HttpNetworkSession* network_session_; |
| 9079 | |
| 9080 | DISALLOW_COPY_AND_ASSIGN(FailingHttpTransactionFactory); |
| 9081 | }; |
| 9082 | |
| 9083 | } // namespace |
| 9084 | |
| 9085 | // Check that when a request that fails to create an HttpTransaction can be |
| 9086 | // cancelled while the failure notification is pending, and doesn't send two |
| 9087 | // failure notifications. |
| 9088 | // |
| 9089 | // This currently only happens when in suspend mode and there's no cache, but |
| 9090 | // just use a special HttpTransactionFactory, to avoid depending on those |
| 9091 | // behaviors. |
| 9092 | TEST_F(URLRequestTestHTTP, NetworkCancelAfterCreateTransactionFailsTest) { |
| 9093 | FailingHttpTransactionFactory http_transaction_factory( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9094 | default_context().http_transaction_factory()->GetSession()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9095 | TestURLRequestContext context(true); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9096 | context.set_http_transaction_factory(&http_transaction_factory); |
| 9097 | context.set_network_delegate(default_network_delegate()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9098 | context.Init(); |
| 9099 | |
| 9100 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9101 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9102 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 9103 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9104 | // Don't send cookies (Collecting cookies is asynchronous, and need request to |
| 9105 | // try to create an HttpNetworkTransaction synchronously on start). |
| 9106 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9107 | req->Start(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9108 | req->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9109 | d.RunUntilComplete(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9110 | // Run pending error task, if there is one. |
| 9111 | base::RunLoop().RunUntilIdle(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9112 | |
| 9113 | EXPECT_TRUE(d.request_failed()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9114 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9115 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9116 | |
| 9117 | // NetworkDelegate should see the cancellation, but not the error. |
| 9118 | EXPECT_EQ(1, default_network_delegate()->canceled_requests()); |
| 9119 | EXPECT_EQ(0, default_network_delegate()->error_count()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9120 | } |
| 9121 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9122 | TEST_F(URLRequestTestHTTP, NetworkAccessedSetOnNetworkRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9123 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9124 | |
| 9125 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9126 | GURL test_url(http_test_server()->GetURL("/")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9127 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9128 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9129 | |
| 9130 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9131 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9132 | |
| 9133 | EXPECT_TRUE(req->response_info().network_accessed); |
| 9134 | } |
| 9135 | |
| 9136 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnCachedResponse) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9137 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9138 | |
| 9139 | // Populate the cache. |
| 9140 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9141 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9142 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 9143 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9144 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9145 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9146 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9147 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9148 | EXPECT_TRUE(req->response_info().network_accessed); |
| 9149 | EXPECT_FALSE(req->response_info().was_cached); |
| 9150 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9151 | req = default_context().CreateRequest( |
| 9152 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 9153 | TRAFFIC_ANNOTATION_FOR_TESTS); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9154 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9155 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9156 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9157 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9158 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9159 | EXPECT_TRUE(req->response_info().was_cached); |
| 9160 | } |
| 9161 | |
| 9162 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnLoadOnlyFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9163 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9164 | |
| 9165 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9166 | GURL test_url(http_test_server()->GetURL("/")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9167 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9168 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jkarlin | a067deed | 2016-10-27 14:48:33 | [diff] [blame] | 9169 | req->SetLoadFlags(LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9170 | |
| 9171 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9172 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9173 | |
| 9174 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9175 | } |
| 9176 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 9177 | // Test that a single job with a THROTTLED priority completes |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 9178 | // correctly in the absence of contention. |
| 9179 | TEST_F(URLRequestTestHTTP, ThrottledPriority) { |
| 9180 | ASSERT_TRUE(http_test_server()->Start()); |
| 9181 | |
| 9182 | TestDelegate d; |
| 9183 | GURL test_url(http_test_server()->GetURL("/")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9184 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9185 | test_url, THROTTLED, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 9186 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9187 | d.RunUntilComplete(); |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 9188 | |
| 9189 | EXPECT_TRUE(req->status().is_success()); |
| 9190 | } |
| 9191 | |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9192 | TEST_F(URLRequestTestHTTP, RawBodyBytesNoContentEncoding) { |
| 9193 | ASSERT_TRUE(http_test_server()->Start()); |
| 9194 | |
| 9195 | TestDelegate d; |
| 9196 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9197 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 9198 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9199 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9200 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9201 | |
| 9202 | EXPECT_EQ(5, req->GetRawBodyBytes()); |
| 9203 | } |
| 9204 | |
| 9205 | TEST_F(URLRequestTestHTTP, RawBodyBytesGzipEncoding) { |
| 9206 | ASSERT_TRUE(http_test_server()->Start()); |
| 9207 | |
| 9208 | TestDelegate d; |
| 9209 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9210 | http_test_server()->GetURL("/gzip-encoded"), DEFAULT_PRIORITY, &d, |
| 9211 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9212 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9213 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9214 | |
| 9215 | EXPECT_EQ(30, req->GetRawBodyBytes()); |
| 9216 | } |
| 9217 | |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9218 | // Check that if NetworkDelegate::OnBeforeStartTransaction returns an error, |
| 9219 | // the delegate isn't called back synchronously. |
| 9220 | TEST_F(URLRequestTestHTTP, TesBeforeStartTransactionFails) { |
| 9221 | ASSERT_TRUE(http_test_server()->Start()); |
| 9222 | default_network_delegate_.set_before_start_transaction_fails(); |
| 9223 | |
| 9224 | TestDelegate d; |
| 9225 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9226 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 9227 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9228 | req->Start(); |
| 9229 | DCHECK(!d.response_completed()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9230 | d.RunUntilComplete(); |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9231 | DCHECK(d.response_completed()); |
| 9232 | EXPECT_EQ(ERR_FAILED, d.request_status()); |
| 9233 | } |
| 9234 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9235 | class URLRequestInterceptorTestHTTP : public URLRequestTestHTTP { |
| 9236 | public: |
| 9237 | // TODO(bengr): Merge this with the URLRequestInterceptorHTTPTest fixture, |
| 9238 | // ideally remove the dependency on URLRequestTestJob, and maybe move these |
| 9239 | // tests into the factory tests. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 9240 | URLRequestInterceptorTestHTTP() |
| 9241 | : URLRequestTestHTTP(), interceptor_(nullptr) {} |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9242 | |
| 9243 | void SetUpFactory() override { |
| 9244 | interceptor_ = new MockURLRequestInterceptor(); |
| 9245 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9246 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9247 | } |
| 9248 | |
| 9249 | MockURLRequestInterceptor* interceptor() const { |
| 9250 | return interceptor_; |
| 9251 | } |
| 9252 | |
| 9253 | private: |
| 9254 | MockURLRequestInterceptor* interceptor_; |
| 9255 | }; |
| 9256 | |
| 9257 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9258 | NetworkDelegateNotificationOnRedirectIntercept) { |
| 9259 | interceptor()->set_intercept_redirect(true); |
| 9260 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 9261 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 9262 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9263 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9264 | |
| 9265 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9266 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9267 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 9268 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9269 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9270 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9271 | |
| 9272 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 9273 | // Check we got one good response |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9274 | int status = d.request_status(); |
| 9275 | EXPECT_EQ(OK, status); |
| 9276 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9277 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9278 | |
| 9279 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 9280 | EXPECT_EQ(1, d.response_started_count()); |
| 9281 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9282 | |
| 9283 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9284 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9285 | EXPECT_EQ(1, default_network_delegate()->headers_received_count()); |
| 9286 | } |
| 9287 | |
| 9288 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9289 | NetworkDelegateNotificationOnErrorIntercept) { |
| 9290 | // Intercept that error and respond with an OK response. |
| 9291 | interceptor()->set_intercept_final_response(true); |
| 9292 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 9293 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 9294 | default_network_delegate()->set_can_be_intercepted_on_error(true); |
| 9295 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9296 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9297 | |
| 9298 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9299 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9300 | http_test_server()->GetURL("/two-content-lengths.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9301 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9302 | req->set_method("GET"); |
| 9303 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9304 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9305 | |
| 9306 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 9307 | |
| 9308 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9309 | int status = d.request_status(); |
| 9310 | EXPECT_EQ(OK, status); |
| 9311 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9312 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9313 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 9314 | EXPECT_EQ(1, d.response_started_count()); |
| 9315 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9316 | |
| 9317 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9318 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9319 | EXPECT_EQ(0, default_network_delegate()->headers_received_count()); |
| 9320 | } |
| 9321 | |
| 9322 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9323 | NetworkDelegateNotificationOnResponseIntercept) { |
| 9324 | // Intercept that error and respond with an OK response. |
| 9325 | interceptor()->set_intercept_final_response(true); |
| 9326 | |
| 9327 | // Intercept with a real URLRequestHttpJob. |
| 9328 | interceptor()->set_use_url_request_http_job(true); |
| 9329 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9330 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9331 | |
| 9332 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9333 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9334 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 9335 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9336 | req->set_method("GET"); |
| 9337 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9338 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9339 | |
| 9340 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 9341 | |
| 9342 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9343 | int status = d.request_status(); |
| 9344 | EXPECT_EQ(OK, status); |
| 9345 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9346 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9347 | EXPECT_EQ("hello", d.data_received()); |
| 9348 | EXPECT_EQ(1, d.response_started_count()); |
| 9349 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9350 | |
| 9351 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9352 | EXPECT_EQ(2, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9353 | EXPECT_EQ(2, default_network_delegate()->headers_received_count()); |
| 9354 | } |
| 9355 | |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9356 | class URLRequestTestReferrerPolicy : public URLRequestTest { |
| 9357 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 9358 | URLRequestTestReferrerPolicy() = default; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9359 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9360 | void InstantiateSameOriginServers(net::EmbeddedTestServer::Type type) { |
| 9361 | origin_server_.reset(new EmbeddedTestServer(type)); |
| 9362 | if (type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9363 | origin_server_->AddDefaultHandlers( |
| 9364 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9365 | } else { |
| 9366 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9367 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9368 | ASSERT_TRUE(origin_server_->Start()); |
| 9369 | } |
| 9370 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9371 | void InstantiateCrossOriginServers(net::EmbeddedTestServer::Type origin_type, |
| 9372 | net::EmbeddedTestServer::Type dest_type) { |
| 9373 | origin_server_.reset(new EmbeddedTestServer(origin_type)); |
| 9374 | if (origin_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9375 | origin_server_->AddDefaultHandlers( |
| 9376 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9377 | } else { |
| 9378 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9379 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9380 | ASSERT_TRUE(origin_server_->Start()); |
| 9381 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9382 | destination_server_.reset(new EmbeddedTestServer(dest_type)); |
| 9383 | if (dest_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9384 | destination_server_->AddDefaultHandlers( |
| 9385 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9386 | } else { |
| 9387 | destination_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9388 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9389 | ASSERT_TRUE(destination_server_->Start()); |
| 9390 | } |
| 9391 | |
| 9392 | void VerifyReferrerAfterRedirect(URLRequest::ReferrerPolicy policy, |
| 9393 | const GURL& referrer, |
| 9394 | const GURL& expected) { |
| 9395 | // Create and execute the request: we'll only have a |destination_server_| |
| 9396 | // if the origins are meant to be distinct. Otherwise, we'll use the |
| 9397 | // |origin_server_| for both endpoints. |
| 9398 | GURL destination_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9399 | destination_server_ ? destination_server_->GetURL("/echoheader?Referer") |
| 9400 | : origin_server_->GetURL("/echoheader?Referer"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9401 | GURL origin_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9402 | origin_server_->GetURL("/server-redirect?" + destination_url.spec()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9403 | |
| 9404 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9405 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9406 | origin_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9407 | req->set_referrer_policy(policy); |
| 9408 | req->SetReferrer(referrer.spec()); |
| 9409 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9410 | d.RunUntilComplete(); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9411 | |
| 9412 | EXPECT_EQ(1, d.response_started_count()); |
| 9413 | EXPECT_EQ(1, d.received_redirect_count()); |
| 9414 | EXPECT_EQ(destination_url, req->url()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9415 | EXPECT_EQ(OK, d.request_status()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9416 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9417 | |
| 9418 | EXPECT_EQ(expected.spec(), req->referrer()); |
| 9419 | if (expected.is_empty()) |
| 9420 | EXPECT_EQ("None", d.data_received()); |
| 9421 | else |
| 9422 | EXPECT_EQ(expected.spec(), d.data_received()); |
| 9423 | } |
| 9424 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9425 | EmbeddedTestServer* origin_server() const { return origin_server_.get(); } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9426 | |
| 9427 | private: |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9428 | std::unique_ptr<EmbeddedTestServer> origin_server_; |
| 9429 | std::unique_ptr<EmbeddedTestServer> destination_server_; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9430 | }; |
| 9431 | |
| 9432 | TEST_F(URLRequestTestReferrerPolicy, HTTPToSameOriginHTTP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9433 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTP); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9434 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9435 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9436 | VerifyReferrerAfterRedirect( |
| 9437 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9438 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9439 | |
| 9440 | VerifyReferrerAfterRedirect( |
| 9441 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9442 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9443 | |
| 9444 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9445 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9446 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9447 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9448 | referrer); |
| 9449 | |
| 9450 | // The original referrer set on the request is expected to obey the referrer |
| 9451 | // policy and already be stripped to the origin; thus this test case just |
| 9452 | // checks that this policy doesn't cause the referrer to change when following |
| 9453 | // a redirect. |
| 9454 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9455 | referrer.GetOrigin()); |
| 9456 | |
| 9457 | VerifyReferrerAfterRedirect( |
| 9458 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9459 | referrer); |
| 9460 | |
| 9461 | // The original referrer set on the request is expected to obey the referrer |
| 9462 | // policy and already be stripped to the origin; thus this test case just |
| 9463 | // checks that this policy doesn't cause the referrer to change when following |
| 9464 | // a redirect. |
| 9465 | VerifyReferrerAfterRedirect( |
| 9466 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9467 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9468 | |
| 9469 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9470 | } |
| 9471 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9472 | TEST_F(URLRequestTestReferrerPolicy, HTTPToCrossOriginHTTP) { |
| 9473 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 9474 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9475 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9476 | |
| 9477 | VerifyReferrerAfterRedirect( |
| 9478 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9479 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9480 | |
| 9481 | VerifyReferrerAfterRedirect( |
| 9482 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9483 | referrer, referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9484 | |
| 9485 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9486 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9487 | referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9488 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9489 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9490 | referrer); |
| 9491 | |
| 9492 | // The original referrer set on the request is expected to obey the referrer |
| 9493 | // policy and already be stripped to the origin; thus this test case just |
| 9494 | // checks that this policy doesn't cause the referrer to change when following |
| 9495 | // a redirect. |
| 9496 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9497 | referrer.GetOrigin()); |
| 9498 | |
| 9499 | VerifyReferrerAfterRedirect( |
| 9500 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9501 | |
| 9502 | // The original referrer set on the request is expected to obey the referrer |
| 9503 | // policy and already be stripped to the origin; thus this test case just |
| 9504 | // checks that this policy doesn't cause the referrer to change when following |
| 9505 | // a redirect. |
| 9506 | VerifyReferrerAfterRedirect( |
| 9507 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9508 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9509 | |
| 9510 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9511 | } |
| 9512 | |
| 9513 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToSameOriginHTTPS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9514 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9515 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9516 | |
| 9517 | VerifyReferrerAfterRedirect( |
| 9518 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9519 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9520 | |
| 9521 | VerifyReferrerAfterRedirect( |
| 9522 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9523 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9524 | |
| 9525 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9526 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9527 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9528 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9529 | referrer); |
| 9530 | |
| 9531 | // The original referrer set on the request is expected to obey the referrer |
| 9532 | // policy and already be stripped to the origin; thus this test case just |
| 9533 | // checks that this policy doesn't cause the referrer to change when following |
| 9534 | // a redirect. |
| 9535 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9536 | referrer.GetOrigin()); |
| 9537 | |
| 9538 | VerifyReferrerAfterRedirect( |
| 9539 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9540 | referrer); |
| 9541 | |
| 9542 | // The original referrer set on the request is expected to obey the referrer |
| 9543 | // policy and already be stripped to the origin; thus this test case just |
| 9544 | // checks that this policy doesn't cause the referrer to change when following |
| 9545 | // a redirect. |
| 9546 | VerifyReferrerAfterRedirect( |
| 9547 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9548 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9549 | |
| 9550 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9551 | } |
| 9552 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9553 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToCrossOriginHTTPS) { |
| 9554 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 9555 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9556 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9557 | |
| 9558 | VerifyReferrerAfterRedirect( |
| 9559 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9560 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9561 | |
| 9562 | VerifyReferrerAfterRedirect( |
| 9563 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9564 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9565 | |
| 9566 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9567 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9568 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9569 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9570 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9571 | referrer); |
| 9572 | |
| 9573 | // The original referrer set on the request is expected to obey the referrer |
| 9574 | // policy and already be stripped to the origin; thus this test case just |
| 9575 | // checks that this policy doesn't cause the referrer to change when following |
| 9576 | // a redirect. |
| 9577 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9578 | referrer.GetOrigin()); |
| 9579 | |
| 9580 | VerifyReferrerAfterRedirect( |
| 9581 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9582 | |
| 9583 | // The original referrer set on the request is expected to obey the referrer |
| 9584 | // policy and already be stripped to the origin; thus this test case just |
| 9585 | // checks that this policy doesn't cause the referrer to change when following |
| 9586 | // a redirect. |
| 9587 | VerifyReferrerAfterRedirect( |
| 9588 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9589 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9590 | |
| 9591 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9592 | } |
| 9593 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9594 | TEST_F(URLRequestTestReferrerPolicy, HTTPToHTTPS) { |
| 9595 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 9596 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9597 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9598 | |
| 9599 | VerifyReferrerAfterRedirect( |
| 9600 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9601 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9602 | |
| 9603 | VerifyReferrerAfterRedirect( |
| 9604 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9605 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9606 | |
| 9607 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9608 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9609 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9610 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9611 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9612 | referrer); |
| 9613 | |
| 9614 | // The original referrer set on the request is expected to obey the referrer |
| 9615 | // policy and already be stripped to the origin; thus this test case just |
| 9616 | // checks that this policy doesn't cause the referrer to change when following |
| 9617 | // a redirect. |
| 9618 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9619 | referrer.GetOrigin()); |
| 9620 | |
| 9621 | VerifyReferrerAfterRedirect( |
| 9622 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9623 | |
| 9624 | // The original referrer set on the request is expected to obey the referrer |
| 9625 | // policy and already be stripped to the origin; thus this test case just |
| 9626 | // checks that this policy doesn't cause the referrer to change when following |
| 9627 | // a redirect. |
| 9628 | VerifyReferrerAfterRedirect( |
| 9629 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9630 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9631 | |
| 9632 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9633 | } |
| 9634 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9635 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToHTTP) { |
| 9636 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 9637 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9638 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9639 | |
| 9640 | VerifyReferrerAfterRedirect( |
| 9641 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9642 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9643 | |
| 9644 | VerifyReferrerAfterRedirect( |
| 9645 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9646 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9647 | |
| 9648 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9649 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9650 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9651 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9652 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9653 | referrer); |
| 9654 | |
| 9655 | // The original referrer set on the request is expected to obey the referrer |
| 9656 | // policy and already be stripped to the origin; thus this test case just |
| 9657 | // checks that this policy doesn't cause the referrer to change when following |
| 9658 | // a redirect. |
| 9659 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9660 | referrer.GetOrigin()); |
| 9661 | |
| 9662 | VerifyReferrerAfterRedirect( |
| 9663 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9664 | |
| 9665 | // The original referrer set on the request is expected to obey the referrer |
| 9666 | // policy and already be stripped to the origin, though it should be |
| 9667 | // subsequently cleared during the downgrading redirect. |
| 9668 | VerifyReferrerAfterRedirect( |
| 9669 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9670 | referrer.GetOrigin(), GURL()); |
| 9671 | |
| 9672 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9673 | } |
| 9674 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 9675 | class HTTPSRequestTest : public TestWithScopedTaskEnvironment { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9676 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 9677 | HTTPSRequestTest() : default_context_(true) { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9678 | default_context_.set_network_delegate(&default_network_delegate_); |
| 9679 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9680 | } |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9681 | ~HTTPSRequestTest() override { |
| 9682 | SetTransportSecurityStateSourceForTesting(nullptr); |
| 9683 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9684 | |
| 9685 | protected: |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9686 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9687 | TestURLRequestContext default_context_; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9688 | }; |
| 9689 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 9690 | TEST_F(HTTPSRequestTest, HTTPSGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9691 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9692 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9693 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9694 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9695 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9696 | TestDelegate d; |
| 9697 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9698 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9699 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9700 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9701 | r->Start(); |
| 9702 | EXPECT_TRUE(r->is_pending()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9703 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9704 | d.RunUntilComplete(); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9705 | |
| 9706 | EXPECT_EQ(1, d.response_started_count()); |
| 9707 | EXPECT_FALSE(d.received_data_before_response()); |
| 9708 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9709 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 9710 | EXPECT_EQ(test_server.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 9711 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 9712 | EXPECT_EQ(test_server.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 9713 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9714 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9715 | } |
| 9716 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 9717 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9718 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9719 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9720 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9721 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9722 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9723 | |
| 9724 | bool err_allowed = true; |
| 9725 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 9726 | TestDelegate d; |
| 9727 | { |
| 9728 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9729 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9730 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9731 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9732 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9733 | r->Start(); |
| 9734 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9735 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9736 | d.RunUntilComplete(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9737 | |
| 9738 | EXPECT_EQ(1, d.response_started_count()); |
| 9739 | EXPECT_FALSE(d.received_data_before_response()); |
| 9740 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9741 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9742 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9743 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9744 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9745 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9746 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9747 | } |
| 9748 | } |
| 9749 | } |
| 9750 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 9751 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9752 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9753 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED); |
| 9754 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9755 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9756 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9757 | |
| 9758 | // Iterate from false to true, just so that we do the opposite of the |
| 9759 | // previous test in order to increase test coverage. |
| 9760 | bool err_allowed = false; |
| 9761 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 9762 | TestDelegate d; |
| 9763 | { |
| 9764 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9765 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9766 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9767 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9768 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9769 | r->Start(); |
| 9770 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9771 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9772 | d.RunUntilComplete(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9773 | |
| 9774 | EXPECT_EQ(1, d.response_started_count()); |
| 9775 | EXPECT_FALSE(d.received_data_before_response()); |
| 9776 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9777 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9778 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9779 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9780 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9781 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9782 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9783 | } |
| 9784 | } |
| 9785 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 9786 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9787 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 9788 | // security state. (see http://crbug.com/550977). |
| 9789 | #if !defined(OS_IOS) |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9790 | // This tests that a load of a domain with preloaded HSTS and HPKP with a |
| 9791 | // certificate error sets the |certificate_errors_are_fatal| flag correctly. |
| 9792 | // This flag will cause the interstitial to be fatal. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9793 | TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9794 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 9795 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9796 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9797 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9798 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9799 | ASSERT_TRUE(test_server.Start()); |
| 9800 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9801 | // We require that the URL be hsts-hpkp-preloaded.test. This is a test domain |
| 9802 | // that has a preloaded HSTS+HPKP entry in the TransportSecurityState. This |
| 9803 | // means that we have to use a MockHostResolver in order to direct |
| 9804 | // hsts-hpkp-preloaded.test to the testserver. By default, MockHostResolver |
| 9805 | // maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9806 | |
| 9807 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9808 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9809 | TestURLRequestContext context(true); |
| 9810 | context.set_network_delegate(&network_delegate); |
| 9811 | context.set_host_resolver(&host_resolver); |
| 9812 | TransportSecurityState transport_security_state; |
| 9813 | context.set_transport_security_state(&transport_security_state); |
| 9814 | context.Init(); |
| 9815 | |
| 9816 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9817 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9818 | GURL(base::StringPrintf("https://hsts-hpkp-preloaded.test:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9819 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9820 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9821 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9822 | r->Start(); |
| 9823 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9824 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9825 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9826 | |
| 9827 | EXPECT_EQ(1, d.response_started_count()); |
| 9828 | EXPECT_FALSE(d.received_data_before_response()); |
| 9829 | EXPECT_TRUE(d.have_certificate_errors()); |
| 9830 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 9831 | } |
| 9832 | |
| 9833 | // This tests that cached HTTPS page loads do not cause any updates to the |
| 9834 | // TransportSecurityState. |
| 9835 | TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9836 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 9837 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9838 | // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't |
| 9839 | // matter. It just has to be any error. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9840 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9841 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9842 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9843 | ASSERT_TRUE(test_server.Start()); |
| 9844 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9845 | // We require that the URL be hsts-hpkp-preloaded.test. This is a test domain |
| 9846 | // that has a preloaded HSTS+HPKP entry in the TransportSecurityState. This |
| 9847 | // means that we have to use a MockHostResolver in order to direct |
| 9848 | // hsts-hpkp-preloaded.test to the testserver. By default, MockHostResolver |
| 9849 | // maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9850 | |
| 9851 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9852 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9853 | TestURLRequestContext context(true); |
| 9854 | context.set_network_delegate(&network_delegate); |
| 9855 | context.set_host_resolver(&host_resolver); |
| 9856 | TransportSecurityState transport_security_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9857 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9858 | TransportSecurityState::STSState static_sts_state; |
| 9859 | TransportSecurityState::PKPState static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9860 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9861 | "hsts-hpkp-preloaded.test", &static_sts_state, &static_pkp_state)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9862 | context.set_transport_security_state(&transport_security_state); |
| 9863 | context.Init(); |
| 9864 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9865 | TransportSecurityState::STSState dynamic_sts_state; |
| 9866 | TransportSecurityState::PKPState dynamic_pkp_state; |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9867 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
| 9868 | "hsts-hpkp-preloaded.test", &dynamic_sts_state)); |
| 9869 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
| 9870 | "hsts-hpkp-preloaded.test", &dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9871 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9872 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9873 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9874 | GURL(base::StringPrintf("https://hsts-hpkp-preloaded.test:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9875 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9876 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9877 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9878 | r->Start(); |
| 9879 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9880 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9881 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9882 | |
| 9883 | EXPECT_EQ(1, d.response_started_count()); |
| 9884 | EXPECT_FALSE(d.received_data_before_response()); |
| 9885 | EXPECT_TRUE(d.have_certificate_errors()); |
| 9886 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 9887 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9888 | // Get a fresh copy of the states, and check that they haven't changed. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9889 | TransportSecurityState::STSState new_static_sts_state; |
| 9890 | TransportSecurityState::PKPState new_static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9891 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9892 | "hsts-hpkp-preloaded.test", &new_static_sts_state, |
| 9893 | &new_static_pkp_state)); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9894 | TransportSecurityState::STSState new_dynamic_sts_state; |
| 9895 | TransportSecurityState::PKPState new_dynamic_pkp_state; |
| 9896 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9897 | "hsts-hpkp-preloaded.test", &new_dynamic_sts_state)); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9898 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9899 | "hsts-hpkp-preloaded.test", &new_dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9900 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9901 | EXPECT_EQ(new_static_sts_state.upgrade_mode, static_sts_state.upgrade_mode); |
| 9902 | EXPECT_EQ(new_static_sts_state.include_subdomains, |
| 9903 | static_sts_state.include_subdomains); |
| 9904 | EXPECT_EQ(new_static_pkp_state.include_subdomains, |
| 9905 | static_pkp_state.include_subdomains); |
rsleevi | 91d4c9c | 2016-05-14 20:28:48 | [diff] [blame] | 9906 | EXPECT_EQ(new_static_pkp_state.spki_hashes, static_pkp_state.spki_hashes); |
| 9907 | EXPECT_EQ(new_static_pkp_state.bad_spki_hashes, |
| 9908 | static_pkp_state.bad_spki_hashes); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9909 | } |
| 9910 | |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9911 | // Make sure HSTS preserves a POST request's method and body. |
| 9912 | TEST_F(HTTPSRequestTest, HSTSPreservesPosts) { |
| 9913 | static const char kData[] = "hello world"; |
| 9914 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9915 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9916 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9917 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9918 | ASSERT_TRUE(test_server.Start()); |
| 9919 | |
| 9920 | |
| 9921 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 9922 | // address, so a MockHostResolver is needed to redirect www.somewhere.com to |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9923 | // the EmbeddedTestServer. By default, MockHostResolver maps all hosts |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9924 | // to 127.0.0.1. |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9925 | MockHostResolver host_resolver; |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9926 | |
| 9927 | // Force https for www.somewhere.com. |
| 9928 | TransportSecurityState transport_security_state; |
[email protected] | 474f079e | 2013-03-02 19:11:20 | [diff] [blame] | 9929 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 9930 | bool include_subdomains = false; |
| 9931 | transport_security_state.AddHSTS("www.somewhere.com", expiry, |
| 9932 | include_subdomains); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9933 | |
| 9934 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 9935 | |
| 9936 | TestURLRequestContext context(true); |
| 9937 | context.set_host_resolver(&host_resolver); |
| 9938 | context.set_transport_security_state(&transport_security_state); |
| 9939 | context.set_network_delegate(&network_delegate); |
| 9940 | context.Init(); |
| 9941 | |
| 9942 | TestDelegate d; |
| 9943 | // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will |
| 9944 | // cause a certificate error. Ignore the error. |
| 9945 | d.set_allow_certificate_errors(true); |
| 9946 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9947 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9948 | GURL(base::StringPrintf("http://www.somewhere.com:%d/echo", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9949 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9950 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9951 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 9952 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9953 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9954 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9955 | d.RunUntilComplete(); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9956 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9957 | EXPECT_EQ("https", req->url().scheme()); |
| 9958 | EXPECT_EQ("POST", req->method()); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9959 | EXPECT_EQ(kData, d.data_received()); |
[email protected] | 07d9bfd | 2013-06-27 14:16:40 | [diff] [blame] | 9960 | |
| 9961 | LoadTimingInfo load_timing_info; |
| 9962 | network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info); |
| 9963 | // LoadTimingInfo of HSTS redirects is similar to that of network cache hits |
| 9964 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9965 | } |
| 9966 | |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9967 | // Make sure that the CORS headers are added to cross-origin HSTS redirects. |
| 9968 | TEST_F(HTTPSRequestTest, HSTSCrossOriginAddHeaders) { |
| 9969 | static const char kOriginHeaderValue[] = "http://www.example.com"; |
| 9970 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9971 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9972 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9973 | ASSERT_TRUE(test_server.Start()); |
| 9974 | |
| 9975 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 9976 | // address, so a MockHostResolver is needed to redirect example.net to the |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9977 | // EmbeddedTestServer. MockHostResolver maps all hosts to 127.0.0.1 by |
| 9978 | // default. |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9979 | MockHostResolver host_resolver; |
| 9980 | |
| 9981 | TransportSecurityState transport_security_state; |
| 9982 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 9983 | bool include_subdomains = false; |
| 9984 | transport_security_state.AddHSTS("example.net", expiry, include_subdomains); |
| 9985 | |
| 9986 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 9987 | |
| 9988 | MockCertVerifier cert_verifier; |
| 9989 | cert_verifier.set_default_result(OK); |
| 9990 | |
| 9991 | TestURLRequestContext context(true); |
| 9992 | context.set_host_resolver(&host_resolver); |
| 9993 | context.set_transport_security_state(&transport_security_state); |
| 9994 | context.set_network_delegate(&network_delegate); |
| 9995 | context.set_cert_verifier(&cert_verifier); |
| 9996 | context.Init(); |
| 9997 | |
| 9998 | GURL hsts_http_url(base::StringPrintf("http://example.net:%d/somehstssite", |
| 9999 | test_server.host_port_pair().port())); |
| 10000 | url::Replacements<char> replacements; |
| 10001 | const char kNewScheme[] = "https"; |
| 10002 | replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme))); |
| 10003 | GURL hsts_https_url = hsts_http_url.ReplaceComponents(replacements); |
| 10004 | |
| 10005 | TestDelegate d; |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10006 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10007 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 10008 | hsts_http_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10009 | // Set Origin header to simulate a cross-origin request. |
| 10010 | HttpRequestHeaders request_headers; |
| 10011 | request_headers.SetHeader("Origin", kOriginHeaderValue); |
| 10012 | req->SetExtraRequestHeaders(request_headers); |
| 10013 | |
| 10014 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10015 | d.RunUntilRedirect(); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10016 | |
| 10017 | EXPECT_EQ(1, d.received_redirect_count()); |
| 10018 | |
| 10019 | const HttpResponseHeaders* headers = req->response_headers(); |
| 10020 | std::string redirect_location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 10021 | EXPECT_TRUE( |
| 10022 | headers->EnumerateHeader(nullptr, "Location", &redirect_location)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10023 | EXPECT_EQ(hsts_https_url.spec(), redirect_location); |
| 10024 | |
| 10025 | std::string received_cors_header; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 10026 | EXPECT_TRUE(headers->EnumerateHeader(nullptr, "Access-Control-Allow-Origin", |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10027 | &received_cors_header)); |
| 10028 | EXPECT_EQ(kOriginHeaderValue, received_cors_header); |
| 10029 | } |
| 10030 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10031 | namespace { |
| 10032 | |
| 10033 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 10034 | public: |
| 10035 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10036 | set_on_complete(base::DoNothing()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10037 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 10038 | void OnCertificateRequested(URLRequest* request, |
| 10039 | SSLCertRequestInfo* cert_request_info) override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10040 | on_certificate_requested_count_++; |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10041 | std::move(on_certificate_requested_).Run(); |
| 10042 | } |
| 10043 | void RunUntilCertificateRequested() { |
| 10044 | base::RunLoop run_loop; |
| 10045 | on_certificate_requested_ = run_loop.QuitClosure(); |
| 10046 | run_loop.Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10047 | } |
| 10048 | int on_certificate_requested_count() { |
| 10049 | return on_certificate_requested_count_; |
| 10050 | } |
| 10051 | private: |
| 10052 | int on_certificate_requested_count_; |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10053 | base::OnceClosure on_certificate_requested_; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10054 | }; |
| 10055 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10056 | class TestSSLPrivateKey : public SSLPrivateKey { |
| 10057 | public: |
| 10058 | explicit TestSSLPrivateKey(scoped_refptr<SSLPrivateKey> key) |
| 10059 | : key_(std::move(key)) {} |
| 10060 | |
| 10061 | void set_fail_signing(bool fail_signing) { fail_signing_ = fail_signing; } |
| 10062 | int sign_count() const { return sign_count_; } |
| 10063 | |
David Benjamin | b65b073 | 2018-11-09 20:33:53 | [diff] [blame] | 10064 | std::string GetProviderName() override { return key_->GetProviderName(); } |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10065 | std::vector<uint16_t> GetAlgorithmPreferences() override { |
| 10066 | return key_->GetAlgorithmPreferences(); |
| 10067 | } |
| 10068 | void Sign(uint16_t algorithm, |
| 10069 | base::span<const uint8_t> input, |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 10070 | SignCallback callback) override { |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10071 | sign_count_++; |
| 10072 | if (fail_signing_) { |
| 10073 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 10074 | FROM_HERE, base::BindOnce(std::move(callback), |
| 10075 | ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED, |
| 10076 | std::vector<uint8_t>())); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10077 | } else { |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 10078 | key_->Sign(algorithm, input, std::move(callback)); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10079 | } |
| 10080 | } |
| 10081 | |
| 10082 | private: |
| 10083 | ~TestSSLPrivateKey() override = default; |
| 10084 | |
| 10085 | scoped_refptr<SSLPrivateKey> key_; |
| 10086 | bool fail_signing_ = false; |
| 10087 | int sign_count_ = 0; |
| 10088 | }; |
| 10089 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10090 | } // namespace |
| 10091 | |
| 10092 | // TODO(davidben): Test the rest of the code. Specifically, |
| 10093 | // - Filtering which certificates to select. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10094 | // - Getting a certificate request in an SSL renegotiation sending the |
| 10095 | // HTTP request. |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10096 | TEST_F(HTTPSRequestTest, ClientAuthNoCertificate) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10097 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10098 | net::SSLServerConfig ssl_config; |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 10099 | ssl_config.client_cert_type = |
| 10100 | SSLServerConfig::ClientCertType::OPTIONAL_CLIENT_CERT; |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10101 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10102 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10103 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10104 | ASSERT_TRUE(test_server.Start()); |
| 10105 | |
| 10106 | SSLClientAuthTestDelegate d; |
| 10107 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10108 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10109 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10110 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10111 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10112 | r->Start(); |
| 10113 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10114 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10115 | d.RunUntilCertificateRequested(); |
| 10116 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10117 | |
| 10118 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10119 | EXPECT_FALSE(d.received_data_before_response()); |
| 10120 | EXPECT_EQ(0, d.bytes_received()); |
| 10121 | |
| 10122 | // Send no certificate. |
| 10123 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 10124 | // all platforms so we can test sending a cert as well. |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 10125 | r->ContinueWithCertificate(nullptr, nullptr); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10126 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10127 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10128 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10129 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10130 | EXPECT_EQ(1, d.response_started_count()); |
| 10131 | EXPECT_FALSE(d.received_data_before_response()); |
| 10132 | EXPECT_NE(0, d.bytes_received()); |
| 10133 | } |
| 10134 | } |
| 10135 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10136 | TEST_F(HTTPSRequestTest, ClientAuth) { |
| 10137 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 10138 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 10139 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 10140 | ASSERT_TRUE(identity); |
| 10141 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 10142 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 10143 | |
| 10144 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10145 | net::SSLServerConfig ssl_config; |
| 10146 | ssl_config.client_cert_type = |
| 10147 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 10148 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 10149 | test_server.AddDefaultHandlers( |
| 10150 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10151 | ASSERT_TRUE(test_server.Start()); |
| 10152 | |
| 10153 | { |
| 10154 | SSLClientAuthTestDelegate d; |
| 10155 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10156 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10157 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10158 | |
| 10159 | r->Start(); |
| 10160 | EXPECT_TRUE(r->is_pending()); |
| 10161 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10162 | d.RunUntilCertificateRequested(); |
| 10163 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10164 | |
| 10165 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10166 | EXPECT_FALSE(d.received_data_before_response()); |
| 10167 | EXPECT_EQ(0, d.bytes_received()); |
| 10168 | |
| 10169 | // Send a certificate. |
| 10170 | r->ContinueWithCertificate(identity->certificate(), private_key); |
| 10171 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10172 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10173 | |
| 10174 | EXPECT_EQ(OK, d.request_status()); |
| 10175 | EXPECT_EQ(1, d.response_started_count()); |
| 10176 | EXPECT_FALSE(d.received_data_before_response()); |
| 10177 | EXPECT_NE(0, d.bytes_received()); |
| 10178 | |
| 10179 | // The private key should have been used. |
| 10180 | EXPECT_EQ(1, private_key->sign_count()); |
| 10181 | } |
| 10182 | |
| 10183 | // Close all connections and clear the session cache to force a new handshake. |
| 10184 | default_context_.http_transaction_factory() |
| 10185 | ->GetSession() |
| 10186 | ->CloseAllConnections(); |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10187 | default_context_.http_transaction_factory() |
| 10188 | ->GetSession() |
| 10189 | ->ClearSSLSessionCache(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10190 | |
| 10191 | // Connecting again should not call OnCertificateRequested. The identity is |
| 10192 | // taken from the client auth cache. |
| 10193 | { |
| 10194 | SSLClientAuthTestDelegate d; |
| 10195 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10196 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10197 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10198 | |
| 10199 | r->Start(); |
| 10200 | EXPECT_TRUE(r->is_pending()); |
| 10201 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10202 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10203 | |
| 10204 | EXPECT_EQ(OK, d.request_status()); |
| 10205 | EXPECT_EQ(0, d.on_certificate_requested_count()); |
| 10206 | EXPECT_FALSE(d.received_data_before_response()); |
| 10207 | EXPECT_EQ(1, d.response_started_count()); |
| 10208 | EXPECT_FALSE(d.received_data_before_response()); |
| 10209 | EXPECT_NE(0, d.bytes_received()); |
| 10210 | |
| 10211 | // The private key should have been used. |
| 10212 | EXPECT_EQ(2, private_key->sign_count()); |
| 10213 | } |
| 10214 | } |
| 10215 | |
| 10216 | // Test that private keys that fail to sign anything get evicted from the cache. |
| 10217 | TEST_F(HTTPSRequestTest, ClientAuthFailSigning) { |
| 10218 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 10219 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 10220 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 10221 | ASSERT_TRUE(identity); |
| 10222 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 10223 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 10224 | private_key->set_fail_signing(true); |
| 10225 | |
| 10226 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10227 | net::SSLServerConfig ssl_config; |
| 10228 | ssl_config.client_cert_type = |
| 10229 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 10230 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 10231 | test_server.AddDefaultHandlers( |
| 10232 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10233 | ASSERT_TRUE(test_server.Start()); |
| 10234 | |
| 10235 | { |
| 10236 | SSLClientAuthTestDelegate d; |
| 10237 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10238 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10239 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10240 | |
| 10241 | r->Start(); |
| 10242 | EXPECT_TRUE(r->is_pending()); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10243 | |
| 10244 | d.RunUntilCertificateRequested(); |
| 10245 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10246 | |
| 10247 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10248 | EXPECT_FALSE(d.received_data_before_response()); |
| 10249 | EXPECT_EQ(0, d.bytes_received()); |
| 10250 | |
| 10251 | // Send a certificate. |
| 10252 | r->ContinueWithCertificate(identity->certificate(), private_key); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10253 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10254 | |
| 10255 | // The private key cannot sign anything, so we report an error. |
| 10256 | EXPECT_EQ(ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED, d.request_status()); |
| 10257 | EXPECT_EQ(1, d.response_started_count()); |
| 10258 | EXPECT_FALSE(d.received_data_before_response()); |
| 10259 | EXPECT_EQ(0, d.bytes_received()); |
| 10260 | |
| 10261 | // The private key should have been used. |
| 10262 | EXPECT_EQ(1, private_key->sign_count()); |
| 10263 | } |
| 10264 | |
| 10265 | // Close all connections and clear the session cache to force a new handshake. |
| 10266 | default_context_.http_transaction_factory() |
| 10267 | ->GetSession() |
| 10268 | ->CloseAllConnections(); |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10269 | default_context_.http_transaction_factory() |
| 10270 | ->GetSession() |
| 10271 | ->ClearSSLSessionCache(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10272 | |
| 10273 | // The bad identity should have been evicted from the cache, so connecting |
| 10274 | // again should call OnCertificateRequested again. |
| 10275 | { |
| 10276 | SSLClientAuthTestDelegate d; |
| 10277 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10278 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10279 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10280 | |
| 10281 | r->Start(); |
| 10282 | EXPECT_TRUE(r->is_pending()); |
| 10283 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10284 | d.RunUntilCertificateRequested(); |
| 10285 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10286 | |
| 10287 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10288 | EXPECT_FALSE(d.received_data_before_response()); |
| 10289 | EXPECT_EQ(0, d.bytes_received()); |
| 10290 | |
| 10291 | // There should have been no additional uses of the private key. |
| 10292 | EXPECT_EQ(1, private_key->sign_count()); |
| 10293 | } |
| 10294 | } |
| 10295 | |
| 10296 | // Test that cached private keys that fail to sign anything trigger a |
| 10297 | // retry. This is so we handle unplugged smartcards |
| 10298 | // gracefully. https://crbug.com/813022. |
| 10299 | TEST_F(HTTPSRequestTest, ClientAuthFailSigningRetry) { |
| 10300 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 10301 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 10302 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 10303 | ASSERT_TRUE(identity); |
| 10304 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 10305 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 10306 | |
| 10307 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10308 | net::SSLServerConfig ssl_config; |
| 10309 | ssl_config.client_cert_type = |
| 10310 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 10311 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 10312 | test_server.AddDefaultHandlers( |
| 10313 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10314 | ASSERT_TRUE(test_server.Start()); |
| 10315 | |
| 10316 | // Connect with a client certificate to put it in the client auth cache. |
| 10317 | { |
| 10318 | SSLClientAuthTestDelegate d; |
| 10319 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10320 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10321 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10322 | |
| 10323 | r->Start(); |
| 10324 | EXPECT_TRUE(r->is_pending()); |
| 10325 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10326 | d.RunUntilCertificateRequested(); |
| 10327 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10328 | |
| 10329 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10330 | EXPECT_FALSE(d.received_data_before_response()); |
| 10331 | EXPECT_EQ(0, d.bytes_received()); |
| 10332 | |
| 10333 | r->ContinueWithCertificate(identity->certificate(), private_key); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10334 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10335 | |
| 10336 | EXPECT_EQ(OK, d.request_status()); |
| 10337 | EXPECT_EQ(1, d.response_started_count()); |
| 10338 | EXPECT_FALSE(d.received_data_before_response()); |
| 10339 | EXPECT_NE(0, d.bytes_received()); |
| 10340 | |
| 10341 | // The private key should have been used. |
| 10342 | EXPECT_EQ(1, private_key->sign_count()); |
| 10343 | } |
| 10344 | |
| 10345 | // Close all connections and clear the session cache to force a new handshake. |
| 10346 | default_context_.http_transaction_factory() |
| 10347 | ->GetSession() |
| 10348 | ->CloseAllConnections(); |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10349 | default_context_.http_transaction_factory() |
| 10350 | ->GetSession() |
| 10351 | ->ClearSSLSessionCache(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10352 | |
| 10353 | // Cause the private key to fail. Connecting again should attempt to use it, |
| 10354 | // notice the failure, and then request a new identity via |
| 10355 | // OnCertificateRequested. |
| 10356 | private_key->set_fail_signing(true); |
| 10357 | |
| 10358 | { |
| 10359 | SSLClientAuthTestDelegate d; |
| 10360 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10361 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10362 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10363 | |
| 10364 | r->Start(); |
| 10365 | EXPECT_TRUE(r->is_pending()); |
| 10366 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10367 | d.RunUntilCertificateRequested(); |
| 10368 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10369 | |
| 10370 | // There was an additional signing call on the private key (the one which |
| 10371 | // failed). |
| 10372 | EXPECT_EQ(2, private_key->sign_count()); |
| 10373 | |
| 10374 | // That caused another OnCertificateRequested call. |
| 10375 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10376 | EXPECT_FALSE(d.received_data_before_response()); |
| 10377 | EXPECT_EQ(0, d.bytes_received()); |
| 10378 | } |
| 10379 | } |
| 10380 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10381 | TEST_F(HTTPSRequestTest, ResumeTest) { |
| 10382 | // Test that we attempt a session resume when making two connections to the |
| 10383 | // same host. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10384 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10385 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10386 | SpawnedTestServer test_server( |
| 10387 | SpawnedTestServer::TYPE_HTTPS, |
| 10388 | ssl_options, |
| 10389 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10390 | ASSERT_TRUE(test_server.Start()); |
| 10391 | |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10392 | default_context_.http_transaction_factory() |
| 10393 | ->GetSession() |
| 10394 | ->ClearSSLSessionCache(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10395 | |
| 10396 | { |
| 10397 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10398 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10399 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10400 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10401 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10402 | r->Start(); |
| 10403 | EXPECT_TRUE(r->is_pending()); |
[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 | |
| 10407 | EXPECT_EQ(1, d.response_started_count()); |
| 10408 | } |
| 10409 | |
| 10410 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 10411 | CloseAllConnections(); |
| 10412 | |
| 10413 | { |
| 10414 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10415 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10416 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10417 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10418 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10419 | r->Start(); |
| 10420 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10421 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10422 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10423 | |
| 10424 | // The response will look like; |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10425 | // lookup uvw (TLS 1.3's compatibility session ID) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10426 | // insert abc |
| 10427 | // lookup abc |
| 10428 | // insert xyz |
| 10429 | // |
| 10430 | // With a newline at the end which makes the split think that there are |
| 10431 | // four lines. |
| 10432 | |
| 10433 | EXPECT_EQ(1, d.response_started_count()); |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 10434 | std::vector<std::string> lines = base::SplitString( |
| 10435 | d.data_received(), "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10436 | ASSERT_EQ(5u, lines.size()) << d.data_received(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10437 | |
| 10438 | std::string session_id; |
| 10439 | |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10440 | for (size_t i = 0; i < 3; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 10441 | std::vector<std::string> parts = base::SplitString( |
| 10442 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10443 | ASSERT_EQ(2u, parts.size()); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10444 | if (i % 2 == 1) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10445 | EXPECT_EQ("insert", parts[0]); |
| 10446 | session_id = parts[1]; |
| 10447 | } else { |
| 10448 | EXPECT_EQ("lookup", parts[0]); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10449 | if (i != 0) |
| 10450 | EXPECT_EQ(session_id, parts[1]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10451 | } |
| 10452 | } |
| 10453 | } |
| 10454 | } |
| 10455 | |
| 10456 | TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { |
| 10457 | // Test that sessions aren't resumed when the value of ssl_session_cache_shard |
| 10458 | // differs. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10459 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10460 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10461 | SpawnedTestServer test_server( |
| 10462 | SpawnedTestServer::TYPE_HTTPS, |
| 10463 | ssl_options, |
| 10464 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10465 | ASSERT_TRUE(test_server.Start()); |
| 10466 | |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10467 | default_context_.http_transaction_factory() |
| 10468 | ->GetSession() |
| 10469 | ->ClearSSLSessionCache(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10470 | |
| 10471 | { |
| 10472 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10473 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 10474 | test_server.GetURL("/"), DEFAULT_PRIORITY, &d, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10475 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10476 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10477 | r->Start(); |
| 10478 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10479 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10480 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10481 | |
| 10482 | EXPECT_EQ(1, d.response_started_count()); |
| 10483 | } |
| 10484 | |
| 10485 | // Now create a new HttpCache with a different ssl_session_cache_shard value. |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10486 | HttpNetworkSession::Context session_context; |
| 10487 | session_context.host_resolver = default_context_.host_resolver(); |
| 10488 | session_context.cert_verifier = default_context_.cert_verifier(); |
| 10489 | session_context.transport_security_state = |
| 10490 | default_context_.transport_security_state(); |
| 10491 | session_context.cert_transparency_verifier = |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10492 | default_context_.cert_transparency_verifier(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10493 | session_context.ct_policy_enforcer = default_context_.ct_policy_enforcer(); |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 10494 | session_context.proxy_resolution_service = default_context_.proxy_resolution_service(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10495 | session_context.ssl_config_service = default_context_.ssl_config_service(); |
| 10496 | session_context.http_auth_handler_factory = |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10497 | default_context_.http_auth_handler_factory(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10498 | session_context.http_server_properties = |
| 10499 | default_context_.http_server_properties(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10500 | |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10501 | HttpNetworkSession network_session(HttpNetworkSession::Params(), |
| 10502 | session_context); |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 10503 | std::unique_ptr<HttpCache> cache( |
| 10504 | new HttpCache(&network_session, HttpCache::DefaultBackend::InMemory(0), |
| 10505 | false /* is_main_cache */)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10506 | |
| 10507 | default_context_.set_http_transaction_factory(cache.get()); |
| 10508 | |
| 10509 | { |
| 10510 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10511 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10512 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10513 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10514 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10515 | r->Start(); |
| 10516 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10517 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10518 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10519 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10520 | EXPECT_EQ(1, d.response_started_count()); |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 10521 | EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, r->ssl_info().handshake_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10522 | } |
| 10523 | } |
| 10524 | |
Daniel McArdle | da3fa94 | 2019-02-15 16:41:21 | [diff] [blame] | 10525 | // Test that sessions started with privacy mode enabled cannot be resumed when |
| 10526 | // it is disabled, and vice versa. |
| 10527 | TEST_F(HTTPSRequestTest, NoSessionResumptionBetweenPrivacyModes) { |
| 10528 | // Start a server. |
| 10529 | SpawnedTestServer test_server( |
| 10530 | SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::SSLOptions(), |
| 10531 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10532 | ASSERT_TRUE(test_server.Start()); |
| 10533 | const auto url = test_server.GetURL("/"); |
| 10534 | |
| 10535 | auto ConnectAndCheckHandshake = [this, url](bool allow_credentials, |
| 10536 | auto expected_handshake) { |
| 10537 | // Construct request and indirectly set the privacy mode. |
| 10538 | TestDelegate d; |
| 10539 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10540 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10541 | r->set_allow_credentials(allow_credentials); |
| 10542 | |
| 10543 | // Start the request and check the SSL handshake type. |
| 10544 | r->Start(); |
| 10545 | EXPECT_TRUE(r->is_pending()); |
| 10546 | d.RunUntilComplete(); |
| 10547 | EXPECT_EQ(1, d.response_started_count()); |
| 10548 | EXPECT_EQ(expected_handshake, r->ssl_info().handshake_type); |
| 10549 | }; |
| 10550 | |
| 10551 | // Exhaustively check all pairs of privacy mode values. Note that we are using |
| 10552 | // allow_credentials to indirectly enable/disable privacy mode. |
| 10553 | const bool kAllowCredentialsValues[] = {false, true}; |
| 10554 | for (const auto allow_creds_1 : kAllowCredentialsValues) { |
| 10555 | for (const auto allow_creds_2 : kAllowCredentialsValues) { |
| 10556 | SCOPED_TRACE(base::StringPrintf("allow_creds_1=%d, allow_creds_2=%d", |
| 10557 | allow_creds_1, allow_creds_2)); |
| 10558 | |
| 10559 | // The session cache starts off empty, so we expect a full handshake. |
| 10560 | ConnectAndCheckHandshake(allow_creds_1, SSLInfo::HANDSHAKE_FULL); |
| 10561 | |
| 10562 | // The second handshake depends on whether we are using the same session |
| 10563 | // cache as the first request. |
| 10564 | ConnectAndCheckHandshake(allow_creds_2, allow_creds_1 == allow_creds_2 |
| 10565 | ? SSLInfo::HANDSHAKE_RESUME |
| 10566 | : SSLInfo::HANDSHAKE_FULL); |
| 10567 | // Flush both session caches. |
| 10568 | auto* network_session = |
| 10569 | default_context_.http_transaction_factory()->GetSession(); |
| 10570 | network_session->ClearSSLSessionCache(); |
| 10571 | } |
| 10572 | } |
| 10573 | } |
| 10574 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 10575 | class HTTPSFallbackTest : public TestWithScopedTaskEnvironment { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10576 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 10577 | HTTPSFallbackTest() : context_(true) { |
Nick Harper | 2243e800 | 2018-09-28 20:33:26 | [diff] [blame] | 10578 | ssl_config_service_ = std::make_unique<TestSSLConfigService>(); |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10579 | context_.set_ssl_config_service(ssl_config_service_.get()); |
| 10580 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 10581 | ~HTTPSFallbackTest() override = default; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10582 | |
| 10583 | protected: |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10584 | TestSSLConfigService* ssl_config_service() { |
| 10585 | return ssl_config_service_.get(); |
| 10586 | } |
| 10587 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10588 | void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) { |
| 10589 | DCHECK(!request_); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10590 | context_.Init(); |
| 10591 | delegate_.set_allow_certificate_errors(true); |
| 10592 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10593 | SpawnedTestServer test_server( |
| 10594 | SpawnedTestServer::TYPE_HTTPS, |
| 10595 | ssl_options, |
| 10596 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10597 | ASSERT_TRUE(test_server.Start()); |
| 10598 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10599 | request_ = context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10600 | &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10601 | request_->Start(); |
| 10602 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10603 | delegate_.RunUntilComplete(); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10604 | } |
| 10605 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10606 | void ExpectConnection(int version) { |
| 10607 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 10608 | EXPECT_NE(0, delegate_.bytes_received()); |
| 10609 | EXPECT_EQ(version, SSLConnectionStatusToVersion( |
| 10610 | request_->ssl_info().connection_status)); |
| 10611 | } |
| 10612 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10613 | void ExpectFailure(int error) { |
| 10614 | EXPECT_EQ(1, delegate_.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 10615 | EXPECT_EQ(error, delegate_.request_status()); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10616 | } |
| 10617 | |
| 10618 | private: |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 10619 | std::unique_ptr<TestSSLConfigService> ssl_config_service_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10620 | TestDelegate delegate_; |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 10621 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10622 | std::unique_ptr<URLRequest> request_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10623 | }; |
| 10624 | |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10625 | // Tests the TLS 1.0 fallback doesn't happen. |
| 10626 | TEST_F(HTTPSFallbackTest, TLSv1NoFallback) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10627 | SpawnedTestServer::SSLOptions ssl_options( |
| 10628 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10629 | ssl_options.tls_intolerant = |
| 10630 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 10631 | |
| 10632 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 10633 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10634 | } |
| 10635 | |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 10636 | // Tests the TLS 1.1 fallback doesn't happen. |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 10637 | TEST_F(HTTPSFallbackTest, TLSv1_1NoFallback) { |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10638 | SpawnedTestServer::SSLOptions ssl_options( |
| 10639 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10640 | ssl_options.tls_intolerant = |
| 10641 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2; |
| 10642 | |
| 10643 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10644 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
| 10645 | } |
| 10646 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10647 | // Tests that TLS 1.3 interference results in a dedicated error code. |
| 10648 | TEST_F(HTTPSFallbackTest, TLSv1_3Interference) { |
| 10649 | SpawnedTestServer::SSLOptions ssl_options( |
| 10650 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10651 | ssl_options.tls_intolerant = |
| 10652 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_3; |
| 10653 | ssl_config_service()->set_max_version(SSL_PROTOCOL_VERSION_TLS1_3); |
| 10654 | |
| 10655 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 10656 | ExpectFailure(ERR_SSL_VERSION_INTERFERENCE); |
| 10657 | } |
| 10658 | |
| 10659 | // Tests that disabling TLS 1.3 leaves TLS 1.3 interference unnoticed. |
| 10660 | TEST_F(HTTPSFallbackTest, TLSv1_3InterferenceDisableVersion) { |
| 10661 | SpawnedTestServer::SSLOptions ssl_options( |
| 10662 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10663 | ssl_options.tls_intolerant = |
| 10664 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_3; |
| 10665 | ssl_config_service()->set_max_version(SSL_PROTOCOL_VERSION_TLS1_2); |
| 10666 | |
| 10667 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 10668 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1_2); |
| 10669 | } |
| 10670 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 10671 | class HTTPSSessionTest : public TestWithScopedTaskEnvironment { |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10672 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 10673 | HTTPSSessionTest() : default_context_(true) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10674 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10675 | |
| 10676 | default_context_.set_network_delegate(&default_network_delegate_); |
| 10677 | default_context_.set_cert_verifier(&cert_verifier_); |
| 10678 | default_context_.Init(); |
| 10679 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 10680 | ~HTTPSSessionTest() override = default; |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10681 | |
| 10682 | protected: |
| 10683 | MockCertVerifier cert_verifier_; |
| 10684 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 10685 | TestURLRequestContext default_context_; |
| 10686 | }; |
| 10687 | |
| 10688 | // Tests that session resumption is not attempted if an invalid certificate |
| 10689 | // is presented. |
| 10690 | TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) { |
| 10691 | SpawnedTestServer::SSLOptions ssl_options; |
| 10692 | ssl_options.record_resume = true; |
| 10693 | SpawnedTestServer test_server( |
| 10694 | SpawnedTestServer::TYPE_HTTPS, |
| 10695 | ssl_options, |
| 10696 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10697 | ASSERT_TRUE(test_server.Start()); |
| 10698 | |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10699 | default_context_.http_transaction_factory() |
| 10700 | ->GetSession() |
| 10701 | ->ClearSSLSessionCache(); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10702 | |
| 10703 | // Simulate the certificate being expired and attempt a connection. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10704 | cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10705 | { |
| 10706 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10707 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 10708 | test_server.GetURL("/"), DEFAULT_PRIORITY, &d, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10709 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10710 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10711 | r->Start(); |
| 10712 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10713 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10714 | d.RunUntilComplete(); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10715 | |
| 10716 | EXPECT_EQ(1, d.response_started_count()); |
| 10717 | } |
| 10718 | |
| 10719 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 10720 | CloseAllConnections(); |
| 10721 | |
| 10722 | // Now change the certificate to be acceptable (so that the response is |
| 10723 | // loaded), and ensure that no session id is presented to the peer. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10724 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10725 | { |
| 10726 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10727 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10728 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10729 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10730 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10731 | r->Start(); |
| 10732 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10733 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10734 | d.RunUntilComplete(); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10735 | |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10736 | EXPECT_EQ(1, d.response_started_count()); |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 10737 | EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, r->ssl_info().handshake_type); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10738 | } |
| 10739 | } |
| 10740 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10741 | // This the fingerprint of the "Testing CA" certificate used by the testserver. |
| 10742 | // See net/data/ssl/certificates/ocsp-test-root.pem. |
David Benjamin | 9cedc3a5 | 2017-08-20 21:30:58 | [diff] [blame] | 10743 | static const SHA256HashValue kOCSPTestCertFingerprint = {{ |
| 10744 | 0x0c, 0xa9, 0x05, 0x11, 0xb0, 0xa2, 0xc0, 0x1d, 0x40, 0x6a, 0x99, |
| 10745 | 0x04, 0x21, 0x36, 0x45, 0x3f, 0x59, 0x12, 0x5c, 0x80, 0x64, 0x2d, |
| 10746 | 0x46, 0x6a, 0x3b, 0x78, 0x9e, 0x84, 0xea, 0x54, 0x0f, 0x8b, |
mattm | 3b4376f1 | 2016-10-03 21:07:15 | [diff] [blame] | 10747 | }}; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10748 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 10749 | // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the |
| 10750 | // testserver. |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 10751 | static const SHA256HashValue kOCSPTestCertSPKI = {{ |
| 10752 | 0x05, 0xa8, 0xf6, 0xfd, 0x8e, 0x10, 0xfe, 0x92, 0x2f, 0x22, 0x75, |
| 10753 | 0x46, 0x40, 0xf4, 0xc4, 0x57, 0x06, 0x0d, 0x95, 0xfd, 0x60, 0x31, |
| 10754 | 0x3b, 0xf3, 0xfc, 0x12, 0x47, 0xe7, 0x66, 0x1a, 0x82, 0xa3, |
| 10755 | }}; |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 10756 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10757 | // This is the policy OID contained in the certificates that testserver |
| 10758 | // generates. |
| 10759 | static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1"; |
| 10760 | |
| 10761 | class HTTPSOCSPTest : public HTTPSRequestTest { |
| 10762 | public: |
| 10763 | HTTPSOCSPTest() |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10764 | : context_(true), |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 10765 | ev_test_policy_( |
| 10766 | new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(), |
| 10767 | kOCSPTestCertFingerprint, |
| 10768 | kOCSPTestCertPolicy)) { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10769 | } |
| 10770 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 10771 | void SetUp() override { |
Ryan Sleevi | 8a9c9c1 | 2018-05-09 02:36:23 | [diff] [blame] | 10772 | context_.SetCTPolicyEnforcer(std::make_unique<DefaultCTPolicyEnforcer>()); |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10773 | context_.Init(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10774 | |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 10775 | context_.cert_verifier()->SetConfig(GetCertVerifierConfig()); |
| 10776 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10777 | scoped_refptr<X509Certificate> root_cert = |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10778 | ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 10779 | CHECK_NE(static_cast<X509Certificate*>(nullptr), root_cert.get()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 10780 | test_root_.reset(new ScopedTestRoot(root_cert.get())); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10781 | |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 10782 | #if defined(OS_ANDROID) || defined(USE_BUILTIN_CERT_VERIFIER) |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 10783 | SetGlobalCertNetFetcherForTesting(net::CreateCertNetFetcher(&context_)); |
| 10784 | #endif |
| 10785 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 10786 | #if defined(USE_NSS_CERTS) |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10787 | SetURLRequestContextForNSSHttpIO(&context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10788 | #endif |
| 10789 | } |
| 10790 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10791 | void DoConnectionWithDelegate( |
| 10792 | const SpawnedTestServer::SSLOptions& ssl_options, |
| 10793 | TestDelegate* delegate, |
| 10794 | SSLInfo* out_ssl_info) { |
| 10795 | // Always overwrite |out_ssl_info|. |
| 10796 | out_ssl_info->Reset(); |
| 10797 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10798 | SpawnedTestServer test_server( |
| 10799 | SpawnedTestServer::TYPE_HTTPS, |
| 10800 | ssl_options, |
| 10801 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10802 | ASSERT_TRUE(test_server.Start()); |
| 10803 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10804 | delegate->set_allow_certificate_errors(true); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10805 | std::unique_ptr<URLRequest> r( |
| 10806 | context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
| 10807 | delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10808 | r->Start(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10809 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10810 | delegate->RunUntilComplete(); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10811 | EXPECT_EQ(1, delegate->response_started_count()); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10812 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10813 | *out_ssl_info = r->ssl_info(); |
| 10814 | } |
| 10815 | |
| 10816 | void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options, |
| 10817 | CertStatus* out_cert_status) { |
| 10818 | // Always overwrite |out_cert_status|. |
| 10819 | *out_cert_status = 0; |
| 10820 | |
| 10821 | TestDelegate d; |
| 10822 | SSLInfo ssl_info; |
| 10823 | ASSERT_NO_FATAL_FAILURE( |
| 10824 | DoConnectionWithDelegate(ssl_options, &d, &ssl_info)); |
| 10825 | |
| 10826 | *out_cert_status = ssl_info.cert_status; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10827 | } |
| 10828 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 10829 | ~HTTPSOCSPTest() override { |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 10830 | #if defined(OS_ANDROID) || defined(USE_BUILTIN_CERT_VERIFIER) |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 10831 | ShutdownGlobalCertNetFetcher(); |
| 10832 | #endif |
| 10833 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 10834 | #if defined(USE_NSS_CERTS) |
Matt Mueller | a17ffaf | 2018-03-28 20:08:18 | [diff] [blame] | 10835 | SetURLRequestContextForNSSHttpIO(nullptr); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10836 | #endif |
| 10837 | } |
| 10838 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10839 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 10840 | // GetCertVerifierConfig() configures the URLRequestContext that will be used |
| 10841 | // for making connections to the testserver. This can be overridden in test |
| 10842 | // subclasses for different behaviour. |
| 10843 | virtual CertVerifier::Config GetCertVerifierConfig() { |
| 10844 | CertVerifier::Config config; |
| 10845 | config.enable_rev_checking = true; |
| 10846 | return config; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10847 | } |
| 10848 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10849 | std::unique_ptr<ScopedTestRoot> test_root_; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 10850 | std::unique_ptr<TestSSLConfigService> ssl_config_service_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10851 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10852 | std::unique_ptr<ScopedTestEVPolicy> ev_test_policy_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10853 | }; |
| 10854 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10855 | static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() { |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 10856 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10857 | // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't |
| 10858 | // have that ability on other platforms. |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10859 | // TODO(eroman): Should this also be the return value for |
| 10860 | // CertVerifyProcBuiltin? |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10861 | return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 10862 | #else |
| 10863 | return 0; |
| 10864 | #endif |
| 10865 | } |
| 10866 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10867 | // SystemSupportsHardFailRevocationChecking returns true iff the current |
| 10868 | // operating system supports revocation checking and can distinguish between |
| 10869 | // situations where a given certificate lacks any revocation information (eg: |
| 10870 | // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when |
| 10871 | // revocation information cannot be obtained (eg: the CRL was unreachable). |
| 10872 | // If it does not, then tests which rely on 'hard fail' behaviour should be |
| 10873 | // skipped. |
| 10874 | static bool SystemSupportsHardFailRevocationChecking() { |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10875 | #if defined(OS_WIN) || defined(USE_NSS_CERTS) || \ |
| 10876 | defined(USE_BUILTIN_CERT_VERIFIER) |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10877 | return true; |
| 10878 | #else |
| 10879 | return false; |
| 10880 | #endif |
| 10881 | } |
| 10882 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10883 | // SystemUsesChromiumEVMetadata returns true iff the current operating system |
| 10884 | // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then |
| 10885 | // several tests are effected because our testing EV certificate won't be |
| 10886 | // recognised as EV. |
| 10887 | static bool SystemUsesChromiumEVMetadata() { |
Eric Roman | 5f8d9d2 | 2017-10-17 02:32:52 | [diff] [blame] | 10888 | #if defined(PLATFORM_USES_CHROMIUM_EV_METADATA) |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 10889 | return true; |
Eric Roman | 5f8d9d2 | 2017-10-17 02:32:52 | [diff] [blame] | 10890 | #else |
| 10891 | return false; |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 10892 | #endif |
| 10893 | } |
| 10894 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 10895 | // Returns the expected CertStatus for tests that expect an online revocation |
| 10896 | // check failure as a result of checking a test EV cert, which will not |
| 10897 | // actually trigger an online revocation check on some platforms. |
| 10898 | static CertStatus ExpectedCertStatusForFailedOnlineEVRevocationCheck() { |
| 10899 | if (SystemUsesChromiumEVMetadata()) { |
| 10900 | return ExpectedCertStatusForFailedOnlineRevocationCheck(); |
| 10901 | } else { |
| 10902 | // If SystemUsesChromiumEVMetadata is false, revocation checking will not |
| 10903 | // be enabled, and thus there will not be a revocation check to fail. |
| 10904 | return 0u; |
| 10905 | } |
| 10906 | } |
| 10907 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 10908 | static bool SystemSupportsOCSP() { |
Sergey Ulanov | ee7c8db | 2017-12-08 00:18:39 | [diff] [blame] | 10909 | #if defined(OS_ANDROID) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10910 | // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. |
| 10911 | return false; |
| 10912 | #else |
| 10913 | return true; |
| 10914 | #endif |
| 10915 | } |
| 10916 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10917 | static bool SystemSupportsOCSPStapling() { |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10918 | #if defined(USE_NSS_CERTS) || defined(OS_WIN) || \ |
| 10919 | defined(USE_BUILTIN_CERT_VERIFIER) |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10920 | return true; |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10921 | #else |
| 10922 | return false; |
| 10923 | #endif |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10924 | } |
| 10925 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10926 | TEST_F(HTTPSOCSPTest, Valid) { |
| 10927 | if (!SystemSupportsOCSP()) { |
| 10928 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10929 | return; |
| 10930 | } |
| 10931 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10932 | SpawnedTestServer::SSLOptions ssl_options( |
| 10933 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10934 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10935 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 10936 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 10937 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10938 | |
| 10939 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10940 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10941 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 10942 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10943 | |
| 10944 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10945 | } |
| 10946 | |
| 10947 | TEST_F(HTTPSOCSPTest, Revoked) { |
| 10948 | if (!SystemSupportsOCSP()) { |
| 10949 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10950 | return; |
| 10951 | } |
| 10952 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10953 | SpawnedTestServer::SSLOptions ssl_options( |
| 10954 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10955 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10956 | |
| 10957 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 10958 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10959 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10960 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10961 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 10962 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10963 | } |
| 10964 | |
| 10965 | TEST_F(HTTPSOCSPTest, Invalid) { |
| 10966 | if (!SystemSupportsOCSP()) { |
| 10967 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10968 | return; |
| 10969 | } |
| 10970 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10971 | SpawnedTestServer::SSLOptions ssl_options( |
| 10972 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10973 | ssl_options.ocsp_status = |
| 10974 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10975 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 10976 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 10977 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10978 | |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10979 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 10980 | // TODO(649017): This test uses soft-fail revocation checking, but returns an |
| 10981 | // invalid OCSP response (can't parse). CertVerifyProcBuiltin currently |
| 10982 | // doesn't consider this a candidate for soft-fail (only considers |
| 10983 | // network-level failures as skippable). |
| 10984 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 10985 | cert_status & CERT_STATUS_UNABLE_TO_CHECK_REVOCATION); |
| 10986 | #else |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10987 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10988 | cert_status & CERT_STATUS_ALL_ERRORS); |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10989 | #endif |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10990 | |
| 10991 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 10992 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 10993 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10994 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10995 | |
Matt Mueller | 5339d86 | 2018-05-02 18:53:57 | [diff] [blame] | 10996 | TEST_F(HTTPSOCSPTest, IntermediateValid) { |
| 10997 | if (!SystemSupportsOCSP()) { |
| 10998 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10999 | return; |
| 11000 | } |
| 11001 | |
| 11002 | SpawnedTestServer::SSLOptions ssl_options( |
| 11003 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11004 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11005 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11006 | |
| 11007 | CertStatus cert_status; |
| 11008 | DoConnection(ssl_options, &cert_status); |
| 11009 | |
| 11010 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11011 | |
| 11012 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11013 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11014 | |
| 11015 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11016 | } |
| 11017 | |
| 11018 | TEST_F(HTTPSOCSPTest, IntermediateResponseOldButStillValid) { |
| 11019 | if (!SystemSupportsOCSP()) { |
| 11020 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11021 | return; |
| 11022 | } |
| 11023 | |
| 11024 | SpawnedTestServer::SSLOptions ssl_options( |
| 11025 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11026 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11027 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11028 | // Use an OCSP response for the intermediate that would be too old for a leaf |
| 11029 | // cert, but is still valid for an intermediate. |
| 11030 | ssl_options.ocsp_intermediate_date = |
| 11031 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG; |
| 11032 | |
| 11033 | CertStatus cert_status; |
| 11034 | DoConnection(ssl_options, &cert_status); |
| 11035 | |
| 11036 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11037 | |
| 11038 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11039 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11040 | |
| 11041 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11042 | } |
| 11043 | |
| 11044 | TEST_F(HTTPSOCSPTest, IntermediateResponseTooOld) { |
| 11045 | if (!SystemSupportsOCSP()) { |
| 11046 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11047 | return; |
| 11048 | } |
| 11049 | |
| 11050 | SpawnedTestServer::SSLOptions ssl_options( |
| 11051 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11052 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11053 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11054 | ssl_options.ocsp_intermediate_date = |
| 11055 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONGER; |
| 11056 | |
| 11057 | CertStatus cert_status; |
| 11058 | DoConnection(ssl_options, &cert_status); |
| 11059 | |
| 11060 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 11061 | // The builtin verifier enforces the baseline requirements for max age of an |
| 11062 | // intermediate's OCSP response. |
| 11063 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 11064 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11065 | EXPECT_EQ(0u, cert_status & CERT_STATUS_IS_EV); |
| 11066 | #else |
| 11067 | // The platform verifiers are more lenient. |
| 11068 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11069 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11070 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11071 | #endif |
| 11072 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11073 | } |
| 11074 | |
| 11075 | TEST_F(HTTPSOCSPTest, IntermediateRevoked) { |
| 11076 | if (!SystemSupportsOCSP()) { |
| 11077 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11078 | return; |
| 11079 | } |
| 11080 | |
| 11081 | SpawnedTestServer::SSLOptions ssl_options( |
| 11082 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11083 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11084 | ssl_options.ocsp_intermediate_status = |
| 11085 | SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 11086 | |
| 11087 | CertStatus cert_status; |
| 11088 | DoConnection(ssl_options, &cert_status); |
| 11089 | |
| 11090 | #if defined(OS_WIN) |
| 11091 | // TODO(mattm): why does CertVerifyProcWin accept this? |
| 11092 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11093 | #else |
| 11094 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11095 | #endif |
| 11096 | EXPECT_EQ(0u, cert_status & CERT_STATUS_IS_EV); |
| 11097 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11098 | } |
| 11099 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11100 | TEST_F(HTTPSOCSPTest, ValidStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11101 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11102 | LOG(WARNING) |
| 11103 | << "Skipping test because system doesn't support OCSP stapling"; |
| 11104 | return; |
| 11105 | } |
| 11106 | |
| 11107 | SpawnedTestServer::SSLOptions ssl_options( |
| 11108 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11109 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11110 | ssl_options.staple_ocsp_response = true; |
| 11111 | ssl_options.ocsp_server_unavailable = true; |
| 11112 | |
| 11113 | CertStatus cert_status; |
| 11114 | DoConnection(ssl_options, &cert_status); |
| 11115 | |
| 11116 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11117 | |
| 11118 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11119 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11120 | |
| 11121 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11122 | } |
| 11123 | |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 11124 | // Disabled on NSS ports. See https://crbug.com/431716. |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 11125 | #if defined(USE_NSS_CERTS) |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 11126 | #define MAYBE_RevokedStapled DISABLED_RevokedStapled |
| 11127 | #else |
| 11128 | #define MAYBE_RevokedStapled RevokedStapled |
| 11129 | #endif |
| 11130 | TEST_F(HTTPSOCSPTest, MAYBE_RevokedStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11131 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11132 | LOG(WARNING) |
| 11133 | << "Skipping test because system doesn't support OCSP stapling"; |
| 11134 | return; |
| 11135 | } |
| 11136 | |
| 11137 | SpawnedTestServer::SSLOptions ssl_options( |
| 11138 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11139 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 11140 | ssl_options.staple_ocsp_response = true; |
| 11141 | ssl_options.ocsp_server_unavailable = true; |
| 11142 | |
| 11143 | CertStatus cert_status; |
| 11144 | DoConnection(ssl_options, &cert_status); |
| 11145 | |
| 11146 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11147 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11148 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11149 | } |
| 11150 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11151 | static const struct OCSPVerifyTestData { |
| 11152 | std::vector<SpawnedTestServer::SSLOptions::OCSPSingleResponse> ocsp_responses; |
| 11153 | SpawnedTestServer::SSLOptions::OCSPProduced ocsp_produced; |
| 11154 | OCSPVerifyResult::ResponseStatus response_status; |
| 11155 | bool has_revocation_status; |
| 11156 | OCSPRevocationStatus cert_status; |
| 11157 | } kOCSPVerifyData[] = { |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11158 | // 0 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11159 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11160 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11161 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11162 | OCSPVerifyResult::PROVIDED, |
| 11163 | true, |
| 11164 | OCSPRevocationStatus::GOOD}, |
| 11165 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11166 | // 1 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11167 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11168 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 11169 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11170 | OCSPVerifyResult::INVALID_DATE, |
| 11171 | false, |
| 11172 | OCSPRevocationStatus::UNKNOWN}, |
| 11173 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11174 | // 2 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11175 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11176 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11177 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11178 | OCSPVerifyResult::INVALID_DATE, |
| 11179 | false, |
| 11180 | OCSPRevocationStatus::UNKNOWN}, |
| 11181 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11182 | // 3 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11183 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11184 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11185 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11186 | OCSPVerifyResult::INVALID_DATE, |
| 11187 | false, |
| 11188 | OCSPRevocationStatus::UNKNOWN}, |
| 11189 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11190 | // 4 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11191 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11192 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11193 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11194 | OCSPVerifyResult::INVALID_DATE, |
| 11195 | false, |
| 11196 | OCSPRevocationStatus::UNKNOWN}, |
| 11197 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11198 | // 5 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11199 | {{{SpawnedTestServer::SSLOptions::OCSP_TRY_LATER, |
| 11200 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11201 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11202 | OCSPVerifyResult::ERROR_RESPONSE, |
| 11203 | false, |
| 11204 | OCSPRevocationStatus::UNKNOWN}, |
| 11205 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11206 | // 6 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11207 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE, |
| 11208 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11209 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11210 | OCSPVerifyResult::PARSE_RESPONSE_ERROR, |
| 11211 | false, |
| 11212 | OCSPRevocationStatus::UNKNOWN}, |
| 11213 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11214 | // 7 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11215 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE_DATA, |
| 11216 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11217 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11218 | OCSPVerifyResult::PARSE_RESPONSE_DATA_ERROR, |
| 11219 | false, |
| 11220 | OCSPRevocationStatus::UNKNOWN}, |
| 11221 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11222 | // 8 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11223 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11224 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11225 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11226 | OCSPVerifyResult::INVALID_DATE, |
| 11227 | false, |
| 11228 | OCSPRevocationStatus::UNKNOWN}, |
| 11229 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11230 | // 9 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11231 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11232 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11233 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11234 | OCSPVerifyResult::PROVIDED, |
| 11235 | true, |
| 11236 | OCSPRevocationStatus::UNKNOWN}, |
| 11237 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11238 | // 10 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11239 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11240 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 11241 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11242 | OCSPVerifyResult::INVALID_DATE, |
| 11243 | false, |
| 11244 | OCSPRevocationStatus::UNKNOWN}, |
| 11245 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11246 | // 11 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11247 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11248 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11249 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11250 | OCSPVerifyResult::INVALID_DATE, |
| 11251 | false, |
| 11252 | OCSPRevocationStatus::UNKNOWN}, |
| 11253 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11254 | // 12 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11255 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11256 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11257 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_BEFORE_CERT, |
| 11258 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11259 | false, |
| 11260 | OCSPRevocationStatus::UNKNOWN}, |
| 11261 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11262 | // 13 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11263 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11264 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11265 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 11266 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11267 | false, |
| 11268 | OCSPRevocationStatus::UNKNOWN}, |
| 11269 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11270 | // 14 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11271 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11272 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11273 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 11274 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11275 | false, |
| 11276 | OCSPRevocationStatus::UNKNOWN}, |
| 11277 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11278 | // 15 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11279 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11280 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11281 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11282 | OCSPVerifyResult::PROVIDED, |
| 11283 | true, |
| 11284 | OCSPRevocationStatus::GOOD}, |
| 11285 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11286 | // 16 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11287 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11288 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 11289 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11290 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11291 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11292 | OCSPVerifyResult::PROVIDED, |
| 11293 | true, |
| 11294 | OCSPRevocationStatus::GOOD}, |
| 11295 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11296 | // 17 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11297 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11298 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 11299 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11300 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11301 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11302 | OCSPVerifyResult::PROVIDED, |
| 11303 | true, |
| 11304 | OCSPRevocationStatus::GOOD}, |
| 11305 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11306 | // 18 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11307 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11308 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 11309 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11310 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11311 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11312 | OCSPVerifyResult::PROVIDED, |
| 11313 | true, |
| 11314 | OCSPRevocationStatus::GOOD}, |
| 11315 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11316 | // 19 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11317 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11318 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 11319 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11320 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 11321 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11322 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11323 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11324 | OCSPVerifyResult::INVALID_DATE, |
| 11325 | false, |
| 11326 | OCSPRevocationStatus::UNKNOWN}, |
| 11327 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11328 | // 20 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11329 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11330 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11331 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11332 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11333 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11334 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11335 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11336 | OCSPVerifyResult::PROVIDED, |
| 11337 | true, |
| 11338 | OCSPRevocationStatus::REVOKED}, |
| 11339 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11340 | // 21 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11341 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11342 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11343 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11344 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11345 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11346 | OCSPVerifyResult::PROVIDED, |
| 11347 | true, |
| 11348 | OCSPRevocationStatus::UNKNOWN}, |
| 11349 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11350 | // 22 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11351 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11352 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11353 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11354 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 11355 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11356 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11357 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11358 | OCSPVerifyResult::PROVIDED, |
| 11359 | true, |
| 11360 | OCSPRevocationStatus::UNKNOWN}, |
| 11361 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11362 | // 23 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11363 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 11364 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11365 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11366 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 11367 | false, |
| 11368 | OCSPRevocationStatus::UNKNOWN}, |
| 11369 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11370 | // 24 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11371 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 11372 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11373 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11374 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 11375 | false, |
| 11376 | OCSPRevocationStatus::UNKNOWN}, |
| 11377 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11378 | // These tests fail when using NSS for certificate verification, as NSS fails |
| 11379 | // and doesn't return the partial path. As a result the OCSP checks being done |
| 11380 | // at the CertVerifyProc layer cannot access the issuer certificate. |
| 11381 | #if !defined(USE_NSS_CERTS) |
| 11382 | // 25 |
| 11383 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11384 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11385 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11386 | OCSPVerifyResult::PROVIDED, |
| 11387 | true, |
| 11388 | OCSPRevocationStatus::REVOKED}, |
| 11389 | |
| 11390 | // 26 |
| 11391 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11392 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 11393 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11394 | OCSPVerifyResult::INVALID_DATE, |
| 11395 | false, |
| 11396 | OCSPRevocationStatus::UNKNOWN}, |
| 11397 | |
| 11398 | // 27 |
| 11399 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11400 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11401 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11402 | OCSPVerifyResult::INVALID_DATE, |
| 11403 | false, |
| 11404 | OCSPRevocationStatus::UNKNOWN}, |
| 11405 | #endif |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11406 | }; |
| 11407 | |
| 11408 | class HTTPSOCSPVerifyTest |
| 11409 | : public HTTPSOCSPTest, |
| 11410 | public testing::WithParamInterface<OCSPVerifyTestData> {}; |
| 11411 | |
| 11412 | TEST_P(HTTPSOCSPVerifyTest, VerifyResult) { |
| 11413 | SpawnedTestServer::SSLOptions ssl_options( |
| 11414 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11415 | OCSPVerifyTestData test = GetParam(); |
| 11416 | |
| 11417 | ssl_options.ocsp_responses = test.ocsp_responses; |
| 11418 | ssl_options.ocsp_produced = test.ocsp_produced; |
| 11419 | ssl_options.staple_ocsp_response = true; |
| 11420 | |
| 11421 | SSLInfo ssl_info; |
| 11422 | OCSPErrorTestDelegate delegate; |
| 11423 | ASSERT_NO_FATAL_FAILURE( |
| 11424 | DoConnectionWithDelegate(ssl_options, &delegate, &ssl_info)); |
| 11425 | |
| 11426 | // The SSLInfo must be extracted from |delegate| on error, due to how |
| 11427 | // URLRequest caches certificate errors. |
| 11428 | if (delegate.have_certificate_errors()) { |
| 11429 | ASSERT_TRUE(delegate.on_ssl_certificate_error_called()); |
| 11430 | ssl_info = delegate.ssl_info(); |
| 11431 | } |
| 11432 | |
| 11433 | EXPECT_EQ(test.response_status, ssl_info.ocsp_result.response_status); |
| 11434 | |
| 11435 | if (test.has_revocation_status) |
| 11436 | EXPECT_EQ(test.cert_status, ssl_info.ocsp_result.revocation_status); |
| 11437 | } |
| 11438 | |
Victor Costan | 8fb98f6f | 2019-02-01 17:08:29 | [diff] [blame] | 11439 | INSTANTIATE_TEST_SUITE_P(OCSPVerify, |
| 11440 | HTTPSOCSPVerifyTest, |
| 11441 | testing::ValuesIn(kOCSPVerifyData)); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11442 | |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11443 | class HTTPSAIATest : public HTTPSOCSPTest { |
| 11444 | public: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11445 | CertVerifier::Config GetCertVerifierConfig() override { |
| 11446 | CertVerifier::Config config; |
| 11447 | return config; |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11448 | } |
| 11449 | }; |
| 11450 | |
| 11451 | TEST_F(HTTPSAIATest, AIAFetching) { |
| 11452 | SpawnedTestServer::SSLOptions ssl_options( |
| 11453 | SpawnedTestServer::SSLOptions::CERT_AUTO_AIA_INTERMEDIATE); |
| 11454 | SpawnedTestServer test_server( |
| 11455 | SpawnedTestServer::TYPE_HTTPS, ssl_options, |
| 11456 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 11457 | ASSERT_TRUE(test_server.Start()); |
| 11458 | |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11459 | // Unmark the certificate's OID as EV, which will disable revocation |
| 11460 | // checking. |
| 11461 | ev_test_policy_.reset(); |
| 11462 | |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11463 | TestDelegate d; |
| 11464 | d.set_allow_certificate_errors(true); |
| 11465 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11466 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 11467 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11468 | |
| 11469 | r->Start(); |
| 11470 | EXPECT_TRUE(r->is_pending()); |
| 11471 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11472 | d.RunUntilComplete(); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11473 | |
| 11474 | EXPECT_EQ(1, d.response_started_count()); |
| 11475 | |
| 11476 | CertStatus cert_status = r->ssl_info().cert_status; |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 11477 | EXPECT_EQ(OK, d.request_status()); |
| 11478 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11479 | ASSERT_TRUE(r->ssl_info().cert); |
| 11480 | EXPECT_EQ(2u, r->ssl_info().cert->intermediate_buffers().size()); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11481 | ASSERT_TRUE(r->ssl_info().unverified_cert); |
Matt Mueller | a419327 | 2017-12-07 00:23:34 | [diff] [blame] | 11482 | EXPECT_EQ(0u, r->ssl_info().unverified_cert->intermediate_buffers().size()); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11483 | } |
| 11484 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11485 | class HTTPSHardFailTest : public HTTPSOCSPTest { |
| 11486 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11487 | CertVerifier::Config GetCertVerifierConfig() override { |
| 11488 | CertVerifier::Config config; |
| 11489 | config.require_rev_checking_local_anchors = true; |
| 11490 | return config; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11491 | } |
| 11492 | }; |
| 11493 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11494 | TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) { |
| 11495 | if (!SystemSupportsOCSP()) { |
| 11496 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11497 | return; |
| 11498 | } |
| 11499 | |
| 11500 | if (!SystemSupportsHardFailRevocationChecking()) { |
| 11501 | LOG(WARNING) << "Skipping test because system doesn't support hard fail " |
| 11502 | << "revocation checking"; |
| 11503 | return; |
| 11504 | } |
| 11505 | |
| 11506 | SpawnedTestServer::SSLOptions ssl_options( |
| 11507 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11508 | ssl_options.ocsp_status = |
| 11509 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11510 | |
| 11511 | CertStatus cert_status; |
| 11512 | DoConnection(ssl_options, &cert_status); |
| 11513 | |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11514 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 11515 | // TODO(crbug.com/649017): Should we consider invalid response as |
| 11516 | // affirmatively revoked? |
| 11517 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 11518 | cert_status & CERT_STATUS_UNABLE_TO_CHECK_REVOCATION); |
| 11519 | #else |
| 11520 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_REVOKED); |
| 11521 | #endif |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11522 | |
| 11523 | // Without a positive OCSP response, we shouldn't show the EV status. |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11524 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11525 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11526 | } |
| 11527 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11528 | class HTTPSEVCRLSetTest : public HTTPSOCSPTest { |
| 11529 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11530 | CertVerifier::Config GetCertVerifierConfig() override { |
| 11531 | CertVerifier::Config config; |
| 11532 | return config; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11533 | } |
| 11534 | }; |
| 11535 | |
| 11536 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) { |
| 11537 | if (!SystemSupportsOCSP()) { |
| 11538 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11539 | return; |
| 11540 | } |
| 11541 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11542 | SpawnedTestServer::SSLOptions ssl_options( |
| 11543 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11544 | ssl_options.ocsp_status = |
| 11545 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11546 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11547 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11548 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11549 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11550 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11551 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11552 | |
| 11553 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11554 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11555 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11556 | } |
| 11557 | |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11558 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) { |
| 11559 | if (!SystemSupportsOCSP()) { |
| 11560 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11561 | return; |
| 11562 | } |
| 11563 | |
| 11564 | SpawnedTestServer::SSLOptions ssl_options( |
| 11565 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11566 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11567 | |
| 11568 | CertStatus cert_status; |
| 11569 | DoConnection(ssl_options, &cert_status); |
| 11570 | |
mattm | 1a282f5 | 2016-11-10 21:49:42 | [diff] [blame] | 11571 | // Currently only works for Windows and OS X. When using NSS, it's not |
| 11572 | // possible to determine whether the check failed because of actual |
| 11573 | // revocation or because there was an OCSP failure. |
| 11574 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11575 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11576 | #else |
| 11577 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11578 | #endif |
| 11579 | |
| 11580 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11581 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11582 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 11583 | } |
| 11584 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11585 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) { |
| 11586 | if (!SystemSupportsOCSP()) { |
| 11587 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11588 | return; |
| 11589 | } |
| 11590 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11591 | SpawnedTestServer::SSLOptions ssl_options( |
| 11592 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11593 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11594 | |
| 11595 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11596 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11597 | |
| 11598 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11599 | |
| 11600 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11601 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11602 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11603 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11604 | } |
| 11605 | |
| 11606 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) { |
| 11607 | if (!SystemSupportsOCSP()) { |
| 11608 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11609 | return; |
| 11610 | } |
| 11611 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11612 | SpawnedTestServer::SSLOptions ssl_options( |
| 11613 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11614 | ssl_options.ocsp_status = |
| 11615 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11616 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11617 | cert_verifier_config.crl_set = CRLSet::ExpiredCRLSetForTesting(); |
| 11618 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11619 | |
| 11620 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11621 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11622 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11623 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11624 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11625 | |
| 11626 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11627 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11628 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11629 | } |
| 11630 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11631 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) { |
| 11632 | if (!SystemSupportsOCSP()) { |
| 11633 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11634 | return; |
| 11635 | } |
| 11636 | |
| 11637 | SpawnedTestServer::SSLOptions ssl_options( |
| 11638 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11639 | ssl_options.ocsp_status = |
| 11640 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11641 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11642 | cert_verifier_config.crl_set = |
| 11643 | CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "", "", {}); |
| 11644 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11645 | |
| 11646 | CertStatus cert_status; |
| 11647 | DoConnection(ssl_options, &cert_status); |
| 11648 | |
| 11649 | // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a |
| 11650 | // revocation check for EV. |
| 11651 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11652 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11653 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11654 | EXPECT_FALSE( |
| 11655 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 11656 | } |
| 11657 | |
| 11658 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) { |
| 11659 | if (!SystemSupportsOCSP()) { |
| 11660 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11661 | return; |
| 11662 | } |
| 11663 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11664 | SpawnedTestServer::SSLOptions ssl_options( |
| 11665 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11666 | ssl_options.ocsp_status = |
| 11667 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11668 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11669 | cert_verifier_config.crl_set = CRLSet::EmptyCRLSetForTesting(); |
| 11670 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11671 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11672 | CertStatus cert_status = 0; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11673 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11674 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11675 | // Even with a fresh CRLSet, we should still do online revocation checks when |
| 11676 | // the certificate chain isn't covered by the CRLSet, which it isn't in this |
| 11677 | // test. |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11678 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11679 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11680 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11681 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11682 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11683 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11684 | } |
| 11685 | |
| 11686 | class HTTPSCRLSetTest : public HTTPSOCSPTest { |
| 11687 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11688 | CertVerifier::Config GetCertVerifierConfig() override { |
| 11689 | CertVerifier::Config config; |
| 11690 | return config; |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11691 | } |
| 11692 | |
| 11693 | void SetUp() override { |
| 11694 | HTTPSOCSPTest::SetUp(); |
| 11695 | |
| 11696 | // Unmark the certificate's OID as EV, which should disable revocation |
| 11697 | // checking (as per the user preference). |
| 11698 | ev_test_policy_.reset(); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11699 | } |
| 11700 | }; |
| 11701 | |
| 11702 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11703 | SpawnedTestServer::SSLOptions ssl_options( |
| 11704 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11705 | ssl_options.ocsp_status = |
| 11706 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11707 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11708 | cert_verifier_config.crl_set = CRLSet::ExpiredCRLSetForTesting(); |
| 11709 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11710 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11711 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11712 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11713 | |
| 11714 | // If we're not trying EV verification then, even if the CRLSet has expired, |
| 11715 | // we don't fall back to online revocation checks. |
| 11716 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11717 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11718 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11719 | } |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11720 | |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11721 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSetAndRevoked) { |
| 11722 | // Test that when online revocation checking is disabled, and the leaf |
| 11723 | // certificate is not EV, that no revocation checking actually happens. |
| 11724 | if (!SystemSupportsOCSP()) { |
| 11725 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11726 | return; |
| 11727 | } |
| 11728 | |
| 11729 | SpawnedTestServer::SSLOptions ssl_options( |
| 11730 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11731 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11732 | |
| 11733 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11734 | cert_verifier_config.crl_set = CRLSet::ExpiredCRLSetForTesting(); |
| 11735 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11736 | |
| 11737 | CertStatus cert_status; |
| 11738 | DoConnection(ssl_options, &cert_status); |
| 11739 | |
| 11740 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11741 | |
| 11742 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11743 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11744 | } |
| 11745 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11746 | TEST_F(HTTPSCRLSetTest, CRLSetRevoked) { |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 11747 | #if defined(OS_ANDROID) |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11748 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 11749 | return; |
| 11750 | #endif |
| 11751 | |
| 11752 | SpawnedTestServer::SSLOptions ssl_options( |
| 11753 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11754 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11755 | ssl_options.cert_serial = 10; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11756 | |
| 11757 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11758 | cert_verifier_config.crl_set = |
| 11759 | CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "\x0a", "", {}); |
| 11760 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11761 | |
| 11762 | CertStatus cert_status = 0; |
| 11763 | DoConnection(ssl_options, &cert_status); |
| 11764 | |
| 11765 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 11766 | // reflected without online revocation checking. |
| 11767 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11768 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11769 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11770 | } |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 11771 | |
| 11772 | TEST_F(HTTPSCRLSetTest, CRLSetRevokedBySubject) { |
| 11773 | #if defined(OS_ANDROID) |
| 11774 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 11775 | return; |
| 11776 | #endif |
| 11777 | |
| 11778 | SpawnedTestServer::SSLOptions ssl_options( |
| 11779 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11780 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11781 | static const char kCommonName[] = "Test CN"; |
| 11782 | ssl_options.cert_common_name = kCommonName; |
| 11783 | |
| 11784 | { |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11785 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11786 | cert_verifier_config.crl_set = |
| 11787 | CRLSet::ForTesting(false, nullptr, "", kCommonName, {}); |
| 11788 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 11789 | |
| 11790 | CertStatus cert_status = 0; |
| 11791 | DoConnection(ssl_options, &cert_status); |
| 11792 | |
| 11793 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 11794 | // reflected without online revocation checking. |
| 11795 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11796 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11797 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 11798 | } |
| 11799 | |
| 11800 | const uint8_t kTestServerSPKISHA256[32] = { |
| 11801 | 0xb3, 0x91, 0xac, 0x73, 0x32, 0x54, 0x7f, 0x7b, 0x8a, 0x62, 0x77, |
| 11802 | 0x73, 0x1d, 0x45, 0x7b, 0x23, 0x46, 0x69, 0xef, 0x6f, 0x05, 0x3d, |
| 11803 | 0x07, 0x22, 0x15, 0x18, 0xd6, 0x10, 0x8b, 0xa1, 0x49, 0x33, |
| 11804 | }; |
| 11805 | const std::string spki_hash( |
| 11806 | reinterpret_cast<const char*>(kTestServerSPKISHA256), |
| 11807 | sizeof(kTestServerSPKISHA256)); |
| 11808 | |
| 11809 | { |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11810 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11811 | cert_verifier_config.crl_set = |
| 11812 | CRLSet::ForTesting(false, nullptr, "", kCommonName, {spki_hash}); |
| 11813 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 11814 | |
| 11815 | CertStatus cert_status = 0; |
| 11816 | DoConnection(ssl_options, &cert_status); |
| 11817 | |
| 11818 | // When the correct SPKI hash is specified, the connection should succeed |
| 11819 | // even though the subject is listed in the CRLSet. |
| 11820 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11821 | } |
| 11822 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11823 | #endif // !defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11824 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 11825 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 11826 | !defined(OS_FUCHSIA) |
| 11827 | // FTP uses a second TCP connection with the port number allocated dynamically |
| 11828 | // on the server side, so it would be hard to make RemoteTestServer proxy FTP |
| 11829 | // connections reliably. FTP tests are disabled on platforms that use |
| 11830 | // RemoteTestServer. See http://crbug.com/495220 |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 11831 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11832 | public: |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 11833 | URLRequestTestFTP() |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 11834 | : ftp_test_server_(SpawnedTestServer::TYPE_FTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11835 | base::FilePath(kTestFilePath)) { |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11836 | // Can't use |default_context_|'s HostResolver to set up the |
| 11837 | // FTPTransactionFactory because it hasn't been created yet. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11838 | default_context().set_host_resolver(&host_resolver_); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11839 | } |
| 11840 | |
| 11841 | // URLRequestTest interface: |
| 11842 | void SetUpFactory() override { |
| 11843 | // Add FTP support to the default URLRequestContext. |
| 11844 | job_factory_impl_->SetProtocolHandler( |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 11845 | "ftp", FtpProtocolHandler::Create(&host_resolver_)); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11846 | } |
| 11847 | |
| 11848 | std::string GetTestFileContents() { |
| 11849 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 11850 | EXPECT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &path)); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11851 | path = path.Append(kTestFilePath); |
| 11852 | path = path.AppendASCII(kFtpTestFile); |
| 11853 | std::string contents; |
| 11854 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
| 11855 | return contents; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11856 | } |
| 11857 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 11858 | protected: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11859 | MockHostResolver host_resolver_; |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11860 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11861 | SpawnedTestServer ftp_test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 11862 | }; |
| 11863 | |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11864 | // Make sure an FTP request using an unsafe ports fails. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 11865 | TEST_F(URLRequestTestFTP, UnsafePort) { |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11866 | GURL url("ftp://127.0.0.1:7"); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11867 | |
| 11868 | TestDelegate d; |
| 11869 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11870 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11871 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11872 | r->Start(); |
| 11873 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11874 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11875 | d.RunUntilComplete(); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11876 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11877 | EXPECT_FALSE(r->is_pending()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 11878 | EXPECT_EQ(ERR_UNSAFE_PORT, d.request_status()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11879 | } |
| 11880 | } |
| 11881 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11882 | TEST_F(URLRequestTestFTP, FTPDirectoryListing) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11883 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11884 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11885 | TestDelegate d; |
| 11886 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11887 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11888 | ftp_test_server_.GetURL("/"), DEFAULT_PRIORITY, &d, |
| 11889 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11890 | r->Start(); |
| 11891 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11892 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11893 | d.RunUntilComplete(); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11894 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11895 | EXPECT_FALSE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11896 | EXPECT_EQ(1, d.response_started_count()); |
| 11897 | EXPECT_FALSE(d.received_data_before_response()); |
| 11898 | EXPECT_LT(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11899 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 11900 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11901 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 11902 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11903 | } |
| 11904 | } |
| 11905 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11906 | TEST_F(URLRequestTestFTP, FTPGetTestAnonymous) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11907 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11908 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11909 | TestDelegate d; |
| 11910 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11911 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11912 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, &d, |
| 11913 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11914 | r->Start(); |
| 11915 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11916 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11917 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11918 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11919 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11920 | EXPECT_EQ(1, d.response_started_count()); |
| 11921 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11922 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11923 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 11924 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11925 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 11926 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11927 | } |
| 11928 | } |
| 11929 | |
Mike West | 80462a1 | 2018-11-27 16:05:06 | [diff] [blame] | 11930 | TEST_F(URLRequestTestFTP, FTPMimeType) { |
| 11931 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 11932 | |
| 11933 | struct { |
| 11934 | const char* path; |
| 11935 | const char* mime; |
| 11936 | } test_cases[] = { |
| 11937 | {"/", "text/vnd.chromium.ftp-dir"}, |
| 11938 | {kFtpTestFile, "application/octet-stream"}, |
| 11939 | }; |
| 11940 | |
| 11941 | for (const auto test : test_cases) { |
| 11942 | TestDelegate d; |
| 11943 | |
| 11944 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 11945 | ftp_test_server_.GetURL(test.path), DEFAULT_PRIORITY, &d, |
| 11946 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11947 | r->Start(); |
| 11948 | EXPECT_TRUE(r->is_pending()); |
| 11949 | |
| 11950 | d.RunUntilComplete(); |
| 11951 | |
| 11952 | std::string mime; |
| 11953 | r->GetMimeType(&mime); |
| 11954 | EXPECT_EQ(test.mime, mime); |
| 11955 | } |
| 11956 | } |
| 11957 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11958 | TEST_F(URLRequestTestFTP, FTPGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11959 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11960 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11961 | TestDelegate d; |
| 11962 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11963 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11964 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11965 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11966 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11967 | r->Start(); |
| 11968 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11969 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11970 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11971 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11972 | EXPECT_FALSE(r->is_pending()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11973 | EXPECT_EQ(1, d.response_started_count()); |
| 11974 | EXPECT_FALSE(d.received_data_before_response()); |
| 11975 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11976 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 11977 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11978 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 11979 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 11980 | |
| 11981 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11982 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 11983 | TestLoadTimingNoHttpResponse(load_timing_info); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11984 | } |
| 11985 | } |
| 11986 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11987 | TEST_F(URLRequestTestFTP, FTPCheckWrongPassword) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11988 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11989 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11990 | TestDelegate d; |
| 11991 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11992 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11993 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11994 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11995 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11996 | r->Start(); |
| 11997 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11998 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11999 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12000 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12001 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12002 | EXPECT_EQ(1, d.response_started_count()); |
| 12003 | EXPECT_FALSE(d.received_data_before_response()); |
| 12004 | EXPECT_EQ(d.bytes_received(), 0); |
| 12005 | } |
| 12006 | } |
| 12007 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12008 | TEST_F(URLRequestTestFTP, FTPCheckWrongPasswordRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12009 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12010 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12011 | TestDelegate d; |
| 12012 | // Set correct login credentials. The delegate will be asked for them when |
| 12013 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 12014 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12015 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12016 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12017 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12018 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12019 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12020 | r->Start(); |
| 12021 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12022 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12023 | d.RunUntilComplete(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12024 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12025 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12026 | EXPECT_EQ(1, d.response_started_count()); |
| 12027 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12028 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12029 | } |
| 12030 | } |
| 12031 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12032 | TEST_F(URLRequestTestFTP, FTPCheckWrongUser) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12033 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12034 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12035 | TestDelegate d; |
| 12036 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12037 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12038 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 12039 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12040 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12041 | r->Start(); |
| 12042 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12043 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12044 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12045 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12046 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12047 | EXPECT_EQ(1, d.response_started_count()); |
| 12048 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12049 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12050 | } |
| 12051 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12052 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12053 | TEST_F(URLRequestTestFTP, FTPCheckWrongUserRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12054 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12055 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12056 | TestDelegate d; |
| 12057 | // Set correct login credentials. The delegate will be asked for them when |
| 12058 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 12059 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12060 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12061 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12062 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 12063 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12064 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12065 | r->Start(); |
| 12066 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12067 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12068 | d.RunUntilComplete(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12069 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12070 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12071 | EXPECT_EQ(1, d.response_started_count()); |
| 12072 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12073 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12074 | } |
| 12075 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12076 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12077 | TEST_F(URLRequestTestFTP, FTPCacheURLCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12078 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12079 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12080 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12081 | { |
| 12082 | // Pass correct login identity in the URL. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12083 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12084 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12085 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12086 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12087 | r->Start(); |
| 12088 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12089 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12090 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12091 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12092 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12093 | EXPECT_EQ(1, d->response_started_count()); |
| 12094 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12095 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12096 | } |
| 12097 | |
| 12098 | d.reset(new TestDelegate); |
| 12099 | { |
| 12100 | // This request should use cached identity from previous request. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12101 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12102 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 12103 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12104 | r->Start(); |
| 12105 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12106 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12107 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12108 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12109 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12110 | EXPECT_EQ(1, d->response_started_count()); |
| 12111 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12112 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12113 | } |
| 12114 | } |
| 12115 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12116 | TEST_F(URLRequestTestFTP, FTPCacheLoginBoxCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12117 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12118 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12119 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12120 | // Set correct login credentials. The delegate will be asked for them when |
| 12121 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 12122 | d->set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12123 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12124 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12125 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12126 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12127 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12128 | r->Start(); |
| 12129 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12130 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12131 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12132 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12133 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12134 | EXPECT_EQ(1, d->response_started_count()); |
| 12135 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12136 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12137 | } |
| 12138 | |
| 12139 | // Use a new delegate without explicit credentials. The cached ones should be |
| 12140 | // used. |
| 12141 | d.reset(new TestDelegate); |
| 12142 | { |
| 12143 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 12144 | // ones. |
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 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 12147 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12148 | r->Start(); |
| 12149 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12150 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12151 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12152 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12153 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12154 | EXPECT_EQ(1, d->response_started_count()); |
| 12155 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12156 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12157 | } |
| 12158 | } |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 12159 | |
| 12160 | TEST_F(URLRequestTestFTP, RawBodyBytes) { |
| 12161 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 12162 | |
| 12163 | TestDelegate d; |
| 12164 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12165 | ftp_test_server_.GetURL("simple.html"), DEFAULT_PRIORITY, &d, |
| 12166 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 12167 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12168 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 12169 | |
| 12170 | EXPECT_EQ(6, req->GetRawBodyBytes()); |
| 12171 | } |
| 12172 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 12173 | #endif // !BUILDFLAG(DISABLE_FTP_SUPPORT) |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 12174 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12175 | TEST_F(URLRequestTest, NetworkAccessedClearOnDataRequest) { |
| 12176 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12177 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12178 | GURL("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12179 | |
| 12180 | EXPECT_FALSE(req->response_info().network_accessed); |
| 12181 | |
| 12182 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12183 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12184 | |
| 12185 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 12186 | EXPECT_FALSE(req->response_info().network_accessed); |
| 12187 | } |
| 12188 | |
| 12189 | TEST_F(URLRequestTest, NetworkAccessedSetOnHostResolutionFailure) { |
| 12190 | MockHostResolver host_resolver; |
| 12191 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 12192 | TestURLRequestContext context(true); |
| 12193 | context.set_network_delegate(&network_delegate); |
| 12194 | context.set_host_resolver(&host_resolver); |
| 12195 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 12196 | context.Init(); |
| 12197 | |
| 12198 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12199 | std::unique_ptr<URLRequest> req( |
| 12200 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 12201 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12202 | |
| 12203 | EXPECT_FALSE(req->response_info().network_accessed); |
| 12204 | |
| 12205 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12206 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12207 | EXPECT_TRUE(req->response_info().network_accessed); |
| 12208 | } |
| 12209 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 12210 | // Test that URLRequest is canceled correctly. |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12211 | // See http://crbug.com/508900 |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 12212 | TEST_F(URLRequestTest, URLRequestRedirectJobCancelRequest) { |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12213 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12214 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12215 | GURL("http://not-a-real-domain/"), DEFAULT_PRIORITY, &d, |
| 12216 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12217 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12218 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12219 | req.get(), &default_network_delegate_, |
| 12220 | GURL("http://this-should-never-be-navigated-to/"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 12221 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Jumbo shrimp")); |
| 12222 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12223 | |
| 12224 | req->Start(); |
| 12225 | req->Cancel(); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12226 | base::RunLoop().RunUntilIdle(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 12227 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12228 | EXPECT_EQ(0, d.received_redirect_count()); |
| 12229 | } |
| 12230 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12231 | TEST_F(URLRequestTestHTTP, HeadersCallbacks) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12232 | ASSERT_TRUE(http_test_server()->Start()); |
| 12233 | TestURLRequestContext context; |
| 12234 | GURL url(http_test_server()->GetURL("/cachetime")); |
| 12235 | TestDelegate delegate; |
| 12236 | HttpRequestHeaders extra_headers; |
| 12237 | extra_headers.SetHeader("X-Foo", "bar"); |
| 12238 | |
| 12239 | { |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12240 | HttpRawRequestHeaders raw_req_headers; |
| 12241 | scoped_refptr<const HttpResponseHeaders> raw_resp_headers; |
| 12242 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12243 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12244 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12245 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12246 | r->SetRequestHeadersCallback(base::Bind( |
| 12247 | &HttpRawRequestHeaders::Assign, base::Unretained(&raw_req_headers))); |
| 12248 | r->SetResponseHeadersCallback(base::Bind( |
| 12249 | [](scoped_refptr<const HttpResponseHeaders>* left, |
| 12250 | scoped_refptr<const HttpResponseHeaders> right) { *left = right; }, |
| 12251 | base::Unretained(&raw_resp_headers))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12252 | r->Start(); |
| 12253 | while (!delegate.response_started_count()) |
| 12254 | base::RunLoop().RunUntilIdle(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12255 | EXPECT_FALSE(raw_req_headers.headers().empty()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12256 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12257 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12258 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12259 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12260 | EXPECT_EQ("gzip, deflate", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12261 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Connection", &value)); |
| 12262 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Host", &value)); |
| 12263 | EXPECT_EQ("GET /cachetime HTTP/1.1\r\n", raw_req_headers.request_line()); |
| 12264 | EXPECT_EQ(raw_resp_headers.get(), r->response_headers()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12265 | } |
| 12266 | { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12267 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12268 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12269 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12270 | r->SetRequestHeadersCallback(base::Bind([](HttpRawRequestHeaders) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12271 | FAIL() << "Callback should not be called unless request is sent"; |
| 12272 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12273 | r->SetResponseHeadersCallback( |
| 12274 | base::Bind([](scoped_refptr<const HttpResponseHeaders>) { |
| 12275 | FAIL() << "Callback should not be called unless request is sent"; |
| 12276 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12277 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12278 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12279 | EXPECT_TRUE(r->was_cached()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12280 | } |
| 12281 | } |
| 12282 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12283 | TEST_F(URLRequestTestHTTP, HeadersCallbacksWithRedirect) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12284 | ASSERT_TRUE(http_test_server()->Start()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12285 | HttpRawRequestHeaders raw_req_headers; |
| 12286 | scoped_refptr<const HttpResponseHeaders> raw_resp_headers; |
| 12287 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12288 | TestURLRequestContext context; |
| 12289 | TestDelegate delegate; |
| 12290 | HttpRequestHeaders extra_headers; |
| 12291 | extra_headers.SetHeader("X-Foo", "bar"); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12292 | GURL url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12293 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12294 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12295 | r->SetExtraRequestHeaders(extra_headers); |
| 12296 | r->SetRequestHeadersCallback(base::Bind(&HttpRawRequestHeaders::Assign, |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12297 | base::Unretained(&raw_req_headers))); |
| 12298 | r->SetResponseHeadersCallback(base::Bind( |
| 12299 | [](scoped_refptr<const HttpResponseHeaders>* left, |
| 12300 | scoped_refptr<const HttpResponseHeaders> right) { *left = right; }, |
| 12301 | base::Unretained(&raw_resp_headers))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12302 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 12303 | delegate.RunUntilRedirect(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12304 | |
| 12305 | ASSERT_EQ(1, delegate.received_redirect_count()); |
| 12306 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12307 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12308 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12309 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12310 | EXPECT_EQ("gzip, deflate", value); |
| 12311 | EXPECT_EQ(1, delegate.received_redirect_count()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12312 | EXPECT_EQ("GET /redirect-test.html HTTP/1.1\r\n", |
| 12313 | raw_req_headers.request_line()); |
| 12314 | EXPECT_TRUE(raw_resp_headers->HasHeader("Location")); |
| 12315 | EXPECT_EQ(302, raw_resp_headers->response_code()); |
| 12316 | EXPECT_EQ("Redirect", raw_resp_headers->GetStatusText()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12317 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12318 | raw_req_headers = HttpRawRequestHeaders(); |
| 12319 | raw_resp_headers = nullptr; |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 12320 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 12321 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12322 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12323 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12324 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12325 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12326 | EXPECT_EQ("gzip, deflate", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12327 | EXPECT_EQ("GET /with-headers.html HTTP/1.1\r\n", |
| 12328 | raw_req_headers.request_line()); |
| 12329 | EXPECT_EQ(r->response_headers(), raw_resp_headers.get()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12330 | } |
| 12331 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12332 | TEST_F(URLRequestTest, HeadersCallbacksConnectFailed) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12333 | TestDelegate request_delegate; |
| 12334 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12335 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12336 | GURL("http://127.0.0.1:9/"), DEFAULT_PRIORITY, &request_delegate, |
| 12337 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12338 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
| 12339 | FAIL() << "Callback should not be called unless request is sent"; |
| 12340 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12341 | r->SetResponseHeadersCallback( |
| 12342 | base::Bind([](scoped_refptr<const net::HttpResponseHeaders>) { |
| 12343 | FAIL() << "Callback should not be called unless request is sent"; |
| 12344 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12345 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12346 | request_delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12347 | EXPECT_FALSE(r->is_pending()); |
| 12348 | } |
| 12349 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12350 | TEST_F(URLRequestTestHTTP, HeadersCallbacksAuthRetry) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12351 | ASSERT_TRUE(http_test_server()->Start()); |
| 12352 | GURL url(http_test_server()->GetURL("/auth-basic")); |
| 12353 | |
| 12354 | TestURLRequestContext context; |
| 12355 | TestDelegate delegate; |
| 12356 | |
| 12357 | delegate.set_credentials(AuthCredentials(kUser, kSecret)); |
| 12358 | HttpRequestHeaders extra_headers; |
| 12359 | extra_headers.SetHeader("X-Foo", "bar"); |
| 12360 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12361 | using ReqHeadersVector = std::vector<std::unique_ptr<HttpRawRequestHeaders>>; |
| 12362 | ReqHeadersVector raw_req_headers; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12363 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12364 | using RespHeadersVector = |
| 12365 | std::vector<scoped_refptr<const HttpResponseHeaders>>; |
| 12366 | RespHeadersVector raw_resp_headers; |
| 12367 | |
| 12368 | auto req_headers_callback = base::Bind( |
| 12369 | [](ReqHeadersVector* vec, HttpRawRequestHeaders headers) { |
| 12370 | vec->emplace_back(new HttpRawRequestHeaders(std::move(headers))); |
| 12371 | }, |
| 12372 | &raw_req_headers); |
| 12373 | auto resp_headers_callback = base::Bind( |
| 12374 | [](RespHeadersVector* vec, |
| 12375 | scoped_refptr<const HttpResponseHeaders> headers) { |
| 12376 | vec->push_back(headers); |
| 12377 | }, |
| 12378 | &raw_resp_headers); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12379 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12380 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12381 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12382 | r->SetRequestHeadersCallback(req_headers_callback); |
| 12383 | r->SetResponseHeadersCallback(resp_headers_callback); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12384 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12385 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12386 | EXPECT_FALSE(r->is_pending()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12387 | ASSERT_EQ(raw_req_headers.size(), 2u); |
| 12388 | ASSERT_EQ(raw_resp_headers.size(), 2u); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12389 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12390 | EXPECT_FALSE(raw_req_headers[0]->FindHeaderForTest("Authorization", &value)); |
| 12391 | EXPECT_TRUE(raw_req_headers[0]->FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12392 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12393 | EXPECT_TRUE(raw_req_headers[1]->FindHeaderForTest("Authorization", &value)); |
| 12394 | EXPECT_TRUE(raw_req_headers[1]->FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12395 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12396 | EXPECT_EQ(raw_resp_headers[1], r->response_headers()); |
| 12397 | EXPECT_NE(raw_resp_headers[0], raw_resp_headers[1]); |
| 12398 | EXPECT_EQ(401, raw_resp_headers[0]->response_code()); |
| 12399 | EXPECT_EQ("Unauthorized", raw_resp_headers[0]->GetStatusText()); |
| 12400 | |
| 12401 | std::unique_ptr<URLRequest> r2(context.CreateRequest( |
| 12402 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12403 | r2->SetExtraRequestHeaders(extra_headers); |
| 12404 | r2->SetRequestHeadersCallback(req_headers_callback); |
| 12405 | r2->SetResponseHeadersCallback(resp_headers_callback); |
| 12406 | r2->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 12407 | r2->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12408 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12409 | EXPECT_FALSE(r2->is_pending()); |
| 12410 | ASSERT_EQ(raw_req_headers.size(), 3u); |
| 12411 | ASSERT_EQ(raw_resp_headers.size(), 3u); |
| 12412 | EXPECT_TRUE(raw_req_headers[2]->FindHeaderForTest("If-None-Match", &value)); |
| 12413 | EXPECT_NE(raw_resp_headers[2].get(), r2->response_headers()); |
| 12414 | EXPECT_EQ(304, raw_resp_headers[2]->response_code()); |
| 12415 | EXPECT_EQ("Not Modified", raw_resp_headers[2]->GetStatusText()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12416 | } |
| 12417 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12418 | TEST_F(URLRequestTest, HeadersCallbacksNonHTTP) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12419 | GURL data_url("data:text/html,<html><body>Hello!</body></html>"); |
| 12420 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12421 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12422 | data_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12423 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12424 | FAIL() << "Callback should not be called for non-HTTP schemes"; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12425 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12426 | r->SetResponseHeadersCallback( |
| 12427 | base::Bind([](scoped_refptr<const net::HttpResponseHeaders>) { |
| 12428 | FAIL() << "Callback should not be called for non-HTTP schemes"; |
| 12429 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12430 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12431 | d.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12432 | EXPECT_FALSE(r->is_pending()); |
| 12433 | } |
| 12434 | |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12435 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSet) { |
| 12436 | TestDelegate d; |
| 12437 | BlockingNetworkDelegate network_delegate( |
| 12438 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 12439 | const GURL kOriginalUrl("https://original.test"); |
| 12440 | const GURL kRedirectUrl("http://redirect.test"); |
| 12441 | network_delegate.set_redirect_url(kRedirectUrl); |
| 12442 | TestURLRequestContext context(true /* delay_initialization */); |
| 12443 | context.set_network_delegate(&network_delegate); |
| 12444 | context.Init(); |
| 12445 | |
| 12446 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12447 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12448 | r->set_upgrade_if_insecure(true); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12449 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 12450 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12451 | GURL::Replacements replacements; |
| 12452 | // Check that the redirect URL was upgraded to HTTPS since upgrade_if_insecure |
| 12453 | // was set. |
| 12454 | replacements.SetSchemeStr("https"); |
| 12455 | EXPECT_EQ(kRedirectUrl.ReplaceComponents(replacements), |
| 12456 | d.redirect_info().new_url); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 12457 | EXPECT_TRUE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12458 | } |
| 12459 | |
| 12460 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSetExplicitPort80) { |
| 12461 | TestDelegate d; |
| 12462 | BlockingNetworkDelegate network_delegate( |
| 12463 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 12464 | const GURL kOriginalUrl("https://original.test"); |
| 12465 | const GURL kRedirectUrl("http://redirect.test:80"); |
| 12466 | network_delegate.set_redirect_url(kRedirectUrl); |
| 12467 | TestURLRequestContext context(true /* delay_initialization */); |
| 12468 | context.set_network_delegate(&network_delegate); |
| 12469 | context.Init(); |
| 12470 | |
| 12471 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12472 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12473 | r->set_upgrade_if_insecure(true); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12474 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 12475 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12476 | GURL::Replacements replacements; |
| 12477 | // The URL host should have not been changed. |
| 12478 | EXPECT_EQ(d.redirect_info().new_url.host(), kRedirectUrl.host()); |
| 12479 | // The scheme should now be https, and the effective port should now be 443. |
| 12480 | EXPECT_TRUE(d.redirect_info().new_url.SchemeIs("https")); |
| 12481 | EXPECT_EQ(d.redirect_info().new_url.EffectiveIntPort(), 443); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 12482 | EXPECT_TRUE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12483 | } |
| 12484 | |
| 12485 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSetNonStandardPort) { |
| 12486 | TestDelegate d; |
| 12487 | BlockingNetworkDelegate network_delegate( |
| 12488 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 12489 | const GURL kOriginalUrl("https://original.test"); |
| 12490 | const GURL kRedirectUrl("http://redirect.test:1234"); |
| 12491 | network_delegate.set_redirect_url(kRedirectUrl); |
| 12492 | TestURLRequestContext context(true /* delay_initialization */); |
| 12493 | context.set_network_delegate(&network_delegate); |
| 12494 | context.Init(); |
| 12495 | |
| 12496 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12497 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12498 | r->set_upgrade_if_insecure(true); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12499 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 12500 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12501 | GURL::Replacements replacements; |
| 12502 | // Check that the redirect URL was upgraded to HTTPS since upgrade_if_insecure |
| 12503 | // was set, nonstandard port should not have been modified. |
| 12504 | replacements.SetSchemeStr("https"); |
| 12505 | EXPECT_EQ(kRedirectUrl.ReplaceComponents(replacements), |
| 12506 | d.redirect_info().new_url); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 12507 | EXPECT_TRUE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12508 | } |
| 12509 | |
| 12510 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagNotSet) { |
| 12511 | TestDelegate d; |
| 12512 | BlockingNetworkDelegate network_delegate( |
| 12513 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 12514 | const GURL kOriginalUrl("https://original.test"); |
| 12515 | const GURL kRedirectUrl("http://redirect.test"); |
| 12516 | network_delegate.set_redirect_url(kRedirectUrl); |
| 12517 | TestURLRequestContext context(true /* delay_initialization */); |
| 12518 | context.set_network_delegate(&network_delegate); |
| 12519 | context.Init(); |
| 12520 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12521 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12522 | r->set_upgrade_if_insecure(false); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12523 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 12524 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12525 | // The redirect URL should not be changed if the upgrade_if_insecure flag is |
| 12526 | // not set. |
| 12527 | EXPECT_EQ(kRedirectUrl, d.redirect_info().new_url); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 12528 | EXPECT_FALSE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12529 | } |
| 12530 | |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12531 | // Test that URLRequests get properly tagged. |
| 12532 | #if defined(OS_ANDROID) |
| 12533 | TEST_F(URLRequestTestHTTP, TestTagging) { |
Peter Collingbourne | 8ccab72 | 2019-02-12 18:10:48 | [diff] [blame] | 12534 | if (!CanGetTaggedBytes()) { |
| 12535 | DVLOG(0) << "Skipping test - GetTaggedBytes unsupported."; |
| 12536 | return; |
| 12537 | } |
| 12538 | |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12539 | ASSERT_TRUE(http_test_server()->Start()); |
| 12540 | |
| 12541 | // The tag under which the system reports untagged traffic. |
| 12542 | static const int32_t UNTAGGED_TAG = 0; |
| 12543 | |
| 12544 | uint64_t old_traffic = GetTaggedBytes(UNTAGGED_TAG); |
| 12545 | |
| 12546 | // Untagged traffic should be tagged with tag UNTAGGED_TAG. |
| 12547 | TestDelegate delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12548 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12549 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &delegate, |
| 12550 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12551 | EXPECT_EQ(SocketTag(), req->socket_tag()); |
| 12552 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12553 | delegate.RunUntilComplete(); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12554 | |
| 12555 | EXPECT_GT(GetTaggedBytes(UNTAGGED_TAG), old_traffic); |
| 12556 | |
| 12557 | int32_t tag_val1 = 0x12345678; |
| 12558 | SocketTag tag1(SocketTag::UNSET_UID, tag_val1); |
| 12559 | old_traffic = GetTaggedBytes(tag_val1); |
| 12560 | |
| 12561 | // Test specific tag value. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12562 | req = default_context().CreateRequest(http_test_server()->GetURL("/"), |
| 12563 | DEFAULT_PRIORITY, &delegate, |
| 12564 | TRAFFIC_ANNOTATION_FOR_TESTS); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12565 | req->set_socket_tag(tag1); |
| 12566 | EXPECT_EQ(tag1, req->socket_tag()); |
| 12567 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12568 | delegate.RunUntilComplete(); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12569 | |
| 12570 | EXPECT_GT(GetTaggedBytes(tag_val1), old_traffic); |
| 12571 | } |
| 12572 | #endif |
| 12573 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 12574 | } // namespace net |