[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" |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 56 | #include "net/base/layered_network_delegate.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 57 | #include "net/base/load_flags.h" |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 58 | #include "net/base/load_timing_info.h" |
| 59 | #include "net/base/load_timing_info_test_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 60 | #include "net/base/net_errors.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 61 | #include "net/base/net_module.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 62 | #include "net/base/proxy_server.h" |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 63 | #include "net/base/request_priority.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 64 | #include "net/base/test_completion_callback.h" |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 65 | #include "net/base/upload_bytes_element_reader.h" |
| 66 | #include "net/base/upload_data_stream.h" |
| 67 | #include "net/base/upload_file_element_reader.h" |
tfarina | 7a4a7fd | 2016-01-20 14:23:44 | [diff] [blame] | 68 | #include "net/base/url_util.h" |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 69 | #include "net/cert/crl_set.h" |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 70 | #include "net/cert/ct_policy_enforcer.h" |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 71 | #include "net/cert/ct_policy_status.h" |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 72 | #include "net/cert/do_nothing_ct_verifier.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 73 | #include "net/cert/ev_root_ca_metadata.h" |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 74 | #include "net/cert/mock_cert_verifier.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 75 | #include "net/cert/multi_log_ct_verifier.h" |
eranm | dcec963 | 2016-10-10 14:16:10 | [diff] [blame] | 76 | #include "net/cert/signed_certificate_timestamp_and_status.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 77 | #include "net/cert/test_root_certs.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 78 | #include "net/cookies/cookie_monster.h" |
| 79 | #include "net/cookies/cookie_store_test_helpers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 80 | #include "net/disk_cache/disk_cache.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 81 | #include "net/dns/mock_host_resolver.h" |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 82 | #include "net/http/http_byte_range.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 83 | #include "net/http/http_cache.h" |
| 84 | #include "net/http/http_network_layer.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 85 | #include "net/http/http_network_session.h" |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 86 | #include "net/http/http_request_headers.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 87 | #include "net/http/http_response_headers.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 88 | #include "net/http/http_server_properties_impl.h" |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 89 | #include "net/http/http_util.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 90 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 91 | #include "net/log/net_log_source.h" |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 92 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 93 | #include "net/log/test_net_log_entry.h" |
| 94 | #include "net/log/test_net_log_util.h" |
Scott Violet | 0caaaf43 | 2018-03-24 00:43:59 | [diff] [blame] | 95 | #include "net/net_buildflags.h" |
Lily Houghton | ffe89daa0 | 2018-03-09 18:30:03 | [diff] [blame] | 96 | #include "net/proxy_resolution/proxy_resolution_service.h" |
Ryan Hamilton | a3ee93a7 | 2018-08-01 22:03:08 | [diff] [blame] | 97 | #include "net/quic/mock_crypto_client_stream_factory.h" |
| 98 | #include "net/quic/quic_server_info.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 99 | #include "net/socket/socket_test_util.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 100 | #include "net/socket/ssl_client_socket.h" |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 101 | #include "net/ssl/channel_id_service.h" |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 102 | #include "net/ssl/client_cert_identity_test_util.h" |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 103 | #include "net/ssl/default_channel_id_store.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 104 | #include "net/ssl/ssl_connection_status_flags.h" |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 105 | #include "net/ssl/ssl_private_key.h" |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 106 | #include "net/ssl/ssl_server_config.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 107 | #include "net/test/cert_test_util.h" |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 108 | #include "net/test/embedded_test_server/embedded_test_server.h" |
| 109 | #include "net/test/embedded_test_server/http_request.h" |
| 110 | #include "net/test/embedded_test_server/http_response.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 111 | #include "net/test/gtest_util.h" |
[email protected] | 89b3252 | 2013-05-07 20:04:21 | [diff] [blame] | 112 | #include "net/test/spawned_test_server/spawned_test_server.h" |
rsleevi | a69c79a | 2016-06-22 03:28:43 | [diff] [blame] | 113 | #include "net/test/test_data_directory.h" |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 114 | #include "net/test/test_with_scoped_task_environment.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 115 | #include "net/test/url_request/url_request_failed_job.h" |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 116 | #include "net/test/url_request/url_request_mock_http_job.h" |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 117 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 118 | #include "net/url_request/data_protocol_handler.h" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 119 | #include "net/url_request/static_http_user_agent_settings.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 120 | #include "net/url_request/url_request.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 121 | #include "net/url_request/url_request_filter.h" |
[email protected] | bcb84f8b | 2009-08-31 16:20:14 | [diff] [blame] | 122 | #include "net/url_request/url_request_http_job.h" |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 123 | #include "net/url_request/url_request_http_job_histogram.h" |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 124 | #include "net/url_request/url_request_intercepting_job_factory.h" |
| 125 | #include "net/url_request/url_request_interceptor.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 126 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 127 | #include "net/url_request/url_request_redirect_job.h" |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 128 | #include "net/url_request/url_request_test_job.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 129 | #include "net/url_request/url_request_test_util.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 130 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 131 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 132 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 133 | |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 134 | #if defined(OS_FUCHSIA) |
| 135 | #define USE_BUILTIN_CERT_VERIFIER |
| 136 | #endif |
| 137 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 138 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 139 | #include "net/base/filename_util.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 140 | #include "net/url_request/file_protocol_handler.h" |
| 141 | #include "net/url_request/url_request_file_dir_job.h" |
| 142 | #endif |
| 143 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 144 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 145 | #include "net/ftp/ftp_network_layer.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 146 | #include "net/url_request/ftp_protocol_handler.h" |
| 147 | #endif |
| 148 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 149 | #if defined(OS_WIN) |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 150 | #include "base/win/scoped_com_initializer.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 151 | #endif |
| 152 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 153 | #if BUILDFLAG(ENABLE_REPORTING) |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 154 | #include "net/network_error_logging/network_error_logging_service.h" |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 155 | #include "net/network_error_logging/network_error_logging_test_util.h" |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 156 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 157 | |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 158 | #if defined(OS_ANDROID) || defined(USE_BUILTIN_CERT_VERIFIER) |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 159 | #include "net/cert/cert_net_fetcher.h" |
| 160 | #include "net/cert_net/cert_net_fetcher_impl.h" |
| 161 | #endif |
| 162 | |
| 163 | #if defined(USE_NSS_CERTS) |
| 164 | #include "net/cert_net/nss_ocsp.h" |
| 165 | #endif |
| 166 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 167 | using net::test::IsError; |
| 168 | using net::test::IsOk; |
| 169 | |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 170 | using base::ASCIIToUTF16; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 171 | using base::Time; |
halton.huo | e4e4574 | 2014-12-08 07:55:46 | [diff] [blame] | 172 | using std::string; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 173 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 174 | namespace net { |
| 175 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 176 | namespace { |
| 177 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 178 | namespace test_default { |
| 179 | #include "net/http/transport_security_state_static_unittest_default.h" |
xunjieli | 815ad5b | 2017-07-18 15:51:35 | [diff] [blame] | 180 | } |
| 181 | |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 182 | const base::string16 kChrome(ASCIIToUTF16("chrome")); |
| 183 | const base::string16 kSecret(ASCIIToUTF16("secret")); |
| 184 | const base::string16 kUser(ASCIIToUTF16("user")); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 185 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 186 | const base::FilePath::CharType kTestFilePath[] = |
| 187 | FILE_PATH_LITERAL("net/data/url_request_unittest"); |
| 188 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 189 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 190 | !defined(OS_FUCHSIA) |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 191 | // Test file used in most FTP tests. |
| 192 | const char kFtpTestFile[] = "BullRunSpeech.txt"; |
| 193 | #endif |
| 194 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 195 | // Tests load timing information in the case a fresh connection was used, with |
| 196 | // no proxy. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 197 | void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info, |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 198 | int connect_timing_flags) { |
| 199 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 200 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 201 | |
| 202 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 203 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 204 | |
| 205 | EXPECT_LE(load_timing_info.request_start, |
| 206 | load_timing_info.connect_timing.connect_start); |
| 207 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 208 | connect_timing_flags); |
| 209 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 210 | load_timing_info.send_start); |
| 211 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 212 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_start); |
| 213 | EXPECT_LE(load_timing_info.receive_headers_start, |
| 214 | load_timing_info.receive_headers_end); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 215 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 216 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 217 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 218 | } |
| 219 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 220 | // Same as above, but with proxy times. |
| 221 | void TestLoadTimingNotReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 222 | const LoadTimingInfo& load_timing_info, |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 223 | int connect_timing_flags) { |
| 224 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 225 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 226 | |
| 227 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 228 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 229 | |
| 230 | EXPECT_LE(load_timing_info.request_start, |
| 231 | load_timing_info.proxy_resolve_start); |
| 232 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 233 | load_timing_info.proxy_resolve_end); |
| 234 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 235 | load_timing_info.connect_timing.connect_start); |
| 236 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 237 | connect_timing_flags); |
| 238 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 239 | load_timing_info.send_start); |
| 240 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 241 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_start); |
| 242 | EXPECT_LE(load_timing_info.receive_headers_start, |
| 243 | load_timing_info.receive_headers_end); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | // Same as above, but with a reused socket and proxy times. |
| 247 | void TestLoadTimingReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 248 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 249 | EXPECT_TRUE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 250 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 251 | |
| 252 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 253 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 254 | |
| 255 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 256 | |
| 257 | EXPECT_LE(load_timing_info.request_start, |
| 258 | load_timing_info.proxy_resolve_start); |
| 259 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 260 | load_timing_info.proxy_resolve_end); |
| 261 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 262 | load_timing_info.send_start); |
| 263 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 264 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_start); |
| 265 | EXPECT_LE(load_timing_info.receive_headers_start, |
| 266 | load_timing_info.receive_headers_end); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 267 | } |
| 268 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 269 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 270 | // Tests load timing information in the case of a cache hit, when no cache |
| 271 | // validation request was sent over the wire. |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 272 | base::StringPiece TestNetResourceProvider(int key) { |
| 273 | return "header"; |
| 274 | } |
| 275 | |
| 276 | void FillBuffer(char* buffer, size_t len) { |
| 277 | static bool called = false; |
| 278 | if (!called) { |
| 279 | called = true; |
| 280 | int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 281 | srand(seed); |
| 282 | } |
| 283 | |
| 284 | for (size_t i = 0; i < len; i++) { |
| 285 | buffer[i] = static_cast<char>(rand()); |
| 286 | if (!buffer[i]) |
| 287 | buffer[i] = 'g'; |
| 288 | } |
| 289 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 290 | #endif |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 291 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 292 | void TestLoadTimingCacheHitNoNetwork( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 293 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 294 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 295 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 296 | |
| 297 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 298 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 299 | |
| 300 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 301 | EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start); |
| 302 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 303 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_start); |
| 304 | EXPECT_LE(load_timing_info.receive_headers_start, |
| 305 | load_timing_info.receive_headers_end); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 306 | |
| 307 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 308 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 309 | } |
| 310 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 311 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 312 | !defined(OS_FUCHSIA) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 313 | // Tests load timing in the case that there is no HTTP response. This can be |
| 314 | // used to test in the case of errors or non-HTTP requests. |
| 315 | void TestLoadTimingNoHttpResponse( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 316 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 317 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 318 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 319 | |
| 320 | // Only the request times should be non-null. |
| 321 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 322 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 323 | |
| 324 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 325 | |
| 326 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 327 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 328 | EXPECT_TRUE(load_timing_info.send_start.is_null()); |
| 329 | EXPECT_TRUE(load_timing_info.send_end.is_null()); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 330 | EXPECT_TRUE(load_timing_info.receive_headers_start.is_null()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 331 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 332 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 333 | #endif |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 334 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 335 | // Test power monitor source that can simulate entering suspend mode. Can't use |
| 336 | // the one in base/ because it insists on bringing its own MessageLoop. |
| 337 | class TestPowerMonitorSource : public base::PowerMonitorSource { |
| 338 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 339 | TestPowerMonitorSource() = default; |
| 340 | ~TestPowerMonitorSource() override = default; |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 341 | |
Colin Blundell | c46597f | 2018-06-14 16:43:10 | [diff] [blame] | 342 | void Shutdown() override {} |
| 343 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 344 | void Suspend() { ProcessPowerEvent(SUSPEND_EVENT); } |
| 345 | |
| 346 | void Resume() { ProcessPowerEvent(RESUME_EVENT); } |
| 347 | |
| 348 | bool IsOnBatteryPowerImpl() override { return false; } |
| 349 | |
| 350 | private: |
| 351 | DISALLOW_COPY_AND_ASSIGN(TestPowerMonitorSource); |
| 352 | }; |
| 353 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 354 | // Job that allows monitoring of its priority. |
| 355 | class PriorityMonitoringURLRequestJob : public URLRequestTestJob { |
| 356 | public: |
| 357 | // The latest priority of the job is always written to |request_priority_|. |
| 358 | PriorityMonitoringURLRequestJob(URLRequest* request, |
| 359 | NetworkDelegate* network_delegate, |
| 360 | RequestPriority* request_priority) |
| 361 | : URLRequestTestJob(request, network_delegate), |
| 362 | request_priority_(request_priority) { |
| 363 | *request_priority_ = DEFAULT_PRIORITY; |
| 364 | } |
| 365 | |
| 366 | void SetPriority(RequestPriority priority) override { |
| 367 | *request_priority_ = priority; |
| 368 | URLRequestTestJob::SetPriority(priority); |
| 369 | } |
| 370 | |
| 371 | private: |
| 372 | RequestPriority* const request_priority_; |
| 373 | }; |
| 374 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 375 | // Do a case-insensitive search through |haystack| for |needle|. |
| 376 | bool ContainsString(const std::string& haystack, const char* needle) { |
brettw | a2027fb | 2015-07-14 02:24:50 | [diff] [blame] | 377 | std::string::const_iterator it = std::search( |
| 378 | haystack.begin(), haystack.end(), needle, needle + strlen(needle), |
| 379 | base::CaseInsensitiveCompareASCII<char>()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 380 | return it != haystack.end(); |
| 381 | } |
| 382 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 383 | std::unique_ptr<UploadDataStream> CreateSimpleUploadData(const char* data) { |
| 384 | std::unique_ptr<UploadElementReader> reader( |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 385 | new UploadBytesElementReader(data, strlen(data))); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 386 | return ElementsUploadDataStream::CreateWithReader(std::move(reader), 0); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 387 | } |
| 388 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 389 | // Verify that the SSLInfo of a successful SSL connection has valid values. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 390 | void CheckSSLInfo(const SSLInfo& ssl_info) { |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 391 | // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated. |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 392 | uint16_t cipher_suite = |
| 393 | SSLConnectionStatusToCipherSuite(ssl_info.connection_status); |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 394 | EXPECT_NE(0U, cipher_suite); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 395 | } |
| 396 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 397 | void CheckFullRequestHeaders(const HttpRequestHeaders& headers, |
| 398 | const GURL& host_url) { |
| 399 | std::string sent_value; |
| 400 | |
| 401 | EXPECT_TRUE(headers.GetHeader("Host", &sent_value)); |
| 402 | EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value); |
| 403 | |
| 404 | EXPECT_TRUE(headers.GetHeader("Connection", &sent_value)); |
| 405 | EXPECT_EQ("keep-alive", sent_value); |
| 406 | } |
| 407 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 408 | // A network delegate that allows the user to choose a subset of request stages |
| 409 | // to block in. When blocking, the delegate can do one of the following: |
| 410 | // * synchronously return a pre-specified error code, or |
| 411 | // * asynchronously return that value via an automatically called callback, |
| 412 | // or |
| 413 | // * block and wait for the user to do a callback. |
| 414 | // Additionally, the user may also specify a redirect URL -- then each request |
| 415 | // with the current URL different from the redirect target will be redirected |
| 416 | // to that target, in the on-before-URL-request stage, independent of whether |
| 417 | // the delegate blocks in ON_BEFORE_URL_REQUEST or not. |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 418 | class BlockingNetworkDelegate : public TestNetworkDelegate { |
| 419 | public: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 420 | // Stages in which the delegate can block. |
| 421 | enum Stage { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 422 | NOT_BLOCKED = 0, |
| 423 | ON_BEFORE_URL_REQUEST = 1 << 0, |
| 424 | ON_BEFORE_SEND_HEADERS = 1 << 1, |
| 425 | ON_HEADERS_RECEIVED = 1 << 2, |
| 426 | ON_AUTH_REQUIRED = 1 << 3 |
| 427 | }; |
| 428 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 429 | // Behavior during blocked stages. During other stages, just |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 430 | // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 431 | enum BlockMode { |
| 432 | SYNCHRONOUS, // No callback, returns specified return values. |
| 433 | AUTO_CALLBACK, // |this| posts a task to run the callback using the |
| 434 | // specified return codes. |
| 435 | USER_CALLBACK, // User takes care of doing a callback. |retval_| and |
| 436 | // |auth_retval_| are ignored. In every blocking stage the |
| 437 | // message loop is quit. |
| 438 | }; |
| 439 | |
| 440 | // Creates a delegate which does not block at all. |
| 441 | explicit BlockingNetworkDelegate(BlockMode block_mode); |
| 442 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 443 | // Runs the message loop until the delegate blocks. |
| 444 | void RunUntilBlocked(); |
| 445 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 446 | // For users to trigger a callback returning |response|. |
| 447 | // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks. |
| 448 | // Only call if |block_mode_| == USER_CALLBACK. |
| 449 | void DoCallback(int response); |
| 450 | void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response); |
| 451 | |
| 452 | // Setters. |
| 453 | void set_retval(int retval) { |
| 454 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 455 | ASSERT_NE(ERR_IO_PENDING, retval); |
| 456 | ASSERT_NE(OK, retval); |
| 457 | retval_ = retval; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 458 | } |
| 459 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 460 | // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then |
| 461 | // |auth_credentials_| will be passed with the response. |
| 462 | void set_auth_retval(AuthRequiredResponse auth_retval) { |
| 463 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 464 | ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval); |
| 465 | auth_retval_ = auth_retval; |
| 466 | } |
| 467 | void set_auth_credentials(const AuthCredentials& auth_credentials) { |
| 468 | auth_credentials_ = auth_credentials; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 469 | } |
| 470 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 471 | void set_redirect_url(const GURL& url) { |
| 472 | redirect_url_ = url; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 473 | } |
| 474 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 475 | void set_block_on(int block_on) { |
| 476 | block_on_ = block_on; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 477 | } |
| 478 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 479 | // Allows the user to check in which state did we block. |
| 480 | Stage stage_blocked_for_callback() const { |
| 481 | EXPECT_EQ(USER_CALLBACK, block_mode_); |
| 482 | return stage_blocked_for_callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | private: |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 486 | void OnBlocked(); |
| 487 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 488 | void RunCallback(int response, CompletionOnceCallback callback); |
| 489 | void RunAuthCallback(AuthRequiredResponse response, AuthCallback callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 490 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 491 | // TestNetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 492 | int OnBeforeURLRequest(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 493 | CompletionOnceCallback callback, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 494 | GURL* new_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 495 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 496 | int OnBeforeStartTransaction(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 497 | CompletionOnceCallback callback, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 498 | HttpRequestHeaders* headers) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 499 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 500 | int OnHeadersReceived( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 501 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 502 | CompletionOnceCallback callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 503 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 504 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 505 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 506 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 507 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 508 | URLRequest* request, |
| 509 | const AuthChallengeInfo& auth_info, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 510 | AuthCallback callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 511 | AuthCredentials* credentials) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 512 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 513 | // Resets the callbacks and |stage_blocked_for_callback_|. |
| 514 | void Reset(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 515 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 516 | // Checks whether we should block in |stage|. If yes, returns an error code |
| 517 | // and optionally sets up callback based on |block_mode_|. If no, returns OK. |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 518 | int MaybeBlockStage(Stage stage, CompletionOnceCallback callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 519 | |
| 520 | // Configuration parameters, can be adjusted by public methods: |
| 521 | const BlockMode block_mode_; |
| 522 | |
| 523 | // Values returned on blocking stages when mode is SYNCHRONOUS or |
| 524 | // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING. |
| 525 | int retval_; // To be returned in non-auth stages. |
| 526 | AuthRequiredResponse auth_retval_; |
| 527 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 528 | GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 529 | int block_on_; // Bit mask: in which stages to block. |
| 530 | |
| 531 | // |auth_credentials_| will be copied to |*target_auth_credential_| on |
| 532 | // callback. |
| 533 | AuthCredentials auth_credentials_; |
| 534 | AuthCredentials* target_auth_credentials_; |
| 535 | |
| 536 | // Internal variables, not set by not the user: |
| 537 | // Last blocked stage waiting for user callback (unused if |block_mode_| != |
| 538 | // USER_CALLBACK). |
| 539 | Stage stage_blocked_for_callback_; |
| 540 | |
| 541 | // Callback objects stored during blocking stages. |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 542 | CompletionOnceCallback callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 543 | AuthCallback auth_callback_; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 544 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 545 | // Closure to run to exit RunUntilBlocked(). |
| 546 | base::OnceClosure on_blocked_; |
| 547 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 548 | base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; |
| 549 | |
| 550 | DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 551 | }; |
| 552 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 553 | BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode) |
| 554 | : block_mode_(block_mode), |
| 555 | retval_(OK), |
| 556 | auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION), |
| 557 | block_on_(0), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 558 | target_auth_credentials_(nullptr), |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 559 | stage_blocked_for_callback_(NOT_BLOCKED), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 560 | weak_factory_(this) {} |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 561 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 562 | void BlockingNetworkDelegate::RunUntilBlocked() { |
| 563 | base::RunLoop run_loop; |
| 564 | on_blocked_ = run_loop.QuitClosure(); |
| 565 | run_loop.Run(); |
| 566 | } |
| 567 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 568 | void BlockingNetworkDelegate::DoCallback(int response) { |
| 569 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 570 | ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 571 | ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 572 | CompletionOnceCallback callback = std::move(callback_); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 573 | Reset(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 574 | |
| 575 | // |callback| may trigger completion of a request, so post it as a task, so |
| 576 | // it will run under a subsequent TestDelegate::RunUntilComplete() loop. |
| 577 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 578 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::RunCallback, |
| 579 | weak_factory_.GetWeakPtr(), response, |
| 580 | std::move(callback))); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | void BlockingNetworkDelegate::DoAuthCallback( |
| 584 | NetworkDelegate::AuthRequiredResponse response) { |
| 585 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 586 | ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 587 | AuthCallback auth_callback = std::move(auth_callback_); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 588 | Reset(); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 589 | RunAuthCallback(response, std::move(auth_callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 590 | } |
| 591 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 592 | void BlockingNetworkDelegate::OnBlocked() { |
| 593 | // If this fails due to |on_blocked_| being null then OnBlocked() was run by |
| 594 | // a RunLoop other than RunUntilBlocked(), indicating a bug in the calling |
| 595 | // test. |
| 596 | std::move(on_blocked_).Run(); |
| 597 | } |
| 598 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 599 | void BlockingNetworkDelegate::RunCallback(int response, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 600 | CompletionOnceCallback callback) { |
| 601 | std::move(callback).Run(response); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 605 | AuthCallback callback) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 606 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 607 | ASSERT_TRUE(target_auth_credentials_ != nullptr); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 608 | *target_auth_credentials_ = auth_credentials_; |
| 609 | } |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 610 | std::move(callback).Run(response); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 611 | } |
| 612 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 613 | int BlockingNetworkDelegate::OnBeforeURLRequest(URLRequest* request, |
| 614 | CompletionOnceCallback callback, |
| 615 | GURL* new_url) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 616 | if (redirect_url_ == request->url()) |
| 617 | return OK; // We've already seen this request and redirected elsewhere. |
| 618 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 619 | // TestNetworkDelegate always completes synchronously. |
| 620 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeURLRequest( |
| 621 | request, base::NullCallback(), new_url)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 622 | |
| 623 | if (!redirect_url_.is_empty()) |
| 624 | *new_url = redirect_url_; |
| 625 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 626 | return MaybeBlockStage(ON_BEFORE_URL_REQUEST, std::move(callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 627 | } |
| 628 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 629 | int BlockingNetworkDelegate::OnBeforeStartTransaction( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 630 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 631 | CompletionOnceCallback callback, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 632 | HttpRequestHeaders* headers) { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 633 | // TestNetworkDelegate always completes synchronously. |
| 634 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeStartTransaction( |
| 635 | request, base::NullCallback(), headers)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 636 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 637 | return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, std::move(callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | int BlockingNetworkDelegate::OnHeadersReceived( |
| 641 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 642 | CompletionOnceCallback callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 643 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 644 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
| 645 | GURL* allowed_unsafe_redirect_url) { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 646 | // TestNetworkDelegate always completes synchronously. |
| 647 | CHECK_NE(ERR_IO_PENDING, |
| 648 | TestNetworkDelegate::OnHeadersReceived( |
| 649 | request, base::NullCallback(), original_response_headers, |
| 650 | override_response_headers, allowed_unsafe_redirect_url)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 651 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 652 | return MaybeBlockStage(ON_HEADERS_RECEIVED, std::move(callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired( |
| 656 | URLRequest* request, |
| 657 | const AuthChallengeInfo& auth_info, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 658 | AuthCallback callback, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 659 | AuthCredentials* credentials) { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 660 | // TestNetworkDelegate always completes synchronously. |
| 661 | CHECK_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, |
| 662 | TestNetworkDelegate::OnAuthRequired( |
| 663 | request, auth_info, base::NullCallback(), credentials)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 664 | // Check that the user has provided callback for the previous blocked stage. |
| 665 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 666 | |
| 667 | if ((block_on_ & ON_AUTH_REQUIRED) == 0) { |
| 668 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 669 | } |
| 670 | |
| 671 | target_auth_credentials_ = credentials; |
| 672 | |
| 673 | switch (block_mode_) { |
| 674 | case SYNCHRONOUS: |
| 675 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) |
| 676 | *target_auth_credentials_ = auth_credentials_; |
| 677 | return auth_retval_; |
| 678 | |
| 679 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 680 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 681 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::RunAuthCallback, |
| 682 | weak_factory_.GetWeakPtr(), auth_retval_, |
| 683 | std::move(callback))); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 684 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 685 | |
| 686 | case USER_CALLBACK: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 687 | auth_callback_ = std::move(callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 688 | stage_blocked_for_callback_ = ON_AUTH_REQUIRED; |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 689 | // We may reach here via a callback prior to RunUntilBlocked(), so post |
| 690 | // a task to fetch and run the |on_blocked_| closure. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 691 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 692 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::OnBlocked, |
| 693 | weak_factory_.GetWeakPtr())); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 694 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 695 | } |
| 696 | NOTREACHED(); |
| 697 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value. |
| 698 | } |
| 699 | |
| 700 | void BlockingNetworkDelegate::Reset() { |
| 701 | EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 702 | stage_blocked_for_callback_ = NOT_BLOCKED; |
| 703 | callback_.Reset(); |
| 704 | auth_callback_.Reset(); |
| 705 | } |
| 706 | |
| 707 | int BlockingNetworkDelegate::MaybeBlockStage( |
| 708 | BlockingNetworkDelegate::Stage stage, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 709 | CompletionOnceCallback callback) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 710 | // Check that the user has provided callback for the previous blocked stage. |
| 711 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 712 | |
| 713 | if ((block_on_ & stage) == 0) { |
| 714 | return OK; |
| 715 | } |
| 716 | |
| 717 | switch (block_mode_) { |
| 718 | case SYNCHRONOUS: |
| 719 | EXPECT_NE(OK, retval_); |
| 720 | return retval_; |
| 721 | |
| 722 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 723 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 724 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::RunCallback, |
| 725 | weak_factory_.GetWeakPtr(), retval_, |
| 726 | std::move(callback))); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 727 | return ERR_IO_PENDING; |
| 728 | |
| 729 | case USER_CALLBACK: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 730 | callback_ = std::move(callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 731 | stage_blocked_for_callback_ = stage; |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 732 | // We may reach here via a callback prior to RunUntilBlocked(), so post |
| 733 | // a task to fetch and run the |on_blocked_| closure. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 734 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 735 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::OnBlocked, |
| 736 | weak_factory_.GetWeakPtr())); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 737 | return ERR_IO_PENDING; |
| 738 | } |
| 739 | NOTREACHED(); |
| 740 | return 0; |
| 741 | } |
| 742 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 743 | class TestURLRequestContextWithProxy : public TestURLRequestContext { |
| 744 | public: |
| 745 | // Does not own |delegate|. |
| 746 | TestURLRequestContextWithProxy(const std::string& proxy, |
| 747 | NetworkDelegate* delegate) |
| 748 | : TestURLRequestContext(true) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 749 | context_storage_.set_proxy_resolution_service( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 750 | ProxyResolutionService::CreateFixed(proxy, |
| 751 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 752 | set_network_delegate(delegate); |
| 753 | Init(); |
| 754 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 755 | ~TestURLRequestContextWithProxy() override = default; |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 756 | }; |
| 757 | |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 758 | // A mock ReportSenderInterface that just remembers the latest report |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 759 | // URI and report to be sent. |
| 760 | class MockCertificateReportSender |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 761 | : public TransportSecurityState::ReportSenderInterface { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 762 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 763 | MockCertificateReportSender() = default; |
| 764 | ~MockCertificateReportSender() override = default; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 765 | |
meacer | 5d4dc5a | 2017-04-27 20:37:48 | [diff] [blame] | 766 | void Send(const GURL& report_uri, |
| 767 | base::StringPiece content_type, |
| 768 | base::StringPiece report, |
| 769 | const base::Callback<void()>& success_callback, |
| 770 | const base::Callback<void(const GURL&, int, int)>& error_callback) |
| 771 | override { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 772 | latest_report_uri_ = report_uri; |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 773 | report.CopyToString(&latest_report_); |
| 774 | content_type.CopyToString(&latest_content_type_); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 775 | } |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 776 | const GURL& latest_report_uri() { return latest_report_uri_; } |
| 777 | const std::string& latest_report() { return latest_report_; } |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 778 | const std::string& latest_content_type() { return latest_content_type_; } |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 779 | |
| 780 | private: |
| 781 | GURL latest_report_uri_; |
| 782 | std::string latest_report_; |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 783 | std::string latest_content_type_; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 784 | }; |
| 785 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 786 | // OCSPErrorTestDelegate caches the SSLInfo passed to OnSSLCertificateError. |
| 787 | // This is needed because after the certificate failure, the URLRequest will |
| 788 | // retry the connection, and return a partial SSLInfo with a cached cert status. |
| 789 | // The partial SSLInfo does not have the OCSP information filled out. |
| 790 | class OCSPErrorTestDelegate : public TestDelegate { |
| 791 | public: |
| 792 | void OnSSLCertificateError(URLRequest* request, |
| 793 | const SSLInfo& ssl_info, |
| 794 | bool fatal) override { |
| 795 | ssl_info_ = ssl_info; |
| 796 | on_ssl_certificate_error_called_ = true; |
| 797 | TestDelegate::OnSSLCertificateError(request, ssl_info, fatal); |
| 798 | } |
| 799 | |
| 800 | bool on_ssl_certificate_error_called() { |
| 801 | return on_ssl_certificate_error_called_; |
| 802 | } |
| 803 | |
| 804 | SSLInfo ssl_info() { return ssl_info_; } |
| 805 | |
| 806 | private: |
| 807 | bool on_ssl_certificate_error_called_ = false; |
| 808 | SSLInfo ssl_info_; |
| 809 | }; |
| 810 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 811 | } // namespace |
| 812 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 813 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 814 | class URLRequestTest : public PlatformTest, public WithScopedTaskEnvironment { |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 815 | public: |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 816 | URLRequestTest() |
| 817 | : default_context_(std::make_unique<TestURLRequestContext>(true)) { |
| 818 | default_context_->set_network_delegate(&default_network_delegate_); |
| 819 | default_context_->set_net_log(&net_log_); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 820 | job_factory_impl_ = new URLRequestJobFactoryImpl(); |
| 821 | job_factory_.reset(job_factory_impl_); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 822 | } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 823 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 824 | ~URLRequestTest() override { |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 825 | // URLRequestJobs may post clean-up tasks on destruction. |
| 826 | base::RunLoop().RunUntilIdle(); |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 827 | |
| 828 | SetTransportSecurityStateSourceForTesting(nullptr); |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 829 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 830 | |
dcheng | 2339883c | 2014-12-23 00:23:05 | [diff] [blame] | 831 | void SetUp() override { |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 832 | SetUpFactory(); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 833 | default_context_->set_job_factory(job_factory_.get()); |
| 834 | default_context_->Init(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 835 | PlatformTest::SetUp(); |
| 836 | } |
| 837 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 838 | void TearDown() override { default_context_.reset(); } |
| 839 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 840 | virtual void SetUpFactory() { |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 841 | job_factory_impl_->SetProtocolHandler( |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 842 | "data", std::make_unique<DataProtocolHandler>()); |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 843 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 844 | job_factory_impl_->SetProtocolHandler( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 845 | "file", std::make_unique<FileProtocolHandler>( |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 846 | base::ThreadTaskRunnerHandle::Get())); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 847 | #endif |
| 848 | } |
| 849 | |
| 850 | TestNetworkDelegate* default_network_delegate() { |
| 851 | return &default_network_delegate_; |
| 852 | } |
| 853 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 854 | TestURLRequestContext& default_context() const { return *default_context_; } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 855 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 856 | // Adds the TestJobInterceptor to the default context. |
| 857 | TestJobInterceptor* AddTestInterceptor() { |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 858 | TestJobInterceptor* protocol_handler_ = new TestJobInterceptor(); |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 859 | job_factory_impl_->SetProtocolHandler("http", nullptr); |
| 860 | job_factory_impl_->SetProtocolHandler("http", |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 861 | base::WrapUnique(protocol_handler_)); |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 862 | return protocol_handler_; |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 863 | } |
| 864 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 865 | // Creates a temp test file and writes |data| to the file. The file will be |
| 866 | // deleted after the test completes. |
| 867 | void CreateTestFile(const char* data, |
| 868 | size_t data_size, |
| 869 | base::FilePath* test_file) { |
| 870 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 871 | // Get an absolute path since |temp_dir| can contain a symbolic link. As of |
| 872 | // now, Mac and Android bots return a path with a symbolic link. |
| 873 | base::FilePath absolute_temp_dir = |
| 874 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
| 875 | |
| 876 | ASSERT_TRUE(base::CreateTemporaryFileInDir(absolute_temp_dir, test_file)); |
| 877 | ASSERT_EQ(static_cast<int>(data_size), |
| 878 | base::WriteFile(*test_file, data, data_size)); |
| 879 | } |
| 880 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 881 | protected: |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 882 | TestNetLog net_log_; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 883 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 884 | URLRequestJobFactoryImpl* job_factory_impl_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 885 | std::unique_ptr<URLRequestJobFactory> job_factory_; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 886 | std::unique_ptr<TestURLRequestContext> default_context_; |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 887 | base::ScopedTempDir temp_dir_; |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 888 | }; |
| 889 | |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 890 | // This NetworkDelegate is picky about what files are accessible. Only |
| 891 | // whitelisted files are allowed. |
| 892 | class CookieBlockingNetworkDelegate : public TestNetworkDelegate { |
| 893 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 894 | CookieBlockingNetworkDelegate() = default; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 895 | |
| 896 | // Adds |directory| to the access white list. |
| 897 | void AddToWhitelist(const base::FilePath& directory) { |
| 898 | whitelist_.insert(directory); |
| 899 | } |
| 900 | |
| 901 | private: |
| 902 | // Returns true if |path| matches the white list. |
| 903 | bool OnCanAccessFileInternal(const base::FilePath& path) const { |
| 904 | for (const auto& directory : whitelist_) { |
| 905 | if (directory == path || directory.IsParent(path)) |
| 906 | return true; |
| 907 | } |
| 908 | return false; |
| 909 | } |
| 910 | |
| 911 | // Returns true only if both |original_path| and |absolute_path| match the |
| 912 | // white list. |
| 913 | bool OnCanAccessFile(const URLRequest& request, |
| 914 | const base::FilePath& original_path, |
| 915 | const base::FilePath& absolute_path) const override { |
| 916 | return (OnCanAccessFileInternal(original_path) && |
| 917 | OnCanAccessFileInternal(absolute_path)); |
| 918 | } |
| 919 | |
| 920 | std::set<base::FilePath> whitelist_; |
| 921 | |
| 922 | DISALLOW_COPY_AND_ASSIGN(CookieBlockingNetworkDelegate); |
| 923 | }; |
| 924 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 925 | TEST_F(URLRequestTest, AboutBlankTest) { |
| 926 | TestDelegate d; |
| 927 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 928 | std::unique_ptr<URLRequest> r( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 929 | default_context().CreateRequest(GURL("about:blank"), DEFAULT_PRIORITY, |
| 930 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 931 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 932 | r->Start(); |
| 933 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 934 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 935 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 936 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 937 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 938 | EXPECT_FALSE(d.received_data_before_response()); |
| 939 | EXPECT_EQ(d.bytes_received(), 0); |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 940 | EXPECT_TRUE(r->GetResponseRemoteEndpoint().address().empty()); |
| 941 | EXPECT_EQ(0, r->GetResponseRemoteEndpoint().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 942 | |
| 943 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 944 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | |
| 948 | TEST_F(URLRequestTest, DataURLImageTest) { |
| 949 | TestDelegate d; |
| 950 | { |
| 951 | // Use our nice little Chrome logo. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 952 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 953 | GURL("data:image/png;base64," |
| 954 | "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUB" |
| 955 | "BjG3w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQ" |
| 956 | "xIEVxitD5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI" |
| 957 | "5MpCVdW0gO7tvNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKz" |
| 958 | "Mx1+fg9bAgK6zHq9cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7Ck" |
| 959 | "IMDxQpF7r/MWq12UctI1dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0" |
| 960 | "WYUqt57yWf2McHTObYPbVD+ZwbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2H" |
| 961 | "QQcm43XwmtoYM6vVKleh0uoWvnzW3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nk" |
| 962 | "DFHF9ZS+uYVjRUasMeHUmyLYtcklTvzWGFZnNOXczThvpKIzjcahSqIzkvDLayDq6" |
| 963 | "D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTbxXX1T5xsV9tm9r4TQwHLiZw/pdDZJ" |
| 964 | "ea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5MrVvMzNaI3+ERHfrFzPKQukrQ" |
| 965 | "GI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gVMd6q5c8GdosynKmSeRuGz" |
| 966 | "pjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq/uxxZKzNiZFGD0wRC" |
| 967 | "3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7FUx1qLnV7MGF40" |
| 968 | "smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufBJbsysjjW4" |
| 969 | "kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJMnlm2O" |
| 970 | "34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfmm" |
| 971 | "oRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV" |
| 972 | "5EnhORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 973 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 974 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 975 | r->Start(); |
| 976 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 977 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 978 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 979 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 980 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 981 | EXPECT_FALSE(d.received_data_before_response()); |
| 982 | EXPECT_EQ(d.bytes_received(), 911); |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 983 | EXPECT_TRUE(r->GetResponseRemoteEndpoint().address().empty()); |
| 984 | EXPECT_EQ(0, r->GetResponseRemoteEndpoint().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 985 | |
| 986 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 987 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 988 | } |
| 989 | } |
| 990 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 991 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 992 | TEST_F(URLRequestTest, FileTest) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 993 | const char kTestFileContent[] = "Hello"; |
| 994 | base::FilePath test_file; |
| 995 | ASSERT_NO_FATAL_FAILURE( |
| 996 | CreateTestFile(kTestFileContent, sizeof(kTestFileContent), &test_file)); |
kraush | 5a64582 | 2016-04-07 18:35:04 | [diff] [blame] | 997 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 998 | GURL test_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 999 | |
| 1000 | TestDelegate d; |
| 1001 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1002 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1003 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1004 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1005 | r->Start(); |
| 1006 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1007 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1008 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1009 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1010 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1011 | EXPECT_EQ(1, d.response_started_count()); |
| 1012 | EXPECT_FALSE(d.received_data_before_response()); |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1013 | EXPECT_EQ(d.bytes_received(), static_cast<int>(sizeof(kTestFileContent))); |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 1014 | EXPECT_TRUE(r->GetResponseRemoteEndpoint().address().empty()); |
| 1015 | EXPECT_EQ(0, r->GetResponseRemoteEndpoint().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 1016 | |
| 1017 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1018 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1022 | TEST_F(URLRequestTest, FileTestCancel) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1023 | const char kTestFileContent[] = "Hello"; |
| 1024 | base::FilePath test_file; |
| 1025 | ASSERT_NO_FATAL_FAILURE( |
| 1026 | CreateTestFile(kTestFileContent, sizeof(kTestFileContent), &test_file)); |
| 1027 | |
| 1028 | GURL test_url = FilePathToFileURL(test_file); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1029 | |
| 1030 | TestDelegate d; |
| 1031 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1032 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1033 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1034 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1035 | r->Start(); |
| 1036 | EXPECT_TRUE(r->is_pending()); |
| 1037 | r->Cancel(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1038 | } |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 1039 | // Async cancellation should be safe even when URLRequest has been already |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1040 | // destroyed. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1041 | base::RunLoop().RunUntilIdle(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1042 | } |
| 1043 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1044 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 1045 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1046 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1047 | FillBuffer(buffer.get(), buffer_size); |
| 1048 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1049 | base::FilePath test_file; |
| 1050 | ASSERT_NO_FATAL_FAILURE( |
| 1051 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1052 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1053 | |
| 1054 | const size_t first_byte_position = 500; |
| 1055 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 1056 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 1057 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 1058 | buffer.get() + last_byte_position + 1); |
| 1059 | |
| 1060 | TestDelegate d; |
| 1061 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1062 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1063 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1064 | |
| 1065 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1066 | headers.SetHeader( |
| 1067 | HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1068 | HttpByteRange::Bounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1069 | first_byte_position, last_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1070 | r->SetExtraRequestHeaders(headers); |
| 1071 | r->Start(); |
| 1072 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1073 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1074 | d.RunUntilComplete(); |
| 1075 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1076 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1077 | EXPECT_EQ(1, d.response_started_count()); |
| 1078 | EXPECT_FALSE(d.received_data_before_response()); |
| 1079 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 1080 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 1081 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 1082 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 1086 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1087 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1088 | FillBuffer(buffer.get(), buffer_size); |
| 1089 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1090 | base::FilePath test_file; |
| 1091 | ASSERT_NO_FATAL_FAILURE( |
| 1092 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1093 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1094 | |
| 1095 | const size_t first_byte_position = 500; |
| 1096 | const size_t last_byte_position = buffer_size - 1; |
| 1097 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 1098 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 1099 | buffer.get() + last_byte_position + 1); |
| 1100 | |
| 1101 | TestDelegate d; |
| 1102 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1103 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1104 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1105 | |
| 1106 | HttpRequestHeaders headers; |
| 1107 | headers.SetHeader(HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1108 | HttpByteRange::RightUnbounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1109 | first_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1110 | r->SetExtraRequestHeaders(headers); |
| 1111 | r->Start(); |
| 1112 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1113 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1114 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1115 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1116 | EXPECT_EQ(1, d.response_started_count()); |
| 1117 | EXPECT_FALSE(d.received_data_before_response()); |
| 1118 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 1119 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 1120 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 1121 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 1125 | const size_t buffer_size = 400000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1126 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1127 | FillBuffer(buffer.get(), buffer_size); |
| 1128 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1129 | base::FilePath test_file; |
| 1130 | ASSERT_NO_FATAL_FAILURE( |
| 1131 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1132 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1133 | |
| 1134 | TestDelegate d; |
| 1135 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1136 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1137 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1138 | |
| 1139 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1140 | headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1141 | r->SetExtraRequestHeaders(headers); |
| 1142 | r->Start(); |
| 1143 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1144 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1145 | d.RunUntilComplete(); |
| 1146 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1147 | EXPECT_TRUE(d.request_failed()); |
| 1148 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1149 | } |
| 1150 | |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1151 | TEST_F(URLRequestTest, AllowFileURLs) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1152 | std::string test_data("monkey"); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1153 | base::FilePath test_file; |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1154 | ASSERT_NO_FATAL_FAILURE( |
| 1155 | CreateTestFile(test_data.data(), test_data.size(), &test_file)); |
| 1156 | |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1157 | // The directory part of the path returned from CreateTemporaryFileInDir() |
| 1158 | // can be slightly different from |absolute_temp_dir| on Windows. |
| 1159 | // Example: C:\\Users\\CHROME~2 -> C:\\Users\\chrome-bot |
| 1160 | // Hence the test should use the directory name of |test_file|, rather than |
| 1161 | // |absolute_temp_dir|, for whitelisting. |
| 1162 | base::FilePath real_temp_dir = test_file.DirName(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1163 | GURL test_file_url = FilePathToFileURL(test_file); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1164 | { |
| 1165 | TestDelegate d; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1166 | CookieBlockingNetworkDelegate network_delegate; |
| 1167 | network_delegate.AddToWhitelist(real_temp_dir); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1168 | default_context().set_network_delegate(&network_delegate); |
| 1169 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1170 | test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1171 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1172 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1173 | // This should be allowed as the file path is whitelisted. |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1174 | EXPECT_FALSE(d.request_failed()); |
| 1175 | EXPECT_EQ(test_data, d.data_received()); |
| 1176 | } |
| 1177 | |
| 1178 | { |
| 1179 | TestDelegate d; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1180 | CookieBlockingNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1181 | default_context().set_network_delegate(&network_delegate); |
| 1182 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1183 | test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1184 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1185 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1186 | // This should be rejected as the file path is not whitelisted. |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1187 | EXPECT_TRUE(d.request_failed()); |
| 1188 | EXPECT_EQ("", d.data_received()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1189 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1190 | } |
| 1191 | } |
| 1192 | |
Wez | 3553913 | 2018-07-17 11:26:05 | [diff] [blame] | 1193 | #if defined(OS_POSIX) // Because of symbolic links. |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1194 | |
| 1195 | TEST_F(URLRequestTest, SymlinksToFiles) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1196 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1197 | // Get an absolute path since temp_dir can contain a symbolic link. |
| 1198 | base::FilePath absolute_temp_dir = |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1199 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1200 | |
| 1201 | // Create a good directory (will be whitelisted) and a good file. |
| 1202 | base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); |
| 1203 | ASSERT_TRUE(base::CreateDirectory(good_dir)); |
| 1204 | base::FilePath good_file; |
| 1205 | ASSERT_TRUE(base::CreateTemporaryFileInDir(good_dir, &good_file)); |
| 1206 | std::string good_data("good"); |
| 1207 | base::WriteFile(good_file, good_data.data(), good_data.size()); |
| 1208 | // See the comment in AllowFileURLs() for why this is done. |
| 1209 | base::FilePath real_good_dir = good_file.DirName(); |
| 1210 | |
| 1211 | // Create a bad directory (will not be whitelisted) and a bad file. |
| 1212 | base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); |
| 1213 | ASSERT_TRUE(base::CreateDirectory(bad_dir)); |
| 1214 | base::FilePath bad_file; |
| 1215 | ASSERT_TRUE(base::CreateTemporaryFileInDir(bad_dir, &bad_file)); |
| 1216 | std::string bad_data("bad"); |
| 1217 | base::WriteFile(bad_file, bad_data.data(), bad_data.size()); |
| 1218 | |
| 1219 | // This symlink will point to the good file. Access to the symlink will be |
| 1220 | // allowed as both the symlink and the destination file are in the same |
| 1221 | // good directory. |
| 1222 | base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); |
| 1223 | ASSERT_TRUE(base::CreateSymbolicLink(good_file, good_symlink)); |
| 1224 | GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1225 | // This symlink will point to the bad file. Even though the symlink is in |
| 1226 | // the good directory, access to the symlink will be rejected since it |
| 1227 | // points to the bad file. |
| 1228 | base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); |
| 1229 | ASSERT_TRUE(base::CreateSymbolicLink(bad_file, bad_symlink)); |
| 1230 | GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1231 | |
| 1232 | CookieBlockingNetworkDelegate network_delegate; |
| 1233 | network_delegate.AddToWhitelist(real_good_dir); |
| 1234 | { |
| 1235 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1236 | default_context().set_network_delegate(&network_delegate); |
| 1237 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1238 | good_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1239 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1240 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1241 | // good_file_url should be allowed. |
| 1242 | EXPECT_FALSE(d.request_failed()); |
| 1243 | EXPECT_EQ(good_data, d.data_received()); |
| 1244 | } |
| 1245 | |
| 1246 | { |
| 1247 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1248 | default_context().set_network_delegate(&network_delegate); |
| 1249 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1250 | bad_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1251 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1252 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1253 | // bad_file_url should be rejected. |
| 1254 | EXPECT_TRUE(d.request_failed()); |
| 1255 | EXPECT_EQ("", d.data_received()); |
| 1256 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | TEST_F(URLRequestTest, SymlinksToDirs) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1261 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1262 | // Get an absolute path since temp_dir can contain a symbolic link. |
| 1263 | base::FilePath absolute_temp_dir = |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1264 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1265 | |
| 1266 | // Create a good directory (will be whitelisted). |
| 1267 | base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); |
| 1268 | ASSERT_TRUE(base::CreateDirectory(good_dir)); |
| 1269 | |
| 1270 | // Create a bad directory (will not be whitelisted). |
| 1271 | base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); |
| 1272 | ASSERT_TRUE(base::CreateDirectory(bad_dir)); |
| 1273 | |
| 1274 | // This symlink will point to the good directory. Access to the symlink |
| 1275 | // will be allowed as the symlink is in the good dir that'll be white |
| 1276 | // listed. |
| 1277 | base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); |
| 1278 | ASSERT_TRUE(base::CreateSymbolicLink(good_dir, good_symlink)); |
| 1279 | GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1280 | // This symlink will point to the bad directory. Even though the symlink is |
| 1281 | // in the good directory, access to the symlink will be rejected since it |
| 1282 | // points to the bad directory. |
| 1283 | base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); |
| 1284 | ASSERT_TRUE(base::CreateSymbolicLink(bad_dir, bad_symlink)); |
| 1285 | GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1286 | |
| 1287 | CookieBlockingNetworkDelegate network_delegate; |
| 1288 | network_delegate.AddToWhitelist(good_dir); |
| 1289 | { |
| 1290 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1291 | default_context().set_network_delegate(&network_delegate); |
| 1292 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1293 | good_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1294 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1295 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1296 | // good_file_url should be allowed. |
| 1297 | EXPECT_FALSE(d.request_failed()); |
| 1298 | ASSERT_NE(d.data_received().find("good_symlink"), std::string::npos); |
| 1299 | } |
| 1300 | |
| 1301 | { |
| 1302 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1303 | default_context().set_network_delegate(&network_delegate); |
| 1304 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1305 | bad_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1306 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1307 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1308 | // bad_file_url should be rejected. |
| 1309 | EXPECT_TRUE(d.request_failed()); |
| 1310 | EXPECT_EQ("", d.data_received()); |
| 1311 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
| 1312 | } |
| 1313 | } |
| 1314 | |
Wez | 3553913 | 2018-07-17 11:26:05 | [diff] [blame] | 1315 | #endif // defined(OS_POSIX) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1316 | |
| 1317 | TEST_F(URLRequestTest, FileDirCancelTest) { |
| 1318 | // Put in mock resource provider. |
| 1319 | NetModule::SetResourceProvider(TestNetResourceProvider); |
| 1320 | |
| 1321 | TestDelegate d; |
| 1322 | { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1323 | base::FilePath file_path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1324 | base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1325 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 1326 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
| 1327 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1328 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1329 | FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d, |
| 1330 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1331 | req->Start(); |
| 1332 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1333 | |
| 1334 | d.set_cancel_in_received_data_pending(true); |
| 1335 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1336 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | // Take out mock resource provider. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 1340 | NetModule::SetResourceProvider(nullptr); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1341 | } |
| 1342 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1343 | TEST_F(URLRequestTest, FileDirOutputSanity) { |
| 1344 | // Verify the general sanity of the the output of the file: |
| 1345 | // directory lister by checking for the output of a known existing |
| 1346 | // file. |
| 1347 | const char sentinel_name[] = "filedir-sentinel"; |
| 1348 | |
| 1349 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1350 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1351 | path = path.Append(kTestFilePath); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1352 | |
| 1353 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1354 | std::unique_ptr<URLRequest> req( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1355 | default_context().CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 1356 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1357 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1358 | d.RunUntilComplete(); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1359 | |
| 1360 | // Generate entry for the sentinel file. |
| 1361 | base::FilePath sentinel_path = path.AppendASCII(sentinel_name); |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 1362 | base::File::Info info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 1363 | EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1364 | EXPECT_GT(info.size, 0); |
| 1365 | std::string sentinel_output = GetDirectoryListingEntry( |
| 1366 | base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1367 | std::string(sentinel_name), false /* is_dir */, info.size, |
| 1368 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1369 | info.last_modified); |
| 1370 | |
| 1371 | ASSERT_LT(0, d.bytes_received()); |
| 1372 | ASSERT_FALSE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1373 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1374 | // Check for the entry generated for the "sentinel" file. |
| 1375 | const std::string& data = d.data_received(); |
| 1376 | ASSERT_NE(data.find(sentinel_output), std::string::npos); |
| 1377 | } |
| 1378 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1379 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 1380 | // There is an implicit redirect when loading a file path that matches a |
| 1381 | // directory and does not end with a slash. Ensure that following such |
| 1382 | // redirects does not crash. See http://crbug.com/18686. |
| 1383 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1384 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1385 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1386 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1387 | |
| 1388 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1389 | std::unique_ptr<URLRequest> req( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1390 | default_context().CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 1391 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1392 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1393 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1394 | |
| 1395 | ASSERT_EQ(1, d.received_redirect_count()); |
| 1396 | ASSERT_LT(0, d.bytes_received()); |
| 1397 | ASSERT_FALSE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1398 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | #if defined(OS_WIN) |
| 1402 | // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
| 1403 | TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
| 1404 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1405 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1406 | GURL("file:///"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1407 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1408 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1409 | |
| 1410 | ASSERT_EQ(1, d.received_redirect_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1411 | EXPECT_NE(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1412 | } |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1413 | #endif // defined(OS_WIN) |
| 1414 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 1415 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1416 | |
| 1417 | TEST_F(URLRequestTest, InvalidUrlTest) { |
| 1418 | TestDelegate d; |
| 1419 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1420 | std::unique_ptr<URLRequest> r( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1421 | default_context().CreateRequest(GURL("invalid url"), DEFAULT_PRIORITY, |
| 1422 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1423 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1424 | r->Start(); |
| 1425 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1426 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1427 | d.RunUntilComplete(); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1428 | EXPECT_TRUE(d.request_failed()); |
| 1429 | } |
| 1430 | } |
| 1431 | |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1432 | TEST_F(URLRequestTest, InvalidReferrerTest) { |
| 1433 | TestURLRequestContext context; |
| 1434 | TestNetworkDelegate network_delegate; |
| 1435 | network_delegate.set_cancel_request_with_policy_violating_referrer(true); |
| 1436 | context.set_network_delegate(&network_delegate); |
| 1437 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1438 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1439 | context.CreateRequest(GURL("http://localhost/"), DEFAULT_PRIORITY, &d, |
| 1440 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1441 | req->SetReferrer("https://somewhere.com/"); |
| 1442 | |
| 1443 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1444 | d.RunUntilComplete(); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1445 | EXPECT_TRUE(d.request_failed()); |
| 1446 | } |
| 1447 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1448 | #if defined(OS_WIN) |
| 1449 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
| 1450 | base::FilePath app_path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1451 | base::PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1452 | app_path = app_path.Append(kTestFilePath); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1453 | app_path = app_path.AppendASCII("with-headers.html"); |
| 1454 | |
jdoerrie | 6312bf6 | 2019-02-01 22:03:42 | [diff] [blame] | 1455 | base::string16 lnk_path = app_path.value() + FILE_PATH_LITERAL(".lnk"); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1456 | |
| 1457 | base::win::ScopedCOMInitializer com_initializer; |
| 1458 | |
| 1459 | // Temporarily create a shortcut for test |
| 1460 | { |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 1461 | Microsoft::WRL::ComPtr<IShellLink> shell; |
robliao | eb9bfd64 | 2017-05-18 17:35:16 | [diff] [blame] | 1462 | ASSERT_TRUE(SUCCEEDED(::CoCreateInstance( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 1463 | CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shell)))); |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 1464 | Microsoft::WRL::ComPtr<IPersistFile> persist; |
robliao | 310fa98b | 2017-05-11 17:14:00 | [diff] [blame] | 1465 | ASSERT_TRUE(SUCCEEDED(shell.CopyTo(persist.GetAddressOf()))); |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1466 | EXPECT_TRUE(SUCCEEDED(shell->SetPath(base::as_wcstr(app_path.value())))); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1467 | EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1468 | EXPECT_TRUE(SUCCEEDED(persist->Save(base::as_wcstr(lnk_path), TRUE))); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | TestDelegate d; |
| 1472 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1473 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1474 | FilePathToFileURL(base::FilePath(lnk_path)), DEFAULT_PRIORITY, &d, |
| 1475 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1476 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1477 | r->Start(); |
| 1478 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1479 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1480 | d.RunUntilComplete(); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1481 | |
| 1482 | WIN32_FILE_ATTRIBUTE_DATA data; |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1483 | GetFileAttributesEx(base::as_wcstr(app_path.value()), GetFileExInfoStandard, |
jdoerrie | 6312bf6 | 2019-02-01 22:03:42 | [diff] [blame] | 1484 | &data); |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1485 | HANDLE file = CreateFile(base::as_wcstr(app_path.value()), GENERIC_READ, |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 1486 | FILE_SHARE_READ, nullptr, OPEN_EXISTING, |
| 1487 | FILE_ATTRIBUTE_NORMAL, nullptr); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1488 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1489 | std::unique_ptr<char[]> buffer(new char[data.nFileSizeLow]); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1490 | DWORD read_size; |
| 1491 | BOOL result; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 1492 | result = |
| 1493 | ReadFile(file, buffer.get(), data.nFileSizeLow, &read_size, nullptr); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1494 | std::string content(buffer.get(), read_size); |
| 1495 | CloseHandle(file); |
| 1496 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1497 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1498 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1499 | EXPECT_EQ(content, d.data_received()); |
| 1500 | } |
| 1501 | |
| 1502 | // Clean the shortcut |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1503 | DeleteFile(base::as_wcstr(lnk_path)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1504 | } |
| 1505 | #endif // defined(OS_WIN) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1506 | |
| 1507 | // Custom URLRequestJobs for use with interceptor tests |
| 1508 | class RestartTestJob : public URLRequestTestJob { |
| 1509 | public: |
| 1510 | RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1511 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1512 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1513 | void StartAsync() override { this->NotifyRestartRequired(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1514 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1515 | ~RestartTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1516 | }; |
| 1517 | |
| 1518 | class CancelTestJob : public URLRequestTestJob { |
| 1519 | public: |
| 1520 | explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1521 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1522 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1523 | void StartAsync() override { request_->Cancel(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1524 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1525 | ~CancelTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1526 | }; |
| 1527 | |
| 1528 | class CancelThenRestartTestJob : public URLRequestTestJob { |
| 1529 | public: |
| 1530 | explicit CancelThenRestartTestJob(URLRequest* request, |
| 1531 | NetworkDelegate* network_delegate) |
| 1532 | : URLRequestTestJob(request, network_delegate, true) { |
| 1533 | } |
| 1534 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1535 | void StartAsync() override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1536 | request_->Cancel(); |
| 1537 | this->NotifyRestartRequired(); |
| 1538 | } |
| 1539 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1540 | ~CancelThenRestartTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1541 | }; |
| 1542 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1543 | // An Interceptor for use with interceptor tests. |
| 1544 | class MockURLRequestInterceptor : public URLRequestInterceptor { |
| 1545 | public: |
| 1546 | // Static getters for canned response header and data strings. |
| 1547 | static std::string ok_data() { |
| 1548 | return URLRequestTestJob::test_data_1(); |
| 1549 | } |
| 1550 | |
| 1551 | static std::string ok_headers() { |
| 1552 | return URLRequestTestJob::test_headers(); |
| 1553 | } |
| 1554 | |
| 1555 | static std::string redirect_data() { |
| 1556 | return std::string(); |
| 1557 | } |
| 1558 | |
| 1559 | static std::string redirect_headers() { |
| 1560 | return URLRequestTestJob::test_redirect_headers(); |
| 1561 | } |
| 1562 | |
| 1563 | static std::string error_data() { |
| 1564 | return std::string("ohhh nooooo mr. bill!"); |
| 1565 | } |
| 1566 | |
| 1567 | static std::string error_headers() { |
| 1568 | return URLRequestTestJob::test_error_headers(); |
| 1569 | } |
| 1570 | |
| 1571 | MockURLRequestInterceptor() |
| 1572 | : intercept_main_request_(false), restart_main_request_(false), |
| 1573 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 1574 | simulate_main_network_error_(false), |
| 1575 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 1576 | intercept_final_response_(false), cancel_final_request_(false), |
| 1577 | use_url_request_http_job_(false), |
| 1578 | did_intercept_main_(false), did_restart_main_(false), |
| 1579 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 1580 | did_simulate_error_main_(false), |
| 1581 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 1582 | did_intercept_final_(false), did_cancel_final_(false) { |
| 1583 | } |
| 1584 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1585 | ~MockURLRequestInterceptor() override = default; |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1586 | |
| 1587 | // URLRequestInterceptor implementation: |
| 1588 | URLRequestJob* MaybeInterceptRequest( |
| 1589 | URLRequest* request, |
| 1590 | NetworkDelegate* network_delegate) const override { |
| 1591 | if (restart_main_request_) { |
| 1592 | restart_main_request_ = false; |
| 1593 | did_restart_main_ = true; |
| 1594 | return new RestartTestJob(request, network_delegate); |
| 1595 | } |
| 1596 | if (cancel_main_request_) { |
| 1597 | cancel_main_request_ = false; |
| 1598 | did_cancel_main_ = true; |
| 1599 | return new CancelTestJob(request, network_delegate); |
| 1600 | } |
| 1601 | if (cancel_then_restart_main_request_) { |
| 1602 | cancel_then_restart_main_request_ = false; |
| 1603 | did_cancel_then_restart_main_ = true; |
| 1604 | return new CancelThenRestartTestJob(request, network_delegate); |
| 1605 | } |
| 1606 | if (simulate_main_network_error_) { |
| 1607 | simulate_main_network_error_ = false; |
| 1608 | did_simulate_error_main_ = true; |
| 1609 | if (use_url_request_http_job_) { |
| 1610 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1611 | } |
| 1612 | // This job will result in error since the requested URL is not one of the |
| 1613 | // URLs supported by these tests. |
| 1614 | return new URLRequestTestJob(request, network_delegate, true); |
| 1615 | } |
| 1616 | if (!intercept_main_request_) |
| 1617 | return nullptr; |
| 1618 | intercept_main_request_ = false; |
| 1619 | did_intercept_main_ = true; |
| 1620 | URLRequestTestJob* job = new URLRequestTestJob(request, |
| 1621 | network_delegate, |
| 1622 | main_headers_, |
| 1623 | main_data_, |
| 1624 | true); |
| 1625 | job->set_load_timing_info(main_request_load_timing_info_); |
| 1626 | return job; |
| 1627 | } |
| 1628 | |
| 1629 | URLRequestJob* MaybeInterceptRedirect(URLRequest* request, |
| 1630 | NetworkDelegate* network_delegate, |
| 1631 | const GURL& location) const override { |
| 1632 | if (cancel_redirect_request_) { |
| 1633 | cancel_redirect_request_ = false; |
| 1634 | did_cancel_redirect_ = true; |
| 1635 | return new CancelTestJob(request, network_delegate); |
| 1636 | } |
| 1637 | if (!intercept_redirect_) |
| 1638 | return nullptr; |
| 1639 | intercept_redirect_ = false; |
| 1640 | did_intercept_redirect_ = true; |
| 1641 | if (use_url_request_http_job_) { |
| 1642 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1643 | } |
| 1644 | return new URLRequestTestJob(request, |
| 1645 | network_delegate, |
| 1646 | redirect_headers_, |
| 1647 | redirect_data_, |
| 1648 | true); |
| 1649 | } |
| 1650 | |
| 1651 | URLRequestJob* MaybeInterceptResponse( |
| 1652 | URLRequest* request, |
| 1653 | NetworkDelegate* network_delegate) const override { |
| 1654 | if (cancel_final_request_) { |
| 1655 | cancel_final_request_ = false; |
| 1656 | did_cancel_final_ = true; |
| 1657 | return new CancelTestJob(request, network_delegate); |
| 1658 | } |
| 1659 | if (!intercept_final_response_) |
| 1660 | return nullptr; |
| 1661 | intercept_final_response_ = false; |
| 1662 | did_intercept_final_ = true; |
| 1663 | if (use_url_request_http_job_) { |
| 1664 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1665 | } |
| 1666 | return new URLRequestTestJob(request, |
| 1667 | network_delegate, |
| 1668 | final_headers_, |
| 1669 | final_data_, |
| 1670 | true); |
| 1671 | } |
| 1672 | |
| 1673 | void set_intercept_main_request(bool intercept_main_request) { |
| 1674 | intercept_main_request_ = intercept_main_request; |
| 1675 | } |
| 1676 | |
| 1677 | void set_main_headers(const std::string& main_headers) { |
| 1678 | main_headers_ = main_headers; |
| 1679 | } |
| 1680 | |
| 1681 | void set_main_data(const std::string& main_data) { |
| 1682 | main_data_ = main_data; |
| 1683 | } |
| 1684 | |
| 1685 | void set_main_request_load_timing_info( |
| 1686 | const LoadTimingInfo& main_request_load_timing_info) { |
| 1687 | main_request_load_timing_info_ = main_request_load_timing_info; |
| 1688 | } |
| 1689 | |
| 1690 | void set_restart_main_request(bool restart_main_request) { |
| 1691 | restart_main_request_ = restart_main_request; |
| 1692 | } |
| 1693 | |
| 1694 | void set_cancel_main_request(bool cancel_main_request) { |
| 1695 | cancel_main_request_ = cancel_main_request; |
| 1696 | } |
| 1697 | |
| 1698 | void set_cancel_then_restart_main_request( |
| 1699 | bool cancel_then_restart_main_request) { |
| 1700 | cancel_then_restart_main_request_ = cancel_then_restart_main_request; |
| 1701 | } |
| 1702 | |
| 1703 | void set_simulate_main_network_error(bool simulate_main_network_error) { |
| 1704 | simulate_main_network_error_ = simulate_main_network_error; |
| 1705 | } |
| 1706 | |
| 1707 | void set_intercept_redirect(bool intercept_redirect) { |
| 1708 | intercept_redirect_ = intercept_redirect; |
| 1709 | } |
| 1710 | |
| 1711 | void set_redirect_headers(const std::string& redirect_headers) { |
| 1712 | redirect_headers_ = redirect_headers; |
| 1713 | } |
| 1714 | |
| 1715 | void set_redirect_data(const std::string& redirect_data) { |
| 1716 | redirect_data_ = redirect_data; |
| 1717 | } |
| 1718 | |
| 1719 | void set_cancel_redirect_request(bool cancel_redirect_request) { |
| 1720 | cancel_redirect_request_ = cancel_redirect_request; |
| 1721 | } |
| 1722 | |
| 1723 | void set_intercept_final_response(bool intercept_final_response) { |
| 1724 | intercept_final_response_ = intercept_final_response; |
| 1725 | } |
| 1726 | |
| 1727 | void set_final_headers(const std::string& final_headers) { |
| 1728 | final_headers_ = final_headers; |
| 1729 | } |
| 1730 | |
| 1731 | void set_final_data(const std::string& final_data) { |
| 1732 | final_data_ = final_data; |
| 1733 | } |
| 1734 | |
| 1735 | void set_cancel_final_request(bool cancel_final_request) { |
| 1736 | cancel_final_request_ = cancel_final_request; |
| 1737 | } |
| 1738 | |
| 1739 | void set_use_url_request_http_job(bool use_url_request_http_job) { |
| 1740 | use_url_request_http_job_ = use_url_request_http_job; |
| 1741 | } |
| 1742 | |
| 1743 | bool did_intercept_main() const { |
| 1744 | return did_intercept_main_; |
| 1745 | } |
| 1746 | |
| 1747 | bool did_restart_main() const { |
| 1748 | return did_restart_main_; |
| 1749 | } |
| 1750 | |
| 1751 | bool did_cancel_main() const { |
| 1752 | return did_cancel_main_; |
| 1753 | } |
| 1754 | |
| 1755 | bool did_cancel_then_restart_main() const { |
| 1756 | return did_cancel_then_restart_main_; |
| 1757 | } |
| 1758 | |
| 1759 | bool did_simulate_error_main() const { |
| 1760 | return did_simulate_error_main_; |
| 1761 | } |
| 1762 | |
| 1763 | bool did_intercept_redirect() const { |
| 1764 | return did_intercept_redirect_; |
| 1765 | } |
| 1766 | |
| 1767 | bool did_cancel_redirect() const { |
| 1768 | return did_cancel_redirect_; |
| 1769 | } |
| 1770 | |
| 1771 | bool did_intercept_final() const { |
| 1772 | return did_intercept_final_; |
| 1773 | } |
| 1774 | |
| 1775 | bool did_cancel_final() const { |
| 1776 | return did_cancel_final_; |
| 1777 | } |
| 1778 | |
| 1779 | private: |
| 1780 | // Indicate whether to intercept the main request, and if so specify the |
| 1781 | // response to return and the LoadTimingInfo to use. |
| 1782 | mutable bool intercept_main_request_; |
| 1783 | mutable std::string main_headers_; |
| 1784 | mutable std::string main_data_; |
| 1785 | mutable LoadTimingInfo main_request_load_timing_info_; |
| 1786 | |
| 1787 | // These indicate actions that can be taken within MaybeInterceptRequest. |
| 1788 | mutable bool restart_main_request_; |
| 1789 | mutable bool cancel_main_request_; |
| 1790 | mutable bool cancel_then_restart_main_request_; |
| 1791 | mutable bool simulate_main_network_error_; |
| 1792 | |
| 1793 | // Indicate whether to intercept redirects, and if so specify the response to |
| 1794 | // return. |
| 1795 | mutable bool intercept_redirect_; |
| 1796 | mutable std::string redirect_headers_; |
| 1797 | mutable std::string redirect_data_; |
| 1798 | |
| 1799 | // Cancel the request within MaybeInterceptRedirect. |
| 1800 | mutable bool cancel_redirect_request_; |
| 1801 | |
| 1802 | // Indicate whether to intercept the final response, and if so specify the |
| 1803 | // response to return. |
| 1804 | mutable bool intercept_final_response_; |
| 1805 | mutable std::string final_headers_; |
| 1806 | mutable std::string final_data_; |
| 1807 | |
| 1808 | // Cancel the final request within MaybeInterceptResponse. |
| 1809 | mutable bool cancel_final_request_; |
| 1810 | |
| 1811 | // Instruct the interceptor to use a real URLRequestHTTPJob. |
| 1812 | mutable bool use_url_request_http_job_; |
| 1813 | |
| 1814 | // These indicate if the interceptor did something or not. |
| 1815 | mutable bool did_intercept_main_; |
| 1816 | mutable bool did_restart_main_; |
| 1817 | mutable bool did_cancel_main_; |
| 1818 | mutable bool did_cancel_then_restart_main_; |
| 1819 | mutable bool did_simulate_error_main_; |
| 1820 | mutable bool did_intercept_redirect_; |
| 1821 | mutable bool did_cancel_redirect_; |
| 1822 | mutable bool did_intercept_final_; |
| 1823 | mutable bool did_cancel_final_; |
| 1824 | }; |
| 1825 | |
| 1826 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
| 1827 | class URLRequestInterceptorTest : public URLRequestTest { |
| 1828 | public: |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 1829 | URLRequestInterceptorTest() : URLRequestTest(), interceptor_(nullptr) {} |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1830 | |
| 1831 | ~URLRequestInterceptorTest() override { |
| 1832 | // URLRequestJobs may post clean-up tasks on destruction. |
| 1833 | base::RunLoop().RunUntilIdle(); |
| 1834 | } |
| 1835 | |
| 1836 | void SetUpFactory() override { |
| 1837 | interceptor_ = new MockURLRequestInterceptor(); |
| 1838 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1839 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | MockURLRequestInterceptor* interceptor() const { |
| 1843 | return interceptor_; |
| 1844 | } |
| 1845 | |
| 1846 | private: |
| 1847 | MockURLRequestInterceptor* interceptor_; |
| 1848 | }; |
| 1849 | |
| 1850 | TEST_F(URLRequestInterceptorTest, Intercept) { |
| 1851 | // Intercept the main request and respond with a simple response. |
| 1852 | interceptor()->set_intercept_main_request(true); |
| 1853 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1854 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1855 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1856 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1857 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1858 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1859 | base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); |
| 1860 | base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); |
| 1861 | base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); |
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 1862 | req->SetUserData(&user_data0, base::WrapUnique(user_data0)); |
avi | 0d99147 | 2017-04-27 07:04:04 | [diff] [blame] | 1863 | req->SetUserData(&user_data1, base::WrapUnique(user_data1)); |
| 1864 | req->SetUserData(&user_data2, base::WrapUnique(user_data2)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1865 | req->set_method("GET"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1866 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1867 | d.RunUntilComplete(); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1868 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1869 | // Make sure we can retrieve our specific user data. |
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 1870 | EXPECT_EQ(user_data0, req->GetUserData(&user_data0)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1871 | EXPECT_EQ(user_data1, req->GetUserData(&user_data1)); |
| 1872 | EXPECT_EQ(user_data2, req->GetUserData(&user_data2)); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1873 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1874 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1875 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1876 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1877 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1878 | EXPECT_EQ(1, d.response_started_count()); |
| 1879 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1880 | } |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1881 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1882 | TEST_F(URLRequestInterceptorTest, InterceptRedirect) { |
| 1883 | // Intercept the main request and respond with a redirect. |
| 1884 | interceptor()->set_intercept_main_request(true); |
| 1885 | interceptor()->set_main_headers( |
| 1886 | MockURLRequestInterceptor::redirect_headers()); |
| 1887 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 1888 | |
| 1889 | // Intercept that redirect and respond with a final OK response. |
| 1890 | interceptor()->set_intercept_redirect(true); |
| 1891 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 1892 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 1893 | |
| 1894 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1895 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1896 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1897 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1898 | req->set_method("GET"); |
| 1899 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1900 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1901 | |
| 1902 | // Check that the interceptor got called as expected. |
| 1903 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1904 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 1905 | |
| 1906 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1907 | int status = d.request_status(); |
| 1908 | EXPECT_EQ(OK, status); |
| 1909 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1910 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1911 | |
| 1912 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1913 | EXPECT_EQ(1, d.response_started_count()); |
| 1914 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1915 | } |
| 1916 | |
| 1917 | TEST_F(URLRequestInterceptorTest, InterceptServerError) { |
| 1918 | // Intercept the main request to generate a server error response. |
| 1919 | interceptor()->set_intercept_main_request(true); |
| 1920 | interceptor()->set_main_headers(MockURLRequestInterceptor::error_headers()); |
| 1921 | interceptor()->set_main_data(MockURLRequestInterceptor::error_data()); |
| 1922 | |
| 1923 | // Intercept that error and respond with an OK response. |
| 1924 | interceptor()->set_intercept_final_response(true); |
| 1925 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1926 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1927 | |
| 1928 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1929 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1930 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1931 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1932 | req->set_method("GET"); |
| 1933 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1934 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1935 | |
| 1936 | // Check that the interceptor got called as expected. |
| 1937 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1938 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1939 | |
| 1940 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1941 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1942 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1943 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1944 | EXPECT_EQ(1, d.response_started_count()); |
| 1945 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1946 | } |
| 1947 | |
| 1948 | TEST_F(URLRequestInterceptorTest, InterceptNetworkError) { |
| 1949 | // Intercept the main request to simulate a network error. |
| 1950 | interceptor()->set_simulate_main_network_error(true); |
| 1951 | |
| 1952 | // Intercept that error and respond with an OK response. |
| 1953 | interceptor()->set_intercept_final_response(true); |
| 1954 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1955 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1956 | |
| 1957 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1958 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1959 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1960 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1961 | req->set_method("GET"); |
| 1962 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1963 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1964 | |
| 1965 | // Check that the interceptor got called as expected. |
| 1966 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 1967 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1968 | |
| 1969 | // Check that we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1970 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1971 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1972 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1973 | EXPECT_EQ(1, d.response_started_count()); |
| 1974 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1975 | } |
| 1976 | |
| 1977 | TEST_F(URLRequestInterceptorTest, InterceptRestartRequired) { |
| 1978 | // Restart the main request. |
| 1979 | interceptor()->set_restart_main_request(true); |
| 1980 | |
| 1981 | // then intercept the new main request and respond with an OK response |
| 1982 | interceptor()->set_intercept_main_request(true); |
| 1983 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1984 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
| 1985 | |
| 1986 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1987 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1988 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1989 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1990 | req->set_method("GET"); |
| 1991 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1992 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1993 | |
| 1994 | // Check that the interceptor got called as expected. |
| 1995 | EXPECT_TRUE(interceptor()->did_restart_main()); |
| 1996 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1997 | |
| 1998 | // Check that we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1999 | int status = d.request_status(); |
| 2000 | EXPECT_EQ(OK, status); |
| 2001 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2002 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 2003 | |
| 2004 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 2005 | EXPECT_EQ(1, d.response_started_count()); |
| 2006 | EXPECT_EQ(0, d.received_redirect_count()); |
| 2007 | } |
| 2008 | |
| 2009 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelMain) { |
| 2010 | // Intercept the main request and cancel from within the restarted job. |
| 2011 | interceptor()->set_cancel_main_request(true); |
| 2012 | |
| 2013 | // Set up to intercept the final response and override it with an OK response. |
| 2014 | interceptor()->set_intercept_final_response(true); |
| 2015 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2016 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 2017 | |
| 2018 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2019 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2020 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2021 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2022 | req->set_method("GET"); |
| 2023 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2024 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2025 | |
| 2026 | // Check that the interceptor got called as expected. |
| 2027 | EXPECT_TRUE(interceptor()->did_cancel_main()); |
| 2028 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2029 | |
| 2030 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2031 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelRedirect) { |
| 2035 | // Intercept the main request and respond with a redirect. |
| 2036 | interceptor()->set_intercept_main_request(true); |
| 2037 | interceptor()->set_main_headers( |
| 2038 | MockURLRequestInterceptor::redirect_headers()); |
| 2039 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 2040 | |
| 2041 | // Intercept the redirect and cancel from within that job. |
| 2042 | interceptor()->set_cancel_redirect_request(true); |
| 2043 | |
| 2044 | // Set up to intercept the final response and override it with an OK response. |
| 2045 | interceptor()->set_intercept_final_response(true); |
| 2046 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2047 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 2048 | |
| 2049 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2050 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2051 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2052 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2053 | req->set_method("GET"); |
| 2054 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2055 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2056 | |
| 2057 | // Check that the interceptor got called as expected. |
| 2058 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 2059 | EXPECT_TRUE(interceptor()->did_cancel_redirect()); |
| 2060 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2061 | |
| 2062 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2063 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelFinal) { |
| 2067 | // Intercept the main request to simulate a network error. |
| 2068 | interceptor()->set_simulate_main_network_error(true); |
| 2069 | |
| 2070 | // Set up to intercept final the response and cancel from within that job. |
| 2071 | interceptor()->set_cancel_final_request(true); |
| 2072 | |
| 2073 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2074 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2075 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2076 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2077 | req->set_method("GET"); |
| 2078 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2079 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2080 | |
| 2081 | // Check that the interceptor got called as expected. |
| 2082 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 2083 | EXPECT_TRUE(interceptor()->did_cancel_final()); |
| 2084 | |
| 2085 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2086 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2087 | } |
| 2088 | |
| 2089 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelInRestart) { |
| 2090 | // Intercept the main request and cancel then restart from within that job. |
| 2091 | interceptor()->set_cancel_then_restart_main_request(true); |
| 2092 | |
| 2093 | // Set up to intercept the final response and override it with an OK response. |
| 2094 | interceptor()->set_intercept_final_response(true); |
bengr | b50d631e | 2014-11-17 22:50:50 | [diff] [blame] | 2095 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2096 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2097 | |
| 2098 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2099 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2100 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2101 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2102 | req->set_method("GET"); |
| 2103 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2104 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2105 | |
| 2106 | // Check that the interceptor got called as expected. |
| 2107 | EXPECT_TRUE(interceptor()->did_cancel_then_restart_main()); |
| 2108 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2109 | |
| 2110 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2111 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not |
| 2115 | // reused. |connect_time_flags| is used to indicate if there should be dns |
| 2116 | // or SSL times, and |used_proxy| is used for proxy times. |
| 2117 | LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now, |
| 2118 | int connect_time_flags, |
| 2119 | bool used_proxy) { |
| 2120 | LoadTimingInfo load_timing; |
| 2121 | load_timing.socket_log_id = 1; |
| 2122 | |
| 2123 | if (used_proxy) { |
| 2124 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 2125 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 2126 | } |
| 2127 | |
| 2128 | LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing; |
| 2129 | if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) { |
| 2130 | connect_timing.dns_start = now + base::TimeDelta::FromDays(3); |
| 2131 | connect_timing.dns_end = now + base::TimeDelta::FromDays(4); |
| 2132 | } |
| 2133 | connect_timing.connect_start = now + base::TimeDelta::FromDays(5); |
| 2134 | if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) { |
| 2135 | connect_timing.ssl_start = now + base::TimeDelta::FromDays(6); |
| 2136 | connect_timing.ssl_end = now + base::TimeDelta::FromDays(7); |
| 2137 | } |
| 2138 | connect_timing.connect_end = now + base::TimeDelta::FromDays(8); |
| 2139 | |
| 2140 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 2141 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 2142 | load_timing.receive_headers_start = now + base::TimeDelta::FromDays(11); |
| 2143 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(12); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2144 | return load_timing; |
| 2145 | } |
| 2146 | |
| 2147 | // Same as above, but in the case of a reused socket. |
| 2148 | LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now, |
| 2149 | bool used_proxy) { |
| 2150 | LoadTimingInfo load_timing; |
| 2151 | load_timing.socket_log_id = 1; |
| 2152 | load_timing.socket_reused = true; |
| 2153 | |
| 2154 | if (used_proxy) { |
| 2155 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 2156 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 2157 | } |
| 2158 | |
| 2159 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 2160 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 2161 | load_timing.receive_headers_start = now + base::TimeDelta::FromDays(11); |
| 2162 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(12); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2163 | return load_timing; |
| 2164 | } |
| 2165 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2166 | LoadTimingInfo RunURLRequestInterceptorLoadTimingTest( |
| 2167 | const LoadTimingInfo& job_load_timing, |
| 2168 | const URLRequestContext& context, |
| 2169 | MockURLRequestInterceptor* interceptor) { |
| 2170 | interceptor->set_intercept_main_request(true); |
| 2171 | interceptor->set_main_request_load_timing_info(job_load_timing); |
| 2172 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2173 | std::unique_ptr<URLRequest> req( |
| 2174 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 2175 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2176 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2177 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2178 | |
| 2179 | LoadTimingInfo resulting_load_timing; |
| 2180 | req->GetLoadTimingInfo(&resulting_load_timing); |
| 2181 | |
| 2182 | // None of these should be modified by the URLRequest. |
| 2183 | EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); |
| 2184 | EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); |
| 2185 | EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); |
| 2186 | EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 2187 | EXPECT_EQ(job_load_timing.receive_headers_start, |
| 2188 | resulting_load_timing.receive_headers_start); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2189 | EXPECT_EQ(job_load_timing.receive_headers_end, |
| 2190 | resulting_load_timing.receive_headers_end); |
caseq | e8340bc9 | 2016-04-20 00:02:57 | [diff] [blame] | 2191 | EXPECT_EQ(job_load_timing.push_start, resulting_load_timing.push_start); |
| 2192 | EXPECT_EQ(job_load_timing.push_end, resulting_load_timing.push_end); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2193 | |
| 2194 | return resulting_load_timing; |
| 2195 | } |
| 2196 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2197 | // Basic test that the intercept + load timing tests work. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2198 | TEST_F(URLRequestInterceptorTest, InterceptLoadTiming) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2199 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2200 | LoadTimingInfo job_load_timing = |
| 2201 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); |
| 2202 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2203 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2204 | RunURLRequestInterceptorLoadTimingTest( |
| 2205 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2206 | |
| 2207 | // Nothing should have been changed by the URLRequest. |
| 2208 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 2209 | load_timing_result.proxy_resolve_start); |
| 2210 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 2211 | load_timing_result.proxy_resolve_end); |
| 2212 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 2213 | load_timing_result.connect_timing.dns_start); |
| 2214 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 2215 | load_timing_result.connect_timing.dns_end); |
| 2216 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 2217 | load_timing_result.connect_timing.connect_start); |
| 2218 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 2219 | load_timing_result.connect_timing.connect_end); |
| 2220 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 2221 | load_timing_result.connect_timing.ssl_start); |
| 2222 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 2223 | load_timing_result.connect_timing.ssl_end); |
| 2224 | |
| 2225 | // Redundant sanity check. |
| 2226 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2227 | } |
| 2228 | |
| 2229 | // Another basic test, with proxy and SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2230 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2231 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2232 | LoadTimingInfo job_load_timing = |
| 2233 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); |
| 2234 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2235 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2236 | RunURLRequestInterceptorLoadTimingTest( |
| 2237 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2238 | |
| 2239 | // Nothing should have been changed by the URLRequest. |
| 2240 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 2241 | load_timing_result.proxy_resolve_start); |
| 2242 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 2243 | load_timing_result.proxy_resolve_end); |
| 2244 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 2245 | load_timing_result.connect_timing.dns_start); |
| 2246 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 2247 | load_timing_result.connect_timing.dns_end); |
| 2248 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 2249 | load_timing_result.connect_timing.connect_start); |
| 2250 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 2251 | load_timing_result.connect_timing.connect_end); |
| 2252 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 2253 | load_timing_result.connect_timing.ssl_start); |
| 2254 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 2255 | load_timing_result.connect_timing.ssl_end); |
| 2256 | |
| 2257 | // Redundant sanity check. |
| 2258 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2259 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 2260 | } |
| 2261 | |
| 2262 | // Make sure that URLRequest correctly adjusts proxy times when they're before |
| 2263 | // |request_start|, due to already having a connected socket. This happens in |
[email protected] | cf4cae3 | 2014-05-27 00:39:10 | [diff] [blame] | 2264 | // the case of reusing a SPDY session. The connected socket is not considered |
| 2265 | // reused in this test (May be a preconnect). |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2266 | // |
| 2267 | // 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] | 2268 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyProxyResolution) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2269 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2270 | LoadTimingInfo job_load_timing = |
| 2271 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); |
| 2272 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); |
| 2273 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); |
| 2274 | job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); |
| 2275 | job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); |
| 2276 | job_load_timing.connect_timing.connect_start = |
| 2277 | now - base::TimeDelta::FromDays(2); |
| 2278 | job_load_timing.connect_timing.connect_end = |
| 2279 | now - base::TimeDelta::FromDays(1); |
| 2280 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2281 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2282 | RunURLRequestInterceptorLoadTimingTest( |
| 2283 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2284 | |
| 2285 | // Proxy times, connect times, and DNS times should all be replaced with |
| 2286 | // request_start. |
| 2287 | EXPECT_EQ(load_timing_result.request_start, |
| 2288 | load_timing_result.proxy_resolve_start); |
| 2289 | EXPECT_EQ(load_timing_result.request_start, |
| 2290 | load_timing_result.proxy_resolve_end); |
| 2291 | EXPECT_EQ(load_timing_result.request_start, |
| 2292 | load_timing_result.connect_timing.dns_start); |
| 2293 | EXPECT_EQ(load_timing_result.request_start, |
| 2294 | load_timing_result.connect_timing.dns_end); |
| 2295 | EXPECT_EQ(load_timing_result.request_start, |
| 2296 | load_timing_result.connect_timing.connect_start); |
| 2297 | EXPECT_EQ(load_timing_result.request_start, |
| 2298 | load_timing_result.connect_timing.connect_end); |
| 2299 | |
| 2300 | // Other times should have been left null. |
| 2301 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2302 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 2303 | } |
| 2304 | |
| 2305 | // Same as above, but in the reused case. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2306 | TEST_F(URLRequestInterceptorTest, |
| 2307 | InterceptLoadTimingEarlyProxyResolutionReused) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2308 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2309 | LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); |
| 2310 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); |
| 2311 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); |
| 2312 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2313 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2314 | RunURLRequestInterceptorLoadTimingTest( |
| 2315 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2316 | |
| 2317 | // Proxy times and connect times should all be replaced with request_start. |
| 2318 | EXPECT_EQ(load_timing_result.request_start, |
| 2319 | load_timing_result.proxy_resolve_start); |
| 2320 | EXPECT_EQ(load_timing_result.request_start, |
| 2321 | load_timing_result.proxy_resolve_end); |
| 2322 | |
| 2323 | // Other times should have been left null. |
| 2324 | TestLoadTimingReusedWithProxy(load_timing_result); |
| 2325 | } |
| 2326 | |
| 2327 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2328 | // |request_start|, due to reusing a connected socket. The connected socket is |
| 2329 | // not considered reused in this test (May be a preconnect). |
| 2330 | // |
| 2331 | // To mix things up, the request has SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2332 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnect) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2333 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2334 | LoadTimingInfo job_load_timing = |
| 2335 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); |
| 2336 | job_load_timing.connect_timing.connect_start = |
| 2337 | now - base::TimeDelta::FromDays(1); |
| 2338 | job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); |
| 2339 | job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); |
| 2340 | job_load_timing.connect_timing.connect_end = |
| 2341 | now - base::TimeDelta::FromDays(4); |
| 2342 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2343 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2344 | RunURLRequestInterceptorLoadTimingTest( |
| 2345 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2346 | |
| 2347 | // Connect times, and SSL times should be replaced with request_start. |
| 2348 | EXPECT_EQ(load_timing_result.request_start, |
| 2349 | load_timing_result.connect_timing.connect_start); |
| 2350 | EXPECT_EQ(load_timing_result.request_start, |
| 2351 | load_timing_result.connect_timing.ssl_start); |
| 2352 | EXPECT_EQ(load_timing_result.request_start, |
| 2353 | load_timing_result.connect_timing.ssl_end); |
| 2354 | EXPECT_EQ(load_timing_result.request_start, |
| 2355 | load_timing_result.connect_timing.connect_end); |
| 2356 | |
| 2357 | // Other times should have been left null. |
| 2358 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); |
| 2359 | } |
| 2360 | |
| 2361 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2362 | // |request_start|, due to reusing a connected socket in the case that there |
| 2363 | // are also proxy times. The connected socket is not considered reused in this |
| 2364 | // test (May be a preconnect). |
| 2365 | // |
| 2366 | // In this test, there are no SSL or DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2367 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnectWithProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2368 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2369 | LoadTimingInfo job_load_timing = |
| 2370 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); |
| 2371 | job_load_timing.connect_timing.connect_start = |
| 2372 | now - base::TimeDelta::FromDays(1); |
| 2373 | job_load_timing.connect_timing.connect_end = |
| 2374 | now - base::TimeDelta::FromDays(2); |
| 2375 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2376 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2377 | RunURLRequestInterceptorLoadTimingTest( |
| 2378 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2379 | |
| 2380 | // Connect times should be replaced with proxy_resolve_end. |
| 2381 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2382 | load_timing_result.connect_timing.connect_start); |
| 2383 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2384 | load_timing_result.connect_timing.connect_end); |
| 2385 | |
| 2386 | // Other times should have been left null. |
| 2387 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2388 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 2389 | } |
| 2390 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2391 | // Check that two different URL requests have different identifiers. |
| 2392 | TEST_F(URLRequestTest, Identifiers) { |
| 2393 | TestDelegate d; |
| 2394 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2395 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2396 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2397 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2398 | std::unique_ptr<URLRequest> other_req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2399 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2400 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2401 | |
mmenke | 19378d2 | 2014-09-09 04:12:59 | [diff] [blame] | 2402 | ASSERT_NE(req->identifier(), other_req->identifier()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2403 | } |
| 2404 | |
blundell | b8163592f | 2015-12-16 14:22:42 | [diff] [blame] | 2405 | #if defined(OS_IOS) |
| 2406 | // TODO(droger): Check that a failure to connect to the proxy is reported to |
| 2407 | // the network delegate. crbug.com/496743 |
| 2408 | #define MAYBE_NetworkDelegateProxyError DISABLED_NetworkDelegateProxyError |
| 2409 | #else |
| 2410 | #define MAYBE_NetworkDelegateProxyError NetworkDelegateProxyError |
| 2411 | #endif |
| 2412 | TEST_F(URLRequestTest, MAYBE_NetworkDelegateProxyError) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2413 | MockHostResolver host_resolver; |
| 2414 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 2415 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2416 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2417 | TestURLRequestContextWithProxy context("myproxy:70", &network_delegate); |
| 2418 | |
| 2419 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2420 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2421 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2422 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2423 | req->set_method("GET"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2424 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2425 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2426 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2427 | |
| 2428 | // Check we see a failed request. |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 2429 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 2430 | EXPECT_FALSE(req->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2431 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2432 | |
| 2433 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2434 | EXPECT_THAT(network_delegate.last_error(), |
| 2435 | IsError(ERR_PROXY_CONNECTION_FAILED)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2436 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2437 | } |
| 2438 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2439 | // Make sure that NetworkDelegate::NotifyCompleted is called if |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2440 | // content is empty. |
| 2441 | TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) { |
| 2442 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2443 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2444 | GURL("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2445 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2446 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2447 | EXPECT_EQ("", d.data_received()); |
| 2448 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 2449 | } |
| 2450 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2451 | // Make sure that SetPriority actually sets the URLRequest's priority |
| 2452 | // correctly, both before and after start. |
| 2453 | TEST_F(URLRequestTest, SetPriorityBasic) { |
| 2454 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2455 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2456 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2457 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2458 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2459 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2460 | req->SetPriority(LOW); |
| 2461 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2462 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2463 | req->Start(); |
| 2464 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2465 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2466 | req->SetPriority(MEDIUM); |
| 2467 | EXPECT_EQ(MEDIUM, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | // Make sure that URLRequest calls SetPriority on a job before calling |
| 2471 | // Start on it. |
| 2472 | TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) { |
| 2473 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2474 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2475 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2476 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2477 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2478 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2479 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2480 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2481 | req.get(), &default_network_delegate_, &job_priority)); |
| 2482 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
| 2483 | EXPECT_EQ(DEFAULT_PRIORITY, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2484 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2485 | req->SetPriority(LOW); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2486 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2487 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2488 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2489 | } |
| 2490 | |
| 2491 | // Make sure that URLRequest passes on its priority updates to its |
| 2492 | // job. |
| 2493 | TEST_F(URLRequestTest, SetJobPriority) { |
| 2494 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2495 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2496 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2497 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2498 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2499 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2500 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2501 | req.get(), &default_network_delegate_, &job_priority)); |
| 2502 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2503 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2504 | req->SetPriority(LOW); |
| 2505 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2506 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2507 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2508 | req->SetPriority(MEDIUM); |
| 2509 | EXPECT_EQ(MEDIUM, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2510 | EXPECT_EQ(MEDIUM, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2511 | } |
| 2512 | |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2513 | // Setting the IGNORE_LIMITS load flag should be okay if the priority |
| 2514 | // is MAXIMUM_PRIORITY. |
| 2515 | TEST_F(URLRequestTest, PriorityIgnoreLimits) { |
| 2516 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2517 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2518 | GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY, &d, |
| 2519 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2520 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2521 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2522 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2523 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2524 | req.get(), &default_network_delegate_, &job_priority)); |
| 2525 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2526 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2527 | req->SetLoadFlags(LOAD_IGNORE_LIMITS); |
| 2528 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2529 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2530 | req->SetPriority(MAXIMUM_PRIORITY); |
| 2531 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2532 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2533 | req->Start(); |
| 2534 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2535 | EXPECT_EQ(MAXIMUM_PRIORITY, job_priority); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2536 | } |
| 2537 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2538 | namespace { |
| 2539 | |
| 2540 | // Less verbose way of running a simple testserver for the tests below. |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2541 | class HttpTestServer : public EmbeddedTestServer { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2542 | public: |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2543 | explicit HttpTestServer(const base::FilePath& document_root) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2544 | AddDefaultHandlers(document_root); |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2545 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2546 | |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2547 | HttpTestServer() { AddDefaultHandlers(base::FilePath()); } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2548 | }; |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2549 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2550 | } // namespace |
| 2551 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2552 | TEST_F(URLRequestTest, DelayedCookieCallback) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2553 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2554 | ASSERT_TRUE(test_server.Start()); |
| 2555 | |
| 2556 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2557 | std::unique_ptr<DelayedCookieMonster> delayed_cm(new DelayedCookieMonster()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2558 | context.set_cookie_store(delayed_cm.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2559 | |
| 2560 | // Set up a cookie. |
| 2561 | { |
| 2562 | TestNetworkDelegate network_delegate; |
| 2563 | context.set_network_delegate(&network_delegate); |
| 2564 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2565 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2566 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2567 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2568 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2569 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2570 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2571 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2572 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2573 | } |
| 2574 | |
| 2575 | // Verify that the cookie is set. |
| 2576 | { |
| 2577 | TestNetworkDelegate network_delegate; |
| 2578 | context.set_network_delegate(&network_delegate); |
| 2579 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2580 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2581 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2582 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2583 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2584 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2585 | |
| 2586 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2587 | != std::string::npos); |
| 2588 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2589 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2590 | } |
| 2591 | } |
| 2592 | |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 2593 | class FilteringTestLayeredNetworkDelegate : public LayeredNetworkDelegate { |
| 2594 | public: |
| 2595 | FilteringTestLayeredNetworkDelegate( |
| 2596 | std::unique_ptr<NetworkDelegate> network_delegate) |
| 2597 | : LayeredNetworkDelegate(std::move((network_delegate))), |
| 2598 | set_cookie_called_count_(0), |
| 2599 | blocked_set_cookie_count_(0) {} |
| 2600 | ~FilteringTestLayeredNetworkDelegate() override = default; |
| 2601 | |
| 2602 | bool OnCanSetCookieInternal(const URLRequest& request, |
| 2603 | const net::CanonicalCookie& cookie, |
| 2604 | CookieOptions* options, |
| 2605 | bool allowed_from_caller) override { |
| 2606 | // Filter out cookies with the same name as |cookie_name_filter_| and |
| 2607 | // combine with |allowed_from_caller|. |
| 2608 | bool allowed = |
| 2609 | allowed_from_caller && !(cookie.Name() == cookie_name_filter_); |
| 2610 | |
| 2611 | ++set_cookie_called_count_; |
| 2612 | |
| 2613 | if (!allowed) |
| 2614 | ++blocked_set_cookie_count_; |
| 2615 | |
| 2616 | return allowed; |
| 2617 | } |
| 2618 | |
| 2619 | void SetCookieFilter(std::string filter) { |
| 2620 | cookie_name_filter_ = std::move(filter); |
| 2621 | } |
| 2622 | |
| 2623 | int set_cookie_called_count() { return set_cookie_called_count_; } |
| 2624 | |
| 2625 | int blocked_set_cookie_count() { return blocked_set_cookie_count_; } |
| 2626 | |
| 2627 | void ResetSetCookieCalledCount() { set_cookie_called_count_ = 0; } |
| 2628 | |
| 2629 | void ResetBlockedSetCookieCount() { blocked_set_cookie_count_ = 0; } |
| 2630 | |
| 2631 | private: |
| 2632 | std::string cookie_name_filter_; |
| 2633 | int set_cookie_called_count_; |
| 2634 | int blocked_set_cookie_count_; |
| 2635 | }; |
| 2636 | |
| 2637 | TEST_F(URLRequestTest, DelayedCookieCallbackAsync) { |
| 2638 | HttpTestServer test_server; |
| 2639 | ASSERT_TRUE(test_server.Start()); |
| 2640 | |
| 2641 | TestURLRequestContext async_context; |
| 2642 | std::unique_ptr<DelayedCookieMonster> delayed_cm = |
| 2643 | std::make_unique<DelayedCookieMonster>(); |
| 2644 | async_context.set_cookie_store(delayed_cm.get()); |
| 2645 | FilteringTestLayeredNetworkDelegate async_filter_network_delegate( |
| 2646 | std::make_unique<TestNetworkDelegate>()); |
| 2647 | async_filter_network_delegate.SetCookieFilter("CookieBlockedOnCanGetCookie"); |
| 2648 | async_context.set_network_delegate(&async_filter_network_delegate); |
| 2649 | TestDelegate async_delegate; |
| 2650 | |
| 2651 | TestURLRequestContext sync_context; |
| 2652 | std::unique_ptr<CookieMonster> cm = |
| 2653 | std::make_unique<CookieMonster>(nullptr, nullptr, nullptr); |
| 2654 | sync_context.set_cookie_store(cm.get()); |
| 2655 | FilteringTestLayeredNetworkDelegate sync_filter_network_delegate( |
| 2656 | std::make_unique<TestNetworkDelegate>()); |
| 2657 | sync_filter_network_delegate.SetCookieFilter("CookieBlockedOnCanGetCookie"); |
| 2658 | sync_context.set_network_delegate(&sync_filter_network_delegate); |
| 2659 | TestDelegate sync_delegate; |
| 2660 | |
| 2661 | // Add a secure cookie so we can try to set an insecure cookie and have |
| 2662 | // SetCanonicalCookie (and therefore SetCookieWithOptions) fail. |
| 2663 | GURL::Replacements replace_scheme; |
| 2664 | replace_scheme.SetSchemeStr("https"); |
| 2665 | GURL url = test_server.base_url().ReplaceComponents(replace_scheme); |
| 2666 | |
| 2667 | delayed_cm->SetCookieWithOptionsAsync(url, "AlreadySetCookie=1;Secure", |
| 2668 | CookieOptions(), |
| 2669 | CookieStore::SetCookiesCallback()); |
| 2670 | cm->SetCookieWithOptionsAsync(url, "AlreadySetCookie=1;Secure", |
| 2671 | CookieOptions(), |
| 2672 | CookieStore::SetCookiesCallback()); |
| 2673 | |
| 2674 | std::vector<std::string> cookie_lines( |
| 2675 | {// Fails on CanonicalCookie::Create for trying to create a secure cookie |
| 2676 | // on an insecure host. |
| 2677 | "CookieNotSet=1;Secure", |
| 2678 | // Fail in FilteringTestLayeredNetworkDelegate::CanGetCookie. |
| 2679 | "CookieBlockedOnCanGetCookie=1", |
| 2680 | // Fails in SetCanonicalCookie for trying to overwrite a secure cookie |
| 2681 | // with an insecure cookie. |
| 2682 | "AlreadySetCookie=1", |
| 2683 | // Succeeds and added cookie to store. Delayed (which makes the callback |
| 2684 | // run asynchronously) in DelayedCookieMonster. |
| 2685 | "CookieSet=1"}); |
| 2686 | |
| 2687 | for (auto first_cookie_line : cookie_lines) { |
| 2688 | for (auto second_cookie_line : cookie_lines) { |
| 2689 | // Run with the delayed cookie monster. |
| 2690 | std::unique_ptr<URLRequest> request = async_context.CreateRequest( |
| 2691 | test_server.GetURL("/set-cookie?" + first_cookie_line + "&" + |
| 2692 | second_cookie_line), |
| 2693 | DEFAULT_PRIORITY, &async_delegate, TRAFFIC_ANNOTATION_FOR_TESTS); |
| 2694 | |
| 2695 | request->Start(); |
| 2696 | async_delegate.RunUntilComplete(); |
| 2697 | EXPECT_THAT(async_delegate.request_status(), IsOk()); |
| 2698 | |
| 2699 | // Run with the regular cookie monster. |
| 2700 | request = sync_context.CreateRequest( |
| 2701 | test_server.GetURL("/set-cookie?" + first_cookie_line + "&" + |
| 2702 | second_cookie_line), |
| 2703 | DEFAULT_PRIORITY, &sync_delegate, TRAFFIC_ANNOTATION_FOR_TESTS); |
| 2704 | |
| 2705 | request->Start(); |
| 2706 | sync_delegate.RunUntilComplete(); |
| 2707 | EXPECT_THAT(sync_delegate.request_status(), IsOk()); |
| 2708 | |
| 2709 | int expected_set_cookie_count = 0; |
| 2710 | int expected_blocked_cookie_count = 0; |
| 2711 | |
| 2712 | if (first_cookie_line != "CookieNotSet=1;Secure") |
| 2713 | ++expected_set_cookie_count; |
| 2714 | if (second_cookie_line != "CookieNotSet=1;Secure") |
| 2715 | ++expected_set_cookie_count; |
| 2716 | |
| 2717 | if (first_cookie_line == "CookieBlockedOnCanGetCookie=1") |
| 2718 | ++expected_blocked_cookie_count; |
| 2719 | if (second_cookie_line == "CookieBlockedOnCanGetCookie=1") |
| 2720 | ++expected_blocked_cookie_count; |
| 2721 | |
| 2722 | EXPECT_EQ(expected_set_cookie_count, |
| 2723 | async_filter_network_delegate.set_cookie_called_count()); |
| 2724 | EXPECT_EQ(expected_blocked_cookie_count, |
| 2725 | async_filter_network_delegate.blocked_set_cookie_count()); |
| 2726 | |
| 2727 | EXPECT_EQ(expected_set_cookie_count, |
| 2728 | sync_filter_network_delegate.set_cookie_called_count()); |
| 2729 | EXPECT_EQ(expected_blocked_cookie_count, |
| 2730 | sync_filter_network_delegate.blocked_set_cookie_count()); |
| 2731 | |
| 2732 | async_filter_network_delegate.ResetSetCookieCalledCount(); |
| 2733 | async_filter_network_delegate.ResetBlockedSetCookieCount(); |
| 2734 | |
| 2735 | sync_filter_network_delegate.ResetSetCookieCalledCount(); |
| 2736 | sync_filter_network_delegate.ResetBlockedSetCookieCount(); |
| 2737 | } |
| 2738 | } |
| 2739 | } |
| 2740 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2741 | TEST_F(URLRequestTest, DoNotSendCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2742 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2743 | ASSERT_TRUE(test_server.Start()); |
| 2744 | |
| 2745 | // Set up a cookie. |
| 2746 | { |
| 2747 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2748 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2749 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2750 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2751 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2752 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2753 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2754 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2755 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2756 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2757 | } |
| 2758 | |
| 2759 | // Verify that the cookie is set. |
| 2760 | { |
| 2761 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2762 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2763 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2764 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2765 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2766 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2767 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2768 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2769 | |
| 2770 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2771 | != std::string::npos); |
| 2772 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2773 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2774 | } |
| 2775 | |
| 2776 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 2777 | { |
| 2778 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2779 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2780 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2781 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2782 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2783 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2784 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
| 2785 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2786 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2787 | |
| 2788 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2789 | == std::string::npos); |
| 2790 | |
| 2791 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
| 2792 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2793 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2794 | } |
| 2795 | } |
| 2796 | |
| 2797 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2798 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2799 | ASSERT_TRUE(test_server.Start()); |
| 2800 | |
| 2801 | // Set up a cookie. |
| 2802 | { |
| 2803 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2804 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2805 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2806 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2807 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2808 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2809 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2810 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2811 | |
| 2812 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2813 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2814 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2815 | } |
| 2816 | |
| 2817 | // Try to set-up another cookie and update the previous cookie. |
| 2818 | { |
| 2819 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2820 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2821 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2822 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2823 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2824 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2825 | req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES); |
| 2826 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2827 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2828 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2829 | |
| 2830 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
| 2831 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2832 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2833 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2834 | } |
| 2835 | |
| 2836 | // Verify the cookies weren't saved or updated. |
| 2837 | { |
| 2838 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2839 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2840 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2841 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2842 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2843 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2844 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2845 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2846 | |
| 2847 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2848 | == std::string::npos); |
| 2849 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2850 | != std::string::npos); |
| 2851 | |
| 2852 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2853 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2854 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2855 | } |
| 2856 | } |
| 2857 | |
| 2858 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2859 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2860 | ASSERT_TRUE(test_server.Start()); |
| 2861 | |
| 2862 | // Set up a cookie. |
| 2863 | { |
| 2864 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2865 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2866 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2867 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2868 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2869 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2870 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2871 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2872 | |
| 2873 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2874 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2875 | } |
| 2876 | |
| 2877 | // Verify that the cookie is set. |
| 2878 | { |
| 2879 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2880 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2881 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2882 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2883 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2884 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2885 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2886 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2887 | |
| 2888 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2889 | != std::string::npos); |
| 2890 | |
| 2891 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2892 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2893 | TestNetLogEntry::List entries; |
| 2894 | net_log_.GetEntries(&entries); |
| 2895 | for (const auto& entry : entries) { |
| 2896 | EXPECT_NE(entry.type, |
| 2897 | NetLogEventType::COOKIE_GET_BLOCKED_BY_NETWORK_DELEGATE); |
| 2898 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2899 | } |
| 2900 | |
| 2901 | // Verify that the cookie isn't sent. |
| 2902 | { |
| 2903 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2904 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2905 | TestDelegate d; |
| 2906 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2907 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2908 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2909 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2910 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2911 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2912 | |
| 2913 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2914 | == std::string::npos); |
| 2915 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2916 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2917 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2918 | TestNetLogEntry::List entries; |
| 2919 | net_log_.GetEntries(&entries); |
| 2920 | ExpectLogContainsSomewhereAfter( |
| 2921 | entries, 0, NetLogEventType::COOKIE_GET_BLOCKED_BY_NETWORK_DELEGATE, |
| 2922 | NetLogEventPhase::NONE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2923 | } |
| 2924 | } |
| 2925 | |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 2926 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 2927 | #if defined(OS_IOS) |
| 2928 | #define MAYBE_DoNotSaveCookies_ViaPolicy FLAKY_DoNotSaveCookies_ViaPolicy |
| 2929 | #else |
| 2930 | #define MAYBE_DoNotSaveCookies_ViaPolicy DoNotSaveCookies_ViaPolicy |
| 2931 | #endif |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2932 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2933 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2934 | ASSERT_TRUE(test_server.Start()); |
| 2935 | |
| 2936 | // Set up a cookie. |
| 2937 | { |
| 2938 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2939 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2940 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2941 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2942 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2943 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2944 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2945 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2946 | |
| 2947 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2948 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2949 | TestNetLogEntry::List entries; |
| 2950 | net_log_.GetEntries(&entries); |
| 2951 | for (const auto& entry : entries) { |
| 2952 | EXPECT_NE(entry.type, |
| 2953 | NetLogEventType::COOKIE_SET_BLOCKED_BY_NETWORK_DELEGATE); |
| 2954 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2955 | } |
| 2956 | |
| 2957 | // Try to set-up another cookie and update the previous cookie. |
| 2958 | { |
| 2959 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2960 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2961 | TestDelegate d; |
| 2962 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2963 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2964 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2965 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2966 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2967 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2968 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2969 | |
| 2970 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2971 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2972 | TestNetLogEntry::List entries; |
| 2973 | net_log_.GetEntries(&entries); |
| 2974 | ExpectLogContainsSomewhereAfter( |
| 2975 | entries, 0, NetLogEventType::COOKIE_SET_BLOCKED_BY_NETWORK_DELEGATE, |
| 2976 | NetLogEventPhase::NONE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2977 | } |
| 2978 | |
| 2979 | // Verify the cookies weren't saved or updated. |
| 2980 | { |
| 2981 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2982 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2983 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2984 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2985 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2986 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2987 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2988 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2989 | |
| 2990 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2991 | == std::string::npos); |
| 2992 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2993 | != std::string::npos); |
| 2994 | |
| 2995 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2996 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2997 | } |
| 2998 | } |
| 2999 | |
| 3000 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3001 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3002 | ASSERT_TRUE(test_server.Start()); |
| 3003 | |
| 3004 | // Set up an empty cookie. |
| 3005 | { |
| 3006 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3007 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3008 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3009 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3010 | test_server.GetURL("/set-cookie"), DEFAULT_PRIORITY, &d, |
| 3011 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3012 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3013 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3014 | |
| 3015 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3016 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3017 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 3018 | } |
| 3019 | } |
| 3020 | |
| 3021 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3022 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3023 | ASSERT_TRUE(test_server.Start()); |
| 3024 | |
| 3025 | // Set up a cookie. |
| 3026 | { |
| 3027 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3028 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3029 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3030 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3031 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3032 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3033 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3034 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3035 | |
| 3036 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3037 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3038 | } |
| 3039 | |
| 3040 | // Verify that the cookie is set. |
| 3041 | { |
| 3042 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3043 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3044 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3045 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3046 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3047 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3048 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3049 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3050 | |
| 3051 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 3052 | != std::string::npos); |
| 3053 | |
| 3054 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3055 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3056 | } |
| 3057 | |
| 3058 | // Verify that the cookie isn't sent. |
| 3059 | { |
| 3060 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3061 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3062 | TestDelegate d; |
| 3063 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3064 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3065 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3066 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3067 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3068 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3069 | |
| 3070 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 3071 | == std::string::npos); |
| 3072 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 3073 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3074 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3075 | } |
| 3076 | } |
| 3077 | |
| 3078 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3079 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3080 | ASSERT_TRUE(test_server.Start()); |
| 3081 | |
| 3082 | // Set up a cookie. |
| 3083 | { |
| 3084 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3085 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3086 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3087 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3088 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3089 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3090 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3091 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3092 | |
| 3093 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3094 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3095 | } |
| 3096 | |
| 3097 | // Try to set-up another cookie and update the previous cookie. |
| 3098 | { |
| 3099 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3100 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3101 | TestDelegate d; |
| 3102 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3103 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3104 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3105 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3106 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3107 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3108 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3109 | |
| 3110 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3111 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 3112 | } |
| 3113 | |
| 3114 | // Verify the cookies weren't saved or updated. |
| 3115 | { |
| 3116 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3117 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3118 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3119 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3120 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3121 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3122 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3123 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3124 | |
| 3125 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 3126 | == std::string::npos); |
| 3127 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 3128 | != std::string::npos); |
| 3129 | |
| 3130 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3131 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3132 | } |
| 3133 | } |
| 3134 | |
mkwst | c5fa776 | 2016-03-28 09:28:23 | [diff] [blame] | 3135 | TEST_F(URLRequestTest, SameSiteCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3136 | HttpTestServer test_server; |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3137 | ASSERT_TRUE(test_server.Start()); |
| 3138 | |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3139 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3140 | default_context().set_network_delegate(&network_delegate); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3141 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3142 | const std::string kHost = "example.test"; |
| 3143 | const std::string kSubHost = "subdomain.example.test"; |
| 3144 | const std::string kCrossHost = "cross-origin.test"; |
| 3145 | |
| 3146 | // Set up two 'SameSite' cookies on 'example.test' |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3147 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3148 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3149 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3150 | test_server.GetURL(kHost, |
| 3151 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 3152 | "LaxSameSiteCookie=1;SameSite=Lax"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3153 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3154 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3155 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3156 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3157 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3158 | EXPECT_EQ(2, network_delegate.set_cookie_count()); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3159 | } |
| 3160 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3161 | // Verify that both cookies are sent for same-site requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3162 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3163 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3164 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3165 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3166 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3167 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3168 | req->set_initiator(url::Origin::Create(test_server.GetURL(kHost, "/"))); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3169 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3170 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3171 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3172 | EXPECT_NE(std::string::npos, |
| 3173 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3174 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3175 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3176 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3177 | } |
| 3178 | |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3179 | // Verify that both cookies are sent when the request has no initiator (can |
| 3180 | // happen for main frame browser-initiated navigations). |
| 3181 | { |
| 3182 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3183 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3184 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3185 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3186 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3187 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3188 | d.RunUntilComplete(); |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3189 | |
| 3190 | EXPECT_NE(std::string::npos, |
| 3191 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3192 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 3193 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3194 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3195 | } |
| 3196 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3197 | // Verify that both cookies are sent for same-registrable-domain requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3198 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3199 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3200 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3201 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3202 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3203 | req->set_site_for_cookies(test_server.GetURL(kSubHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3204 | req->set_initiator(url::Origin::Create(test_server.GetURL(kSubHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3205 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3206 | d.RunUntilComplete(); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3207 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3208 | EXPECT_NE(std::string::npos, |
| 3209 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3210 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3211 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3212 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3213 | } |
| 3214 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3215 | // Verify that neither cookie is not sent for cross-site requests. |
| 3216 | { |
| 3217 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3218 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3219 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3220 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3221 | req->set_site_for_cookies(test_server.GetURL(kCrossHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3222 | req->set_initiator( |
| 3223 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3224 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3225 | d.RunUntilComplete(); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3226 | |
| 3227 | EXPECT_EQ(std::string::npos, |
| 3228 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3229 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 3230 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3231 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3232 | } |
| 3233 | |
| 3234 | // Verify that the lax cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3235 | // method is "safe". |
| 3236 | { |
| 3237 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3238 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3239 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3240 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3241 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3242 | req->set_initiator( |
| 3243 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3244 | req->set_method("GET"); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3245 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3246 | d.RunUntilComplete(); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3247 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3248 | EXPECT_EQ(std::string::npos, |
| 3249 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3250 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3251 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3252 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3253 | } |
| 3254 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3255 | // Verify that neither cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3256 | // method is unsafe (e.g. POST). |
| 3257 | { |
| 3258 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3259 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3260 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3261 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3262 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3263 | req->set_initiator( |
| 3264 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3265 | req->set_method("POST"); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3266 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3267 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3268 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3269 | EXPECT_EQ(std::string::npos, |
| 3270 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3271 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3272 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3273 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3274 | } |
| 3275 | } |
| 3276 | |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3277 | // Tests that __Secure- cookies can't be set on non-secure origins. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3278 | TEST_F(URLRequestTest, SecureCookiePrefixOnNonsecureOrigin) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3279 | EmbeddedTestServer http_server; |
| 3280 | http_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3281 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3282 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3283 | https_server.AddDefaultHandlers( |
| 3284 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3285 | ASSERT_TRUE(http_server.Start()); |
| 3286 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3287 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3288 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3289 | TestURLRequestContext context(true); |
| 3290 | context.set_network_delegate(&network_delegate); |
| 3291 | context.Init(); |
| 3292 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3293 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3294 | { |
| 3295 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3296 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3297 | http_server.GetURL("/set-cookie?__Secure-nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3298 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3299 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3300 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3301 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3302 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3303 | } |
| 3304 | |
| 3305 | // Verify that the cookie is not set. |
| 3306 | { |
| 3307 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3308 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3309 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3310 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3311 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3312 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3313 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3314 | EXPECT_EQ(d.data_received().find("__Secure-nonsecure-origin=1"), |
| 3315 | std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3316 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3317 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3318 | } |
| 3319 | } |
| 3320 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3321 | TEST_F(URLRequestTest, SecureCookiePrefixNonsecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3322 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3323 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3324 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3325 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3326 | |
| 3327 | TestNetworkDelegate network_delegate; |
| 3328 | TestURLRequestContext context(true); |
| 3329 | context.set_network_delegate(&network_delegate); |
| 3330 | context.Init(); |
| 3331 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3332 | // Try to set a non-Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3333 | { |
| 3334 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3335 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 3336 | https_server.GetURL("/set-cookie?__Secure-foo=1"), DEFAULT_PRIORITY, &d, |
| 3337 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3338 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3339 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3340 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3341 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3342 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3343 | } |
| 3344 | |
| 3345 | // Verify that the cookie is not set. |
| 3346 | { |
| 3347 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3348 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3349 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3350 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3351 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3352 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3353 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3354 | EXPECT_EQ(d.data_received().find("__Secure-foo=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3355 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3356 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3357 | } |
| 3358 | } |
| 3359 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3360 | TEST_F(URLRequestTest, SecureCookiePrefixSecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3361 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3362 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3363 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3364 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3365 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3366 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3367 | TestURLRequestContext context(true); |
| 3368 | context.set_network_delegate(&network_delegate); |
| 3369 | context.Init(); |
| 3370 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3371 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3372 | { |
| 3373 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3374 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3375 | https_server.GetURL("/set-cookie?__Secure-bar=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3376 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3377 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3378 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3379 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3380 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3381 | } |
| 3382 | |
| 3383 | // Verify that the cookie is set. |
| 3384 | { |
| 3385 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3386 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3387 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3388 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3389 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3390 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3391 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3392 | EXPECT_NE(d.data_received().find("__Secure-bar=1"), std::string::npos); |
| 3393 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3394 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3395 | } |
| 3396 | } |
| 3397 | |
| 3398 | // Tests that secure cookies can't be set on non-secure origins if strict secure |
| 3399 | // cookies are enabled. |
| 3400 | TEST_F(URLRequestTest, StrictSecureCookiesOnNonsecureOrigin) { |
| 3401 | EmbeddedTestServer http_server; |
| 3402 | http_server.AddDefaultHandlers( |
| 3403 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3404 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3405 | https_server.AddDefaultHandlers( |
| 3406 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3407 | ASSERT_TRUE(http_server.Start()); |
| 3408 | ASSERT_TRUE(https_server.Start()); |
| 3409 | |
Matt Menke | be9b6eb | 2018-10-18 12:11:44 | [diff] [blame] | 3410 | TestNetworkDelegate network_delegate; |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3411 | TestURLRequestContext context(true); |
| 3412 | context.set_network_delegate(&network_delegate); |
| 3413 | context.Init(); |
| 3414 | |
| 3415 | // Try to set a Secure cookie, with experimental features enabled. |
| 3416 | { |
| 3417 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3418 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3419 | http_server.GetURL("/set-cookie?nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3420 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3421 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3422 | d.RunUntilComplete(); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3423 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3424 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3425 | } |
| 3426 | |
| 3427 | // Verify that the cookie is not set. |
| 3428 | { |
| 3429 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3430 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3431 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3432 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3433 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3434 | d.RunUntilComplete(); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3435 | |
| 3436 | EXPECT_EQ(d.data_received().find("nonsecure-origin=1"), std::string::npos); |
| 3437 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3438 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3439 | } |
| 3440 | } |
| 3441 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3442 | // The parameter is true for same-site and false for cross-site requests. |
| 3443 | class URLRequestTestParameterizedSameSite |
| 3444 | : public URLRequestTest, |
| 3445 | public ::testing::WithParamInterface<bool> { |
| 3446 | protected: |
| 3447 | URLRequestTestParameterizedSameSite() { |
| 3448 | auto params = std::make_unique<HttpNetworkSession::Params>(); |
| 3449 | params->ignore_certificate_errors = true; |
| 3450 | context_.set_http_network_session_params(std::move(params)); |
| 3451 | context_.set_network_delegate(&network_delegate_); |
| 3452 | https_server_.AddDefaultHandlers( |
| 3453 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3454 | EXPECT_TRUE(https_server_.Start()); |
| 3455 | } |
| 3456 | |
| 3457 | // To be called after configuration of |context_| has been finalized. |
| 3458 | void InitContext() { context_.Init(); } |
| 3459 | |
| 3460 | const std::string kHost_ = "example.test"; |
| 3461 | const std::string kCrossHost_ = "cross-site.test"; |
| 3462 | TestURLRequestContext context_{true}; |
| 3463 | TestNetworkDelegate network_delegate_; |
| 3464 | base::HistogramTester histograms_; |
| 3465 | EmbeddedTestServer https_server_{EmbeddedTestServer::TYPE_HTTPS}; |
| 3466 | }; |
| 3467 | |
Victor Costan | 8fb98f6f | 2019-02-01 17:08:29 | [diff] [blame] | 3468 | INSTANTIATE_TEST_SUITE_P(URLRequestTest, |
| 3469 | URLRequestTestParameterizedSameSite, |
| 3470 | ::testing::Bool()); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3471 | |
| 3472 | TEST_P(URLRequestTestParameterizedSameSite, CookieAgeMetrics) { |
| 3473 | const bool same_site = GetParam(); |
| 3474 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3475 | InitContext(); |
| 3476 | |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3477 | EmbeddedTestServer http_server; |
| 3478 | http_server.AddDefaultHandlers( |
| 3479 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3480 | ASSERT_TRUE(http_server.Start()); |
| 3481 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3482 | // Set two test cookies. |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3483 | { |
| 3484 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3485 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3486 | http_server.GetURL(kHost_, "/set-cookie?cookie=value&cookie2=value2"), |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3487 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3488 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3489 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3490 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3491 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3492 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 0); |
| 3493 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", 0); |
| 3494 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", 0); |
| 3495 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3496 | 0); |
| 3497 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3498 | 0); |
| 3499 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", 0); |
| 3500 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", 0); |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3501 | } |
| 3502 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3503 | // Make a secure request. |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3504 | { |
| 3505 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3506 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3507 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3508 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3509 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3510 | req->set_initiator( |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3511 | url::Origin::Create(https_server_.GetURL(kInitiatingHost, "/"))); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3512 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3513 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3514 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3515 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 0); |
| 3516 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", |
| 3517 | !same_site); |
| 3518 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", |
| 3519 | same_site); |
| 3520 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3521 | 0); |
| 3522 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3523 | 0); |
| 3524 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", |
| 3525 | same_site ? 0 : 2); |
| 3526 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", |
| 3527 | same_site ? 2 : 0); |
| 3528 | EXPECT_TRUE(d.data_received().find("cookie=value") != std::string::npos); |
| 3529 | EXPECT_TRUE(d.data_received().find("cookie2=value2") != std::string::npos); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3530 | } |
| 3531 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3532 | // Make a non-secure request. |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3533 | { |
| 3534 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3535 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3536 | http_server.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3537 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3538 | req->set_site_for_cookies(http_server.GetURL(kInitiatingHost, "/")); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3539 | req->set_initiator( |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3540 | url::Origin::Create(http_server.GetURL(kInitiatingHost, "/"))); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3541 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3542 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3543 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", |
| 3544 | !same_site); |
| 3545 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", |
| 3546 | same_site); |
| 3547 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", |
| 3548 | !same_site); |
| 3549 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", |
| 3550 | same_site); |
| 3551 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3552 | same_site ? 0 : 2); |
| 3553 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3554 | same_site ? 2 : 0); |
| 3555 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", |
| 3556 | same_site ? 0 : 2); |
| 3557 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", |
| 3558 | same_site ? 2 : 0); |
| 3559 | EXPECT_TRUE(d.data_received().find("cookie=value") != std::string::npos); |
| 3560 | EXPECT_TRUE(d.data_received().find("cookie2=value2") != std::string::npos); |
| 3561 | } |
| 3562 | } |
| 3563 | |
| 3564 | // Cookies with secure attribute (no HSTS) --> k1pSecureAttribute |
| 3565 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3566 | CookieNetworkSecurityMetricSecureAttribute) { |
| 3567 | const bool same_site = GetParam(); |
| 3568 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3569 | InitContext(); |
| 3570 | |
| 3571 | // Set cookies. |
| 3572 | { |
| 3573 | TestDelegate d; |
| 3574 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3575 | https_server_.GetURL(kHost_, |
| 3576 | "/set-cookie?session-cookie=value;Secure&" |
| 3577 | "longlived-cookie=value;Secure;domain=" + |
| 3578 | kHost_ + ";Max-Age=360000"), |
| 3579 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3580 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3581 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3582 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3583 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3584 | } |
| 3585 | |
| 3586 | // Verify that the cookies fall into the correct metrics bucket. |
| 3587 | { |
| 3588 | TestDelegate d; |
| 3589 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3590 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3591 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3592 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3593 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3594 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3595 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3596 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3597 | // Static cast of boolean required for MSVC 1911. |
| 3598 | histograms_.ExpectBucketCount( |
| 3599 | "Cookie.NetworkSecurity", |
| 3600 | static_cast<int>(CookieNetworkSecurity::k1pSecureAttribute) | |
| 3601 | static_cast<int>(!same_site), |
| 3602 | 2); |
| 3603 | } |
| 3604 | } |
| 3605 | |
| 3606 | // Short-lived host cookie --> k1pHSTSHostCookie |
| 3607 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3608 | CookieNetworkSecurityMetricShortlivedHostCookie) { |
| 3609 | const bool same_site = GetParam(); |
| 3610 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3611 | |
| 3612 | TransportSecurityState transport_security_state; |
| 3613 | transport_security_state.AddHSTS( |
| 3614 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3615 | false /* include_subdomains */); |
| 3616 | context_.set_transport_security_state(&transport_security_state); |
| 3617 | InitContext(); |
| 3618 | |
| 3619 | // Set cookie. |
| 3620 | { |
| 3621 | TestDelegate d; |
| 3622 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3623 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;Max-Age=3600"), |
| 3624 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3625 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3626 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3627 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3628 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3629 | } |
| 3630 | |
| 3631 | // Verify that the cookie falls into the correct metrics bucket. |
| 3632 | { |
| 3633 | TestDelegate d; |
| 3634 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3635 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3636 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3637 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3638 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3639 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3640 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3641 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3642 | // Static cast of boolean required for MSVC 1911. |
| 3643 | histograms_.ExpectBucketCount( |
| 3644 | "Cookie.NetworkSecurity", |
| 3645 | static_cast<int>(CookieNetworkSecurity::k1pHSTSHostCookie) | |
| 3646 | static_cast<int>(!same_site), |
| 3647 | 1); |
| 3648 | } |
| 3649 | } |
| 3650 | |
| 3651 | // Long-lived (either due to expiry or due to being a session cookie) host |
| 3652 | // cookies --> k1pExpiringHSTSHostCookie |
| 3653 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3654 | CookieNetworkSecurityMetricLonglivedHostCookie) { |
| 3655 | const bool same_site = GetParam(); |
| 3656 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3657 | |
| 3658 | TransportSecurityState transport_security_state; |
| 3659 | transport_security_state.AddHSTS( |
| 3660 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3661 | false /* include_subdomains */); |
| 3662 | context_.set_transport_security_state(&transport_security_state); |
| 3663 | InitContext(); |
| 3664 | |
| 3665 | // Set cookies. |
| 3666 | { |
| 3667 | TestDelegate d; |
| 3668 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3669 | https_server_.GetURL(kHost_, |
| 3670 | "/set-cookie?session-cookie=value&" |
| 3671 | "longlived-cookie=value;Max-Age=360000"), |
| 3672 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3673 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3674 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3675 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3676 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3677 | } |
| 3678 | |
| 3679 | // Verify that the cookies fall into the correct metrics bucket. |
| 3680 | { |
| 3681 | TestDelegate d; |
| 3682 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3683 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3684 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3685 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3686 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3687 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3688 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3689 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3690 | // Static cast of boolean required for MSVC 1911. |
| 3691 | histograms_.ExpectBucketCount( |
| 3692 | "Cookie.NetworkSecurity", |
| 3693 | static_cast<int>(CookieNetworkSecurity::k1pExpiringHSTSHostCookie) | |
| 3694 | static_cast<int>(!same_site), |
| 3695 | 2); |
| 3696 | } |
| 3697 | } |
| 3698 | |
| 3699 | // Domain cookie with HSTS subdomains with cookie expiry before HSTS expiry --> |
| 3700 | // k1pHSTSSubdomainsIncluded |
| 3701 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3702 | CookieNetworkSecurityMetricShortlivedDomainCookie) { |
| 3703 | const bool same_site = GetParam(); |
| 3704 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3705 | |
| 3706 | TransportSecurityState transport_security_state; |
| 3707 | transport_security_state.AddHSTS( |
| 3708 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3709 | true /* include_subdomains */); |
| 3710 | context_.set_transport_security_state(&transport_security_state); |
| 3711 | InitContext(); |
| 3712 | |
| 3713 | // Set cookie. |
| 3714 | { |
| 3715 | TestDelegate d; |
| 3716 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3717 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;domain=" + |
| 3718 | kHost_ + ";Max-Age=3600"), |
| 3719 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3720 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3721 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3722 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3723 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3724 | } |
| 3725 | |
| 3726 | // Verify that the cookie falls into the correct metrics bucket. |
| 3727 | { |
| 3728 | TestDelegate d; |
| 3729 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3730 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3731 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3732 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3733 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3734 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3735 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3736 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3737 | // Static cast of boolean required for MSVC 1911. |
| 3738 | histograms_.ExpectBucketCount( |
| 3739 | "Cookie.NetworkSecurity", |
| 3740 | static_cast<int>(CookieNetworkSecurity::k1pHSTSSubdomainsIncluded) | |
| 3741 | static_cast<int>(!same_site), |
| 3742 | 1); |
| 3743 | } |
| 3744 | } |
| 3745 | |
| 3746 | // Long-lived (either due to expiry or due to being a session cookie) domain |
| 3747 | // cookies with HSTS subdomains --> k1pExpiringHSTSSubdomainsIncluded |
| 3748 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3749 | CookieNetworkSecurityMetricLonglivedDomainCookie) { |
| 3750 | const bool same_site = GetParam(); |
| 3751 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3752 | |
| 3753 | TransportSecurityState transport_security_state; |
| 3754 | transport_security_state.AddHSTS( |
| 3755 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3756 | true /* include_subdomains */); |
| 3757 | context_.set_transport_security_state(&transport_security_state); |
| 3758 | InitContext(); |
| 3759 | |
| 3760 | // Set cookies. |
| 3761 | { |
| 3762 | TestDelegate d; |
| 3763 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3764 | https_server_.GetURL( |
| 3765 | kHost_, "/set-cookie?session-cookie=value;domain=" + kHost_ + "&" + |
| 3766 | "longlived-cookie=value;domain=" + kHost_ + |
| 3767 | ";Max-Age=360000"), |
| 3768 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3769 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3770 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3771 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3772 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3773 | } |
| 3774 | |
| 3775 | // Verify that the cookies fall into the correct metrics bucket. |
| 3776 | { |
| 3777 | TestDelegate d; |
| 3778 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3779 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3780 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3781 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3782 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3783 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3784 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3785 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3786 | // Static cast of boolean required for MSVC 1911. |
| 3787 | histograms_.ExpectBucketCount( |
| 3788 | "Cookie.NetworkSecurity", |
| 3789 | static_cast<int>( |
| 3790 | CookieNetworkSecurity::k1pExpiringHSTSSubdomainsIncluded) | |
| 3791 | static_cast<int>(!same_site), |
| 3792 | 2); |
| 3793 | } |
| 3794 | } |
| 3795 | |
| 3796 | // Domain cookie with HSTS subdomains not included --> k1pHSTSSpoofable |
| 3797 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3798 | CookieNetworkSecurityMetricSpoofableDomainCookie) { |
| 3799 | const bool same_site = GetParam(); |
| 3800 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3801 | |
| 3802 | TransportSecurityState transport_security_state; |
| 3803 | transport_security_state.AddHSTS( |
| 3804 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3805 | false /* include_subdomains */); |
| 3806 | context_.set_transport_security_state(&transport_security_state); |
| 3807 | InitContext(); |
| 3808 | |
| 3809 | // Set cookie. |
| 3810 | { |
| 3811 | TestDelegate d; |
| 3812 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3813 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;domain=" + |
| 3814 | kHost_ + ";Max-Age=3600"), |
| 3815 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3816 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3817 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3818 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3819 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3820 | } |
| 3821 | |
| 3822 | // Verify that the cookie falls into the correct metrics bucket. |
| 3823 | { |
| 3824 | TestDelegate d; |
| 3825 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3826 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3827 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3828 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3829 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3830 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3831 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3832 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3833 | // Static cast of boolean required for MSVC 1911. |
| 3834 | histograms_.ExpectBucketCount( |
| 3835 | "Cookie.NetworkSecurity", |
| 3836 | static_cast<int>(CookieNetworkSecurity::k1pHSTSSpoofable) | |
| 3837 | static_cast<int>(!same_site), |
| 3838 | 1); |
| 3839 | } |
| 3840 | } |
| 3841 | |
| 3842 | // Cookie without HSTS --> k1p(Non)SecureConnection |
| 3843 | TEST_P(URLRequestTestParameterizedSameSite, CookieNetworkSecurityMetricNoHSTS) { |
| 3844 | const bool same_site = GetParam(); |
| 3845 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3846 | InitContext(); |
| 3847 | |
| 3848 | EmbeddedTestServer http_server; |
| 3849 | http_server.AddDefaultHandlers( |
| 3850 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3851 | ASSERT_TRUE(http_server.Start()); |
| 3852 | |
| 3853 | // Set cookies. |
| 3854 | { |
| 3855 | TestDelegate d; |
| 3856 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3857 | https_server_.GetURL(kHost_, |
| 3858 | "/set-cookie?cookie=value;domain=" + kHost_ + |
| 3859 | ";Max-Age=3600&host-cookie=value"), |
| 3860 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3861 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3862 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3863 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3864 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3865 | } |
| 3866 | |
| 3867 | // Verify that the cookie falls into the correct metrics bucket. |
| 3868 | { |
| 3869 | TestDelegate d; |
| 3870 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3871 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3872 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3873 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3874 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3875 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3876 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3877 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3878 | // Static cast of boolean required for MSVC 1911. |
| 3879 | histograms_.ExpectBucketCount( |
| 3880 | "Cookie.NetworkSecurity", |
| 3881 | static_cast<int>(CookieNetworkSecurity::k1pSecureConnection) | |
| 3882 | static_cast<int>(!same_site), |
| 3883 | 2); |
| 3884 | } |
| 3885 | |
| 3886 | // Verify that the cookie falls into the correct metrics bucket. |
| 3887 | { |
| 3888 | TestDelegate d; |
| 3889 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3890 | http_server.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3891 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3892 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3893 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3894 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3895 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3896 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 4); |
| 3897 | // Static cast of boolean required for MSVC 1911. |
| 3898 | histograms_.ExpectBucketCount( |
| 3899 | "Cookie.NetworkSecurity", |
| 3900 | static_cast<int>(CookieNetworkSecurity::k1pSecureConnection) | |
| 3901 | static_cast<int>(!same_site), |
| 3902 | 2); |
| 3903 | // Static cast of boolean required for MSVC 1911. |
| 3904 | histograms_.ExpectBucketCount( |
| 3905 | "Cookie.NetworkSecurity", |
| 3906 | static_cast<int>(CookieNetworkSecurity::k1pNonsecureConnection) | |
| 3907 | static_cast<int>(!same_site), |
| 3908 | 2); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3909 | } |
| 3910 | } |
| 3911 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3912 | // Tests that a request is cancelled while entering suspend mode. Uses mocks |
| 3913 | // rather than a spawned test server because the connection used to talk to |
| 3914 | // the test server is affected by entering suspend mode on Android. |
| 3915 | TEST_F(URLRequestTest, CancelOnSuspend) { |
| 3916 | TestPowerMonitorSource* power_monitor_source = new TestPowerMonitorSource(); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3917 | base::PowerMonitor power_monitor(base::WrapUnique(power_monitor_source)); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3918 | |
| 3919 | URLRequestFailedJob::AddUrlHandler(); |
| 3920 | |
| 3921 | TestDelegate d; |
| 3922 | // Request that just hangs. |
| 3923 | GURL url(URLRequestFailedJob::GetMockHttpUrl(ERR_IO_PENDING)); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3924 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3925 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3926 | r->Start(); |
| 3927 | |
| 3928 | power_monitor_source->Suspend(); |
| 3929 | // Wait for the suspend notification to cause the request to fail. |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3930 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3931 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3932 | EXPECT_TRUE(d.request_failed()); |
| 3933 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 3934 | |
| 3935 | URLRequestFilter::GetInstance()->ClearHandlers(); |
| 3936 | |
| 3937 | // Shouldn't be needed, but just in case. |
| 3938 | power_monitor_source->Resume(); |
| 3939 | } |
| 3940 | |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3941 | // FixedDateNetworkDelegate swaps out the server's HTTP Date response header |
| 3942 | // value for the |fixed_date| argument given to the constructor. |
| 3943 | class FixedDateNetworkDelegate : public TestNetworkDelegate { |
| 3944 | public: |
| 3945 | explicit FixedDateNetworkDelegate(const std::string& fixed_date) |
| 3946 | : fixed_date_(fixed_date) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 3947 | ~FixedDateNetworkDelegate() override = default; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3948 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3949 | // NetworkDelegate implementation |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 3950 | int OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3951 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 3952 | CompletionOnceCallback callback, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3953 | const HttpResponseHeaders* original_response_headers, |
| 3954 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 3955 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3956 | |
| 3957 | private: |
| 3958 | std::string fixed_date_; |
| 3959 | |
| 3960 | DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); |
| 3961 | }; |
| 3962 | |
| 3963 | int FixedDateNetworkDelegate::OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3964 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 3965 | CompletionOnceCallback callback, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3966 | const HttpResponseHeaders* original_response_headers, |
| 3967 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3968 | GURL* allowed_unsafe_redirect_url) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3969 | HttpResponseHeaders* new_response_headers = |
| 3970 | new HttpResponseHeaders(original_response_headers->raw_headers()); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3971 | |
| 3972 | new_response_headers->RemoveHeader("Date"); |
| 3973 | new_response_headers->AddHeader("Date: " + fixed_date_); |
| 3974 | |
| 3975 | *override_response_headers = new_response_headers; |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 3976 | return TestNetworkDelegate::OnHeadersReceived( |
| 3977 | request, std::move(callback), original_response_headers, |
| 3978 | override_response_headers, allowed_unsafe_redirect_url); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3979 | } |
| 3980 | |
| 3981 | // Test that cookie expiration times are adjusted for server/client clock |
| 3982 | // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date |
| 3983 | // headers by defaulting to GMT. (crbug.com/135131) |
| 3984 | TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3985 | HttpTestServer test_server; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3986 | ASSERT_TRUE(test_server.Start()); |
| 3987 | |
| 3988 | // Set up an expired cookie. |
| 3989 | { |
| 3990 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3991 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3992 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3993 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3994 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3995 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3996 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3997 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3998 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3999 | } |
| 4000 | // Verify that the cookie is not set. |
| 4001 | { |
| 4002 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4003 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4004 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4005 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4006 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 4007 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4008 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4009 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4010 | |
| 4011 | EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos); |
| 4012 | } |
| 4013 | // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier. |
| 4014 | { |
| 4015 | FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC"); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4016 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4017 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4018 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4019 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4020 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4021 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4022 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4023 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4024 | } |
| 4025 | // Verify that the cookie is set. |
| 4026 | { |
| 4027 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4028 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4029 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4030 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4031 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 4032 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4033 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4034 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4035 | |
| 4036 | EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos); |
| 4037 | } |
| 4038 | } |
| 4039 | |
| 4040 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4041 | // Check that it is impossible to change the referrer in the extra headers of |
| 4042 | // an URLRequest. |
| 4043 | TEST_F(URLRequestTest, DoNotOverrideReferrer) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4044 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4045 | ASSERT_TRUE(test_server.Start()); |
| 4046 | |
| 4047 | // If extra headers contain referer and the request contains a referer, |
| 4048 | // only the latter shall be respected. |
| 4049 | { |
| 4050 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4051 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4052 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 4053 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4054 | req->SetReferrer("http://foo.com/"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4055 | |
| 4056 | HttpRequestHeaders headers; |
| 4057 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4058 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4059 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4060 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4061 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4062 | |
| 4063 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 4064 | } |
| 4065 | |
| 4066 | // If extra headers contain a referer but the request does not, no referer |
| 4067 | // shall be sent in the header. |
| 4068 | { |
| 4069 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4070 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4071 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 4072 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4073 | |
| 4074 | HttpRequestHeaders headers; |
| 4075 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4076 | req->SetExtraRequestHeaders(headers); |
| 4077 | req->SetLoadFlags(LOAD_VALIDATE_CACHE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4078 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4079 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4080 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4081 | |
| 4082 | EXPECT_EQ("None", d.data_received()); |
| 4083 | } |
| 4084 | } |
| 4085 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4086 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4087 | public: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 4088 | URLRequestTestHTTP() : test_server_(base::FilePath(kTestFilePath)) {} |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4089 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4090 | protected: |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4091 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 4092 | // |request_method| is the method to use for the initial request. |
| 4093 | // |redirect_method| is the method that is expected to be used for the second |
| 4094 | // request, after redirection. |
| 4095 | // If |include_data| is true, data is uploaded with the request. The |
| 4096 | // response body is expected to match it exactly, if and only if |
| 4097 | // |request_method| == |redirect_method|. |
| 4098 | void HTTPRedirectMethodTest(const GURL& redirect_url, |
| 4099 | const std::string& request_method, |
| 4100 | const std::string& redirect_method, |
| 4101 | bool include_data) { |
| 4102 | static const char kData[] = "hello world"; |
| 4103 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4104 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4105 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4106 | req->set_method(request_method); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4107 | if (include_data) { |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4108 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4109 | HttpRequestHeaders headers; |
| 4110 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 4111 | base::NumberToString(base::size(kData) - 1)); |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 4112 | headers.SetHeader(HttpRequestHeaders::kContentType, "text/plain"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4113 | req->SetExtraRequestHeaders(headers); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4114 | } |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4115 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4116 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4117 | EXPECT_EQ(redirect_method, req->method()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4118 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4119 | if (include_data) { |
| 4120 | if (request_method == redirect_method) { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 4121 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 4122 | HttpRequestHeaders::kContentLength)); |
| 4123 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 4124 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4125 | EXPECT_EQ(kData, d.data_received()); |
| 4126 | } else { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 4127 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 4128 | HttpRequestHeaders::kContentLength)); |
| 4129 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 4130 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4131 | EXPECT_NE(kData, d.data_received()); |
| 4132 | } |
| 4133 | } |
| 4134 | if (HasFailure()) |
| 4135 | LOG(WARNING) << "Request method was: " << request_method; |
| 4136 | } |
| 4137 | |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4138 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. It's also |
| 4139 | // used as the initial origin. |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4140 | // |request_method| is the method to use for the initial request. |
| 4141 | // |redirect_method| is the method that is expected to be used for the second |
| 4142 | // request, after redirection. |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4143 | // |expected_origin_value| is the expected value for the Origin header after |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4144 | // redirection. If empty, expects that there will be no Origin header. |
| 4145 | void HTTPRedirectOriginHeaderTest(const GURL& redirect_url, |
| 4146 | const std::string& request_method, |
| 4147 | const std::string& redirect_method, |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4148 | const std::string& expected_origin_value) { |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4149 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4150 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4151 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4152 | req->set_method(request_method); |
| 4153 | req->SetExtraRequestHeaderByName(HttpRequestHeaders::kOrigin, |
| 4154 | redirect_url.GetOrigin().spec(), false); |
| 4155 | req->Start(); |
| 4156 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4157 | d.RunUntilComplete(); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4158 | |
| 4159 | EXPECT_EQ(redirect_method, req->method()); |
| 4160 | // Note that there is no check for request success here because, for |
| 4161 | // purposes of testing, the request very well may fail. For example, if the |
| 4162 | // test redirects to an HTTPS server from an HTTP origin, thus it is cross |
| 4163 | // origin, there is not an HTTPS server in this unit test framework, so the |
| 4164 | // request would fail. However, that's fine, as long as the request headers |
| 4165 | // are in order and pass the checks below. |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4166 | if (expected_origin_value.empty()) { |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4167 | EXPECT_FALSE( |
| 4168 | req->extra_request_headers().HasHeader(HttpRequestHeaders::kOrigin)); |
| 4169 | } else { |
| 4170 | std::string origin_header; |
| 4171 | EXPECT_TRUE(req->extra_request_headers().GetHeader( |
| 4172 | HttpRequestHeaders::kOrigin, &origin_header)); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4173 | EXPECT_EQ(expected_origin_value, origin_header); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4174 | } |
| 4175 | } |
| 4176 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4177 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4178 | const int kMsgSize = 20000; // multiple of 10 |
| 4179 | const int kIterations = 50; |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 4180 | char* uploadBytes = new char[kMsgSize+1]; |
| 4181 | char* ptr = uploadBytes; |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4182 | char marker = 'a'; |
| 4183 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 4184 | memcpy(ptr, "----------", 10); |
| 4185 | ptr += 10; |
| 4186 | if (idx % 100 == 0) { |
| 4187 | ptr--; |
| 4188 | *ptr++ = marker; |
| 4189 | if (++marker > 'z') |
| 4190 | marker = 'a'; |
| 4191 | } |
| 4192 | } |
| 4193 | uploadBytes[kMsgSize] = '\0'; |
| 4194 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4195 | for (int i = 0; i < kIterations; ++i) { |
| 4196 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4197 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4198 | test_server_.GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 4199 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4200 | r->set_method(method.c_str()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4201 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4202 | r->set_upload(CreateSimpleUploadData(uploadBytes)); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4203 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4204 | r->Start(); |
| 4205 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4206 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4207 | d.RunUntilComplete(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4208 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4209 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 4210 | << d.request_status(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4211 | |
| 4212 | EXPECT_FALSE(d.received_data_before_response()); |
| 4213 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4214 | } |
| 4215 | delete[] uploadBytes; |
| 4216 | } |
| 4217 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 4218 | bool DoManyCookiesRequest(int num_cookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4219 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4220 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4221 | test_server_.GetURL("/set-many-cookies?" + |
Raul Tambre | 8c1981d | 2019-02-08 02:22:26 | [diff] [blame] | 4222 | base::NumberToString(num_cookies)), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4223 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4224 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4225 | r->Start(); |
| 4226 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4227 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4228 | d.RunUntilComplete(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4229 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4230 | if (d.request_status() != OK) { |
| 4231 | EXPECT_EQ(ERR_RESPONSE_HEADERS_TOO_BIG, d.request_status()); |
| 4232 | return false; |
| 4233 | } |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4234 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4235 | return true; |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4236 | } |
| 4237 | |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4238 | HttpTestServer* http_test_server() { return &test_server_; } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 4239 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4240 | private: |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4241 | HttpTestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4242 | }; |
| 4243 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4244 | namespace { |
| 4245 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4246 | std::unique_ptr<test_server::HttpResponse> HandleRedirectConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4247 | const test_server::HttpRequest& request) { |
| 4248 | if (request.headers.find("Host") == request.headers.end() || |
| 4249 | request.headers.at("Host") != "www.redirect.com" || |
| 4250 | request.method != test_server::METHOD_CONNECT) { |
| 4251 | return nullptr; |
| 4252 | } |
| 4253 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4254 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4255 | new test_server::BasicHttpResponse); |
| 4256 | http_response->set_code(HTTP_FOUND); |
| 4257 | http_response->AddCustomHeader("Location", |
| 4258 | "http://www.destination.com/foo.js"); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 4259 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4260 | } |
| 4261 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4262 | class TestSSLConfigService : public SSLConfigService { |
| 4263 | public: |
Nick Harper | 2243e800 | 2018-09-28 20:33:26 | [diff] [blame] | 4264 | TestSSLConfigService() |
| 4265 | : min_version_(kDefaultSSLVersionMin), |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4266 | max_version_(kDefaultSSLVersionMax) {} |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4267 | ~TestSSLConfigService() override = default; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4268 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4269 | void set_max_version(uint16_t version) { max_version_ = version; } |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4270 | void set_min_version(uint16_t version) { min_version_ = version; } |
| 4271 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4272 | // SSLConfigService: |
| 4273 | void GetSSLConfig(SSLConfig* config) override { |
| 4274 | *config = SSLConfig(); |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4275 | config->version_min = min_version_; |
| 4276 | config->version_max = max_version_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4277 | } |
| 4278 | |
Nick Harper | 89bc721 | 2018-07-31 19:07:57 | [diff] [blame] | 4279 | bool CanShareConnectionWithClientCerts( |
| 4280 | const std::string& hostname) const override { |
| 4281 | return false; |
| 4282 | } |
| 4283 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4284 | private: |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4285 | uint16_t min_version_; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4286 | uint16_t max_version_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4287 | }; |
| 4288 | |
Nick Harper | 89bc721 | 2018-07-31 19:07:57 | [diff] [blame] | 4289 | } // namespace |
| 4290 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4291 | // 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] | 4292 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4293 | // The EmbeddedTestServer will return a 302 response, which we should not |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4294 | // follow. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 4295 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4296 | http_test_server()->RegisterRequestHandler( |
Matt Menke | 2b2a6047 | 2018-02-07 02:12:12 | [diff] [blame] | 4297 | base::BindRepeating(&HandleRedirectConnect)); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4298 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4299 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4300 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4301 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4302 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4303 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4304 | TestDelegate d; |
| 4305 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4306 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4307 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 4308 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4309 | r->Start(); |
| 4310 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4311 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4312 | d.RunUntilComplete(); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4313 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4314 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4315 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4316 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4317 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4318 | // We should not have followed the redirect. |
| 4319 | EXPECT_EQ(0, d.received_redirect_count()); |
| 4320 | } |
| 4321 | } |
| 4322 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4323 | // This is the same as the previous test, but checks that the network delegate |
| 4324 | // registers the error. |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 4325 | TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4326 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4327 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4328 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4329 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4330 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4331 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4332 | TestDelegate d; |
| 4333 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4334 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4335 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 4336 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4337 | r->Start(); |
| 4338 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4339 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4340 | d.RunUntilComplete(); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4341 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4342 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4343 | EXPECT_FALSE(r->proxy_server().is_valid()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4344 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4345 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4346 | // We should not have followed the redirect. |
| 4347 | EXPECT_EQ(0, d.received_redirect_count()); |
| 4348 | |
| 4349 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4350 | EXPECT_THAT(network_delegate.last_error(), |
| 4351 | IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4352 | } |
| 4353 | } |
| 4354 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4355 | // Tests that we can block and asynchronously return OK in various stages. |
| 4356 | TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) { |
| 4357 | static const BlockingNetworkDelegate::Stage blocking_stages[] = { |
| 4358 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 4359 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 4360 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED |
| 4361 | }; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 4362 | static const size_t blocking_stages_length = base::size(blocking_stages); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4363 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4364 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4365 | |
| 4366 | TestDelegate d; |
| 4367 | BlockingNetworkDelegate network_delegate( |
| 4368 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4369 | network_delegate.set_block_on( |
| 4370 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST | |
| 4371 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS | |
| 4372 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 4373 | |
| 4374 | TestURLRequestContext context(true); |
| 4375 | context.set_network_delegate(&network_delegate); |
| 4376 | context.Init(); |
| 4377 | |
| 4378 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4379 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4380 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4381 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4382 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4383 | r->Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4384 | for (size_t i = 0; i < blocking_stages_length; ++i) { |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4385 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4386 | EXPECT_EQ(blocking_stages[i], |
| 4387 | network_delegate.stage_blocked_for_callback()); |
| 4388 | network_delegate.DoCallback(OK); |
| 4389 | } |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4390 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4391 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4392 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4393 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4394 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4395 | } |
| 4396 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4397 | } |
| 4398 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4399 | // Tests that the network delegate can block and cancel a request. |
| 4400 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4401 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4402 | |
| 4403 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4404 | BlockingNetworkDelegate network_delegate( |
| 4405 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4406 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 4407 | network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4408 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4409 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4410 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4411 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4412 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4413 | std::unique_ptr<URLRequest> r( |
| 4414 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4415 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4416 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4417 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4418 | d.RunUntilComplete(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4419 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4420 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4421 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4422 | EXPECT_EQ(ERR_EMPTY_RESPONSE, d.request_status()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4423 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4424 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4425 | } |
| 4426 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4427 | } |
| 4428 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4429 | // Helper function for NetworkDelegateCancelRequestAsynchronously and |
| 4430 | // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network |
| 4431 | // delegate operating in |block_mode| and a request for |url|. It blocks the |
| 4432 | // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT. |
| 4433 | void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode, |
| 4434 | BlockingNetworkDelegate::Stage stage, |
| 4435 | const GURL& url) { |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4436 | TestDelegate d; |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4437 | BlockingNetworkDelegate network_delegate(block_mode); |
| 4438 | network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT); |
| 4439 | network_delegate.set_block_on(stage); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4440 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4441 | TestURLRequestContext context(true); |
| 4442 | context.set_network_delegate(&network_delegate); |
| 4443 | context.Init(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4444 | |
| 4445 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4446 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4447 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4448 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4449 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4450 | d.RunUntilComplete(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4451 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4452 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4453 | if (stage == BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST || |
| 4454 | stage == BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS) { |
| 4455 | EXPECT_FALSE(r->proxy_server().is_valid()); |
| 4456 | } else if (stage == BlockingNetworkDelegate::ON_HEADERS_RECEIVED) { |
| 4457 | EXPECT_TRUE(r->proxy_server().is_direct()); |
| 4458 | } else { |
| 4459 | NOTREACHED(); |
| 4460 | } |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4461 | EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, d.request_status()); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4462 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4463 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4464 | } |
| 4465 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4466 | } |
| 4467 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4468 | // The following 3 tests check that the network delegate can cancel a request |
| 4469 | // synchronously in various stages of the request. |
| 4470 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4471 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4472 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4473 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4474 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4475 | } |
| 4476 | |
| 4477 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4478 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4479 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4480 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4481 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4482 | } |
| 4483 | |
| 4484 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4485 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4486 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4487 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4488 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4489 | } |
| 4490 | |
| 4491 | // The following 3 tests check that the network delegate can cancel a request |
| 4492 | // asynchronously in various stages of the request. |
| 4493 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4494 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4495 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4496 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4497 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4498 | } |
| 4499 | |
| 4500 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4501 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4502 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4503 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4504 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4505 | } |
| 4506 | |
| 4507 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4508 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4509 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4510 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4511 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4512 | } |
| 4513 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4514 | // Tests that the network delegate can block and redirect a request to a new |
| 4515 | // URL. |
| 4516 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4517 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4518 | |
| 4519 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4520 | BlockingNetworkDelegate network_delegate( |
| 4521 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4522 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4523 | GURL redirect_url("http://does.not.resolve.test/simple.html"); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4524 | network_delegate.set_redirect_url(redirect_url); |
| 4525 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4526 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4527 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4528 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4529 | { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4530 | GURL original_url("http://does.not.resolve.test/defaultresponse"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4531 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4532 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4533 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4534 | // Quit after hitting the redirect, so can check the headers. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4535 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 4536 | d.RunUntilRedirect(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4537 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4538 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4539 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4540 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4541 | std::string location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 4542 | ASSERT_TRUE( |
| 4543 | r->response_headers()->EnumerateHeader(nullptr, "Location", &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4544 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4545 | |
| 4546 | // Let the request finish. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 4547 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 4548 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4549 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4550 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4551 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4552 | http_test_server()->host_port_pair()), |
| 4553 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4554 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4555 | // through an untunneled proxy. |
| 4556 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4557 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4558 | http_test_server()->host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 4559 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4560 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4561 | EXPECT_EQ(redirect_url, r->url()); |
| 4562 | EXPECT_EQ(original_url, r->original_url()); |
| 4563 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4564 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4565 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4566 | } |
| 4567 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4568 | } |
| 4569 | |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4570 | // Tests that the network delegate can block and redirect a request to a new |
| 4571 | // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 4572 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4573 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4574 | |
| 4575 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4576 | BlockingNetworkDelegate network_delegate( |
| 4577 | BlockingNetworkDelegate::SYNCHRONOUS); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4578 | GURL redirect_url("http://does.not.resolve.test/simple.html"); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4579 | network_delegate.set_redirect_url(redirect_url); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4580 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4581 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4582 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4583 | |
| 4584 | { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4585 | GURL original_url("http://does.not.resolve.test/defaultresponse"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4586 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4587 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4588 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4589 | // Quit after hitting the redirect, so can check the headers. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4590 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 4591 | d.RunUntilRedirect(); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4592 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4593 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4594 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4595 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4596 | std::string location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 4597 | ASSERT_TRUE( |
| 4598 | r->response_headers()->EnumerateHeader(nullptr, "Location", &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4599 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4600 | |
| 4601 | // Let the request finish. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 4602 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 4603 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4604 | d.RunUntilComplete(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4605 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4606 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4607 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4608 | http_test_server()->host_port_pair()), |
| 4609 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4610 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4611 | // through an untunneled proxy. |
| 4612 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4613 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4614 | http_test_server()->host_port_pair())); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4615 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4616 | EXPECT_EQ(redirect_url, r->url()); |
| 4617 | EXPECT_EQ(original_url, r->original_url()); |
| 4618 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4619 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4620 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4621 | } |
| 4622 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4623 | } |
| 4624 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4625 | // Tests that redirects caused by the network delegate preserve POST data. |
| 4626 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4627 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4628 | |
| 4629 | const char kData[] = "hello world"; |
| 4630 | |
| 4631 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4632 | BlockingNetworkDelegate network_delegate( |
| 4633 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4634 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4635 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4636 | network_delegate.set_redirect_url(redirect_url); |
| 4637 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4638 | TestURLRequestContext context(true); |
| 4639 | context.set_network_delegate(&network_delegate); |
| 4640 | context.Init(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4641 | |
| 4642 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4643 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4644 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4645 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4646 | r->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4647 | r->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4648 | HttpRequestHeaders headers; |
| 4649 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 4650 | base::NumberToString(base::size(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4651 | r->SetExtraRequestHeaders(headers); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4652 | |
| 4653 | // Quit after hitting the redirect, so can check the headers. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4654 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 4655 | d.RunUntilRedirect(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4656 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4657 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4658 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4659 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4660 | std::string location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 4661 | ASSERT_TRUE( |
| 4662 | r->response_headers()->EnumerateHeader(nullptr, "Location", &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4663 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4664 | |
| 4665 | // Let the request finish. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 4666 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 4667 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4668 | d.RunUntilComplete(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4669 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4670 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4671 | EXPECT_EQ(redirect_url, r->url()); |
| 4672 | EXPECT_EQ(original_url, r->original_url()); |
| 4673 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4674 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4675 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4676 | EXPECT_EQ("POST", r->method()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4677 | EXPECT_EQ(kData, d.data_received()); |
| 4678 | } |
| 4679 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4680 | } |
| 4681 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4682 | // Tests that the network delegate can block and redirect a request to a new |
| 4683 | // URL during OnHeadersReceived. |
| 4684 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4685 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4686 | |
| 4687 | TestDelegate d; |
| 4688 | BlockingNetworkDelegate network_delegate( |
| 4689 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4690 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4691 | GURL redirect_url("http://does.not.resolve.test/simple.html"); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4692 | network_delegate.set_redirect_on_headers_received_url(redirect_url); |
| 4693 | |
| 4694 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4695 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4696 | |
| 4697 | { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4698 | GURL original_url("http://does.not.resolve.test/defaultresponse"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4699 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4700 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4701 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4702 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4703 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4704 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4705 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4706 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4707 | http_test_server()->host_port_pair()), |
| 4708 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4709 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4710 | // through an untunneled proxy. |
| 4711 | EXPECT_EQ(2, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4712 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4713 | http_test_server()->host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4714 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4715 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4716 | EXPECT_EQ(redirect_url, r->url()); |
| 4717 | EXPECT_EQ(original_url, r->original_url()); |
| 4718 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4719 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 4720 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4721 | } |
| 4722 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4723 | } |
| 4724 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4725 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4726 | // by taking no action. This indicates that the NetworkDelegate does not want to |
| 4727 | // handle the challenge, and is passing the buck along to the |
| 4728 | // URLRequest::Delegate. |
| 4729 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4730 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4731 | |
| 4732 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4733 | BlockingNetworkDelegate network_delegate( |
| 4734 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4735 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4736 | TestURLRequestContext context(true); |
| 4737 | context.set_network_delegate(&network_delegate); |
| 4738 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4739 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4740 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4741 | |
| 4742 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4743 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4744 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4745 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4746 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4747 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4748 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4749 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4750 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4751 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4752 | EXPECT_TRUE(d.auth_required_called()); |
| 4753 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4754 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4755 | } |
| 4756 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4757 | } |
| 4758 | |
| 4759 | TEST_F(URLRequestTestHTTP, |
| 4760 | NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4761 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4762 | |
| 4763 | TestDelegate d; |
| 4764 | BlockingNetworkDelegate network_delegate( |
| 4765 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4766 | |
| 4767 | TestURLRequestContext context(true); |
| 4768 | context.set_network_delegate(&network_delegate); |
| 4769 | context.Init(); |
| 4770 | |
| 4771 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 4772 | |
| 4773 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4774 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4775 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4776 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4777 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4778 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4779 | d.RunUntilComplete(); |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4780 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4781 | { |
| 4782 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4783 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4784 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4785 | } |
| 4786 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4787 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4788 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4789 | EXPECT_TRUE(d.auth_required_called()); |
| 4790 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4791 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4792 | } |
| 4793 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4794 | } |
| 4795 | |
| 4796 | // Tests that the network delegate can synchronously complete OnAuthRequired |
[email protected] | 1e110eae | 2013-05-10 22:02:40 | [diff] [blame] | 4797 | // by setting credentials. |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4798 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4799 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4800 | |
| 4801 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4802 | BlockingNetworkDelegate network_delegate( |
| 4803 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4804 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4805 | network_delegate.set_auth_retval( |
| 4806 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4807 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4808 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4809 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4810 | TestURLRequestContext context(true); |
| 4811 | context.set_network_delegate(&network_delegate); |
| 4812 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4813 | |
| 4814 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4815 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4816 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4817 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4818 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4819 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4820 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4821 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4822 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4823 | EXPECT_FALSE(d.auth_required_called()); |
| 4824 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4825 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4826 | } |
| 4827 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4828 | } |
| 4829 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4830 | // Same as above, but also tests that GetFullRequestHeaders returns the proper |
| 4831 | // headers (for the first or second request) when called at the proper times. |
| 4832 | TEST_F(URLRequestTestHTTP, |
| 4833 | NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4834 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4835 | |
| 4836 | TestDelegate d; |
| 4837 | BlockingNetworkDelegate network_delegate( |
| 4838 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4839 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 4840 | network_delegate.set_auth_retval( |
| 4841 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4842 | |
| 4843 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
| 4844 | |
| 4845 | TestURLRequestContext context(true); |
| 4846 | context.set_network_delegate(&network_delegate); |
| 4847 | context.Init(); |
| 4848 | |
| 4849 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4850 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4851 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4852 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4853 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4854 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4855 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4856 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4857 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4858 | EXPECT_FALSE(d.auth_required_called()); |
| 4859 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4860 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4861 | |
| 4862 | { |
| 4863 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4864 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4865 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
| 4866 | } |
| 4867 | } |
| 4868 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4869 | } |
| 4870 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4871 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4872 | // by cancelling authentication. |
| 4873 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4874 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4875 | |
| 4876 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4877 | BlockingNetworkDelegate network_delegate( |
| 4878 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4879 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4880 | network_delegate.set_auth_retval( |
| 4881 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4882 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4883 | TestURLRequestContext context(true); |
| 4884 | context.set_network_delegate(&network_delegate); |
| 4885 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4886 | |
| 4887 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4888 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4889 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4890 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4891 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4892 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4893 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4894 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4895 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4896 | EXPECT_FALSE(d.auth_required_called()); |
| 4897 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4898 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4899 | } |
| 4900 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4901 | } |
| 4902 | |
| 4903 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4904 | // by taking no action. This indicates that the NetworkDelegate does not want |
| 4905 | // to handle the challenge, and is passing the buck along to the |
| 4906 | // URLRequest::Delegate. |
| 4907 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4908 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4909 | |
| 4910 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4911 | BlockingNetworkDelegate network_delegate( |
| 4912 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4913 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4914 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4915 | TestURLRequestContext context(true); |
| 4916 | context.set_network_delegate(&network_delegate); |
| 4917 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4918 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4919 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4920 | |
| 4921 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4922 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4923 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4924 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4925 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4926 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4927 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4928 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4929 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4930 | EXPECT_TRUE(d.auth_required_called()); |
| 4931 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4932 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4933 | } |
| 4934 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4935 | } |
| 4936 | |
| 4937 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4938 | // by setting credentials. |
| 4939 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4940 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4941 | |
| 4942 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4943 | BlockingNetworkDelegate network_delegate( |
| 4944 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4945 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4946 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4947 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4948 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4949 | AuthCredentials auth_credentials(kUser, kSecret); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4950 | network_delegate.set_auth_credentials(auth_credentials); |
| 4951 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4952 | TestURLRequestContext context(true); |
| 4953 | context.set_network_delegate(&network_delegate); |
| 4954 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4955 | |
| 4956 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4957 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4958 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4959 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4960 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4961 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4962 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4963 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4964 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4965 | EXPECT_FALSE(d.auth_required_called()); |
| 4966 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4967 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4968 | } |
| 4969 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4970 | } |
| 4971 | |
| 4972 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4973 | // by cancelling authentication. |
| 4974 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4975 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4976 | |
| 4977 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4978 | BlockingNetworkDelegate network_delegate( |
| 4979 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4980 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4981 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4982 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4983 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4984 | TestURLRequestContext context(true); |
| 4985 | context.set_network_delegate(&network_delegate); |
| 4986 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4987 | |
| 4988 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4989 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4990 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4991 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4992 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4993 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4994 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4995 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4996 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4997 | EXPECT_FALSE(d.auth_required_called()); |
| 4998 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4999 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5000 | } |
| 5001 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5002 | } |
| 5003 | |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5004 | // Tests that NetworkDelegate header overrides from the 401 response do not |
| 5005 | // affect the 200 response. This is a regression test for |
| 5006 | // https://crbug.com/801237. |
| 5007 | TEST_F(URLRequestTestHTTP, NetworkDelegateOverrideHeadersWithAuth) { |
| 5008 | ASSERT_TRUE(http_test_server()->Start()); |
| 5009 | |
| 5010 | TestDelegate d; |
| 5011 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 5012 | default_network_delegate_.set_add_header_to_first_response(true); |
| 5013 | |
| 5014 | { |
| 5015 | GURL url(http_test_server()->GetURL("/auth-basic")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5016 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5017 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 5018 | r->Start(); |
| 5019 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5020 | d.RunUntilComplete(); |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5021 | |
| 5022 | EXPECT_EQ(OK, d.request_status()); |
| 5023 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5024 | EXPECT_TRUE(d.auth_required_called()); |
| 5025 | EXPECT_FALSE(r->response_headers()->HasHeader("X-Network-Delegate")); |
| 5026 | } |
| 5027 | |
| 5028 | { |
| 5029 | GURL url(http_test_server()->GetURL("/defaultresponse")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5030 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5031 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 5032 | r->Start(); |
| 5033 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5034 | d.RunUntilComplete(); |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5035 | |
| 5036 | // Check that set_add_header_to_first_response normally adds a header. |
| 5037 | EXPECT_EQ(OK, d.request_status()); |
| 5038 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5039 | EXPECT_TRUE(r->response_headers()->HasHeader("X-Network-Delegate")); |
| 5040 | } |
| 5041 | } |
| 5042 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5043 | // Tests that we can handle when a network request was canceled while we were |
| 5044 | // waiting for the network delegate. |
| 5045 | // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 5046 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5047 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5048 | |
| 5049 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5050 | BlockingNetworkDelegate network_delegate( |
| 5051 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5052 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5053 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5054 | TestURLRequestContext context(true); |
| 5055 | context.set_network_delegate(&network_delegate); |
| 5056 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5057 | |
| 5058 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5059 | std::unique_ptr<URLRequest> r( |
| 5060 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 5061 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5062 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5063 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5064 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5065 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 5066 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5067 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5068 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5069 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5070 | // Ensure that network delegate is notified. |
| 5071 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5072 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5073 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5074 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5075 | } |
| 5076 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5077 | } |
| 5078 | |
| 5079 | // Tests that we can handle when a network request was canceled while we were |
| 5080 | // waiting for the network delegate. |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5081 | // Part 2: Request is cancelled while waiting for OnBeforeStartTransaction |
| 5082 | // callback. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5083 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5084 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5085 | |
| 5086 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5087 | BlockingNetworkDelegate network_delegate( |
| 5088 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5089 | network_delegate.set_block_on( |
| 5090 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5091 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5092 | TestURLRequestContext context(true); |
| 5093 | context.set_network_delegate(&network_delegate); |
| 5094 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5095 | |
| 5096 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5097 | std::unique_ptr<URLRequest> r( |
| 5098 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 5099 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5100 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5101 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5102 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5103 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 5104 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5105 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5106 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5107 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5108 | // Ensure that network delegate is notified. |
| 5109 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5110 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5111 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5112 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5113 | } |
| 5114 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5115 | } |
| 5116 | |
| 5117 | // Tests that we can handle when a network request was canceled while we were |
| 5118 | // waiting for the network delegate. |
| 5119 | // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 5120 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5121 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5122 | |
| 5123 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5124 | BlockingNetworkDelegate network_delegate( |
| 5125 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5126 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5127 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5128 | TestURLRequestContext context(true); |
| 5129 | context.set_network_delegate(&network_delegate); |
| 5130 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5131 | |
| 5132 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5133 | std::unique_ptr<URLRequest> r( |
| 5134 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 5135 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5136 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5137 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5138 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5139 | EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 5140 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5141 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5142 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5143 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5144 | // Ensure that network delegate is notified. |
| 5145 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5146 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5147 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5148 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5149 | } |
| 5150 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5151 | } |
| 5152 | |
| 5153 | // Tests that we can handle when a network request was canceled while we were |
| 5154 | // waiting for the network delegate. |
| 5155 | // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
[email protected] | bfe8b30 | 2013-02-15 12:16:02 | [diff] [blame] | 5156 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5157 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5158 | |
| 5159 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5160 | BlockingNetworkDelegate network_delegate( |
| 5161 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5162 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5163 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5164 | TestURLRequestContext context(true); |
| 5165 | context.set_network_delegate(&network_delegate); |
| 5166 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5167 | |
| 5168 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5169 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5170 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 5171 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5172 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5173 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5174 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5175 | EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED, |
| 5176 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5177 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5178 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5179 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5180 | // Ensure that network delegate is notified. |
| 5181 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5182 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5183 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5184 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5185 | } |
| 5186 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5187 | } |
| 5188 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5189 | namespace { |
| 5190 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5191 | std::unique_ptr<test_server::HttpResponse> HandleServerAuthConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5192 | const test_server::HttpRequest& request) { |
| 5193 | if (request.headers.find("Host") == request.headers.end() || |
| 5194 | request.headers.at("Host") != "www.server-auth.com" || |
| 5195 | request.method != test_server::METHOD_CONNECT) { |
| 5196 | return nullptr; |
| 5197 | } |
| 5198 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5199 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5200 | new test_server::BasicHttpResponse); |
| 5201 | http_response->set_code(HTTP_UNAUTHORIZED); |
| 5202 | http_response->AddCustomHeader("WWW-Authenticate", |
| 5203 | "Basic realm=\"WallyWorld\""); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 5204 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5205 | } |
| 5206 | |
| 5207 | } // namespace |
| 5208 | |
| 5209 | // 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] | 5210 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5211 | // The EmbeddedTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5212 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5213 | http_test_server()->RegisterRequestHandler( |
Matt Menke | 2b2a6047 | 2018-02-07 02:12:12 | [diff] [blame] | 5214 | base::BindRepeating(&HandleServerAuthConnect)); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5215 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5216 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5217 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 5218 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5219 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 5220 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5221 | TestDelegate d; |
| 5222 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5223 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5224 | GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d, |
| 5225 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5226 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5227 | r->Start(); |
| 5228 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5229 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5230 | d.RunUntilComplete(); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5231 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 5232 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 5233 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5234 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5235 | } |
| 5236 | } |
| 5237 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5238 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5239 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5240 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5241 | TestDelegate d; |
| 5242 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5243 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5244 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5245 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5246 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5247 | r->Start(); |
| 5248 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5249 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5250 | d.RunUntilComplete(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5251 | |
| 5252 | EXPECT_EQ(1, d.response_started_count()); |
| 5253 | EXPECT_FALSE(d.received_data_before_response()); |
| 5254 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5255 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5256 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5257 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5258 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 5259 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5260 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5261 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5262 | } |
| 5263 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5264 | // This test has the server send a large number of cookies to the client. |
| 5265 | // To ensure that no number of cookies causes a crash, a galloping binary |
| 5266 | // search is used to estimate that maximum number of cookies that are accepted |
| 5267 | // by the browser. Beyond the maximum number, the request will fail with |
| 5268 | // ERR_RESPONSE_HEADERS_TOO_BIG. |
Sergey Ulanov | 0fb900c | 2017-09-20 00:27:31 | [diff] [blame] | 5269 | #if defined(OS_WIN) || defined(OS_FUCHSIA) |
[email protected] | 69dd6fe | 2013-02-23 23:15:30 | [diff] [blame] | 5270 | // http://crbug.com/177916 |
| 5271 | #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies |
| 5272 | #else |
| 5273 | #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies |
| 5274 | #endif // defined(OS_WIN) |
| 5275 | TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5276 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5277 | |
| 5278 | int lower_bound = 0; |
| 5279 | int upper_bound = 1; |
| 5280 | |
| 5281 | // Double the number of cookies until the response header limits are |
| 5282 | // exceeded. |
| 5283 | while (DoManyCookiesRequest(upper_bound)) { |
| 5284 | lower_bound = upper_bound; |
| 5285 | upper_bound *= 2; |
| 5286 | ASSERT_LT(upper_bound, 1000000); |
| 5287 | } |
| 5288 | |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 5289 | int tolerance = static_cast<int>(upper_bound * 0.005); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5290 | if (tolerance < 2) |
| 5291 | tolerance = 2; |
| 5292 | |
| 5293 | // Perform a binary search to find the highest possible number of cookies, |
| 5294 | // within the desired tolerance. |
| 5295 | while (upper_bound - lower_bound >= tolerance) { |
| 5296 | int num_cookies = (lower_bound + upper_bound) / 2; |
| 5297 | |
| 5298 | if (DoManyCookiesRequest(num_cookies)) |
| 5299 | lower_bound = num_cookies; |
| 5300 | else |
| 5301 | upper_bound = num_cookies; |
| 5302 | } |
| 5303 | // Success: the test did not crash. |
| 5304 | } |
| 5305 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5306 | TEST_F(URLRequestTestHTTP, GetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5307 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5308 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5309 | TestDelegate d; |
| 5310 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5311 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5312 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5313 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5314 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5315 | r->Start(); |
| 5316 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5317 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5318 | d.RunUntilComplete(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5319 | |
| 5320 | EXPECT_EQ(1, d.response_started_count()); |
| 5321 | EXPECT_FALSE(d.received_data_before_response()); |
| 5322 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5323 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5324 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5325 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5326 | r->GetResponseRemoteEndpoint().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5327 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 5328 | } |
| 5329 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5330 | TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5331 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5332 | |
| 5333 | TestDelegate d; |
| 5334 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5335 | GURL test_url(http_test_server()->GetURL("/defaultresponse")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5336 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5337 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5338 | |
| 5339 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5340 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5341 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5342 | r->Start(); |
| 5343 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5344 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5345 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5346 | |
| 5347 | EXPECT_EQ(1, d.response_started_count()); |
| 5348 | EXPECT_FALSE(d.received_data_before_response()); |
| 5349 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5350 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5351 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5352 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5353 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5354 | |
| 5355 | EXPECT_TRUE(d.have_full_request_headers()); |
| 5356 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 5357 | } |
| 5358 | } |
| 5359 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5360 | TEST_F(URLRequestTestHTTP, GetTestLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5361 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5362 | |
| 5363 | TestDelegate d; |
| 5364 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5365 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5366 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5367 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5368 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5369 | r->Start(); |
| 5370 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5371 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5372 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5373 | |
| 5374 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5375 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5376 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5377 | |
| 5378 | EXPECT_EQ(1, d.response_started_count()); |
| 5379 | EXPECT_FALSE(d.received_data_before_response()); |
| 5380 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5381 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5382 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5383 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5384 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5385 | } |
| 5386 | } |
| 5387 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5388 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 5389 | #if !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5390 | TEST_F(URLRequestTestHTTP, GetZippedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5391 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5392 | base::FilePath(kTestFilePath)); |
| 5393 | |
| 5394 | ASSERT_TRUE(test_server.Start()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5395 | |
| 5396 | // Parameter that specifies the Content-Length field in the response: |
| 5397 | // C - Compressed length. |
| 5398 | // U - Uncompressed length. |
| 5399 | // L - Large length (larger than both C & U). |
| 5400 | // M - Medium length (between C & U). |
| 5401 | // S - Small length (smaller than both C & U). |
| 5402 | const char test_parameters[] = "CULMS"; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 5403 | const int num_tests = base::size(test_parameters) - 1; // Skip NULL. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5404 | // C & U should be OK. |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 5405 | // L & M are larger than the data sent, and show an error. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5406 | // S has too little data, but we seem to accept it. |
| 5407 | const bool test_expect_success[num_tests] = |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 5408 | { true, true, false, false, true }; |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5409 | |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5410 | base::FilePath file_path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 5411 | base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5412 | file_path = file_path.Append(kTestFilePath); |
| 5413 | file_path = file_path.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); |
| 5414 | std::string expected_content; |
| 5415 | ASSERT_TRUE(base::ReadFileToString(file_path, &expected_content)); |
| 5416 | |
| 5417 | for (int i = 0; i < num_tests; i++) { |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5418 | TestDelegate d; |
| 5419 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5420 | std::string test_file = base::StringPrintf( |
| 5421 | "compressedfiles/BullRunSpeech.txt?%c", test_parameters[i]); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5422 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5423 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5424 | TestURLRequestContext context(true); |
| 5425 | context.set_network_delegate(&network_delegate); |
| 5426 | context.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 5427 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5428 | std::unique_ptr<URLRequest> r( |
| 5429 | context.CreateRequest(test_server.GetURL(test_file), DEFAULT_PRIORITY, |
| 5430 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5431 | r->Start(); |
| 5432 | EXPECT_TRUE(r->is_pending()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5433 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5434 | d.RunUntilComplete(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5435 | |
| 5436 | EXPECT_EQ(1, d.response_started_count()); |
| 5437 | EXPECT_FALSE(d.received_data_before_response()); |
| 5438 | VLOG(1) << " Received " << d.bytes_received() << " bytes" |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5439 | << " error = " << d.request_status(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5440 | if (test_expect_success[i]) { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5441 | EXPECT_EQ(OK, d.request_status()) << " Parameter = \"" << test_file |
| 5442 | << "\""; |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5443 | if (test_parameters[i] == 'S') { |
| 5444 | // When content length is smaller than both compressed length and |
| 5445 | // uncompressed length, HttpStreamParser might not read the full |
| 5446 | // response body. |
| 5447 | continue; |
| 5448 | } |
| 5449 | EXPECT_EQ(expected_content, d.data_received()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5450 | } else { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5451 | EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, d.request_status()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5452 | << " Parameter = \"" << test_file << "\""; |
| 5453 | } |
| 5454 | } |
| 5455 | } |
| 5456 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5457 | #endif // !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5458 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5459 | TEST_F(URLRequestTestHTTP, RedirectLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5460 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5461 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5462 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5463 | GURL original_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5464 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5465 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5466 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5467 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5468 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5469 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5470 | |
| 5471 | EXPECT_EQ(1, d.response_started_count()); |
| 5472 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5473 | EXPECT_EQ(destination_url, req->url()); |
| 5474 | EXPECT_EQ(original_url, req->original_url()); |
| 5475 | ASSERT_EQ(2U, req->url_chain().size()); |
| 5476 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5477 | EXPECT_EQ(destination_url, req->url_chain()[1]); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5478 | |
| 5479 | LoadTimingInfo load_timing_info_before_redirect; |
| 5480 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect( |
| 5481 | &load_timing_info_before_redirect)); |
| 5482 | TestLoadTimingNotReused(load_timing_info_before_redirect, |
| 5483 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 5484 | |
| 5485 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5486 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5487 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5488 | |
| 5489 | // Check that a new socket was used on redirect, since the server does not |
| 5490 | // supposed keep-alive sockets, and that the times before the redirect are |
| 5491 | // before the ones recorded for the second request. |
| 5492 | EXPECT_NE(load_timing_info_before_redirect.socket_log_id, |
| 5493 | load_timing_info.socket_log_id); |
| 5494 | EXPECT_LE(load_timing_info_before_redirect.receive_headers_end, |
| 5495 | load_timing_info.connect_timing.connect_start); |
| 5496 | } |
| 5497 | |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5498 | TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5499 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5500 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5501 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5502 | GURL middle_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5503 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
| 5504 | GURL original_url = http_test_server()->GetURL("/server-redirect?" + |
| 5505 | middle_redirect_url.spec()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5506 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5507 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5508 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5509 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5510 | d.RunUntilComplete(); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5511 | |
| 5512 | EXPECT_EQ(1, d.response_started_count()); |
| 5513 | EXPECT_EQ(2, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5514 | EXPECT_EQ(destination_url, req->url()); |
| 5515 | EXPECT_EQ(original_url, req->original_url()); |
| 5516 | ASSERT_EQ(3U, req->url_chain().size()); |
| 5517 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5518 | EXPECT_EQ(middle_redirect_url, req->url_chain()[1]); |
| 5519 | EXPECT_EQ(destination_url, req->url_chain()[2]); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5520 | } |
| 5521 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5522 | // First and second pieces of information logged by delegates to URLRequests. |
| 5523 | const char kFirstDelegateInfo[] = "Wonderful delegate"; |
| 5524 | const char kSecondDelegateInfo[] = "Exciting delegate"; |
| 5525 | |
| 5526 | // Logs delegate information to a URLRequest. The first string is logged |
| 5527 | // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is |
| 5528 | // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then |
| 5529 | // another asynchronous call is used to clear the delegate information |
| 5530 | // before calling a callback. The object then deletes itself. |
| 5531 | class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> { |
| 5532 | public: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5533 | using Callback = base::OnceCallback<void()>; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5534 | |
| 5535 | // Each time delegate information is added to the URLRequest, the resulting |
| 5536 | // load state is checked. The expected load state after each request is |
| 5537 | // passed in as an argument. |
| 5538 | static void Run(URLRequest* url_request, |
| 5539 | LoadState expected_first_load_state, |
| 5540 | LoadState expected_second_load_state, |
| 5541 | LoadState expected_third_load_state, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5542 | Callback callback) { |
tzik | 38f920a | 2018-08-01 04:41:20 | [diff] [blame] | 5543 | // base::MakeRefCounted<AsyncDelegateLogger> is unavailable here, since the |
| 5544 | // constructor of AsyncDelegateLogger is private. |
| 5545 | auto logger = base::WrapRefCounted(new AsyncDelegateLogger( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5546 | url_request, expected_first_load_state, expected_second_load_state, |
tzik | 38f920a | 2018-08-01 04:41:20 | [diff] [blame] | 5547 | expected_third_load_state, std::move(callback))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5548 | logger->Start(); |
| 5549 | } |
| 5550 | |
| 5551 | // Checks that the log entries, starting with log_position, contain the |
| 5552 | // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have |
| 5553 | // recorded. Returns the index of entry after the expected number of |
| 5554 | // events this logged, or entries.size() if there aren't enough entries. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5555 | static size_t CheckDelegateInfo(const TestNetLogEntry::List& entries, |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 5556 | size_t log_position) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5557 | // There should be 4 DELEGATE_INFO events: Two begins and two ends. |
| 5558 | if (log_position + 3 >= entries.size()) { |
| 5559 | ADD_FAILURE() << "Not enough log entries"; |
| 5560 | return entries.size(); |
| 5561 | } |
| 5562 | std::string delegate_info; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5563 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5564 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
rdsmith | 37a0dde | 2017-01-04 00:12:07 | [diff] [blame] | 5565 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_blocked_by", |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5566 | &delegate_info)); |
| 5567 | EXPECT_EQ(kFirstDelegateInfo, delegate_info); |
| 5568 | |
| 5569 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5570 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5571 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5572 | |
| 5573 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5574 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5575 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
rdsmith | 37a0dde | 2017-01-04 00:12:07 | [diff] [blame] | 5576 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_blocked_by", |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5577 | &delegate_info)); |
| 5578 | EXPECT_EQ(kSecondDelegateInfo, delegate_info); |
| 5579 | |
| 5580 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5581 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5582 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5583 | |
| 5584 | return log_position + 1; |
| 5585 | } |
| 5586 | |
| 5587 | private: |
| 5588 | friend class base::RefCounted<AsyncDelegateLogger>; |
| 5589 | |
| 5590 | AsyncDelegateLogger(URLRequest* url_request, |
| 5591 | LoadState expected_first_load_state, |
| 5592 | LoadState expected_second_load_state, |
| 5593 | LoadState expected_third_load_state, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5594 | Callback callback) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5595 | : url_request_(url_request), |
| 5596 | expected_first_load_state_(expected_first_load_state), |
| 5597 | expected_second_load_state_(expected_second_load_state), |
| 5598 | expected_third_load_state_(expected_third_load_state), |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5599 | callback_(std::move(callback)) {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5600 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5601 | ~AsyncDelegateLogger() = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5602 | |
| 5603 | void Start() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5604 | url_request_->LogBlockedBy(kFirstDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5605 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5606 | EXPECT_EQ(expected_first_load_state_, load_state.state); |
| 5607 | EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5608 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5609 | FROM_HERE, |
| 5610 | base::BindOnce(&AsyncDelegateLogger::LogSecondDelegate, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5611 | } |
| 5612 | |
| 5613 | void LogSecondDelegate() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5614 | url_request_->LogAndReportBlockedBy(kSecondDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5615 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5616 | EXPECT_EQ(expected_second_load_state_, load_state.state); |
| 5617 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) { |
| 5618 | EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5619 | } else { |
| 5620 | EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5621 | } |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5622 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5623 | FROM_HERE, base::BindOnce(&AsyncDelegateLogger::LogComplete, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5624 | } |
| 5625 | |
| 5626 | void LogComplete() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5627 | url_request_->LogUnblocked(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5628 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5629 | EXPECT_EQ(expected_third_load_state_, load_state.state); |
| 5630 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5631 | EXPECT_EQ(base::string16(), load_state.param); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5632 | std::move(callback_).Run(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5633 | } |
| 5634 | |
| 5635 | URLRequest* url_request_; |
| 5636 | const int expected_first_load_state_; |
| 5637 | const int expected_second_load_state_; |
| 5638 | const int expected_third_load_state_; |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5639 | Callback callback_; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5640 | |
| 5641 | DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger); |
| 5642 | }; |
| 5643 | |
| 5644 | // NetworkDelegate that logs delegate information before a request is started, |
| 5645 | // before headers are sent, when headers are read, and when auth information |
| 5646 | // is requested. Uses AsyncDelegateLogger. |
| 5647 | class AsyncLoggingNetworkDelegate : public TestNetworkDelegate { |
| 5648 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5649 | AsyncLoggingNetworkDelegate() = default; |
| 5650 | ~AsyncLoggingNetworkDelegate() override = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5651 | |
| 5652 | // NetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5653 | int OnBeforeURLRequest(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5654 | CompletionOnceCallback callback, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5655 | GURL* new_url) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5656 | // TestNetworkDelegate always completes synchronously. |
| 5657 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeURLRequest( |
| 5658 | request, base::NullCallback(), new_url)); |
| 5659 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5660 | } |
| 5661 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5662 | int OnBeforeStartTransaction(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5663 | CompletionOnceCallback callback, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5664 | HttpRequestHeaders* headers) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5665 | // TestNetworkDelegate always completes synchronously. |
| 5666 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeStartTransaction( |
| 5667 | request, base::NullCallback(), headers)); |
| 5668 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5669 | } |
| 5670 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5671 | int OnHeadersReceived( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5672 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5673 | CompletionOnceCallback callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5674 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5675 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5676 | GURL* allowed_unsafe_redirect_url) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5677 | // TestNetworkDelegate always completes synchronously. |
| 5678 | CHECK_NE(ERR_IO_PENDING, |
| 5679 | TestNetworkDelegate::OnHeadersReceived( |
| 5680 | request, base::NullCallback(), original_response_headers, |
| 5681 | override_response_headers, allowed_unsafe_redirect_url)); |
| 5682 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5683 | } |
| 5684 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5685 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5686 | URLRequest* request, |
| 5687 | const AuthChallengeInfo& auth_info, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5688 | AuthCallback callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5689 | AuthCredentials* credentials) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5690 | AsyncDelegateLogger::Run( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5691 | request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5692 | LOAD_STATE_WAITING_FOR_DELEGATE, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5693 | base::BindOnce(&AsyncLoggingNetworkDelegate::SetAuthAndResume, |
| 5694 | std::move(callback), credentials)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5695 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 5696 | } |
| 5697 | |
| 5698 | private: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5699 | static int RunCallbackAsynchronously(URLRequest* request, |
| 5700 | CompletionOnceCallback callback) { |
| 5701 | AsyncDelegateLogger::Run(request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5702 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5703 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5704 | base::BindOnce(std::move(callback), OK)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5705 | return ERR_IO_PENDING; |
| 5706 | } |
| 5707 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5708 | static void SetAuthAndResume(AuthCallback callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5709 | AuthCredentials* credentials) { |
| 5710 | *credentials = AuthCredentials(kUser, kSecret); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5711 | std::move(callback).Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5712 | } |
| 5713 | |
| 5714 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate); |
| 5715 | }; |
| 5716 | |
| 5717 | // URLRequest::Delegate that logs delegate information when the headers |
| 5718 | // are received, when each read completes, and during redirects. Uses |
| 5719 | // AsyncDelegateLogger. Can optionally cancel a request in any phase. |
| 5720 | // |
| 5721 | // Inherits from TestDelegate to reuse the TestDelegate code to handle |
| 5722 | // advancing to the next step in most cases, as well as cancellation. |
| 5723 | class AsyncLoggingUrlRequestDelegate : public TestDelegate { |
| 5724 | public: |
| 5725 | enum CancelStage { |
| 5726 | NO_CANCEL = 0, |
| 5727 | CANCEL_ON_RECEIVED_REDIRECT, |
| 5728 | CANCEL_ON_RESPONSE_STARTED, |
| 5729 | CANCEL_ON_READ_COMPLETED |
| 5730 | }; |
| 5731 | |
| 5732 | explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage) |
| 5733 | : cancel_stage_(cancel_stage) { |
| 5734 | if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT) |
| 5735 | set_cancel_in_received_redirect(true); |
| 5736 | else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED) |
| 5737 | set_cancel_in_response_started(true); |
| 5738 | else if (cancel_stage == CANCEL_ON_READ_COMPLETED) |
| 5739 | set_cancel_in_received_data(true); |
| 5740 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5741 | ~AsyncLoggingUrlRequestDelegate() override = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5742 | |
| 5743 | // URLRequest::Delegate implementation: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5744 | void OnReceivedRedirect(URLRequest* request, |
| 5745 | const RedirectInfo& redirect_info, |
| 5746 | bool* defer_redirect) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5747 | *defer_redirect = true; |
| 5748 | AsyncDelegateLogger::Run( |
| 5749 | request, |
| 5750 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5751 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5752 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5753 | base::Bind( |
| 5754 | &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5755 | base::Unretained(this), request, redirect_info)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5756 | } |
| 5757 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5758 | void OnResponseStarted(URLRequest* request, int net_error) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5759 | AsyncDelegateLogger::Run( |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5760 | request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5761 | LOAD_STATE_WAITING_FOR_DELEGATE, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5762 | base::Bind( |
| 5763 | &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete, |
| 5764 | base::Unretained(this), request, net_error)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5765 | } |
| 5766 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5767 | void OnReadCompleted(URLRequest* request, int bytes_read) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5768 | AsyncDelegateLogger::Run( |
| 5769 | request, |
| 5770 | LOAD_STATE_IDLE, |
| 5771 | LOAD_STATE_IDLE, |
| 5772 | LOAD_STATE_IDLE, |
| 5773 | base::Bind( |
| 5774 | &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete, |
| 5775 | base::Unretained(this), request, bytes_read)); |
| 5776 | } |
| 5777 | |
| 5778 | private: |
| 5779 | void OnReceivedRedirectLoggingComplete(URLRequest* request, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5780 | const RedirectInfo& redirect_info) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5781 | bool defer_redirect = false; |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5782 | TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5783 | // FollowDeferredRedirect should not be called after cancellation. |
| 5784 | if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT) |
| 5785 | return; |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 5786 | if (!defer_redirect) { |
| 5787 | request->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 5788 | base::nullopt /* modified_headers */); |
| 5789 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5790 | } |
| 5791 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5792 | void OnResponseStartedLoggingComplete(URLRequest* request, int net_error) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5793 | // The parent class continues the request. |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5794 | TestDelegate::OnResponseStarted(request, net_error); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5795 | } |
| 5796 | |
| 5797 | void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) { |
| 5798 | // The parent class continues the request. |
| 5799 | TestDelegate::OnReadCompleted(request, bytes_read); |
| 5800 | } |
| 5801 | |
| 5802 | const CancelStage cancel_stage_; |
| 5803 | |
| 5804 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate); |
| 5805 | }; |
| 5806 | |
| 5807 | // Tests handling of delegate info before a request starts. |
| 5808 | TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5809 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5810 | |
| 5811 | TestDelegate request_delegate; |
| 5812 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 5813 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5814 | context.set_net_log(&net_log_); |
| 5815 | context.Init(); |
| 5816 | |
| 5817 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5818 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5819 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, |
| 5820 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5821 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5822 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5823 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5824 | |
| 5825 | AsyncDelegateLogger::Run( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5826 | r.get(), |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5827 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5828 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5829 | LOAD_STATE_IDLE, |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5830 | base::Bind(&URLRequest::Start, base::Unretained(r.get()))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5831 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5832 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5833 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5834 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5835 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5836 | } |
| 5837 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5838 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5839 | net_log_.GetEntries(&entries); |
| 5840 | size_t log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5841 | entries, 0, NetLogEventType::DELEGATE_INFO, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5842 | |
| 5843 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position); |
| 5844 | |
| 5845 | // Nothing else should add any delegate info to the request. |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5846 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5847 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5848 | } |
| 5849 | |
| 5850 | // Tests handling of delegate info from a network delegate. |
| 5851 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5852 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5853 | |
| 5854 | TestDelegate request_delegate; |
| 5855 | AsyncLoggingNetworkDelegate network_delegate; |
| 5856 | TestURLRequestContext context(true); |
| 5857 | context.set_network_delegate(&network_delegate); |
| 5858 | context.set_net_log(&net_log_); |
| 5859 | context.Init(); |
| 5860 | |
| 5861 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5862 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5863 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, |
| 5864 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5865 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5866 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5867 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5868 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5869 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5870 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5871 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5872 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5873 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5874 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5875 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5876 | } |
| 5877 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5878 | |
| 5879 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5880 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5881 | net_log_.GetEntries(&entries); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5882 | static const NetLogEventType kExpectedEvents[] = { |
| 5883 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 5884 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 5885 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 5886 | }; |
| 5887 | for (NetLogEventType event : kExpectedEvents) { |
| 5888 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5889 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5890 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5891 | |
| 5892 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5893 | log_position + 1); |
| 5894 | |
| 5895 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5896 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5897 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5898 | } |
| 5899 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5900 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5901 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5902 | } |
| 5903 | |
| 5904 | // Tests handling of delegate info from a network delegate in the case of an |
| 5905 | // HTTP redirect. |
| 5906 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5907 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5908 | |
| 5909 | TestDelegate request_delegate; |
| 5910 | AsyncLoggingNetworkDelegate network_delegate; |
| 5911 | TestURLRequestContext context(true); |
| 5912 | context.set_network_delegate(&network_delegate); |
| 5913 | context.set_net_log(&net_log_); |
| 5914 | context.Init(); |
| 5915 | |
| 5916 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5917 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5918 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5919 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5920 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5921 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5922 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5923 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5924 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5925 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5926 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5927 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5928 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5929 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 5930 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5931 | } |
| 5932 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5933 | |
| 5934 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5935 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5936 | net_log_.GetEntries(&entries); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5937 | static const NetLogEventType kExpectedEvents[] = { |
| 5938 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 5939 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 5940 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 5941 | }; |
| 5942 | for (NetLogEventType event : kExpectedEvents) { |
| 5943 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5944 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5945 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5946 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5947 | log_position = |
| 5948 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5949 | |
| 5950 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5951 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5952 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5953 | } |
| 5954 | |
| 5955 | // The URLRequest::Delegate then gets informed about the redirect. |
| 5956 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5957 | entries, log_position + 1, |
| 5958 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5959 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5960 | |
| 5961 | // The NetworkDelegate logged information in the same three events as before. |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5962 | for (NetLogEventType event : kExpectedEvents) { |
| 5963 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5964 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5965 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5966 | |
| 5967 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5968 | log_position + 1); |
| 5969 | |
| 5970 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5971 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5972 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5973 | } |
| 5974 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5975 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5976 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5977 | } |
| 5978 | |
| 5979 | // Tests handling of delegate info from a network delegate in the case of HTTP |
| 5980 | // AUTH. |
| 5981 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5982 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5983 | |
| 5984 | TestDelegate request_delegate; |
| 5985 | AsyncLoggingNetworkDelegate network_delegate; |
| 5986 | TestURLRequestContext context(true); |
| 5987 | context.set_network_delegate(&network_delegate); |
| 5988 | context.set_net_log(&net_log_); |
| 5989 | context.Init(); |
| 5990 | |
| 5991 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5992 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5993 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, |
| 5994 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5995 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5996 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5997 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5998 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5999 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6000 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6001 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6002 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6003 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6004 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 6005 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 6006 | } |
| 6007 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 6008 | |
| 6009 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6010 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6011 | net_log_.GetEntries(&entries); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6012 | static const NetLogEventType kExpectedEvents[] = { |
| 6013 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 6014 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6015 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6016 | NetLogEventType::NETWORK_DELEGATE_AUTH_REQUIRED, |
| 6017 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6018 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6019 | }; |
| 6020 | for (NetLogEventType event : kExpectedEvents) { |
| 6021 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6022 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6023 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6024 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6025 | log_position = |
| 6026 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6027 | |
| 6028 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6029 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6030 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6031 | } |
| 6032 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6033 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6034 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6035 | } |
| 6036 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6037 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 6038 | #if !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6039 | // Tests handling of delegate info from a URLRequest::Delegate. |
| 6040 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6041 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6042 | base::FilePath(kTestFilePath)); |
| 6043 | |
| 6044 | ASSERT_TRUE(test_server.Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6045 | |
| 6046 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 6047 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 6048 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 6049 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6050 | context.set_net_log(&net_log_); |
| 6051 | context.Init(); |
| 6052 | |
| 6053 | { |
| 6054 | // A chunked response with delays between chunks is used to make sure that |
| 6055 | // attempts by the URLRequest delegate to log information while reading the |
| 6056 | // body are ignored. Since they are ignored, this test is robust against |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6057 | // the possibility of multiple reads being combined in the unlikely event |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6058 | // that it occurs. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6059 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6060 | test_server.GetURL("/chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6061 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6062 | LoadStateWithParam load_state = r->GetLoadState(); |
| 6063 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6064 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6065 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6066 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6067 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6068 | } |
| 6069 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6070 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6071 | net_log_.GetEntries(&entries); |
| 6072 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6073 | size_t log_position = 0; |
| 6074 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6075 | // The delegate info should only have been logged on header complete. Other |
| 6076 | // times it should silently be ignored. |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6077 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 6078 | entries, 0, NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST)); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6079 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6080 | entries, log_position + 1, |
| 6081 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6082 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6083 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6084 | log_position = |
| 6085 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6086 | |
| 6087 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6088 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 6089 | entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6090 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6091 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6092 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6093 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6094 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6095 | entries, log_position + 1, |
| 6096 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6097 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6098 | #endif // !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6099 | |
| 6100 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 6101 | // an HTTP redirect. |
| 6102 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6103 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6104 | |
| 6105 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 6106 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 6107 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 6108 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6109 | context.set_net_log(&net_log_); |
| 6110 | context.Init(); |
| 6111 | |
| 6112 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6113 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6114 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6115 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6116 | LoadStateWithParam load_state = r->GetLoadState(); |
| 6117 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6118 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6119 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6120 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6121 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6122 | } |
| 6123 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6124 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6125 | net_log_.GetEntries(&entries); |
| 6126 | |
| 6127 | // Delegate info should only have been logged in OnReceivedRedirect and |
| 6128 | // OnResponseStarted. |
| 6129 | size_t log_position = 0; |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6130 | static const NetLogEventType kExpectedEvents[] = { |
| 6131 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
| 6132 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 6133 | }; |
| 6134 | for (NetLogEventType event : kExpectedEvents) { |
| 6135 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
| 6136 | log_position = ExpectLogContainsSomewhereAfter(entries, log_position, event, |
| 6137 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6138 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6139 | log_position = |
| 6140 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6141 | |
| 6142 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6143 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6144 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6145 | } |
| 6146 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6147 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6148 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6149 | } |
| 6150 | |
| 6151 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 6152 | // an HTTP redirect, with cancellation at various points. |
| 6153 | TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6154 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6155 | |
| 6156 | const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = { |
| 6157 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT, |
| 6158 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED, |
| 6159 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED, |
| 6160 | }; |
| 6161 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6162 | for (auto cancel_stage : kCancelStages) { |
| 6163 | AsyncLoggingUrlRequestDelegate request_delegate(cancel_stage); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 6164 | TestNetLog net_log; |
krasin | 0bfeb6b | 2017-01-13 21:48:04 | [diff] [blame] | 6165 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 6166 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6167 | context.set_net_log(&net_log); |
| 6168 | context.Init(); |
| 6169 | |
| 6170 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6171 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6172 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6173 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6174 | LoadStateWithParam load_state = r->GetLoadState(); |
| 6175 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6176 | request_delegate.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6177 | EXPECT_EQ(ERR_ABORTED, request_delegate.request_status()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6178 | |
| 6179 | // Spin the message loop to run AsyncDelegateLogger task(s) posted after |
| 6180 | // the |request_delegate| completion task. |
| 6181 | base::RunLoop().RunUntilIdle(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6182 | } |
| 6183 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6184 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6185 | net_log.GetEntries(&entries); |
| 6186 | |
| 6187 | // Delegate info is always logged in both OnReceivedRedirect and |
| 6188 | // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the |
| 6189 | // OnResponseStarted delegate call is after cancellation, but logging is |
| 6190 | // still currently supported in that call. |
| 6191 | size_t log_position = 0; |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6192 | static const NetLogEventType kExpectedEvents[] = { |
| 6193 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
| 6194 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 6195 | }; |
| 6196 | for (NetLogEventType event : kExpectedEvents) { |
| 6197 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6198 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6199 | entries, log_position, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6200 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6201 | log_position = |
| 6202 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6203 | |
| 6204 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6205 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6206 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6207 | } |
| 6208 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6209 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6210 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6211 | } |
| 6212 | } |
| 6213 | |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6214 | namespace { |
| 6215 | |
| 6216 | const char kExtraHeader[] = "Allow-Snafu"; |
| 6217 | const char kExtraValue[] = "fubar"; |
| 6218 | |
| 6219 | class RedirectWithAdditionalHeadersDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 6220 | void OnReceivedRedirect(URLRequest* request, |
| 6221 | const RedirectInfo& redirect_info, |
| 6222 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6223 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6224 | request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false); |
| 6225 | } |
| 6226 | }; |
| 6227 | |
| 6228 | } // namespace |
| 6229 | |
| 6230 | TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6231 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6232 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6233 | GURL destination_url = |
| 6234 | http_test_server()->GetURL("/echoheader?" + std::string(kExtraHeader)); |
| 6235 | GURL original_url = |
| 6236 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6237 | RedirectWithAdditionalHeadersDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6238 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6239 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6240 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6241 | d.RunUntilComplete(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6242 | |
| 6243 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6244 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6245 | EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value)); |
| 6246 | EXPECT_EQ(kExtraValue, value); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6247 | EXPECT_FALSE(req->is_pending()); |
| 6248 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6249 | EXPECT_EQ(kExtraValue, d.data_received()); |
| 6250 | } |
| 6251 | |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6252 | namespace { |
| 6253 | |
| 6254 | const char kExtraHeaderToRemove[] = "To-Be-Removed"; |
| 6255 | |
| 6256 | class RedirectWithHeaderRemovalDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 6257 | void OnReceivedRedirect(URLRequest* request, |
| 6258 | const RedirectInfo& redirect_info, |
| 6259 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6260 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6261 | request->RemoveRequestHeaderByName(kExtraHeaderToRemove); |
| 6262 | } |
| 6263 | }; |
| 6264 | |
| 6265 | } // namespace |
| 6266 | |
| 6267 | TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6268 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6269 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6270 | GURL destination_url = http_test_server()->GetURL( |
| 6271 | "/echoheader?" + std::string(kExtraHeaderToRemove)); |
| 6272 | GURL original_url = |
| 6273 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6274 | RedirectWithHeaderRemovalDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6275 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6276 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6277 | req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false); |
| 6278 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6279 | d.RunUntilComplete(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6280 | |
| 6281 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6282 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6283 | EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6284 | EXPECT_FALSE(req->is_pending()); |
| 6285 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6286 | EXPECT_EQ("None", d.data_received()); |
| 6287 | } |
| 6288 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6289 | TEST_F(URLRequestTestHTTP, CancelAfterStart) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6290 | TestDelegate d; |
| 6291 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6292 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6293 | GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d, |
| 6294 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6295 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6296 | r->Start(); |
| 6297 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6298 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6299 | r->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6300 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6301 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6302 | |
| 6303 | // We expect to receive OnResponseStarted even though the request has been |
| 6304 | // cancelled. |
| 6305 | EXPECT_EQ(1, d.response_started_count()); |
| 6306 | EXPECT_EQ(0, d.bytes_received()); |
| 6307 | EXPECT_FALSE(d.received_data_before_response()); |
| 6308 | } |
| 6309 | } |
| 6310 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6311 | TEST_F(URLRequestTestHTTP, CancelInResponseStarted) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6312 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6313 | |
| 6314 | TestDelegate d; |
| 6315 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6316 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6317 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6318 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6319 | |
| 6320 | d.set_cancel_in_response_started(true); |
| 6321 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6322 | r->Start(); |
| 6323 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6324 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6325 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6326 | |
| 6327 | EXPECT_EQ(1, d.response_started_count()); |
| 6328 | EXPECT_EQ(0, d.bytes_received()); |
| 6329 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6330 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6331 | } |
| 6332 | } |
| 6333 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6334 | TEST_F(URLRequestTestHTTP, CancelOnDataReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6335 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6336 | |
| 6337 | TestDelegate d; |
| 6338 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6339 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6340 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 6341 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6342 | |
| 6343 | d.set_cancel_in_received_data(true); |
| 6344 | |
| 6345 | r->Start(); |
| 6346 | EXPECT_TRUE(r->is_pending()); |
| 6347 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6348 | d.RunUntilComplete(); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6349 | |
| 6350 | EXPECT_EQ(1, d.response_started_count()); |
| 6351 | EXPECT_NE(0, d.received_bytes_count()); |
| 6352 | EXPECT_FALSE(d.received_data_before_response()); |
| 6353 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
| 6354 | } |
| 6355 | } |
| 6356 | |
| 6357 | TEST_F(URLRequestTestHTTP, CancelDuringEofRead) { |
| 6358 | ASSERT_TRUE(http_test_server()->Start()); |
| 6359 | |
| 6360 | TestDelegate d; |
| 6361 | { |
| 6362 | // This returns an empty response (With headers). |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6363 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6364 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6365 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6366 | |
| 6367 | d.set_cancel_in_received_data(true); |
| 6368 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6369 | r->Start(); |
| 6370 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6371 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6372 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6373 | |
| 6374 | EXPECT_EQ(1, d.response_started_count()); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6375 | EXPECT_EQ(0, d.received_bytes_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6376 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6377 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6378 | } |
| 6379 | } |
| 6380 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6381 | TEST_F(URLRequestTestHTTP, CancelByDestroyingAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6382 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6383 | |
| 6384 | TestDelegate d; |
| 6385 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6386 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6387 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6388 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6389 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6390 | r->Start(); |
| 6391 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6392 | |
| 6393 | // The request will be implicitly canceled when it is destroyed. The |
| 6394 | // test delegate must not post a quit message when this happens because |
| 6395 | // this test doesn't actually have a message loop. The quit message would |
| 6396 | // get put on this thread's message queue and the next test would exit |
| 6397 | // early, causing problems. |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 6398 | d.set_on_complete(base::DoNothing()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6399 | } |
| 6400 | // expect things to just cleanup properly. |
| 6401 | |
kimwjdalsl | 2bb4ff0 | 2015-12-16 22:06:02 | [diff] [blame] | 6402 | // 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] | 6403 | // message loop |
| 6404 | EXPECT_FALSE(d.received_data_before_response()); |
| 6405 | EXPECT_EQ(0, d.bytes_received()); |
| 6406 | } |
| 6407 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6408 | TEST_F(URLRequestTestHTTP, CancelWhileReadingFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6409 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6410 | |
| 6411 | // populate cache |
| 6412 | { |
| 6413 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6414 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6415 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6416 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6417 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6418 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6419 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6420 | } |
| 6421 | |
| 6422 | // cancel read from cache (see bug 990242) |
| 6423 | { |
| 6424 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6425 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6426 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6427 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6428 | r->Start(); |
| 6429 | r->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6430 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6431 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6432 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6433 | EXPECT_EQ(1, d.response_started_count()); |
| 6434 | EXPECT_EQ(0, d.bytes_received()); |
| 6435 | EXPECT_FALSE(d.received_data_before_response()); |
| 6436 | } |
| 6437 | } |
| 6438 | |
| 6439 | TEST_F(URLRequestTestHTTP, PostTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6440 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6441 | HTTPUploadDataOperationTest("POST"); |
| 6442 | } |
| 6443 | |
| 6444 | TEST_F(URLRequestTestHTTP, PutTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6445 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6446 | HTTPUploadDataOperationTest("PUT"); |
| 6447 | } |
| 6448 | |
| 6449 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6450 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6451 | |
| 6452 | TestDelegate d; |
| 6453 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6454 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6455 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6456 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6457 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6458 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6459 | r->Start(); |
| 6460 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6461 | |
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 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6465 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6466 | |
| 6467 | EXPECT_FALSE(d.received_data_before_response()); |
| 6468 | EXPECT_TRUE(d.data_received().empty()); |
| 6469 | } |
| 6470 | } |
| 6471 | |
| 6472 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6473 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6474 | |
| 6475 | TestDelegate d; |
| 6476 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6477 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6478 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6479 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6480 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6481 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6482 | base::FilePath dir; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 6483 | base::PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 6484 | base::SetCurrentDirectory(dir); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6485 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6486 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6487 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6488 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 6489 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 6490 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6491 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6492 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6493 | base::ThreadTaskRunnerHandle::Get().get(), path, 0, |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6494 | std::numeric_limits<uint64_t>::max(), base::Time())); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6495 | r->set_upload(std::make_unique<ElementsUploadDataStream>( |
| 6496 | std::move(element_readers), 0)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6497 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6498 | r->Start(); |
| 6499 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6500 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6501 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6502 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6503 | int64_t size64 = 0; |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 6504 | ASSERT_EQ(true, base::GetFileSize(path, &size64)); |
| 6505 | ASSERT_LE(size64, std::numeric_limits<int>::max()); |
| 6506 | int size = static_cast<int>(size64); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6507 | std::unique_ptr<char[]> buf(new char[size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6508 | |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 6509 | ASSERT_EQ(size, base::ReadFile(path, buf.get(), size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6510 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6511 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6512 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6513 | |
| 6514 | EXPECT_FALSE(d.received_data_before_response()); |
| 6515 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6516 | EXPECT_EQ(size, d.bytes_received()); |
| 6517 | EXPECT_EQ(std::string(&buf[0], size), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6518 | } |
| 6519 | } |
| 6520 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6521 | TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6522 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6523 | |
| 6524 | TestDelegate d; |
| 6525 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6526 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6527 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6528 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6529 | r->set_method("POST"); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6530 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6531 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6532 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6533 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 6534 | base::ThreadTaskRunnerHandle::Get().get(), |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6535 | base::FilePath(FILE_PATH_LITERAL( |
| 6536 | "c:\\path\\to\\non\\existant\\file.randomness.12345")), |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6537 | 0, std::numeric_limits<uint64_t>::max(), base::Time())); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6538 | r->set_upload(std::make_unique<ElementsUploadDataStream>( |
| 6539 | std::move(element_readers), 0)); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6540 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6541 | r->Start(); |
| 6542 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6543 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6544 | d.RunUntilComplete(); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6545 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6546 | EXPECT_TRUE(d.request_failed()); |
| 6547 | EXPECT_FALSE(d.received_data_before_response()); |
| 6548 | EXPECT_EQ(0, d.bytes_received()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6549 | EXPECT_EQ(ERR_FILE_NOT_FOUND, d.request_status()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6550 | } |
| 6551 | } |
| 6552 | |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6553 | namespace { |
| 6554 | |
| 6555 | // Adds a standard set of data to an upload for chunked upload integration |
| 6556 | // tests. |
| 6557 | void AddDataToUpload(ChunkedUploadDataStream::Writer* writer) { |
| 6558 | writer->AppendData("a", 1, false); |
| 6559 | writer->AppendData("bcd", 3, false); |
| 6560 | writer->AppendData("this is a longer chunk than before.", 35, false); |
| 6561 | writer->AppendData("\r\n\r\n", 4, false); |
| 6562 | writer->AppendData("0", 1, false); |
| 6563 | writer->AppendData("2323", 4, true); |
| 6564 | } |
| 6565 | |
| 6566 | // Checks that the upload data added in AddChunksToUpload() was echoed back from |
| 6567 | // the server. |
| 6568 | void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { |
| 6569 | // This should match the chunks sent by AddChunksToUpload(). |
| 6570 | const std::string expected_data = |
| 6571 | "abcdthis is a longer chunk than before.\r\n\r\n02323"; |
| 6572 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6573 | ASSERT_EQ(1, d->response_started_count()) << "request failed. Error: " |
| 6574 | << d->request_status(); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6575 | |
| 6576 | EXPECT_FALSE(d->received_data_before_response()); |
| 6577 | |
| 6578 | EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); |
| 6579 | EXPECT_EQ(expected_data, d->data_received()); |
| 6580 | } |
| 6581 | |
| 6582 | } // namespace |
| 6583 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6584 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6585 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6586 | |
| 6587 | TestDelegate d; |
| 6588 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6589 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6590 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6591 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6592 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6593 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6594 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6595 | upload_data_stream->CreateWriter(); |
| 6596 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6597 | r->set_method("POST"); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6598 | AddDataToUpload(writer.get()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6599 | r->Start(); |
| 6600 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6601 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6602 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6603 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6604 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6605 | } |
| 6606 | } |
| 6607 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6608 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6609 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6610 | |
| 6611 | TestDelegate d; |
| 6612 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6613 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6614 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6615 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6616 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6617 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6618 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6619 | upload_data_stream->CreateWriter(); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6620 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6621 | r->set_method("POST"); |
| 6622 | r->Start(); |
| 6623 | EXPECT_TRUE(r->is_pending()); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6624 | AddDataToUpload(writer.get()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6625 | d.RunUntilComplete(); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6626 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6627 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6628 | } |
| 6629 | } |
| 6630 | |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6631 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6632 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6633 | |
| 6634 | TestDelegate d; |
| 6635 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6636 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6637 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6638 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6639 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6640 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6641 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6642 | upload_data_stream->CreateWriter(); |
| 6643 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6644 | r->set_method("POST"); |
| 6645 | r->Start(); |
| 6646 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6647 | |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6648 | // Pump messages until we start sending headers.. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6649 | base::RunLoop().RunUntilIdle(); |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6650 | |
| 6651 | // And now wait for completion. |
| 6652 | base::RunLoop run_loop; |
| 6653 | d.set_on_complete(run_loop.QuitClosure()); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6654 | AddDataToUpload(writer.get()); |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6655 | run_loop.Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6656 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6657 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6658 | } |
| 6659 | } |
| 6660 | |
| 6661 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6662 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6663 | |
| 6664 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6665 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6666 | http_test_server()->GetURL("/with-headers.html"), DEFAULT_PRIORITY, &d, |
| 6667 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6668 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6669 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6670 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6671 | const HttpResponseHeaders* headers = req->response_headers(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6672 | |
| 6673 | // Simple sanity check that response_info() accesses the same data. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6674 | EXPECT_EQ(headers, req->response_info().headers.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6675 | |
| 6676 | std::string header; |
| 6677 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 6678 | EXPECT_EQ("private", header); |
| 6679 | |
| 6680 | header.clear(); |
| 6681 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 6682 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 6683 | |
| 6684 | // The response has two "X-Multiple-Entries" headers. |
| 6685 | // This verfies our output has them concatenated together. |
| 6686 | header.clear(); |
| 6687 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 6688 | EXPECT_EQ("a, b", header); |
| 6689 | } |
| 6690 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6691 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 6692 | // security state. (see http://crbug.com/550977). |
| 6693 | #if !defined(OS_IOS) |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6694 | TEST_F(URLRequestTestHTTP, ProcessSTS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6695 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6696 | https_test_server.SetSSLConfig( |
| 6697 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6698 | https_test_server.ServeFilesFromSourceDirectory( |
| 6699 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6700 | ASSERT_TRUE(https_test_server.Start()); |
| 6701 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6702 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6703 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6704 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6705 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6706 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6707 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6708 | d.RunUntilComplete(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6709 | |
| 6710 | TransportSecurityState* security_state = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6711 | default_context().transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6712 | TransportSecurityState::STSState sts_state; |
| 6713 | TransportSecurityState::PKPState pkp_state; |
| 6714 | EXPECT_TRUE( |
| 6715 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6716 | EXPECT_FALSE( |
| 6717 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6718 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6719 | sts_state.upgrade_mode); |
| 6720 | EXPECT_TRUE(sts_state.include_subdomains); |
| 6721 | EXPECT_FALSE(pkp_state.include_subdomains); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6722 | #if defined(OS_ANDROID) |
| 6723 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6724 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6725 | EXPECT_FALSE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6726 | #endif |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6727 | } |
| 6728 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6729 | TEST_F(URLRequestTestHTTP, STSNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6730 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6731 | https_test_server.ServeFilesFromSourceDirectory( |
| 6732 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6733 | ASSERT_TRUE(https_test_server.Start()); |
| 6734 | // Make sure this test fails if the test server is changed to not |
| 6735 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6736 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 6737 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6738 | |
| 6739 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6740 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6741 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6742 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6743 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6744 | d.RunUntilComplete(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6745 | TransportSecurityState* security_state = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6746 | default_context().transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6747 | TransportSecurityState::STSState sts_state; |
| 6748 | EXPECT_FALSE( |
| 6749 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6750 | } |
| 6751 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6752 | namespace { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 6753 | const char kExpectCTStaticHostname[] = "expect-ct.preloaded.test"; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6754 | const char kPKPReportUri[] = "http://report-uri.preloaded.test/pkp"; |
| 6755 | const char kPKPHost[] = "with-report-uri-pkp.preloaded.test"; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6756 | } // namespace |
| 6757 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6758 | // 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] | 6759 | TEST_F(URLRequestTestHTTP, ProcessPKPAndSendReport) { |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6760 | GURL report_uri(kPKPReportUri); |
Hiroki Nakagawa | 4e4967bcb | 2018-10-11 04:09:51 | [diff] [blame] | 6761 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6762 | https_test_server.SetSSLConfig( |
| 6763 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6764 | https_test_server.ServeFilesFromSourceDirectory( |
| 6765 | base::FilePath(kTestFilePath)); |
| 6766 | ASSERT_TRUE(https_test_server.Start()); |
| 6767 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6768 | std::string test_server_hostname = kPKPHost; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6769 | |
| 6770 | // Set up a pin for |test_server_hostname|. |
| 6771 | TransportSecurityState security_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6772 | security_state.EnableStaticPinsForTesting(); |
| 6773 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6774 | |
| 6775 | MockCertificateReportSender mock_report_sender; |
| 6776 | security_state.SetReportSender(&mock_report_sender); |
| 6777 | |
| 6778 | // Set up a MockCertVerifier to trigger a violation of the previously |
| 6779 | // set pin. |
| 6780 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6781 | ASSERT_TRUE(cert); |
| 6782 | |
| 6783 | MockCertVerifier cert_verifier; |
| 6784 | CertVerifyResult verify_result; |
| 6785 | verify_result.verified_cert = cert; |
| 6786 | verify_result.is_issued_by_known_root = true; |
| 6787 | HashValue hash3; |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6788 | ASSERT_TRUE( |
| 6789 | hash3.FromString("sha256/3333333333333333333333333333333333333333333=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6790 | verify_result.public_key_hashes.push_back(hash3); |
| 6791 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6792 | |
| 6793 | TestNetworkDelegate network_delegate; |
| 6794 | TestURLRequestContext context(true); |
| 6795 | context.set_transport_security_state(&security_state); |
| 6796 | context.set_network_delegate(&network_delegate); |
| 6797 | context.set_cert_verifier(&cert_verifier); |
| 6798 | context.Init(); |
| 6799 | |
| 6800 | // Now send a request to trigger the violation. |
| 6801 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6802 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6803 | https_test_server.GetURL(test_server_hostname, "/simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6804 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6805 | violating_request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6806 | d.RunUntilComplete(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6807 | |
| 6808 | // Check that a report was sent. |
| 6809 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 6810 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 6811 | EXPECT_EQ("application/json; charset=utf-8", |
| 6812 | mock_report_sender.latest_content_type()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6813 | std::unique_ptr<base::Value> value( |
Lei Zhang | a8b4c5fb | 2019-02-16 03:02:03 | [diff] [blame] | 6814 | base::JSONReader::ReadDeprecated(mock_report_sender.latest_report())); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6815 | ASSERT_TRUE(value); |
jdoerrie | e48b26a | 2017-12-09 14:19:08 | [diff] [blame] | 6816 | ASSERT_TRUE(value->is_dict()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6817 | base::DictionaryValue* report_dict; |
| 6818 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 6819 | std::string report_hostname; |
| 6820 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 6821 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 6822 | } |
| 6823 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6824 | // Tests that reports do not get sent on requests to static pkp hosts that |
| 6825 | // don't have pin violations. |
| 6826 | TEST_F(URLRequestTestHTTP, ProcessPKPWithNoViolation) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6827 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6828 | https_test_server.SetSSLConfig( |
| 6829 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6830 | https_test_server.ServeFilesFromSourceDirectory( |
| 6831 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6832 | ASSERT_TRUE(https_test_server.Start()); |
| 6833 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6834 | std::string test_server_hostname = kPKPHost; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6835 | |
| 6836 | TransportSecurityState security_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6837 | security_state.EnableStaticPinsForTesting(); |
| 6838 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6839 | MockCertificateReportSender mock_report_sender; |
| 6840 | security_state.SetReportSender(&mock_report_sender); |
| 6841 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6842 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6843 | ASSERT_TRUE(cert); |
Hiroki Nakagawa | 4e4967bcb | 2018-10-11 04:09:51 | [diff] [blame] | 6844 | MockCertVerifier mock_cert_verifier; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6845 | CertVerifyResult verify_result; |
| 6846 | verify_result.verified_cert = cert; |
| 6847 | verify_result.is_issued_by_known_root = true; |
| 6848 | HashValue hash; |
| 6849 | // The expected value of GoodPin1 used by |test_default::kHSTSSource|. |
| 6850 | ASSERT_TRUE( |
| 6851 | hash.FromString("sha256/Nn8jk5By4Vkq6BeOVZ7R7AC6XUUBZsWmUbJR1f1Y5FY=")); |
| 6852 | verify_result.public_key_hashes.push_back(hash); |
| 6853 | mock_cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6854 | |
| 6855 | TestNetworkDelegate network_delegate; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6856 | TestURLRequestContext context(true); |
| 6857 | context.set_transport_security_state(&security_state); |
| 6858 | context.set_network_delegate(&network_delegate); |
| 6859 | context.set_cert_verifier(&mock_cert_verifier); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6860 | context.Init(); |
| 6861 | |
| 6862 | // Now send a request that does not trigger the violation. |
| 6863 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6864 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6865 | https_test_server.GetURL(test_server_hostname, "/simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6866 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6867 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6868 | d.RunUntilComplete(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6869 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6870 | // Check that the request succeeded, a report was not sent and the pkp was |
| 6871 | // not bypassed. |
| 6872 | EXPECT_EQ(OK, d.request_status()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6873 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 6874 | EXPECT_EQ(std::string(), mock_report_sender.latest_report()); |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6875 | TransportSecurityState::STSState sts_state; |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6876 | TransportSecurityState::PKPState pkp_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6877 | EXPECT_TRUE(security_state.GetStaticDomainState(test_server_hostname, |
| 6878 | &sts_state, &pkp_state)); |
| 6879 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
| 6880 | EXPECT_FALSE(request->ssl_info().pkp_bypassed); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6881 | } |
| 6882 | |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6883 | TEST_F(URLRequestTestHTTP, PKPBypassRecorded) { |
| 6884 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6885 | https_test_server.SetSSLConfig( |
| 6886 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6887 | https_test_server.ServeFilesFromSourceDirectory( |
| 6888 | base::FilePath(kTestFilePath)); |
| 6889 | ASSERT_TRUE(https_test_server.Start()); |
| 6890 | |
| 6891 | // Set up a MockCertVerifier to be a local root that violates the pin |
| 6892 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6893 | ASSERT_TRUE(cert); |
| 6894 | |
| 6895 | MockCertVerifier cert_verifier; |
| 6896 | CertVerifyResult verify_result; |
| 6897 | verify_result.verified_cert = cert; |
| 6898 | verify_result.is_issued_by_known_root = false; |
| 6899 | HashValue hash; |
| 6900 | ASSERT_TRUE( |
| 6901 | hash.FromString("sha256/1111111111111111111111111111111111111111111=")); |
| 6902 | verify_result.public_key_hashes.push_back(hash); |
| 6903 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6904 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6905 | std::string test_server_hostname = kPKPHost; |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6906 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6907 | // Set up PKP |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6908 | TransportSecurityState security_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6909 | security_state.EnableStaticPinsForTesting(); |
| 6910 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 6911 | MockCertificateReportSender mock_report_sender; |
| 6912 | security_state.SetReportSender(&mock_report_sender); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6913 | |
| 6914 | TestNetworkDelegate network_delegate; |
| 6915 | TestURLRequestContext context(true); |
| 6916 | context.set_transport_security_state(&security_state); |
| 6917 | context.set_network_delegate(&network_delegate); |
| 6918 | context.set_cert_verifier(&cert_verifier); |
| 6919 | context.Init(); |
| 6920 | |
| 6921 | TestDelegate d; |
| 6922 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6923 | https_test_server.GetURL(test_server_hostname, "/simple.html"), |
| 6924 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6925 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6926 | d.RunUntilComplete(); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6927 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6928 | // Check that the request succeeded, a report was not sent and the PKP was |
| 6929 | // bypassed. |
| 6930 | EXPECT_EQ(OK, d.request_status()); |
| 6931 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 6932 | EXPECT_EQ(std::string(), mock_report_sender.latest_report()); |
| 6933 | TransportSecurityState::STSState sts_state; |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6934 | TransportSecurityState::PKPState pkp_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6935 | EXPECT_TRUE(security_state.GetStaticDomainState(test_server_hostname, |
| 6936 | &sts_state, &pkp_state)); |
| 6937 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6938 | EXPECT_TRUE(request->ssl_info().pkp_bypassed); |
| 6939 | } |
| 6940 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6941 | TEST_F(URLRequestTestHTTP, ProcessSTSOnce) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6942 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6943 | https_test_server.SetSSLConfig( |
| 6944 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6945 | https_test_server.ServeFilesFromSourceDirectory( |
| 6946 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6947 | ASSERT_TRUE(https_test_server.Start()); |
| 6948 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6949 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6950 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6951 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6952 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6953 | https_test_server.GetURL("/hsts-multiple-headers.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6954 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6955 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6956 | d.RunUntilComplete(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6957 | |
| 6958 | // We should have set parameters from the first header, not the second. |
| 6959 | TransportSecurityState* security_state = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6960 | default_context().transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6961 | TransportSecurityState::STSState sts_state; |
| 6962 | EXPECT_TRUE( |
| 6963 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6964 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6965 | sts_state.upgrade_mode); |
| 6966 | EXPECT_FALSE(sts_state.include_subdomains); |
| 6967 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6968 | } |
| 6969 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6970 | // An ExpectCTReporter that records the number of times OnExpectCTFailed() was |
| 6971 | // called. |
| 6972 | class MockExpectCTReporter : public TransportSecurityState::ExpectCTReporter { |
| 6973 | public: |
| 6974 | MockExpectCTReporter() : num_failures_(0) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 6975 | ~MockExpectCTReporter() override = default; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6976 | |
| 6977 | void OnExpectCTFailed(const HostPortPair& host_port_pair, |
| 6978 | const GURL& report_uri, |
estark | ae028b46 | 2017-06-20 23:25:01 | [diff] [blame] | 6979 | base::Time expiration, |
estark | bf1b5296 | 2017-05-05 17:05:25 | [diff] [blame] | 6980 | const X509Certificate* validated_certificate_chain, |
| 6981 | const X509Certificate* served_certificate_chain, |
| 6982 | const SignedCertificateTimestampAndStatusList& |
| 6983 | signed_certificate_timestamps) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6984 | num_failures_++; |
| 6985 | } |
| 6986 | |
| 6987 | uint32_t num_failures() { return num_failures_; } |
| 6988 | |
| 6989 | private: |
| 6990 | uint32_t num_failures_; |
| 6991 | }; |
| 6992 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 6993 | // A CTPolicyEnforcer that returns a default CTPolicyCompliance value |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6994 | // for every certificate. |
| 6995 | class MockCTPolicyEnforcer : public CTPolicyEnforcer { |
| 6996 | public: |
| 6997 | MockCTPolicyEnforcer() |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 6998 | : default_result_(ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 6999 | ~MockCTPolicyEnforcer() override = default; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7000 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7001 | ct::CTPolicyCompliance CheckCompliance( |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7002 | X509Certificate* cert, |
Ryan Sleevi | 8a9c9c1 | 2018-05-09 02:36:23 | [diff] [blame] | 7003 | const ct::SCTList& verified_scts, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 7004 | const NetLogWithSource& net_log) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7005 | return default_result_; |
| 7006 | } |
| 7007 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7008 | void set_default_result(ct::CTPolicyCompliance default_result) { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7009 | default_result_ = default_result; |
| 7010 | } |
| 7011 | |
| 7012 | private: |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7013 | ct::CTPolicyCompliance default_result_; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7014 | }; |
| 7015 | |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7016 | // Tests that Expect CT headers for the preload list are processed correctly. |
| 7017 | TEST_F(URLRequestTestHTTP, PreloadExpectCTHeader) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 7018 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 7019 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7020 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7021 | https_test_server.SetSSLConfig( |
| 7022 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7023 | https_test_server.ServeFilesFromSourceDirectory( |
| 7024 | base::FilePath(kTestFilePath)); |
| 7025 | ASSERT_TRUE(https_test_server.Start()); |
| 7026 | |
| 7027 | MockExpectCTReporter reporter; |
| 7028 | TransportSecurityState transport_security_state; |
| 7029 | transport_security_state.enable_static_expect_ct_ = true; |
| 7030 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7031 | |
| 7032 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7033 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7034 | ASSERT_TRUE(cert); |
| 7035 | MockCertVerifier cert_verifier; |
| 7036 | CertVerifyResult verify_result; |
| 7037 | verify_result.verified_cert = cert; |
| 7038 | verify_result.is_issued_by_known_root = true; |
| 7039 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7040 | |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 7041 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to trigger an Expect |
| 7042 | // CT violation. |
| 7043 | DoNothingCTVerifier ct_verifier; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7044 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7045 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7046 | ct::CTPolicyCompliance::CT_POLICY_NOT_ENOUGH_SCTS); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7047 | |
| 7048 | TestNetworkDelegate network_delegate; |
| 7049 | // Use a MockHostResolver (which by default maps all hosts to |
| 7050 | // 127.0.0.1) so that the request can be sent to a site on the Expect |
| 7051 | // CT preload list. |
| 7052 | MockHostResolver host_resolver; |
| 7053 | TestURLRequestContext context(true); |
| 7054 | context.set_host_resolver(&host_resolver); |
| 7055 | context.set_transport_security_state(&transport_security_state); |
| 7056 | context.set_network_delegate(&network_delegate); |
| 7057 | context.set_cert_verifier(&cert_verifier); |
| 7058 | context.set_cert_transparency_verifier(&ct_verifier); |
vabr | 13d0074 | 2017-06-05 10:31:46 | [diff] [blame] | 7059 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7060 | context.Init(); |
| 7061 | |
| 7062 | // Now send a request to trigger the violation. |
| 7063 | TestDelegate d; |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7064 | GURL url = https_test_server.GetURL("/expect-ct-header-preload.html"); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7065 | GURL::Replacements replace_host; |
| 7066 | replace_host.SetHostStr(kExpectCTStaticHostname); |
| 7067 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7068 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
| 7069 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7070 | violating_request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7071 | d.RunUntilComplete(); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7072 | |
| 7073 | EXPECT_EQ(1u, reporter.num_failures()); |
| 7074 | } |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7075 | |
| 7076 | // Tests that Expect CT HTTP headers are processed correctly. |
| 7077 | TEST_F(URLRequestTestHTTP, ExpectCTHeader) { |
| 7078 | base::test::ScopedFeatureList feature_list; |
| 7079 | feature_list.InitAndEnableFeature( |
| 7080 | TransportSecurityState::kDynamicExpectCTFeature); |
| 7081 | |
| 7082 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7083 | https_test_server.SetSSLConfig( |
| 7084 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7085 | https_test_server.ServeFilesFromSourceDirectory( |
| 7086 | base::FilePath(kTestFilePath)); |
| 7087 | ASSERT_TRUE(https_test_server.Start()); |
| 7088 | |
| 7089 | MockExpectCTReporter reporter; |
| 7090 | TransportSecurityState transport_security_state; |
| 7091 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7092 | |
| 7093 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7094 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7095 | ASSERT_TRUE(cert); |
| 7096 | MockCertVerifier cert_verifier; |
| 7097 | CertVerifyResult verify_result; |
| 7098 | verify_result.verified_cert = cert; |
| 7099 | verify_result.is_issued_by_known_root = true; |
| 7100 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7101 | |
| 7102 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 7103 | // compliance. |
| 7104 | DoNothingCTVerifier ct_verifier; |
| 7105 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7106 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7107 | ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7108 | |
| 7109 | TestNetworkDelegate network_delegate; |
| 7110 | // Use a MockHostResolver (which by default maps all hosts to |
| 7111 | // 127.0.0.1). |
| 7112 | MockHostResolver host_resolver; |
| 7113 | TestURLRequestContext context(true); |
| 7114 | context.set_host_resolver(&host_resolver); |
| 7115 | context.set_transport_security_state(&transport_security_state); |
| 7116 | context.set_network_delegate(&network_delegate); |
| 7117 | context.set_cert_verifier(&cert_verifier); |
| 7118 | context.set_cert_transparency_verifier(&ct_verifier); |
| 7119 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 7120 | context.Init(); |
| 7121 | |
| 7122 | // Now send a request to trigger the header processing. |
| 7123 | TestDelegate d; |
| 7124 | GURL url = https_test_server.GetURL("/expect-ct-header.html"); |
| 7125 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7126 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7127 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7128 | d.RunUntilComplete(); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7129 | |
| 7130 | TransportSecurityState::ExpectCTState state; |
| 7131 | ASSERT_TRUE( |
| 7132 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 7133 | EXPECT_TRUE(state.enforce); |
| 7134 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 7135 | } |
| 7136 | |
| 7137 | // Tests that if multiple Expect CT HTTP headers are sent, they are all |
| 7138 | // processed. |
| 7139 | TEST_F(URLRequestTestHTTP, MultipleExpectCTHeaders) { |
| 7140 | base::test::ScopedFeatureList feature_list; |
| 7141 | feature_list.InitAndEnableFeature( |
| 7142 | TransportSecurityState::kDynamicExpectCTFeature); |
| 7143 | |
| 7144 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7145 | https_test_server.SetSSLConfig( |
| 7146 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7147 | https_test_server.ServeFilesFromSourceDirectory( |
| 7148 | base::FilePath(kTestFilePath)); |
| 7149 | ASSERT_TRUE(https_test_server.Start()); |
| 7150 | |
| 7151 | MockExpectCTReporter reporter; |
| 7152 | TransportSecurityState transport_security_state; |
| 7153 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7154 | |
| 7155 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7156 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7157 | ASSERT_TRUE(cert); |
| 7158 | MockCertVerifier cert_verifier; |
| 7159 | CertVerifyResult verify_result; |
| 7160 | verify_result.verified_cert = cert; |
| 7161 | verify_result.is_issued_by_known_root = true; |
| 7162 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7163 | |
| 7164 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 7165 | // compliance. |
| 7166 | DoNothingCTVerifier ct_verifier; |
| 7167 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7168 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7169 | ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7170 | |
| 7171 | TestNetworkDelegate network_delegate; |
| 7172 | // Use a MockHostResolver (which by default maps all hosts to |
| 7173 | // 127.0.0.1). |
| 7174 | MockHostResolver host_resolver; |
| 7175 | TestURLRequestContext context(true); |
| 7176 | context.set_host_resolver(&host_resolver); |
| 7177 | context.set_transport_security_state(&transport_security_state); |
| 7178 | context.set_network_delegate(&network_delegate); |
| 7179 | context.set_cert_verifier(&cert_verifier); |
| 7180 | context.set_cert_transparency_verifier(&ct_verifier); |
| 7181 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 7182 | context.Init(); |
| 7183 | |
| 7184 | // Now send a request to trigger the header processing. |
| 7185 | TestDelegate d; |
| 7186 | GURL url = https_test_server.GetURL("/expect-ct-header-multiple.html"); |
| 7187 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7188 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7189 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7190 | d.RunUntilComplete(); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7191 | |
| 7192 | TransportSecurityState::ExpectCTState state; |
| 7193 | ASSERT_TRUE( |
| 7194 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 7195 | EXPECT_TRUE(state.enforce); |
| 7196 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 7197 | } |
| 7198 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 7199 | #endif // !defined(OS_IOS) |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7200 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7201 | #if BUILDFLAG(ENABLE_REPORTING) |
| 7202 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7203 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_DontReportIfNetworkNotAccessed) { |
| 7204 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7205 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7206 | ASSERT_TRUE(https_test_server.Start()); |
| 7207 | GURL request_url = https_test_server.GetURL("/cachetime"); |
| 7208 | |
| 7209 | TestNetworkErrorLoggingService nel_service; |
| 7210 | TestURLRequestContext context(true); |
| 7211 | context.set_network_error_logging_service(&nel_service); |
| 7212 | context.Init(); |
| 7213 | |
| 7214 | // Populate the cache. |
| 7215 | TestDelegate d; |
| 7216 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7217 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7218 | request->Start(); |
| 7219 | d.RunUntilComplete(); |
| 7220 | |
| 7221 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7222 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7223 | nel_service.errors()[0]; |
| 7224 | EXPECT_EQ(request_url, error.uri); |
| 7225 | EXPECT_EQ(200, error.status_code); |
| 7226 | EXPECT_EQ(OK, error.type); |
| 7227 | |
| 7228 | request = context.CreateRequest(request_url, DEFAULT_PRIORITY, &d, |
| 7229 | TRAFFIC_ANNOTATION_FOR_TESTS); |
| 7230 | request->Start(); |
| 7231 | d.RunUntilComplete(); |
| 7232 | |
| 7233 | EXPECT_FALSE(request->response_info().network_accessed); |
| 7234 | EXPECT_TRUE(request->response_info().was_cached); |
| 7235 | // No additional NEL report was generated. |
| 7236 | EXPECT_EQ(1u, nel_service.errors().size()); |
| 7237 | } |
| 7238 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7239 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_BasicSuccess) { |
| 7240 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7241 | https_test_server.ServeFilesFromSourceDirectory( |
| 7242 | base::FilePath(kTestFilePath)); |
| 7243 | ASSERT_TRUE(https_test_server.Start()); |
| 7244 | GURL request_url = https_test_server.GetURL("/simple.html"); |
| 7245 | |
| 7246 | TestNetworkErrorLoggingService nel_service; |
| 7247 | TestURLRequestContext context(true); |
| 7248 | context.set_network_error_logging_service(&nel_service); |
| 7249 | context.Init(); |
| 7250 | |
| 7251 | TestDelegate d; |
| 7252 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7253 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7254 | request->Start(); |
| 7255 | d.RunUntilComplete(); |
| 7256 | |
| 7257 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7258 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7259 | nel_service.errors()[0]; |
| 7260 | EXPECT_EQ(request_url, error.uri); |
| 7261 | EXPECT_EQ(200, error.status_code); |
| 7262 | EXPECT_EQ(OK, error.type); |
| 7263 | } |
| 7264 | |
| 7265 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_BasicError) { |
| 7266 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7267 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7268 | ASSERT_TRUE(https_test_server.Start()); |
| 7269 | GURL request_url = https_test_server.GetURL("/close-socket"); |
| 7270 | |
| 7271 | TestNetworkErrorLoggingService nel_service; |
| 7272 | TestURLRequestContext context(true); |
| 7273 | context.set_network_error_logging_service(&nel_service); |
| 7274 | context.Init(); |
| 7275 | |
| 7276 | TestDelegate d; |
| 7277 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7278 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7279 | request->Start(); |
| 7280 | d.RunUntilComplete(); |
| 7281 | |
| 7282 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7283 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7284 | nel_service.errors()[0]; |
| 7285 | EXPECT_EQ(request_url, error.uri); |
| 7286 | EXPECT_EQ(0, error.status_code); |
| 7287 | EXPECT_EQ(ERR_EMPTY_RESPONSE, error.type); |
| 7288 | } |
| 7289 | |
| 7290 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_Redirect) { |
| 7291 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7292 | https_test_server.ServeFilesFromSourceDirectory( |
| 7293 | base::FilePath(kTestFilePath)); |
| 7294 | ASSERT_TRUE(https_test_server.Start()); |
| 7295 | GURL request_url = https_test_server.GetURL("/redirect-test.html"); |
| 7296 | GURL redirect_url = https_test_server.GetURL("/with-headers.html"); |
| 7297 | |
| 7298 | TestNetworkErrorLoggingService nel_service; |
| 7299 | TestURLRequestContext context(true); |
| 7300 | context.set_network_error_logging_service(&nel_service); |
| 7301 | context.Init(); |
| 7302 | |
| 7303 | TestDelegate d; |
| 7304 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7305 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7306 | request->Start(); |
| 7307 | d.RunUntilComplete(); |
| 7308 | |
| 7309 | ASSERT_EQ(2u, nel_service.errors().size()); |
| 7310 | const TestNetworkErrorLoggingService::RequestDetails& error1 = |
| 7311 | nel_service.errors()[0]; |
| 7312 | EXPECT_EQ(request_url, error1.uri); |
| 7313 | EXPECT_EQ(302, error1.status_code); |
| 7314 | EXPECT_EQ(OK, error1.type); |
| 7315 | const TestNetworkErrorLoggingService::RequestDetails& error2 = |
| 7316 | nel_service.errors()[1]; |
| 7317 | EXPECT_EQ(redirect_url, error2.uri); |
| 7318 | EXPECT_EQ(200, error2.status_code); |
| 7319 | EXPECT_EQ(OK, error2.type); |
| 7320 | } |
| 7321 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7322 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_RedirectWithoutLocationHeader) { |
| 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("/308-without-location-header"); |
| 7328 | |
| 7329 | TestNetworkErrorLoggingService nel_service; |
| 7330 | TestURLRequestContext context(true); |
| 7331 | context.set_network_error_logging_service(&nel_service); |
| 7332 | context.Init(); |
| 7333 | |
| 7334 | TestDelegate d; |
| 7335 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7336 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7337 | request->Start(); |
| 7338 | d.RunUntilComplete(); |
| 7339 | |
| 7340 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7341 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7342 | nel_service.errors()[0]; |
| 7343 | EXPECT_EQ(request_url, error.uri); |
| 7344 | EXPECT_EQ(308, error.status_code); |
| 7345 | // The body of the response was successfully read. |
| 7346 | EXPECT_EQ(OK, error.type); |
| 7347 | } |
| 7348 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7349 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_Auth) { |
| 7350 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7351 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7352 | ASSERT_TRUE(https_test_server.Start()); |
| 7353 | GURL request_url = https_test_server.GetURL("/auth-basic"); |
| 7354 | |
| 7355 | TestNetworkErrorLoggingService nel_service; |
| 7356 | TestURLRequestContext context(true); |
| 7357 | context.set_network_error_logging_service(&nel_service); |
| 7358 | context.Init(); |
| 7359 | |
| 7360 | TestDelegate d; |
| 7361 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7362 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7363 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7364 | request->Start(); |
| 7365 | d.RunUntilComplete(); |
| 7366 | |
| 7367 | ASSERT_EQ(2u, nel_service.errors().size()); |
| 7368 | const TestNetworkErrorLoggingService::RequestDetails& error1 = |
| 7369 | nel_service.errors()[0]; |
| 7370 | EXPECT_EQ(request_url, error1.uri); |
| 7371 | EXPECT_EQ(401, error1.status_code); |
| 7372 | EXPECT_EQ(OK, error1.type); |
| 7373 | const TestNetworkErrorLoggingService::RequestDetails& error2 = |
| 7374 | nel_service.errors()[1]; |
| 7375 | EXPECT_EQ(request_url, error2.uri); |
| 7376 | EXPECT_EQ(200, error2.status_code); |
| 7377 | EXPECT_EQ(OK, error2.type); |
| 7378 | } |
| 7379 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7380 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_304Response) { |
| 7381 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7382 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7383 | ASSERT_TRUE(https_test_server.Start()); |
| 7384 | GURL request_url = https_test_server.GetURL("/auth-basic"); |
| 7385 | |
| 7386 | TestNetworkErrorLoggingService nel_service; |
| 7387 | TestURLRequestContext context(true); |
| 7388 | context.set_network_error_logging_service(&nel_service); |
| 7389 | context.Init(); |
| 7390 | |
| 7391 | // populate the cache |
| 7392 | { |
| 7393 | TestDelegate d; |
| 7394 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7395 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 7396 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7397 | r->Start(); |
| 7398 | d.RunUntilComplete(); |
| 7399 | } |
| 7400 | ASSERT_EQ(2u, nel_service.errors().size()); |
| 7401 | const TestNetworkErrorLoggingService::RequestDetails& error1 = |
| 7402 | nel_service.errors()[0]; |
| 7403 | EXPECT_EQ(request_url, error1.uri); |
| 7404 | EXPECT_EQ(401, error1.status_code); |
| 7405 | EXPECT_EQ(OK, error1.type); |
| 7406 | const TestNetworkErrorLoggingService::RequestDetails& error2 = |
| 7407 | nel_service.errors()[1]; |
| 7408 | EXPECT_EQ(request_url, error2.uri); |
| 7409 | EXPECT_EQ(200, error2.status_code); |
| 7410 | EXPECT_EQ(OK, error2.type); |
| 7411 | |
| 7412 | // repeat request with end-to-end validation. since auth-basic results in a |
| 7413 | // cachable page, we expect this test to result in a 304. in which case, the |
| 7414 | // response should be fetched from the cache. |
| 7415 | { |
| 7416 | TestDelegate d; |
| 7417 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7418 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 7419 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7420 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 7421 | r->Start(); |
| 7422 | d.RunUntilComplete(); |
| 7423 | |
| 7424 | // Should be the same cached document. |
| 7425 | EXPECT_TRUE(r->was_cached()); |
| 7426 | } |
| 7427 | ASSERT_EQ(3u, nel_service.errors().size()); |
| 7428 | const TestNetworkErrorLoggingService::RequestDetails& error3 = |
| 7429 | nel_service.errors()[2]; |
| 7430 | EXPECT_EQ(request_url, error3.uri); |
| 7431 | EXPECT_EQ(304, error3.status_code); |
| 7432 | EXPECT_EQ(OK, error3.type); |
| 7433 | } |
| 7434 | |
| 7435 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_CancelInResponseStarted) { |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7436 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7437 | https_test_server.ServeFilesFromSourceDirectory( |
| 7438 | base::FilePath(kTestFilePath)); |
| 7439 | ASSERT_TRUE(https_test_server.Start()); |
| 7440 | GURL request_url = https_test_server.GetURL("/simple.html"); |
| 7441 | |
| 7442 | TestNetworkErrorLoggingService nel_service; |
| 7443 | TestURLRequestContext context(true); |
| 7444 | context.set_network_error_logging_service(&nel_service); |
| 7445 | context.Init(); |
| 7446 | |
| 7447 | TestDelegate d; |
| 7448 | d.set_cancel_in_response_started(true); |
| 7449 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7450 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7451 | request->Start(); |
| 7452 | d.RunUntilComplete(); |
| 7453 | |
| 7454 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7455 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7456 | nel_service.errors()[0]; |
| 7457 | EXPECT_EQ(request_url, error.uri); |
| 7458 | EXPECT_EQ(200, error.status_code); |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7459 | // Headers were received and the body should have been read but was not. |
| 7460 | EXPECT_EQ(ERR_ABORTED, error.type); |
| 7461 | } |
| 7462 | |
| 7463 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_CancelOnDataReceived) { |
| 7464 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7465 | https_test_server.ServeFilesFromSourceDirectory( |
| 7466 | base::FilePath(kTestFilePath)); |
| 7467 | ASSERT_TRUE(https_test_server.Start()); |
| 7468 | GURL request_url = https_test_server.GetURL("/simple.html"); |
| 7469 | |
| 7470 | TestNetworkErrorLoggingService nel_service; |
| 7471 | TestURLRequestContext context(true); |
| 7472 | context.set_network_error_logging_service(&nel_service); |
| 7473 | context.Init(); |
| 7474 | |
| 7475 | TestDelegate d; |
| 7476 | d.set_cancel_in_received_data(true); |
| 7477 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7478 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7479 | request->Start(); |
| 7480 | d.RunUntilComplete(); |
| 7481 | |
| 7482 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7483 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7484 | nel_service.errors()[0]; |
| 7485 | EXPECT_EQ(request_url, error.uri); |
| 7486 | EXPECT_EQ(200, error.status_code); |
| 7487 | // Data was received but the body was not completely read. |
| 7488 | EXPECT_EQ(ERR_ABORTED, error.type); |
| 7489 | } |
| 7490 | |
| 7491 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_CancelRedirect) { |
| 7492 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7493 | https_test_server.ServeFilesFromSourceDirectory( |
| 7494 | base::FilePath(kTestFilePath)); |
| 7495 | ASSERT_TRUE(https_test_server.Start()); |
| 7496 | GURL request_url = https_test_server.GetURL("/redirect-test.html"); |
| 7497 | |
| 7498 | TestNetworkErrorLoggingService nel_service; |
| 7499 | TestURLRequestContext context(true); |
| 7500 | context.set_network_error_logging_service(&nel_service); |
| 7501 | context.Init(); |
| 7502 | |
| 7503 | TestDelegate d; |
| 7504 | d.set_cancel_in_received_redirect(true); |
| 7505 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7506 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7507 | request->Start(); |
| 7508 | d.RunUntilComplete(); |
| 7509 | |
| 7510 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7511 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7512 | nel_service.errors()[0]; |
| 7513 | EXPECT_EQ(request_url, error.uri); |
| 7514 | EXPECT_EQ(302, error.status_code); |
| 7515 | // A valid HTTP response was received, even though the request was cancelled. |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7516 | EXPECT_EQ(OK, error.type); |
| 7517 | } |
| 7518 | |
| 7519 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 7520 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7521 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7522 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7523 | |
| 7524 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7525 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7526 | http_test_server()->GetURL("/content-type-normalization.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7527 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7528 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7529 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7530 | |
| 7531 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7532 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7533 | EXPECT_EQ("text/html", mime_type); |
| 7534 | |
| 7535 | std::string charset; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7536 | req->GetCharset(&charset); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7537 | EXPECT_EQ("utf-8", charset); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7538 | req->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7539 | } |
| 7540 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7541 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) { |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7542 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7543 | GURL data_url("data:,foo"); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7544 | DataProtocolHandler data_protocol_handler; |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7545 | EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7546 | |
| 7547 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7548 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7549 | } |
| 7550 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7551 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7552 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) { |
| 7553 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
| 7554 | GURL file_url("file:///foo.txt"); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 7555 | FileProtocolHandler file_protocol_handler( |
| 7556 | base::ThreadTaskRunnerHandle::Get()); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7557 | EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url)); |
| 7558 | |
| 7559 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7560 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(file_url)); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7561 | } |
| 7562 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7563 | TEST_F(URLRequestTestHTTP, RestrictFileRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7564 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7565 | |
| 7566 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7567 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7568 | http_test_server()->GetURL("/redirect-to-file.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7569 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7570 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7571 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7572 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7573 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7574 | |
| 7575 | // The redirect should have been rejected before reporting it to the caller. |
| 7576 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7577 | } |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7578 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7579 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7580 | TEST_F(URLRequestTestHTTP, RestrictDataRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7581 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7582 | |
| 7583 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7584 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7585 | http_test_server()->GetURL("/redirect-to-data.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7586 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7587 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7588 | d.RunUntilComplete(); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7589 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7590 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7591 | |
| 7592 | // The redirect should have been rejected before reporting it to the |
| 7593 | // caller. See https://crbug.com/723796 |
| 7594 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7595 | } |
| 7596 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7597 | // Test that redirects to invalid URLs are rejected. See |
| 7598 | // https://crbug.com/462272. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7599 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7600 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7601 | |
| 7602 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7603 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7604 | http_test_server()->GetURL("/redirect-to-invalid-url.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7605 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7606 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7607 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7608 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7609 | EXPECT_EQ(1, d.response_started_count()); |
| 7610 | EXPECT_EQ(ERR_INVALID_REDIRECT, d.request_status()); |
| 7611 | |
| 7612 | // The redirect should have been rejected before reporting it to the caller. |
| 7613 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7614 | } |
| 7615 | |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7616 | // Make sure redirects are cached, despite not reading their bodies. |
| 7617 | TEST_F(URLRequestTestHTTP, CacheRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7618 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7619 | GURL redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7620 | http_test_server()->GetURL("/redirect302-to-echo-cacheable"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7621 | |
| 7622 | { |
| 7623 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7624 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7625 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7626 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7627 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7628 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7629 | EXPECT_EQ(1, d.received_redirect_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7630 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7631 | } |
| 7632 | |
| 7633 | { |
| 7634 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7635 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7636 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7637 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 7638 | d.RunUntilRedirect(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7639 | |
| 7640 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7641 | EXPECT_EQ(0, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7642 | EXPECT_TRUE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7643 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 7644 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 7645 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7646 | d.RunUntilComplete(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7647 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7648 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7649 | EXPECT_EQ(OK, d.request_status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7650 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7651 | } |
| 7652 | } |
| 7653 | |
| 7654 | // Make sure a request isn't cached when a NetworkDelegate forces a redirect |
| 7655 | // when the headers are read, since the body won't have been read. |
| 7656 | TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7657 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7658 | // URL that is normally cached. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7659 | GURL initial_url = http_test_server()->GetURL("/cachetime"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7660 | |
| 7661 | { |
| 7662 | // Set up the TestNetworkDelegate tp force a redirect. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7663 | GURL redirect_to_url = http_test_server()->GetURL("/echo"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7664 | default_network_delegate_.set_redirect_on_headers_received_url( |
| 7665 | redirect_to_url); |
| 7666 | |
| 7667 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7668 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7669 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7670 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7671 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7672 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7673 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7674 | EXPECT_EQ(redirect_to_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7675 | } |
| 7676 | |
| 7677 | { |
| 7678 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7679 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7680 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7681 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7682 | d.RunUntilComplete(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7683 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7684 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7685 | EXPECT_FALSE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7686 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7687 | EXPECT_EQ(initial_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7688 | } |
| 7689 | } |
| 7690 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7691 | // Tests that redirection to an unsafe URL is allowed when it has been marked as |
| 7692 | // safe. |
| 7693 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7694 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7695 | |
| 7696 | GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url"); |
| 7697 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7698 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7699 | |
| 7700 | TestDelegate d; |
| 7701 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7702 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7703 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 7704 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7705 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7706 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7707 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7708 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7709 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7710 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7711 | EXPECT_EQ(unsafe_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7712 | EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received()); |
| 7713 | } |
| 7714 | } |
| 7715 | |
| 7716 | // Tests that a redirect to a different unsafe URL is blocked, even after adding |
| 7717 | // some other URL to the whitelist. |
| 7718 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7719 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7720 | |
| 7721 | GURL unsafe_url("data:text/html,something"); |
| 7722 | GURL different_unsafe_url("data:text/html,something-else"); |
| 7723 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7724 | default_network_delegate_.set_allowed_unsafe_redirect_url( |
| 7725 | different_unsafe_url); |
| 7726 | |
| 7727 | TestDelegate d; |
| 7728 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7729 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7730 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 7731 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7732 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7733 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7734 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7735 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7736 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7737 | |
| 7738 | // The redirect should have been rejected before reporting it to the caller. |
| 7739 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7740 | } |
| 7741 | } |
| 7742 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7743 | // Redirects from an URL with fragment to an unsafe URL with fragment should |
| 7744 | // be allowed, and the reference fragment of the target URL should be preserved. |
| 7745 | TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7746 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7747 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7748 | GURL original_url(http_test_server()->GetURL("/original#fragment1")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7749 | GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 7750 | GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 7751 | |
| 7752 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7753 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7754 | |
| 7755 | TestDelegate d; |
| 7756 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7757 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7758 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7759 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7760 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7761 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7762 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7763 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7764 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7765 | EXPECT_EQ(original_url, r->original_url()); |
| 7766 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7767 | } |
| 7768 | } |
| 7769 | |
| 7770 | // When a delegate has specified a safe redirect URL, but it does not match the |
| 7771 | // redirect target, then do not prevent the reference fragment from being added. |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7772 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7773 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7774 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7775 | GURL original_url(http_test_server()->GetURL("/original#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7776 | GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7777 | GURL redirect_url(http_test_server()->GetURL("/target")); |
| 7778 | GURL expected_redirect_url( |
| 7779 | http_test_server()->GetURL("/target#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7780 | |
| 7781 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 7782 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7783 | |
| 7784 | TestDelegate d; |
| 7785 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7786 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7787 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7788 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7789 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7790 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7791 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7792 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7793 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7794 | EXPECT_EQ(original_url, r->original_url()); |
| 7795 | EXPECT_EQ(expected_redirect_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7796 | } |
| 7797 | } |
| 7798 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7799 | // When a delegate has specified a safe redirect URL, assume that the redirect |
| 7800 | // URL should not be changed. In particular, the reference fragment should not |
| 7801 | // be modified. |
| 7802 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7803 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7804 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7805 | GURL original_url( |
| 7806 | http_test_server()->GetURL("/original#should-not-be-appended")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7807 | GURL redirect_url("data:text/html,expect-no-reference-fragment"); |
| 7808 | |
| 7809 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 7810 | default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url); |
| 7811 | |
| 7812 | TestDelegate d; |
| 7813 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7814 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7815 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7816 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7817 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7818 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7819 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7820 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7821 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7822 | EXPECT_EQ(original_url, r->original_url()); |
| 7823 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7824 | } |
| 7825 | } |
| 7826 | |
| 7827 | // When a URLRequestRedirectJob is created, the redirection must be followed and |
| 7828 | // the reference fragment of the target URL must not be modified. |
| 7829 | TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7830 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7831 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7832 | GURL original_url( |
| 7833 | http_test_server()->GetURL("/original#should-not-be-appended")); |
| 7834 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7835 | |
| 7836 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7837 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7838 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7839 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7840 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7841 | r.get(), &default_network_delegate_, redirect_url, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7842 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 7843 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7844 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7845 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7846 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7847 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7848 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7849 | EXPECT_EQ(original_url, r->original_url()); |
| 7850 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7851 | } |
| 7852 | |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7853 | TEST_F(URLRequestTestHTTP, UnsupportedReferrerScheme) { |
| 7854 | ASSERT_TRUE(http_test_server()->Start()); |
| 7855 | |
| 7856 | const std::string referrer("foobar://totally.legit.referrer"); |
| 7857 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7858 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7859 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7860 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7861 | req->SetReferrer(referrer); |
| 7862 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7863 | d.RunUntilComplete(); |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7864 | |
| 7865 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 7866 | } |
| 7867 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7868 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7869 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7870 | |
| 7871 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7872 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7873 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7874 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7875 | req->SetReferrer("http://user:[email protected]/"); |
| 7876 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7877 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7878 | |
| 7879 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 7880 | } |
| 7881 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7882 | TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7883 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7884 | |
| 7885 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7886 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7887 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7888 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7889 | req->SetReferrer("http://foo.com/test#fragment"); |
| 7890 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7891 | d.RunUntilComplete(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7892 | |
| 7893 | EXPECT_EQ(std::string("http://foo.com/test"), d.data_received()); |
| 7894 | } |
| 7895 | |
| 7896 | TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7897 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7898 | |
| 7899 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7900 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7901 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7902 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7903 | req->SetReferrer("http://foo.com/test#fragment"); |
| 7904 | req->SetReferrer(""); |
| 7905 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7906 | d.RunUntilComplete(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7907 | |
| 7908 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 7909 | } |
| 7910 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7911 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7912 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7913 | |
| 7914 | TestDelegate d; |
| 7915 | { |
| 7916 | d.set_cancel_in_received_redirect(true); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7917 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7918 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 7919 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7920 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7921 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7922 | |
| 7923 | EXPECT_EQ(1, d.response_started_count()); |
| 7924 | EXPECT_EQ(0, d.bytes_received()); |
| 7925 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7926 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7927 | } |
| 7928 | } |
| 7929 | |
| 7930 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7931 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7932 | |
| 7933 | TestDelegate d; |
| 7934 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7935 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7936 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7937 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7938 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7939 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 7940 | d.RunUntilRedirect(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7941 | |
| 7942 | EXPECT_EQ(1, d.received_redirect_count()); |
Shivani Sharma | c18f976 | 2017-10-23 16:43:23 | [diff] [blame] | 7943 | EXPECT_TRUE(d.have_full_request_headers()); |
| 7944 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 7945 | d.ClearFullRequestHeaders(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7946 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 7947 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 7948 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7949 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7950 | |
| 7951 | EXPECT_EQ(1, d.response_started_count()); |
| 7952 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7953 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7954 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7955 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 7956 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 7957 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7958 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 7959 | |
| 7960 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 7961 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7962 | EXPECT_EQ(contents, d.data_received()); |
| 7963 | } |
| 7964 | } |
| 7965 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7966 | TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7967 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7968 | |
| 7969 | TestDelegate d; |
| 7970 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7971 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7972 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7973 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7974 | |
| 7975 | EXPECT_FALSE(d.have_full_request_headers()); |
| 7976 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7977 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 7978 | d.RunUntilRedirect(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7979 | |
| 7980 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7981 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 7982 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 7983 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7984 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7985 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7986 | GURL target_url(http_test_server()->GetURL("/with-headers.html")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7987 | EXPECT_EQ(1, d.response_started_count()); |
| 7988 | EXPECT_TRUE(d.have_full_request_headers()); |
| 7989 | CheckFullRequestHeaders(d.full_request_headers(), target_url); |
| 7990 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7991 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7992 | |
| 7993 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 7994 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 7995 | path = path.Append(kTestFilePath); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7996 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 7997 | |
| 7998 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 7999 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8000 | EXPECT_EQ(contents, d.data_received()); |
| 8001 | } |
| 8002 | } |
| 8003 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8004 | TEST_F(URLRequestTestHTTP, DeferredRedirect_ModifiedHeaders) { |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8005 | ASSERT_TRUE(http_test_server()->Start()); |
| 8006 | |
| 8007 | TestDelegate d; |
| 8008 | { |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8009 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8010 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8011 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8012 | |
| 8013 | // Set initial headers for the request. |
| 8014 | req->SetExtraRequestHeaderByName("Header1", "Value1", true /* overwrite */); |
| 8015 | req->SetExtraRequestHeaderByName("Header2", "Value2", true /* overwrite */); |
| 8016 | |
| 8017 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 8018 | d.RunUntilRedirect(); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8019 | |
| 8020 | // Initial request should only have initial headers. |
| 8021 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8022 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8023 | const HttpRequestHeaders& sent_headers1 = d.full_request_headers(); |
| 8024 | std::string sent_value; |
| 8025 | EXPECT_TRUE(sent_headers1.GetHeader("Header1", &sent_value)); |
| 8026 | EXPECT_EQ("Value1", sent_value); |
| 8027 | EXPECT_TRUE(sent_headers1.GetHeader("Header2", &sent_value)); |
| 8028 | EXPECT_EQ("Value2", sent_value); |
| 8029 | EXPECT_FALSE(sent_headers1.GetHeader("Header3", &sent_value)); |
| 8030 | d.ClearFullRequestHeaders(); |
| 8031 | |
| 8032 | // Overwrite Header2 and add Header3. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8033 | net::HttpRequestHeaders modified_headers; |
| 8034 | modified_headers.SetHeader("Header2", ""); |
| 8035 | modified_headers.SetHeader("Header3", "Value3"); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8036 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8037 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 8038 | modified_headers); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8039 | d.RunUntilComplete(); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8040 | |
| 8041 | EXPECT_EQ(1, d.response_started_count()); |
| 8042 | EXPECT_FALSE(d.received_data_before_response()); |
| 8043 | EXPECT_EQ(OK, d.request_status()); |
| 8044 | |
| 8045 | // Redirected request should also have modified headers. |
| 8046 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8047 | const HttpRequestHeaders& sent_headers2 = d.full_request_headers(); |
| 8048 | EXPECT_TRUE(sent_headers2.GetHeader("Header1", &sent_value)); |
| 8049 | EXPECT_EQ("Value1", sent_value); |
| 8050 | EXPECT_TRUE(sent_headers2.GetHeader("Header2", &sent_value)); |
| 8051 | EXPECT_EQ("", sent_value); |
| 8052 | EXPECT_TRUE(sent_headers2.GetHeader("Header3", &sent_value)); |
| 8053 | EXPECT_EQ("Value3", sent_value); |
| 8054 | } |
| 8055 | } |
| 8056 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8057 | TEST_F(URLRequestTestHTTP, DeferredRedirect_RemovedHeaders) { |
| 8058 | ASSERT_TRUE(http_test_server()->Start()); |
| 8059 | |
| 8060 | TestDelegate d; |
| 8061 | { |
| 8062 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
| 8063 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8064 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8065 | |
| 8066 | // Set initial headers for the request. |
| 8067 | req->SetExtraRequestHeaderByName("Header1", "Value1", true /* overwrite */); |
| 8068 | req->SetExtraRequestHeaderByName("Header2", "Value2", true /* overwrite */); |
| 8069 | |
| 8070 | req->Start(); |
| 8071 | d.RunUntilRedirect(); |
| 8072 | |
| 8073 | // Initial request should have initial headers. |
| 8074 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8075 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8076 | const HttpRequestHeaders& sent_headers1 = d.full_request_headers(); |
| 8077 | std::string sent_value; |
| 8078 | EXPECT_TRUE(sent_headers1.GetHeader("Header1", &sent_value)); |
| 8079 | EXPECT_EQ("Value1", sent_value); |
| 8080 | EXPECT_TRUE(sent_headers1.GetHeader("Header2", &sent_value)); |
| 8081 | EXPECT_EQ("Value2", sent_value); |
| 8082 | d.ClearFullRequestHeaders(); |
| 8083 | |
| 8084 | // Keep Header1 and remove Header2. |
| 8085 | std::vector<std::string> removed_headers({"Header2"}); |
| 8086 | req->FollowDeferredRedirect(removed_headers, |
| 8087 | base::nullopt /* modified_headers */); |
| 8088 | d.RunUntilComplete(); |
| 8089 | |
| 8090 | EXPECT_EQ(1, d.response_started_count()); |
| 8091 | EXPECT_FALSE(d.received_data_before_response()); |
| 8092 | EXPECT_EQ(OK, d.request_status()); |
| 8093 | |
| 8094 | // Redirected request should also have |
| 8095 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8096 | const HttpRequestHeaders& sent_headers2 = d.full_request_headers(); |
| 8097 | EXPECT_TRUE(sent_headers2.GetHeader("Header1", &sent_value)); |
| 8098 | EXPECT_EQ("Value1", sent_value); |
| 8099 | EXPECT_FALSE(sent_headers2.GetHeader("Header2", &sent_value)); |
| 8100 | } |
| 8101 | } |
| 8102 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8103 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8104 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8105 | |
| 8106 | TestDelegate d; |
| 8107 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8108 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8109 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 8110 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8111 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 8112 | d.RunUntilRedirect(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8113 | |
| 8114 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8115 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8116 | req->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8117 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8118 | |
| 8119 | EXPECT_EQ(1, d.response_started_count()); |
| 8120 | EXPECT_EQ(0, d.bytes_received()); |
| 8121 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8122 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8123 | } |
| 8124 | } |
| 8125 | |
| 8126 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8127 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8128 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8129 | // Populate the cache. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8130 | { |
| 8131 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8132 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8133 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8134 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8135 | HttpRequestHeaders headers; |
| 8136 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8137 | req->SetExtraRequestHeaders(headers); |
| 8138 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8139 | d.RunUntilComplete(); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8140 | |
| 8141 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8142 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8143 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8144 | } |
| 8145 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8146 | // Expect a cache hit. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8147 | { |
| 8148 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8149 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8150 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8151 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8152 | HttpRequestHeaders headers; |
| 8153 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8154 | req->SetExtraRequestHeaders(headers); |
| 8155 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8156 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8157 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8158 | EXPECT_TRUE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8159 | |
| 8160 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8161 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8162 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8163 | } |
| 8164 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8165 | // Expect a cache miss. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8166 | { |
| 8167 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8168 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8169 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8170 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8171 | HttpRequestHeaders headers; |
| 8172 | headers.SetHeader("foo", "2"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8173 | req->SetExtraRequestHeaders(headers); |
| 8174 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8175 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8176 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8177 | EXPECT_FALSE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8178 | |
| 8179 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8180 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8181 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8182 | } |
| 8183 | } |
| 8184 | |
| 8185 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8186 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8187 | |
| 8188 | // populate the cache |
| 8189 | { |
| 8190 | TestDelegate d; |
| 8191 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8192 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8193 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8194 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8195 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8196 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8197 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8198 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8199 | |
| 8200 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8201 | } |
| 8202 | |
| 8203 | // repeat request with end-to-end validation. since auth-basic results in a |
| 8204 | // cachable page, we expect this test to result in a 304. in which case, the |
| 8205 | // response should be fetched from the cache. |
| 8206 | { |
| 8207 | TestDelegate d; |
| 8208 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8209 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8210 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8211 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8212 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8213 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 8214 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8215 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8216 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8217 | |
| 8218 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8219 | |
| 8220 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8221 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8222 | } |
| 8223 | } |
| 8224 | |
| 8225 | // Check that Set-Cookie headers in 401 responses are respected. |
| 8226 | // http://crbug.com/6450 |
| 8227 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8228 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8229 | |
| 8230 | GURL url_requiring_auth = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8231 | http_test_server()->GetURL("/auth-basic?set-cookie-if-challenged"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8232 | |
| 8233 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 8234 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 8235 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8236 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8237 | TestURLRequestContext context(true); |
| 8238 | context.set_network_delegate(&network_delegate); |
| 8239 | context.Init(); |
| 8240 | |
| 8241 | TestDelegate d; |
| 8242 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8243 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8244 | std::unique_ptr<URLRequest> r( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8245 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &d, |
| 8246 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8247 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8248 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8249 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8250 | |
| 8251 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8252 | |
| 8253 | // Make sure we sent the cookie in the restarted transaction. |
| 8254 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 8255 | != std::string::npos); |
| 8256 | } |
| 8257 | |
| 8258 | // Same test as above, except this time the restart is initiated earlier |
| 8259 | // (without user intervention since identity is embedded in the URL). |
| 8260 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8261 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8262 | TestURLRequestContext context(true); |
| 8263 | context.set_network_delegate(&network_delegate); |
| 8264 | context.Init(); |
| 8265 | |
| 8266 | TestDelegate d; |
| 8267 | |
| 8268 | GURL::Replacements replacements; |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 8269 | replacements.SetUsernameStr("user2"); |
| 8270 | replacements.SetPasswordStr("secret"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8271 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 8272 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8273 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 8274 | url_with_identity, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8275 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8276 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8277 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8278 | |
| 8279 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 8280 | |
| 8281 | // Make sure we sent the cookie in the restarted transaction. |
| 8282 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 8283 | != std::string::npos); |
| 8284 | } |
| 8285 | } |
| 8286 | |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8287 | TEST_F(URLRequestTestHTTP, AuthChallengeWithFilteredCookie) { |
| 8288 | ASSERT_TRUE(http_test_server()->Start()); |
| 8289 | |
| 8290 | GURL url_requiring_auth = http_test_server()->GetURL( |
| 8291 | "/auth-basic?set-cookie-if-not-challenged&set-cookie-if-challenged"); |
| 8292 | |
| 8293 | FilteringTestLayeredNetworkDelegate filtering_network_delegate( |
| 8294 | std::make_unique<TestNetworkDelegate>()); // Must outlive URLRequest. |
| 8295 | filtering_network_delegate.SetCookieFilter( |
| 8296 | "got_challenged"); // Filter the cookie auth-basic sets |
| 8297 | TestURLRequestContext context(true); |
| 8298 | context.set_network_delegate(&filtering_network_delegate); |
| 8299 | context.Init(); |
| 8300 | |
| 8301 | TestDelegate delegate; |
| 8302 | |
| 8303 | delegate.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8304 | |
| 8305 | std::unique_ptr<URLRequest> request( |
| 8306 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &delegate, |
| 8307 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8308 | request->Start(); |
| 8309 | |
| 8310 | delegate.RunUntilComplete(); |
| 8311 | EXPECT_THAT(delegate.request_status(), IsOk()); |
| 8312 | |
| 8313 | // Make sure the cookie was actually filtered. |
| 8314 | EXPECT_EQ(std::string::npos, |
| 8315 | delegate.data_received().find("Cookie: got_challenged=true")); |
| 8316 | // Make sure it was blocked twice. |
| 8317 | EXPECT_EQ(2, filtering_network_delegate.blocked_set_cookie_count()); |
| 8318 | } |
| 8319 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8320 | // Tests that load timing works as expected with auth and the cache. |
| 8321 | TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8322 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8323 | |
| 8324 | // populate the cache |
| 8325 | { |
| 8326 | TestDelegate d; |
| 8327 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8328 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8329 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8330 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8331 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8332 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8333 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8334 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8335 | |
| 8336 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8337 | |
| 8338 | LoadTimingInfo load_timing_info_before_auth; |
| 8339 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth( |
| 8340 | &load_timing_info_before_auth)); |
| 8341 | TestLoadTimingNotReused(load_timing_info_before_auth, |
| 8342 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 8343 | |
| 8344 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8345 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8346 | // The test server does not support keep alive sockets, so the second |
| 8347 | // request with auth should use a new socket. |
| 8348 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 8349 | EXPECT_NE(load_timing_info_before_auth.socket_log_id, |
| 8350 | load_timing_info.socket_log_id); |
| 8351 | EXPECT_LE(load_timing_info_before_auth.receive_headers_end, |
| 8352 | load_timing_info.connect_timing.connect_start); |
| 8353 | } |
| 8354 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8355 | // Repeat request with end-to-end validation. Since auth-basic results in a |
| 8356 | // 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] | 8357 | // response should be fetched from the cache. |
| 8358 | { |
| 8359 | TestDelegate d; |
| 8360 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8361 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8362 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8363 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8364 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8365 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 8366 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8367 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8368 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8369 | |
| 8370 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8371 | |
| 8372 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8373 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8374 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8375 | // Since there was a request that went over the wire, the load timing |
| 8376 | // information should include connection times. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8377 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8378 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8379 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8380 | } |
| 8381 | } |
| 8382 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8383 | // In this test, we do a POST which the server will 302 redirect. |
| 8384 | // The subsequent transaction should use GET, and should not send the |
| 8385 | // Content-Type header. |
| 8386 | // http://code.google.com/p/chromium/issues/detail?id=843 |
| 8387 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8388 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8389 | |
| 8390 | const char kData[] = "hello world"; |
| 8391 | |
| 8392 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8393 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8394 | http_test_server()->GetURL("/redirect-to-echoall"), DEFAULT_PRIORITY, &d, |
| 8395 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8396 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8397 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8398 | |
| 8399 | // Set headers (some of which are specific to the POST). |
| 8400 | HttpRequestHeaders headers; |
Yeol | 45b9e0a | 2018-07-27 02:03:29 | [diff] [blame] | 8401 | headers.SetHeader("Content-Type", |
| 8402 | "multipart/form-data;" |
| 8403 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ"); |
| 8404 | headers.SetHeader("Accept", |
| 8405 | "text/xml,application/xml,application/xhtml+xml," |
| 8406 | "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); |
| 8407 | headers.SetHeader("Accept-Language", "en-US,en"); |
| 8408 | headers.SetHeader("Accept-Charset", "ISO-8859-1,*,utf-8"); |
| 8409 | headers.SetHeader("Content-Length", "11"); |
| 8410 | headers.SetHeader("Origin", "http://localhost:1337/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8411 | req->SetExtraRequestHeaders(headers); |
| 8412 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8413 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8414 | |
| 8415 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8416 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8417 | EXPECT_EQ("text/html", mime_type); |
| 8418 | |
| 8419 | const std::string& data = d.data_received(); |
| 8420 | |
| 8421 | // Check that the post-specific headers were stripped: |
| 8422 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 8423 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8424 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8425 | |
| 8426 | // These extra request headers should not have been stripped. |
| 8427 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 8428 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 8429 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 8430 | } |
| 8431 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8432 | // The following tests check that we handle mutating the request for HTTP |
| 8433 | // redirects as expected. |
| 8434 | // See https://crbug.com/56373, https://crbug.com/102130, and |
| 8435 | // https://crbug.com/465517. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8436 | |
| 8437 | TEST_F(URLRequestTestHTTP, Redirect301Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8438 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8439 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8440 | const GURL url = http_test_server()->GetURL("/redirect301-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8441 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8442 | http_test_server()->GetURL("/redirect301-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8443 | |
| 8444 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8445 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8446 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8447 | |
| 8448 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8449 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8450 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8451 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8452 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8453 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8454 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8455 | } |
| 8456 | |
| 8457 | TEST_F(URLRequestTestHTTP, Redirect302Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8458 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8459 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8460 | const GURL url = http_test_server()->GetURL("/redirect302-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8461 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8462 | http_test_server()->GetURL("/redirect302-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8463 | |
| 8464 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8465 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8466 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8467 | |
| 8468 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8469 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8470 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8471 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8472 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8473 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8474 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8475 | } |
| 8476 | |
| 8477 | TEST_F(URLRequestTestHTTP, Redirect303Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8478 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8479 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8480 | const GURL url = http_test_server()->GetURL("/redirect303-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8481 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8482 | http_test_server()->GetURL("/redirect303-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8483 | |
| 8484 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8485 | HTTPRedirectMethodTest(url, "PUT", "GET", true); |
| 8486 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8487 | |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8488 | HTTPRedirectOriginHeaderTest(url, "CONNECT", "GET", std::string()); |
| 8489 | HTTPRedirectOriginHeaderTest(https_redirect_url, "CONNECT", "GET", |
| 8490 | std::string()); |
| 8491 | HTTPRedirectOriginHeaderTest(url, "DELETE", "GET", std::string()); |
| 8492 | HTTPRedirectOriginHeaderTest(https_redirect_url, "DELETE", "GET", |
| 8493 | std::string()); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8494 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8495 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8496 | HTTPRedirectOriginHeaderTest(url, "HEAD", "HEAD", url.GetOrigin().spec()); |
| 8497 | HTTPRedirectOriginHeaderTest(https_redirect_url, "HEAD", "HEAD", "null"); |
| 8498 | HTTPRedirectOriginHeaderTest(url, "OPTIONS", "GET", std::string()); |
| 8499 | HTTPRedirectOriginHeaderTest(https_redirect_url, "OPTIONS", "GET", |
| 8500 | std::string()); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8501 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8502 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8503 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8504 | HTTPRedirectOriginHeaderTest(url, "PUT", "GET", std::string()); |
| 8505 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "GET", std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8506 | } |
| 8507 | |
| 8508 | TEST_F(URLRequestTestHTTP, Redirect307Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8509 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8510 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8511 | const GURL url = http_test_server()->GetURL("/redirect307-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8512 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8513 | http_test_server()->GetURL("/redirect307-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8514 | |
| 8515 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 8516 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8517 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8518 | |
| 8519 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8520 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 8521 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 8522 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8523 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8524 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8525 | } |
| 8526 | |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8527 | TEST_F(URLRequestTestHTTP, Redirect308Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8528 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8529 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8530 | const GURL url = http_test_server()->GetURL("/redirect308-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8531 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8532 | http_test_server()->GetURL("/redirect308-to-https"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8533 | |
| 8534 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 8535 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8536 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8537 | |
| 8538 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8539 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 8540 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 8541 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8542 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8543 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8544 | } |
| 8545 | |
| 8546 | // Make sure that 308 responses without bodies are not treated as redirects. |
| 8547 | // Certain legacy apis that pre-date the response code expect this behavior |
| 8548 | // (Like Google Drive). |
| 8549 | TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8550 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8551 | |
| 8552 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8553 | const GURL url = http_test_server()->GetURL("/308-without-location-header"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8554 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8555 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8556 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8557 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8558 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8559 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8560 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8561 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8562 | EXPECT_EQ(308, request->response_headers()->response_code()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8563 | EXPECT_EQ("This is not a redirect.", d.data_received()); |
| 8564 | } |
| 8565 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8566 | TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8567 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8568 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8569 | GURL original_url( |
| 8570 | http_test_server()->GetURL("/redirect302-to-echo#fragment")); |
| 8571 | GURL expected_url(http_test_server()->GetURL("/echo#fragment")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8572 | |
| 8573 | TestDelegate d; |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8574 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8575 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8576 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8577 | r->Start(); |
| 8578 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8579 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8580 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8581 | EXPECT_EQ(OK, d.request_status()); |
| 8582 | EXPECT_EQ(original_url, r->original_url()); |
| 8583 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8584 | } |
| 8585 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8586 | TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8587 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8588 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8589 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8590 | GURL first_party_url("http://example.com"); |
| 8591 | |
| 8592 | TestDelegate d; |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8593 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8594 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8595 | r->set_site_for_cookies(first_party_url); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8596 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8597 | r->Start(); |
| 8598 | d.RunUntilComplete(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8599 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8600 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8601 | EXPECT_EQ(OK, d.request_status()); |
| 8602 | EXPECT_EQ(first_party_url, r->site_for_cookies()); |
| 8603 | } |
| 8604 | |
| 8605 | TEST_F(URLRequestTestHTTP, RedirectPreserveTopFrameOrigin) { |
| 8606 | ASSERT_TRUE(http_test_server()->Start()); |
| 8607 | |
| 8608 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 8609 | url::Origin top_frame_origin = |
| 8610 | url::Origin::Create(GURL("http://example.com")); |
| 8611 | TestDelegate d; |
| 8612 | |
| 8613 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8614 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8615 | r->set_top_frame_origin(top_frame_origin); |
| 8616 | |
| 8617 | r->Start(); |
| 8618 | d.RunUntilComplete(); |
| 8619 | |
| 8620 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8621 | EXPECT_EQ(OK, d.request_status()); |
| 8622 | EXPECT_EQ(top_frame_origin, *r->top_frame_origin()); |
| 8623 | } |
| 8624 | |
| 8625 | TEST_F(URLRequestTestHTTP, RedirectPreserveUnsetTopFrameOrigin) { |
| 8626 | ASSERT_TRUE(http_test_server()->Start()); |
| 8627 | |
| 8628 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 8629 | TestDelegate d; |
| 8630 | |
| 8631 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8632 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8633 | // Since we're not setting a top frame origin, we wouldn't expect one after |
| 8634 | // the redirect. |
| 8635 | r->Start(); |
| 8636 | d.RunUntilComplete(); |
| 8637 | |
| 8638 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8639 | EXPECT_EQ(OK, d.request_status()); |
| 8640 | EXPECT_FALSE(r->top_frame_origin()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8641 | } |
| 8642 | |
Josh Karlin | f3caee164 | 2018-12-10 22:54:27 | [diff] [blame] | 8643 | TEST_F(URLRequestTestHTTP, RedirectPreserveTopFrameURL) { |
| 8644 | ASSERT_TRUE(http_test_server()->Start()); |
| 8645 | |
| 8646 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 8647 | url::Origin top_frame_origin = |
| 8648 | url::Origin::Create(GURL("http://example.com")); |
| 8649 | TestDelegate d; |
| 8650 | { |
| 8651 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8652 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8653 | r->set_top_frame_origin(top_frame_origin); |
| 8654 | |
| 8655 | r->Start(); |
| 8656 | d.RunUntilComplete(); |
| 8657 | |
| 8658 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8659 | EXPECT_EQ(OK, d.request_status()); |
| 8660 | EXPECT_EQ(top_frame_origin, *r->top_frame_origin()); |
| 8661 | } |
| 8662 | } |
| 8663 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8664 | TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8665 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8666 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8667 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8668 | GURL original_first_party_url("http://example.com"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8669 | GURL expected_first_party_url(http_test_server()->GetURL("/echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8670 | |
| 8671 | TestDelegate d; |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8672 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8673 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8674 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8675 | r->set_site_for_cookies(original_first_party_url); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8676 | r->set_first_party_url_policy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8677 | URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 8678 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8679 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8680 | d.RunUntilComplete(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8681 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8682 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8683 | EXPECT_EQ(OK, d.request_status()); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8684 | EXPECT_EQ(expected_first_party_url, r->site_for_cookies()); |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8685 | } |
| 8686 | |
| 8687 | TEST_F(URLRequestTestHTTP, RedirectUpdateTopFrameOrigin) { |
| 8688 | ASSERT_TRUE(http_test_server()->Start()); |
| 8689 | |
| 8690 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 8691 | url::Origin original_top_frame_origin = |
| 8692 | url::Origin::Create(GURL("http://example.com")); |
| 8693 | url::Origin expected_top_frame_origin = |
| 8694 | url::Origin::Create(GURL(http_test_server()->GetURL("/echo"))); |
| 8695 | |
| 8696 | TestDelegate d; |
| 8697 | |
| 8698 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8699 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8700 | r->set_top_frame_origin(original_top_frame_origin); |
| 8701 | r->set_first_party_url_policy(URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 8702 | |
| 8703 | r->Start(); |
| 8704 | d.RunUntilComplete(); |
| 8705 | |
| 8706 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8707 | EXPECT_EQ(OK, d.request_status()); |
| 8708 | EXPECT_EQ(expected_top_frame_origin, *r->top_frame_origin()); |
| 8709 | } |
| 8710 | |
| 8711 | TEST_F(URLRequestTestHTTP, RedirectIgnoreUnsetTopFrameOrigin) { |
| 8712 | ASSERT_TRUE(http_test_server()->Start()); |
| 8713 | |
| 8714 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 8715 | TestDelegate d; |
| 8716 | |
| 8717 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8718 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8719 | // Since we're not setting a top frame origin, we wouldn't expect one after |
| 8720 | // the redirect. |
| 8721 | r->set_first_party_url_policy(URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 8722 | |
| 8723 | r->Start(); |
| 8724 | d.RunUntilComplete(); |
| 8725 | |
| 8726 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8727 | EXPECT_EQ(OK, d.request_status()); |
| 8728 | EXPECT_FALSE(r->top_frame_origin()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8729 | } |
| 8730 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8731 | TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8732 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8733 | |
| 8734 | const char kData[] = "hello world"; |
| 8735 | |
| 8736 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8737 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8738 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 8739 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8740 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8741 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8742 | HttpRequestHeaders headers; |
| 8743 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8744 | base::NumberToString(base::size(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8745 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8746 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8747 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8748 | req.get(), &default_network_delegate_, |
| 8749 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8750 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 8751 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8752 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8753 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8754 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8755 | EXPECT_EQ("GET", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8756 | } |
| 8757 | |
| 8758 | TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8759 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8760 | |
| 8761 | const char kData[] = "hello world"; |
| 8762 | |
| 8763 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8764 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8765 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 8766 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8767 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8768 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8769 | HttpRequestHeaders headers; |
| 8770 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8771 | base::NumberToString(base::size(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8772 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8773 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8774 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8775 | req.get(), &default_network_delegate_, |
| 8776 | http_test_server()->GetURL("/echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 8777 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8778 | "Very Good Reason")); |
| 8779 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8780 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8781 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8782 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8783 | EXPECT_EQ("POST", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8784 | EXPECT_EQ(kData, d.data_received()); |
| 8785 | } |
| 8786 | |
| 8787 | // Check that default A-L header is sent. |
| 8788 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8789 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8790 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 8791 | StaticHttpUserAgentSettings settings("en", std::string()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8792 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8793 | TestURLRequestContext context(true); |
| 8794 | context.set_network_delegate(&network_delegate); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8795 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8796 | context.Init(); |
| 8797 | |
| 8798 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8799 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8800 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8801 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8802 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8803 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8804 | EXPECT_EQ("en", d.data_received()); |
| 8805 | } |
| 8806 | |
| 8807 | // Check that an empty A-L header is not sent. http://crbug.com/77365. |
| 8808 | TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8809 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8810 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 8811 | std::string empty_string; // Avoid most vexing parse on line below. |
| 8812 | StaticHttpUserAgentSettings settings(empty_string, empty_string); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8813 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8814 | TestURLRequestContext context(true); |
| 8815 | context.set_network_delegate(&network_delegate); |
| 8816 | context.Init(); |
| 8817 | // We override the language after initialization because empty entries |
| 8818 | // get overridden by Init(). |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8819 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8820 | |
| 8821 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8822 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8823 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8824 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8825 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8826 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8827 | EXPECT_EQ("None", d.data_received()); |
| 8828 | } |
| 8829 | |
| 8830 | // Check that if request overrides the A-L header, the default is not appended. |
| 8831 | // See http://crbug.com/20894 |
| 8832 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8833 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8834 | |
| 8835 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8836 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8837 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8838 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8839 | HttpRequestHeaders headers; |
| 8840 | headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8841 | req->SetExtraRequestHeaders(headers); |
| 8842 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8843 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8844 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 8845 | } |
| 8846 | |
| 8847 | // Check that default A-E header is sent. |
| 8848 | TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8849 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8850 | |
| 8851 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8852 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8853 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8854 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8855 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8856 | req->SetExtraRequestHeaders(headers); |
| 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_TRUE(ContainsString(d.data_received(), "gzip")); |
| 8860 | } |
| 8861 | |
| 8862 | // Check that if request overrides the A-E header, the default is not appended. |
| 8863 | // See http://crbug.com/47381 |
| 8864 | TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { |
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-Encoding"), |
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::kAcceptEncoding, "identity"); |
[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_FALSE(ContainsString(d.data_received(), "gzip")); |
| 8877 | EXPECT_TRUE(ContainsString(d.data_received(), "identity")); |
| 8878 | } |
| 8879 | |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 8880 | // Check that setting the A-C header sends the proper header. |
| 8881 | TEST_F(URLRequestTestHTTP, SetAcceptCharset) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8882 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8883 | |
| 8884 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8885 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8886 | http_test_server()->GetURL("/echoheader?Accept-Charset"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8887 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8888 | HttpRequestHeaders headers; |
| 8889 | headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8890 | req->SetExtraRequestHeaders(headers); |
| 8891 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8892 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8893 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 8894 | } |
| 8895 | |
| 8896 | // Check that default User-Agent header is sent. |
| 8897 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8898 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8899 | |
| 8900 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8901 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8902 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8903 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8904 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8905 | d.RunUntilComplete(); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8906 | EXPECT_EQ(default_context().http_user_agent_settings()->GetUserAgent(), |
[email protected] | 051a4b3 | 2014-01-09 04:02:37 | [diff] [blame] | 8907 | d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8908 | } |
| 8909 | |
| 8910 | // Check that if request overrides the User-Agent header, |
| 8911 | // the default is not appended. |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 8912 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 8913 | #if defined(OS_IOS) |
| 8914 | #define MAYBE_OverrideUserAgent FLAKY_OverrideUserAgent |
| 8915 | #else |
| 8916 | #define MAYBE_OverrideUserAgent OverrideUserAgent |
| 8917 | #endif |
| 8918 | TEST_F(URLRequestTestHTTP, MAYBE_OverrideUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8919 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8920 | |
| 8921 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8922 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8923 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8924 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8925 | HttpRequestHeaders headers; |
| 8926 | headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8927 | req->SetExtraRequestHeaders(headers); |
| 8928 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8929 | d.RunUntilComplete(); |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 8930 | EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8931 | } |
| 8932 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8933 | // Check that a NULL HttpUserAgentSettings causes the corresponding empty |
| 8934 | // User-Agent header to be sent but does not send the Accept-Language and |
| 8935 | // Accept-Charset headers. |
| 8936 | TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8937 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8938 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8939 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8940 | TestURLRequestContext context(true); |
| 8941 | context.set_network_delegate(&network_delegate); |
| 8942 | context.Init(); |
| 8943 | // We override the HttpUserAgentSettings after initialization because empty |
| 8944 | // entries get overridden by Init(). |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 8945 | context.set_http_user_agent_settings(nullptr); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8946 | |
| 8947 | struct { |
| 8948 | const char* request; |
| 8949 | const char* expected_response; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8950 | } tests[] = {{"/echoheader?Accept-Language", "None"}, |
| 8951 | {"/echoheader?Accept-Charset", "None"}, |
| 8952 | {"/echoheader?User-Agent", ""}}; |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8953 | |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8954 | for (size_t i = 0; i < base::size(tests); i++) { |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8955 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8956 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8957 | http_test_server()->GetURL(tests[i].request), DEFAULT_PRIORITY, &d, |
| 8958 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8959 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8960 | d.RunUntilComplete(); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8961 | EXPECT_EQ(tests[i].expected_response, d.data_received()) |
| 8962 | << " Request = \"" << tests[i].request << "\""; |
| 8963 | } |
| 8964 | } |
| 8965 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8966 | // Make sure that URLRequest passes on its priority updates to |
| 8967 | // newly-created jobs after the first one. |
| 8968 | TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8969 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8970 | |
| 8971 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8972 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8973 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 8974 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8975 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8976 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8977 | std::unique_ptr<URLRequestRedirectJob> redirect_job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8978 | req.get(), &default_network_delegate_, |
| 8979 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8980 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 8981 | AddTestInterceptor()->set_main_intercept_job(std::move(redirect_job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8982 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8983 | req->SetPriority(LOW); |
| 8984 | req->Start(); |
| 8985 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8986 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8987 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8988 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8989 | req.get(), &default_network_delegate_, &job_priority)); |
| 8990 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8991 | |
| 8992 | // Should trigger |job| to be started. |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8993 | d.RunUntilComplete(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8994 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8995 | } |
| 8996 | |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8997 | // Check that creating a network request while entering/exiting suspend mode |
| 8998 | // fails as it should. This is the only case where an HttpTransactionFactory |
| 8999 | // does not return an HttpTransaction. |
| 9000 | TEST_F(URLRequestTestHTTP, NetworkSuspendTest) { |
| 9001 | // Create a new HttpNetworkLayer that thinks it's suspended. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9002 | std::unique_ptr<HttpNetworkLayer> network_layer(new HttpNetworkLayer( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9003 | default_context().http_transaction_factory()->GetSession())); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9004 | network_layer->OnSuspend(); |
| 9005 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 9006 | HttpCache http_cache(std::move(network_layer), |
zhongyi | 3ceab9f | 2017-02-04 02:06:03 | [diff] [blame] | 9007 | HttpCache::DefaultBackend::InMemory(0), |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 9008 | false /* is_main_cache */); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9009 | |
| 9010 | TestURLRequestContext context(true); |
| 9011 | context.set_http_transaction_factory(&http_cache); |
| 9012 | context.Init(); |
| 9013 | |
| 9014 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9015 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9016 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 9017 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9018 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9019 | d.RunUntilComplete(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9020 | |
| 9021 | EXPECT_TRUE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9022 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, d.request_status()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9023 | } |
| 9024 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9025 | namespace { |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9026 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9027 | // HttpTransactionFactory that synchronously fails to create transactions. |
| 9028 | class FailingHttpTransactionFactory : public HttpTransactionFactory { |
| 9029 | public: |
| 9030 | explicit FailingHttpTransactionFactory(HttpNetworkSession* network_session) |
| 9031 | : network_session_(network_session) {} |
| 9032 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 9033 | ~FailingHttpTransactionFactory() override = default; |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9034 | |
| 9035 | // HttpTransactionFactory methods: |
| 9036 | int CreateTransaction(RequestPriority priority, |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9037 | std::unique_ptr<HttpTransaction>* trans) override { |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9038 | return ERR_FAILED; |
| 9039 | } |
| 9040 | |
| 9041 | HttpCache* GetCache() override { return nullptr; } |
| 9042 | |
| 9043 | HttpNetworkSession* GetSession() override { return network_session_; } |
| 9044 | |
| 9045 | private: |
| 9046 | HttpNetworkSession* network_session_; |
| 9047 | |
| 9048 | DISALLOW_COPY_AND_ASSIGN(FailingHttpTransactionFactory); |
| 9049 | }; |
| 9050 | |
| 9051 | } // namespace |
| 9052 | |
| 9053 | // Check that when a request that fails to create an HttpTransaction can be |
| 9054 | // cancelled while the failure notification is pending, and doesn't send two |
| 9055 | // failure notifications. |
| 9056 | // |
| 9057 | // This currently only happens when in suspend mode and there's no cache, but |
| 9058 | // just use a special HttpTransactionFactory, to avoid depending on those |
| 9059 | // behaviors. |
| 9060 | TEST_F(URLRequestTestHTTP, NetworkCancelAfterCreateTransactionFailsTest) { |
| 9061 | FailingHttpTransactionFactory http_transaction_factory( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9062 | default_context().http_transaction_factory()->GetSession()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9063 | TestURLRequestContext context(true); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9064 | context.set_http_transaction_factory(&http_transaction_factory); |
| 9065 | context.set_network_delegate(default_network_delegate()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9066 | context.Init(); |
| 9067 | |
| 9068 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9069 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9070 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 9071 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9072 | // Don't send cookies (Collecting cookies is asynchronous, and need request to |
| 9073 | // try to create an HttpNetworkTransaction synchronously on start). |
| 9074 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9075 | req->Start(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9076 | req->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9077 | d.RunUntilComplete(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9078 | // Run pending error task, if there is one. |
| 9079 | base::RunLoop().RunUntilIdle(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9080 | |
| 9081 | EXPECT_TRUE(d.request_failed()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9082 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9083 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9084 | |
| 9085 | // NetworkDelegate should see the cancellation, but not the error. |
| 9086 | EXPECT_EQ(1, default_network_delegate()->canceled_requests()); |
| 9087 | EXPECT_EQ(0, default_network_delegate()->error_count()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9088 | } |
| 9089 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9090 | TEST_F(URLRequestTestHTTP, NetworkAccessedSetOnNetworkRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9091 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9092 | |
| 9093 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9094 | GURL test_url(http_test_server()->GetURL("/")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9095 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9096 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9097 | |
| 9098 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9099 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9100 | |
| 9101 | EXPECT_TRUE(req->response_info().network_accessed); |
| 9102 | } |
| 9103 | |
| 9104 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnCachedResponse) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9105 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9106 | |
| 9107 | // Populate the cache. |
| 9108 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9109 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9110 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 9111 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9112 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9113 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9114 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9115 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9116 | EXPECT_TRUE(req->response_info().network_accessed); |
| 9117 | EXPECT_FALSE(req->response_info().was_cached); |
| 9118 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9119 | req = default_context().CreateRequest( |
| 9120 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 9121 | TRAFFIC_ANNOTATION_FOR_TESTS); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9122 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9123 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9124 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9125 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9126 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9127 | EXPECT_TRUE(req->response_info().was_cached); |
| 9128 | } |
| 9129 | |
| 9130 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnLoadOnlyFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9131 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9132 | |
| 9133 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9134 | GURL test_url(http_test_server()->GetURL("/")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9135 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9136 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jkarlin | a067deed | 2016-10-27 14:48:33 | [diff] [blame] | 9137 | req->SetLoadFlags(LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9138 | |
| 9139 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9140 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9141 | |
| 9142 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9143 | } |
| 9144 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 9145 | // Test that a single job with a THROTTLED priority completes |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 9146 | // correctly in the absence of contention. |
| 9147 | TEST_F(URLRequestTestHTTP, ThrottledPriority) { |
| 9148 | ASSERT_TRUE(http_test_server()->Start()); |
| 9149 | |
| 9150 | TestDelegate d; |
| 9151 | GURL test_url(http_test_server()->GetURL("/")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9152 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9153 | test_url, THROTTLED, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 9154 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9155 | d.RunUntilComplete(); |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 9156 | |
| 9157 | EXPECT_TRUE(req->status().is_success()); |
| 9158 | } |
| 9159 | |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9160 | TEST_F(URLRequestTestHTTP, RawBodyBytesNoContentEncoding) { |
| 9161 | ASSERT_TRUE(http_test_server()->Start()); |
| 9162 | |
| 9163 | TestDelegate d; |
| 9164 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9165 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 9166 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9167 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9168 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9169 | |
| 9170 | EXPECT_EQ(5, req->GetRawBodyBytes()); |
| 9171 | } |
| 9172 | |
| 9173 | TEST_F(URLRequestTestHTTP, RawBodyBytesGzipEncoding) { |
| 9174 | ASSERT_TRUE(http_test_server()->Start()); |
| 9175 | |
| 9176 | TestDelegate d; |
| 9177 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9178 | http_test_server()->GetURL("/gzip-encoded"), DEFAULT_PRIORITY, &d, |
| 9179 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9180 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9181 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9182 | |
| 9183 | EXPECT_EQ(30, req->GetRawBodyBytes()); |
| 9184 | } |
| 9185 | |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9186 | // Check that if NetworkDelegate::OnBeforeStartTransaction returns an error, |
| 9187 | // the delegate isn't called back synchronously. |
| 9188 | TEST_F(URLRequestTestHTTP, TesBeforeStartTransactionFails) { |
| 9189 | ASSERT_TRUE(http_test_server()->Start()); |
| 9190 | default_network_delegate_.set_before_start_transaction_fails(); |
| 9191 | |
| 9192 | TestDelegate d; |
| 9193 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9194 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 9195 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9196 | req->Start(); |
| 9197 | DCHECK(!d.response_completed()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9198 | d.RunUntilComplete(); |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9199 | DCHECK(d.response_completed()); |
| 9200 | EXPECT_EQ(ERR_FAILED, d.request_status()); |
| 9201 | } |
| 9202 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9203 | class URLRequestInterceptorTestHTTP : public URLRequestTestHTTP { |
| 9204 | public: |
| 9205 | // TODO(bengr): Merge this with the URLRequestInterceptorHTTPTest fixture, |
| 9206 | // ideally remove the dependency on URLRequestTestJob, and maybe move these |
| 9207 | // tests into the factory tests. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 9208 | URLRequestInterceptorTestHTTP() |
| 9209 | : URLRequestTestHTTP(), interceptor_(nullptr) {} |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9210 | |
| 9211 | void SetUpFactory() override { |
| 9212 | interceptor_ = new MockURLRequestInterceptor(); |
| 9213 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9214 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9215 | } |
| 9216 | |
| 9217 | MockURLRequestInterceptor* interceptor() const { |
| 9218 | return interceptor_; |
| 9219 | } |
| 9220 | |
| 9221 | private: |
| 9222 | MockURLRequestInterceptor* interceptor_; |
| 9223 | }; |
| 9224 | |
| 9225 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9226 | NetworkDelegateNotificationOnRedirectIntercept) { |
| 9227 | interceptor()->set_intercept_redirect(true); |
| 9228 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 9229 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 9230 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9231 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9232 | |
| 9233 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9234 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9235 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 9236 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9237 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9238 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9239 | |
| 9240 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 9241 | // Check we got one good response |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9242 | int status = d.request_status(); |
| 9243 | EXPECT_EQ(OK, status); |
| 9244 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9245 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9246 | |
| 9247 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 9248 | EXPECT_EQ(1, d.response_started_count()); |
| 9249 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9250 | |
| 9251 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9252 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9253 | EXPECT_EQ(1, default_network_delegate()->headers_received_count()); |
| 9254 | } |
| 9255 | |
| 9256 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9257 | NetworkDelegateNotificationOnErrorIntercept) { |
| 9258 | // Intercept that error and respond with an OK response. |
| 9259 | interceptor()->set_intercept_final_response(true); |
| 9260 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 9261 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 9262 | default_network_delegate()->set_can_be_intercepted_on_error(true); |
| 9263 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9264 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9265 | |
| 9266 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9267 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9268 | http_test_server()->GetURL("/two-content-lengths.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9269 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9270 | req->set_method("GET"); |
| 9271 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9272 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9273 | |
| 9274 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 9275 | |
| 9276 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9277 | int status = d.request_status(); |
| 9278 | EXPECT_EQ(OK, status); |
| 9279 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9280 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9281 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 9282 | EXPECT_EQ(1, d.response_started_count()); |
| 9283 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9284 | |
| 9285 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9286 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9287 | EXPECT_EQ(0, default_network_delegate()->headers_received_count()); |
| 9288 | } |
| 9289 | |
| 9290 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9291 | NetworkDelegateNotificationOnResponseIntercept) { |
| 9292 | // Intercept that error and respond with an OK response. |
| 9293 | interceptor()->set_intercept_final_response(true); |
| 9294 | |
| 9295 | // Intercept with a real URLRequestHttpJob. |
| 9296 | interceptor()->set_use_url_request_http_job(true); |
| 9297 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9298 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9299 | |
| 9300 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9301 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9302 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 9303 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9304 | req->set_method("GET"); |
| 9305 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9306 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9307 | |
| 9308 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 9309 | |
| 9310 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9311 | int status = d.request_status(); |
| 9312 | EXPECT_EQ(OK, status); |
| 9313 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9314 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9315 | EXPECT_EQ("hello", d.data_received()); |
| 9316 | EXPECT_EQ(1, d.response_started_count()); |
| 9317 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9318 | |
| 9319 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9320 | EXPECT_EQ(2, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9321 | EXPECT_EQ(2, default_network_delegate()->headers_received_count()); |
| 9322 | } |
| 9323 | |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9324 | class URLRequestTestReferrerPolicy : public URLRequestTest { |
| 9325 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 9326 | URLRequestTestReferrerPolicy() = default; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9327 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9328 | void InstantiateSameOriginServers(net::EmbeddedTestServer::Type type) { |
| 9329 | origin_server_.reset(new EmbeddedTestServer(type)); |
| 9330 | if (type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9331 | origin_server_->AddDefaultHandlers( |
| 9332 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9333 | } else { |
| 9334 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9335 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9336 | ASSERT_TRUE(origin_server_->Start()); |
| 9337 | } |
| 9338 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9339 | void InstantiateCrossOriginServers(net::EmbeddedTestServer::Type origin_type, |
| 9340 | net::EmbeddedTestServer::Type dest_type) { |
| 9341 | origin_server_.reset(new EmbeddedTestServer(origin_type)); |
| 9342 | if (origin_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9343 | origin_server_->AddDefaultHandlers( |
| 9344 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9345 | } else { |
| 9346 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9347 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9348 | ASSERT_TRUE(origin_server_->Start()); |
| 9349 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9350 | destination_server_.reset(new EmbeddedTestServer(dest_type)); |
| 9351 | if (dest_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9352 | destination_server_->AddDefaultHandlers( |
| 9353 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9354 | } else { |
| 9355 | destination_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9356 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9357 | ASSERT_TRUE(destination_server_->Start()); |
| 9358 | } |
| 9359 | |
| 9360 | void VerifyReferrerAfterRedirect(URLRequest::ReferrerPolicy policy, |
| 9361 | const GURL& referrer, |
| 9362 | const GURL& expected) { |
| 9363 | // Create and execute the request: we'll only have a |destination_server_| |
| 9364 | // if the origins are meant to be distinct. Otherwise, we'll use the |
| 9365 | // |origin_server_| for both endpoints. |
| 9366 | GURL destination_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9367 | destination_server_ ? destination_server_->GetURL("/echoheader?Referer") |
| 9368 | : origin_server_->GetURL("/echoheader?Referer"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9369 | GURL origin_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9370 | origin_server_->GetURL("/server-redirect?" + destination_url.spec()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9371 | |
| 9372 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9373 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9374 | origin_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9375 | req->set_referrer_policy(policy); |
| 9376 | req->SetReferrer(referrer.spec()); |
| 9377 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9378 | d.RunUntilComplete(); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9379 | |
| 9380 | EXPECT_EQ(1, d.response_started_count()); |
| 9381 | EXPECT_EQ(1, d.received_redirect_count()); |
| 9382 | EXPECT_EQ(destination_url, req->url()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9383 | EXPECT_EQ(OK, d.request_status()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9384 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9385 | |
| 9386 | EXPECT_EQ(expected.spec(), req->referrer()); |
| 9387 | if (expected.is_empty()) |
| 9388 | EXPECT_EQ("None", d.data_received()); |
| 9389 | else |
| 9390 | EXPECT_EQ(expected.spec(), d.data_received()); |
| 9391 | } |
| 9392 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9393 | EmbeddedTestServer* origin_server() const { return origin_server_.get(); } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9394 | |
| 9395 | private: |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9396 | std::unique_ptr<EmbeddedTestServer> origin_server_; |
| 9397 | std::unique_ptr<EmbeddedTestServer> destination_server_; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9398 | }; |
| 9399 | |
| 9400 | TEST_F(URLRequestTestReferrerPolicy, HTTPToSameOriginHTTP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9401 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTP); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9402 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9403 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9404 | VerifyReferrerAfterRedirect( |
| 9405 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9406 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9407 | |
| 9408 | VerifyReferrerAfterRedirect( |
| 9409 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9410 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9411 | |
| 9412 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9413 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9414 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9415 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9416 | referrer); |
| 9417 | |
| 9418 | // The original referrer set on the request is expected to obey the referrer |
| 9419 | // policy and already be stripped to the origin; thus this test case just |
| 9420 | // checks that this policy doesn't cause the referrer to change when following |
| 9421 | // a redirect. |
| 9422 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9423 | referrer.GetOrigin()); |
| 9424 | |
| 9425 | VerifyReferrerAfterRedirect( |
| 9426 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9427 | referrer); |
| 9428 | |
| 9429 | // The original referrer set on the request is expected to obey the referrer |
| 9430 | // policy and already be stripped to the origin; thus this test case just |
| 9431 | // checks that this policy doesn't cause the referrer to change when following |
| 9432 | // a redirect. |
| 9433 | VerifyReferrerAfterRedirect( |
| 9434 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9435 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9436 | |
| 9437 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9438 | } |
| 9439 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9440 | TEST_F(URLRequestTestReferrerPolicy, HTTPToCrossOriginHTTP) { |
| 9441 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 9442 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9443 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9444 | |
| 9445 | VerifyReferrerAfterRedirect( |
| 9446 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9447 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9448 | |
| 9449 | VerifyReferrerAfterRedirect( |
| 9450 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9451 | referrer, referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9452 | |
| 9453 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9454 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9455 | referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9456 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9457 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9458 | referrer); |
| 9459 | |
| 9460 | // The original referrer set on the request is expected to obey the referrer |
| 9461 | // policy and already be stripped to the origin; thus this test case just |
| 9462 | // checks that this policy doesn't cause the referrer to change when following |
| 9463 | // a redirect. |
| 9464 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9465 | referrer.GetOrigin()); |
| 9466 | |
| 9467 | VerifyReferrerAfterRedirect( |
| 9468 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9469 | |
| 9470 | // The original referrer set on the request is expected to obey the referrer |
| 9471 | // policy and already be stripped to the origin; thus this test case just |
| 9472 | // checks that this policy doesn't cause the referrer to change when following |
| 9473 | // a redirect. |
| 9474 | VerifyReferrerAfterRedirect( |
| 9475 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9476 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9477 | |
| 9478 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9479 | } |
| 9480 | |
| 9481 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToSameOriginHTTPS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9482 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9483 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9484 | |
| 9485 | VerifyReferrerAfterRedirect( |
| 9486 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9487 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9488 | |
| 9489 | VerifyReferrerAfterRedirect( |
| 9490 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9491 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9492 | |
| 9493 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9494 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9495 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9496 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9497 | referrer); |
| 9498 | |
| 9499 | // The original referrer set on the request is expected to obey the referrer |
| 9500 | // policy and already be stripped to the origin; thus this test case just |
| 9501 | // checks that this policy doesn't cause the referrer to change when following |
| 9502 | // a redirect. |
| 9503 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9504 | referrer.GetOrigin()); |
| 9505 | |
| 9506 | VerifyReferrerAfterRedirect( |
| 9507 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9508 | referrer); |
| 9509 | |
| 9510 | // The original referrer set on the request is expected to obey the referrer |
| 9511 | // policy and already be stripped to the origin; thus this test case just |
| 9512 | // checks that this policy doesn't cause the referrer to change when following |
| 9513 | // a redirect. |
| 9514 | VerifyReferrerAfterRedirect( |
| 9515 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9516 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9517 | |
| 9518 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9519 | } |
| 9520 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9521 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToCrossOriginHTTPS) { |
| 9522 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 9523 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9524 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9525 | |
| 9526 | VerifyReferrerAfterRedirect( |
| 9527 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9528 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9529 | |
| 9530 | VerifyReferrerAfterRedirect( |
| 9531 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9532 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9533 | |
| 9534 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9535 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9536 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9537 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9538 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9539 | referrer); |
| 9540 | |
| 9541 | // The original referrer set on the request is expected to obey the referrer |
| 9542 | // policy and already be stripped to the origin; thus this test case just |
| 9543 | // checks that this policy doesn't cause the referrer to change when following |
| 9544 | // a redirect. |
| 9545 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9546 | referrer.GetOrigin()); |
| 9547 | |
| 9548 | VerifyReferrerAfterRedirect( |
| 9549 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9550 | |
| 9551 | // The original referrer set on the request is expected to obey the referrer |
| 9552 | // policy and already be stripped to the origin; thus this test case just |
| 9553 | // checks that this policy doesn't cause the referrer to change when following |
| 9554 | // a redirect. |
| 9555 | VerifyReferrerAfterRedirect( |
| 9556 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9557 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9558 | |
| 9559 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9560 | } |
| 9561 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9562 | TEST_F(URLRequestTestReferrerPolicy, HTTPToHTTPS) { |
| 9563 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 9564 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9565 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9566 | |
| 9567 | VerifyReferrerAfterRedirect( |
| 9568 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9569 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9570 | |
| 9571 | VerifyReferrerAfterRedirect( |
| 9572 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9573 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9574 | |
| 9575 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9576 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9577 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9578 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9579 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9580 | referrer); |
| 9581 | |
| 9582 | // The original referrer set on the request is expected to obey the referrer |
| 9583 | // policy and already be stripped to the origin; thus this test case just |
| 9584 | // checks that this policy doesn't cause the referrer to change when following |
| 9585 | // a redirect. |
| 9586 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9587 | referrer.GetOrigin()); |
| 9588 | |
| 9589 | VerifyReferrerAfterRedirect( |
| 9590 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9591 | |
| 9592 | // The original referrer set on the request is expected to obey the referrer |
| 9593 | // policy and already be stripped to the origin; thus this test case just |
| 9594 | // checks that this policy doesn't cause the referrer to change when following |
| 9595 | // a redirect. |
| 9596 | VerifyReferrerAfterRedirect( |
| 9597 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9598 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9599 | |
| 9600 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9601 | } |
| 9602 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9603 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToHTTP) { |
| 9604 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 9605 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9606 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9607 | |
| 9608 | VerifyReferrerAfterRedirect( |
| 9609 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9610 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9611 | |
| 9612 | VerifyReferrerAfterRedirect( |
| 9613 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9614 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9615 | |
| 9616 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9617 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9618 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9619 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9620 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9621 | referrer); |
| 9622 | |
| 9623 | // The original referrer set on the request is expected to obey the referrer |
| 9624 | // policy and already be stripped to the origin; thus this test case just |
| 9625 | // checks that this policy doesn't cause the referrer to change when following |
| 9626 | // a redirect. |
| 9627 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9628 | referrer.GetOrigin()); |
| 9629 | |
| 9630 | VerifyReferrerAfterRedirect( |
| 9631 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9632 | |
| 9633 | // The original referrer set on the request is expected to obey the referrer |
| 9634 | // policy and already be stripped to the origin, though it should be |
| 9635 | // subsequently cleared during the downgrading redirect. |
| 9636 | VerifyReferrerAfterRedirect( |
| 9637 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9638 | referrer.GetOrigin(), GURL()); |
| 9639 | |
| 9640 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9641 | } |
| 9642 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 9643 | class HTTPSRequestTest : public TestWithScopedTaskEnvironment { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9644 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 9645 | HTTPSRequestTest() : default_context_(true) { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9646 | default_context_.set_network_delegate(&default_network_delegate_); |
| 9647 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9648 | } |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9649 | ~HTTPSRequestTest() override { |
| 9650 | SetTransportSecurityStateSourceForTesting(nullptr); |
| 9651 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9652 | |
| 9653 | protected: |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9654 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9655 | TestURLRequestContext default_context_; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9656 | }; |
| 9657 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 9658 | TEST_F(HTTPSRequestTest, HTTPSGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9659 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9660 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9661 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9662 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9663 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9664 | TestDelegate d; |
| 9665 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9666 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9667 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9668 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9669 | r->Start(); |
| 9670 | EXPECT_TRUE(r->is_pending()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9671 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9672 | d.RunUntilComplete(); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9673 | |
| 9674 | EXPECT_EQ(1, d.response_started_count()); |
| 9675 | EXPECT_FALSE(d.received_data_before_response()); |
| 9676 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9677 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 9678 | EXPECT_EQ(test_server.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 9679 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 9680 | EXPECT_EQ(test_server.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 9681 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9682 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9683 | } |
| 9684 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 9685 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9686 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9687 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9688 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9689 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9690 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9691 | |
| 9692 | bool err_allowed = true; |
| 9693 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 9694 | TestDelegate d; |
| 9695 | { |
| 9696 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9697 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9698 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9699 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9700 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9701 | r->Start(); |
| 9702 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9703 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9704 | d.RunUntilComplete(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9705 | |
| 9706 | EXPECT_EQ(1, d.response_started_count()); |
| 9707 | EXPECT_FALSE(d.received_data_before_response()); |
| 9708 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9709 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9710 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9711 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9712 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9713 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9714 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9715 | } |
| 9716 | } |
| 9717 | } |
| 9718 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 9719 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9720 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9721 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED); |
| 9722 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9723 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9724 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9725 | |
| 9726 | // Iterate from false to true, just so that we do the opposite of the |
| 9727 | // previous test in order to increase test coverage. |
| 9728 | bool err_allowed = false; |
| 9729 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 9730 | TestDelegate d; |
| 9731 | { |
| 9732 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9733 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9734 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9735 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9736 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9737 | r->Start(); |
| 9738 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9739 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9740 | d.RunUntilComplete(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9741 | |
| 9742 | EXPECT_EQ(1, d.response_started_count()); |
| 9743 | EXPECT_FALSE(d.received_data_before_response()); |
| 9744 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9745 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9746 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9747 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9748 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9749 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9750 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9751 | } |
| 9752 | } |
| 9753 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 9754 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9755 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 9756 | // security state. (see http://crbug.com/550977). |
| 9757 | #if !defined(OS_IOS) |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9758 | // This tests that a load of a domain with preloaded HSTS and HPKP with a |
| 9759 | // certificate error sets the |certificate_errors_are_fatal| flag correctly. |
| 9760 | // This flag will cause the interstitial to be fatal. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9761 | TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9762 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 9763 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9764 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9765 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9766 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9767 | ASSERT_TRUE(test_server.Start()); |
| 9768 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9769 | // We require that the URL be hsts-hpkp-preloaded.test. This is a test domain |
| 9770 | // that has a preloaded HSTS+HPKP entry in the TransportSecurityState. This |
| 9771 | // means that we have to use a MockHostResolver in order to direct |
| 9772 | // hsts-hpkp-preloaded.test to the testserver. By default, MockHostResolver |
| 9773 | // maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9774 | |
| 9775 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9776 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9777 | TestURLRequestContext context(true); |
| 9778 | context.set_network_delegate(&network_delegate); |
| 9779 | context.set_host_resolver(&host_resolver); |
| 9780 | TransportSecurityState transport_security_state; |
| 9781 | context.set_transport_security_state(&transport_security_state); |
| 9782 | context.Init(); |
| 9783 | |
| 9784 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9785 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9786 | GURL(base::StringPrintf("https://hsts-hpkp-preloaded.test:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9787 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9788 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9789 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9790 | r->Start(); |
| 9791 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9792 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9793 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9794 | |
| 9795 | EXPECT_EQ(1, d.response_started_count()); |
| 9796 | EXPECT_FALSE(d.received_data_before_response()); |
| 9797 | EXPECT_TRUE(d.have_certificate_errors()); |
| 9798 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 9799 | } |
| 9800 | |
| 9801 | // This tests that cached HTTPS page loads do not cause any updates to the |
| 9802 | // TransportSecurityState. |
| 9803 | TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9804 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 9805 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9806 | // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't |
| 9807 | // matter. It just has to be any error. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9808 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9809 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9810 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9811 | ASSERT_TRUE(test_server.Start()); |
| 9812 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9813 | // We require that the URL be hsts-hpkp-preloaded.test. This is a test domain |
| 9814 | // that has a preloaded HSTS+HPKP entry in the TransportSecurityState. This |
| 9815 | // means that we have to use a MockHostResolver in order to direct |
| 9816 | // hsts-hpkp-preloaded.test to the testserver. By default, MockHostResolver |
| 9817 | // maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9818 | |
| 9819 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9820 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9821 | TestURLRequestContext context(true); |
| 9822 | context.set_network_delegate(&network_delegate); |
| 9823 | context.set_host_resolver(&host_resolver); |
| 9824 | TransportSecurityState transport_security_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9825 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9826 | TransportSecurityState::STSState static_sts_state; |
| 9827 | TransportSecurityState::PKPState static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9828 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9829 | "hsts-hpkp-preloaded.test", &static_sts_state, &static_pkp_state)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9830 | context.set_transport_security_state(&transport_security_state); |
| 9831 | context.Init(); |
| 9832 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9833 | TransportSecurityState::STSState dynamic_sts_state; |
| 9834 | TransportSecurityState::PKPState dynamic_pkp_state; |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9835 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
| 9836 | "hsts-hpkp-preloaded.test", &dynamic_sts_state)); |
| 9837 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
| 9838 | "hsts-hpkp-preloaded.test", &dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9839 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9840 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9841 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9842 | GURL(base::StringPrintf("https://hsts-hpkp-preloaded.test:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9843 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9844 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9845 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9846 | r->Start(); |
| 9847 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9848 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9849 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9850 | |
| 9851 | EXPECT_EQ(1, d.response_started_count()); |
| 9852 | EXPECT_FALSE(d.received_data_before_response()); |
| 9853 | EXPECT_TRUE(d.have_certificate_errors()); |
| 9854 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 9855 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9856 | // Get a fresh copy of the states, and check that they haven't changed. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9857 | TransportSecurityState::STSState new_static_sts_state; |
| 9858 | TransportSecurityState::PKPState new_static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9859 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9860 | "hsts-hpkp-preloaded.test", &new_static_sts_state, |
| 9861 | &new_static_pkp_state)); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9862 | TransportSecurityState::STSState new_dynamic_sts_state; |
| 9863 | TransportSecurityState::PKPState new_dynamic_pkp_state; |
| 9864 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9865 | "hsts-hpkp-preloaded.test", &new_dynamic_sts_state)); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9866 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9867 | "hsts-hpkp-preloaded.test", &new_dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9868 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9869 | EXPECT_EQ(new_static_sts_state.upgrade_mode, static_sts_state.upgrade_mode); |
| 9870 | EXPECT_EQ(new_static_sts_state.include_subdomains, |
| 9871 | static_sts_state.include_subdomains); |
| 9872 | EXPECT_EQ(new_static_pkp_state.include_subdomains, |
| 9873 | static_pkp_state.include_subdomains); |
rsleevi | 91d4c9c | 2016-05-14 20:28:48 | [diff] [blame] | 9874 | EXPECT_EQ(new_static_pkp_state.spki_hashes, static_pkp_state.spki_hashes); |
| 9875 | EXPECT_EQ(new_static_pkp_state.bad_spki_hashes, |
| 9876 | static_pkp_state.bad_spki_hashes); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9877 | } |
| 9878 | |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9879 | // Make sure HSTS preserves a POST request's method and body. |
| 9880 | TEST_F(HTTPSRequestTest, HSTSPreservesPosts) { |
| 9881 | static const char kData[] = "hello world"; |
| 9882 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9883 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9884 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9885 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9886 | ASSERT_TRUE(test_server.Start()); |
| 9887 | |
| 9888 | |
| 9889 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 9890 | // address, so a MockHostResolver is needed to redirect www.somewhere.com to |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9891 | // the EmbeddedTestServer. By default, MockHostResolver maps all hosts |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9892 | // to 127.0.0.1. |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9893 | MockHostResolver host_resolver; |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9894 | |
| 9895 | // Force https for www.somewhere.com. |
| 9896 | TransportSecurityState transport_security_state; |
[email protected] | 474f079e | 2013-03-02 19:11:20 | [diff] [blame] | 9897 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 9898 | bool include_subdomains = false; |
| 9899 | transport_security_state.AddHSTS("www.somewhere.com", expiry, |
| 9900 | include_subdomains); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9901 | |
| 9902 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 9903 | |
| 9904 | TestURLRequestContext context(true); |
| 9905 | context.set_host_resolver(&host_resolver); |
| 9906 | context.set_transport_security_state(&transport_security_state); |
| 9907 | context.set_network_delegate(&network_delegate); |
| 9908 | context.Init(); |
| 9909 | |
| 9910 | TestDelegate d; |
| 9911 | // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will |
| 9912 | // cause a certificate error. Ignore the error. |
| 9913 | d.set_allow_certificate_errors(true); |
| 9914 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9915 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9916 | GURL(base::StringPrintf("http://www.somewhere.com:%d/echo", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9917 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9918 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9919 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 9920 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9921 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9922 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9923 | d.RunUntilComplete(); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9924 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9925 | EXPECT_EQ("https", req->url().scheme()); |
| 9926 | EXPECT_EQ("POST", req->method()); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9927 | EXPECT_EQ(kData, d.data_received()); |
[email protected] | 07d9bfd | 2013-06-27 14:16:40 | [diff] [blame] | 9928 | |
| 9929 | LoadTimingInfo load_timing_info; |
| 9930 | network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info); |
| 9931 | // LoadTimingInfo of HSTS redirects is similar to that of network cache hits |
| 9932 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9933 | } |
| 9934 | |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9935 | // Make sure that the CORS headers are added to cross-origin HSTS redirects. |
| 9936 | TEST_F(HTTPSRequestTest, HSTSCrossOriginAddHeaders) { |
| 9937 | static const char kOriginHeaderValue[] = "http://www.example.com"; |
| 9938 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9939 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9940 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9941 | ASSERT_TRUE(test_server.Start()); |
| 9942 | |
| 9943 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 9944 | // address, so a MockHostResolver is needed to redirect example.net to the |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9945 | // EmbeddedTestServer. MockHostResolver maps all hosts to 127.0.0.1 by |
| 9946 | // default. |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9947 | MockHostResolver host_resolver; |
| 9948 | |
| 9949 | TransportSecurityState transport_security_state; |
| 9950 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 9951 | bool include_subdomains = false; |
| 9952 | transport_security_state.AddHSTS("example.net", expiry, include_subdomains); |
| 9953 | |
| 9954 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 9955 | |
| 9956 | MockCertVerifier cert_verifier; |
| 9957 | cert_verifier.set_default_result(OK); |
| 9958 | |
| 9959 | TestURLRequestContext context(true); |
| 9960 | context.set_host_resolver(&host_resolver); |
| 9961 | context.set_transport_security_state(&transport_security_state); |
| 9962 | context.set_network_delegate(&network_delegate); |
| 9963 | context.set_cert_verifier(&cert_verifier); |
| 9964 | context.Init(); |
| 9965 | |
| 9966 | GURL hsts_http_url(base::StringPrintf("http://example.net:%d/somehstssite", |
| 9967 | test_server.host_port_pair().port())); |
| 9968 | url::Replacements<char> replacements; |
| 9969 | const char kNewScheme[] = "https"; |
| 9970 | replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme))); |
| 9971 | GURL hsts_https_url = hsts_http_url.ReplaceComponents(replacements); |
| 9972 | |
| 9973 | TestDelegate d; |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9974 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9975 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 9976 | hsts_http_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9977 | // Set Origin header to simulate a cross-origin request. |
| 9978 | HttpRequestHeaders request_headers; |
| 9979 | request_headers.SetHeader("Origin", kOriginHeaderValue); |
| 9980 | req->SetExtraRequestHeaders(request_headers); |
| 9981 | |
| 9982 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 9983 | d.RunUntilRedirect(); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9984 | |
| 9985 | EXPECT_EQ(1, d.received_redirect_count()); |
| 9986 | |
| 9987 | const HttpResponseHeaders* headers = req->response_headers(); |
| 9988 | std::string redirect_location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 9989 | EXPECT_TRUE( |
| 9990 | headers->EnumerateHeader(nullptr, "Location", &redirect_location)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9991 | EXPECT_EQ(hsts_https_url.spec(), redirect_location); |
| 9992 | |
| 9993 | std::string received_cors_header; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 9994 | EXPECT_TRUE(headers->EnumerateHeader(nullptr, "Access-Control-Allow-Origin", |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9995 | &received_cors_header)); |
| 9996 | EXPECT_EQ(kOriginHeaderValue, received_cors_header); |
| 9997 | } |
| 9998 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9999 | namespace { |
| 10000 | |
| 10001 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 10002 | public: |
| 10003 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10004 | set_on_complete(base::DoNothing()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10005 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 10006 | void OnCertificateRequested(URLRequest* request, |
| 10007 | SSLCertRequestInfo* cert_request_info) override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10008 | on_certificate_requested_count_++; |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10009 | std::move(on_certificate_requested_).Run(); |
| 10010 | } |
| 10011 | void RunUntilCertificateRequested() { |
| 10012 | base::RunLoop run_loop; |
| 10013 | on_certificate_requested_ = run_loop.QuitClosure(); |
| 10014 | run_loop.Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10015 | } |
| 10016 | int on_certificate_requested_count() { |
| 10017 | return on_certificate_requested_count_; |
| 10018 | } |
| 10019 | private: |
| 10020 | int on_certificate_requested_count_; |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10021 | base::OnceClosure on_certificate_requested_; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10022 | }; |
| 10023 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10024 | class TestSSLPrivateKey : public SSLPrivateKey { |
| 10025 | public: |
| 10026 | explicit TestSSLPrivateKey(scoped_refptr<SSLPrivateKey> key) |
| 10027 | : key_(std::move(key)) {} |
| 10028 | |
| 10029 | void set_fail_signing(bool fail_signing) { fail_signing_ = fail_signing; } |
| 10030 | int sign_count() const { return sign_count_; } |
| 10031 | |
David Benjamin | b65b073 | 2018-11-09 20:33:53 | [diff] [blame] | 10032 | std::string GetProviderName() override { return key_->GetProviderName(); } |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10033 | std::vector<uint16_t> GetAlgorithmPreferences() override { |
| 10034 | return key_->GetAlgorithmPreferences(); |
| 10035 | } |
| 10036 | void Sign(uint16_t algorithm, |
| 10037 | base::span<const uint8_t> input, |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 10038 | SignCallback callback) override { |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10039 | sign_count_++; |
| 10040 | if (fail_signing_) { |
| 10041 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 10042 | FROM_HERE, base::BindOnce(std::move(callback), |
| 10043 | ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED, |
| 10044 | std::vector<uint8_t>())); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10045 | } else { |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 10046 | key_->Sign(algorithm, input, std::move(callback)); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10047 | } |
| 10048 | } |
| 10049 | |
| 10050 | private: |
| 10051 | ~TestSSLPrivateKey() override = default; |
| 10052 | |
| 10053 | scoped_refptr<SSLPrivateKey> key_; |
| 10054 | bool fail_signing_ = false; |
| 10055 | int sign_count_ = 0; |
| 10056 | }; |
| 10057 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10058 | } // namespace |
| 10059 | |
| 10060 | // TODO(davidben): Test the rest of the code. Specifically, |
| 10061 | // - Filtering which certificates to select. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10062 | // - Getting a certificate request in an SSL renegotiation sending the |
| 10063 | // HTTP request. |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10064 | TEST_F(HTTPSRequestTest, ClientAuthNoCertificate) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10065 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10066 | net::SSLServerConfig ssl_config; |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 10067 | ssl_config.client_cert_type = |
| 10068 | SSLServerConfig::ClientCertType::OPTIONAL_CLIENT_CERT; |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10069 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10070 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10071 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10072 | ASSERT_TRUE(test_server.Start()); |
| 10073 | |
| 10074 | SSLClientAuthTestDelegate d; |
| 10075 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10076 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10077 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10078 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10079 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10080 | r->Start(); |
| 10081 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10082 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10083 | d.RunUntilCertificateRequested(); |
| 10084 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10085 | |
| 10086 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10087 | EXPECT_FALSE(d.received_data_before_response()); |
| 10088 | EXPECT_EQ(0, d.bytes_received()); |
| 10089 | |
| 10090 | // Send no certificate. |
| 10091 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 10092 | // all platforms so we can test sending a cert as well. |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 10093 | r->ContinueWithCertificate(nullptr, nullptr); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10094 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10095 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10096 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10097 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10098 | EXPECT_EQ(1, d.response_started_count()); |
| 10099 | EXPECT_FALSE(d.received_data_before_response()); |
| 10100 | EXPECT_NE(0, d.bytes_received()); |
| 10101 | } |
| 10102 | } |
| 10103 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10104 | TEST_F(HTTPSRequestTest, ClientAuth) { |
| 10105 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 10106 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 10107 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 10108 | ASSERT_TRUE(identity); |
| 10109 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 10110 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 10111 | |
| 10112 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10113 | net::SSLServerConfig ssl_config; |
| 10114 | ssl_config.client_cert_type = |
| 10115 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 10116 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 10117 | test_server.AddDefaultHandlers( |
| 10118 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10119 | ASSERT_TRUE(test_server.Start()); |
| 10120 | |
| 10121 | { |
| 10122 | SSLClientAuthTestDelegate d; |
| 10123 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10124 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10125 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10126 | |
| 10127 | r->Start(); |
| 10128 | EXPECT_TRUE(r->is_pending()); |
| 10129 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10130 | d.RunUntilCertificateRequested(); |
| 10131 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10132 | |
| 10133 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10134 | EXPECT_FALSE(d.received_data_before_response()); |
| 10135 | EXPECT_EQ(0, d.bytes_received()); |
| 10136 | |
| 10137 | // Send a certificate. |
| 10138 | r->ContinueWithCertificate(identity->certificate(), private_key); |
| 10139 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10140 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10141 | |
| 10142 | EXPECT_EQ(OK, d.request_status()); |
| 10143 | EXPECT_EQ(1, d.response_started_count()); |
| 10144 | EXPECT_FALSE(d.received_data_before_response()); |
| 10145 | EXPECT_NE(0, d.bytes_received()); |
| 10146 | |
| 10147 | // The private key should have been used. |
| 10148 | EXPECT_EQ(1, private_key->sign_count()); |
| 10149 | } |
| 10150 | |
| 10151 | // Close all connections and clear the session cache to force a new handshake. |
| 10152 | default_context_.http_transaction_factory() |
| 10153 | ->GetSession() |
| 10154 | ->CloseAllConnections(); |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10155 | default_context_.http_transaction_factory() |
| 10156 | ->GetSession() |
| 10157 | ->ClearSSLSessionCache(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10158 | |
| 10159 | // Connecting again should not call OnCertificateRequested. The identity is |
| 10160 | // taken from the client auth cache. |
| 10161 | { |
| 10162 | SSLClientAuthTestDelegate d; |
| 10163 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10164 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10165 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10166 | |
| 10167 | r->Start(); |
| 10168 | EXPECT_TRUE(r->is_pending()); |
| 10169 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10170 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10171 | |
| 10172 | EXPECT_EQ(OK, d.request_status()); |
| 10173 | EXPECT_EQ(0, d.on_certificate_requested_count()); |
| 10174 | EXPECT_FALSE(d.received_data_before_response()); |
| 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(2, private_key->sign_count()); |
| 10181 | } |
| 10182 | } |
| 10183 | |
| 10184 | // Test that private keys that fail to sign anything get evicted from the cache. |
| 10185 | TEST_F(HTTPSRequestTest, ClientAuthFailSigning) { |
| 10186 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 10187 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 10188 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 10189 | ASSERT_TRUE(identity); |
| 10190 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 10191 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 10192 | private_key->set_fail_signing(true); |
| 10193 | |
| 10194 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10195 | net::SSLServerConfig ssl_config; |
| 10196 | ssl_config.client_cert_type = |
| 10197 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 10198 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 10199 | test_server.AddDefaultHandlers( |
| 10200 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10201 | ASSERT_TRUE(test_server.Start()); |
| 10202 | |
| 10203 | { |
| 10204 | SSLClientAuthTestDelegate d; |
| 10205 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10206 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10207 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10208 | |
| 10209 | r->Start(); |
| 10210 | EXPECT_TRUE(r->is_pending()); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10211 | |
| 10212 | d.RunUntilCertificateRequested(); |
| 10213 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10214 | |
| 10215 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10216 | EXPECT_FALSE(d.received_data_before_response()); |
| 10217 | EXPECT_EQ(0, d.bytes_received()); |
| 10218 | |
| 10219 | // Send a certificate. |
| 10220 | r->ContinueWithCertificate(identity->certificate(), private_key); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10221 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10222 | |
| 10223 | // The private key cannot sign anything, so we report an error. |
| 10224 | EXPECT_EQ(ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED, d.request_status()); |
| 10225 | EXPECT_EQ(1, d.response_started_count()); |
| 10226 | EXPECT_FALSE(d.received_data_before_response()); |
| 10227 | EXPECT_EQ(0, d.bytes_received()); |
| 10228 | |
| 10229 | // The private key should have been used. |
| 10230 | EXPECT_EQ(1, private_key->sign_count()); |
| 10231 | } |
| 10232 | |
| 10233 | // Close all connections and clear the session cache to force a new handshake. |
| 10234 | default_context_.http_transaction_factory() |
| 10235 | ->GetSession() |
| 10236 | ->CloseAllConnections(); |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10237 | default_context_.http_transaction_factory() |
| 10238 | ->GetSession() |
| 10239 | ->ClearSSLSessionCache(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10240 | |
| 10241 | // The bad identity should have been evicted from the cache, so connecting |
| 10242 | // again should call OnCertificateRequested again. |
| 10243 | { |
| 10244 | SSLClientAuthTestDelegate d; |
| 10245 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10246 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10247 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10248 | |
| 10249 | r->Start(); |
| 10250 | EXPECT_TRUE(r->is_pending()); |
| 10251 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10252 | d.RunUntilCertificateRequested(); |
| 10253 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10254 | |
| 10255 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10256 | EXPECT_FALSE(d.received_data_before_response()); |
| 10257 | EXPECT_EQ(0, d.bytes_received()); |
| 10258 | |
| 10259 | // There should have been no additional uses of the private key. |
| 10260 | EXPECT_EQ(1, private_key->sign_count()); |
| 10261 | } |
| 10262 | } |
| 10263 | |
| 10264 | // Test that cached private keys that fail to sign anything trigger a |
| 10265 | // retry. This is so we handle unplugged smartcards |
| 10266 | // gracefully. https://crbug.com/813022. |
| 10267 | TEST_F(HTTPSRequestTest, ClientAuthFailSigningRetry) { |
| 10268 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 10269 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 10270 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 10271 | ASSERT_TRUE(identity); |
| 10272 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 10273 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 10274 | |
| 10275 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10276 | net::SSLServerConfig ssl_config; |
| 10277 | ssl_config.client_cert_type = |
| 10278 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 10279 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 10280 | test_server.AddDefaultHandlers( |
| 10281 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10282 | ASSERT_TRUE(test_server.Start()); |
| 10283 | |
| 10284 | // Connect with a client certificate to put it in the client auth cache. |
| 10285 | { |
| 10286 | SSLClientAuthTestDelegate d; |
| 10287 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10288 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10289 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10290 | |
| 10291 | r->Start(); |
| 10292 | EXPECT_TRUE(r->is_pending()); |
| 10293 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10294 | d.RunUntilCertificateRequested(); |
| 10295 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10296 | |
| 10297 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10298 | EXPECT_FALSE(d.received_data_before_response()); |
| 10299 | EXPECT_EQ(0, d.bytes_received()); |
| 10300 | |
| 10301 | r->ContinueWithCertificate(identity->certificate(), private_key); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10302 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10303 | |
| 10304 | EXPECT_EQ(OK, d.request_status()); |
| 10305 | EXPECT_EQ(1, d.response_started_count()); |
| 10306 | EXPECT_FALSE(d.received_data_before_response()); |
| 10307 | EXPECT_NE(0, d.bytes_received()); |
| 10308 | |
| 10309 | // The private key should have been used. |
| 10310 | EXPECT_EQ(1, private_key->sign_count()); |
| 10311 | } |
| 10312 | |
| 10313 | // Close all connections and clear the session cache to force a new handshake. |
| 10314 | default_context_.http_transaction_factory() |
| 10315 | ->GetSession() |
| 10316 | ->CloseAllConnections(); |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10317 | default_context_.http_transaction_factory() |
| 10318 | ->GetSession() |
| 10319 | ->ClearSSLSessionCache(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10320 | |
| 10321 | // Cause the private key to fail. Connecting again should attempt to use it, |
| 10322 | // notice the failure, and then request a new identity via |
| 10323 | // OnCertificateRequested. |
| 10324 | private_key->set_fail_signing(true); |
| 10325 | |
| 10326 | { |
| 10327 | SSLClientAuthTestDelegate d; |
| 10328 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10329 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10330 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10331 | |
| 10332 | r->Start(); |
| 10333 | EXPECT_TRUE(r->is_pending()); |
| 10334 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10335 | d.RunUntilCertificateRequested(); |
| 10336 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10337 | |
| 10338 | // There was an additional signing call on the private key (the one which |
| 10339 | // failed). |
| 10340 | EXPECT_EQ(2, private_key->sign_count()); |
| 10341 | |
| 10342 | // That caused another OnCertificateRequested call. |
| 10343 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10344 | EXPECT_FALSE(d.received_data_before_response()); |
| 10345 | EXPECT_EQ(0, d.bytes_received()); |
| 10346 | } |
| 10347 | } |
| 10348 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10349 | TEST_F(HTTPSRequestTest, ResumeTest) { |
| 10350 | // Test that we attempt a session resume when making two connections to the |
| 10351 | // same host. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10352 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10353 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10354 | SpawnedTestServer test_server( |
| 10355 | SpawnedTestServer::TYPE_HTTPS, |
| 10356 | ssl_options, |
| 10357 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10358 | ASSERT_TRUE(test_server.Start()); |
| 10359 | |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10360 | default_context_.http_transaction_factory() |
| 10361 | ->GetSession() |
| 10362 | ->ClearSSLSessionCache(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10363 | |
| 10364 | { |
| 10365 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10366 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10367 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10368 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10369 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10370 | r->Start(); |
| 10371 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10372 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10373 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10374 | |
| 10375 | EXPECT_EQ(1, d.response_started_count()); |
| 10376 | } |
| 10377 | |
| 10378 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 10379 | CloseAllConnections(); |
| 10380 | |
| 10381 | { |
| 10382 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10383 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10384 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10385 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10386 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10387 | r->Start(); |
| 10388 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10389 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10390 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10391 | |
| 10392 | // The response will look like; |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10393 | // lookup uvw (TLS 1.3's compatibility session ID) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10394 | // insert abc |
| 10395 | // lookup abc |
| 10396 | // insert xyz |
| 10397 | // |
| 10398 | // With a newline at the end which makes the split think that there are |
| 10399 | // four lines. |
| 10400 | |
| 10401 | EXPECT_EQ(1, d.response_started_count()); |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 10402 | std::vector<std::string> lines = base::SplitString( |
| 10403 | d.data_received(), "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10404 | ASSERT_EQ(5u, lines.size()) << d.data_received(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10405 | |
| 10406 | std::string session_id; |
| 10407 | |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10408 | for (size_t i = 0; i < 3; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 10409 | std::vector<std::string> parts = base::SplitString( |
| 10410 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10411 | ASSERT_EQ(2u, parts.size()); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10412 | if (i % 2 == 1) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10413 | EXPECT_EQ("insert", parts[0]); |
| 10414 | session_id = parts[1]; |
| 10415 | } else { |
| 10416 | EXPECT_EQ("lookup", parts[0]); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10417 | if (i != 0) |
| 10418 | EXPECT_EQ(session_id, parts[1]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10419 | } |
| 10420 | } |
| 10421 | } |
| 10422 | } |
| 10423 | |
| 10424 | TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { |
| 10425 | // Test that sessions aren't resumed when the value of ssl_session_cache_shard |
| 10426 | // differs. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10427 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10428 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10429 | SpawnedTestServer test_server( |
| 10430 | SpawnedTestServer::TYPE_HTTPS, |
| 10431 | ssl_options, |
| 10432 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10433 | ASSERT_TRUE(test_server.Start()); |
| 10434 | |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10435 | default_context_.http_transaction_factory() |
| 10436 | ->GetSession() |
| 10437 | ->ClearSSLSessionCache(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10438 | |
| 10439 | { |
| 10440 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10441 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 10442 | test_server.GetURL("/"), DEFAULT_PRIORITY, &d, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10443 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10444 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10445 | r->Start(); |
| 10446 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10447 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10448 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10449 | |
| 10450 | EXPECT_EQ(1, d.response_started_count()); |
| 10451 | } |
| 10452 | |
| 10453 | // Now create a new HttpCache with a different ssl_session_cache_shard value. |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10454 | HttpNetworkSession::Context session_context; |
| 10455 | session_context.host_resolver = default_context_.host_resolver(); |
| 10456 | session_context.cert_verifier = default_context_.cert_verifier(); |
| 10457 | session_context.transport_security_state = |
| 10458 | default_context_.transport_security_state(); |
| 10459 | session_context.cert_transparency_verifier = |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10460 | default_context_.cert_transparency_verifier(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10461 | session_context.ct_policy_enforcer = default_context_.ct_policy_enforcer(); |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 10462 | session_context.proxy_resolution_service = default_context_.proxy_resolution_service(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10463 | session_context.ssl_config_service = default_context_.ssl_config_service(); |
| 10464 | session_context.http_auth_handler_factory = |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10465 | default_context_.http_auth_handler_factory(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10466 | session_context.http_server_properties = |
| 10467 | default_context_.http_server_properties(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10468 | |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10469 | HttpNetworkSession network_session(HttpNetworkSession::Params(), |
| 10470 | session_context); |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 10471 | std::unique_ptr<HttpCache> cache( |
| 10472 | new HttpCache(&network_session, HttpCache::DefaultBackend::InMemory(0), |
| 10473 | false /* is_main_cache */)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10474 | |
| 10475 | default_context_.set_http_transaction_factory(cache.get()); |
| 10476 | |
| 10477 | { |
| 10478 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10479 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10480 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10481 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10482 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10483 | r->Start(); |
| 10484 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10485 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10486 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10487 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10488 | EXPECT_EQ(1, d.response_started_count()); |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 10489 | EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, r->ssl_info().handshake_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10490 | } |
| 10491 | } |
| 10492 | |
Daniel McArdle | da3fa94 | 2019-02-15 16:41:21 | [diff] [blame] | 10493 | // Test that sessions started with privacy mode enabled cannot be resumed when |
| 10494 | // it is disabled, and vice versa. |
| 10495 | TEST_F(HTTPSRequestTest, NoSessionResumptionBetweenPrivacyModes) { |
| 10496 | // Start a server. |
| 10497 | SpawnedTestServer test_server( |
| 10498 | SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::SSLOptions(), |
| 10499 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10500 | ASSERT_TRUE(test_server.Start()); |
| 10501 | const auto url = test_server.GetURL("/"); |
| 10502 | |
| 10503 | auto ConnectAndCheckHandshake = [this, url](bool allow_credentials, |
| 10504 | auto expected_handshake) { |
| 10505 | // Construct request and indirectly set the privacy mode. |
| 10506 | TestDelegate d; |
| 10507 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10508 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10509 | r->set_allow_credentials(allow_credentials); |
| 10510 | |
| 10511 | // Start the request and check the SSL handshake type. |
| 10512 | r->Start(); |
| 10513 | EXPECT_TRUE(r->is_pending()); |
| 10514 | d.RunUntilComplete(); |
| 10515 | EXPECT_EQ(1, d.response_started_count()); |
| 10516 | EXPECT_EQ(expected_handshake, r->ssl_info().handshake_type); |
| 10517 | }; |
| 10518 | |
| 10519 | // Exhaustively check all pairs of privacy mode values. Note that we are using |
| 10520 | // allow_credentials to indirectly enable/disable privacy mode. |
| 10521 | const bool kAllowCredentialsValues[] = {false, true}; |
| 10522 | for (const auto allow_creds_1 : kAllowCredentialsValues) { |
| 10523 | for (const auto allow_creds_2 : kAllowCredentialsValues) { |
| 10524 | SCOPED_TRACE(base::StringPrintf("allow_creds_1=%d, allow_creds_2=%d", |
| 10525 | allow_creds_1, allow_creds_2)); |
| 10526 | |
| 10527 | // The session cache starts off empty, so we expect a full handshake. |
| 10528 | ConnectAndCheckHandshake(allow_creds_1, SSLInfo::HANDSHAKE_FULL); |
| 10529 | |
| 10530 | // The second handshake depends on whether we are using the same session |
| 10531 | // cache as the first request. |
| 10532 | ConnectAndCheckHandshake(allow_creds_2, allow_creds_1 == allow_creds_2 |
| 10533 | ? SSLInfo::HANDSHAKE_RESUME |
| 10534 | : SSLInfo::HANDSHAKE_FULL); |
| 10535 | // Flush both session caches. |
| 10536 | auto* network_session = |
| 10537 | default_context_.http_transaction_factory()->GetSession(); |
| 10538 | network_session->ClearSSLSessionCache(); |
| 10539 | } |
| 10540 | } |
| 10541 | } |
| 10542 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 10543 | class HTTPSFallbackTest : public TestWithScopedTaskEnvironment { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10544 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 10545 | HTTPSFallbackTest() : context_(true) { |
Nick Harper | 2243e800 | 2018-09-28 20:33:26 | [diff] [blame] | 10546 | ssl_config_service_ = std::make_unique<TestSSLConfigService>(); |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10547 | context_.set_ssl_config_service(ssl_config_service_.get()); |
| 10548 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 10549 | ~HTTPSFallbackTest() override = default; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10550 | |
| 10551 | protected: |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10552 | TestSSLConfigService* ssl_config_service() { |
| 10553 | return ssl_config_service_.get(); |
| 10554 | } |
| 10555 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10556 | void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) { |
| 10557 | DCHECK(!request_); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10558 | context_.Init(); |
| 10559 | delegate_.set_allow_certificate_errors(true); |
| 10560 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10561 | SpawnedTestServer test_server( |
| 10562 | SpawnedTestServer::TYPE_HTTPS, |
| 10563 | ssl_options, |
| 10564 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10565 | ASSERT_TRUE(test_server.Start()); |
| 10566 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10567 | request_ = context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10568 | &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10569 | request_->Start(); |
| 10570 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10571 | delegate_.RunUntilComplete(); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10572 | } |
| 10573 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10574 | void ExpectConnection(int version) { |
| 10575 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 10576 | EXPECT_NE(0, delegate_.bytes_received()); |
| 10577 | EXPECT_EQ(version, SSLConnectionStatusToVersion( |
| 10578 | request_->ssl_info().connection_status)); |
| 10579 | } |
| 10580 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10581 | void ExpectFailure(int error) { |
| 10582 | EXPECT_EQ(1, delegate_.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 10583 | EXPECT_EQ(error, delegate_.request_status()); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10584 | } |
| 10585 | |
| 10586 | private: |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 10587 | std::unique_ptr<TestSSLConfigService> ssl_config_service_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10588 | TestDelegate delegate_; |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 10589 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10590 | std::unique_ptr<URLRequest> request_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10591 | }; |
| 10592 | |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10593 | // Tests the TLS 1.0 fallback doesn't happen. |
| 10594 | TEST_F(HTTPSFallbackTest, TLSv1NoFallback) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10595 | SpawnedTestServer::SSLOptions ssl_options( |
| 10596 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10597 | ssl_options.tls_intolerant = |
| 10598 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 10599 | |
| 10600 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 10601 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10602 | } |
| 10603 | |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 10604 | // Tests the TLS 1.1 fallback doesn't happen. |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 10605 | TEST_F(HTTPSFallbackTest, TLSv1_1NoFallback) { |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10606 | SpawnedTestServer::SSLOptions ssl_options( |
| 10607 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10608 | ssl_options.tls_intolerant = |
| 10609 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2; |
| 10610 | |
| 10611 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10612 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
| 10613 | } |
| 10614 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10615 | // Tests that TLS 1.3 interference results in a dedicated error code. |
| 10616 | TEST_F(HTTPSFallbackTest, TLSv1_3Interference) { |
| 10617 | SpawnedTestServer::SSLOptions ssl_options( |
| 10618 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10619 | ssl_options.tls_intolerant = |
| 10620 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_3; |
| 10621 | ssl_config_service()->set_max_version(SSL_PROTOCOL_VERSION_TLS1_3); |
| 10622 | |
| 10623 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 10624 | ExpectFailure(ERR_SSL_VERSION_INTERFERENCE); |
| 10625 | } |
| 10626 | |
| 10627 | // Tests that disabling TLS 1.3 leaves TLS 1.3 interference unnoticed. |
| 10628 | TEST_F(HTTPSFallbackTest, TLSv1_3InterferenceDisableVersion) { |
| 10629 | SpawnedTestServer::SSLOptions ssl_options( |
| 10630 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10631 | ssl_options.tls_intolerant = |
| 10632 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_3; |
| 10633 | ssl_config_service()->set_max_version(SSL_PROTOCOL_VERSION_TLS1_2); |
| 10634 | |
| 10635 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 10636 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1_2); |
| 10637 | } |
| 10638 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 10639 | class HTTPSSessionTest : public TestWithScopedTaskEnvironment { |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10640 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 10641 | HTTPSSessionTest() : default_context_(true) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10642 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10643 | |
| 10644 | default_context_.set_network_delegate(&default_network_delegate_); |
| 10645 | default_context_.set_cert_verifier(&cert_verifier_); |
| 10646 | default_context_.Init(); |
| 10647 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 10648 | ~HTTPSSessionTest() override = default; |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10649 | |
| 10650 | protected: |
| 10651 | MockCertVerifier cert_verifier_; |
| 10652 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 10653 | TestURLRequestContext default_context_; |
| 10654 | }; |
| 10655 | |
| 10656 | // Tests that session resumption is not attempted if an invalid certificate |
| 10657 | // is presented. |
| 10658 | TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) { |
| 10659 | SpawnedTestServer::SSLOptions ssl_options; |
| 10660 | ssl_options.record_resume = true; |
| 10661 | SpawnedTestServer test_server( |
| 10662 | SpawnedTestServer::TYPE_HTTPS, |
| 10663 | ssl_options, |
| 10664 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10665 | ASSERT_TRUE(test_server.Start()); |
| 10666 | |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10667 | default_context_.http_transaction_factory() |
| 10668 | ->GetSession() |
| 10669 | ->ClearSSLSessionCache(); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10670 | |
| 10671 | // Simulate the certificate being expired and attempt a connection. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10672 | cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10673 | { |
| 10674 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10675 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 10676 | test_server.GetURL("/"), DEFAULT_PRIORITY, &d, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10677 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10678 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10679 | r->Start(); |
| 10680 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10681 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10682 | d.RunUntilComplete(); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10683 | |
| 10684 | EXPECT_EQ(1, d.response_started_count()); |
| 10685 | } |
| 10686 | |
| 10687 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 10688 | CloseAllConnections(); |
| 10689 | |
| 10690 | // Now change the certificate to be acceptable (so that the response is |
| 10691 | // loaded), and ensure that no session id is presented to the peer. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10692 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10693 | { |
| 10694 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10695 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10696 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10697 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10698 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10699 | r->Start(); |
| 10700 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10701 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10702 | d.RunUntilComplete(); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10703 | |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10704 | EXPECT_EQ(1, d.response_started_count()); |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 10705 | EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, r->ssl_info().handshake_type); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10706 | } |
| 10707 | } |
| 10708 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10709 | // This the fingerprint of the "Testing CA" certificate used by the testserver. |
| 10710 | // See net/data/ssl/certificates/ocsp-test-root.pem. |
David Benjamin | 9cedc3a5 | 2017-08-20 21:30:58 | [diff] [blame] | 10711 | static const SHA256HashValue kOCSPTestCertFingerprint = {{ |
| 10712 | 0x0c, 0xa9, 0x05, 0x11, 0xb0, 0xa2, 0xc0, 0x1d, 0x40, 0x6a, 0x99, |
| 10713 | 0x04, 0x21, 0x36, 0x45, 0x3f, 0x59, 0x12, 0x5c, 0x80, 0x64, 0x2d, |
| 10714 | 0x46, 0x6a, 0x3b, 0x78, 0x9e, 0x84, 0xea, 0x54, 0x0f, 0x8b, |
mattm | 3b4376f1 | 2016-10-03 21:07:15 | [diff] [blame] | 10715 | }}; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10716 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 10717 | // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the |
| 10718 | // testserver. |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 10719 | static const SHA256HashValue kOCSPTestCertSPKI = {{ |
| 10720 | 0x05, 0xa8, 0xf6, 0xfd, 0x8e, 0x10, 0xfe, 0x92, 0x2f, 0x22, 0x75, |
| 10721 | 0x46, 0x40, 0xf4, 0xc4, 0x57, 0x06, 0x0d, 0x95, 0xfd, 0x60, 0x31, |
| 10722 | 0x3b, 0xf3, 0xfc, 0x12, 0x47, 0xe7, 0x66, 0x1a, 0x82, 0xa3, |
| 10723 | }}; |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 10724 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10725 | // This is the policy OID contained in the certificates that testserver |
| 10726 | // generates. |
| 10727 | static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1"; |
| 10728 | |
| 10729 | class HTTPSOCSPTest : public HTTPSRequestTest { |
| 10730 | public: |
| 10731 | HTTPSOCSPTest() |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10732 | : context_(true), |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 10733 | ev_test_policy_( |
| 10734 | new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(), |
| 10735 | kOCSPTestCertFingerprint, |
| 10736 | kOCSPTestCertPolicy)) { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10737 | } |
| 10738 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 10739 | void SetUp() override { |
Ryan Sleevi | 8a9c9c1 | 2018-05-09 02:36:23 | [diff] [blame] | 10740 | context_.SetCTPolicyEnforcer(std::make_unique<DefaultCTPolicyEnforcer>()); |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10741 | context_.Init(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10742 | |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 10743 | context_.cert_verifier()->SetConfig(GetCertVerifierConfig()); |
| 10744 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10745 | scoped_refptr<X509Certificate> root_cert = |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10746 | ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame^] | 10747 | CHECK_NE(static_cast<X509Certificate*>(nullptr), root_cert.get()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 10748 | test_root_.reset(new ScopedTestRoot(root_cert.get())); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10749 | |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 10750 | #if defined(OS_ANDROID) || defined(USE_BUILTIN_CERT_VERIFIER) |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 10751 | SetGlobalCertNetFetcherForTesting(net::CreateCertNetFetcher(&context_)); |
| 10752 | #endif |
| 10753 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 10754 | #if defined(USE_NSS_CERTS) |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10755 | SetURLRequestContextForNSSHttpIO(&context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10756 | #endif |
| 10757 | } |
| 10758 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10759 | void DoConnectionWithDelegate( |
| 10760 | const SpawnedTestServer::SSLOptions& ssl_options, |
| 10761 | TestDelegate* delegate, |
| 10762 | SSLInfo* out_ssl_info) { |
| 10763 | // Always overwrite |out_ssl_info|. |
| 10764 | out_ssl_info->Reset(); |
| 10765 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10766 | SpawnedTestServer test_server( |
| 10767 | SpawnedTestServer::TYPE_HTTPS, |
| 10768 | ssl_options, |
| 10769 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10770 | ASSERT_TRUE(test_server.Start()); |
| 10771 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10772 | delegate->set_allow_certificate_errors(true); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10773 | std::unique_ptr<URLRequest> r( |
| 10774 | context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
| 10775 | delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10776 | r->Start(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10777 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10778 | delegate->RunUntilComplete(); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10779 | EXPECT_EQ(1, delegate->response_started_count()); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10780 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10781 | *out_ssl_info = r->ssl_info(); |
| 10782 | } |
| 10783 | |
| 10784 | void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options, |
| 10785 | CertStatus* out_cert_status) { |
| 10786 | // Always overwrite |out_cert_status|. |
| 10787 | *out_cert_status = 0; |
| 10788 | |
| 10789 | TestDelegate d; |
| 10790 | SSLInfo ssl_info; |
| 10791 | ASSERT_NO_FATAL_FAILURE( |
| 10792 | DoConnectionWithDelegate(ssl_options, &d, &ssl_info)); |
| 10793 | |
| 10794 | *out_cert_status = ssl_info.cert_status; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10795 | } |
| 10796 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 10797 | ~HTTPSOCSPTest() override { |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 10798 | #if defined(OS_ANDROID) || defined(USE_BUILTIN_CERT_VERIFIER) |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 10799 | ShutdownGlobalCertNetFetcher(); |
| 10800 | #endif |
| 10801 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 10802 | #if defined(USE_NSS_CERTS) |
Matt Mueller | a17ffaf | 2018-03-28 20:08:18 | [diff] [blame] | 10803 | SetURLRequestContextForNSSHttpIO(nullptr); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10804 | #endif |
| 10805 | } |
| 10806 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10807 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 10808 | // GetCertVerifierConfig() configures the URLRequestContext that will be used |
| 10809 | // for making connections to the testserver. This can be overridden in test |
| 10810 | // subclasses for different behaviour. |
| 10811 | virtual CertVerifier::Config GetCertVerifierConfig() { |
| 10812 | CertVerifier::Config config; |
| 10813 | config.enable_rev_checking = true; |
| 10814 | return config; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10815 | } |
| 10816 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10817 | std::unique_ptr<ScopedTestRoot> test_root_; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 10818 | std::unique_ptr<TestSSLConfigService> ssl_config_service_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10819 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10820 | std::unique_ptr<ScopedTestEVPolicy> ev_test_policy_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10821 | }; |
| 10822 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10823 | static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() { |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 10824 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10825 | // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't |
| 10826 | // have that ability on other platforms. |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10827 | // TODO(eroman): Should this also be the return value for |
| 10828 | // CertVerifyProcBuiltin? |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10829 | return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 10830 | #else |
| 10831 | return 0; |
| 10832 | #endif |
| 10833 | } |
| 10834 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10835 | // SystemSupportsHardFailRevocationChecking returns true iff the current |
| 10836 | // operating system supports revocation checking and can distinguish between |
| 10837 | // situations where a given certificate lacks any revocation information (eg: |
| 10838 | // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when |
| 10839 | // revocation information cannot be obtained (eg: the CRL was unreachable). |
| 10840 | // If it does not, then tests which rely on 'hard fail' behaviour should be |
| 10841 | // skipped. |
| 10842 | static bool SystemSupportsHardFailRevocationChecking() { |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10843 | #if defined(OS_WIN) || defined(USE_NSS_CERTS) || \ |
| 10844 | defined(USE_BUILTIN_CERT_VERIFIER) |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10845 | return true; |
| 10846 | #else |
| 10847 | return false; |
| 10848 | #endif |
| 10849 | } |
| 10850 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10851 | // SystemUsesChromiumEVMetadata returns true iff the current operating system |
| 10852 | // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then |
| 10853 | // several tests are effected because our testing EV certificate won't be |
| 10854 | // recognised as EV. |
| 10855 | static bool SystemUsesChromiumEVMetadata() { |
Eric Roman | 5f8d9d2 | 2017-10-17 02:32:52 | [diff] [blame] | 10856 | #if defined(PLATFORM_USES_CHROMIUM_EV_METADATA) |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 10857 | return true; |
Eric Roman | 5f8d9d2 | 2017-10-17 02:32:52 | [diff] [blame] | 10858 | #else |
| 10859 | return false; |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 10860 | #endif |
| 10861 | } |
| 10862 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 10863 | // Returns the expected CertStatus for tests that expect an online revocation |
| 10864 | // check failure as a result of checking a test EV cert, which will not |
| 10865 | // actually trigger an online revocation check on some platforms. |
| 10866 | static CertStatus ExpectedCertStatusForFailedOnlineEVRevocationCheck() { |
| 10867 | if (SystemUsesChromiumEVMetadata()) { |
| 10868 | return ExpectedCertStatusForFailedOnlineRevocationCheck(); |
| 10869 | } else { |
| 10870 | // If SystemUsesChromiumEVMetadata is false, revocation checking will not |
| 10871 | // be enabled, and thus there will not be a revocation check to fail. |
| 10872 | return 0u; |
| 10873 | } |
| 10874 | } |
| 10875 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 10876 | static bool SystemSupportsOCSP() { |
Sergey Ulanov | ee7c8db | 2017-12-08 00:18:39 | [diff] [blame] | 10877 | #if defined(OS_ANDROID) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10878 | // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. |
| 10879 | return false; |
| 10880 | #else |
| 10881 | return true; |
| 10882 | #endif |
| 10883 | } |
| 10884 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10885 | static bool SystemSupportsOCSPStapling() { |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10886 | #if defined(USE_NSS_CERTS) || defined(OS_WIN) || \ |
| 10887 | defined(USE_BUILTIN_CERT_VERIFIER) |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10888 | return true; |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10889 | #else |
| 10890 | return false; |
| 10891 | #endif |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10892 | } |
| 10893 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10894 | TEST_F(HTTPSOCSPTest, Valid) { |
| 10895 | if (!SystemSupportsOCSP()) { |
| 10896 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10897 | return; |
| 10898 | } |
| 10899 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10900 | SpawnedTestServer::SSLOptions ssl_options( |
| 10901 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10902 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10903 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 10904 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 10905 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10906 | |
| 10907 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10908 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10909 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 10910 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10911 | |
| 10912 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10913 | } |
| 10914 | |
| 10915 | TEST_F(HTTPSOCSPTest, Revoked) { |
| 10916 | if (!SystemSupportsOCSP()) { |
| 10917 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10918 | return; |
| 10919 | } |
| 10920 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10921 | SpawnedTestServer::SSLOptions ssl_options( |
| 10922 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10923 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10924 | |
| 10925 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 10926 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10927 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10928 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10929 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 10930 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10931 | } |
| 10932 | |
| 10933 | TEST_F(HTTPSOCSPTest, Invalid) { |
| 10934 | if (!SystemSupportsOCSP()) { |
| 10935 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10936 | return; |
| 10937 | } |
| 10938 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10939 | SpawnedTestServer::SSLOptions ssl_options( |
| 10940 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10941 | ssl_options.ocsp_status = |
| 10942 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10943 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 10944 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 10945 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10946 | |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10947 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 10948 | // TODO(649017): This test uses soft-fail revocation checking, but returns an |
| 10949 | // invalid OCSP response (can't parse). CertVerifyProcBuiltin currently |
| 10950 | // doesn't consider this a candidate for soft-fail (only considers |
| 10951 | // network-level failures as skippable). |
| 10952 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 10953 | cert_status & CERT_STATUS_UNABLE_TO_CHECK_REVOCATION); |
| 10954 | #else |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10955 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10956 | cert_status & CERT_STATUS_ALL_ERRORS); |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10957 | #endif |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10958 | |
| 10959 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 10960 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 10961 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10962 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10963 | |
Matt Mueller | 5339d86 | 2018-05-02 18:53:57 | [diff] [blame] | 10964 | TEST_F(HTTPSOCSPTest, IntermediateValid) { |
| 10965 | if (!SystemSupportsOCSP()) { |
| 10966 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10967 | return; |
| 10968 | } |
| 10969 | |
| 10970 | SpawnedTestServer::SSLOptions ssl_options( |
| 10971 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 10972 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 10973 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 10974 | |
| 10975 | CertStatus cert_status; |
| 10976 | DoConnection(ssl_options, &cert_status); |
| 10977 | |
| 10978 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10979 | |
| 10980 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 10981 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 10982 | |
| 10983 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10984 | } |
| 10985 | |
| 10986 | TEST_F(HTTPSOCSPTest, IntermediateResponseOldButStillValid) { |
| 10987 | if (!SystemSupportsOCSP()) { |
| 10988 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10989 | return; |
| 10990 | } |
| 10991 | |
| 10992 | SpawnedTestServer::SSLOptions ssl_options( |
| 10993 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 10994 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 10995 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 10996 | // Use an OCSP response for the intermediate that would be too old for a leaf |
| 10997 | // cert, but is still valid for an intermediate. |
| 10998 | ssl_options.ocsp_intermediate_date = |
| 10999 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG; |
| 11000 | |
| 11001 | CertStatus cert_status; |
| 11002 | DoConnection(ssl_options, &cert_status); |
| 11003 | |
| 11004 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11005 | |
| 11006 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11007 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11008 | |
| 11009 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11010 | } |
| 11011 | |
| 11012 | TEST_F(HTTPSOCSPTest, IntermediateResponseTooOld) { |
| 11013 | if (!SystemSupportsOCSP()) { |
| 11014 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11015 | return; |
| 11016 | } |
| 11017 | |
| 11018 | SpawnedTestServer::SSLOptions ssl_options( |
| 11019 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11020 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11021 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11022 | ssl_options.ocsp_intermediate_date = |
| 11023 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONGER; |
| 11024 | |
| 11025 | CertStatus cert_status; |
| 11026 | DoConnection(ssl_options, &cert_status); |
| 11027 | |
| 11028 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 11029 | // The builtin verifier enforces the baseline requirements for max age of an |
| 11030 | // intermediate's OCSP response. |
| 11031 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 11032 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11033 | EXPECT_EQ(0u, cert_status & CERT_STATUS_IS_EV); |
| 11034 | #else |
| 11035 | // The platform verifiers are more lenient. |
| 11036 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11037 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11038 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11039 | #endif |
| 11040 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11041 | } |
| 11042 | |
| 11043 | TEST_F(HTTPSOCSPTest, IntermediateRevoked) { |
| 11044 | if (!SystemSupportsOCSP()) { |
| 11045 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11046 | return; |
| 11047 | } |
| 11048 | |
| 11049 | SpawnedTestServer::SSLOptions ssl_options( |
| 11050 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11051 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11052 | ssl_options.ocsp_intermediate_status = |
| 11053 | SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 11054 | |
| 11055 | CertStatus cert_status; |
| 11056 | DoConnection(ssl_options, &cert_status); |
| 11057 | |
| 11058 | #if defined(OS_WIN) |
| 11059 | // TODO(mattm): why does CertVerifyProcWin accept this? |
| 11060 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11061 | #else |
| 11062 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11063 | #endif |
| 11064 | EXPECT_EQ(0u, cert_status & CERT_STATUS_IS_EV); |
| 11065 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11066 | } |
| 11067 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11068 | TEST_F(HTTPSOCSPTest, ValidStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11069 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11070 | LOG(WARNING) |
| 11071 | << "Skipping test because system doesn't support OCSP stapling"; |
| 11072 | return; |
| 11073 | } |
| 11074 | |
| 11075 | SpawnedTestServer::SSLOptions ssl_options( |
| 11076 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11077 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11078 | ssl_options.staple_ocsp_response = true; |
| 11079 | ssl_options.ocsp_server_unavailable = true; |
| 11080 | |
| 11081 | CertStatus cert_status; |
| 11082 | DoConnection(ssl_options, &cert_status); |
| 11083 | |
| 11084 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11085 | |
| 11086 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11087 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11088 | |
| 11089 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11090 | } |
| 11091 | |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 11092 | // Disabled on NSS ports. See https://crbug.com/431716. |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 11093 | #if defined(USE_NSS_CERTS) |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 11094 | #define MAYBE_RevokedStapled DISABLED_RevokedStapled |
| 11095 | #else |
| 11096 | #define MAYBE_RevokedStapled RevokedStapled |
| 11097 | #endif |
| 11098 | TEST_F(HTTPSOCSPTest, MAYBE_RevokedStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11099 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11100 | LOG(WARNING) |
| 11101 | << "Skipping test because system doesn't support OCSP stapling"; |
| 11102 | return; |
| 11103 | } |
| 11104 | |
| 11105 | SpawnedTestServer::SSLOptions ssl_options( |
| 11106 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11107 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 11108 | ssl_options.staple_ocsp_response = true; |
| 11109 | ssl_options.ocsp_server_unavailable = true; |
| 11110 | |
| 11111 | CertStatus cert_status; |
| 11112 | DoConnection(ssl_options, &cert_status); |
| 11113 | |
| 11114 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11115 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11116 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11117 | } |
| 11118 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11119 | static const struct OCSPVerifyTestData { |
| 11120 | std::vector<SpawnedTestServer::SSLOptions::OCSPSingleResponse> ocsp_responses; |
| 11121 | SpawnedTestServer::SSLOptions::OCSPProduced ocsp_produced; |
| 11122 | OCSPVerifyResult::ResponseStatus response_status; |
| 11123 | bool has_revocation_status; |
| 11124 | OCSPRevocationStatus cert_status; |
| 11125 | } kOCSPVerifyData[] = { |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11126 | // 0 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11127 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11128 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11129 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11130 | OCSPVerifyResult::PROVIDED, |
| 11131 | true, |
| 11132 | OCSPRevocationStatus::GOOD}, |
| 11133 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11134 | // 1 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11135 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11136 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 11137 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11138 | OCSPVerifyResult::INVALID_DATE, |
| 11139 | false, |
| 11140 | OCSPRevocationStatus::UNKNOWN}, |
| 11141 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11142 | // 2 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11143 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11144 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11145 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11146 | OCSPVerifyResult::INVALID_DATE, |
| 11147 | false, |
| 11148 | OCSPRevocationStatus::UNKNOWN}, |
| 11149 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11150 | // 3 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11151 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11152 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11153 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11154 | OCSPVerifyResult::INVALID_DATE, |
| 11155 | false, |
| 11156 | OCSPRevocationStatus::UNKNOWN}, |
| 11157 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11158 | // 4 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11159 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11160 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11161 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11162 | OCSPVerifyResult::INVALID_DATE, |
| 11163 | false, |
| 11164 | OCSPRevocationStatus::UNKNOWN}, |
| 11165 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11166 | // 5 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11167 | {{{SpawnedTestServer::SSLOptions::OCSP_TRY_LATER, |
| 11168 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11169 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11170 | OCSPVerifyResult::ERROR_RESPONSE, |
| 11171 | false, |
| 11172 | OCSPRevocationStatus::UNKNOWN}, |
| 11173 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11174 | // 6 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11175 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE, |
| 11176 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11177 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11178 | OCSPVerifyResult::PARSE_RESPONSE_ERROR, |
| 11179 | false, |
| 11180 | OCSPRevocationStatus::UNKNOWN}, |
| 11181 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11182 | // 7 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11183 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE_DATA, |
| 11184 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11185 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11186 | OCSPVerifyResult::PARSE_RESPONSE_DATA_ERROR, |
| 11187 | false, |
| 11188 | OCSPRevocationStatus::UNKNOWN}, |
| 11189 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11190 | // 8 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11191 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11192 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 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 | // 9 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11199 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11200 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11201 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11202 | OCSPVerifyResult::PROVIDED, |
| 11203 | true, |
| 11204 | OCSPRevocationStatus::UNKNOWN}, |
| 11205 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11206 | // 10 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11207 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11208 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 11209 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11210 | OCSPVerifyResult::INVALID_DATE, |
| 11211 | false, |
| 11212 | OCSPRevocationStatus::UNKNOWN}, |
| 11213 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11214 | // 11 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11215 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11216 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11217 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11218 | OCSPVerifyResult::INVALID_DATE, |
| 11219 | false, |
| 11220 | OCSPRevocationStatus::UNKNOWN}, |
| 11221 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11222 | // 12 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11223 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11224 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11225 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_BEFORE_CERT, |
| 11226 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11227 | false, |
| 11228 | OCSPRevocationStatus::UNKNOWN}, |
| 11229 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11230 | // 13 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11231 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11232 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11233 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 11234 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11235 | false, |
| 11236 | OCSPRevocationStatus::UNKNOWN}, |
| 11237 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11238 | // 14 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11239 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11240 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11241 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 11242 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11243 | false, |
| 11244 | OCSPRevocationStatus::UNKNOWN}, |
| 11245 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11246 | // 15 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11247 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11248 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11249 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11250 | OCSPVerifyResult::PROVIDED, |
| 11251 | true, |
| 11252 | OCSPRevocationStatus::GOOD}, |
| 11253 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11254 | // 16 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11255 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11256 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 11257 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11258 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11259 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11260 | OCSPVerifyResult::PROVIDED, |
| 11261 | true, |
| 11262 | OCSPRevocationStatus::GOOD}, |
| 11263 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11264 | // 17 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11265 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11266 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 11267 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11268 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11269 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11270 | OCSPVerifyResult::PROVIDED, |
| 11271 | true, |
| 11272 | OCSPRevocationStatus::GOOD}, |
| 11273 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11274 | // 18 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11275 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11276 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 11277 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11278 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11279 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11280 | OCSPVerifyResult::PROVIDED, |
| 11281 | true, |
| 11282 | OCSPRevocationStatus::GOOD}, |
| 11283 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11284 | // 19 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11285 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11286 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 11287 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11288 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 11289 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11290 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11291 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11292 | OCSPVerifyResult::INVALID_DATE, |
| 11293 | false, |
| 11294 | OCSPRevocationStatus::UNKNOWN}, |
| 11295 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11296 | // 20 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11297 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11298 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11299 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11300 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11301 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11302 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11303 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11304 | OCSPVerifyResult::PROVIDED, |
| 11305 | true, |
| 11306 | OCSPRevocationStatus::REVOKED}, |
| 11307 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11308 | // 21 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11309 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11310 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11311 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11312 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11313 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11314 | OCSPVerifyResult::PROVIDED, |
| 11315 | true, |
| 11316 | OCSPRevocationStatus::UNKNOWN}, |
| 11317 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11318 | // 22 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11319 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11320 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11321 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11322 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 11323 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11324 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11325 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11326 | OCSPVerifyResult::PROVIDED, |
| 11327 | true, |
| 11328 | OCSPRevocationStatus::UNKNOWN}, |
| 11329 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11330 | // 23 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11331 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 11332 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11333 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11334 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 11335 | false, |
| 11336 | OCSPRevocationStatus::UNKNOWN}, |
| 11337 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11338 | // 24 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11339 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 11340 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11341 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11342 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 11343 | false, |
| 11344 | OCSPRevocationStatus::UNKNOWN}, |
| 11345 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11346 | // These tests fail when using NSS for certificate verification, as NSS fails |
| 11347 | // and doesn't return the partial path. As a result the OCSP checks being done |
| 11348 | // at the CertVerifyProc layer cannot access the issuer certificate. |
| 11349 | #if !defined(USE_NSS_CERTS) |
| 11350 | // 25 |
| 11351 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11352 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11353 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11354 | OCSPVerifyResult::PROVIDED, |
| 11355 | true, |
| 11356 | OCSPRevocationStatus::REVOKED}, |
| 11357 | |
| 11358 | // 26 |
| 11359 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11360 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 11361 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11362 | OCSPVerifyResult::INVALID_DATE, |
| 11363 | false, |
| 11364 | OCSPRevocationStatus::UNKNOWN}, |
| 11365 | |
| 11366 | // 27 |
| 11367 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11368 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11369 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11370 | OCSPVerifyResult::INVALID_DATE, |
| 11371 | false, |
| 11372 | OCSPRevocationStatus::UNKNOWN}, |
| 11373 | #endif |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11374 | }; |
| 11375 | |
| 11376 | class HTTPSOCSPVerifyTest |
| 11377 | : public HTTPSOCSPTest, |
| 11378 | public testing::WithParamInterface<OCSPVerifyTestData> {}; |
| 11379 | |
| 11380 | TEST_P(HTTPSOCSPVerifyTest, VerifyResult) { |
| 11381 | SpawnedTestServer::SSLOptions ssl_options( |
| 11382 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11383 | OCSPVerifyTestData test = GetParam(); |
| 11384 | |
| 11385 | ssl_options.ocsp_responses = test.ocsp_responses; |
| 11386 | ssl_options.ocsp_produced = test.ocsp_produced; |
| 11387 | ssl_options.staple_ocsp_response = true; |
| 11388 | |
| 11389 | SSLInfo ssl_info; |
| 11390 | OCSPErrorTestDelegate delegate; |
| 11391 | ASSERT_NO_FATAL_FAILURE( |
| 11392 | DoConnectionWithDelegate(ssl_options, &delegate, &ssl_info)); |
| 11393 | |
| 11394 | // The SSLInfo must be extracted from |delegate| on error, due to how |
| 11395 | // URLRequest caches certificate errors. |
| 11396 | if (delegate.have_certificate_errors()) { |
| 11397 | ASSERT_TRUE(delegate.on_ssl_certificate_error_called()); |
| 11398 | ssl_info = delegate.ssl_info(); |
| 11399 | } |
| 11400 | |
| 11401 | EXPECT_EQ(test.response_status, ssl_info.ocsp_result.response_status); |
| 11402 | |
| 11403 | if (test.has_revocation_status) |
| 11404 | EXPECT_EQ(test.cert_status, ssl_info.ocsp_result.revocation_status); |
| 11405 | } |
| 11406 | |
Victor Costan | 8fb98f6f | 2019-02-01 17:08:29 | [diff] [blame] | 11407 | INSTANTIATE_TEST_SUITE_P(OCSPVerify, |
| 11408 | HTTPSOCSPVerifyTest, |
| 11409 | testing::ValuesIn(kOCSPVerifyData)); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11410 | |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11411 | class HTTPSAIATest : public HTTPSOCSPTest { |
| 11412 | public: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11413 | CertVerifier::Config GetCertVerifierConfig() override { |
| 11414 | CertVerifier::Config config; |
| 11415 | return config; |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11416 | } |
| 11417 | }; |
| 11418 | |
| 11419 | TEST_F(HTTPSAIATest, AIAFetching) { |
| 11420 | SpawnedTestServer::SSLOptions ssl_options( |
| 11421 | SpawnedTestServer::SSLOptions::CERT_AUTO_AIA_INTERMEDIATE); |
| 11422 | SpawnedTestServer test_server( |
| 11423 | SpawnedTestServer::TYPE_HTTPS, ssl_options, |
| 11424 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 11425 | ASSERT_TRUE(test_server.Start()); |
| 11426 | |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11427 | // Unmark the certificate's OID as EV, which will disable revocation |
| 11428 | // checking. |
| 11429 | ev_test_policy_.reset(); |
| 11430 | |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11431 | TestDelegate d; |
| 11432 | d.set_allow_certificate_errors(true); |
| 11433 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11434 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 11435 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11436 | |
| 11437 | r->Start(); |
| 11438 | EXPECT_TRUE(r->is_pending()); |
| 11439 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11440 | d.RunUntilComplete(); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11441 | |
| 11442 | EXPECT_EQ(1, d.response_started_count()); |
| 11443 | |
| 11444 | CertStatus cert_status = r->ssl_info().cert_status; |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 11445 | EXPECT_EQ(OK, d.request_status()); |
| 11446 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11447 | ASSERT_TRUE(r->ssl_info().cert); |
| 11448 | EXPECT_EQ(2u, r->ssl_info().cert->intermediate_buffers().size()); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11449 | ASSERT_TRUE(r->ssl_info().unverified_cert); |
Matt Mueller | a419327 | 2017-12-07 00:23:34 | [diff] [blame] | 11450 | EXPECT_EQ(0u, r->ssl_info().unverified_cert->intermediate_buffers().size()); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11451 | } |
| 11452 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11453 | class HTTPSHardFailTest : public HTTPSOCSPTest { |
| 11454 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11455 | CertVerifier::Config GetCertVerifierConfig() override { |
| 11456 | CertVerifier::Config config; |
| 11457 | config.require_rev_checking_local_anchors = true; |
| 11458 | return config; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11459 | } |
| 11460 | }; |
| 11461 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11462 | TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) { |
| 11463 | if (!SystemSupportsOCSP()) { |
| 11464 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11465 | return; |
| 11466 | } |
| 11467 | |
| 11468 | if (!SystemSupportsHardFailRevocationChecking()) { |
| 11469 | LOG(WARNING) << "Skipping test because system doesn't support hard fail " |
| 11470 | << "revocation checking"; |
| 11471 | return; |
| 11472 | } |
| 11473 | |
| 11474 | SpawnedTestServer::SSLOptions ssl_options( |
| 11475 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11476 | ssl_options.ocsp_status = |
| 11477 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11478 | |
| 11479 | CertStatus cert_status; |
| 11480 | DoConnection(ssl_options, &cert_status); |
| 11481 | |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11482 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 11483 | // TODO(crbug.com/649017): Should we consider invalid response as |
| 11484 | // affirmatively revoked? |
| 11485 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 11486 | cert_status & CERT_STATUS_UNABLE_TO_CHECK_REVOCATION); |
| 11487 | #else |
| 11488 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_REVOKED); |
| 11489 | #endif |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11490 | |
| 11491 | // Without a positive OCSP response, we shouldn't show the EV status. |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11492 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11493 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11494 | } |
| 11495 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11496 | class HTTPSEVCRLSetTest : public HTTPSOCSPTest { |
| 11497 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11498 | CertVerifier::Config GetCertVerifierConfig() override { |
| 11499 | CertVerifier::Config config; |
| 11500 | return config; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11501 | } |
| 11502 | }; |
| 11503 | |
| 11504 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) { |
| 11505 | if (!SystemSupportsOCSP()) { |
| 11506 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11507 | return; |
| 11508 | } |
| 11509 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11510 | SpawnedTestServer::SSLOptions ssl_options( |
| 11511 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11512 | ssl_options.ocsp_status = |
| 11513 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11514 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11515 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11516 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11517 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11518 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11519 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11520 | |
| 11521 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11522 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11523 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11524 | } |
| 11525 | |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11526 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) { |
| 11527 | if (!SystemSupportsOCSP()) { |
| 11528 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11529 | return; |
| 11530 | } |
| 11531 | |
| 11532 | SpawnedTestServer::SSLOptions ssl_options( |
| 11533 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11534 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11535 | |
| 11536 | CertStatus cert_status; |
| 11537 | DoConnection(ssl_options, &cert_status); |
| 11538 | |
mattm | 1a282f5 | 2016-11-10 21:49:42 | [diff] [blame] | 11539 | // Currently only works for Windows and OS X. When using NSS, it's not |
| 11540 | // possible to determine whether the check failed because of actual |
| 11541 | // revocation or because there was an OCSP failure. |
| 11542 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11543 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11544 | #else |
| 11545 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11546 | #endif |
| 11547 | |
| 11548 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11549 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11550 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 11551 | } |
| 11552 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11553 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) { |
| 11554 | if (!SystemSupportsOCSP()) { |
| 11555 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11556 | return; |
| 11557 | } |
| 11558 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11559 | SpawnedTestServer::SSLOptions ssl_options( |
| 11560 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11561 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11562 | |
| 11563 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11564 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11565 | |
| 11566 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11567 | |
| 11568 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11569 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11570 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11571 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11572 | } |
| 11573 | |
| 11574 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) { |
| 11575 | if (!SystemSupportsOCSP()) { |
| 11576 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11577 | return; |
| 11578 | } |
| 11579 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11580 | SpawnedTestServer::SSLOptions ssl_options( |
| 11581 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11582 | ssl_options.ocsp_status = |
| 11583 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11584 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11585 | cert_verifier_config.crl_set = CRLSet::ExpiredCRLSetForTesting(); |
| 11586 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11587 | |
| 11588 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11589 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11590 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11591 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11592 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11593 | |
| 11594 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11595 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11596 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11597 | } |
| 11598 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11599 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) { |
| 11600 | if (!SystemSupportsOCSP()) { |
| 11601 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11602 | return; |
| 11603 | } |
| 11604 | |
| 11605 | SpawnedTestServer::SSLOptions ssl_options( |
| 11606 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11607 | ssl_options.ocsp_status = |
| 11608 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11609 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11610 | cert_verifier_config.crl_set = |
| 11611 | CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "", "", {}); |
| 11612 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11613 | |
| 11614 | CertStatus cert_status; |
| 11615 | DoConnection(ssl_options, &cert_status); |
| 11616 | |
| 11617 | // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a |
| 11618 | // revocation check for EV. |
| 11619 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11620 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11621 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11622 | EXPECT_FALSE( |
| 11623 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 11624 | } |
| 11625 | |
| 11626 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) { |
| 11627 | if (!SystemSupportsOCSP()) { |
| 11628 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11629 | return; |
| 11630 | } |
| 11631 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11632 | SpawnedTestServer::SSLOptions ssl_options( |
| 11633 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11634 | ssl_options.ocsp_status = |
| 11635 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11636 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11637 | cert_verifier_config.crl_set = CRLSet::EmptyCRLSetForTesting(); |
| 11638 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11639 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11640 | CertStatus cert_status = 0; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11641 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11642 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11643 | // Even with a fresh CRLSet, we should still do online revocation checks when |
| 11644 | // the certificate chain isn't covered by the CRLSet, which it isn't in this |
| 11645 | // test. |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11646 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11647 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11648 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11649 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11650 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11651 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11652 | } |
| 11653 | |
| 11654 | class HTTPSCRLSetTest : public HTTPSOCSPTest { |
| 11655 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11656 | CertVerifier::Config GetCertVerifierConfig() override { |
| 11657 | CertVerifier::Config config; |
| 11658 | return config; |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11659 | } |
| 11660 | |
| 11661 | void SetUp() override { |
| 11662 | HTTPSOCSPTest::SetUp(); |
| 11663 | |
| 11664 | // Unmark the certificate's OID as EV, which should disable revocation |
| 11665 | // checking (as per the user preference). |
| 11666 | ev_test_policy_.reset(); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11667 | } |
| 11668 | }; |
| 11669 | |
| 11670 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11671 | SpawnedTestServer::SSLOptions ssl_options( |
| 11672 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11673 | ssl_options.ocsp_status = |
| 11674 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11675 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11676 | cert_verifier_config.crl_set = CRLSet::ExpiredCRLSetForTesting(); |
| 11677 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11678 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11679 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11680 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11681 | |
| 11682 | // If we're not trying EV verification then, even if the CRLSet has expired, |
| 11683 | // we don't fall back to online revocation checks. |
| 11684 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11685 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11686 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11687 | } |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11688 | |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11689 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSetAndRevoked) { |
| 11690 | // Test that when online revocation checking is disabled, and the leaf |
| 11691 | // certificate is not EV, that no revocation checking actually happens. |
| 11692 | if (!SystemSupportsOCSP()) { |
| 11693 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11694 | return; |
| 11695 | } |
| 11696 | |
| 11697 | SpawnedTestServer::SSLOptions ssl_options( |
| 11698 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11699 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11700 | |
| 11701 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11702 | cert_verifier_config.crl_set = CRLSet::ExpiredCRLSetForTesting(); |
| 11703 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11704 | |
| 11705 | CertStatus cert_status; |
| 11706 | DoConnection(ssl_options, &cert_status); |
| 11707 | |
| 11708 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11709 | |
| 11710 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11711 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11712 | } |
| 11713 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11714 | TEST_F(HTTPSCRLSetTest, CRLSetRevoked) { |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 11715 | #if defined(OS_ANDROID) |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11716 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 11717 | return; |
| 11718 | #endif |
| 11719 | |
| 11720 | SpawnedTestServer::SSLOptions ssl_options( |
| 11721 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11722 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11723 | ssl_options.cert_serial = 10; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11724 | |
| 11725 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11726 | cert_verifier_config.crl_set = |
| 11727 | CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "\x0a", "", {}); |
| 11728 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11729 | |
| 11730 | CertStatus cert_status = 0; |
| 11731 | DoConnection(ssl_options, &cert_status); |
| 11732 | |
| 11733 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 11734 | // reflected without online revocation checking. |
| 11735 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11736 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11737 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11738 | } |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 11739 | |
| 11740 | TEST_F(HTTPSCRLSetTest, CRLSetRevokedBySubject) { |
| 11741 | #if defined(OS_ANDROID) |
| 11742 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 11743 | return; |
| 11744 | #endif |
| 11745 | |
| 11746 | SpawnedTestServer::SSLOptions ssl_options( |
| 11747 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11748 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11749 | static const char kCommonName[] = "Test CN"; |
| 11750 | ssl_options.cert_common_name = kCommonName; |
| 11751 | |
| 11752 | { |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11753 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11754 | cert_verifier_config.crl_set = |
| 11755 | CRLSet::ForTesting(false, nullptr, "", kCommonName, {}); |
| 11756 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 11757 | |
| 11758 | CertStatus cert_status = 0; |
| 11759 | DoConnection(ssl_options, &cert_status); |
| 11760 | |
| 11761 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 11762 | // reflected without online revocation checking. |
| 11763 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11764 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11765 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 11766 | } |
| 11767 | |
| 11768 | const uint8_t kTestServerSPKISHA256[32] = { |
| 11769 | 0xb3, 0x91, 0xac, 0x73, 0x32, 0x54, 0x7f, 0x7b, 0x8a, 0x62, 0x77, |
| 11770 | 0x73, 0x1d, 0x45, 0x7b, 0x23, 0x46, 0x69, 0xef, 0x6f, 0x05, 0x3d, |
| 11771 | 0x07, 0x22, 0x15, 0x18, 0xd6, 0x10, 0x8b, 0xa1, 0x49, 0x33, |
| 11772 | }; |
| 11773 | const std::string spki_hash( |
| 11774 | reinterpret_cast<const char*>(kTestServerSPKISHA256), |
| 11775 | sizeof(kTestServerSPKISHA256)); |
| 11776 | |
| 11777 | { |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11778 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11779 | cert_verifier_config.crl_set = |
| 11780 | CRLSet::ForTesting(false, nullptr, "", kCommonName, {spki_hash}); |
| 11781 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 11782 | |
| 11783 | CertStatus cert_status = 0; |
| 11784 | DoConnection(ssl_options, &cert_status); |
| 11785 | |
| 11786 | // When the correct SPKI hash is specified, the connection should succeed |
| 11787 | // even though the subject is listed in the CRLSet. |
| 11788 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11789 | } |
| 11790 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11791 | #endif // !defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11792 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 11793 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 11794 | !defined(OS_FUCHSIA) |
| 11795 | // FTP uses a second TCP connection with the port number allocated dynamically |
| 11796 | // on the server side, so it would be hard to make RemoteTestServer proxy FTP |
| 11797 | // connections reliably. FTP tests are disabled on platforms that use |
| 11798 | // RemoteTestServer. See http://crbug.com/495220 |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 11799 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11800 | public: |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 11801 | URLRequestTestFTP() |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 11802 | : ftp_test_server_(SpawnedTestServer::TYPE_FTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11803 | base::FilePath(kTestFilePath)) { |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11804 | // Can't use |default_context_|'s HostResolver to set up the |
| 11805 | // FTPTransactionFactory because it hasn't been created yet. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11806 | default_context().set_host_resolver(&host_resolver_); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11807 | } |
| 11808 | |
| 11809 | // URLRequestTest interface: |
| 11810 | void SetUpFactory() override { |
| 11811 | // Add FTP support to the default URLRequestContext. |
| 11812 | job_factory_impl_->SetProtocolHandler( |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 11813 | "ftp", FtpProtocolHandler::Create(&host_resolver_)); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11814 | } |
| 11815 | |
| 11816 | std::string GetTestFileContents() { |
| 11817 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 11818 | EXPECT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &path)); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11819 | path = path.Append(kTestFilePath); |
| 11820 | path = path.AppendASCII(kFtpTestFile); |
| 11821 | std::string contents; |
| 11822 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
| 11823 | return contents; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11824 | } |
| 11825 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 11826 | protected: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11827 | MockHostResolver host_resolver_; |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11828 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11829 | SpawnedTestServer ftp_test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 11830 | }; |
| 11831 | |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11832 | // Make sure an FTP request using an unsafe ports fails. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 11833 | TEST_F(URLRequestTestFTP, UnsafePort) { |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11834 | GURL url("ftp://127.0.0.1:7"); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11835 | |
| 11836 | TestDelegate d; |
| 11837 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11838 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11839 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11840 | r->Start(); |
| 11841 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11842 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11843 | d.RunUntilComplete(); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11844 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11845 | EXPECT_FALSE(r->is_pending()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 11846 | EXPECT_EQ(ERR_UNSAFE_PORT, d.request_status()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11847 | } |
| 11848 | } |
| 11849 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11850 | TEST_F(URLRequestTestFTP, FTPDirectoryListing) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11851 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11852 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11853 | TestDelegate d; |
| 11854 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11855 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11856 | ftp_test_server_.GetURL("/"), DEFAULT_PRIORITY, &d, |
| 11857 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11858 | r->Start(); |
| 11859 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11860 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11861 | d.RunUntilComplete(); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11862 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11863 | EXPECT_FALSE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11864 | EXPECT_EQ(1, d.response_started_count()); |
| 11865 | EXPECT_FALSE(d.received_data_before_response()); |
| 11866 | EXPECT_LT(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11867 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 11868 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11869 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 11870 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11871 | } |
| 11872 | } |
| 11873 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11874 | TEST_F(URLRequestTestFTP, FTPGetTestAnonymous) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11875 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11876 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11877 | TestDelegate d; |
| 11878 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11879 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11880 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, &d, |
| 11881 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11882 | r->Start(); |
| 11883 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11884 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11885 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11886 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11887 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11888 | EXPECT_EQ(1, d.response_started_count()); |
| 11889 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11890 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11891 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 11892 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11893 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 11894 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11895 | } |
| 11896 | } |
| 11897 | |
Mike West | 80462a1 | 2018-11-27 16:05:06 | [diff] [blame] | 11898 | TEST_F(URLRequestTestFTP, FTPMimeType) { |
| 11899 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 11900 | |
| 11901 | struct { |
| 11902 | const char* path; |
| 11903 | const char* mime; |
| 11904 | } test_cases[] = { |
| 11905 | {"/", "text/vnd.chromium.ftp-dir"}, |
| 11906 | {kFtpTestFile, "application/octet-stream"}, |
| 11907 | }; |
| 11908 | |
| 11909 | for (const auto test : test_cases) { |
| 11910 | TestDelegate d; |
| 11911 | |
| 11912 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 11913 | ftp_test_server_.GetURL(test.path), DEFAULT_PRIORITY, &d, |
| 11914 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11915 | r->Start(); |
| 11916 | EXPECT_TRUE(r->is_pending()); |
| 11917 | |
| 11918 | d.RunUntilComplete(); |
| 11919 | |
| 11920 | std::string mime; |
| 11921 | r->GetMimeType(&mime); |
| 11922 | EXPECT_EQ(test.mime, mime); |
| 11923 | } |
| 11924 | } |
| 11925 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11926 | TEST_F(URLRequestTestFTP, FTPGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11927 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11928 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11929 | TestDelegate d; |
| 11930 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11931 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11932 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11933 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11934 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11935 | r->Start(); |
| 11936 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11937 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11938 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11939 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11940 | EXPECT_FALSE(r->is_pending()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11941 | EXPECT_EQ(1, d.response_started_count()); |
| 11942 | EXPECT_FALSE(d.received_data_before_response()); |
| 11943 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11944 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 11945 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11946 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 11947 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 11948 | |
| 11949 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11950 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 11951 | TestLoadTimingNoHttpResponse(load_timing_info); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11952 | } |
| 11953 | } |
| 11954 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11955 | TEST_F(URLRequestTestFTP, FTPCheckWrongPassword) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11956 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11957 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11958 | TestDelegate d; |
| 11959 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11960 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11961 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11962 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11963 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11964 | r->Start(); |
| 11965 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11966 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11967 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11968 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11969 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11970 | EXPECT_EQ(1, d.response_started_count()); |
| 11971 | EXPECT_FALSE(d.received_data_before_response()); |
| 11972 | EXPECT_EQ(d.bytes_received(), 0); |
| 11973 | } |
| 11974 | } |
| 11975 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11976 | TEST_F(URLRequestTestFTP, FTPCheckWrongPasswordRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11977 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11978 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11979 | TestDelegate d; |
| 11980 | // Set correct login credentials. The delegate will be asked for them when |
| 11981 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 11982 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11983 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11984 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11985 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11986 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11987 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11988 | r->Start(); |
| 11989 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11990 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11991 | d.RunUntilComplete(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11992 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11993 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11994 | EXPECT_EQ(1, d.response_started_count()); |
| 11995 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11996 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11997 | } |
| 11998 | } |
| 11999 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12000 | TEST_F(URLRequestTestFTP, FTPCheckWrongUser) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12001 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12002 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12003 | TestDelegate d; |
| 12004 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12005 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12006 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 12007 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12008 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12009 | r->Start(); |
| 12010 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12011 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12012 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12013 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12014 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12015 | EXPECT_EQ(1, d.response_started_count()); |
| 12016 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12017 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12018 | } |
| 12019 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12020 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12021 | TEST_F(URLRequestTestFTP, FTPCheckWrongUserRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12022 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12023 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12024 | TestDelegate d; |
| 12025 | // Set correct login credentials. The delegate will be asked for them when |
| 12026 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 12027 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12028 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12029 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12030 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 12031 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12032 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12033 | r->Start(); |
| 12034 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12035 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12036 | d.RunUntilComplete(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12037 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12038 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12039 | EXPECT_EQ(1, d.response_started_count()); |
| 12040 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12041 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12042 | } |
| 12043 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12044 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12045 | TEST_F(URLRequestTestFTP, FTPCacheURLCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12046 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12047 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12048 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12049 | { |
| 12050 | // Pass correct login identity in the URL. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12051 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12052 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12053 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12054 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12055 | r->Start(); |
| 12056 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12057 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12058 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12059 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12060 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12061 | EXPECT_EQ(1, d->response_started_count()); |
| 12062 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12063 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12064 | } |
| 12065 | |
| 12066 | d.reset(new TestDelegate); |
| 12067 | { |
| 12068 | // This request should use cached identity from previous request. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12069 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12070 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 12071 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12072 | r->Start(); |
| 12073 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12074 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12075 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12076 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12077 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12078 | EXPECT_EQ(1, d->response_started_count()); |
| 12079 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12080 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12081 | } |
| 12082 | } |
| 12083 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12084 | TEST_F(URLRequestTestFTP, FTPCacheLoginBoxCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12085 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12086 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12087 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12088 | // Set correct login credentials. The delegate will be asked for them when |
| 12089 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 12090 | d->set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12091 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12092 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12093 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12094 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12095 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12096 | r->Start(); |
| 12097 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12098 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12099 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12100 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12101 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12102 | EXPECT_EQ(1, d->response_started_count()); |
| 12103 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12104 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12105 | } |
| 12106 | |
| 12107 | // Use a new delegate without explicit credentials. The cached ones should be |
| 12108 | // used. |
| 12109 | d.reset(new TestDelegate); |
| 12110 | { |
| 12111 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 12112 | // ones. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12113 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12114 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 12115 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12116 | r->Start(); |
| 12117 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12118 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12119 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12120 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12121 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12122 | EXPECT_EQ(1, d->response_started_count()); |
| 12123 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12124 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12125 | } |
| 12126 | } |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 12127 | |
| 12128 | TEST_F(URLRequestTestFTP, RawBodyBytes) { |
| 12129 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 12130 | |
| 12131 | TestDelegate d; |
| 12132 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12133 | ftp_test_server_.GetURL("simple.html"), DEFAULT_PRIORITY, &d, |
| 12134 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 12135 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12136 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 12137 | |
| 12138 | EXPECT_EQ(6, req->GetRawBodyBytes()); |
| 12139 | } |
| 12140 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 12141 | #endif // !BUILDFLAG(DISABLE_FTP_SUPPORT) |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 12142 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12143 | TEST_F(URLRequestTest, NetworkAccessedClearOnDataRequest) { |
| 12144 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12145 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12146 | GURL("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12147 | |
| 12148 | EXPECT_FALSE(req->response_info().network_accessed); |
| 12149 | |
| 12150 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12151 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12152 | |
| 12153 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 12154 | EXPECT_FALSE(req->response_info().network_accessed); |
| 12155 | } |
| 12156 | |
| 12157 | TEST_F(URLRequestTest, NetworkAccessedSetOnHostResolutionFailure) { |
| 12158 | MockHostResolver host_resolver; |
| 12159 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 12160 | TestURLRequestContext context(true); |
| 12161 | context.set_network_delegate(&network_delegate); |
| 12162 | context.set_host_resolver(&host_resolver); |
| 12163 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 12164 | context.Init(); |
| 12165 | |
| 12166 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12167 | std::unique_ptr<URLRequest> req( |
| 12168 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 12169 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12170 | |
| 12171 | EXPECT_FALSE(req->response_info().network_accessed); |
| 12172 | |
| 12173 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12174 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12175 | EXPECT_TRUE(req->response_info().network_accessed); |
| 12176 | } |
| 12177 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 12178 | // Test that URLRequest is canceled correctly. |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12179 | // See http://crbug.com/508900 |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 12180 | TEST_F(URLRequestTest, URLRequestRedirectJobCancelRequest) { |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12181 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12182 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12183 | GURL("http://not-a-real-domain/"), DEFAULT_PRIORITY, &d, |
| 12184 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12185 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12186 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12187 | req.get(), &default_network_delegate_, |
| 12188 | GURL("http://this-should-never-be-navigated-to/"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 12189 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Jumbo shrimp")); |
| 12190 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12191 | |
| 12192 | req->Start(); |
| 12193 | req->Cancel(); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12194 | base::RunLoop().RunUntilIdle(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 12195 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12196 | EXPECT_EQ(0, d.received_redirect_count()); |
| 12197 | } |
| 12198 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12199 | TEST_F(URLRequestTestHTTP, HeadersCallbacks) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12200 | ASSERT_TRUE(http_test_server()->Start()); |
| 12201 | TestURLRequestContext context; |
| 12202 | GURL url(http_test_server()->GetURL("/cachetime")); |
| 12203 | TestDelegate delegate; |
| 12204 | HttpRequestHeaders extra_headers; |
| 12205 | extra_headers.SetHeader("X-Foo", "bar"); |
| 12206 | |
| 12207 | { |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12208 | HttpRawRequestHeaders raw_req_headers; |
| 12209 | scoped_refptr<const HttpResponseHeaders> raw_resp_headers; |
| 12210 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12211 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12212 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12213 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12214 | r->SetRequestHeadersCallback(base::Bind( |
| 12215 | &HttpRawRequestHeaders::Assign, base::Unretained(&raw_req_headers))); |
| 12216 | r->SetResponseHeadersCallback(base::Bind( |
| 12217 | [](scoped_refptr<const HttpResponseHeaders>* left, |
| 12218 | scoped_refptr<const HttpResponseHeaders> right) { *left = right; }, |
| 12219 | base::Unretained(&raw_resp_headers))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12220 | r->Start(); |
| 12221 | while (!delegate.response_started_count()) |
| 12222 | base::RunLoop().RunUntilIdle(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12223 | EXPECT_FALSE(raw_req_headers.headers().empty()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12224 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12225 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12226 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12227 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12228 | EXPECT_EQ("gzip, deflate", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12229 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Connection", &value)); |
| 12230 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Host", &value)); |
| 12231 | EXPECT_EQ("GET /cachetime HTTP/1.1\r\n", raw_req_headers.request_line()); |
| 12232 | EXPECT_EQ(raw_resp_headers.get(), r->response_headers()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12233 | } |
| 12234 | { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12235 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12236 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12237 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12238 | r->SetRequestHeadersCallback(base::Bind([](HttpRawRequestHeaders) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12239 | FAIL() << "Callback should not be called unless request is sent"; |
| 12240 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12241 | r->SetResponseHeadersCallback( |
| 12242 | base::Bind([](scoped_refptr<const HttpResponseHeaders>) { |
| 12243 | FAIL() << "Callback should not be called unless request is sent"; |
| 12244 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12245 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12246 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12247 | EXPECT_TRUE(r->was_cached()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12248 | } |
| 12249 | } |
| 12250 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12251 | TEST_F(URLRequestTestHTTP, HeadersCallbacksWithRedirect) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12252 | ASSERT_TRUE(http_test_server()->Start()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12253 | HttpRawRequestHeaders raw_req_headers; |
| 12254 | scoped_refptr<const HttpResponseHeaders> raw_resp_headers; |
| 12255 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12256 | TestURLRequestContext context; |
| 12257 | TestDelegate delegate; |
| 12258 | HttpRequestHeaders extra_headers; |
| 12259 | extra_headers.SetHeader("X-Foo", "bar"); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12260 | GURL url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12261 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12262 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12263 | r->SetExtraRequestHeaders(extra_headers); |
| 12264 | r->SetRequestHeadersCallback(base::Bind(&HttpRawRequestHeaders::Assign, |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12265 | base::Unretained(&raw_req_headers))); |
| 12266 | r->SetResponseHeadersCallback(base::Bind( |
| 12267 | [](scoped_refptr<const HttpResponseHeaders>* left, |
| 12268 | scoped_refptr<const HttpResponseHeaders> right) { *left = right; }, |
| 12269 | base::Unretained(&raw_resp_headers))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12270 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 12271 | delegate.RunUntilRedirect(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12272 | |
| 12273 | ASSERT_EQ(1, delegate.received_redirect_count()); |
| 12274 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12275 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12276 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12277 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12278 | EXPECT_EQ("gzip, deflate", value); |
| 12279 | EXPECT_EQ(1, delegate.received_redirect_count()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12280 | EXPECT_EQ("GET /redirect-test.html HTTP/1.1\r\n", |
| 12281 | raw_req_headers.request_line()); |
| 12282 | EXPECT_TRUE(raw_resp_headers->HasHeader("Location")); |
| 12283 | EXPECT_EQ(302, raw_resp_headers->response_code()); |
| 12284 | EXPECT_EQ("Redirect", raw_resp_headers->GetStatusText()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12285 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12286 | raw_req_headers = HttpRawRequestHeaders(); |
| 12287 | raw_resp_headers = nullptr; |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 12288 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 12289 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12290 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12291 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12292 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12293 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12294 | EXPECT_EQ("gzip, deflate", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12295 | EXPECT_EQ("GET /with-headers.html HTTP/1.1\r\n", |
| 12296 | raw_req_headers.request_line()); |
| 12297 | EXPECT_EQ(r->response_headers(), raw_resp_headers.get()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12298 | } |
| 12299 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12300 | TEST_F(URLRequestTest, HeadersCallbacksConnectFailed) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12301 | TestDelegate request_delegate; |
| 12302 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12303 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12304 | GURL("http://127.0.0.1:9/"), DEFAULT_PRIORITY, &request_delegate, |
| 12305 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12306 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
| 12307 | FAIL() << "Callback should not be called unless request is sent"; |
| 12308 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12309 | r->SetResponseHeadersCallback( |
| 12310 | base::Bind([](scoped_refptr<const net::HttpResponseHeaders>) { |
| 12311 | FAIL() << "Callback should not be called unless request is sent"; |
| 12312 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12313 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12314 | request_delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12315 | EXPECT_FALSE(r->is_pending()); |
| 12316 | } |
| 12317 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12318 | TEST_F(URLRequestTestHTTP, HeadersCallbacksAuthRetry) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12319 | ASSERT_TRUE(http_test_server()->Start()); |
| 12320 | GURL url(http_test_server()->GetURL("/auth-basic")); |
| 12321 | |
| 12322 | TestURLRequestContext context; |
| 12323 | TestDelegate delegate; |
| 12324 | |
| 12325 | delegate.set_credentials(AuthCredentials(kUser, kSecret)); |
| 12326 | HttpRequestHeaders extra_headers; |
| 12327 | extra_headers.SetHeader("X-Foo", "bar"); |
| 12328 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12329 | using ReqHeadersVector = std::vector<std::unique_ptr<HttpRawRequestHeaders>>; |
| 12330 | ReqHeadersVector raw_req_headers; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12331 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12332 | using RespHeadersVector = |
| 12333 | std::vector<scoped_refptr<const HttpResponseHeaders>>; |
| 12334 | RespHeadersVector raw_resp_headers; |
| 12335 | |
| 12336 | auto req_headers_callback = base::Bind( |
| 12337 | [](ReqHeadersVector* vec, HttpRawRequestHeaders headers) { |
| 12338 | vec->emplace_back(new HttpRawRequestHeaders(std::move(headers))); |
| 12339 | }, |
| 12340 | &raw_req_headers); |
| 12341 | auto resp_headers_callback = base::Bind( |
| 12342 | [](RespHeadersVector* vec, |
| 12343 | scoped_refptr<const HttpResponseHeaders> headers) { |
| 12344 | vec->push_back(headers); |
| 12345 | }, |
| 12346 | &raw_resp_headers); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12347 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12348 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12349 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12350 | r->SetRequestHeadersCallback(req_headers_callback); |
| 12351 | r->SetResponseHeadersCallback(resp_headers_callback); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12352 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12353 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12354 | EXPECT_FALSE(r->is_pending()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12355 | ASSERT_EQ(raw_req_headers.size(), 2u); |
| 12356 | ASSERT_EQ(raw_resp_headers.size(), 2u); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12357 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12358 | EXPECT_FALSE(raw_req_headers[0]->FindHeaderForTest("Authorization", &value)); |
| 12359 | EXPECT_TRUE(raw_req_headers[0]->FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12360 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12361 | EXPECT_TRUE(raw_req_headers[1]->FindHeaderForTest("Authorization", &value)); |
| 12362 | EXPECT_TRUE(raw_req_headers[1]->FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12363 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12364 | EXPECT_EQ(raw_resp_headers[1], r->response_headers()); |
| 12365 | EXPECT_NE(raw_resp_headers[0], raw_resp_headers[1]); |
| 12366 | EXPECT_EQ(401, raw_resp_headers[0]->response_code()); |
| 12367 | EXPECT_EQ("Unauthorized", raw_resp_headers[0]->GetStatusText()); |
| 12368 | |
| 12369 | std::unique_ptr<URLRequest> r2(context.CreateRequest( |
| 12370 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12371 | r2->SetExtraRequestHeaders(extra_headers); |
| 12372 | r2->SetRequestHeadersCallback(req_headers_callback); |
| 12373 | r2->SetResponseHeadersCallback(resp_headers_callback); |
| 12374 | r2->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 12375 | r2->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12376 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12377 | EXPECT_FALSE(r2->is_pending()); |
| 12378 | ASSERT_EQ(raw_req_headers.size(), 3u); |
| 12379 | ASSERT_EQ(raw_resp_headers.size(), 3u); |
| 12380 | EXPECT_TRUE(raw_req_headers[2]->FindHeaderForTest("If-None-Match", &value)); |
| 12381 | EXPECT_NE(raw_resp_headers[2].get(), r2->response_headers()); |
| 12382 | EXPECT_EQ(304, raw_resp_headers[2]->response_code()); |
| 12383 | EXPECT_EQ("Not Modified", raw_resp_headers[2]->GetStatusText()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12384 | } |
| 12385 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12386 | TEST_F(URLRequestTest, HeadersCallbacksNonHTTP) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12387 | GURL data_url("data:text/html,<html><body>Hello!</body></html>"); |
| 12388 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12389 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12390 | data_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12391 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12392 | FAIL() << "Callback should not be called for non-HTTP schemes"; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12393 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12394 | r->SetResponseHeadersCallback( |
| 12395 | base::Bind([](scoped_refptr<const net::HttpResponseHeaders>) { |
| 12396 | FAIL() << "Callback should not be called for non-HTTP schemes"; |
| 12397 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12398 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12399 | d.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12400 | EXPECT_FALSE(r->is_pending()); |
| 12401 | } |
| 12402 | |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12403 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSet) { |
| 12404 | TestDelegate d; |
| 12405 | BlockingNetworkDelegate network_delegate( |
| 12406 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 12407 | const GURL kOriginalUrl("https://original.test"); |
| 12408 | const GURL kRedirectUrl("http://redirect.test"); |
| 12409 | network_delegate.set_redirect_url(kRedirectUrl); |
| 12410 | TestURLRequestContext context(true /* delay_initialization */); |
| 12411 | context.set_network_delegate(&network_delegate); |
| 12412 | context.Init(); |
| 12413 | |
| 12414 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12415 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12416 | r->set_upgrade_if_insecure(true); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12417 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 12418 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12419 | GURL::Replacements replacements; |
| 12420 | // Check that the redirect URL was upgraded to HTTPS since upgrade_if_insecure |
| 12421 | // was set. |
| 12422 | replacements.SetSchemeStr("https"); |
| 12423 | EXPECT_EQ(kRedirectUrl.ReplaceComponents(replacements), |
| 12424 | d.redirect_info().new_url); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 12425 | EXPECT_TRUE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12426 | } |
| 12427 | |
| 12428 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSetExplicitPort80) { |
| 12429 | TestDelegate d; |
| 12430 | BlockingNetworkDelegate network_delegate( |
| 12431 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 12432 | const GURL kOriginalUrl("https://original.test"); |
| 12433 | const GURL kRedirectUrl("http://redirect.test:80"); |
| 12434 | network_delegate.set_redirect_url(kRedirectUrl); |
| 12435 | TestURLRequestContext context(true /* delay_initialization */); |
| 12436 | context.set_network_delegate(&network_delegate); |
| 12437 | context.Init(); |
| 12438 | |
| 12439 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12440 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12441 | r->set_upgrade_if_insecure(true); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12442 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 12443 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12444 | GURL::Replacements replacements; |
| 12445 | // The URL host should have not been changed. |
| 12446 | EXPECT_EQ(d.redirect_info().new_url.host(), kRedirectUrl.host()); |
| 12447 | // The scheme should now be https, and the effective port should now be 443. |
| 12448 | EXPECT_TRUE(d.redirect_info().new_url.SchemeIs("https")); |
| 12449 | EXPECT_EQ(d.redirect_info().new_url.EffectiveIntPort(), 443); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 12450 | EXPECT_TRUE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12451 | } |
| 12452 | |
| 12453 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSetNonStandardPort) { |
| 12454 | TestDelegate d; |
| 12455 | BlockingNetworkDelegate network_delegate( |
| 12456 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 12457 | const GURL kOriginalUrl("https://original.test"); |
| 12458 | const GURL kRedirectUrl("http://redirect.test:1234"); |
| 12459 | network_delegate.set_redirect_url(kRedirectUrl); |
| 12460 | TestURLRequestContext context(true /* delay_initialization */); |
| 12461 | context.set_network_delegate(&network_delegate); |
| 12462 | context.Init(); |
| 12463 | |
| 12464 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12465 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12466 | r->set_upgrade_if_insecure(true); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12467 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 12468 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12469 | GURL::Replacements replacements; |
| 12470 | // Check that the redirect URL was upgraded to HTTPS since upgrade_if_insecure |
| 12471 | // was set, nonstandard port should not have been modified. |
| 12472 | replacements.SetSchemeStr("https"); |
| 12473 | EXPECT_EQ(kRedirectUrl.ReplaceComponents(replacements), |
| 12474 | d.redirect_info().new_url); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 12475 | EXPECT_TRUE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12476 | } |
| 12477 | |
| 12478 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagNotSet) { |
| 12479 | TestDelegate d; |
| 12480 | BlockingNetworkDelegate network_delegate( |
| 12481 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 12482 | const GURL kOriginalUrl("https://original.test"); |
| 12483 | const GURL kRedirectUrl("http://redirect.test"); |
| 12484 | network_delegate.set_redirect_url(kRedirectUrl); |
| 12485 | TestURLRequestContext context(true /* delay_initialization */); |
| 12486 | context.set_network_delegate(&network_delegate); |
| 12487 | context.Init(); |
| 12488 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12489 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12490 | r->set_upgrade_if_insecure(false); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12491 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 12492 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12493 | // The redirect URL should not be changed if the upgrade_if_insecure flag is |
| 12494 | // not set. |
| 12495 | EXPECT_EQ(kRedirectUrl, d.redirect_info().new_url); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 12496 | EXPECT_FALSE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12497 | } |
| 12498 | |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12499 | // Test that URLRequests get properly tagged. |
| 12500 | #if defined(OS_ANDROID) |
| 12501 | TEST_F(URLRequestTestHTTP, TestTagging) { |
Peter Collingbourne | 8ccab72 | 2019-02-12 18:10:48 | [diff] [blame] | 12502 | if (!CanGetTaggedBytes()) { |
| 12503 | DVLOG(0) << "Skipping test - GetTaggedBytes unsupported."; |
| 12504 | return; |
| 12505 | } |
| 12506 | |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12507 | ASSERT_TRUE(http_test_server()->Start()); |
| 12508 | |
| 12509 | // The tag under which the system reports untagged traffic. |
| 12510 | static const int32_t UNTAGGED_TAG = 0; |
| 12511 | |
| 12512 | uint64_t old_traffic = GetTaggedBytes(UNTAGGED_TAG); |
| 12513 | |
| 12514 | // Untagged traffic should be tagged with tag UNTAGGED_TAG. |
| 12515 | TestDelegate delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12516 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12517 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &delegate, |
| 12518 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12519 | EXPECT_EQ(SocketTag(), req->socket_tag()); |
| 12520 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12521 | delegate.RunUntilComplete(); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12522 | |
| 12523 | EXPECT_GT(GetTaggedBytes(UNTAGGED_TAG), old_traffic); |
| 12524 | |
| 12525 | int32_t tag_val1 = 0x12345678; |
| 12526 | SocketTag tag1(SocketTag::UNSET_UID, tag_val1); |
| 12527 | old_traffic = GetTaggedBytes(tag_val1); |
| 12528 | |
| 12529 | // Test specific tag value. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12530 | req = default_context().CreateRequest(http_test_server()->GetURL("/"), |
| 12531 | DEFAULT_PRIORITY, &delegate, |
| 12532 | TRAFFIC_ANNOTATION_FOR_TESTS); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12533 | req->set_socket_tag(tag1); |
| 12534 | EXPECT_EQ(tag1, req->socket_tag()); |
| 12535 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12536 | delegate.RunUntilComplete(); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12537 | |
| 12538 | EXPECT_GT(GetTaggedBytes(tag_val1), old_traffic); |
| 12539 | } |
| 12540 | #endif |
| 12541 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 12542 | } // namespace net |