[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
Douglas Creager | 4d1ef84 | 2018-07-16 23:42:25 | [diff] [blame] | 5 | #include <algorithm> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 6 | #include <utility> |
| 7 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 8 | // This must be before Windows headers |
Sebastien Marchand | 17fa278 | 2019-01-25 19:28:10 | [diff] [blame] | 9 | #include "base/bind_helpers.h" |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10 | #include "build/build_config.h" |
| 11 | |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 12 | #if defined(OS_WIN) |
robliao | eb9bfd64 | 2017-05-18 17:35:16 | [diff] [blame] | 13 | #include <objbase.h> |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 14 | #include <shlobj.h> |
Fan Yang | 5a88d5d | 2017-10-24 01:14:29 | [diff] [blame] | 15 | #include <windows.h> |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 16 | #include <wrl/client.h> |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 17 | #endif |
| 18 | |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 21 | #include <algorithm> |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 22 | #include <limits> |
Devlin Cronin | e4bcb40e | 2018-06-05 18:02:47 | [diff] [blame] | 23 | #include <memory> |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 24 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 25 | #include "base/base64url.h" |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 26 | #include "base/bind.h" |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 27 | #include "base/compiler_specific.h" |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 28 | #include "base/files/file_path.h" |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame] | 29 | #include "base/files/file_util.h" |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 30 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 31 | #include "base/format_macros.h" |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 32 | #include "base/json/json_reader.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 33 | #include "base/location.h" |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 34 | #include "base/memory/weak_ptr.h" |
Alexander Timin | 4f9c35c | 2018-11-01 20:15:20 | [diff] [blame] | 35 | #include "base/message_loop/message_loop.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 36 | #include "base/path_service.h" |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 37 | #include "base/run_loop.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 38 | #include "base/single_thread_task_runner.h" |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 39 | #include "base/stl_util.h" |
[email protected] | 4dc3ad4f | 2013-06-11 07:15:50 | [diff] [blame] | 40 | #include "base/strings/string_number_conversions.h" |
[email protected] | d069c11a | 2013-04-13 00:01:55 | [diff] [blame] | 41 | #include "base/strings/string_piece.h" |
[email protected] | d778e042 | 2013-03-06 18:10:22 | [diff] [blame] | 42 | #include "base/strings/string_split.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 43 | #include "base/strings/string_util.h" |
| 44 | #include "base/strings/stringprintf.h" |
[email protected] | 750b2f3c | 2013-06-07 18:41:05 | [diff] [blame] | 45 | #include "base/strings/utf_string_conversions.h" |
Devlin Cronin | e4bcb40e | 2018-06-05 18:02:47 | [diff] [blame] | 46 | #include "base/test/metrics/histogram_tester.h" |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 47 | #include "base/test/scoped_feature_list.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 48 | #include "base/threading/thread_task_runner_handle.h" |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 49 | #include "base/values.h" |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 50 | #include "build/buildflag.h" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 51 | #include "net/base/chunked_upload_data_stream.h" |
tfarina | 43a416b | 2016-01-06 21:48:07 | [diff] [blame] | 52 | #include "net/base/directory_listing.h" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 53 | #include "net/base/elements_upload_data_stream.h" |
David Benjamin | a7fde61 | 2019-03-15 14:20:58 | [diff] [blame] | 54 | #include "net/base/escape.h" |
Mike West | fbd4411 | 2019-05-08 18:23:37 | [diff] [blame] | 55 | #include "net/base/features.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" |
Matt Mueller | a4aaa9d | 2019-04-24 21:09:14 | [diff] [blame] | 69 | #include "net/cert/cert_net_fetcher.h" |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 70 | #include "net/cert/crl_set.h" |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 71 | #include "net/cert/ct_policy_enforcer.h" |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 72 | #include "net/cert/ct_policy_status.h" |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 73 | #include "net/cert/do_nothing_ct_verifier.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 74 | #include "net/cert/ev_root_ca_metadata.h" |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 75 | #include "net/cert/mock_cert_verifier.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 76 | #include "net/cert/multi_log_ct_verifier.h" |
eranm | dcec963 | 2016-10-10 14:16:10 | [diff] [blame] | 77 | #include "net/cert/signed_certificate_timestamp_and_status.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 78 | #include "net/cert/test_root_certs.h" |
Matt Mueller | a4aaa9d | 2019-04-24 21:09:14 | [diff] [blame] | 79 | #include "net/cert_net/cert_net_fetcher_impl.h" |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 80 | #include "net/cookies/canonical_cookie_test_helpers.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 81 | #include "net/cookies/cookie_monster.h" |
| 82 | #include "net/cookies/cookie_store_test_helpers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 83 | #include "net/disk_cache/disk_cache.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 84 | #include "net/dns/mock_host_resolver.h" |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 85 | #include "net/http/http_byte_range.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 86 | #include "net/http/http_cache.h" |
| 87 | #include "net/http/http_network_layer.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 88 | #include "net/http/http_network_session.h" |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 89 | #include "net/http/http_request_headers.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 90 | #include "net/http/http_response_headers.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 91 | #include "net/http/http_server_properties_impl.h" |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 92 | #include "net/http/http_util.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 93 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 94 | #include "net/log/net_log_source.h" |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 95 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 96 | #include "net/log/test_net_log_entry.h" |
| 97 | #include "net/log/test_net_log_util.h" |
Scott Violet | 0caaaf43 | 2018-03-24 00:43:59 | [diff] [blame] | 98 | #include "net/net_buildflags.h" |
Lily Houghton | ffe89daa0 | 2018-03-09 18:30:03 | [diff] [blame] | 99 | #include "net/proxy_resolution/proxy_resolution_service.h" |
Ryan Hamilton | a3ee93a7 | 2018-08-01 22:03:08 | [diff] [blame] | 100 | #include "net/quic/mock_crypto_client_stream_factory.h" |
| 101 | #include "net/quic/quic_server_info.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 102 | #include "net/socket/socket_test_util.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 103 | #include "net/socket/ssl_client_socket.h" |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 104 | #include "net/ssl/client_cert_identity_test_util.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 105 | #include "net/ssl/ssl_connection_status_flags.h" |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 106 | #include "net/ssl/ssl_private_key.h" |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 107 | #include "net/ssl/ssl_server_config.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 108 | #include "net/test/cert_test_util.h" |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 109 | #include "net/test/embedded_test_server/embedded_test_server.h" |
| 110 | #include "net/test/embedded_test_server/http_request.h" |
| 111 | #include "net/test/embedded_test_server/http_response.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 112 | #include "net/test/gtest_util.h" |
[email protected] | 89b3252 | 2013-05-07 20:04:21 | [diff] [blame] | 113 | #include "net/test/spawned_test_server/spawned_test_server.h" |
rsleevi | a69c79a | 2016-06-22 03:28:43 | [diff] [blame] | 114 | #include "net/test/test_data_directory.h" |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 115 | #include "net/test/test_with_scoped_task_environment.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 116 | #include "net/test/url_request/url_request_failed_job.h" |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 117 | #include "net/test/url_request/url_request_mock_http_job.h" |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 118 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 119 | #include "net/url_request/data_protocol_handler.h" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 120 | #include "net/url_request/static_http_user_agent_settings.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 121 | #include "net/url_request/url_request.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 122 | #include "net/url_request/url_request_filter.h" |
[email protected] | bcb84f8b | 2009-08-31 16:20:14 | [diff] [blame] | 123 | #include "net/url_request/url_request_http_job.h" |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 124 | #include "net/url_request/url_request_http_job_histogram.h" |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 125 | #include "net/url_request/url_request_intercepting_job_factory.h" |
| 126 | #include "net/url_request/url_request_interceptor.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 127 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 128 | #include "net/url_request/url_request_redirect_job.h" |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 129 | #include "net/url_request/url_request_test_job.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 130 | #include "net/url_request/url_request_test_util.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 131 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 132 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 133 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 134 | |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 135 | #if defined(OS_FUCHSIA) |
| 136 | #define USE_BUILTIN_CERT_VERIFIER |
| 137 | #endif |
| 138 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 139 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 140 | #include "net/base/filename_util.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 141 | #include "net/url_request/file_protocol_handler.h" |
| 142 | #include "net/url_request/url_request_file_dir_job.h" |
| 143 | #endif |
| 144 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 145 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
Emily Stark | 0122798 | 2019-06-06 18:45:56 | [diff] [blame^] | 146 | #include "net/ftp/ftp_auth_cache.h" |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 147 | #include "net/ftp/ftp_network_layer.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 148 | #include "net/url_request/ftp_protocol_handler.h" |
| 149 | #endif |
| 150 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 151 | #if defined(OS_WIN) |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 152 | #include "base/win/scoped_com_initializer.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 153 | #endif |
| 154 | |
Matt Mueller | 7f60dc4 | 2019-04-26 22:47:10 | [diff] [blame] | 155 | #if defined(OS_MACOSX) |
| 156 | #include "base/mac/mac_util.h" |
| 157 | #endif |
| 158 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 159 | #if BUILDFLAG(ENABLE_REPORTING) |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 160 | #include "net/network_error_logging/network_error_logging_service.h" |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 161 | #include "net/network_error_logging/network_error_logging_test_util.h" |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 162 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 163 | |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 164 | #if defined(USE_NSS_CERTS) |
| 165 | #include "net/cert_net/nss_ocsp.h" |
| 166 | #endif |
| 167 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 168 | using net::test::IsError; |
| 169 | using net::test::IsOk; |
| 170 | |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 171 | using base::ASCIIToUTF16; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 172 | using base::Time; |
halton.huo | e4e4574 | 2014-12-08 07:55:46 | [diff] [blame] | 173 | using std::string; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 174 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 175 | namespace net { |
| 176 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 177 | namespace { |
| 178 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 179 | namespace test_default { |
| 180 | #include "net/http/transport_security_state_static_unittest_default.h" |
xunjieli | 815ad5b | 2017-07-18 15:51:35 | [diff] [blame] | 181 | } |
| 182 | |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 183 | const base::string16 kChrome(ASCIIToUTF16("chrome")); |
| 184 | const base::string16 kSecret(ASCIIToUTF16("secret")); |
| 185 | const base::string16 kUser(ASCIIToUTF16("user")); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 186 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 187 | const base::FilePath::CharType kTestFilePath[] = |
| 188 | FILE_PATH_LITERAL("net/data/url_request_unittest"); |
| 189 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 190 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 191 | !defined(OS_FUCHSIA) |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 192 | // Test file used in most FTP tests. |
| 193 | const char kFtpTestFile[] = "BullRunSpeech.txt"; |
| 194 | #endif |
| 195 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 196 | // Tests load timing information in the case a fresh connection was used, with |
| 197 | // no proxy. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 198 | void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info, |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 199 | int connect_timing_flags) { |
| 200 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 201 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 202 | |
| 203 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 204 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 205 | |
| 206 | EXPECT_LE(load_timing_info.request_start, |
| 207 | load_timing_info.connect_timing.connect_start); |
| 208 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 209 | connect_timing_flags); |
| 210 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 211 | load_timing_info.send_start); |
| 212 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 213 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_start); |
| 214 | EXPECT_LE(load_timing_info.receive_headers_start, |
| 215 | load_timing_info.receive_headers_end); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 216 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 217 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 218 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 219 | } |
| 220 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 221 | // Same as above, but with proxy times. |
| 222 | void TestLoadTimingNotReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 223 | const LoadTimingInfo& load_timing_info, |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 224 | int connect_timing_flags) { |
| 225 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 226 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 227 | |
| 228 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 229 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 230 | |
| 231 | EXPECT_LE(load_timing_info.request_start, |
| 232 | load_timing_info.proxy_resolve_start); |
| 233 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 234 | load_timing_info.proxy_resolve_end); |
| 235 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 236 | load_timing_info.connect_timing.connect_start); |
| 237 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 238 | connect_timing_flags); |
| 239 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 240 | load_timing_info.send_start); |
| 241 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 242 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_start); |
| 243 | EXPECT_LE(load_timing_info.receive_headers_start, |
| 244 | load_timing_info.receive_headers_end); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // Same as above, but with a reused socket and proxy times. |
| 248 | void TestLoadTimingReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 249 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 250 | EXPECT_TRUE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 251 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 252 | |
| 253 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 254 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 255 | |
| 256 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 257 | |
| 258 | EXPECT_LE(load_timing_info.request_start, |
| 259 | load_timing_info.proxy_resolve_start); |
| 260 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 261 | load_timing_info.proxy_resolve_end); |
| 262 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 263 | load_timing_info.send_start); |
| 264 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 265 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_start); |
| 266 | EXPECT_LE(load_timing_info.receive_headers_start, |
| 267 | load_timing_info.receive_headers_end); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 268 | } |
| 269 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 270 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 271 | // Tests load timing information in the case of a cache hit, when no cache |
| 272 | // validation request was sent over the wire. |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 273 | base::StringPiece TestNetResourceProvider(int key) { |
| 274 | return "header"; |
| 275 | } |
| 276 | |
| 277 | void FillBuffer(char* buffer, size_t len) { |
| 278 | static bool called = false; |
| 279 | if (!called) { |
| 280 | called = true; |
| 281 | int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 282 | srand(seed); |
| 283 | } |
| 284 | |
| 285 | for (size_t i = 0; i < len; i++) { |
| 286 | buffer[i] = static_cast<char>(rand()); |
| 287 | if (!buffer[i]) |
| 288 | buffer[i] = 'g'; |
| 289 | } |
| 290 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 291 | #endif |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 292 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 293 | void TestLoadTimingCacheHitNoNetwork( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 294 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 295 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 296 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 297 | |
| 298 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 299 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 300 | |
| 301 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 302 | EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start); |
| 303 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 304 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_start); |
| 305 | EXPECT_LE(load_timing_info.receive_headers_start, |
| 306 | load_timing_info.receive_headers_end); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 307 | |
| 308 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 309 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 310 | } |
| 311 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 312 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 313 | !defined(OS_FUCHSIA) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 314 | // Tests load timing in the case that there is no HTTP response. This can be |
| 315 | // used to test in the case of errors or non-HTTP requests. |
| 316 | void TestLoadTimingNoHttpResponse( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 317 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 318 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 319 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 320 | |
| 321 | // Only the request times should be non-null. |
| 322 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 323 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 324 | |
| 325 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 326 | |
| 327 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 328 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 329 | EXPECT_TRUE(load_timing_info.send_start.is_null()); |
| 330 | EXPECT_TRUE(load_timing_info.send_end.is_null()); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 331 | EXPECT_TRUE(load_timing_info.receive_headers_start.is_null()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 332 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 333 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 334 | #endif |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 335 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 336 | // Job that allows monitoring of its priority. |
| 337 | class PriorityMonitoringURLRequestJob : public URLRequestTestJob { |
| 338 | public: |
| 339 | // The latest priority of the job is always written to |request_priority_|. |
| 340 | PriorityMonitoringURLRequestJob(URLRequest* request, |
| 341 | NetworkDelegate* network_delegate, |
| 342 | RequestPriority* request_priority) |
| 343 | : URLRequestTestJob(request, network_delegate), |
| 344 | request_priority_(request_priority) { |
| 345 | *request_priority_ = DEFAULT_PRIORITY; |
| 346 | } |
| 347 | |
| 348 | void SetPriority(RequestPriority priority) override { |
| 349 | *request_priority_ = priority; |
| 350 | URLRequestTestJob::SetPriority(priority); |
| 351 | } |
| 352 | |
| 353 | private: |
| 354 | RequestPriority* const request_priority_; |
| 355 | }; |
| 356 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 357 | // Do a case-insensitive search through |haystack| for |needle|. |
| 358 | bool ContainsString(const std::string& haystack, const char* needle) { |
brettw | a2027fb | 2015-07-14 02:24:50 | [diff] [blame] | 359 | std::string::const_iterator it = std::search( |
| 360 | haystack.begin(), haystack.end(), needle, needle + strlen(needle), |
| 361 | base::CaseInsensitiveCompareASCII<char>()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 362 | return it != haystack.end(); |
| 363 | } |
| 364 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 365 | std::unique_ptr<UploadDataStream> CreateSimpleUploadData(const char* data) { |
| 366 | std::unique_ptr<UploadElementReader> reader( |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 367 | new UploadBytesElementReader(data, strlen(data))); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 368 | return ElementsUploadDataStream::CreateWithReader(std::move(reader), 0); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 369 | } |
| 370 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 371 | // Verify that the SSLInfo of a successful SSL connection has valid values. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 372 | void CheckSSLInfo(const SSLInfo& ssl_info) { |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 373 | // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated. |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 374 | uint16_t cipher_suite = |
| 375 | SSLConnectionStatusToCipherSuite(ssl_info.connection_status); |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 376 | EXPECT_NE(0U, cipher_suite); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 377 | } |
| 378 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 379 | void CheckFullRequestHeaders(const HttpRequestHeaders& headers, |
| 380 | const GURL& host_url) { |
| 381 | std::string sent_value; |
| 382 | |
| 383 | EXPECT_TRUE(headers.GetHeader("Host", &sent_value)); |
| 384 | EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value); |
| 385 | |
| 386 | EXPECT_TRUE(headers.GetHeader("Connection", &sent_value)); |
| 387 | EXPECT_EQ("keep-alive", sent_value); |
| 388 | } |
| 389 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 390 | // A network delegate that allows the user to choose a subset of request stages |
| 391 | // to block in. When blocking, the delegate can do one of the following: |
| 392 | // * synchronously return a pre-specified error code, or |
| 393 | // * asynchronously return that value via an automatically called callback, |
| 394 | // or |
| 395 | // * block and wait for the user to do a callback. |
| 396 | // Additionally, the user may also specify a redirect URL -- then each request |
| 397 | // with the current URL different from the redirect target will be redirected |
| 398 | // to that target, in the on-before-URL-request stage, independent of whether |
| 399 | // the delegate blocks in ON_BEFORE_URL_REQUEST or not. |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 400 | class BlockingNetworkDelegate : public TestNetworkDelegate { |
| 401 | public: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 402 | // Stages in which the delegate can block. |
| 403 | enum Stage { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 404 | NOT_BLOCKED = 0, |
| 405 | ON_BEFORE_URL_REQUEST = 1 << 0, |
| 406 | ON_BEFORE_SEND_HEADERS = 1 << 1, |
| 407 | ON_HEADERS_RECEIVED = 1 << 2, |
| 408 | ON_AUTH_REQUIRED = 1 << 3 |
| 409 | }; |
| 410 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 411 | // Behavior during blocked stages. During other stages, just |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 412 | // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 413 | enum BlockMode { |
| 414 | SYNCHRONOUS, // No callback, returns specified return values. |
| 415 | AUTO_CALLBACK, // |this| posts a task to run the callback using the |
| 416 | // specified return codes. |
| 417 | USER_CALLBACK, // User takes care of doing a callback. |retval_| and |
| 418 | // |auth_retval_| are ignored. In every blocking stage the |
| 419 | // message loop is quit. |
| 420 | }; |
| 421 | |
| 422 | // Creates a delegate which does not block at all. |
| 423 | explicit BlockingNetworkDelegate(BlockMode block_mode); |
| 424 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 425 | // Runs the message loop until the delegate blocks. |
| 426 | void RunUntilBlocked(); |
| 427 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 428 | // For users to trigger a callback returning |response|. |
| 429 | // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks. |
| 430 | // Only call if |block_mode_| == USER_CALLBACK. |
| 431 | void DoCallback(int response); |
| 432 | void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response); |
| 433 | |
| 434 | // Setters. |
| 435 | void set_retval(int retval) { |
| 436 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 437 | ASSERT_NE(ERR_IO_PENDING, retval); |
| 438 | ASSERT_NE(OK, retval); |
| 439 | retval_ = retval; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 440 | } |
| 441 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 442 | // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then |
| 443 | // |auth_credentials_| will be passed with the response. |
| 444 | void set_auth_retval(AuthRequiredResponse auth_retval) { |
| 445 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 446 | ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval); |
| 447 | auth_retval_ = auth_retval; |
| 448 | } |
| 449 | void set_auth_credentials(const AuthCredentials& auth_credentials) { |
| 450 | auth_credentials_ = auth_credentials; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 451 | } |
| 452 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 453 | void set_redirect_url(const GURL& url) { |
| 454 | redirect_url_ = url; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 455 | } |
| 456 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 457 | void set_block_on(int block_on) { |
| 458 | block_on_ = block_on; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 459 | } |
| 460 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 461 | // Allows the user to check in which state did we block. |
| 462 | Stage stage_blocked_for_callback() const { |
| 463 | EXPECT_EQ(USER_CALLBACK, block_mode_); |
| 464 | return stage_blocked_for_callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | private: |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 468 | void OnBlocked(); |
| 469 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 470 | void RunCallback(int response, CompletionOnceCallback callback); |
| 471 | void RunAuthCallback(AuthRequiredResponse response, AuthCallback callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 472 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 473 | // TestNetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 474 | int OnBeforeURLRequest(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 475 | CompletionOnceCallback callback, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 476 | GURL* new_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 477 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 478 | int OnBeforeStartTransaction(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 479 | CompletionOnceCallback callback, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 480 | HttpRequestHeaders* headers) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 481 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 482 | int OnHeadersReceived( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 483 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 484 | CompletionOnceCallback callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 485 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 486 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 487 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 488 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 489 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 490 | URLRequest* request, |
| 491 | const AuthChallengeInfo& auth_info, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 492 | AuthCallback callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 493 | AuthCredentials* credentials) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 494 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 495 | // Resets the callbacks and |stage_blocked_for_callback_|. |
| 496 | void Reset(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 497 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 498 | // Checks whether we should block in |stage|. If yes, returns an error code |
| 499 | // and optionally sets up callback based on |block_mode_|. If no, returns OK. |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 500 | int MaybeBlockStage(Stage stage, CompletionOnceCallback callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 501 | |
| 502 | // Configuration parameters, can be adjusted by public methods: |
| 503 | const BlockMode block_mode_; |
| 504 | |
| 505 | // Values returned on blocking stages when mode is SYNCHRONOUS or |
| 506 | // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING. |
| 507 | int retval_; // To be returned in non-auth stages. |
| 508 | AuthRequiredResponse auth_retval_; |
| 509 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 510 | GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 511 | int block_on_; // Bit mask: in which stages to block. |
| 512 | |
| 513 | // |auth_credentials_| will be copied to |*target_auth_credential_| on |
| 514 | // callback. |
| 515 | AuthCredentials auth_credentials_; |
| 516 | AuthCredentials* target_auth_credentials_; |
| 517 | |
| 518 | // Internal variables, not set by not the user: |
| 519 | // Last blocked stage waiting for user callback (unused if |block_mode_| != |
| 520 | // USER_CALLBACK). |
| 521 | Stage stage_blocked_for_callback_; |
| 522 | |
| 523 | // Callback objects stored during blocking stages. |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 524 | CompletionOnceCallback callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 525 | AuthCallback auth_callback_; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 526 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 527 | // Closure to run to exit RunUntilBlocked(). |
| 528 | base::OnceClosure on_blocked_; |
| 529 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 530 | base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; |
| 531 | |
| 532 | DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 533 | }; |
| 534 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 535 | BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode) |
| 536 | : block_mode_(block_mode), |
| 537 | retval_(OK), |
| 538 | auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION), |
| 539 | block_on_(0), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 540 | target_auth_credentials_(nullptr), |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 541 | stage_blocked_for_callback_(NOT_BLOCKED), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 542 | weak_factory_(this) {} |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 543 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 544 | void BlockingNetworkDelegate::RunUntilBlocked() { |
| 545 | base::RunLoop run_loop; |
| 546 | on_blocked_ = run_loop.QuitClosure(); |
| 547 | run_loop.Run(); |
| 548 | } |
| 549 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 550 | void BlockingNetworkDelegate::DoCallback(int response) { |
| 551 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 552 | ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 553 | ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 554 | CompletionOnceCallback callback = std::move(callback_); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 555 | Reset(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 556 | |
| 557 | // |callback| may trigger completion of a request, so post it as a task, so |
| 558 | // it will run under a subsequent TestDelegate::RunUntilComplete() loop. |
| 559 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 560 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::RunCallback, |
| 561 | weak_factory_.GetWeakPtr(), response, |
| 562 | std::move(callback))); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | void BlockingNetworkDelegate::DoAuthCallback( |
| 566 | NetworkDelegate::AuthRequiredResponse response) { |
| 567 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 568 | ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 569 | AuthCallback auth_callback = std::move(auth_callback_); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 570 | Reset(); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 571 | RunAuthCallback(response, std::move(auth_callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 572 | } |
| 573 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 574 | void BlockingNetworkDelegate::OnBlocked() { |
| 575 | // If this fails due to |on_blocked_| being null then OnBlocked() was run by |
| 576 | // a RunLoop other than RunUntilBlocked(), indicating a bug in the calling |
| 577 | // test. |
| 578 | std::move(on_blocked_).Run(); |
| 579 | } |
| 580 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 581 | void BlockingNetworkDelegate::RunCallback(int response, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 582 | CompletionOnceCallback callback) { |
| 583 | std::move(callback).Run(response); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 587 | AuthCallback callback) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 588 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 589 | ASSERT_TRUE(target_auth_credentials_ != nullptr); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 590 | *target_auth_credentials_ = auth_credentials_; |
| 591 | } |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 592 | std::move(callback).Run(response); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 593 | } |
| 594 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 595 | int BlockingNetworkDelegate::OnBeforeURLRequest(URLRequest* request, |
| 596 | CompletionOnceCallback callback, |
| 597 | GURL* new_url) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 598 | if (redirect_url_ == request->url()) |
| 599 | return OK; // We've already seen this request and redirected elsewhere. |
| 600 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 601 | // TestNetworkDelegate always completes synchronously. |
| 602 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeURLRequest( |
| 603 | request, base::NullCallback(), new_url)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 604 | |
| 605 | if (!redirect_url_.is_empty()) |
| 606 | *new_url = redirect_url_; |
| 607 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 608 | return MaybeBlockStage(ON_BEFORE_URL_REQUEST, std::move(callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 609 | } |
| 610 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 611 | int BlockingNetworkDelegate::OnBeforeStartTransaction( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 612 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 613 | CompletionOnceCallback callback, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 614 | HttpRequestHeaders* headers) { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 615 | // TestNetworkDelegate always completes synchronously. |
| 616 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeStartTransaction( |
| 617 | request, base::NullCallback(), headers)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 618 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 619 | return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, std::move(callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | int BlockingNetworkDelegate::OnHeadersReceived( |
| 623 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 624 | CompletionOnceCallback callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 625 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 626 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
| 627 | GURL* allowed_unsafe_redirect_url) { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 628 | // TestNetworkDelegate always completes synchronously. |
| 629 | CHECK_NE(ERR_IO_PENDING, |
| 630 | TestNetworkDelegate::OnHeadersReceived( |
| 631 | request, base::NullCallback(), original_response_headers, |
| 632 | override_response_headers, allowed_unsafe_redirect_url)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 633 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 634 | return MaybeBlockStage(ON_HEADERS_RECEIVED, std::move(callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired( |
| 638 | URLRequest* request, |
| 639 | const AuthChallengeInfo& auth_info, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 640 | AuthCallback callback, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 641 | AuthCredentials* credentials) { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 642 | // TestNetworkDelegate always completes synchronously. |
| 643 | CHECK_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, |
| 644 | TestNetworkDelegate::OnAuthRequired( |
| 645 | request, auth_info, base::NullCallback(), credentials)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 646 | // Check that the user has provided callback for the previous blocked stage. |
| 647 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 648 | |
| 649 | if ((block_on_ & ON_AUTH_REQUIRED) == 0) { |
| 650 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 651 | } |
| 652 | |
| 653 | target_auth_credentials_ = credentials; |
| 654 | |
| 655 | switch (block_mode_) { |
| 656 | case SYNCHRONOUS: |
| 657 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) |
| 658 | *target_auth_credentials_ = auth_credentials_; |
| 659 | return auth_retval_; |
| 660 | |
| 661 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 662 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 663 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::RunAuthCallback, |
| 664 | weak_factory_.GetWeakPtr(), auth_retval_, |
| 665 | std::move(callback))); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 666 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 667 | |
| 668 | case USER_CALLBACK: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 669 | auth_callback_ = std::move(callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 670 | stage_blocked_for_callback_ = ON_AUTH_REQUIRED; |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 671 | // We may reach here via a callback prior to RunUntilBlocked(), so post |
| 672 | // a task to fetch and run the |on_blocked_| closure. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 673 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 674 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::OnBlocked, |
| 675 | weak_factory_.GetWeakPtr())); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 676 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 677 | } |
| 678 | NOTREACHED(); |
| 679 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value. |
| 680 | } |
| 681 | |
| 682 | void BlockingNetworkDelegate::Reset() { |
| 683 | EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 684 | stage_blocked_for_callback_ = NOT_BLOCKED; |
| 685 | callback_.Reset(); |
| 686 | auth_callback_.Reset(); |
| 687 | } |
| 688 | |
| 689 | int BlockingNetworkDelegate::MaybeBlockStage( |
| 690 | BlockingNetworkDelegate::Stage stage, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 691 | CompletionOnceCallback callback) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 692 | // Check that the user has provided callback for the previous blocked stage. |
| 693 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 694 | |
| 695 | if ((block_on_ & stage) == 0) { |
| 696 | return OK; |
| 697 | } |
| 698 | |
| 699 | switch (block_mode_) { |
| 700 | case SYNCHRONOUS: |
| 701 | EXPECT_NE(OK, retval_); |
| 702 | return retval_; |
| 703 | |
| 704 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 705 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 706 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::RunCallback, |
| 707 | weak_factory_.GetWeakPtr(), retval_, |
| 708 | std::move(callback))); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 709 | return ERR_IO_PENDING; |
| 710 | |
| 711 | case USER_CALLBACK: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 712 | callback_ = std::move(callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 713 | stage_blocked_for_callback_ = stage; |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 714 | // We may reach here via a callback prior to RunUntilBlocked(), so post |
| 715 | // a task to fetch and run the |on_blocked_| closure. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 716 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 717 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::OnBlocked, |
| 718 | weak_factory_.GetWeakPtr())); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 719 | return ERR_IO_PENDING; |
| 720 | } |
| 721 | NOTREACHED(); |
| 722 | return 0; |
| 723 | } |
| 724 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 725 | class TestURLRequestContextWithProxy : public TestURLRequestContext { |
| 726 | public: |
| 727 | // Does not own |delegate|. |
| 728 | TestURLRequestContextWithProxy(const std::string& proxy, |
| 729 | NetworkDelegate* delegate) |
| 730 | : TestURLRequestContext(true) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 731 | context_storage_.set_proxy_resolution_service( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 732 | ProxyResolutionService::CreateFixed(proxy, |
| 733 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 734 | set_network_delegate(delegate); |
| 735 | Init(); |
| 736 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 737 | ~TestURLRequestContextWithProxy() override = default; |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 738 | }; |
| 739 | |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 740 | // A mock ReportSenderInterface that just remembers the latest report |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 741 | // URI and report to be sent. |
| 742 | class MockCertificateReportSender |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 743 | : public TransportSecurityState::ReportSenderInterface { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 744 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 745 | MockCertificateReportSender() = default; |
| 746 | ~MockCertificateReportSender() override = default; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 747 | |
meacer | 5d4dc5a | 2017-04-27 20:37:48 | [diff] [blame] | 748 | void Send(const GURL& report_uri, |
| 749 | base::StringPiece content_type, |
| 750 | base::StringPiece report, |
| 751 | const base::Callback<void()>& success_callback, |
| 752 | const base::Callback<void(const GURL&, int, int)>& error_callback) |
| 753 | override { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 754 | latest_report_uri_ = report_uri; |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 755 | report.CopyToString(&latest_report_); |
| 756 | content_type.CopyToString(&latest_content_type_); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 757 | } |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 758 | const GURL& latest_report_uri() { return latest_report_uri_; } |
| 759 | const std::string& latest_report() { return latest_report_; } |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 760 | const std::string& latest_content_type() { return latest_content_type_; } |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 761 | |
| 762 | private: |
| 763 | GURL latest_report_uri_; |
| 764 | std::string latest_report_; |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 765 | std::string latest_content_type_; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 766 | }; |
| 767 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 768 | // OCSPErrorTestDelegate caches the SSLInfo passed to OnSSLCertificateError. |
| 769 | // This is needed because after the certificate failure, the URLRequest will |
| 770 | // retry the connection, and return a partial SSLInfo with a cached cert status. |
| 771 | // The partial SSLInfo does not have the OCSP information filled out. |
| 772 | class OCSPErrorTestDelegate : public TestDelegate { |
| 773 | public: |
| 774 | void OnSSLCertificateError(URLRequest* request, |
Emily Stark | 79fba584 | 2019-04-25 04:59:36 | [diff] [blame] | 775 | int net_error, |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 776 | const SSLInfo& ssl_info, |
| 777 | bool fatal) override { |
| 778 | ssl_info_ = ssl_info; |
| 779 | on_ssl_certificate_error_called_ = true; |
Emily Stark | 79fba584 | 2019-04-25 04:59:36 | [diff] [blame] | 780 | TestDelegate::OnSSLCertificateError(request, net_error, ssl_info, fatal); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | bool on_ssl_certificate_error_called() { |
| 784 | return on_ssl_certificate_error_called_; |
| 785 | } |
| 786 | |
| 787 | SSLInfo ssl_info() { return ssl_info_; } |
| 788 | |
| 789 | private: |
| 790 | bool on_ssl_certificate_error_called_ = false; |
| 791 | SSLInfo ssl_info_; |
| 792 | }; |
| 793 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 794 | } // namespace |
| 795 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 796 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 797 | class URLRequestTest : public PlatformTest, public WithScopedTaskEnvironment { |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 798 | public: |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 799 | URLRequestTest() |
| 800 | : default_context_(std::make_unique<TestURLRequestContext>(true)) { |
| 801 | default_context_->set_network_delegate(&default_network_delegate_); |
| 802 | default_context_->set_net_log(&net_log_); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 803 | job_factory_impl_ = new URLRequestJobFactoryImpl(); |
| 804 | job_factory_.reset(job_factory_impl_); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 805 | } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 806 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 807 | ~URLRequestTest() override { |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 808 | // URLRequestJobs may post clean-up tasks on destruction. |
| 809 | base::RunLoop().RunUntilIdle(); |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 810 | |
| 811 | SetTransportSecurityStateSourceForTesting(nullptr); |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 812 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 813 | |
dcheng | 2339883c | 2014-12-23 00:23:05 | [diff] [blame] | 814 | void SetUp() override { |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 815 | SetUpFactory(); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 816 | default_context_->set_job_factory(job_factory_.get()); |
| 817 | default_context_->Init(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 818 | PlatformTest::SetUp(); |
| 819 | } |
| 820 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 821 | void TearDown() override { default_context_.reset(); } |
| 822 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 823 | virtual void SetUpFactory() { |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 824 | job_factory_impl_->SetProtocolHandler( |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 825 | "data", std::make_unique<DataProtocolHandler>()); |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 826 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 827 | job_factory_impl_->SetProtocolHandler( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 828 | "file", std::make_unique<FileProtocolHandler>( |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 829 | base::ThreadTaskRunnerHandle::Get())); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 830 | #endif |
| 831 | } |
| 832 | |
| 833 | TestNetworkDelegate* default_network_delegate() { |
| 834 | return &default_network_delegate_; |
| 835 | } |
| 836 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 837 | TestURLRequestContext& default_context() const { return *default_context_; } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 838 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 839 | // Adds the TestJobInterceptor to the default context. |
| 840 | TestJobInterceptor* AddTestInterceptor() { |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 841 | TestJobInterceptor* protocol_handler_ = new TestJobInterceptor(); |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 842 | job_factory_impl_->SetProtocolHandler("http", nullptr); |
| 843 | job_factory_impl_->SetProtocolHandler("http", |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 844 | base::WrapUnique(protocol_handler_)); |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 845 | return protocol_handler_; |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 846 | } |
| 847 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 848 | // Creates a temp test file and writes |data| to the file. The file will be |
| 849 | // deleted after the test completes. |
| 850 | void CreateTestFile(const char* data, |
| 851 | size_t data_size, |
| 852 | base::FilePath* test_file) { |
| 853 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 854 | // Get an absolute path since |temp_dir| can contain a symbolic link. As of |
| 855 | // now, Mac and Android bots return a path with a symbolic link. |
| 856 | base::FilePath absolute_temp_dir = |
| 857 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
| 858 | |
| 859 | ASSERT_TRUE(base::CreateTemporaryFileInDir(absolute_temp_dir, test_file)); |
| 860 | ASSERT_EQ(static_cast<int>(data_size), |
| 861 | base::WriteFile(*test_file, data, data_size)); |
| 862 | } |
| 863 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 864 | protected: |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 865 | TestNetLog net_log_; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 866 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 867 | URLRequestJobFactoryImpl* job_factory_impl_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 868 | std::unique_ptr<URLRequestJobFactory> job_factory_; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 869 | std::unique_ptr<TestURLRequestContext> default_context_; |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 870 | base::ScopedTempDir temp_dir_; |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 871 | }; |
| 872 | |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 873 | // This NetworkDelegate is picky about what files are accessible. Only |
| 874 | // whitelisted files are allowed. |
| 875 | class CookieBlockingNetworkDelegate : public TestNetworkDelegate { |
| 876 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 877 | CookieBlockingNetworkDelegate() = default; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 878 | |
| 879 | // Adds |directory| to the access white list. |
| 880 | void AddToWhitelist(const base::FilePath& directory) { |
| 881 | whitelist_.insert(directory); |
| 882 | } |
| 883 | |
| 884 | private: |
| 885 | // Returns true if |path| matches the white list. |
| 886 | bool OnCanAccessFileInternal(const base::FilePath& path) const { |
| 887 | for (const auto& directory : whitelist_) { |
| 888 | if (directory == path || directory.IsParent(path)) |
| 889 | return true; |
| 890 | } |
| 891 | return false; |
| 892 | } |
| 893 | |
| 894 | // Returns true only if both |original_path| and |absolute_path| match the |
| 895 | // white list. |
| 896 | bool OnCanAccessFile(const URLRequest& request, |
| 897 | const base::FilePath& original_path, |
| 898 | const base::FilePath& absolute_path) const override { |
| 899 | return (OnCanAccessFileInternal(original_path) && |
| 900 | OnCanAccessFileInternal(absolute_path)); |
| 901 | } |
| 902 | |
| 903 | std::set<base::FilePath> whitelist_; |
| 904 | |
| 905 | DISALLOW_COPY_AND_ASSIGN(CookieBlockingNetworkDelegate); |
| 906 | }; |
| 907 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 908 | TEST_F(URLRequestTest, AboutBlankTest) { |
| 909 | TestDelegate d; |
| 910 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 911 | std::unique_ptr<URLRequest> r( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 912 | default_context().CreateRequest(GURL("about:blank"), DEFAULT_PRIORITY, |
| 913 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 914 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 915 | r->Start(); |
| 916 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 917 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 918 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 919 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 920 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 921 | EXPECT_FALSE(d.received_data_before_response()); |
| 922 | EXPECT_EQ(d.bytes_received(), 0); |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 923 | EXPECT_TRUE(r->GetResponseRemoteEndpoint().address().empty()); |
| 924 | EXPECT_EQ(0, r->GetResponseRemoteEndpoint().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 925 | |
| 926 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 927 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 928 | } |
| 929 | } |
| 930 | |
| 931 | TEST_F(URLRequestTest, DataURLImageTest) { |
| 932 | TestDelegate d; |
| 933 | { |
| 934 | // Use our nice little Chrome logo. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 935 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 936 | GURL("data:image/png;base64," |
| 937 | "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUB" |
| 938 | "BjG3w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQ" |
| 939 | "xIEVxitD5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI" |
| 940 | "5MpCVdW0gO7tvNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKz" |
| 941 | "Mx1+fg9bAgK6zHq9cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7Ck" |
| 942 | "IMDxQpF7r/MWq12UctI1dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0" |
| 943 | "WYUqt57yWf2McHTObYPbVD+ZwbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2H" |
| 944 | "QQcm43XwmtoYM6vVKleh0uoWvnzW3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nk" |
| 945 | "DFHF9ZS+uYVjRUasMeHUmyLYtcklTvzWGFZnNOXczThvpKIzjcahSqIzkvDLayDq6" |
| 946 | "D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTbxXX1T5xsV9tm9r4TQwHLiZw/pdDZJ" |
| 947 | "ea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5MrVvMzNaI3+ERHfrFzPKQukrQ" |
| 948 | "GI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gVMd6q5c8GdosynKmSeRuGz" |
| 949 | "pjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq/uxxZKzNiZFGD0wRC" |
| 950 | "3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7FUx1qLnV7MGF40" |
| 951 | "smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufBJbsysjjW4" |
| 952 | "kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJMnlm2O" |
| 953 | "34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfmm" |
| 954 | "oRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV" |
| 955 | "5EnhORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 956 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 957 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 958 | r->Start(); |
| 959 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 960 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 961 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 962 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 963 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 964 | EXPECT_FALSE(d.received_data_before_response()); |
| 965 | EXPECT_EQ(d.bytes_received(), 911); |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 966 | EXPECT_TRUE(r->GetResponseRemoteEndpoint().address().empty()); |
| 967 | EXPECT_EQ(0, r->GetResponseRemoteEndpoint().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 968 | |
| 969 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 970 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 974 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 975 | TEST_F(URLRequestTest, FileTest) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 976 | const char kTestFileContent[] = "Hello"; |
| 977 | base::FilePath test_file; |
| 978 | ASSERT_NO_FATAL_FAILURE( |
| 979 | CreateTestFile(kTestFileContent, sizeof(kTestFileContent), &test_file)); |
kraush | 5a64582 | 2016-04-07 18:35:04 | [diff] [blame] | 980 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 981 | GURL test_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 982 | |
| 983 | TestDelegate d; |
| 984 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 985 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 986 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 987 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 988 | r->Start(); |
| 989 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 990 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 991 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 992 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 993 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 994 | EXPECT_EQ(1, d.response_started_count()); |
| 995 | EXPECT_FALSE(d.received_data_before_response()); |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 996 | EXPECT_EQ(d.bytes_received(), static_cast<int>(sizeof(kTestFileContent))); |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 997 | EXPECT_TRUE(r->GetResponseRemoteEndpoint().address().empty()); |
| 998 | EXPECT_EQ(0, r->GetResponseRemoteEndpoint().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 999 | |
| 1000 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1001 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1002 | } |
| 1003 | } |
| 1004 | |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1005 | TEST_F(URLRequestTest, FileTestCancel) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1006 | const char kTestFileContent[] = "Hello"; |
| 1007 | base::FilePath test_file; |
| 1008 | ASSERT_NO_FATAL_FAILURE( |
| 1009 | CreateTestFile(kTestFileContent, sizeof(kTestFileContent), &test_file)); |
| 1010 | |
| 1011 | GURL test_url = FilePathToFileURL(test_file); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1012 | |
| 1013 | TestDelegate d; |
| 1014 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1015 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1016 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1017 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1018 | r->Start(); |
| 1019 | EXPECT_TRUE(r->is_pending()); |
| 1020 | r->Cancel(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1021 | } |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 1022 | // Async cancellation should be safe even when URLRequest has been already |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1023 | // destroyed. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1024 | base::RunLoop().RunUntilIdle(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1025 | } |
| 1026 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1027 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 1028 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1029 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1030 | FillBuffer(buffer.get(), buffer_size); |
| 1031 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1032 | base::FilePath test_file; |
| 1033 | ASSERT_NO_FATAL_FAILURE( |
| 1034 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1035 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1036 | |
| 1037 | const size_t first_byte_position = 500; |
| 1038 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 1039 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 1040 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 1041 | buffer.get() + last_byte_position + 1); |
| 1042 | |
| 1043 | TestDelegate d; |
| 1044 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1045 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1046 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1047 | |
| 1048 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1049 | headers.SetHeader( |
| 1050 | HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1051 | HttpByteRange::Bounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1052 | first_byte_position, last_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1053 | r->SetExtraRequestHeaders(headers); |
| 1054 | r->Start(); |
| 1055 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1056 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1057 | d.RunUntilComplete(); |
| 1058 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1059 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1060 | EXPECT_EQ(1, d.response_started_count()); |
| 1061 | EXPECT_FALSE(d.received_data_before_response()); |
| 1062 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 1063 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 1064 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 1065 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 1069 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1070 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1071 | FillBuffer(buffer.get(), buffer_size); |
| 1072 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1073 | base::FilePath test_file; |
| 1074 | ASSERT_NO_FATAL_FAILURE( |
| 1075 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1076 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1077 | |
| 1078 | const size_t first_byte_position = 500; |
| 1079 | const size_t last_byte_position = buffer_size - 1; |
| 1080 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 1081 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 1082 | buffer.get() + last_byte_position + 1); |
| 1083 | |
| 1084 | TestDelegate d; |
| 1085 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1086 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1087 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1088 | |
| 1089 | HttpRequestHeaders headers; |
| 1090 | headers.SetHeader(HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1091 | HttpByteRange::RightUnbounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1092 | first_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1093 | r->SetExtraRequestHeaders(headers); |
| 1094 | r->Start(); |
| 1095 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1096 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1097 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1098 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1099 | EXPECT_EQ(1, d.response_started_count()); |
| 1100 | EXPECT_FALSE(d.received_data_before_response()); |
| 1101 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 1102 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 1103 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 1104 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 1108 | const size_t buffer_size = 400000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1109 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1110 | FillBuffer(buffer.get(), buffer_size); |
| 1111 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1112 | base::FilePath test_file; |
| 1113 | ASSERT_NO_FATAL_FAILURE( |
| 1114 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1115 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1116 | |
| 1117 | TestDelegate d; |
| 1118 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1119 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1120 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1121 | |
| 1122 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1123 | headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1124 | r->SetExtraRequestHeaders(headers); |
| 1125 | r->Start(); |
| 1126 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1127 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1128 | d.RunUntilComplete(); |
| 1129 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1130 | EXPECT_TRUE(d.request_failed()); |
| 1131 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1132 | } |
| 1133 | |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1134 | TEST_F(URLRequestTest, AllowFileURLs) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1135 | std::string test_data("monkey"); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1136 | base::FilePath test_file; |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1137 | ASSERT_NO_FATAL_FAILURE( |
| 1138 | CreateTestFile(test_data.data(), test_data.size(), &test_file)); |
| 1139 | |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1140 | // The directory part of the path returned from CreateTemporaryFileInDir() |
| 1141 | // can be slightly different from |absolute_temp_dir| on Windows. |
| 1142 | // Example: C:\\Users\\CHROME~2 -> C:\\Users\\chrome-bot |
| 1143 | // Hence the test should use the directory name of |test_file|, rather than |
| 1144 | // |absolute_temp_dir|, for whitelisting. |
| 1145 | base::FilePath real_temp_dir = test_file.DirName(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1146 | GURL test_file_url = FilePathToFileURL(test_file); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1147 | { |
| 1148 | TestDelegate d; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1149 | CookieBlockingNetworkDelegate network_delegate; |
| 1150 | network_delegate.AddToWhitelist(real_temp_dir); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1151 | default_context().set_network_delegate(&network_delegate); |
| 1152 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1153 | test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1154 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1155 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1156 | // This should be allowed as the file path is whitelisted. |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1157 | EXPECT_FALSE(d.request_failed()); |
| 1158 | EXPECT_EQ(test_data, d.data_received()); |
| 1159 | } |
| 1160 | |
| 1161 | { |
| 1162 | TestDelegate d; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1163 | CookieBlockingNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1164 | default_context().set_network_delegate(&network_delegate); |
| 1165 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1166 | test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1167 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1168 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1169 | // This should be rejected as the file path is not whitelisted. |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1170 | EXPECT_TRUE(d.request_failed()); |
| 1171 | EXPECT_EQ("", d.data_received()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1172 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1173 | } |
| 1174 | } |
| 1175 | |
Wez | 3553913 | 2018-07-17 11:26:05 | [diff] [blame] | 1176 | #if defined(OS_POSIX) // Because of symbolic links. |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1177 | |
| 1178 | TEST_F(URLRequestTest, SymlinksToFiles) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1179 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1180 | // Get an absolute path since temp_dir can contain a symbolic link. |
| 1181 | base::FilePath absolute_temp_dir = |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1182 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1183 | |
| 1184 | // Create a good directory (will be whitelisted) and a good file. |
| 1185 | base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); |
| 1186 | ASSERT_TRUE(base::CreateDirectory(good_dir)); |
| 1187 | base::FilePath good_file; |
| 1188 | ASSERT_TRUE(base::CreateTemporaryFileInDir(good_dir, &good_file)); |
| 1189 | std::string good_data("good"); |
| 1190 | base::WriteFile(good_file, good_data.data(), good_data.size()); |
| 1191 | // See the comment in AllowFileURLs() for why this is done. |
| 1192 | base::FilePath real_good_dir = good_file.DirName(); |
| 1193 | |
| 1194 | // Create a bad directory (will not be whitelisted) and a bad file. |
| 1195 | base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); |
| 1196 | ASSERT_TRUE(base::CreateDirectory(bad_dir)); |
| 1197 | base::FilePath bad_file; |
| 1198 | ASSERT_TRUE(base::CreateTemporaryFileInDir(bad_dir, &bad_file)); |
| 1199 | std::string bad_data("bad"); |
| 1200 | base::WriteFile(bad_file, bad_data.data(), bad_data.size()); |
| 1201 | |
| 1202 | // This symlink will point to the good file. Access to the symlink will be |
| 1203 | // allowed as both the symlink and the destination file are in the same |
| 1204 | // good directory. |
| 1205 | base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); |
| 1206 | ASSERT_TRUE(base::CreateSymbolicLink(good_file, good_symlink)); |
| 1207 | GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1208 | // This symlink will point to the bad file. Even though the symlink is in |
| 1209 | // the good directory, access to the symlink will be rejected since it |
| 1210 | // points to the bad file. |
| 1211 | base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); |
| 1212 | ASSERT_TRUE(base::CreateSymbolicLink(bad_file, bad_symlink)); |
| 1213 | GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1214 | |
| 1215 | CookieBlockingNetworkDelegate network_delegate; |
| 1216 | network_delegate.AddToWhitelist(real_good_dir); |
| 1217 | { |
| 1218 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1219 | default_context().set_network_delegate(&network_delegate); |
| 1220 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1221 | good_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1222 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1223 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1224 | // good_file_url should be allowed. |
| 1225 | EXPECT_FALSE(d.request_failed()); |
| 1226 | EXPECT_EQ(good_data, d.data_received()); |
| 1227 | } |
| 1228 | |
| 1229 | { |
| 1230 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1231 | default_context().set_network_delegate(&network_delegate); |
| 1232 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1233 | bad_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1234 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1235 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1236 | // bad_file_url should be rejected. |
| 1237 | EXPECT_TRUE(d.request_failed()); |
| 1238 | EXPECT_EQ("", d.data_received()); |
| 1239 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | TEST_F(URLRequestTest, SymlinksToDirs) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1244 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1245 | // Get an absolute path since temp_dir can contain a symbolic link. |
| 1246 | base::FilePath absolute_temp_dir = |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1247 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1248 | |
| 1249 | // Create a good directory (will be whitelisted). |
| 1250 | base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); |
| 1251 | ASSERT_TRUE(base::CreateDirectory(good_dir)); |
| 1252 | |
| 1253 | // Create a bad directory (will not be whitelisted). |
| 1254 | base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); |
| 1255 | ASSERT_TRUE(base::CreateDirectory(bad_dir)); |
| 1256 | |
| 1257 | // This symlink will point to the good directory. Access to the symlink |
| 1258 | // will be allowed as the symlink is in the good dir that'll be white |
| 1259 | // listed. |
| 1260 | base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); |
| 1261 | ASSERT_TRUE(base::CreateSymbolicLink(good_dir, good_symlink)); |
| 1262 | GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1263 | // This symlink will point to the bad directory. Even though the symlink is |
| 1264 | // in the good directory, access to the symlink will be rejected since it |
| 1265 | // points to the bad directory. |
| 1266 | base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); |
| 1267 | ASSERT_TRUE(base::CreateSymbolicLink(bad_dir, bad_symlink)); |
| 1268 | GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1269 | |
| 1270 | CookieBlockingNetworkDelegate network_delegate; |
| 1271 | network_delegate.AddToWhitelist(good_dir); |
| 1272 | { |
| 1273 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1274 | default_context().set_network_delegate(&network_delegate); |
| 1275 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1276 | good_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1277 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1278 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1279 | // good_file_url should be allowed. |
| 1280 | EXPECT_FALSE(d.request_failed()); |
| 1281 | ASSERT_NE(d.data_received().find("good_symlink"), std::string::npos); |
| 1282 | } |
| 1283 | |
| 1284 | { |
| 1285 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1286 | default_context().set_network_delegate(&network_delegate); |
| 1287 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1288 | bad_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1289 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1290 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1291 | // bad_file_url should be rejected. |
| 1292 | EXPECT_TRUE(d.request_failed()); |
| 1293 | EXPECT_EQ("", d.data_received()); |
| 1294 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
| 1295 | } |
| 1296 | } |
| 1297 | |
Wez | 3553913 | 2018-07-17 11:26:05 | [diff] [blame] | 1298 | #endif // defined(OS_POSIX) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1299 | |
| 1300 | TEST_F(URLRequestTest, FileDirCancelTest) { |
| 1301 | // Put in mock resource provider. |
| 1302 | NetModule::SetResourceProvider(TestNetResourceProvider); |
| 1303 | |
| 1304 | TestDelegate d; |
| 1305 | { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1306 | base::FilePath file_path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1307 | base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1308 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 1309 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
| 1310 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1311 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1312 | FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d, |
| 1313 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1314 | req->Start(); |
| 1315 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1316 | |
| 1317 | d.set_cancel_in_received_data_pending(true); |
| 1318 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1319 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | // Take out mock resource provider. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1323 | NetModule::SetResourceProvider(nullptr); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1324 | } |
| 1325 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1326 | TEST_F(URLRequestTest, FileDirOutputSanity) { |
| 1327 | // Verify the general sanity of the the output of the file: |
| 1328 | // directory lister by checking for the output of a known existing |
| 1329 | // file. |
| 1330 | const char sentinel_name[] = "filedir-sentinel"; |
| 1331 | |
| 1332 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1333 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1334 | path = path.Append(kTestFilePath); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1335 | |
| 1336 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1337 | std::unique_ptr<URLRequest> req( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1338 | default_context().CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 1339 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1340 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1341 | d.RunUntilComplete(); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1342 | |
| 1343 | // Generate entry for the sentinel file. |
| 1344 | base::FilePath sentinel_path = path.AppendASCII(sentinel_name); |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 1345 | base::File::Info info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 1346 | EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1347 | EXPECT_GT(info.size, 0); |
| 1348 | std::string sentinel_output = GetDirectoryListingEntry( |
| 1349 | base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1350 | std::string(sentinel_name), false /* is_dir */, info.size, |
| 1351 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1352 | info.last_modified); |
| 1353 | |
| 1354 | ASSERT_LT(0, d.bytes_received()); |
| 1355 | ASSERT_FALSE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1356 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1357 | // Check for the entry generated for the "sentinel" file. |
| 1358 | const std::string& data = d.data_received(); |
| 1359 | ASSERT_NE(data.find(sentinel_output), std::string::npos); |
| 1360 | } |
| 1361 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1362 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 1363 | // There is an implicit redirect when loading a file path that matches a |
| 1364 | // directory and does not end with a slash. Ensure that following such |
| 1365 | // redirects does not crash. See http://crbug.com/18686. |
| 1366 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1367 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1368 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1369 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1370 | |
| 1371 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1372 | std::unique_ptr<URLRequest> req( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1373 | default_context().CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 1374 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1375 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1376 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1377 | |
| 1378 | ASSERT_EQ(1, d.received_redirect_count()); |
| 1379 | ASSERT_LT(0, d.bytes_received()); |
| 1380 | ASSERT_FALSE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1381 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | #if defined(OS_WIN) |
| 1385 | // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
| 1386 | TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
| 1387 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1388 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1389 | GURL("file:///"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1390 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1391 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1392 | |
| 1393 | ASSERT_EQ(1, d.received_redirect_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1394 | EXPECT_NE(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1395 | } |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1396 | #endif // defined(OS_WIN) |
| 1397 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 1398 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1399 | |
| 1400 | TEST_F(URLRequestTest, InvalidUrlTest) { |
| 1401 | TestDelegate d; |
| 1402 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1403 | std::unique_ptr<URLRequest> r( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1404 | default_context().CreateRequest(GURL("invalid url"), DEFAULT_PRIORITY, |
| 1405 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1406 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1407 | r->Start(); |
| 1408 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1409 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1410 | d.RunUntilComplete(); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1411 | EXPECT_TRUE(d.request_failed()); |
| 1412 | } |
| 1413 | } |
| 1414 | |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1415 | TEST_F(URLRequestTest, InvalidReferrerTest) { |
| 1416 | TestURLRequestContext context; |
| 1417 | TestNetworkDelegate network_delegate; |
| 1418 | network_delegate.set_cancel_request_with_policy_violating_referrer(true); |
| 1419 | context.set_network_delegate(&network_delegate); |
| 1420 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1421 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1422 | context.CreateRequest(GURL("http://localhost/"), DEFAULT_PRIORITY, &d, |
| 1423 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1424 | req->SetReferrer("https://somewhere.com/"); |
| 1425 | |
| 1426 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1427 | d.RunUntilComplete(); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1428 | EXPECT_TRUE(d.request_failed()); |
| 1429 | } |
| 1430 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1431 | #if defined(OS_WIN) |
| 1432 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
| 1433 | base::FilePath app_path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1434 | base::PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1435 | app_path = app_path.Append(kTestFilePath); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1436 | app_path = app_path.AppendASCII("with-headers.html"); |
| 1437 | |
jdoerrie | 6312bf6 | 2019-02-01 22:03:42 | [diff] [blame] | 1438 | base::string16 lnk_path = app_path.value() + FILE_PATH_LITERAL(".lnk"); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1439 | |
| 1440 | base::win::ScopedCOMInitializer com_initializer; |
| 1441 | |
| 1442 | // Temporarily create a shortcut for test |
| 1443 | { |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 1444 | Microsoft::WRL::ComPtr<IShellLink> shell; |
robliao | eb9bfd64 | 2017-05-18 17:35:16 | [diff] [blame] | 1445 | ASSERT_TRUE(SUCCEEDED(::CoCreateInstance( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1446 | CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shell)))); |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 1447 | Microsoft::WRL::ComPtr<IPersistFile> persist; |
robliao | 310fa98b | 2017-05-11 17:14:00 | [diff] [blame] | 1448 | ASSERT_TRUE(SUCCEEDED(shell.CopyTo(persist.GetAddressOf()))); |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1449 | EXPECT_TRUE(SUCCEEDED(shell->SetPath(base::as_wcstr(app_path.value())))); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1450 | EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1451 | EXPECT_TRUE(SUCCEEDED(persist->Save(base::as_wcstr(lnk_path), TRUE))); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | TestDelegate d; |
| 1455 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1456 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1457 | FilePathToFileURL(base::FilePath(lnk_path)), DEFAULT_PRIORITY, &d, |
| 1458 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1459 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1460 | r->Start(); |
| 1461 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1462 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1463 | d.RunUntilComplete(); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1464 | |
| 1465 | WIN32_FILE_ATTRIBUTE_DATA data; |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1466 | GetFileAttributesEx(base::as_wcstr(app_path.value()), GetFileExInfoStandard, |
jdoerrie | 6312bf6 | 2019-02-01 22:03:42 | [diff] [blame] | 1467 | &data); |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1468 | HANDLE file = CreateFile(base::as_wcstr(app_path.value()), GENERIC_READ, |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1469 | FILE_SHARE_READ, nullptr, OPEN_EXISTING, |
| 1470 | FILE_ATTRIBUTE_NORMAL, nullptr); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1471 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1472 | std::unique_ptr<char[]> buffer(new char[data.nFileSizeLow]); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1473 | DWORD read_size; |
| 1474 | BOOL result; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1475 | result = |
| 1476 | ReadFile(file, buffer.get(), data.nFileSizeLow, &read_size, nullptr); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1477 | std::string content(buffer.get(), read_size); |
| 1478 | CloseHandle(file); |
| 1479 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1480 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1481 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1482 | EXPECT_EQ(content, d.data_received()); |
| 1483 | } |
| 1484 | |
| 1485 | // Clean the shortcut |
jdoerrie | bacc196 | 2019-02-07 13:39:22 | [diff] [blame] | 1486 | DeleteFile(base::as_wcstr(lnk_path)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1487 | } |
| 1488 | #endif // defined(OS_WIN) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1489 | |
| 1490 | // Custom URLRequestJobs for use with interceptor tests |
| 1491 | class RestartTestJob : public URLRequestTestJob { |
| 1492 | public: |
| 1493 | RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1494 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1495 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1496 | void StartAsync() override { this->NotifyRestartRequired(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1497 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1498 | ~RestartTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1499 | }; |
| 1500 | |
| 1501 | class CancelTestJob : public URLRequestTestJob { |
| 1502 | public: |
| 1503 | explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1504 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1505 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1506 | void StartAsync() override { request_->Cancel(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1507 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1508 | ~CancelTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1509 | }; |
| 1510 | |
| 1511 | class CancelThenRestartTestJob : public URLRequestTestJob { |
| 1512 | public: |
| 1513 | explicit CancelThenRestartTestJob(URLRequest* request, |
| 1514 | NetworkDelegate* network_delegate) |
| 1515 | : URLRequestTestJob(request, network_delegate, true) { |
| 1516 | } |
| 1517 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1518 | void StartAsync() override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1519 | request_->Cancel(); |
| 1520 | this->NotifyRestartRequired(); |
| 1521 | } |
| 1522 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1523 | ~CancelThenRestartTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1524 | }; |
| 1525 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1526 | // An Interceptor for use with interceptor tests. |
| 1527 | class MockURLRequestInterceptor : public URLRequestInterceptor { |
| 1528 | public: |
| 1529 | // Static getters for canned response header and data strings. |
| 1530 | static std::string ok_data() { |
| 1531 | return URLRequestTestJob::test_data_1(); |
| 1532 | } |
| 1533 | |
| 1534 | static std::string ok_headers() { |
| 1535 | return URLRequestTestJob::test_headers(); |
| 1536 | } |
| 1537 | |
| 1538 | static std::string redirect_data() { |
| 1539 | return std::string(); |
| 1540 | } |
| 1541 | |
| 1542 | static std::string redirect_headers() { |
| 1543 | return URLRequestTestJob::test_redirect_headers(); |
| 1544 | } |
| 1545 | |
| 1546 | static std::string error_data() { |
| 1547 | return std::string("ohhh nooooo mr. bill!"); |
| 1548 | } |
| 1549 | |
| 1550 | static std::string error_headers() { |
| 1551 | return URLRequestTestJob::test_error_headers(); |
| 1552 | } |
| 1553 | |
| 1554 | MockURLRequestInterceptor() |
| 1555 | : intercept_main_request_(false), restart_main_request_(false), |
| 1556 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 1557 | simulate_main_network_error_(false), |
| 1558 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 1559 | intercept_final_response_(false), cancel_final_request_(false), |
| 1560 | use_url_request_http_job_(false), |
| 1561 | did_intercept_main_(false), did_restart_main_(false), |
| 1562 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 1563 | did_simulate_error_main_(false), |
| 1564 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 1565 | did_intercept_final_(false), did_cancel_final_(false) { |
| 1566 | } |
| 1567 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1568 | ~MockURLRequestInterceptor() override = default; |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1569 | |
| 1570 | // URLRequestInterceptor implementation: |
| 1571 | URLRequestJob* MaybeInterceptRequest( |
| 1572 | URLRequest* request, |
| 1573 | NetworkDelegate* network_delegate) const override { |
| 1574 | if (restart_main_request_) { |
| 1575 | restart_main_request_ = false; |
| 1576 | did_restart_main_ = true; |
| 1577 | return new RestartTestJob(request, network_delegate); |
| 1578 | } |
| 1579 | if (cancel_main_request_) { |
| 1580 | cancel_main_request_ = false; |
| 1581 | did_cancel_main_ = true; |
| 1582 | return new CancelTestJob(request, network_delegate); |
| 1583 | } |
| 1584 | if (cancel_then_restart_main_request_) { |
| 1585 | cancel_then_restart_main_request_ = false; |
| 1586 | did_cancel_then_restart_main_ = true; |
| 1587 | return new CancelThenRestartTestJob(request, network_delegate); |
| 1588 | } |
| 1589 | if (simulate_main_network_error_) { |
| 1590 | simulate_main_network_error_ = false; |
| 1591 | did_simulate_error_main_ = true; |
| 1592 | if (use_url_request_http_job_) { |
| 1593 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1594 | } |
| 1595 | // This job will result in error since the requested URL is not one of the |
| 1596 | // URLs supported by these tests. |
| 1597 | return new URLRequestTestJob(request, network_delegate, true); |
| 1598 | } |
| 1599 | if (!intercept_main_request_) |
| 1600 | return nullptr; |
| 1601 | intercept_main_request_ = false; |
| 1602 | did_intercept_main_ = true; |
| 1603 | URLRequestTestJob* job = new URLRequestTestJob(request, |
| 1604 | network_delegate, |
| 1605 | main_headers_, |
| 1606 | main_data_, |
| 1607 | true); |
| 1608 | job->set_load_timing_info(main_request_load_timing_info_); |
| 1609 | return job; |
| 1610 | } |
| 1611 | |
| 1612 | URLRequestJob* MaybeInterceptRedirect(URLRequest* request, |
| 1613 | NetworkDelegate* network_delegate, |
| 1614 | const GURL& location) const override { |
| 1615 | if (cancel_redirect_request_) { |
| 1616 | cancel_redirect_request_ = false; |
| 1617 | did_cancel_redirect_ = true; |
| 1618 | return new CancelTestJob(request, network_delegate); |
| 1619 | } |
| 1620 | if (!intercept_redirect_) |
| 1621 | return nullptr; |
| 1622 | intercept_redirect_ = false; |
| 1623 | did_intercept_redirect_ = true; |
| 1624 | if (use_url_request_http_job_) { |
| 1625 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1626 | } |
| 1627 | return new URLRequestTestJob(request, |
| 1628 | network_delegate, |
| 1629 | redirect_headers_, |
| 1630 | redirect_data_, |
| 1631 | true); |
| 1632 | } |
| 1633 | |
| 1634 | URLRequestJob* MaybeInterceptResponse( |
| 1635 | URLRequest* request, |
| 1636 | NetworkDelegate* network_delegate) const override { |
| 1637 | if (cancel_final_request_) { |
| 1638 | cancel_final_request_ = false; |
| 1639 | did_cancel_final_ = true; |
| 1640 | return new CancelTestJob(request, network_delegate); |
| 1641 | } |
| 1642 | if (!intercept_final_response_) |
| 1643 | return nullptr; |
| 1644 | intercept_final_response_ = false; |
| 1645 | did_intercept_final_ = true; |
| 1646 | if (use_url_request_http_job_) { |
| 1647 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1648 | } |
| 1649 | return new URLRequestTestJob(request, |
| 1650 | network_delegate, |
| 1651 | final_headers_, |
| 1652 | final_data_, |
| 1653 | true); |
| 1654 | } |
| 1655 | |
| 1656 | void set_intercept_main_request(bool intercept_main_request) { |
| 1657 | intercept_main_request_ = intercept_main_request; |
| 1658 | } |
| 1659 | |
| 1660 | void set_main_headers(const std::string& main_headers) { |
| 1661 | main_headers_ = main_headers; |
| 1662 | } |
| 1663 | |
| 1664 | void set_main_data(const std::string& main_data) { |
| 1665 | main_data_ = main_data; |
| 1666 | } |
| 1667 | |
| 1668 | void set_main_request_load_timing_info( |
| 1669 | const LoadTimingInfo& main_request_load_timing_info) { |
| 1670 | main_request_load_timing_info_ = main_request_load_timing_info; |
| 1671 | } |
| 1672 | |
| 1673 | void set_restart_main_request(bool restart_main_request) { |
| 1674 | restart_main_request_ = restart_main_request; |
| 1675 | } |
| 1676 | |
| 1677 | void set_cancel_main_request(bool cancel_main_request) { |
| 1678 | cancel_main_request_ = cancel_main_request; |
| 1679 | } |
| 1680 | |
| 1681 | void set_cancel_then_restart_main_request( |
| 1682 | bool cancel_then_restart_main_request) { |
| 1683 | cancel_then_restart_main_request_ = cancel_then_restart_main_request; |
| 1684 | } |
| 1685 | |
| 1686 | void set_simulate_main_network_error(bool simulate_main_network_error) { |
| 1687 | simulate_main_network_error_ = simulate_main_network_error; |
| 1688 | } |
| 1689 | |
| 1690 | void set_intercept_redirect(bool intercept_redirect) { |
| 1691 | intercept_redirect_ = intercept_redirect; |
| 1692 | } |
| 1693 | |
| 1694 | void set_redirect_headers(const std::string& redirect_headers) { |
| 1695 | redirect_headers_ = redirect_headers; |
| 1696 | } |
| 1697 | |
| 1698 | void set_redirect_data(const std::string& redirect_data) { |
| 1699 | redirect_data_ = redirect_data; |
| 1700 | } |
| 1701 | |
| 1702 | void set_cancel_redirect_request(bool cancel_redirect_request) { |
| 1703 | cancel_redirect_request_ = cancel_redirect_request; |
| 1704 | } |
| 1705 | |
| 1706 | void set_intercept_final_response(bool intercept_final_response) { |
| 1707 | intercept_final_response_ = intercept_final_response; |
| 1708 | } |
| 1709 | |
| 1710 | void set_final_headers(const std::string& final_headers) { |
| 1711 | final_headers_ = final_headers; |
| 1712 | } |
| 1713 | |
| 1714 | void set_final_data(const std::string& final_data) { |
| 1715 | final_data_ = final_data; |
| 1716 | } |
| 1717 | |
| 1718 | void set_cancel_final_request(bool cancel_final_request) { |
| 1719 | cancel_final_request_ = cancel_final_request; |
| 1720 | } |
| 1721 | |
| 1722 | void set_use_url_request_http_job(bool use_url_request_http_job) { |
| 1723 | use_url_request_http_job_ = use_url_request_http_job; |
| 1724 | } |
| 1725 | |
| 1726 | bool did_intercept_main() const { |
| 1727 | return did_intercept_main_; |
| 1728 | } |
| 1729 | |
| 1730 | bool did_restart_main() const { |
| 1731 | return did_restart_main_; |
| 1732 | } |
| 1733 | |
| 1734 | bool did_cancel_main() const { |
| 1735 | return did_cancel_main_; |
| 1736 | } |
| 1737 | |
| 1738 | bool did_cancel_then_restart_main() const { |
| 1739 | return did_cancel_then_restart_main_; |
| 1740 | } |
| 1741 | |
| 1742 | bool did_simulate_error_main() const { |
| 1743 | return did_simulate_error_main_; |
| 1744 | } |
| 1745 | |
| 1746 | bool did_intercept_redirect() const { |
| 1747 | return did_intercept_redirect_; |
| 1748 | } |
| 1749 | |
| 1750 | bool did_cancel_redirect() const { |
| 1751 | return did_cancel_redirect_; |
| 1752 | } |
| 1753 | |
| 1754 | bool did_intercept_final() const { |
| 1755 | return did_intercept_final_; |
| 1756 | } |
| 1757 | |
| 1758 | bool did_cancel_final() const { |
| 1759 | return did_cancel_final_; |
| 1760 | } |
| 1761 | |
| 1762 | private: |
| 1763 | // Indicate whether to intercept the main request, and if so specify the |
| 1764 | // response to return and the LoadTimingInfo to use. |
| 1765 | mutable bool intercept_main_request_; |
| 1766 | mutable std::string main_headers_; |
| 1767 | mutable std::string main_data_; |
| 1768 | mutable LoadTimingInfo main_request_load_timing_info_; |
| 1769 | |
| 1770 | // These indicate actions that can be taken within MaybeInterceptRequest. |
| 1771 | mutable bool restart_main_request_; |
| 1772 | mutable bool cancel_main_request_; |
| 1773 | mutable bool cancel_then_restart_main_request_; |
| 1774 | mutable bool simulate_main_network_error_; |
| 1775 | |
| 1776 | // Indicate whether to intercept redirects, and if so specify the response to |
| 1777 | // return. |
| 1778 | mutable bool intercept_redirect_; |
| 1779 | mutable std::string redirect_headers_; |
| 1780 | mutable std::string redirect_data_; |
| 1781 | |
| 1782 | // Cancel the request within MaybeInterceptRedirect. |
| 1783 | mutable bool cancel_redirect_request_; |
| 1784 | |
| 1785 | // Indicate whether to intercept the final response, and if so specify the |
| 1786 | // response to return. |
| 1787 | mutable bool intercept_final_response_; |
| 1788 | mutable std::string final_headers_; |
| 1789 | mutable std::string final_data_; |
| 1790 | |
| 1791 | // Cancel the final request within MaybeInterceptResponse. |
| 1792 | mutable bool cancel_final_request_; |
| 1793 | |
| 1794 | // Instruct the interceptor to use a real URLRequestHTTPJob. |
| 1795 | mutable bool use_url_request_http_job_; |
| 1796 | |
| 1797 | // These indicate if the interceptor did something or not. |
| 1798 | mutable bool did_intercept_main_; |
| 1799 | mutable bool did_restart_main_; |
| 1800 | mutable bool did_cancel_main_; |
| 1801 | mutable bool did_cancel_then_restart_main_; |
| 1802 | mutable bool did_simulate_error_main_; |
| 1803 | mutable bool did_intercept_redirect_; |
| 1804 | mutable bool did_cancel_redirect_; |
| 1805 | mutable bool did_intercept_final_; |
| 1806 | mutable bool did_cancel_final_; |
| 1807 | }; |
| 1808 | |
| 1809 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
| 1810 | class URLRequestInterceptorTest : public URLRequestTest { |
| 1811 | public: |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1812 | URLRequestInterceptorTest() : URLRequestTest(), interceptor_(nullptr) {} |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1813 | |
| 1814 | ~URLRequestInterceptorTest() override { |
| 1815 | // URLRequestJobs may post clean-up tasks on destruction. |
| 1816 | base::RunLoop().RunUntilIdle(); |
| 1817 | } |
| 1818 | |
| 1819 | void SetUpFactory() override { |
| 1820 | interceptor_ = new MockURLRequestInterceptor(); |
| 1821 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1822 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1823 | } |
| 1824 | |
| 1825 | MockURLRequestInterceptor* interceptor() const { |
| 1826 | return interceptor_; |
| 1827 | } |
| 1828 | |
| 1829 | private: |
| 1830 | MockURLRequestInterceptor* interceptor_; |
| 1831 | }; |
| 1832 | |
| 1833 | TEST_F(URLRequestInterceptorTest, Intercept) { |
| 1834 | // Intercept the main request and respond with a simple response. |
| 1835 | interceptor()->set_intercept_main_request(true); |
| 1836 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1837 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1838 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1839 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1840 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1841 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1842 | base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); |
| 1843 | base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); |
| 1844 | base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); |
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 1845 | req->SetUserData(&user_data0, base::WrapUnique(user_data0)); |
avi | 0d99147 | 2017-04-27 07:04:04 | [diff] [blame] | 1846 | req->SetUserData(&user_data1, base::WrapUnique(user_data1)); |
| 1847 | req->SetUserData(&user_data2, base::WrapUnique(user_data2)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1848 | req->set_method("GET"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1849 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1850 | d.RunUntilComplete(); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1851 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1852 | // Make sure we can retrieve our specific user data. |
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 1853 | EXPECT_EQ(user_data0, req->GetUserData(&user_data0)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1854 | EXPECT_EQ(user_data1, req->GetUserData(&user_data1)); |
| 1855 | EXPECT_EQ(user_data2, req->GetUserData(&user_data2)); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1856 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1857 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1858 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1859 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1860 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1861 | EXPECT_EQ(1, d.response_started_count()); |
| 1862 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1863 | } |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1864 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1865 | TEST_F(URLRequestInterceptorTest, InterceptRedirect) { |
| 1866 | // Intercept the main request and respond with a redirect. |
| 1867 | interceptor()->set_intercept_main_request(true); |
| 1868 | interceptor()->set_main_headers( |
| 1869 | MockURLRequestInterceptor::redirect_headers()); |
| 1870 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 1871 | |
| 1872 | // Intercept that redirect and respond with a final OK response. |
| 1873 | interceptor()->set_intercept_redirect(true); |
| 1874 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 1875 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 1876 | |
| 1877 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1878 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1879 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1880 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1881 | req->set_method("GET"); |
| 1882 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1883 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1884 | |
| 1885 | // Check that the interceptor got called as expected. |
| 1886 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1887 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 1888 | |
| 1889 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1890 | int status = d.request_status(); |
| 1891 | EXPECT_EQ(OK, status); |
| 1892 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1893 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1894 | |
| 1895 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1896 | EXPECT_EQ(1, d.response_started_count()); |
| 1897 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1898 | } |
| 1899 | |
| 1900 | TEST_F(URLRequestInterceptorTest, InterceptServerError) { |
| 1901 | // Intercept the main request to generate a server error response. |
| 1902 | interceptor()->set_intercept_main_request(true); |
| 1903 | interceptor()->set_main_headers(MockURLRequestInterceptor::error_headers()); |
| 1904 | interceptor()->set_main_data(MockURLRequestInterceptor::error_data()); |
| 1905 | |
| 1906 | // Intercept that error and respond with an OK response. |
| 1907 | interceptor()->set_intercept_final_response(true); |
| 1908 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1909 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1910 | |
| 1911 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1912 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1913 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1914 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1915 | req->set_method("GET"); |
| 1916 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1917 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1918 | |
| 1919 | // Check that the interceptor got called as expected. |
| 1920 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1921 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1922 | |
| 1923 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1924 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1925 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1926 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1927 | EXPECT_EQ(1, d.response_started_count()); |
| 1928 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1929 | } |
| 1930 | |
| 1931 | TEST_F(URLRequestInterceptorTest, InterceptNetworkError) { |
| 1932 | // Intercept the main request to simulate a network error. |
| 1933 | interceptor()->set_simulate_main_network_error(true); |
| 1934 | |
| 1935 | // Intercept that error and respond with an OK response. |
| 1936 | interceptor()->set_intercept_final_response(true); |
| 1937 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1938 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1939 | |
| 1940 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1941 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1942 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1943 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1944 | req->set_method("GET"); |
| 1945 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1946 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1947 | |
| 1948 | // Check that the interceptor got called as expected. |
| 1949 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 1950 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1951 | |
| 1952 | // Check that we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1953 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1954 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1955 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1956 | EXPECT_EQ(1, d.response_started_count()); |
| 1957 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1958 | } |
| 1959 | |
| 1960 | TEST_F(URLRequestInterceptorTest, InterceptRestartRequired) { |
| 1961 | // Restart the main request. |
| 1962 | interceptor()->set_restart_main_request(true); |
| 1963 | |
| 1964 | // then intercept the new main request and respond with an OK response |
| 1965 | interceptor()->set_intercept_main_request(true); |
| 1966 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1967 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
| 1968 | |
| 1969 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1970 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1971 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1972 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1973 | req->set_method("GET"); |
| 1974 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1975 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1976 | |
| 1977 | // Check that the interceptor got called as expected. |
| 1978 | EXPECT_TRUE(interceptor()->did_restart_main()); |
| 1979 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1980 | |
| 1981 | // Check that we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1982 | int status = d.request_status(); |
| 1983 | EXPECT_EQ(OK, status); |
| 1984 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1985 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1986 | |
| 1987 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1988 | EXPECT_EQ(1, d.response_started_count()); |
| 1989 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1990 | } |
| 1991 | |
| 1992 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelMain) { |
| 1993 | // Intercept the main request and cancel from within the restarted job. |
| 1994 | interceptor()->set_cancel_main_request(true); |
| 1995 | |
| 1996 | // Set up to intercept the final response and override it with an OK response. |
| 1997 | interceptor()->set_intercept_final_response(true); |
| 1998 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1999 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 2000 | |
| 2001 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2002 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2003 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2004 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2005 | req->set_method("GET"); |
| 2006 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2007 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2008 | |
| 2009 | // Check that the interceptor got called as expected. |
| 2010 | EXPECT_TRUE(interceptor()->did_cancel_main()); |
| 2011 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2012 | |
| 2013 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2014 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2015 | } |
| 2016 | |
| 2017 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelRedirect) { |
| 2018 | // Intercept the main request and respond with a redirect. |
| 2019 | interceptor()->set_intercept_main_request(true); |
| 2020 | interceptor()->set_main_headers( |
| 2021 | MockURLRequestInterceptor::redirect_headers()); |
| 2022 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 2023 | |
| 2024 | // Intercept the redirect and cancel from within that job. |
| 2025 | interceptor()->set_cancel_redirect_request(true); |
| 2026 | |
| 2027 | // Set up to intercept the final response and override it with an OK response. |
| 2028 | interceptor()->set_intercept_final_response(true); |
| 2029 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2030 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 2031 | |
| 2032 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2033 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2034 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2035 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2036 | req->set_method("GET"); |
| 2037 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2038 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2039 | |
| 2040 | // Check that the interceptor got called as expected. |
| 2041 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 2042 | EXPECT_TRUE(interceptor()->did_cancel_redirect()); |
| 2043 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2044 | |
| 2045 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2046 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelFinal) { |
| 2050 | // Intercept the main request to simulate a network error. |
| 2051 | interceptor()->set_simulate_main_network_error(true); |
| 2052 | |
| 2053 | // Set up to intercept final the response and cancel from within that job. |
| 2054 | interceptor()->set_cancel_final_request(true); |
| 2055 | |
| 2056 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2057 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2058 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2059 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2060 | req->set_method("GET"); |
| 2061 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2062 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2063 | |
| 2064 | // Check that the interceptor got called as expected. |
| 2065 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 2066 | EXPECT_TRUE(interceptor()->did_cancel_final()); |
| 2067 | |
| 2068 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2069 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelInRestart) { |
| 2073 | // Intercept the main request and cancel then restart from within that job. |
| 2074 | interceptor()->set_cancel_then_restart_main_request(true); |
| 2075 | |
| 2076 | // Set up to intercept the final response and override it with an OK response. |
| 2077 | interceptor()->set_intercept_final_response(true); |
bengr | b50d631e | 2014-11-17 22:50:50 | [diff] [blame] | 2078 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2079 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2080 | |
| 2081 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2082 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2083 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2084 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2085 | req->set_method("GET"); |
| 2086 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2087 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2088 | |
| 2089 | // Check that the interceptor got called as expected. |
| 2090 | EXPECT_TRUE(interceptor()->did_cancel_then_restart_main()); |
| 2091 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2092 | |
| 2093 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2094 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not |
| 2098 | // reused. |connect_time_flags| is used to indicate if there should be dns |
| 2099 | // or SSL times, and |used_proxy| is used for proxy times. |
| 2100 | LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now, |
| 2101 | int connect_time_flags, |
| 2102 | bool used_proxy) { |
| 2103 | LoadTimingInfo load_timing; |
| 2104 | load_timing.socket_log_id = 1; |
| 2105 | |
| 2106 | if (used_proxy) { |
| 2107 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 2108 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 2109 | } |
| 2110 | |
| 2111 | LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing; |
| 2112 | if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) { |
| 2113 | connect_timing.dns_start = now + base::TimeDelta::FromDays(3); |
| 2114 | connect_timing.dns_end = now + base::TimeDelta::FromDays(4); |
| 2115 | } |
| 2116 | connect_timing.connect_start = now + base::TimeDelta::FromDays(5); |
| 2117 | if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) { |
| 2118 | connect_timing.ssl_start = now + base::TimeDelta::FromDays(6); |
| 2119 | connect_timing.ssl_end = now + base::TimeDelta::FromDays(7); |
| 2120 | } |
| 2121 | connect_timing.connect_end = now + base::TimeDelta::FromDays(8); |
| 2122 | |
| 2123 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 2124 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 2125 | load_timing.receive_headers_start = now + base::TimeDelta::FromDays(11); |
| 2126 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(12); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2127 | return load_timing; |
| 2128 | } |
| 2129 | |
| 2130 | // Same as above, but in the case of a reused socket. |
| 2131 | LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now, |
| 2132 | bool used_proxy) { |
| 2133 | LoadTimingInfo load_timing; |
| 2134 | load_timing.socket_log_id = 1; |
| 2135 | load_timing.socket_reused = true; |
| 2136 | |
| 2137 | if (used_proxy) { |
| 2138 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 2139 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 2140 | } |
| 2141 | |
| 2142 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 2143 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 2144 | load_timing.receive_headers_start = now + base::TimeDelta::FromDays(11); |
| 2145 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(12); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2146 | return load_timing; |
| 2147 | } |
| 2148 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2149 | LoadTimingInfo RunURLRequestInterceptorLoadTimingTest( |
| 2150 | const LoadTimingInfo& job_load_timing, |
| 2151 | const URLRequestContext& context, |
| 2152 | MockURLRequestInterceptor* interceptor) { |
| 2153 | interceptor->set_intercept_main_request(true); |
| 2154 | interceptor->set_main_request_load_timing_info(job_load_timing); |
| 2155 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2156 | std::unique_ptr<URLRequest> req( |
| 2157 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 2158 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2159 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2160 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2161 | |
| 2162 | LoadTimingInfo resulting_load_timing; |
| 2163 | req->GetLoadTimingInfo(&resulting_load_timing); |
| 2164 | |
| 2165 | // None of these should be modified by the URLRequest. |
| 2166 | EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); |
| 2167 | EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); |
| 2168 | EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); |
| 2169 | EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 2170 | EXPECT_EQ(job_load_timing.receive_headers_start, |
| 2171 | resulting_load_timing.receive_headers_start); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2172 | EXPECT_EQ(job_load_timing.receive_headers_end, |
| 2173 | resulting_load_timing.receive_headers_end); |
caseq | e8340bc9 | 2016-04-20 00:02:57 | [diff] [blame] | 2174 | EXPECT_EQ(job_load_timing.push_start, resulting_load_timing.push_start); |
| 2175 | EXPECT_EQ(job_load_timing.push_end, resulting_load_timing.push_end); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2176 | |
| 2177 | return resulting_load_timing; |
| 2178 | } |
| 2179 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2180 | // Basic test that the intercept + load timing tests work. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2181 | TEST_F(URLRequestInterceptorTest, InterceptLoadTiming) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2182 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2183 | LoadTimingInfo job_load_timing = |
| 2184 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); |
| 2185 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2186 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2187 | RunURLRequestInterceptorLoadTimingTest( |
| 2188 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2189 | |
| 2190 | // Nothing should have been changed by the URLRequest. |
| 2191 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 2192 | load_timing_result.proxy_resolve_start); |
| 2193 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 2194 | load_timing_result.proxy_resolve_end); |
| 2195 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 2196 | load_timing_result.connect_timing.dns_start); |
| 2197 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 2198 | load_timing_result.connect_timing.dns_end); |
| 2199 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 2200 | load_timing_result.connect_timing.connect_start); |
| 2201 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 2202 | load_timing_result.connect_timing.connect_end); |
| 2203 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 2204 | load_timing_result.connect_timing.ssl_start); |
| 2205 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 2206 | load_timing_result.connect_timing.ssl_end); |
| 2207 | |
| 2208 | // Redundant sanity check. |
| 2209 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2210 | } |
| 2211 | |
| 2212 | // Another basic test, with proxy and SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2213 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2214 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2215 | LoadTimingInfo job_load_timing = |
| 2216 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); |
| 2217 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2218 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2219 | RunURLRequestInterceptorLoadTimingTest( |
| 2220 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2221 | |
| 2222 | // Nothing should have been changed by the URLRequest. |
| 2223 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 2224 | load_timing_result.proxy_resolve_start); |
| 2225 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 2226 | load_timing_result.proxy_resolve_end); |
| 2227 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 2228 | load_timing_result.connect_timing.dns_start); |
| 2229 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 2230 | load_timing_result.connect_timing.dns_end); |
| 2231 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 2232 | load_timing_result.connect_timing.connect_start); |
| 2233 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 2234 | load_timing_result.connect_timing.connect_end); |
| 2235 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 2236 | load_timing_result.connect_timing.ssl_start); |
| 2237 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 2238 | load_timing_result.connect_timing.ssl_end); |
| 2239 | |
| 2240 | // Redundant sanity check. |
| 2241 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2242 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 2243 | } |
| 2244 | |
| 2245 | // Make sure that URLRequest correctly adjusts proxy times when they're before |
| 2246 | // |request_start|, due to already having a connected socket. This happens in |
[email protected] | cf4cae3 | 2014-05-27 00:39:10 | [diff] [blame] | 2247 | // the case of reusing a SPDY session. The connected socket is not considered |
| 2248 | // reused in this test (May be a preconnect). |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2249 | // |
| 2250 | // 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] | 2251 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyProxyResolution) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2252 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2253 | LoadTimingInfo job_load_timing = |
| 2254 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); |
| 2255 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); |
| 2256 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); |
| 2257 | job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); |
| 2258 | job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); |
| 2259 | job_load_timing.connect_timing.connect_start = |
| 2260 | now - base::TimeDelta::FromDays(2); |
| 2261 | job_load_timing.connect_timing.connect_end = |
| 2262 | now - base::TimeDelta::FromDays(1); |
| 2263 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2264 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2265 | RunURLRequestInterceptorLoadTimingTest( |
| 2266 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2267 | |
| 2268 | // Proxy times, connect times, and DNS times should all be replaced with |
| 2269 | // request_start. |
| 2270 | EXPECT_EQ(load_timing_result.request_start, |
| 2271 | load_timing_result.proxy_resolve_start); |
| 2272 | EXPECT_EQ(load_timing_result.request_start, |
| 2273 | load_timing_result.proxy_resolve_end); |
| 2274 | EXPECT_EQ(load_timing_result.request_start, |
| 2275 | load_timing_result.connect_timing.dns_start); |
| 2276 | EXPECT_EQ(load_timing_result.request_start, |
| 2277 | load_timing_result.connect_timing.dns_end); |
| 2278 | EXPECT_EQ(load_timing_result.request_start, |
| 2279 | load_timing_result.connect_timing.connect_start); |
| 2280 | EXPECT_EQ(load_timing_result.request_start, |
| 2281 | load_timing_result.connect_timing.connect_end); |
| 2282 | |
| 2283 | // Other times should have been left null. |
| 2284 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2285 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 2286 | } |
| 2287 | |
| 2288 | // Same as above, but in the reused case. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2289 | TEST_F(URLRequestInterceptorTest, |
| 2290 | InterceptLoadTimingEarlyProxyResolutionReused) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2291 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2292 | LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); |
| 2293 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); |
| 2294 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); |
| 2295 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2296 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2297 | RunURLRequestInterceptorLoadTimingTest( |
| 2298 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2299 | |
| 2300 | // Proxy times and connect times should all be replaced with request_start. |
| 2301 | EXPECT_EQ(load_timing_result.request_start, |
| 2302 | load_timing_result.proxy_resolve_start); |
| 2303 | EXPECT_EQ(load_timing_result.request_start, |
| 2304 | load_timing_result.proxy_resolve_end); |
| 2305 | |
| 2306 | // Other times should have been left null. |
| 2307 | TestLoadTimingReusedWithProxy(load_timing_result); |
| 2308 | } |
| 2309 | |
| 2310 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2311 | // |request_start|, due to reusing a connected socket. The connected socket is |
| 2312 | // not considered reused in this test (May be a preconnect). |
| 2313 | // |
| 2314 | // To mix things up, the request has SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2315 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnect) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2316 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2317 | LoadTimingInfo job_load_timing = |
| 2318 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); |
| 2319 | job_load_timing.connect_timing.connect_start = |
| 2320 | now - base::TimeDelta::FromDays(1); |
| 2321 | job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); |
| 2322 | job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); |
| 2323 | job_load_timing.connect_timing.connect_end = |
| 2324 | now - base::TimeDelta::FromDays(4); |
| 2325 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2326 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2327 | RunURLRequestInterceptorLoadTimingTest( |
| 2328 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2329 | |
| 2330 | // Connect times, and SSL times should be replaced with request_start. |
| 2331 | EXPECT_EQ(load_timing_result.request_start, |
| 2332 | load_timing_result.connect_timing.connect_start); |
| 2333 | EXPECT_EQ(load_timing_result.request_start, |
| 2334 | load_timing_result.connect_timing.ssl_start); |
| 2335 | EXPECT_EQ(load_timing_result.request_start, |
| 2336 | load_timing_result.connect_timing.ssl_end); |
| 2337 | EXPECT_EQ(load_timing_result.request_start, |
| 2338 | load_timing_result.connect_timing.connect_end); |
| 2339 | |
| 2340 | // Other times should have been left null. |
| 2341 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); |
| 2342 | } |
| 2343 | |
| 2344 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2345 | // |request_start|, due to reusing a connected socket in the case that there |
| 2346 | // are also proxy times. The connected socket is not considered reused in this |
| 2347 | // test (May be a preconnect). |
| 2348 | // |
| 2349 | // In this test, there are no SSL or DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2350 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnectWithProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2351 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2352 | LoadTimingInfo job_load_timing = |
| 2353 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); |
| 2354 | job_load_timing.connect_timing.connect_start = |
| 2355 | now - base::TimeDelta::FromDays(1); |
| 2356 | job_load_timing.connect_timing.connect_end = |
| 2357 | now - base::TimeDelta::FromDays(2); |
| 2358 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2359 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2360 | RunURLRequestInterceptorLoadTimingTest( |
| 2361 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2362 | |
| 2363 | // Connect times should be replaced with proxy_resolve_end. |
| 2364 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2365 | load_timing_result.connect_timing.connect_start); |
| 2366 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2367 | load_timing_result.connect_timing.connect_end); |
| 2368 | |
| 2369 | // Other times should have been left null. |
| 2370 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2371 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 2372 | } |
| 2373 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2374 | // Check that two different URL requests have different identifiers. |
| 2375 | TEST_F(URLRequestTest, Identifiers) { |
| 2376 | TestDelegate d; |
| 2377 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2378 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2379 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2380 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2381 | std::unique_ptr<URLRequest> other_req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2382 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2383 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2384 | |
mmenke | 19378d2 | 2014-09-09 04:12:59 | [diff] [blame] | 2385 | ASSERT_NE(req->identifier(), other_req->identifier()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2386 | } |
| 2387 | |
blundell | b8163592f | 2015-12-16 14:22:42 | [diff] [blame] | 2388 | #if defined(OS_IOS) |
| 2389 | // TODO(droger): Check that a failure to connect to the proxy is reported to |
| 2390 | // the network delegate. crbug.com/496743 |
| 2391 | #define MAYBE_NetworkDelegateProxyError DISABLED_NetworkDelegateProxyError |
| 2392 | #else |
| 2393 | #define MAYBE_NetworkDelegateProxyError NetworkDelegateProxyError |
| 2394 | #endif |
| 2395 | TEST_F(URLRequestTest, MAYBE_NetworkDelegateProxyError) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2396 | MockHostResolver host_resolver; |
| 2397 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 2398 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2399 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2400 | TestURLRequestContextWithProxy context("myproxy:70", &network_delegate); |
| 2401 | |
| 2402 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2403 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2404 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2405 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2406 | req->set_method("GET"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2407 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2408 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2409 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2410 | |
| 2411 | // Check we see a failed request. |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 2412 | // The proxy server should be set before failure. |
| 2413 | EXPECT_EQ(ProxyServer::FromPacString("PROXY myproxy:70"), |
| 2414 | req->proxy_server()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2415 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2416 | |
| 2417 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2418 | EXPECT_THAT(network_delegate.last_error(), |
| 2419 | IsError(ERR_PROXY_CONNECTION_FAILED)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2420 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2421 | } |
| 2422 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2423 | // Make sure that NetworkDelegate::NotifyCompleted is called if |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2424 | // content is empty. |
| 2425 | TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) { |
| 2426 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2427 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2428 | GURL("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2429 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2430 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2431 | EXPECT_EQ("", d.data_received()); |
| 2432 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 2433 | } |
| 2434 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2435 | // Make sure that SetPriority actually sets the URLRequest's priority |
| 2436 | // correctly, both before and after start. |
| 2437 | TEST_F(URLRequestTest, SetPriorityBasic) { |
| 2438 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2439 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2440 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2441 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2442 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2443 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2444 | req->SetPriority(LOW); |
| 2445 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2446 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2447 | req->Start(); |
| 2448 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2449 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2450 | req->SetPriority(MEDIUM); |
| 2451 | EXPECT_EQ(MEDIUM, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2452 | } |
| 2453 | |
| 2454 | // Make sure that URLRequest calls SetPriority on a job before calling |
| 2455 | // Start on it. |
| 2456 | TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) { |
| 2457 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2458 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2459 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2460 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2461 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2462 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2463 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2464 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2465 | req.get(), &default_network_delegate_, &job_priority)); |
| 2466 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
| 2467 | EXPECT_EQ(DEFAULT_PRIORITY, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2468 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2469 | req->SetPriority(LOW); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2470 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2471 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2472 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2473 | } |
| 2474 | |
| 2475 | // Make sure that URLRequest passes on its priority updates to its |
| 2476 | // job. |
| 2477 | TEST_F(URLRequestTest, SetJobPriority) { |
| 2478 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2479 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2480 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2481 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2482 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2483 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2484 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2485 | req.get(), &default_network_delegate_, &job_priority)); |
| 2486 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2487 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2488 | req->SetPriority(LOW); |
| 2489 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2490 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2491 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2492 | req->SetPriority(MEDIUM); |
| 2493 | EXPECT_EQ(MEDIUM, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2494 | EXPECT_EQ(MEDIUM, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2495 | } |
| 2496 | |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2497 | // Setting the IGNORE_LIMITS load flag should be okay if the priority |
| 2498 | // is MAXIMUM_PRIORITY. |
| 2499 | TEST_F(URLRequestTest, PriorityIgnoreLimits) { |
| 2500 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2501 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2502 | GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY, &d, |
| 2503 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2504 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2505 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2506 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2507 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2508 | req.get(), &default_network_delegate_, &job_priority)); |
| 2509 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2510 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2511 | req->SetLoadFlags(LOAD_IGNORE_LIMITS); |
| 2512 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2513 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2514 | req->SetPriority(MAXIMUM_PRIORITY); |
| 2515 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2516 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2517 | req->Start(); |
| 2518 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2519 | EXPECT_EQ(MAXIMUM_PRIORITY, job_priority); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2520 | } |
| 2521 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2522 | namespace { |
| 2523 | |
| 2524 | // Less verbose way of running a simple testserver for the tests below. |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2525 | class HttpTestServer : public EmbeddedTestServer { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2526 | public: |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2527 | explicit HttpTestServer(const base::FilePath& document_root) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2528 | AddDefaultHandlers(document_root); |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2529 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2530 | |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2531 | HttpTestServer() { AddDefaultHandlers(base::FilePath()); } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2532 | }; |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2533 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2534 | } // namespace |
| 2535 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2536 | TEST_F(URLRequestTest, DelayedCookieCallback) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2537 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2538 | ASSERT_TRUE(test_server.Start()); |
| 2539 | |
| 2540 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2541 | std::unique_ptr<DelayedCookieMonster> delayed_cm(new DelayedCookieMonster()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2542 | context.set_cookie_store(delayed_cm.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2543 | |
| 2544 | // Set up a cookie. |
| 2545 | { |
| 2546 | TestNetworkDelegate network_delegate; |
| 2547 | context.set_network_delegate(&network_delegate); |
| 2548 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2549 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2550 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2551 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2552 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2553 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2554 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2555 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2556 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2557 | } |
| 2558 | |
| 2559 | // Verify that the cookie is set. |
| 2560 | { |
| 2561 | TestNetworkDelegate network_delegate; |
| 2562 | context.set_network_delegate(&network_delegate); |
| 2563 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2564 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2565 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2566 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2567 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2568 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2569 | |
| 2570 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2571 | != std::string::npos); |
| 2572 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2573 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2574 | } |
| 2575 | } |
| 2576 | |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 2577 | class FilteringTestLayeredNetworkDelegate : public LayeredNetworkDelegate { |
| 2578 | public: |
| 2579 | FilteringTestLayeredNetworkDelegate( |
| 2580 | std::unique_ptr<NetworkDelegate> network_delegate) |
| 2581 | : LayeredNetworkDelegate(std::move((network_delegate))), |
| 2582 | set_cookie_called_count_(0), |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 2583 | blocked_set_cookie_count_(0), |
| 2584 | block_get_cookies_(false), |
| 2585 | get_cookie_called_count_(0), |
| 2586 | blocked_get_cookie_count_(0) {} |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 2587 | ~FilteringTestLayeredNetworkDelegate() override = default; |
| 2588 | |
| 2589 | bool OnCanSetCookieInternal(const URLRequest& request, |
| 2590 | const net::CanonicalCookie& cookie, |
| 2591 | CookieOptions* options, |
| 2592 | bool allowed_from_caller) override { |
| 2593 | // Filter out cookies with the same name as |cookie_name_filter_| and |
| 2594 | // combine with |allowed_from_caller|. |
| 2595 | bool allowed = |
| 2596 | allowed_from_caller && !(cookie.Name() == cookie_name_filter_); |
| 2597 | |
| 2598 | ++set_cookie_called_count_; |
| 2599 | |
| 2600 | if (!allowed) |
| 2601 | ++blocked_set_cookie_count_; |
| 2602 | |
| 2603 | return allowed; |
| 2604 | } |
| 2605 | |
| 2606 | void SetCookieFilter(std::string filter) { |
| 2607 | cookie_name_filter_ = std::move(filter); |
| 2608 | } |
| 2609 | |
| 2610 | int set_cookie_called_count() { return set_cookie_called_count_; } |
| 2611 | |
| 2612 | int blocked_set_cookie_count() { return blocked_set_cookie_count_; } |
| 2613 | |
| 2614 | void ResetSetCookieCalledCount() { set_cookie_called_count_ = 0; } |
| 2615 | |
| 2616 | void ResetBlockedSetCookieCount() { blocked_set_cookie_count_ = 0; } |
| 2617 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 2618 | bool OnCanGetCookiesInternal(const URLRequest& request, |
| 2619 | const net::CookieList& cookie, |
| 2620 | bool allowed_from_caller) override { |
| 2621 | // Filter out cookies if |block_get_cookies_| is set and |
| 2622 | // combine with |allowed_from_caller|. |
| 2623 | bool allowed = allowed_from_caller && !block_get_cookies_; |
| 2624 | |
| 2625 | ++get_cookie_called_count_; |
| 2626 | |
| 2627 | if (!allowed) |
| 2628 | ++blocked_get_cookie_count_; |
| 2629 | |
| 2630 | return allowed; |
| 2631 | } |
| 2632 | |
| 2633 | void set_block_get_cookies() { block_get_cookies_ = true; } |
| 2634 | |
| 2635 | void unset_block_get_cookies() { block_get_cookies_ = false; } |
| 2636 | |
| 2637 | int get_cookie_called_count() const { return get_cookie_called_count_; } |
| 2638 | |
| 2639 | int blocked_get_cookie_count() const { return blocked_get_cookie_count_; } |
| 2640 | |
| 2641 | void ResetGetCookieCalledCount() { get_cookie_called_count_ = 0; } |
| 2642 | |
| 2643 | void ResetBlockedGetCookieCount() { blocked_get_cookie_count_ = 0; } |
| 2644 | |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 2645 | private: |
| 2646 | std::string cookie_name_filter_; |
| 2647 | int set_cookie_called_count_; |
| 2648 | int blocked_set_cookie_count_; |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 2649 | |
| 2650 | bool block_get_cookies_; |
| 2651 | int get_cookie_called_count_; |
| 2652 | int blocked_get_cookie_count_; |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 2653 | }; |
| 2654 | |
| 2655 | TEST_F(URLRequestTest, DelayedCookieCallbackAsync) { |
| 2656 | HttpTestServer test_server; |
| 2657 | ASSERT_TRUE(test_server.Start()); |
| 2658 | |
| 2659 | TestURLRequestContext async_context; |
| 2660 | std::unique_ptr<DelayedCookieMonster> delayed_cm = |
| 2661 | std::make_unique<DelayedCookieMonster>(); |
| 2662 | async_context.set_cookie_store(delayed_cm.get()); |
| 2663 | FilteringTestLayeredNetworkDelegate async_filter_network_delegate( |
| 2664 | std::make_unique<TestNetworkDelegate>()); |
| 2665 | async_filter_network_delegate.SetCookieFilter("CookieBlockedOnCanGetCookie"); |
| 2666 | async_context.set_network_delegate(&async_filter_network_delegate); |
| 2667 | TestDelegate async_delegate; |
| 2668 | |
| 2669 | TestURLRequestContext sync_context; |
| 2670 | std::unique_ptr<CookieMonster> cm = |
Nick Harper | 57142b1c | 2019-03-14 21:03:59 | [diff] [blame] | 2671 | std::make_unique<CookieMonster>(nullptr, nullptr); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 2672 | sync_context.set_cookie_store(cm.get()); |
| 2673 | FilteringTestLayeredNetworkDelegate sync_filter_network_delegate( |
| 2674 | std::make_unique<TestNetworkDelegate>()); |
| 2675 | sync_filter_network_delegate.SetCookieFilter("CookieBlockedOnCanGetCookie"); |
| 2676 | sync_context.set_network_delegate(&sync_filter_network_delegate); |
| 2677 | TestDelegate sync_delegate; |
| 2678 | |
| 2679 | // Add a secure cookie so we can try to set an insecure cookie and have |
| 2680 | // SetCanonicalCookie (and therefore SetCookieWithOptions) fail. |
| 2681 | GURL::Replacements replace_scheme; |
| 2682 | replace_scheme.SetSchemeStr("https"); |
| 2683 | GURL url = test_server.base_url().ReplaceComponents(replace_scheme); |
| 2684 | |
| 2685 | delayed_cm->SetCookieWithOptionsAsync(url, "AlreadySetCookie=1;Secure", |
| 2686 | CookieOptions(), |
| 2687 | CookieStore::SetCookiesCallback()); |
| 2688 | cm->SetCookieWithOptionsAsync(url, "AlreadySetCookie=1;Secure", |
| 2689 | CookieOptions(), |
| 2690 | CookieStore::SetCookiesCallback()); |
| 2691 | |
| 2692 | std::vector<std::string> cookie_lines( |
| 2693 | {// Fails on CanonicalCookie::Create for trying to create a secure cookie |
| 2694 | // on an insecure host. |
| 2695 | "CookieNotSet=1;Secure", |
| 2696 | // Fail in FilteringTestLayeredNetworkDelegate::CanGetCookie. |
| 2697 | "CookieBlockedOnCanGetCookie=1", |
| 2698 | // Fails in SetCanonicalCookie for trying to overwrite a secure cookie |
| 2699 | // with an insecure cookie. |
| 2700 | "AlreadySetCookie=1", |
| 2701 | // Succeeds and added cookie to store. Delayed (which makes the callback |
| 2702 | // run asynchronously) in DelayedCookieMonster. |
| 2703 | "CookieSet=1"}); |
| 2704 | |
| 2705 | for (auto first_cookie_line : cookie_lines) { |
| 2706 | for (auto second_cookie_line : cookie_lines) { |
| 2707 | // Run with the delayed cookie monster. |
| 2708 | std::unique_ptr<URLRequest> request = async_context.CreateRequest( |
| 2709 | test_server.GetURL("/set-cookie?" + first_cookie_line + "&" + |
| 2710 | second_cookie_line), |
| 2711 | DEFAULT_PRIORITY, &async_delegate, TRAFFIC_ANNOTATION_FOR_TESTS); |
| 2712 | |
| 2713 | request->Start(); |
| 2714 | async_delegate.RunUntilComplete(); |
| 2715 | EXPECT_THAT(async_delegate.request_status(), IsOk()); |
| 2716 | |
| 2717 | // Run with the regular cookie monster. |
| 2718 | request = sync_context.CreateRequest( |
| 2719 | test_server.GetURL("/set-cookie?" + first_cookie_line + "&" + |
| 2720 | second_cookie_line), |
| 2721 | DEFAULT_PRIORITY, &sync_delegate, TRAFFIC_ANNOTATION_FOR_TESTS); |
| 2722 | |
| 2723 | request->Start(); |
| 2724 | sync_delegate.RunUntilComplete(); |
| 2725 | EXPECT_THAT(sync_delegate.request_status(), IsOk()); |
| 2726 | |
| 2727 | int expected_set_cookie_count = 0; |
| 2728 | int expected_blocked_cookie_count = 0; |
| 2729 | |
| 2730 | if (first_cookie_line != "CookieNotSet=1;Secure") |
| 2731 | ++expected_set_cookie_count; |
| 2732 | if (second_cookie_line != "CookieNotSet=1;Secure") |
| 2733 | ++expected_set_cookie_count; |
| 2734 | |
| 2735 | if (first_cookie_line == "CookieBlockedOnCanGetCookie=1") |
| 2736 | ++expected_blocked_cookie_count; |
| 2737 | if (second_cookie_line == "CookieBlockedOnCanGetCookie=1") |
| 2738 | ++expected_blocked_cookie_count; |
| 2739 | |
| 2740 | EXPECT_EQ(expected_set_cookie_count, |
| 2741 | async_filter_network_delegate.set_cookie_called_count()); |
| 2742 | EXPECT_EQ(expected_blocked_cookie_count, |
| 2743 | async_filter_network_delegate.blocked_set_cookie_count()); |
| 2744 | |
| 2745 | EXPECT_EQ(expected_set_cookie_count, |
| 2746 | sync_filter_network_delegate.set_cookie_called_count()); |
| 2747 | EXPECT_EQ(expected_blocked_cookie_count, |
| 2748 | sync_filter_network_delegate.blocked_set_cookie_count()); |
| 2749 | |
| 2750 | async_filter_network_delegate.ResetSetCookieCalledCount(); |
| 2751 | async_filter_network_delegate.ResetBlockedSetCookieCount(); |
| 2752 | |
| 2753 | sync_filter_network_delegate.ResetSetCookieCalledCount(); |
| 2754 | sync_filter_network_delegate.ResetBlockedSetCookieCount(); |
| 2755 | } |
| 2756 | } |
| 2757 | } |
| 2758 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2759 | TEST_F(URLRequestTest, DoNotSendCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2760 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2761 | ASSERT_TRUE(test_server.Start()); |
| 2762 | |
| 2763 | // Set up a cookie. |
| 2764 | { |
| 2765 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2766 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2767 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2768 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2769 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2770 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2771 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2772 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2773 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2774 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2775 | } |
| 2776 | |
| 2777 | // Verify that the cookie is set. |
| 2778 | { |
| 2779 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2780 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2781 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2782 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2783 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2784 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2785 | req->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("CookieToNotSend=1") |
| 2789 | != std::string::npos); |
| 2790 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2791 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2792 | } |
| 2793 | |
| 2794 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 2795 | { |
| 2796 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2797 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2798 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2799 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2800 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2801 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2802 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
| 2803 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2804 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2805 | |
| 2806 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2807 | == std::string::npos); |
| 2808 | |
| 2809 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
| 2810 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2811 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2812 | } |
| 2813 | } |
| 2814 | |
| 2815 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2816 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2817 | ASSERT_TRUE(test_server.Start()); |
| 2818 | |
| 2819 | // Set up a cookie. |
| 2820 | { |
| 2821 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2822 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2823 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2824 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2825 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2826 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2827 | req->Start(); |
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 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2831 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2832 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2833 | } |
| 2834 | |
| 2835 | // Try to set-up another cookie and update the previous cookie. |
| 2836 | { |
| 2837 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2838 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2839 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2840 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2841 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2842 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2843 | req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES); |
| 2844 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2845 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2846 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2847 | |
| 2848 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
| 2849 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2850 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2851 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2852 | } |
| 2853 | |
| 2854 | // Verify the cookies weren't saved or updated. |
| 2855 | { |
| 2856 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2857 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2858 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2859 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2860 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2861 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2862 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2863 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2864 | |
| 2865 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2866 | == std::string::npos); |
| 2867 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2868 | != std::string::npos); |
| 2869 | |
| 2870 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2871 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2872 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2873 | } |
| 2874 | } |
| 2875 | |
| 2876 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2877 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2878 | ASSERT_TRUE(test_server.Start()); |
| 2879 | |
| 2880 | // Set up a cookie. |
| 2881 | { |
| 2882 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2883 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2884 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2885 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2886 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2887 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2888 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2889 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2890 | |
| 2891 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2892 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2893 | } |
| 2894 | |
| 2895 | // Verify that the cookie is set. |
| 2896 | { |
| 2897 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2898 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2899 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2900 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2901 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2902 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2903 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2904 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2905 | |
| 2906 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2907 | != std::string::npos); |
| 2908 | |
| 2909 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2910 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2911 | TestNetLogEntry::List entries; |
| 2912 | net_log_.GetEntries(&entries); |
| 2913 | for (const auto& entry : entries) { |
| 2914 | EXPECT_NE(entry.type, |
| 2915 | NetLogEventType::COOKIE_GET_BLOCKED_BY_NETWORK_DELEGATE); |
| 2916 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2917 | } |
| 2918 | |
| 2919 | // Verify that the cookie isn't sent. |
| 2920 | { |
| 2921 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2922 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2923 | TestDelegate d; |
| 2924 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2925 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2926 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2927 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2928 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2929 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2930 | |
| 2931 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2932 | == std::string::npos); |
| 2933 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2934 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2935 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2936 | TestNetLogEntry::List entries; |
| 2937 | net_log_.GetEntries(&entries); |
| 2938 | ExpectLogContainsSomewhereAfter( |
| 2939 | entries, 0, NetLogEventType::COOKIE_GET_BLOCKED_BY_NETWORK_DELEGATE, |
| 2940 | NetLogEventPhase::NONE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2941 | } |
| 2942 | } |
| 2943 | |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 2944 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 2945 | #if defined(OS_IOS) |
| 2946 | #define MAYBE_DoNotSaveCookies_ViaPolicy FLAKY_DoNotSaveCookies_ViaPolicy |
| 2947 | #else |
| 2948 | #define MAYBE_DoNotSaveCookies_ViaPolicy DoNotSaveCookies_ViaPolicy |
| 2949 | #endif |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2950 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2951 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2952 | ASSERT_TRUE(test_server.Start()); |
| 2953 | |
| 2954 | // Set up a cookie. |
| 2955 | { |
| 2956 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2957 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2958 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2959 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2960 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2961 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2962 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2963 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2964 | |
| 2965 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2966 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2967 | TestNetLogEntry::List entries; |
| 2968 | net_log_.GetEntries(&entries); |
| 2969 | for (const auto& entry : entries) { |
| 2970 | EXPECT_NE(entry.type, |
| 2971 | NetLogEventType::COOKIE_SET_BLOCKED_BY_NETWORK_DELEGATE); |
| 2972 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2973 | } |
| 2974 | |
| 2975 | // Try to set-up another cookie and update the previous cookie. |
| 2976 | { |
| 2977 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2978 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2979 | TestDelegate d; |
| 2980 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2981 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2982 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2983 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2984 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2985 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2986 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2987 | |
| 2988 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2989 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2990 | TestNetLogEntry::List entries; |
| 2991 | net_log_.GetEntries(&entries); |
| 2992 | ExpectLogContainsSomewhereAfter( |
| 2993 | entries, 0, NetLogEventType::COOKIE_SET_BLOCKED_BY_NETWORK_DELEGATE, |
| 2994 | NetLogEventPhase::NONE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2995 | } |
| 2996 | |
| 2997 | // Verify the cookies weren't saved or updated. |
| 2998 | { |
| 2999 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3000 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3001 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3002 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3003 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3004 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3005 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3006 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3007 | |
| 3008 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 3009 | == std::string::npos); |
| 3010 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 3011 | != std::string::npos); |
| 3012 | |
| 3013 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3014 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3015 | } |
| 3016 | } |
| 3017 | |
| 3018 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3019 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3020 | ASSERT_TRUE(test_server.Start()); |
| 3021 | |
| 3022 | // Set up an empty cookie. |
| 3023 | { |
| 3024 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3025 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3026 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3027 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3028 | test_server.GetURL("/set-cookie"), DEFAULT_PRIORITY, &d, |
| 3029 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3030 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3031 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3032 | |
| 3033 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3034 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3035 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 3036 | } |
| 3037 | } |
| 3038 | |
| 3039 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3040 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3041 | ASSERT_TRUE(test_server.Start()); |
| 3042 | |
| 3043 | // Set up a cookie. |
| 3044 | { |
| 3045 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3046 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3047 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3048 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3049 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3050 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3051 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3052 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3053 | |
| 3054 | EXPECT_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 is set. |
| 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; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3063 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3064 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3065 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3066 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3067 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3068 | |
| 3069 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 3070 | != std::string::npos); |
| 3071 | |
| 3072 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3073 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3074 | } |
| 3075 | |
| 3076 | // Verify that the cookie isn't sent. |
| 3077 | { |
| 3078 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3079 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3080 | TestDelegate d; |
| 3081 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3082 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3083 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3084 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3085 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3086 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3087 | |
| 3088 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 3089 | == std::string::npos); |
| 3090 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 3091 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3092 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3093 | } |
| 3094 | } |
| 3095 | |
| 3096 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3097 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3098 | ASSERT_TRUE(test_server.Start()); |
| 3099 | |
| 3100 | // Set up a cookie. |
| 3101 | { |
| 3102 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3103 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3104 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3105 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3106 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3107 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3108 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3109 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3110 | |
| 3111 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3112 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3113 | } |
| 3114 | |
| 3115 | // Try to set-up another cookie and update the previous cookie. |
| 3116 | { |
| 3117 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3118 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3119 | TestDelegate d; |
| 3120 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3121 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3122 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3123 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3124 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3125 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3126 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3127 | |
| 3128 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3129 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 3130 | } |
| 3131 | |
| 3132 | // Verify the cookies weren't saved or updated. |
| 3133 | { |
| 3134 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3135 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3136 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3137 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3138 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3139 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3140 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3141 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3142 | |
| 3143 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 3144 | == std::string::npos); |
| 3145 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 3146 | != std::string::npos); |
| 3147 | |
| 3148 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3149 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3150 | } |
| 3151 | } |
| 3152 | |
mkwst | c5fa776 | 2016-03-28 09:28:23 | [diff] [blame] | 3153 | TEST_F(URLRequestTest, SameSiteCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3154 | HttpTestServer test_server; |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3155 | ASSERT_TRUE(test_server.Start()); |
| 3156 | |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3157 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3158 | default_context().set_network_delegate(&network_delegate); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3159 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3160 | const std::string kHost = "example.test"; |
| 3161 | const std::string kSubHost = "subdomain.example.test"; |
| 3162 | const std::string kCrossHost = "cross-origin.test"; |
| 3163 | |
| 3164 | // Set up two 'SameSite' cookies on 'example.test' |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3165 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3166 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3167 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3168 | test_server.GetURL(kHost, |
| 3169 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 3170 | "LaxSameSiteCookie=1;SameSite=Lax"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3171 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3172 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
| 3173 | req->set_initiator(url::Origin::Create(test_server.GetURL(kHost, "/"))); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3174 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3175 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3176 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3177 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3178 | EXPECT_EQ(2, network_delegate.set_cookie_count()); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3179 | } |
| 3180 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3181 | // Verify that both cookies are sent for same-site requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3182 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3183 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3184 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3185 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3186 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3187 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3188 | req->set_initiator(url::Origin::Create(test_server.GetURL(kHost, "/"))); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3189 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3190 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3191 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3192 | EXPECT_NE(std::string::npos, |
| 3193 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3194 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3195 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3196 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3197 | } |
| 3198 | |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3199 | // Verify that both cookies are sent when the request has no initiator (can |
| 3200 | // happen for main frame browser-initiated navigations). |
| 3201 | { |
| 3202 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3203 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3204 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3205 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3206 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3207 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3208 | d.RunUntilComplete(); |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3209 | |
| 3210 | EXPECT_NE(std::string::npos, |
| 3211 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3212 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 3213 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3214 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3215 | } |
| 3216 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3217 | // Verify that both cookies are sent for same-registrable-domain requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3218 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3219 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3220 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3221 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3222 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3223 | req->set_site_for_cookies(test_server.GetURL(kSubHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3224 | req->set_initiator(url::Origin::Create(test_server.GetURL(kSubHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3225 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3226 | d.RunUntilComplete(); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3227 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3228 | EXPECT_NE(std::string::npos, |
| 3229 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3230 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3231 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3232 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3233 | } |
| 3234 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3235 | // Verify that neither cookie is not sent for cross-site requests. |
| 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(kCrossHost, "/")); |
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->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3245 | d.RunUntilComplete(); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3246 | |
| 3247 | EXPECT_EQ(std::string::npos, |
| 3248 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3249 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 3250 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3251 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3252 | } |
| 3253 | |
| 3254 | // Verify that the lax cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3255 | // method is "safe". |
| 3256 | { |
| 3257 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3258 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3259 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3260 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3261 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3262 | req->set_initiator( |
| 3263 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3264 | req->set_method("GET"); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3265 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3266 | d.RunUntilComplete(); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3267 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3268 | EXPECT_EQ(std::string::npos, |
| 3269 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3270 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3271 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3272 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3273 | } |
| 3274 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3275 | // Verify that neither cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3276 | // method is unsafe (e.g. POST). |
| 3277 | { |
| 3278 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3279 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3280 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3281 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3282 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3283 | req->set_initiator( |
| 3284 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3285 | req->set_method("POST"); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3286 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3287 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3288 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3289 | EXPECT_EQ(std::string::npos, |
| 3290 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3291 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3292 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3293 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3294 | } |
| 3295 | } |
| 3296 | |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 3297 | TEST_F(URLRequestTest, SettingSameSiteCookies) { |
| 3298 | HttpTestServer test_server; |
| 3299 | ASSERT_TRUE(test_server.Start()); |
| 3300 | |
| 3301 | TestNetworkDelegate network_delegate; |
| 3302 | default_context().set_network_delegate(&network_delegate); |
| 3303 | |
| 3304 | const std::string kHost = "example.test"; |
| 3305 | const std::string kSubHost = "subdomain.example.test"; |
| 3306 | const std::string kCrossHost = "cross-origin.test"; |
| 3307 | |
| 3308 | int expected_cookies = 0; |
| 3309 | |
| 3310 | { |
| 3311 | TestDelegate d; |
| 3312 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 3313 | test_server.GetURL(kHost, |
| 3314 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 3315 | "LaxSameSiteCookie=1;SameSite=Lax"), |
| 3316 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3317 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
| 3318 | req->set_initiator(url::Origin::Create(test_server.GetURL(kHost, "/"))); |
| 3319 | |
| 3320 | // 'SameSite' cookies are settable from strict same-site contexts |
| 3321 | // (same-origin site_for_cookies, same-origin initiator), so this request |
| 3322 | // should result in two cookies being created. |
| 3323 | expected_cookies += 2; |
| 3324 | |
| 3325 | req->Start(); |
| 3326 | d.RunUntilComplete(); |
| 3327 | EXPECT_EQ(expected_cookies, network_delegate.set_cookie_count()); |
| 3328 | } |
| 3329 | |
| 3330 | { |
| 3331 | TestDelegate d; |
| 3332 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 3333 | test_server.GetURL(kHost, |
| 3334 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 3335 | "LaxSameSiteCookie=1;SameSite=Lax"), |
| 3336 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3337 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
| 3338 | req->set_initiator( |
| 3339 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
| 3340 | |
| 3341 | // 'SameSite' cookies are settable from lax same-site contexts (same-origin |
| 3342 | // site_for_cookies, cross-site initiator), so this request should result in |
| 3343 | // two cookies being created. |
| 3344 | expected_cookies += 2; |
| 3345 | |
| 3346 | req->Start(); |
| 3347 | d.RunUntilComplete(); |
| 3348 | EXPECT_EQ(expected_cookies, network_delegate.set_cookie_count()); |
| 3349 | } |
| 3350 | |
| 3351 | { |
| 3352 | TestDelegate d; |
| 3353 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 3354 | test_server.GetURL(kHost, |
| 3355 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 3356 | "LaxSameSiteCookie=1;SameSite=Lax"), |
| 3357 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3358 | req->set_site_for_cookies(test_server.GetURL(kSubHost, "/")); |
| 3359 | req->set_initiator( |
| 3360 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
| 3361 | |
| 3362 | // 'SameSite' cookies are settable from lax same-site contexts (same-site |
| 3363 | // site_for_cookies, cross-site initiator), so this request should result in |
| 3364 | // two cookies being created. |
| 3365 | expected_cookies += 2; |
| 3366 | |
| 3367 | req->Start(); |
| 3368 | d.RunUntilComplete(); |
| 3369 | EXPECT_EQ(expected_cookies, network_delegate.set_cookie_count()); |
| 3370 | } |
| 3371 | |
| 3372 | { |
| 3373 | TestDelegate d; |
| 3374 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 3375 | test_server.GetURL(kHost, |
| 3376 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 3377 | "LaxSameSiteCookie=1;SameSite=Lax"), |
| 3378 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3379 | req->set_site_for_cookies(test_server.GetURL(kSubHost, "/")); |
| 3380 | |
| 3381 | // 'SameSite' cookies are settable from strict same-site contexts (same-site |
| 3382 | // site_for_cookies, no initiator), so this request should result in two |
| 3383 | // cookies being created. |
| 3384 | expected_cookies += 2; |
| 3385 | |
| 3386 | req->Start(); |
| 3387 | d.RunUntilComplete(); |
| 3388 | EXPECT_EQ(expected_cookies, network_delegate.set_cookie_count()); |
| 3389 | } |
| 3390 | |
| 3391 | { |
| 3392 | TestDelegate d; |
| 3393 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 3394 | test_server.GetURL(kHost, |
| 3395 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 3396 | "LaxSameSiteCookie=1;SameSite=Lax"), |
| 3397 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3398 | req->set_site_for_cookies(test_server.GetURL(kCrossHost, "/")); |
| 3399 | req->set_initiator( |
| 3400 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
| 3401 | |
| 3402 | // 'SameSite' cookies are not settable from cross-site contexts, so this |
| 3403 | // should not result in any new cookies being created. |
| 3404 | expected_cookies += 0; |
| 3405 | |
| 3406 | req->Start(); |
| 3407 | d.RunUntilComplete(); |
| 3408 | EXPECT_EQ(expected_cookies, network_delegate.set_cookie_count()); |
| 3409 | } |
| 3410 | } |
| 3411 | |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3412 | // Tests that __Secure- cookies can't be set on non-secure origins. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3413 | TEST_F(URLRequestTest, SecureCookiePrefixOnNonsecureOrigin) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3414 | EmbeddedTestServer http_server; |
| 3415 | http_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3416 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3417 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3418 | https_server.AddDefaultHandlers( |
| 3419 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3420 | ASSERT_TRUE(http_server.Start()); |
| 3421 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3422 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3423 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3424 | TestURLRequestContext context(true); |
| 3425 | context.set_network_delegate(&network_delegate); |
| 3426 | context.Init(); |
| 3427 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3428 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3429 | { |
| 3430 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3431 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3432 | http_server.GetURL("/set-cookie?__Secure-nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3433 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3434 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3435 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3436 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3437 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3438 | } |
| 3439 | |
| 3440 | // Verify that the cookie is not set. |
| 3441 | { |
| 3442 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3443 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3444 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3445 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3446 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3447 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3448 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3449 | EXPECT_EQ(d.data_received().find("__Secure-nonsecure-origin=1"), |
| 3450 | std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3451 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3452 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3453 | } |
| 3454 | } |
| 3455 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3456 | TEST_F(URLRequestTest, SecureCookiePrefixNonsecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3457 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3458 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3459 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3460 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3461 | |
| 3462 | TestNetworkDelegate network_delegate; |
| 3463 | TestURLRequestContext context(true); |
| 3464 | context.set_network_delegate(&network_delegate); |
| 3465 | context.Init(); |
| 3466 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3467 | // Try to set a non-Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3468 | { |
| 3469 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3470 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 3471 | https_server.GetURL("/set-cookie?__Secure-foo=1"), DEFAULT_PRIORITY, &d, |
| 3472 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3473 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3474 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3475 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3476 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3477 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3478 | } |
| 3479 | |
| 3480 | // Verify that the cookie is not set. |
| 3481 | { |
| 3482 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3483 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3484 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3485 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3486 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3487 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3488 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3489 | EXPECT_EQ(d.data_received().find("__Secure-foo=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3490 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3491 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3492 | } |
| 3493 | } |
| 3494 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3495 | TEST_F(URLRequestTest, SecureCookiePrefixSecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3496 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3497 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3498 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3499 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3500 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3501 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3502 | TestURLRequestContext context(true); |
| 3503 | context.set_network_delegate(&network_delegate); |
| 3504 | context.Init(); |
| 3505 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3506 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3507 | { |
| 3508 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3509 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3510 | https_server.GetURL("/set-cookie?__Secure-bar=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3511 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3512 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3513 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3514 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3515 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3516 | } |
| 3517 | |
| 3518 | // Verify that the cookie is set. |
| 3519 | { |
| 3520 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3521 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3522 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3523 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3524 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3525 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3526 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3527 | EXPECT_NE(d.data_received().find("__Secure-bar=1"), std::string::npos); |
| 3528 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3529 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3530 | } |
| 3531 | } |
| 3532 | |
| 3533 | // Tests that secure cookies can't be set on non-secure origins if strict secure |
| 3534 | // cookies are enabled. |
| 3535 | TEST_F(URLRequestTest, StrictSecureCookiesOnNonsecureOrigin) { |
| 3536 | EmbeddedTestServer http_server; |
| 3537 | http_server.AddDefaultHandlers( |
| 3538 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3539 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3540 | https_server.AddDefaultHandlers( |
| 3541 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3542 | ASSERT_TRUE(http_server.Start()); |
| 3543 | ASSERT_TRUE(https_server.Start()); |
| 3544 | |
Matt Menke | be9b6eb | 2018-10-18 12:11:44 | [diff] [blame] | 3545 | TestNetworkDelegate network_delegate; |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3546 | TestURLRequestContext context(true); |
| 3547 | context.set_network_delegate(&network_delegate); |
| 3548 | context.Init(); |
| 3549 | |
| 3550 | // Try to set a Secure cookie, with experimental features enabled. |
| 3551 | { |
| 3552 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3553 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3554 | http_server.GetURL("/set-cookie?nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3555 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3556 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3557 | d.RunUntilComplete(); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3558 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3559 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3560 | } |
| 3561 | |
| 3562 | // Verify that the cookie is not set. |
| 3563 | { |
| 3564 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3565 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3566 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3567 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3568 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3569 | d.RunUntilComplete(); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3570 | |
| 3571 | EXPECT_EQ(d.data_received().find("nonsecure-origin=1"), std::string::npos); |
| 3572 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3573 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3574 | } |
| 3575 | } |
| 3576 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3577 | // The parameter is true for same-site and false for cross-site requests. |
| 3578 | class URLRequestTestParameterizedSameSite |
| 3579 | : public URLRequestTest, |
| 3580 | public ::testing::WithParamInterface<bool> { |
| 3581 | protected: |
| 3582 | URLRequestTestParameterizedSameSite() { |
| 3583 | auto params = std::make_unique<HttpNetworkSession::Params>(); |
| 3584 | params->ignore_certificate_errors = true; |
| 3585 | context_.set_http_network_session_params(std::move(params)); |
| 3586 | context_.set_network_delegate(&network_delegate_); |
| 3587 | https_server_.AddDefaultHandlers( |
| 3588 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3589 | EXPECT_TRUE(https_server_.Start()); |
| 3590 | } |
| 3591 | |
| 3592 | // To be called after configuration of |context_| has been finalized. |
| 3593 | void InitContext() { context_.Init(); } |
| 3594 | |
| 3595 | const std::string kHost_ = "example.test"; |
| 3596 | const std::string kCrossHost_ = "cross-site.test"; |
| 3597 | TestURLRequestContext context_{true}; |
| 3598 | TestNetworkDelegate network_delegate_; |
| 3599 | base::HistogramTester histograms_; |
| 3600 | EmbeddedTestServer https_server_{EmbeddedTestServer::TYPE_HTTPS}; |
| 3601 | }; |
| 3602 | |
Victor Costan | 8fb98f6f | 2019-02-01 17:08:29 | [diff] [blame] | 3603 | INSTANTIATE_TEST_SUITE_P(URLRequestTest, |
| 3604 | URLRequestTestParameterizedSameSite, |
| 3605 | ::testing::Bool()); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3606 | |
| 3607 | TEST_P(URLRequestTestParameterizedSameSite, CookieAgeMetrics) { |
| 3608 | const bool same_site = GetParam(); |
| 3609 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3610 | InitContext(); |
| 3611 | |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3612 | EmbeddedTestServer http_server; |
| 3613 | http_server.AddDefaultHandlers( |
| 3614 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3615 | ASSERT_TRUE(http_server.Start()); |
| 3616 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3617 | // Set two test cookies. |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3618 | { |
| 3619 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3620 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3621 | http_server.GetURL(kHost_, "/set-cookie?cookie=value&cookie2=value2"), |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3622 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3623 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3624 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3625 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3626 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3627 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 0); |
| 3628 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", 0); |
| 3629 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", 0); |
| 3630 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3631 | 0); |
| 3632 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3633 | 0); |
| 3634 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", 0); |
| 3635 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", 0); |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3636 | } |
| 3637 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3638 | // Make a secure request. |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3639 | { |
| 3640 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3641 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3642 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3643 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3644 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3645 | req->set_initiator( |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3646 | url::Origin::Create(https_server_.GetURL(kInitiatingHost, "/"))); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3647 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3648 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3649 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3650 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 0); |
| 3651 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", |
| 3652 | !same_site); |
| 3653 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", |
| 3654 | same_site); |
| 3655 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3656 | 0); |
| 3657 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3658 | 0); |
| 3659 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", |
| 3660 | same_site ? 0 : 2); |
| 3661 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", |
| 3662 | same_site ? 2 : 0); |
| 3663 | EXPECT_TRUE(d.data_received().find("cookie=value") != std::string::npos); |
| 3664 | EXPECT_TRUE(d.data_received().find("cookie2=value2") != std::string::npos); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3665 | } |
| 3666 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3667 | // Make a non-secure request. |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3668 | { |
| 3669 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3670 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3671 | http_server.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3672 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3673 | req->set_site_for_cookies(http_server.GetURL(kInitiatingHost, "/")); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3674 | req->set_initiator( |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3675 | url::Origin::Create(http_server.GetURL(kInitiatingHost, "/"))); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3676 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3677 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3678 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", |
| 3679 | !same_site); |
| 3680 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", |
| 3681 | same_site); |
| 3682 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", |
| 3683 | !same_site); |
| 3684 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", |
| 3685 | same_site); |
| 3686 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3687 | same_site ? 0 : 2); |
| 3688 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3689 | same_site ? 2 : 0); |
| 3690 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", |
| 3691 | same_site ? 0 : 2); |
| 3692 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", |
| 3693 | same_site ? 2 : 0); |
| 3694 | EXPECT_TRUE(d.data_received().find("cookie=value") != std::string::npos); |
| 3695 | EXPECT_TRUE(d.data_received().find("cookie2=value2") != std::string::npos); |
| 3696 | } |
| 3697 | } |
| 3698 | |
| 3699 | // Cookies with secure attribute (no HSTS) --> k1pSecureAttribute |
| 3700 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3701 | CookieNetworkSecurityMetricSecureAttribute) { |
| 3702 | const bool same_site = GetParam(); |
| 3703 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3704 | InitContext(); |
| 3705 | |
| 3706 | // Set cookies. |
| 3707 | { |
| 3708 | TestDelegate d; |
| 3709 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3710 | https_server_.GetURL(kHost_, |
| 3711 | "/set-cookie?session-cookie=value;Secure&" |
| 3712 | "longlived-cookie=value;Secure;domain=" + |
| 3713 | kHost_ + ";Max-Age=360000"), |
| 3714 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3715 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3716 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3717 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3718 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3719 | } |
| 3720 | |
| 3721 | // Verify that the cookies fall into the correct metrics bucket. |
| 3722 | { |
| 3723 | TestDelegate d; |
| 3724 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3725 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3726 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3727 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3728 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3729 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3730 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3731 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3732 | // Static cast of boolean required for MSVC 1911. |
| 3733 | histograms_.ExpectBucketCount( |
| 3734 | "Cookie.NetworkSecurity", |
| 3735 | static_cast<int>(CookieNetworkSecurity::k1pSecureAttribute) | |
| 3736 | static_cast<int>(!same_site), |
| 3737 | 2); |
| 3738 | } |
| 3739 | } |
| 3740 | |
| 3741 | // Short-lived host cookie --> k1pHSTSHostCookie |
| 3742 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3743 | CookieNetworkSecurityMetricShortlivedHostCookie) { |
| 3744 | const bool same_site = GetParam(); |
| 3745 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3746 | |
| 3747 | TransportSecurityState transport_security_state; |
| 3748 | transport_security_state.AddHSTS( |
| 3749 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3750 | false /* include_subdomains */); |
| 3751 | context_.set_transport_security_state(&transport_security_state); |
| 3752 | InitContext(); |
| 3753 | |
| 3754 | // Set cookie. |
| 3755 | { |
| 3756 | TestDelegate d; |
| 3757 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3758 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;Max-Age=3600"), |
| 3759 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3760 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3761 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3762 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3763 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3764 | } |
| 3765 | |
| 3766 | // Verify that the cookie falls into the correct metrics bucket. |
| 3767 | { |
| 3768 | TestDelegate d; |
| 3769 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3770 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3771 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3772 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3773 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3774 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3775 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3776 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3777 | // Static cast of boolean required for MSVC 1911. |
| 3778 | histograms_.ExpectBucketCount( |
| 3779 | "Cookie.NetworkSecurity", |
| 3780 | static_cast<int>(CookieNetworkSecurity::k1pHSTSHostCookie) | |
| 3781 | static_cast<int>(!same_site), |
| 3782 | 1); |
| 3783 | } |
| 3784 | } |
| 3785 | |
| 3786 | // Long-lived (either due to expiry or due to being a session cookie) host |
| 3787 | // cookies --> k1pExpiringHSTSHostCookie |
| 3788 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3789 | CookieNetworkSecurityMetricLonglivedHostCookie) { |
| 3790 | const bool same_site = GetParam(); |
| 3791 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3792 | |
| 3793 | TransportSecurityState transport_security_state; |
| 3794 | transport_security_state.AddHSTS( |
| 3795 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3796 | false /* include_subdomains */); |
| 3797 | context_.set_transport_security_state(&transport_security_state); |
| 3798 | InitContext(); |
| 3799 | |
| 3800 | // Set cookies. |
| 3801 | { |
| 3802 | TestDelegate d; |
| 3803 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3804 | https_server_.GetURL(kHost_, |
| 3805 | "/set-cookie?session-cookie=value&" |
| 3806 | "longlived-cookie=value;Max-Age=360000"), |
| 3807 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3808 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3809 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3810 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3811 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3812 | } |
| 3813 | |
| 3814 | // Verify that the cookies fall into the correct metrics bucket. |
| 3815 | { |
| 3816 | TestDelegate d; |
| 3817 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3818 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3819 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3820 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3821 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3822 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3823 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3824 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3825 | // Static cast of boolean required for MSVC 1911. |
| 3826 | histograms_.ExpectBucketCount( |
| 3827 | "Cookie.NetworkSecurity", |
| 3828 | static_cast<int>(CookieNetworkSecurity::k1pExpiringHSTSHostCookie) | |
| 3829 | static_cast<int>(!same_site), |
| 3830 | 2); |
| 3831 | } |
| 3832 | } |
| 3833 | |
| 3834 | // Domain cookie with HSTS subdomains with cookie expiry before HSTS expiry --> |
| 3835 | // k1pHSTSSubdomainsIncluded |
| 3836 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3837 | CookieNetworkSecurityMetricShortlivedDomainCookie) { |
| 3838 | const bool same_site = GetParam(); |
| 3839 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3840 | |
| 3841 | TransportSecurityState transport_security_state; |
| 3842 | transport_security_state.AddHSTS( |
| 3843 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3844 | true /* include_subdomains */); |
| 3845 | context_.set_transport_security_state(&transport_security_state); |
| 3846 | InitContext(); |
| 3847 | |
| 3848 | // Set cookie. |
| 3849 | { |
| 3850 | TestDelegate d; |
| 3851 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3852 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;domain=" + |
| 3853 | kHost_ + ";Max-Age=3600"), |
| 3854 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3855 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3856 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3857 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3858 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3859 | } |
| 3860 | |
| 3861 | // Verify that the cookie falls into the correct metrics bucket. |
| 3862 | { |
| 3863 | TestDelegate d; |
| 3864 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3865 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3866 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3867 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3868 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3869 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3870 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3871 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3872 | // Static cast of boolean required for MSVC 1911. |
| 3873 | histograms_.ExpectBucketCount( |
| 3874 | "Cookie.NetworkSecurity", |
| 3875 | static_cast<int>(CookieNetworkSecurity::k1pHSTSSubdomainsIncluded) | |
| 3876 | static_cast<int>(!same_site), |
| 3877 | 1); |
| 3878 | } |
| 3879 | } |
| 3880 | |
| 3881 | // Long-lived (either due to expiry or due to being a session cookie) domain |
| 3882 | // cookies with HSTS subdomains --> k1pExpiringHSTSSubdomainsIncluded |
| 3883 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3884 | CookieNetworkSecurityMetricLonglivedDomainCookie) { |
| 3885 | const bool same_site = GetParam(); |
| 3886 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3887 | |
| 3888 | TransportSecurityState transport_security_state; |
| 3889 | transport_security_state.AddHSTS( |
| 3890 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3891 | true /* include_subdomains */); |
| 3892 | context_.set_transport_security_state(&transport_security_state); |
| 3893 | InitContext(); |
| 3894 | |
| 3895 | // Set cookies. |
| 3896 | { |
| 3897 | TestDelegate d; |
| 3898 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3899 | https_server_.GetURL( |
| 3900 | kHost_, "/set-cookie?session-cookie=value;domain=" + kHost_ + "&" + |
| 3901 | "longlived-cookie=value;domain=" + kHost_ + |
| 3902 | ";Max-Age=360000"), |
| 3903 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3904 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3905 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3906 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3907 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3908 | } |
| 3909 | |
| 3910 | // Verify that the cookies fall into the correct metrics bucket. |
| 3911 | { |
| 3912 | TestDelegate d; |
| 3913 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3914 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3915 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3916 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3917 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3918 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3919 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3920 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3921 | // Static cast of boolean required for MSVC 1911. |
| 3922 | histograms_.ExpectBucketCount( |
| 3923 | "Cookie.NetworkSecurity", |
| 3924 | static_cast<int>( |
| 3925 | CookieNetworkSecurity::k1pExpiringHSTSSubdomainsIncluded) | |
| 3926 | static_cast<int>(!same_site), |
| 3927 | 2); |
| 3928 | } |
| 3929 | } |
| 3930 | |
| 3931 | // Domain cookie with HSTS subdomains not included --> k1pHSTSSpoofable |
| 3932 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3933 | CookieNetworkSecurityMetricSpoofableDomainCookie) { |
| 3934 | const bool same_site = GetParam(); |
| 3935 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3936 | |
| 3937 | TransportSecurityState transport_security_state; |
| 3938 | transport_security_state.AddHSTS( |
| 3939 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3940 | false /* include_subdomains */); |
| 3941 | context_.set_transport_security_state(&transport_security_state); |
| 3942 | InitContext(); |
| 3943 | |
| 3944 | // Set cookie. |
| 3945 | { |
| 3946 | TestDelegate d; |
| 3947 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3948 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;domain=" + |
| 3949 | kHost_ + ";Max-Age=3600"), |
| 3950 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3951 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3952 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3953 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3954 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3955 | } |
| 3956 | |
| 3957 | // Verify that the cookie falls into the correct metrics bucket. |
| 3958 | { |
| 3959 | TestDelegate d; |
| 3960 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3961 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3962 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3963 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3964 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3965 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3966 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3967 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3968 | // Static cast of boolean required for MSVC 1911. |
| 3969 | histograms_.ExpectBucketCount( |
| 3970 | "Cookie.NetworkSecurity", |
| 3971 | static_cast<int>(CookieNetworkSecurity::k1pHSTSSpoofable) | |
| 3972 | static_cast<int>(!same_site), |
| 3973 | 1); |
| 3974 | } |
| 3975 | } |
| 3976 | |
| 3977 | // Cookie without HSTS --> k1p(Non)SecureConnection |
| 3978 | TEST_P(URLRequestTestParameterizedSameSite, CookieNetworkSecurityMetricNoHSTS) { |
| 3979 | const bool same_site = GetParam(); |
| 3980 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3981 | InitContext(); |
| 3982 | |
| 3983 | EmbeddedTestServer http_server; |
| 3984 | http_server.AddDefaultHandlers( |
| 3985 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3986 | ASSERT_TRUE(http_server.Start()); |
| 3987 | |
| 3988 | // Set cookies. |
| 3989 | { |
| 3990 | TestDelegate d; |
| 3991 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3992 | https_server_.GetURL(kHost_, |
| 3993 | "/set-cookie?cookie=value;domain=" + kHost_ + |
| 3994 | ";Max-Age=3600&host-cookie=value"), |
| 3995 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3996 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3997 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3998 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3999 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 4000 | } |
| 4001 | |
| 4002 | // Verify that the cookie falls into the correct metrics bucket. |
| 4003 | { |
| 4004 | TestDelegate d; |
| 4005 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 4006 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 4007 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 4008 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 4009 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 4010 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4011 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 4012 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 4013 | // Static cast of boolean required for MSVC 1911. |
| 4014 | histograms_.ExpectBucketCount( |
| 4015 | "Cookie.NetworkSecurity", |
| 4016 | static_cast<int>(CookieNetworkSecurity::k1pSecureConnection) | |
| 4017 | static_cast<int>(!same_site), |
| 4018 | 2); |
| 4019 | } |
| 4020 | |
| 4021 | // Verify that the cookie falls into the correct metrics bucket. |
| 4022 | { |
| 4023 | TestDelegate d; |
| 4024 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 4025 | http_server.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 4026 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 4027 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 4028 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 4029 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4030 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 4031 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 4); |
| 4032 | // Static cast of boolean required for MSVC 1911. |
| 4033 | histograms_.ExpectBucketCount( |
| 4034 | "Cookie.NetworkSecurity", |
| 4035 | static_cast<int>(CookieNetworkSecurity::k1pSecureConnection) | |
| 4036 | static_cast<int>(!same_site), |
| 4037 | 2); |
| 4038 | // Static cast of boolean required for MSVC 1911. |
| 4039 | histograms_.ExpectBucketCount( |
| 4040 | "Cookie.NetworkSecurity", |
| 4041 | static_cast<int>(CookieNetworkSecurity::k1pNonsecureConnection) | |
| 4042 | static_cast<int>(!same_site), |
| 4043 | 2); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 4044 | } |
| 4045 | } |
| 4046 | |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4047 | // FixedDateNetworkDelegate swaps out the server's HTTP Date response header |
| 4048 | // value for the |fixed_date| argument given to the constructor. |
| 4049 | class FixedDateNetworkDelegate : public TestNetworkDelegate { |
| 4050 | public: |
| 4051 | explicit FixedDateNetworkDelegate(const std::string& fixed_date) |
| 4052 | : fixed_date_(fixed_date) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 4053 | ~FixedDateNetworkDelegate() override = default; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4054 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4055 | // NetworkDelegate implementation |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4056 | int OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4057 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 4058 | CompletionOnceCallback callback, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4059 | const HttpResponseHeaders* original_response_headers, |
| 4060 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 4061 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4062 | |
| 4063 | private: |
| 4064 | std::string fixed_date_; |
| 4065 | |
| 4066 | DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); |
| 4067 | }; |
| 4068 | |
| 4069 | int FixedDateNetworkDelegate::OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4070 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 4071 | CompletionOnceCallback callback, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4072 | const HttpResponseHeaders* original_response_headers, |
| 4073 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4074 | GURL* allowed_unsafe_redirect_url) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4075 | HttpResponseHeaders* new_response_headers = |
| 4076 | new HttpResponseHeaders(original_response_headers->raw_headers()); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4077 | |
| 4078 | new_response_headers->RemoveHeader("Date"); |
| 4079 | new_response_headers->AddHeader("Date: " + fixed_date_); |
| 4080 | |
| 4081 | *override_response_headers = new_response_headers; |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 4082 | return TestNetworkDelegate::OnHeadersReceived( |
| 4083 | request, std::move(callback), original_response_headers, |
| 4084 | override_response_headers, allowed_unsafe_redirect_url); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4085 | } |
| 4086 | |
| 4087 | // Test that cookie expiration times are adjusted for server/client clock |
| 4088 | // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date |
| 4089 | // headers by defaulting to GMT. (crbug.com/135131) |
| 4090 | TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4091 | HttpTestServer test_server; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4092 | ASSERT_TRUE(test_server.Start()); |
| 4093 | |
| 4094 | // Set up an expired cookie. |
| 4095 | { |
| 4096 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4097 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4098 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4099 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4100 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4101 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4102 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4103 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4104 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4105 | } |
| 4106 | // Verify that the cookie is not set. |
| 4107 | { |
| 4108 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4109 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4110 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4111 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4112 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 4113 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4114 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4115 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4116 | |
| 4117 | EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos); |
| 4118 | } |
| 4119 | // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier. |
| 4120 | { |
| 4121 | FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC"); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4122 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4123 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4124 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4125 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4126 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4127 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4128 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4129 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4130 | } |
| 4131 | // Verify that the cookie is set. |
| 4132 | { |
| 4133 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4134 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4135 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4136 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4137 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 4138 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4139 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4140 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 4141 | |
| 4142 | EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos); |
| 4143 | } |
| 4144 | } |
| 4145 | |
| 4146 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4147 | // Check that it is impossible to change the referrer in the extra headers of |
| 4148 | // an URLRequest. |
| 4149 | TEST_F(URLRequestTest, DoNotOverrideReferrer) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4150 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4151 | ASSERT_TRUE(test_server.Start()); |
| 4152 | |
| 4153 | // If extra headers contain referer and the request contains a referer, |
| 4154 | // only the latter shall be respected. |
| 4155 | { |
| 4156 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4157 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4158 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 4159 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4160 | req->SetReferrer("http://foo.com/"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4161 | |
| 4162 | HttpRequestHeaders headers; |
| 4163 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4164 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4165 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4166 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4167 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4168 | |
| 4169 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 4170 | } |
| 4171 | |
| 4172 | // If extra headers contain a referer but the request does not, no referer |
| 4173 | // shall be sent in the header. |
| 4174 | { |
| 4175 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4176 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4177 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 4178 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4179 | |
| 4180 | HttpRequestHeaders headers; |
| 4181 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4182 | req->SetExtraRequestHeaders(headers); |
| 4183 | req->SetLoadFlags(LOAD_VALIDATE_CACHE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4184 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4185 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4186 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4187 | |
| 4188 | EXPECT_EQ("None", d.data_received()); |
| 4189 | } |
| 4190 | } |
| 4191 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4192 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4193 | public: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 4194 | URLRequestTestHTTP() : test_server_(base::FilePath(kTestFilePath)) {} |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4195 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4196 | protected: |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4197 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 4198 | // |request_method| is the method to use for the initial request. |
| 4199 | // |redirect_method| is the method that is expected to be used for the second |
| 4200 | // request, after redirection. |
| 4201 | // If |include_data| is true, data is uploaded with the request. The |
| 4202 | // response body is expected to match it exactly, if and only if |
| 4203 | // |request_method| == |redirect_method|. |
| 4204 | void HTTPRedirectMethodTest(const GURL& redirect_url, |
| 4205 | const std::string& request_method, |
| 4206 | const std::string& redirect_method, |
| 4207 | bool include_data) { |
| 4208 | static const char kData[] = "hello world"; |
| 4209 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4210 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4211 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4212 | req->set_method(request_method); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4213 | if (include_data) { |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4214 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4215 | HttpRequestHeaders headers; |
| 4216 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 4217 | base::NumberToString(base::size(kData) - 1)); |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 4218 | headers.SetHeader(HttpRequestHeaders::kContentType, "text/plain"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4219 | req->SetExtraRequestHeaders(headers); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4220 | } |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4221 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4222 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4223 | EXPECT_EQ(redirect_method, req->method()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4224 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4225 | if (include_data) { |
| 4226 | if (request_method == redirect_method) { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 4227 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 4228 | HttpRequestHeaders::kContentLength)); |
| 4229 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 4230 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4231 | EXPECT_EQ(kData, d.data_received()); |
| 4232 | } else { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 4233 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 4234 | HttpRequestHeaders::kContentLength)); |
| 4235 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 4236 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 4237 | EXPECT_NE(kData, d.data_received()); |
| 4238 | } |
| 4239 | } |
| 4240 | if (HasFailure()) |
| 4241 | LOG(WARNING) << "Request method was: " << request_method; |
| 4242 | } |
| 4243 | |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4244 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. It's also |
| 4245 | // used as the initial origin. |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4246 | // |request_method| is the method to use for the initial request. |
| 4247 | // |redirect_method| is the method that is expected to be used for the second |
| 4248 | // request, after redirection. |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4249 | // |expected_origin_value| is the expected value for the Origin header after |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4250 | // redirection. If empty, expects that there will be no Origin header. |
| 4251 | void HTTPRedirectOriginHeaderTest(const GURL& redirect_url, |
| 4252 | const std::string& request_method, |
| 4253 | const std::string& redirect_method, |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4254 | const std::string& expected_origin_value) { |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4255 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4256 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4257 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4258 | req->set_method(request_method); |
| 4259 | req->SetExtraRequestHeaderByName(HttpRequestHeaders::kOrigin, |
| 4260 | redirect_url.GetOrigin().spec(), false); |
| 4261 | req->Start(); |
| 4262 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4263 | d.RunUntilComplete(); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4264 | |
| 4265 | EXPECT_EQ(redirect_method, req->method()); |
| 4266 | // Note that there is no check for request success here because, for |
| 4267 | // purposes of testing, the request very well may fail. For example, if the |
| 4268 | // test redirects to an HTTPS server from an HTTP origin, thus it is cross |
| 4269 | // origin, there is not an HTTPS server in this unit test framework, so the |
| 4270 | // request would fail. However, that's fine, as long as the request headers |
| 4271 | // are in order and pass the checks below. |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4272 | if (expected_origin_value.empty()) { |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4273 | EXPECT_FALSE( |
| 4274 | req->extra_request_headers().HasHeader(HttpRequestHeaders::kOrigin)); |
| 4275 | } else { |
| 4276 | std::string origin_header; |
| 4277 | EXPECT_TRUE(req->extra_request_headers().GetHeader( |
| 4278 | HttpRequestHeaders::kOrigin, &origin_header)); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4279 | EXPECT_EQ(expected_origin_value, origin_header); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4280 | } |
| 4281 | } |
| 4282 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4283 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4284 | const int kMsgSize = 20000; // multiple of 10 |
| 4285 | const int kIterations = 50; |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 4286 | char* uploadBytes = new char[kMsgSize+1]; |
| 4287 | char* ptr = uploadBytes; |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4288 | char marker = 'a'; |
| 4289 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 4290 | memcpy(ptr, "----------", 10); |
| 4291 | ptr += 10; |
| 4292 | if (idx % 100 == 0) { |
| 4293 | ptr--; |
| 4294 | *ptr++ = marker; |
| 4295 | if (++marker > 'z') |
| 4296 | marker = 'a'; |
| 4297 | } |
| 4298 | } |
| 4299 | uploadBytes[kMsgSize] = '\0'; |
| 4300 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4301 | for (int i = 0; i < kIterations; ++i) { |
| 4302 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4303 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4304 | test_server_.GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 4305 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4306 | r->set_method(method.c_str()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4307 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4308 | r->set_upload(CreateSimpleUploadData(uploadBytes)); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4309 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4310 | r->Start(); |
| 4311 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4312 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4313 | d.RunUntilComplete(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4314 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4315 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 4316 | << d.request_status(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4317 | |
| 4318 | EXPECT_FALSE(d.received_data_before_response()); |
| 4319 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4320 | } |
| 4321 | delete[] uploadBytes; |
| 4322 | } |
| 4323 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 4324 | bool DoManyCookiesRequest(int num_cookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4325 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4326 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4327 | test_server_.GetURL("/set-many-cookies?" + |
Raul Tambre | 8c1981d | 2019-02-08 02:22:26 | [diff] [blame] | 4328 | base::NumberToString(num_cookies)), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4329 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4330 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4331 | r->Start(); |
| 4332 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4333 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4334 | d.RunUntilComplete(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4335 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4336 | if (d.request_status() != OK) { |
| 4337 | EXPECT_EQ(ERR_RESPONSE_HEADERS_TOO_BIG, d.request_status()); |
| 4338 | return false; |
| 4339 | } |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4340 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4341 | return true; |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4342 | } |
| 4343 | |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4344 | HttpTestServer* http_test_server() { return &test_server_; } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 4345 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4346 | private: |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4347 | HttpTestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4348 | }; |
| 4349 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4350 | namespace { |
| 4351 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4352 | std::unique_ptr<test_server::HttpResponse> HandleRedirectConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4353 | const test_server::HttpRequest& request) { |
| 4354 | if (request.headers.find("Host") == request.headers.end() || |
| 4355 | request.headers.at("Host") != "www.redirect.com" || |
| 4356 | request.method != test_server::METHOD_CONNECT) { |
| 4357 | return nullptr; |
| 4358 | } |
| 4359 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4360 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4361 | new test_server::BasicHttpResponse); |
| 4362 | http_response->set_code(HTTP_FOUND); |
| 4363 | http_response->AddCustomHeader("Location", |
| 4364 | "http://www.destination.com/foo.js"); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 4365 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4366 | } |
| 4367 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4368 | class TestSSLConfigService : public SSLConfigService { |
| 4369 | public: |
Nick Harper | 2243e800 | 2018-09-28 20:33:26 | [diff] [blame] | 4370 | TestSSLConfigService() |
| 4371 | : min_version_(kDefaultSSLVersionMin), |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4372 | max_version_(kDefaultSSLVersionMax) {} |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4373 | ~TestSSLConfigService() override = default; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4374 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4375 | void set_max_version(uint16_t version) { max_version_ = version; } |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4376 | void set_min_version(uint16_t version) { min_version_ = version; } |
| 4377 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4378 | // SSLConfigService: |
| 4379 | void GetSSLConfig(SSLConfig* config) override { |
| 4380 | *config = SSLConfig(); |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4381 | config->version_min = min_version_; |
| 4382 | config->version_max = max_version_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4383 | } |
| 4384 | |
Nick Harper | 89bc721 | 2018-07-31 19:07:57 | [diff] [blame] | 4385 | bool CanShareConnectionWithClientCerts( |
| 4386 | const std::string& hostname) const override { |
| 4387 | return false; |
| 4388 | } |
| 4389 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4390 | private: |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4391 | uint16_t min_version_; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4392 | uint16_t max_version_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4393 | }; |
| 4394 | |
Nick Harper | 89bc721 | 2018-07-31 19:07:57 | [diff] [blame] | 4395 | } // namespace |
| 4396 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4397 | // 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] | 4398 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4399 | // The EmbeddedTestServer will return a 302 response, which we should not |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4400 | // follow. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 4401 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4402 | http_test_server()->RegisterRequestHandler( |
Matt Menke | 2b2a6047 | 2018-02-07 02:12:12 | [diff] [blame] | 4403 | base::BindRepeating(&HandleRedirectConnect)); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4404 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4405 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4406 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4407 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4408 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4409 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4410 | TestDelegate d; |
| 4411 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4412 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4413 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 4414 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4415 | r->Start(); |
| 4416 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4417 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4418 | d.RunUntilComplete(); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4419 | |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 4420 | // The proxy server should be set before failure. |
| 4421 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4422 | http_test_server()->host_port_pair()), |
| 4423 | r->proxy_server()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4424 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4425 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4426 | // We should not have followed the redirect. |
| 4427 | EXPECT_EQ(0, d.received_redirect_count()); |
| 4428 | } |
| 4429 | } |
| 4430 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4431 | // This is the same as the previous test, but checks that the network delegate |
| 4432 | // registers the error. |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 4433 | TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4434 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4435 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4436 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4437 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4438 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4439 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4440 | TestDelegate d; |
| 4441 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4442 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4443 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 4444 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4445 | r->Start(); |
| 4446 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4447 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4448 | d.RunUntilComplete(); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4449 | |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 4450 | // The proxy server should be set before failure. |
| 4451 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4452 | http_test_server()->host_port_pair()), |
| 4453 | r->proxy_server()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4454 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4455 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4456 | // We should not have followed the redirect. |
| 4457 | EXPECT_EQ(0, d.received_redirect_count()); |
| 4458 | |
| 4459 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4460 | EXPECT_THAT(network_delegate.last_error(), |
| 4461 | IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4462 | } |
| 4463 | } |
| 4464 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4465 | // Tests that we can block and asynchronously return OK in various stages. |
| 4466 | TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) { |
| 4467 | static const BlockingNetworkDelegate::Stage blocking_stages[] = { |
| 4468 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 4469 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 4470 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED |
| 4471 | }; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 4472 | static const size_t blocking_stages_length = base::size(blocking_stages); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4473 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4474 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4475 | |
| 4476 | TestDelegate d; |
| 4477 | BlockingNetworkDelegate network_delegate( |
| 4478 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4479 | network_delegate.set_block_on( |
| 4480 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST | |
| 4481 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS | |
| 4482 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 4483 | |
| 4484 | TestURLRequestContext context(true); |
| 4485 | context.set_network_delegate(&network_delegate); |
| 4486 | context.Init(); |
| 4487 | |
| 4488 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4489 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4490 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4491 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4492 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4493 | r->Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4494 | for (size_t i = 0; i < blocking_stages_length; ++i) { |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4495 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4496 | EXPECT_EQ(blocking_stages[i], |
| 4497 | network_delegate.stage_blocked_for_callback()); |
| 4498 | network_delegate.DoCallback(OK); |
| 4499 | } |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4500 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4501 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4502 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4503 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4504 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4505 | } |
| 4506 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4507 | } |
| 4508 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4509 | // Tests that the network delegate can block and cancel a request. |
| 4510 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4511 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4512 | |
| 4513 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4514 | BlockingNetworkDelegate network_delegate( |
| 4515 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4516 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 4517 | network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4518 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4519 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4520 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4521 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4522 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4523 | std::unique_ptr<URLRequest> r( |
| 4524 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4525 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4526 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4527 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4528 | d.RunUntilComplete(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4529 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4530 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4531 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4532 | EXPECT_EQ(ERR_EMPTY_RESPONSE, d.request_status()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4533 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4534 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4535 | } |
| 4536 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4537 | } |
| 4538 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4539 | // Helper function for NetworkDelegateCancelRequestAsynchronously and |
| 4540 | // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network |
| 4541 | // delegate operating in |block_mode| and a request for |url|. It blocks the |
| 4542 | // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT. |
| 4543 | void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode, |
| 4544 | BlockingNetworkDelegate::Stage stage, |
| 4545 | const GURL& url) { |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4546 | TestDelegate d; |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4547 | BlockingNetworkDelegate network_delegate(block_mode); |
| 4548 | network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT); |
| 4549 | network_delegate.set_block_on(stage); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4550 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4551 | TestURLRequestContext context(true); |
| 4552 | context.set_network_delegate(&network_delegate); |
| 4553 | context.Init(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4554 | |
| 4555 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4556 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4557 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4558 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4559 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4560 | d.RunUntilComplete(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4561 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4562 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4563 | if (stage == BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST || |
| 4564 | stage == BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS) { |
| 4565 | EXPECT_FALSE(r->proxy_server().is_valid()); |
| 4566 | } else if (stage == BlockingNetworkDelegate::ON_HEADERS_RECEIVED) { |
| 4567 | EXPECT_TRUE(r->proxy_server().is_direct()); |
| 4568 | } else { |
| 4569 | NOTREACHED(); |
| 4570 | } |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4571 | EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, d.request_status()); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4572 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4573 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4574 | } |
| 4575 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4576 | } |
| 4577 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4578 | // The following 3 tests check that the network delegate can cancel a request |
| 4579 | // synchronously in various stages of the request. |
| 4580 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4581 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4582 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4583 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4584 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4585 | } |
| 4586 | |
| 4587 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4588 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4589 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4590 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4591 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4592 | } |
| 4593 | |
| 4594 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4595 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4596 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4597 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4598 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4599 | } |
| 4600 | |
| 4601 | // The following 3 tests check that the network delegate can cancel a request |
| 4602 | // asynchronously in various stages of the request. |
| 4603 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4604 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4605 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4606 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4607 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4608 | } |
| 4609 | |
| 4610 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4611 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4612 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4613 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4614 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4615 | } |
| 4616 | |
| 4617 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4618 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4619 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4620 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4621 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4622 | } |
| 4623 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4624 | // Tests that the network delegate can block and redirect a request to a new |
| 4625 | // URL. |
| 4626 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4627 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4628 | |
| 4629 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4630 | BlockingNetworkDelegate network_delegate( |
| 4631 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4632 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4633 | GURL redirect_url("http://does.not.resolve.test/simple.html"); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4634 | network_delegate.set_redirect_url(redirect_url); |
| 4635 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4636 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4637 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4638 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4639 | { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4640 | GURL original_url("http://does.not.resolve.test/defaultresponse"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4641 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4642 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4643 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4644 | // Quit after hitting the redirect, so can check the headers. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4645 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 4646 | d.RunUntilRedirect(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4647 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4648 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4649 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4650 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4651 | std::string location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4652 | ASSERT_TRUE( |
| 4653 | r->response_headers()->EnumerateHeader(nullptr, "Location", &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4654 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4655 | |
| 4656 | // Let the request finish. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 4657 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 4658 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4659 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4660 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4661 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4662 | http_test_server()->host_port_pair()), |
| 4663 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4664 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4665 | // through an untunneled proxy. |
| 4666 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4667 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4668 | http_test_server()->host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [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] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4674 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4675 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4676 | } |
| 4677 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4678 | } |
| 4679 | |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4680 | // Tests that the network delegate can block and redirect a request to a new |
| 4681 | // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 4682 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4683 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4684 | |
| 4685 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4686 | BlockingNetworkDelegate network_delegate( |
| 4687 | BlockingNetworkDelegate::SYNCHRONOUS); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4688 | GURL redirect_url("http://does.not.resolve.test/simple.html"); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4689 | network_delegate.set_redirect_url(redirect_url); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4690 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4691 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4692 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4693 | |
| 4694 | { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4695 | GURL original_url("http://does.not.resolve.test/defaultresponse"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4696 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4697 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4698 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4699 | // Quit after hitting the redirect, so can check the headers. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4700 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 4701 | d.RunUntilRedirect(); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4702 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4703 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4704 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4705 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4706 | std::string location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4707 | ASSERT_TRUE( |
| 4708 | r->response_headers()->EnumerateHeader(nullptr, "Location", &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4709 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4710 | |
| 4711 | // Let the request finish. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 4712 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 4713 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4714 | d.RunUntilComplete(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4715 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4716 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4717 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4718 | http_test_server()->host_port_pair()), |
| 4719 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4720 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4721 | // through an untunneled proxy. |
| 4722 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4723 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4724 | http_test_server()->host_port_pair())); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4725 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4726 | EXPECT_EQ(redirect_url, r->url()); |
| 4727 | EXPECT_EQ(original_url, r->original_url()); |
| 4728 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4729 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4730 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4731 | } |
| 4732 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4733 | } |
| 4734 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4735 | // Tests that redirects caused by the network delegate preserve POST data. |
| 4736 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4737 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4738 | |
| 4739 | const char kData[] = "hello world"; |
| 4740 | |
| 4741 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4742 | BlockingNetworkDelegate network_delegate( |
| 4743 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4744 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4745 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4746 | network_delegate.set_redirect_url(redirect_url); |
| 4747 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4748 | TestURLRequestContext context(true); |
| 4749 | context.set_network_delegate(&network_delegate); |
| 4750 | context.Init(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4751 | |
| 4752 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4753 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4754 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4755 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4756 | r->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4757 | r->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4758 | HttpRequestHeaders headers; |
| 4759 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 4760 | base::NumberToString(base::size(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4761 | r->SetExtraRequestHeaders(headers); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4762 | |
| 4763 | // Quit after hitting the redirect, so can check the headers. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4764 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 4765 | d.RunUntilRedirect(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4766 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4767 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4768 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4769 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4770 | std::string location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4771 | ASSERT_TRUE( |
| 4772 | r->response_headers()->EnumerateHeader(nullptr, "Location", &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4773 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4774 | |
| 4775 | // Let the request finish. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 4776 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 4777 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4778 | d.RunUntilComplete(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4779 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4780 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4781 | EXPECT_EQ(redirect_url, r->url()); |
| 4782 | EXPECT_EQ(original_url, r->original_url()); |
| 4783 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4784 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4785 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4786 | EXPECT_EQ("POST", r->method()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4787 | EXPECT_EQ(kData, d.data_received()); |
| 4788 | } |
| 4789 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4790 | } |
| 4791 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4792 | // Tests that the network delegate can block and redirect a request to a new |
| 4793 | // URL during OnHeadersReceived. |
| 4794 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4795 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4796 | |
| 4797 | TestDelegate d; |
| 4798 | BlockingNetworkDelegate network_delegate( |
| 4799 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4800 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4801 | GURL redirect_url("http://does.not.resolve.test/simple.html"); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4802 | network_delegate.set_redirect_on_headers_received_url(redirect_url); |
| 4803 | |
| 4804 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4805 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4806 | |
| 4807 | { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4808 | GURL original_url("http://does.not.resolve.test/defaultresponse"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4809 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4810 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4811 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4812 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4813 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4814 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4815 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4816 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4817 | http_test_server()->host_port_pair()), |
| 4818 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4819 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4820 | // through an untunneled proxy. |
| 4821 | EXPECT_EQ(2, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4822 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4823 | http_test_server()->host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4824 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4825 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4826 | EXPECT_EQ(redirect_url, r->url()); |
| 4827 | EXPECT_EQ(original_url, r->original_url()); |
| 4828 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4829 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 4830 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4831 | } |
| 4832 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4833 | } |
| 4834 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4835 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4836 | // by taking no action. This indicates that the NetworkDelegate does not want to |
| 4837 | // handle the challenge, and is passing the buck along to the |
| 4838 | // URLRequest::Delegate. |
| 4839 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4840 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4841 | |
| 4842 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4843 | BlockingNetworkDelegate network_delegate( |
| 4844 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4845 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4846 | TestURLRequestContext context(true); |
| 4847 | context.set_network_delegate(&network_delegate); |
| 4848 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4849 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4850 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4851 | |
| 4852 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4853 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4854 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4855 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4856 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4857 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4858 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4859 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4860 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4861 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4862 | EXPECT_TRUE(d.auth_required_called()); |
| 4863 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4864 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4865 | } |
| 4866 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4867 | } |
| 4868 | |
| 4869 | TEST_F(URLRequestTestHTTP, |
| 4870 | NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4871 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4872 | |
| 4873 | TestDelegate d; |
| 4874 | BlockingNetworkDelegate network_delegate( |
| 4875 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4876 | |
| 4877 | TestURLRequestContext context(true); |
| 4878 | context.set_network_delegate(&network_delegate); |
| 4879 | context.Init(); |
| 4880 | |
| 4881 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 4882 | |
| 4883 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4884 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4885 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4886 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4887 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4888 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4889 | d.RunUntilComplete(); |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4890 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4891 | { |
| 4892 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4893 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4894 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4895 | } |
| 4896 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4897 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4898 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4899 | EXPECT_TRUE(d.auth_required_called()); |
| 4900 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4901 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4902 | } |
| 4903 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4904 | } |
| 4905 | |
| 4906 | // Tests that the network delegate can synchronously complete OnAuthRequired |
[email protected] | 1e110eae | 2013-05-10 22:02:40 | [diff] [blame] | 4907 | // by setting credentials. |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4908 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4909 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4910 | |
| 4911 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4912 | BlockingNetworkDelegate network_delegate( |
| 4913 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4914 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4915 | network_delegate.set_auth_retval( |
| 4916 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4917 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4918 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4919 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4920 | TestURLRequestContext context(true); |
| 4921 | context.set_network_delegate(&network_delegate); |
| 4922 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4923 | |
| 4924 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4925 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4926 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4927 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4928 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4929 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4930 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4931 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4932 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4933 | EXPECT_FALSE(d.auth_required_called()); |
| 4934 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4935 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4936 | } |
| 4937 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4938 | } |
| 4939 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4940 | // Same as above, but also tests that GetFullRequestHeaders returns the proper |
| 4941 | // headers (for the first or second request) when called at the proper times. |
| 4942 | TEST_F(URLRequestTestHTTP, |
| 4943 | NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4944 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4945 | |
| 4946 | TestDelegate d; |
| 4947 | BlockingNetworkDelegate network_delegate( |
| 4948 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4949 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 4950 | network_delegate.set_auth_retval( |
| 4951 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4952 | |
| 4953 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
| 4954 | |
| 4955 | TestURLRequestContext context(true); |
| 4956 | context.set_network_delegate(&network_delegate); |
| 4957 | context.Init(); |
| 4958 | |
| 4959 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4960 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4961 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4962 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4963 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4964 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4965 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4966 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4967 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4968 | EXPECT_FALSE(d.auth_required_called()); |
| 4969 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4970 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4971 | |
| 4972 | { |
| 4973 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4974 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4975 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
| 4976 | } |
| 4977 | } |
| 4978 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4979 | } |
| 4980 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4981 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4982 | // by cancelling authentication. |
| 4983 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4984 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4985 | |
| 4986 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4987 | BlockingNetworkDelegate network_delegate( |
| 4988 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4989 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4990 | network_delegate.set_auth_retval( |
| 4991 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4992 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4993 | TestURLRequestContext context(true); |
| 4994 | context.set_network_delegate(&network_delegate); |
| 4995 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4996 | |
| 4997 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4998 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4999 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5000 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5001 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5002 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5003 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5004 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5005 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5006 | EXPECT_FALSE(d.auth_required_called()); |
| 5007 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5008 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5009 | } |
| 5010 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5011 | } |
| 5012 | |
| 5013 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 5014 | // by taking no action. This indicates that the NetworkDelegate does not want |
| 5015 | // to handle the challenge, and is passing the buck along to the |
| 5016 | // URLRequest::Delegate. |
| 5017 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5018 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5019 | |
| 5020 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5021 | BlockingNetworkDelegate network_delegate( |
| 5022 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 5023 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5024 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5025 | TestURLRequestContext context(true); |
| 5026 | context.set_network_delegate(&network_delegate); |
| 5027 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5028 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 5029 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5030 | |
| 5031 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5032 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5033 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5034 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5035 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5036 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5037 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5038 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5039 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5040 | EXPECT_TRUE(d.auth_required_called()); |
| 5041 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5042 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5043 | } |
| 5044 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5045 | } |
| 5046 | |
| 5047 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 5048 | // by setting credentials. |
| 5049 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5050 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5051 | |
| 5052 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5053 | BlockingNetworkDelegate network_delegate( |
| 5054 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 5055 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5056 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5057 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 5058 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 5059 | AuthCredentials auth_credentials(kUser, kSecret); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5060 | network_delegate.set_auth_credentials(auth_credentials); |
| 5061 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5062 | TestURLRequestContext context(true); |
| 5063 | context.set_network_delegate(&network_delegate); |
| 5064 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5065 | |
| 5066 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5067 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5068 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5069 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5070 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5071 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5072 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5073 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5074 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5075 | EXPECT_FALSE(d.auth_required_called()); |
| 5076 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5077 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5078 | } |
| 5079 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5080 | } |
| 5081 | |
| 5082 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 5083 | // by cancelling authentication. |
| 5084 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5085 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5086 | |
| 5087 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5088 | BlockingNetworkDelegate network_delegate( |
| 5089 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 5090 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5091 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5092 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 5093 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5094 | TestURLRequestContext context(true); |
| 5095 | context.set_network_delegate(&network_delegate); |
| 5096 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5097 | |
| 5098 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5099 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5100 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5101 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5102 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5103 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5104 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5105 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5106 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5107 | EXPECT_FALSE(d.auth_required_called()); |
| 5108 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5109 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5110 | } |
| 5111 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5112 | } |
| 5113 | |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5114 | // Tests that NetworkDelegate header overrides from the 401 response do not |
| 5115 | // affect the 200 response. This is a regression test for |
| 5116 | // https://crbug.com/801237. |
| 5117 | TEST_F(URLRequestTestHTTP, NetworkDelegateOverrideHeadersWithAuth) { |
| 5118 | ASSERT_TRUE(http_test_server()->Start()); |
| 5119 | |
| 5120 | TestDelegate d; |
| 5121 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 5122 | default_network_delegate_.set_add_header_to_first_response(true); |
| 5123 | |
| 5124 | { |
| 5125 | GURL url(http_test_server()->GetURL("/auth-basic")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5126 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5127 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 5128 | r->Start(); |
| 5129 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5130 | d.RunUntilComplete(); |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5131 | |
| 5132 | EXPECT_EQ(OK, d.request_status()); |
| 5133 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5134 | EXPECT_TRUE(d.auth_required_called()); |
| 5135 | EXPECT_FALSE(r->response_headers()->HasHeader("X-Network-Delegate")); |
| 5136 | } |
| 5137 | |
| 5138 | { |
| 5139 | GURL url(http_test_server()->GetURL("/defaultresponse")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5140 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5141 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 5142 | r->Start(); |
| 5143 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5144 | d.RunUntilComplete(); |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5145 | |
| 5146 | // Check that set_add_header_to_first_response normally adds a header. |
| 5147 | EXPECT_EQ(OK, d.request_status()); |
| 5148 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5149 | EXPECT_TRUE(r->response_headers()->HasHeader("X-Network-Delegate")); |
| 5150 | } |
| 5151 | } |
| 5152 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5153 | // Tests that we can handle when a network request was canceled while we were |
| 5154 | // waiting for the network delegate. |
| 5155 | // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 5156 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
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_BEFORE_URL_REQUEST); |
[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 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5169 | std::unique_ptr<URLRequest> r( |
| 5170 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 5171 | &d, 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_BEFORE_URL_REQUEST, |
| 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 | |
| 5189 | // Tests that we can handle when a network request was canceled while we were |
| 5190 | // waiting for the network delegate. |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5191 | // Part 2: Request is cancelled while waiting for OnBeforeStartTransaction |
| 5192 | // callback. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5193 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5194 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5195 | |
| 5196 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5197 | BlockingNetworkDelegate network_delegate( |
| 5198 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5199 | network_delegate.set_block_on( |
| 5200 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5201 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5202 | TestURLRequestContext context(true); |
| 5203 | context.set_network_delegate(&network_delegate); |
| 5204 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5205 | |
| 5206 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5207 | std::unique_ptr<URLRequest> r( |
| 5208 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 5209 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5210 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5211 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5212 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5213 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 5214 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5215 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5216 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5217 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5218 | // Ensure that network delegate is notified. |
| 5219 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5220 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5221 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5222 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5223 | } |
| 5224 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5225 | } |
| 5226 | |
| 5227 | // Tests that we can handle when a network request was canceled while we were |
| 5228 | // waiting for the network delegate. |
| 5229 | // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 5230 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5231 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5232 | |
| 5233 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5234 | BlockingNetworkDelegate network_delegate( |
| 5235 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5236 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5237 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5238 | TestURLRequestContext context(true); |
| 5239 | context.set_network_delegate(&network_delegate); |
| 5240 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5241 | |
| 5242 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5243 | std::unique_ptr<URLRequest> r( |
| 5244 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 5245 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5246 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5247 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5248 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5249 | EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 5250 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5251 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5252 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5253 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5254 | // Ensure that network delegate is notified. |
| 5255 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5256 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5257 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5258 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5259 | } |
| 5260 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5261 | } |
| 5262 | |
| 5263 | // Tests that we can handle when a network request was canceled while we were |
| 5264 | // waiting for the network delegate. |
| 5265 | // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
[email protected] | bfe8b30 | 2013-02-15 12:16:02 | [diff] [blame] | 5266 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5267 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5268 | |
| 5269 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5270 | BlockingNetworkDelegate network_delegate( |
| 5271 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5272 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5273 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5274 | TestURLRequestContext context(true); |
| 5275 | context.set_network_delegate(&network_delegate); |
| 5276 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5277 | |
| 5278 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5279 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5280 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 5281 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5282 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5283 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5284 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5285 | EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED, |
| 5286 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5287 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5288 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5289 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5290 | // Ensure that network delegate is notified. |
| 5291 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5292 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5293 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5294 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5295 | } |
| 5296 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5297 | } |
| 5298 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5299 | namespace { |
| 5300 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5301 | std::unique_ptr<test_server::HttpResponse> HandleServerAuthConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5302 | const test_server::HttpRequest& request) { |
| 5303 | if (request.headers.find("Host") == request.headers.end() || |
| 5304 | request.headers.at("Host") != "www.server-auth.com" || |
| 5305 | request.method != test_server::METHOD_CONNECT) { |
| 5306 | return nullptr; |
| 5307 | } |
| 5308 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5309 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5310 | new test_server::BasicHttpResponse); |
| 5311 | http_response->set_code(HTTP_UNAUTHORIZED); |
| 5312 | http_response->AddCustomHeader("WWW-Authenticate", |
| 5313 | "Basic realm=\"WallyWorld\""); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 5314 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5315 | } |
| 5316 | |
| 5317 | } // namespace |
| 5318 | |
| 5319 | // 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] | 5320 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5321 | // The EmbeddedTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5322 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5323 | http_test_server()->RegisterRequestHandler( |
Matt Menke | 2b2a6047 | 2018-02-07 02:12:12 | [diff] [blame] | 5324 | base::BindRepeating(&HandleServerAuthConnect)); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5325 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5326 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5327 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 5328 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5329 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 5330 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5331 | TestDelegate d; |
| 5332 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5333 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5334 | GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d, |
| 5335 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5336 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5337 | r->Start(); |
| 5338 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5339 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5340 | d.RunUntilComplete(); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5341 | |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 5342 | // The proxy server should be set before failure. |
| 5343 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 5344 | http_test_server()->host_port_pair()), |
| 5345 | r->proxy_server()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5346 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5347 | } |
| 5348 | } |
| 5349 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5350 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5351 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5352 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5353 | TestDelegate d; |
| 5354 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5355 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5356 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5357 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5358 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5359 | r->Start(); |
| 5360 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5361 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5362 | d.RunUntilComplete(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5363 | |
| 5364 | EXPECT_EQ(1, d.response_started_count()); |
| 5365 | EXPECT_FALSE(d.received_data_before_response()); |
| 5366 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5367 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5368 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5369 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5370 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 5371 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5372 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5373 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5374 | } |
| 5375 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5376 | // This test has the server send a large number of cookies to the client. |
| 5377 | // To ensure that no number of cookies causes a crash, a galloping binary |
| 5378 | // search is used to estimate that maximum number of cookies that are accepted |
| 5379 | // by the browser. Beyond the maximum number, the request will fail with |
| 5380 | // ERR_RESPONSE_HEADERS_TOO_BIG. |
Sergey Ulanov | 0fb900c | 2017-09-20 00:27:31 | [diff] [blame] | 5381 | #if defined(OS_WIN) || defined(OS_FUCHSIA) |
[email protected] | 69dd6fe | 2013-02-23 23:15:30 | [diff] [blame] | 5382 | // http://crbug.com/177916 |
| 5383 | #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies |
| 5384 | #else |
| 5385 | #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies |
| 5386 | #endif // defined(OS_WIN) |
| 5387 | TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5388 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5389 | |
| 5390 | int lower_bound = 0; |
| 5391 | int upper_bound = 1; |
| 5392 | |
| 5393 | // Double the number of cookies until the response header limits are |
| 5394 | // exceeded. |
| 5395 | while (DoManyCookiesRequest(upper_bound)) { |
| 5396 | lower_bound = upper_bound; |
| 5397 | upper_bound *= 2; |
| 5398 | ASSERT_LT(upper_bound, 1000000); |
| 5399 | } |
| 5400 | |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 5401 | int tolerance = static_cast<int>(upper_bound * 0.005); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5402 | if (tolerance < 2) |
| 5403 | tolerance = 2; |
| 5404 | |
| 5405 | // Perform a binary search to find the highest possible number of cookies, |
| 5406 | // within the desired tolerance. |
| 5407 | while (upper_bound - lower_bound >= tolerance) { |
| 5408 | int num_cookies = (lower_bound + upper_bound) / 2; |
| 5409 | |
| 5410 | if (DoManyCookiesRequest(num_cookies)) |
| 5411 | lower_bound = num_cookies; |
| 5412 | else |
| 5413 | upper_bound = num_cookies; |
| 5414 | } |
| 5415 | // Success: the test did not crash. |
| 5416 | } |
| 5417 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5418 | TEST_F(URLRequestTestHTTP, GetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5419 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5420 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5421 | TestDelegate d; |
| 5422 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5423 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5424 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5425 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5426 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5427 | r->Start(); |
| 5428 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5429 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5430 | d.RunUntilComplete(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5431 | |
| 5432 | EXPECT_EQ(1, d.response_started_count()); |
| 5433 | EXPECT_FALSE(d.received_data_before_response()); |
| 5434 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5435 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5436 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5437 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5438 | r->GetResponseRemoteEndpoint().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5439 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 5440 | } |
| 5441 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5442 | TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5443 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5444 | |
| 5445 | TestDelegate d; |
| 5446 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5447 | GURL test_url(http_test_server()->GetURL("/defaultresponse")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5448 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5449 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5450 | |
| 5451 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5452 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5453 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5454 | r->Start(); |
| 5455 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5456 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5457 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5458 | |
| 5459 | EXPECT_EQ(1, d.response_started_count()); |
| 5460 | EXPECT_FALSE(d.received_data_before_response()); |
| 5461 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5462 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5463 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5464 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5465 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5466 | |
| 5467 | EXPECT_TRUE(d.have_full_request_headers()); |
| 5468 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 5469 | } |
| 5470 | } |
| 5471 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5472 | TEST_F(URLRequestTestHTTP, GetTestLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5473 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5474 | |
| 5475 | TestDelegate d; |
| 5476 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5477 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5478 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5479 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5480 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5481 | r->Start(); |
| 5482 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5483 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5484 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5485 | |
| 5486 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5487 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5488 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5489 | |
| 5490 | EXPECT_EQ(1, d.response_started_count()); |
| 5491 | EXPECT_FALSE(d.received_data_before_response()); |
| 5492 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5493 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5494 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5495 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 5496 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5497 | } |
| 5498 | } |
| 5499 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5500 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 5501 | #if !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5502 | TEST_F(URLRequestTestHTTP, GetZippedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5503 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5504 | base::FilePath(kTestFilePath)); |
| 5505 | |
| 5506 | ASSERT_TRUE(test_server.Start()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5507 | |
| 5508 | // Parameter that specifies the Content-Length field in the response: |
| 5509 | // C - Compressed length. |
| 5510 | // U - Uncompressed length. |
| 5511 | // L - Large length (larger than both C & U). |
| 5512 | // M - Medium length (between C & U). |
| 5513 | // S - Small length (smaller than both C & U). |
| 5514 | const char test_parameters[] = "CULMS"; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 5515 | const int num_tests = base::size(test_parameters) - 1; // Skip NULL. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5516 | // C & U should be OK. |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 5517 | // L & M are larger than the data sent, and show an error. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5518 | // S has too little data, but we seem to accept it. |
| 5519 | const bool test_expect_success[num_tests] = |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 5520 | { true, true, false, false, true }; |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5521 | |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5522 | base::FilePath file_path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 5523 | base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5524 | file_path = file_path.Append(kTestFilePath); |
| 5525 | file_path = file_path.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); |
| 5526 | std::string expected_content; |
| 5527 | ASSERT_TRUE(base::ReadFileToString(file_path, &expected_content)); |
| 5528 | |
| 5529 | for (int i = 0; i < num_tests; i++) { |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5530 | TestDelegate d; |
| 5531 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5532 | std::string test_file = base::StringPrintf( |
| 5533 | "compressedfiles/BullRunSpeech.txt?%c", test_parameters[i]); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5534 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5535 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5536 | TestURLRequestContext context(true); |
| 5537 | context.set_network_delegate(&network_delegate); |
| 5538 | context.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 5539 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5540 | std::unique_ptr<URLRequest> r( |
| 5541 | context.CreateRequest(test_server.GetURL(test_file), DEFAULT_PRIORITY, |
| 5542 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5543 | r->Start(); |
| 5544 | EXPECT_TRUE(r->is_pending()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5545 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5546 | d.RunUntilComplete(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5547 | |
| 5548 | EXPECT_EQ(1, d.response_started_count()); |
| 5549 | EXPECT_FALSE(d.received_data_before_response()); |
| 5550 | VLOG(1) << " Received " << d.bytes_received() << " bytes" |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5551 | << " error = " << d.request_status(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5552 | if (test_expect_success[i]) { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5553 | EXPECT_EQ(OK, d.request_status()) << " Parameter = \"" << test_file |
| 5554 | << "\""; |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5555 | if (test_parameters[i] == 'S') { |
| 5556 | // When content length is smaller than both compressed length and |
| 5557 | // uncompressed length, HttpStreamParser might not read the full |
| 5558 | // response body. |
| 5559 | continue; |
| 5560 | } |
| 5561 | EXPECT_EQ(expected_content, d.data_received()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5562 | } else { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5563 | EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, d.request_status()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5564 | << " Parameter = \"" << test_file << "\""; |
| 5565 | } |
| 5566 | } |
| 5567 | } |
| 5568 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5569 | #endif // !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5570 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5571 | TEST_F(URLRequestTestHTTP, RedirectLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5572 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5573 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5574 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5575 | GURL original_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5576 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5577 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5578 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5579 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5580 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5581 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5582 | |
| 5583 | EXPECT_EQ(1, d.response_started_count()); |
| 5584 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5585 | EXPECT_EQ(destination_url, req->url()); |
| 5586 | EXPECT_EQ(original_url, req->original_url()); |
| 5587 | ASSERT_EQ(2U, req->url_chain().size()); |
| 5588 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5589 | EXPECT_EQ(destination_url, req->url_chain()[1]); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5590 | |
| 5591 | LoadTimingInfo load_timing_info_before_redirect; |
| 5592 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect( |
| 5593 | &load_timing_info_before_redirect)); |
| 5594 | TestLoadTimingNotReused(load_timing_info_before_redirect, |
| 5595 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 5596 | |
| 5597 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5598 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5599 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5600 | |
| 5601 | // Check that a new socket was used on redirect, since the server does not |
| 5602 | // supposed keep-alive sockets, and that the times before the redirect are |
| 5603 | // before the ones recorded for the second request. |
| 5604 | EXPECT_NE(load_timing_info_before_redirect.socket_log_id, |
| 5605 | load_timing_info.socket_log_id); |
| 5606 | EXPECT_LE(load_timing_info_before_redirect.receive_headers_end, |
| 5607 | load_timing_info.connect_timing.connect_start); |
| 5608 | } |
| 5609 | |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5610 | TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5611 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5612 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5613 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5614 | GURL middle_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5615 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
| 5616 | GURL original_url = http_test_server()->GetURL("/server-redirect?" + |
| 5617 | middle_redirect_url.spec()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5618 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5619 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5620 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5621 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5622 | d.RunUntilComplete(); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5623 | |
| 5624 | EXPECT_EQ(1, d.response_started_count()); |
| 5625 | EXPECT_EQ(2, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5626 | EXPECT_EQ(destination_url, req->url()); |
| 5627 | EXPECT_EQ(original_url, req->original_url()); |
| 5628 | ASSERT_EQ(3U, req->url_chain().size()); |
| 5629 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5630 | EXPECT_EQ(middle_redirect_url, req->url_chain()[1]); |
| 5631 | EXPECT_EQ(destination_url, req->url_chain()[2]); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5632 | } |
| 5633 | |
David Benjamin | a7fde61 | 2019-03-15 14:20:58 | [diff] [blame] | 5634 | // This is a regression test for https://crbug.com/942073. |
| 5635 | TEST_F(URLRequestTestHTTP, RedirectEscaping) { |
| 5636 | ASSERT_TRUE(http_test_server()->Start()); |
| 5637 | |
| 5638 | // Assemble the destination URL as a string so it is not escaped by GURL. |
| 5639 | GURL destination_base = http_test_server()->GetURL("/defaultresponse"); |
| 5640 | // Add a URL fragment of U+2603 unescaped, U+2603 escaped, and then a UTF-8 |
| 5641 | // encoding error. |
| 5642 | std::string destination_url = |
| 5643 | destination_base.spec() + "#\xE2\x98\x83_%E2%98%83_\xE0\xE0"; |
| 5644 | // Redirect resolution should percent-escape bytes and preserve the UTF-8 |
| 5645 | // error at the end. |
| 5646 | std::string destination_escaped = |
| 5647 | destination_base.spec() + "#%E2%98%83_%E2%98%83_%E0%E0"; |
| 5648 | GURL original_url = http_test_server()->GetURL( |
| 5649 | "/server-redirect?" + EscapeQueryParamValue(destination_url, false)); |
| 5650 | TestDelegate d; |
| 5651 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 5652 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 5653 | req->Start(); |
| 5654 | d.RunUntilComplete(); |
| 5655 | |
| 5656 | EXPECT_EQ(1, d.response_started_count()); |
| 5657 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5658 | EXPECT_EQ(destination_escaped, req->url().spec()); |
| 5659 | EXPECT_EQ(original_url, req->original_url()); |
| 5660 | ASSERT_EQ(2U, req->url_chain().size()); |
| 5661 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5662 | EXPECT_EQ(destination_escaped, req->url_chain()[1].spec()); |
| 5663 | } |
| 5664 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5665 | // First and second pieces of information logged by delegates to URLRequests. |
| 5666 | const char kFirstDelegateInfo[] = "Wonderful delegate"; |
| 5667 | const char kSecondDelegateInfo[] = "Exciting delegate"; |
| 5668 | |
| 5669 | // Logs delegate information to a URLRequest. The first string is logged |
| 5670 | // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is |
| 5671 | // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then |
| 5672 | // another asynchronous call is used to clear the delegate information |
| 5673 | // before calling a callback. The object then deletes itself. |
| 5674 | class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> { |
| 5675 | public: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5676 | using Callback = base::OnceCallback<void()>; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5677 | |
| 5678 | // Each time delegate information is added to the URLRequest, the resulting |
| 5679 | // load state is checked. The expected load state after each request is |
| 5680 | // passed in as an argument. |
| 5681 | static void Run(URLRequest* url_request, |
| 5682 | LoadState expected_first_load_state, |
| 5683 | LoadState expected_second_load_state, |
| 5684 | LoadState expected_third_load_state, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5685 | Callback callback) { |
tzik | 38f920a | 2018-08-01 04:41:20 | [diff] [blame] | 5686 | // base::MakeRefCounted<AsyncDelegateLogger> is unavailable here, since the |
| 5687 | // constructor of AsyncDelegateLogger is private. |
| 5688 | auto logger = base::WrapRefCounted(new AsyncDelegateLogger( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5689 | url_request, expected_first_load_state, expected_second_load_state, |
tzik | 38f920a | 2018-08-01 04:41:20 | [diff] [blame] | 5690 | expected_third_load_state, std::move(callback))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5691 | logger->Start(); |
| 5692 | } |
| 5693 | |
| 5694 | // Checks that the log entries, starting with log_position, contain the |
| 5695 | // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have |
| 5696 | // recorded. Returns the index of entry after the expected number of |
| 5697 | // events this logged, or entries.size() if there aren't enough entries. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5698 | static size_t CheckDelegateInfo(const TestNetLogEntry::List& entries, |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 5699 | size_t log_position) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5700 | // There should be 4 DELEGATE_INFO events: Two begins and two ends. |
| 5701 | if (log_position + 3 >= entries.size()) { |
| 5702 | ADD_FAILURE() << "Not enough log entries"; |
| 5703 | return entries.size(); |
| 5704 | } |
| 5705 | std::string delegate_info; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5706 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5707 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
rdsmith | 37a0dde | 2017-01-04 00:12:07 | [diff] [blame] | 5708 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_blocked_by", |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5709 | &delegate_info)); |
| 5710 | EXPECT_EQ(kFirstDelegateInfo, delegate_info); |
| 5711 | |
| 5712 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5713 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5714 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5715 | |
| 5716 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5717 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5718 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
rdsmith | 37a0dde | 2017-01-04 00:12:07 | [diff] [blame] | 5719 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_blocked_by", |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5720 | &delegate_info)); |
| 5721 | EXPECT_EQ(kSecondDelegateInfo, delegate_info); |
| 5722 | |
| 5723 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5724 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5725 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5726 | |
| 5727 | return log_position + 1; |
| 5728 | } |
| 5729 | |
| 5730 | private: |
| 5731 | friend class base::RefCounted<AsyncDelegateLogger>; |
| 5732 | |
| 5733 | AsyncDelegateLogger(URLRequest* url_request, |
| 5734 | LoadState expected_first_load_state, |
| 5735 | LoadState expected_second_load_state, |
| 5736 | LoadState expected_third_load_state, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5737 | Callback callback) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5738 | : url_request_(url_request), |
| 5739 | expected_first_load_state_(expected_first_load_state), |
| 5740 | expected_second_load_state_(expected_second_load_state), |
| 5741 | expected_third_load_state_(expected_third_load_state), |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5742 | callback_(std::move(callback)) {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5743 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5744 | ~AsyncDelegateLogger() = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5745 | |
| 5746 | void Start() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5747 | url_request_->LogBlockedBy(kFirstDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5748 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5749 | EXPECT_EQ(expected_first_load_state_, load_state.state); |
| 5750 | EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5751 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5752 | FROM_HERE, |
| 5753 | base::BindOnce(&AsyncDelegateLogger::LogSecondDelegate, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5754 | } |
| 5755 | |
| 5756 | void LogSecondDelegate() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5757 | url_request_->LogAndReportBlockedBy(kSecondDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5758 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5759 | EXPECT_EQ(expected_second_load_state_, load_state.state); |
| 5760 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) { |
| 5761 | EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5762 | } else { |
| 5763 | EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5764 | } |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5765 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5766 | FROM_HERE, base::BindOnce(&AsyncDelegateLogger::LogComplete, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5767 | } |
| 5768 | |
| 5769 | void LogComplete() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5770 | url_request_->LogUnblocked(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5771 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5772 | EXPECT_EQ(expected_third_load_state_, load_state.state); |
| 5773 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5774 | EXPECT_EQ(base::string16(), load_state.param); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5775 | std::move(callback_).Run(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5776 | } |
| 5777 | |
| 5778 | URLRequest* url_request_; |
| 5779 | const int expected_first_load_state_; |
| 5780 | const int expected_second_load_state_; |
| 5781 | const int expected_third_load_state_; |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5782 | Callback callback_; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5783 | |
| 5784 | DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger); |
| 5785 | }; |
| 5786 | |
| 5787 | // NetworkDelegate that logs delegate information before a request is started, |
| 5788 | // before headers are sent, when headers are read, and when auth information |
| 5789 | // is requested. Uses AsyncDelegateLogger. |
| 5790 | class AsyncLoggingNetworkDelegate : public TestNetworkDelegate { |
| 5791 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5792 | AsyncLoggingNetworkDelegate() = default; |
| 5793 | ~AsyncLoggingNetworkDelegate() override = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5794 | |
| 5795 | // NetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5796 | int OnBeforeURLRequest(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5797 | CompletionOnceCallback callback, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5798 | GURL* new_url) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5799 | // TestNetworkDelegate always completes synchronously. |
| 5800 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeURLRequest( |
| 5801 | request, base::NullCallback(), new_url)); |
| 5802 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5803 | } |
| 5804 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5805 | int OnBeforeStartTransaction(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5806 | CompletionOnceCallback callback, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5807 | HttpRequestHeaders* headers) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5808 | // TestNetworkDelegate always completes synchronously. |
| 5809 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeStartTransaction( |
| 5810 | request, base::NullCallback(), headers)); |
| 5811 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5812 | } |
| 5813 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5814 | int OnHeadersReceived( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5815 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5816 | CompletionOnceCallback callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5817 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5818 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5819 | GURL* allowed_unsafe_redirect_url) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5820 | // TestNetworkDelegate always completes synchronously. |
| 5821 | CHECK_NE(ERR_IO_PENDING, |
| 5822 | TestNetworkDelegate::OnHeadersReceived( |
| 5823 | request, base::NullCallback(), original_response_headers, |
| 5824 | override_response_headers, allowed_unsafe_redirect_url)); |
| 5825 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5826 | } |
| 5827 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5828 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5829 | URLRequest* request, |
| 5830 | const AuthChallengeInfo& auth_info, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5831 | AuthCallback callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5832 | AuthCredentials* credentials) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5833 | AsyncDelegateLogger::Run( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5834 | request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5835 | LOAD_STATE_WAITING_FOR_DELEGATE, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5836 | base::BindOnce(&AsyncLoggingNetworkDelegate::SetAuthAndResume, |
| 5837 | std::move(callback), credentials)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5838 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 5839 | } |
| 5840 | |
| 5841 | private: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5842 | static int RunCallbackAsynchronously(URLRequest* request, |
| 5843 | CompletionOnceCallback callback) { |
| 5844 | AsyncDelegateLogger::Run(request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5845 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5846 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5847 | base::BindOnce(std::move(callback), OK)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5848 | return ERR_IO_PENDING; |
| 5849 | } |
| 5850 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5851 | static void SetAuthAndResume(AuthCallback callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5852 | AuthCredentials* credentials) { |
| 5853 | *credentials = AuthCredentials(kUser, kSecret); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5854 | std::move(callback).Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5855 | } |
| 5856 | |
| 5857 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate); |
| 5858 | }; |
| 5859 | |
| 5860 | // URLRequest::Delegate that logs delegate information when the headers |
| 5861 | // are received, when each read completes, and during redirects. Uses |
| 5862 | // AsyncDelegateLogger. Can optionally cancel a request in any phase. |
| 5863 | // |
| 5864 | // Inherits from TestDelegate to reuse the TestDelegate code to handle |
| 5865 | // advancing to the next step in most cases, as well as cancellation. |
| 5866 | class AsyncLoggingUrlRequestDelegate : public TestDelegate { |
| 5867 | public: |
| 5868 | enum CancelStage { |
| 5869 | NO_CANCEL = 0, |
| 5870 | CANCEL_ON_RECEIVED_REDIRECT, |
| 5871 | CANCEL_ON_RESPONSE_STARTED, |
| 5872 | CANCEL_ON_READ_COMPLETED |
| 5873 | }; |
| 5874 | |
| 5875 | explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage) |
| 5876 | : cancel_stage_(cancel_stage) { |
| 5877 | if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT) |
| 5878 | set_cancel_in_received_redirect(true); |
| 5879 | else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED) |
| 5880 | set_cancel_in_response_started(true); |
| 5881 | else if (cancel_stage == CANCEL_ON_READ_COMPLETED) |
| 5882 | set_cancel_in_received_data(true); |
| 5883 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5884 | ~AsyncLoggingUrlRequestDelegate() override = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5885 | |
| 5886 | // URLRequest::Delegate implementation: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5887 | void OnReceivedRedirect(URLRequest* request, |
| 5888 | const RedirectInfo& redirect_info, |
| 5889 | bool* defer_redirect) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5890 | *defer_redirect = true; |
| 5891 | AsyncDelegateLogger::Run( |
| 5892 | request, |
| 5893 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5894 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5895 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5896 | base::Bind( |
| 5897 | &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5898 | base::Unretained(this), request, redirect_info)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5899 | } |
| 5900 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5901 | void OnResponseStarted(URLRequest* request, int net_error) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5902 | AsyncDelegateLogger::Run( |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5903 | request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5904 | LOAD_STATE_WAITING_FOR_DELEGATE, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5905 | base::Bind( |
| 5906 | &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete, |
| 5907 | base::Unretained(this), request, net_error)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5908 | } |
| 5909 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5910 | void OnReadCompleted(URLRequest* request, int bytes_read) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5911 | AsyncDelegateLogger::Run( |
| 5912 | request, |
| 5913 | LOAD_STATE_IDLE, |
| 5914 | LOAD_STATE_IDLE, |
| 5915 | LOAD_STATE_IDLE, |
| 5916 | base::Bind( |
| 5917 | &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete, |
| 5918 | base::Unretained(this), request, bytes_read)); |
| 5919 | } |
| 5920 | |
| 5921 | private: |
| 5922 | void OnReceivedRedirectLoggingComplete(URLRequest* request, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5923 | const RedirectInfo& redirect_info) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5924 | bool defer_redirect = false; |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5925 | TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5926 | // FollowDeferredRedirect should not be called after cancellation. |
| 5927 | if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT) |
| 5928 | return; |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 5929 | if (!defer_redirect) { |
| 5930 | request->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 5931 | base::nullopt /* modified_headers */); |
| 5932 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5933 | } |
| 5934 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5935 | void OnResponseStartedLoggingComplete(URLRequest* request, int net_error) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5936 | // The parent class continues the request. |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5937 | TestDelegate::OnResponseStarted(request, net_error); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5938 | } |
| 5939 | |
| 5940 | void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) { |
| 5941 | // The parent class continues the request. |
| 5942 | TestDelegate::OnReadCompleted(request, bytes_read); |
| 5943 | } |
| 5944 | |
| 5945 | const CancelStage cancel_stage_; |
| 5946 | |
| 5947 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate); |
| 5948 | }; |
| 5949 | |
| 5950 | // Tests handling of delegate info before a request starts. |
| 5951 | TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5952 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5953 | |
| 5954 | TestDelegate request_delegate; |
| 5955 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 5956 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5957 | context.set_net_log(&net_log_); |
| 5958 | context.Init(); |
| 5959 | |
| 5960 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5961 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5962 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, |
| 5963 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5964 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5965 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5966 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5967 | |
| 5968 | AsyncDelegateLogger::Run( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5969 | r.get(), |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5970 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5971 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5972 | LOAD_STATE_IDLE, |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5973 | base::Bind(&URLRequest::Start, base::Unretained(r.get()))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5974 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5975 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5976 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5977 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5978 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5979 | } |
| 5980 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5981 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5982 | net_log_.GetEntries(&entries); |
| 5983 | size_t log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5984 | entries, 0, NetLogEventType::DELEGATE_INFO, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5985 | |
| 5986 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position); |
| 5987 | |
| 5988 | // Nothing else should add any delegate info to the request. |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5989 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5990 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5991 | } |
| 5992 | |
| 5993 | // Tests handling of delegate info from a network delegate. |
| 5994 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5995 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5996 | |
| 5997 | TestDelegate request_delegate; |
| 5998 | AsyncLoggingNetworkDelegate network_delegate; |
| 5999 | TestURLRequestContext context(true); |
| 6000 | context.set_network_delegate(&network_delegate); |
| 6001 | context.set_net_log(&net_log_); |
| 6002 | context.Init(); |
| 6003 | |
| 6004 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6005 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 6006 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, |
| 6007 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6008 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6009 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 6010 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6011 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6012 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6013 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6014 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6015 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6016 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6017 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 6018 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 6019 | } |
| 6020 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 6021 | |
| 6022 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6023 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6024 | net_log_.GetEntries(&entries); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6025 | static const NetLogEventType kExpectedEvents[] = { |
| 6026 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 6027 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6028 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6029 | }; |
| 6030 | for (NetLogEventType event : kExpectedEvents) { |
| 6031 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6032 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6033 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6034 | |
| 6035 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 6036 | log_position + 1); |
| 6037 | |
| 6038 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6039 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6040 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6041 | } |
| 6042 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6043 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6044 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6045 | } |
| 6046 | |
| 6047 | // Tests handling of delegate info from a network delegate in the case of an |
| 6048 | // HTTP redirect. |
| 6049 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6050 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6051 | |
| 6052 | TestDelegate request_delegate; |
| 6053 | AsyncLoggingNetworkDelegate network_delegate; |
| 6054 | TestURLRequestContext context(true); |
| 6055 | context.set_network_delegate(&network_delegate); |
| 6056 | context.set_net_log(&net_log_); |
| 6057 | context.Init(); |
| 6058 | |
| 6059 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6060 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6061 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6062 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6063 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6064 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 6065 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6066 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6067 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6068 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6069 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6070 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6071 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6072 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 6073 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 6074 | } |
| 6075 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 6076 | |
| 6077 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6078 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6079 | net_log_.GetEntries(&entries); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6080 | static const NetLogEventType kExpectedEvents[] = { |
| 6081 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 6082 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6083 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6084 | }; |
| 6085 | for (NetLogEventType event : kExpectedEvents) { |
| 6086 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6087 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6088 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6089 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6090 | log_position = |
| 6091 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6092 | |
| 6093 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6094 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6095 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6096 | } |
| 6097 | |
| 6098 | // The URLRequest::Delegate then gets informed about the redirect. |
| 6099 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6100 | entries, log_position + 1, |
| 6101 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6102 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6103 | |
| 6104 | // The NetworkDelegate logged information in the same three events as before. |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6105 | for (NetLogEventType event : kExpectedEvents) { |
| 6106 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6107 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6108 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6109 | |
| 6110 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 6111 | log_position + 1); |
| 6112 | |
| 6113 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6114 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6115 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6116 | } |
| 6117 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6118 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6119 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6120 | } |
| 6121 | |
| 6122 | // Tests handling of delegate info from a network delegate in the case of HTTP |
| 6123 | // AUTH. |
| 6124 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6125 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6126 | |
| 6127 | TestDelegate request_delegate; |
| 6128 | AsyncLoggingNetworkDelegate network_delegate; |
| 6129 | TestURLRequestContext context(true); |
| 6130 | context.set_network_delegate(&network_delegate); |
| 6131 | context.set_net_log(&net_log_); |
| 6132 | context.Init(); |
| 6133 | |
| 6134 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6135 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 6136 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, |
| 6137 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6138 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6139 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 6140 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6141 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6142 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6143 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6144 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6145 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6146 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6147 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 6148 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 6149 | } |
| 6150 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 6151 | |
| 6152 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6153 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6154 | net_log_.GetEntries(&entries); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6155 | static const NetLogEventType kExpectedEvents[] = { |
| 6156 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 6157 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6158 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6159 | NetLogEventType::NETWORK_DELEGATE_AUTH_REQUIRED, |
| 6160 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6161 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6162 | }; |
| 6163 | for (NetLogEventType event : kExpectedEvents) { |
| 6164 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6165 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6166 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6167 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6168 | log_position = |
| 6169 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6170 | |
| 6171 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6172 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6173 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6174 | } |
| 6175 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6176 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6177 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6178 | } |
| 6179 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6180 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 6181 | #if !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6182 | // Tests handling of delegate info from a URLRequest::Delegate. |
| 6183 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6184 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6185 | base::FilePath(kTestFilePath)); |
| 6186 | |
| 6187 | ASSERT_TRUE(test_server.Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6188 | |
| 6189 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 6190 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 6191 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6192 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6193 | context.set_net_log(&net_log_); |
| 6194 | context.Init(); |
| 6195 | |
| 6196 | { |
| 6197 | // A chunked response with delays between chunks is used to make sure that |
| 6198 | // attempts by the URLRequest delegate to log information while reading the |
| 6199 | // body are ignored. Since they are ignored, this test is robust against |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6200 | // the possibility of multiple reads being combined in the unlikely event |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6201 | // that it occurs. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6202 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6203 | test_server.GetURL("/chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6204 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6205 | LoadStateWithParam load_state = r->GetLoadState(); |
| 6206 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6207 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6208 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6209 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6210 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6211 | } |
| 6212 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6213 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6214 | net_log_.GetEntries(&entries); |
| 6215 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6216 | size_t log_position = 0; |
| 6217 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6218 | // The delegate info should only have been logged on header complete. Other |
| 6219 | // times it should silently be ignored. |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6220 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 6221 | entries, 0, NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST)); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6222 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6223 | entries, log_position + 1, |
| 6224 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6225 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6226 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6227 | log_position = |
| 6228 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6229 | |
| 6230 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6231 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 6232 | entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6233 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6234 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6235 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6236 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6237 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6238 | entries, log_position + 1, |
| 6239 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6240 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6241 | #endif // !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6242 | |
| 6243 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 6244 | // an HTTP redirect. |
| 6245 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6246 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6247 | |
| 6248 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 6249 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 6250 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6251 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6252 | context.set_net_log(&net_log_); |
| 6253 | context.Init(); |
| 6254 | |
| 6255 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6256 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6257 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6258 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6259 | LoadStateWithParam load_state = r->GetLoadState(); |
| 6260 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6261 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6262 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6263 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6264 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6265 | } |
| 6266 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6267 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6268 | net_log_.GetEntries(&entries); |
| 6269 | |
| 6270 | // Delegate info should only have been logged in OnReceivedRedirect and |
| 6271 | // OnResponseStarted. |
| 6272 | size_t log_position = 0; |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6273 | static const NetLogEventType kExpectedEvents[] = { |
| 6274 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
| 6275 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 6276 | }; |
| 6277 | for (NetLogEventType event : kExpectedEvents) { |
| 6278 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
| 6279 | log_position = ExpectLogContainsSomewhereAfter(entries, log_position, event, |
| 6280 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6281 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6282 | log_position = |
| 6283 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6284 | |
| 6285 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6286 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6287 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6288 | } |
| 6289 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6290 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6291 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6292 | } |
| 6293 | |
| 6294 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 6295 | // an HTTP redirect, with cancellation at various points. |
| 6296 | TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6297 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6298 | |
| 6299 | const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = { |
| 6300 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT, |
| 6301 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED, |
| 6302 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED, |
| 6303 | }; |
| 6304 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6305 | for (auto cancel_stage : kCancelStages) { |
| 6306 | AsyncLoggingUrlRequestDelegate request_delegate(cancel_stage); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 6307 | TestNetLog net_log; |
krasin | 0bfeb6b | 2017-01-13 21:48:04 | [diff] [blame] | 6308 | TestURLRequestContext context(true); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 6309 | context.set_network_delegate(nullptr); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6310 | context.set_net_log(&net_log); |
| 6311 | context.Init(); |
| 6312 | |
| 6313 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6314 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6315 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6316 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6317 | LoadStateWithParam load_state = r->GetLoadState(); |
| 6318 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6319 | request_delegate.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6320 | EXPECT_EQ(ERR_ABORTED, request_delegate.request_status()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6321 | |
| 6322 | // Spin the message loop to run AsyncDelegateLogger task(s) posted after |
| 6323 | // the |request_delegate| completion task. |
| 6324 | base::RunLoop().RunUntilIdle(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6325 | } |
| 6326 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6327 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6328 | net_log.GetEntries(&entries); |
| 6329 | |
| 6330 | // Delegate info is always logged in both OnReceivedRedirect and |
| 6331 | // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the |
| 6332 | // OnResponseStarted delegate call is after cancellation, but logging is |
| 6333 | // still currently supported in that call. |
| 6334 | size_t log_position = 0; |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6335 | static const NetLogEventType kExpectedEvents[] = { |
| 6336 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
| 6337 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 6338 | }; |
| 6339 | for (NetLogEventType event : kExpectedEvents) { |
| 6340 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6341 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6342 | entries, log_position, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6343 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6344 | log_position = |
| 6345 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6346 | |
| 6347 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6348 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6349 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6350 | } |
| 6351 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6352 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6353 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6354 | } |
| 6355 | } |
| 6356 | |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6357 | namespace { |
| 6358 | |
| 6359 | const char kExtraHeader[] = "Allow-Snafu"; |
| 6360 | const char kExtraValue[] = "fubar"; |
| 6361 | |
| 6362 | class RedirectWithAdditionalHeadersDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 6363 | void OnReceivedRedirect(URLRequest* request, |
| 6364 | const RedirectInfo& redirect_info, |
| 6365 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6366 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6367 | request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false); |
| 6368 | } |
| 6369 | }; |
| 6370 | |
| 6371 | } // namespace |
| 6372 | |
| 6373 | TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6374 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6375 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6376 | GURL destination_url = |
| 6377 | http_test_server()->GetURL("/echoheader?" + std::string(kExtraHeader)); |
| 6378 | GURL original_url = |
| 6379 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6380 | RedirectWithAdditionalHeadersDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6381 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6382 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6383 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6384 | d.RunUntilComplete(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6385 | |
| 6386 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6387 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6388 | EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value)); |
| 6389 | EXPECT_EQ(kExtraValue, value); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6390 | EXPECT_FALSE(req->is_pending()); |
| 6391 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6392 | EXPECT_EQ(kExtraValue, d.data_received()); |
| 6393 | } |
| 6394 | |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6395 | namespace { |
| 6396 | |
| 6397 | const char kExtraHeaderToRemove[] = "To-Be-Removed"; |
| 6398 | |
| 6399 | class RedirectWithHeaderRemovalDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 6400 | void OnReceivedRedirect(URLRequest* request, |
| 6401 | const RedirectInfo& redirect_info, |
| 6402 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6403 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6404 | request->RemoveRequestHeaderByName(kExtraHeaderToRemove); |
| 6405 | } |
| 6406 | }; |
| 6407 | |
| 6408 | } // namespace |
| 6409 | |
| 6410 | TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6411 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6412 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6413 | GURL destination_url = http_test_server()->GetURL( |
| 6414 | "/echoheader?" + std::string(kExtraHeaderToRemove)); |
| 6415 | GURL original_url = |
| 6416 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6417 | RedirectWithHeaderRemovalDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6418 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6419 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6420 | req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false); |
| 6421 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6422 | d.RunUntilComplete(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6423 | |
| 6424 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6425 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6426 | EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6427 | EXPECT_FALSE(req->is_pending()); |
| 6428 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6429 | EXPECT_EQ("None", d.data_received()); |
| 6430 | } |
| 6431 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6432 | TEST_F(URLRequestTestHTTP, CancelAfterStart) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6433 | TestDelegate d; |
| 6434 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6435 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6436 | GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d, |
| 6437 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6438 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6439 | r->Start(); |
| 6440 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6441 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6442 | r->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6443 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6444 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6445 | |
| 6446 | // We expect to receive OnResponseStarted even though the request has been |
| 6447 | // cancelled. |
| 6448 | EXPECT_EQ(1, d.response_started_count()); |
| 6449 | EXPECT_EQ(0, d.bytes_received()); |
| 6450 | EXPECT_FALSE(d.received_data_before_response()); |
| 6451 | } |
| 6452 | } |
| 6453 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6454 | TEST_F(URLRequestTestHTTP, CancelInResponseStarted) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6455 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6456 | |
| 6457 | TestDelegate d; |
| 6458 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6459 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6460 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6461 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6462 | |
| 6463 | d.set_cancel_in_response_started(true); |
| 6464 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6465 | r->Start(); |
| 6466 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6467 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6468 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6469 | |
| 6470 | EXPECT_EQ(1, d.response_started_count()); |
| 6471 | EXPECT_EQ(0, d.bytes_received()); |
| 6472 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6473 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6474 | } |
| 6475 | } |
| 6476 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6477 | TEST_F(URLRequestTestHTTP, CancelOnDataReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6478 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6479 | |
| 6480 | TestDelegate d; |
| 6481 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6482 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6483 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 6484 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6485 | |
| 6486 | d.set_cancel_in_received_data(true); |
| 6487 | |
| 6488 | r->Start(); |
| 6489 | EXPECT_TRUE(r->is_pending()); |
| 6490 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6491 | d.RunUntilComplete(); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6492 | |
| 6493 | EXPECT_EQ(1, d.response_started_count()); |
| 6494 | EXPECT_NE(0, d.received_bytes_count()); |
| 6495 | EXPECT_FALSE(d.received_data_before_response()); |
| 6496 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
| 6497 | } |
| 6498 | } |
| 6499 | |
| 6500 | TEST_F(URLRequestTestHTTP, CancelDuringEofRead) { |
| 6501 | ASSERT_TRUE(http_test_server()->Start()); |
| 6502 | |
| 6503 | TestDelegate d; |
| 6504 | { |
| 6505 | // This returns an empty response (With headers). |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6506 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6507 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6508 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6509 | |
| 6510 | d.set_cancel_in_received_data(true); |
| 6511 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6512 | r->Start(); |
| 6513 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6514 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6515 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6516 | |
| 6517 | EXPECT_EQ(1, d.response_started_count()); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6518 | EXPECT_EQ(0, d.received_bytes_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6519 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6520 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6521 | } |
| 6522 | } |
| 6523 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6524 | TEST_F(URLRequestTestHTTP, CancelByDestroyingAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6525 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6526 | |
| 6527 | TestDelegate d; |
| 6528 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6529 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6530 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6531 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6532 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6533 | r->Start(); |
| 6534 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6535 | |
| 6536 | // The request will be implicitly canceled when it is destroyed. The |
| 6537 | // test delegate must not post a quit message when this happens because |
| 6538 | // this test doesn't actually have a message loop. The quit message would |
| 6539 | // get put on this thread's message queue and the next test would exit |
| 6540 | // early, causing problems. |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 6541 | d.set_on_complete(base::DoNothing()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6542 | } |
| 6543 | // expect things to just cleanup properly. |
| 6544 | |
kimwjdalsl | 2bb4ff0 | 2015-12-16 22:06:02 | [diff] [blame] | 6545 | // 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] | 6546 | // message loop |
| 6547 | EXPECT_FALSE(d.received_data_before_response()); |
| 6548 | EXPECT_EQ(0, d.bytes_received()); |
| 6549 | } |
| 6550 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6551 | TEST_F(URLRequestTestHTTP, CancelWhileReadingFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6552 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6553 | |
| 6554 | // populate cache |
| 6555 | { |
| 6556 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6557 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6558 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6559 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6560 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6561 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6562 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6563 | } |
| 6564 | |
| 6565 | // cancel read from cache (see bug 990242) |
| 6566 | { |
| 6567 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6568 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6569 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6570 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6571 | r->Start(); |
| 6572 | r->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6573 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6574 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6575 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6576 | EXPECT_EQ(1, d.response_started_count()); |
| 6577 | EXPECT_EQ(0, d.bytes_received()); |
| 6578 | EXPECT_FALSE(d.received_data_before_response()); |
| 6579 | } |
| 6580 | } |
| 6581 | |
| 6582 | TEST_F(URLRequestTestHTTP, PostTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6583 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6584 | HTTPUploadDataOperationTest("POST"); |
| 6585 | } |
| 6586 | |
| 6587 | TEST_F(URLRequestTestHTTP, PutTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6588 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6589 | HTTPUploadDataOperationTest("PUT"); |
| 6590 | } |
| 6591 | |
| 6592 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6593 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6594 | |
| 6595 | TestDelegate d; |
| 6596 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6597 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6598 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6599 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6600 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6601 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6602 | r->Start(); |
| 6603 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6604 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6605 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6606 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6607 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6608 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6609 | |
| 6610 | EXPECT_FALSE(d.received_data_before_response()); |
| 6611 | EXPECT_TRUE(d.data_received().empty()); |
| 6612 | } |
| 6613 | } |
| 6614 | |
| 6615 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6616 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6617 | |
| 6618 | TestDelegate d; |
| 6619 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6620 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6621 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6622 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6623 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6624 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6625 | base::FilePath dir; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 6626 | base::PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 6627 | base::SetCurrentDirectory(dir); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6628 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6629 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6630 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6631 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 6632 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 6633 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6634 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6635 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6636 | base::ThreadTaskRunnerHandle::Get().get(), path, 0, |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6637 | std::numeric_limits<uint64_t>::max(), base::Time())); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6638 | r->set_upload(std::make_unique<ElementsUploadDataStream>( |
| 6639 | std::move(element_readers), 0)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6640 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6641 | r->Start(); |
| 6642 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6643 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6644 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6645 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6646 | int64_t size64 = 0; |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 6647 | ASSERT_EQ(true, base::GetFileSize(path, &size64)); |
| 6648 | ASSERT_LE(size64, std::numeric_limits<int>::max()); |
| 6649 | int size = static_cast<int>(size64); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6650 | std::unique_ptr<char[]> buf(new char[size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6651 | |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 6652 | ASSERT_EQ(size, base::ReadFile(path, buf.get(), size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6653 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6654 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6655 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6656 | |
| 6657 | EXPECT_FALSE(d.received_data_before_response()); |
| 6658 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6659 | EXPECT_EQ(size, d.bytes_received()); |
| 6660 | EXPECT_EQ(std::string(&buf[0], size), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6661 | } |
| 6662 | } |
| 6663 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6664 | TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6665 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6666 | |
| 6667 | TestDelegate d; |
| 6668 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6669 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6670 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6671 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6672 | r->set_method("POST"); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6673 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6674 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6675 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6676 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 6677 | base::ThreadTaskRunnerHandle::Get().get(), |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6678 | base::FilePath(FILE_PATH_LITERAL( |
| 6679 | "c:\\path\\to\\non\\existant\\file.randomness.12345")), |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6680 | 0, std::numeric_limits<uint64_t>::max(), base::Time())); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6681 | r->set_upload(std::make_unique<ElementsUploadDataStream>( |
| 6682 | std::move(element_readers), 0)); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6683 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6684 | r->Start(); |
| 6685 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6686 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6687 | d.RunUntilComplete(); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6688 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6689 | EXPECT_TRUE(d.request_failed()); |
| 6690 | EXPECT_FALSE(d.received_data_before_response()); |
| 6691 | EXPECT_EQ(0, d.bytes_received()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6692 | EXPECT_EQ(ERR_FILE_NOT_FOUND, d.request_status()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6693 | } |
| 6694 | } |
| 6695 | |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6696 | namespace { |
| 6697 | |
| 6698 | // Adds a standard set of data to an upload for chunked upload integration |
| 6699 | // tests. |
| 6700 | void AddDataToUpload(ChunkedUploadDataStream::Writer* writer) { |
| 6701 | writer->AppendData("a", 1, false); |
| 6702 | writer->AppendData("bcd", 3, false); |
| 6703 | writer->AppendData("this is a longer chunk than before.", 35, false); |
| 6704 | writer->AppendData("\r\n\r\n", 4, false); |
| 6705 | writer->AppendData("0", 1, false); |
| 6706 | writer->AppendData("2323", 4, true); |
| 6707 | } |
| 6708 | |
| 6709 | // Checks that the upload data added in AddChunksToUpload() was echoed back from |
| 6710 | // the server. |
| 6711 | void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { |
| 6712 | // This should match the chunks sent by AddChunksToUpload(). |
| 6713 | const std::string expected_data = |
| 6714 | "abcdthis is a longer chunk than before.\r\n\r\n02323"; |
| 6715 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6716 | ASSERT_EQ(1, d->response_started_count()) << "request failed. Error: " |
| 6717 | << d->request_status(); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6718 | |
| 6719 | EXPECT_FALSE(d->received_data_before_response()); |
| 6720 | |
| 6721 | EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); |
| 6722 | EXPECT_EQ(expected_data, d->data_received()); |
| 6723 | } |
| 6724 | |
| 6725 | } // namespace |
| 6726 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6727 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6728 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6729 | |
| 6730 | TestDelegate d; |
| 6731 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6732 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6733 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6734 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6735 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6736 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6737 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6738 | upload_data_stream->CreateWriter(); |
| 6739 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6740 | r->set_method("POST"); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6741 | AddDataToUpload(writer.get()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6742 | r->Start(); |
| 6743 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6744 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6745 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6746 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6747 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6748 | } |
| 6749 | } |
| 6750 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6751 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6752 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6753 | |
| 6754 | TestDelegate d; |
| 6755 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6756 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6757 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6758 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6759 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6760 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6761 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6762 | upload_data_stream->CreateWriter(); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6763 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6764 | r->set_method("POST"); |
| 6765 | r->Start(); |
| 6766 | EXPECT_TRUE(r->is_pending()); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6767 | AddDataToUpload(writer.get()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6768 | d.RunUntilComplete(); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6769 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6770 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6771 | } |
| 6772 | } |
| 6773 | |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6774 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6775 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6776 | |
| 6777 | TestDelegate d; |
| 6778 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6779 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6780 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6781 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6782 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6783 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6784 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6785 | upload_data_stream->CreateWriter(); |
| 6786 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6787 | r->set_method("POST"); |
| 6788 | r->Start(); |
| 6789 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6790 | |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6791 | // Pump messages until we start sending headers.. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6792 | base::RunLoop().RunUntilIdle(); |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6793 | |
| 6794 | // And now wait for completion. |
| 6795 | base::RunLoop run_loop; |
| 6796 | d.set_on_complete(run_loop.QuitClosure()); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6797 | AddDataToUpload(writer.get()); |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6798 | run_loop.Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6799 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6800 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6801 | } |
| 6802 | } |
| 6803 | |
| 6804 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6805 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6806 | |
| 6807 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6808 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6809 | http_test_server()->GetURL("/with-headers.html"), DEFAULT_PRIORITY, &d, |
| 6810 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6811 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6812 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6813 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6814 | const HttpResponseHeaders* headers = req->response_headers(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6815 | |
| 6816 | // Simple sanity check that response_info() accesses the same data. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6817 | EXPECT_EQ(headers, req->response_info().headers.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6818 | |
| 6819 | std::string header; |
| 6820 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 6821 | EXPECT_EQ("private", header); |
| 6822 | |
| 6823 | header.clear(); |
| 6824 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 6825 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 6826 | |
| 6827 | // The response has two "X-Multiple-Entries" headers. |
| 6828 | // This verfies our output has them concatenated together. |
| 6829 | header.clear(); |
| 6830 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 6831 | EXPECT_EQ("a, b", header); |
| 6832 | } |
| 6833 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6834 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 6835 | // security state. (see http://crbug.com/550977). |
| 6836 | #if !defined(OS_IOS) |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6837 | TEST_F(URLRequestTestHTTP, ProcessSTS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6838 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6839 | https_test_server.SetSSLConfig( |
| 6840 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6841 | https_test_server.ServeFilesFromSourceDirectory( |
| 6842 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6843 | ASSERT_TRUE(https_test_server.Start()); |
| 6844 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6845 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6846 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6847 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6848 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6849 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6850 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6851 | d.RunUntilComplete(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6852 | |
| 6853 | TransportSecurityState* security_state = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6854 | default_context().transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6855 | TransportSecurityState::STSState sts_state; |
| 6856 | TransportSecurityState::PKPState pkp_state; |
| 6857 | EXPECT_TRUE( |
| 6858 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6859 | EXPECT_FALSE( |
| 6860 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6861 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6862 | sts_state.upgrade_mode); |
| 6863 | EXPECT_TRUE(sts_state.include_subdomains); |
| 6864 | EXPECT_FALSE(pkp_state.include_subdomains); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6865 | #if defined(OS_ANDROID) |
| 6866 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6867 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6868 | EXPECT_FALSE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6869 | #endif |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6870 | } |
| 6871 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6872 | TEST_F(URLRequestTestHTTP, STSNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6873 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6874 | https_test_server.ServeFilesFromSourceDirectory( |
| 6875 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6876 | ASSERT_TRUE(https_test_server.Start()); |
| 6877 | // Make sure this test fails if the test server is changed to not |
| 6878 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6879 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 6880 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6881 | |
| 6882 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6883 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6884 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6885 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6886 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6887 | d.RunUntilComplete(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6888 | TransportSecurityState* security_state = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6889 | default_context().transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6890 | TransportSecurityState::STSState sts_state; |
| 6891 | EXPECT_FALSE( |
| 6892 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6893 | } |
| 6894 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6895 | namespace { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 6896 | const char kExpectCTStaticHostname[] = "expect-ct.preloaded.test"; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6897 | const char kPKPReportUri[] = "http://report-uri.preloaded.test/pkp"; |
| 6898 | const char kPKPHost[] = "with-report-uri-pkp.preloaded.test"; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6899 | } // namespace |
| 6900 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6901 | // 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] | 6902 | TEST_F(URLRequestTestHTTP, ProcessPKPAndSendReport) { |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6903 | GURL report_uri(kPKPReportUri); |
Hiroki Nakagawa | 4e4967bcb | 2018-10-11 04:09:51 | [diff] [blame] | 6904 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6905 | https_test_server.SetSSLConfig( |
| 6906 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6907 | https_test_server.ServeFilesFromSourceDirectory( |
| 6908 | base::FilePath(kTestFilePath)); |
| 6909 | ASSERT_TRUE(https_test_server.Start()); |
| 6910 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6911 | std::string test_server_hostname = kPKPHost; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6912 | |
| 6913 | // Set up a pin for |test_server_hostname|. |
| 6914 | TransportSecurityState security_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6915 | security_state.EnableStaticPinsForTesting(); |
| 6916 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6917 | |
| 6918 | MockCertificateReportSender mock_report_sender; |
| 6919 | security_state.SetReportSender(&mock_report_sender); |
| 6920 | |
| 6921 | // Set up a MockCertVerifier to trigger a violation of the previously |
| 6922 | // set pin. |
| 6923 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6924 | ASSERT_TRUE(cert); |
| 6925 | |
| 6926 | MockCertVerifier cert_verifier; |
| 6927 | CertVerifyResult verify_result; |
| 6928 | verify_result.verified_cert = cert; |
| 6929 | verify_result.is_issued_by_known_root = true; |
| 6930 | HashValue hash3; |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6931 | ASSERT_TRUE( |
| 6932 | hash3.FromString("sha256/3333333333333333333333333333333333333333333=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6933 | verify_result.public_key_hashes.push_back(hash3); |
| 6934 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6935 | |
| 6936 | TestNetworkDelegate network_delegate; |
| 6937 | TestURLRequestContext context(true); |
| 6938 | context.set_transport_security_state(&security_state); |
| 6939 | context.set_network_delegate(&network_delegate); |
| 6940 | context.set_cert_verifier(&cert_verifier); |
| 6941 | context.Init(); |
| 6942 | |
| 6943 | // Now send a request to trigger the violation. |
| 6944 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6945 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6946 | https_test_server.GetURL(test_server_hostname, "/simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6947 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6948 | violating_request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6949 | d.RunUntilComplete(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6950 | |
| 6951 | // Check that a report was sent. |
| 6952 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 6953 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 6954 | EXPECT_EQ("application/json; charset=utf-8", |
| 6955 | mock_report_sender.latest_content_type()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6956 | std::unique_ptr<base::Value> value( |
Lei Zhang | a8b4c5fb | 2019-02-16 03:02:03 | [diff] [blame] | 6957 | base::JSONReader::ReadDeprecated(mock_report_sender.latest_report())); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6958 | ASSERT_TRUE(value); |
jdoerrie | e48b26a | 2017-12-09 14:19:08 | [diff] [blame] | 6959 | ASSERT_TRUE(value->is_dict()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6960 | base::DictionaryValue* report_dict; |
| 6961 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 6962 | std::string report_hostname; |
| 6963 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 6964 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 6965 | } |
| 6966 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6967 | // Tests that reports do not get sent on requests to static pkp hosts that |
| 6968 | // don't have pin violations. |
| 6969 | TEST_F(URLRequestTestHTTP, ProcessPKPWithNoViolation) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6970 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6971 | https_test_server.SetSSLConfig( |
| 6972 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6973 | https_test_server.ServeFilesFromSourceDirectory( |
| 6974 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6975 | ASSERT_TRUE(https_test_server.Start()); |
| 6976 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6977 | std::string test_server_hostname = kPKPHost; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6978 | |
| 6979 | TransportSecurityState security_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6980 | security_state.EnableStaticPinsForTesting(); |
| 6981 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6982 | MockCertificateReportSender mock_report_sender; |
| 6983 | security_state.SetReportSender(&mock_report_sender); |
| 6984 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6985 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6986 | ASSERT_TRUE(cert); |
Hiroki Nakagawa | 4e4967bcb | 2018-10-11 04:09:51 | [diff] [blame] | 6987 | MockCertVerifier mock_cert_verifier; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6988 | CertVerifyResult verify_result; |
| 6989 | verify_result.verified_cert = cert; |
| 6990 | verify_result.is_issued_by_known_root = true; |
| 6991 | HashValue hash; |
| 6992 | // The expected value of GoodPin1 used by |test_default::kHSTSSource|. |
| 6993 | ASSERT_TRUE( |
| 6994 | hash.FromString("sha256/Nn8jk5By4Vkq6BeOVZ7R7AC6XUUBZsWmUbJR1f1Y5FY=")); |
| 6995 | verify_result.public_key_hashes.push_back(hash); |
| 6996 | mock_cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6997 | |
| 6998 | TestNetworkDelegate network_delegate; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6999 | TestURLRequestContext context(true); |
| 7000 | context.set_transport_security_state(&security_state); |
| 7001 | context.set_network_delegate(&network_delegate); |
| 7002 | context.set_cert_verifier(&mock_cert_verifier); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 7003 | context.Init(); |
| 7004 | |
| 7005 | // Now send a request that does not trigger the violation. |
| 7006 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7007 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7008 | https_test_server.GetURL(test_server_hostname, "/simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7009 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 7010 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7011 | d.RunUntilComplete(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 7012 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7013 | // Check that the request succeeded, a report was not sent and the pkp was |
| 7014 | // not bypassed. |
| 7015 | EXPECT_EQ(OK, d.request_status()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 7016 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 7017 | EXPECT_EQ(std::string(), mock_report_sender.latest_report()); |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7018 | TransportSecurityState::STSState sts_state; |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 7019 | TransportSecurityState::PKPState pkp_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7020 | EXPECT_TRUE(security_state.GetStaticDomainState(test_server_hostname, |
| 7021 | &sts_state, &pkp_state)); |
| 7022 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
| 7023 | EXPECT_FALSE(request->ssl_info().pkp_bypassed); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 7024 | } |
| 7025 | |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7026 | TEST_F(URLRequestTestHTTP, PKPBypassRecorded) { |
| 7027 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7028 | https_test_server.SetSSLConfig( |
| 7029 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7030 | https_test_server.ServeFilesFromSourceDirectory( |
| 7031 | base::FilePath(kTestFilePath)); |
| 7032 | ASSERT_TRUE(https_test_server.Start()); |
| 7033 | |
| 7034 | // Set up a MockCertVerifier to be a local root that violates the pin |
| 7035 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7036 | ASSERT_TRUE(cert); |
| 7037 | |
| 7038 | MockCertVerifier cert_verifier; |
| 7039 | CertVerifyResult verify_result; |
| 7040 | verify_result.verified_cert = cert; |
| 7041 | verify_result.is_issued_by_known_root = false; |
| 7042 | HashValue hash; |
| 7043 | ASSERT_TRUE( |
| 7044 | hash.FromString("sha256/1111111111111111111111111111111111111111111=")); |
| 7045 | verify_result.public_key_hashes.push_back(hash); |
| 7046 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7047 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7048 | std::string test_server_hostname = kPKPHost; |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7049 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7050 | // Set up PKP |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7051 | TransportSecurityState security_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7052 | security_state.EnableStaticPinsForTesting(); |
| 7053 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 7054 | MockCertificateReportSender mock_report_sender; |
| 7055 | security_state.SetReportSender(&mock_report_sender); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7056 | |
| 7057 | TestNetworkDelegate network_delegate; |
| 7058 | TestURLRequestContext context(true); |
| 7059 | context.set_transport_security_state(&security_state); |
| 7060 | context.set_network_delegate(&network_delegate); |
| 7061 | context.set_cert_verifier(&cert_verifier); |
| 7062 | context.Init(); |
| 7063 | |
| 7064 | TestDelegate d; |
| 7065 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7066 | https_test_server.GetURL(test_server_hostname, "/simple.html"), |
| 7067 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7068 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7069 | d.RunUntilComplete(); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7070 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7071 | // Check that the request succeeded, a report was not sent and the PKP was |
| 7072 | // bypassed. |
| 7073 | EXPECT_EQ(OK, d.request_status()); |
| 7074 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 7075 | EXPECT_EQ(std::string(), mock_report_sender.latest_report()); |
| 7076 | TransportSecurityState::STSState sts_state; |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7077 | TransportSecurityState::PKPState pkp_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 7078 | EXPECT_TRUE(security_state.GetStaticDomainState(test_server_hostname, |
| 7079 | &sts_state, &pkp_state)); |
| 7080 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7081 | EXPECT_TRUE(request->ssl_info().pkp_bypassed); |
| 7082 | } |
| 7083 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7084 | TEST_F(URLRequestTestHTTP, ProcessSTSOnce) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7085 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7086 | https_test_server.SetSSLConfig( |
| 7087 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7088 | https_test_server.ServeFilesFromSourceDirectory( |
| 7089 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7090 | ASSERT_TRUE(https_test_server.Start()); |
| 7091 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7092 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 7093 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7094 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7095 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7096 | https_test_server.GetURL("/hsts-multiple-headers.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7097 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7098 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7099 | d.RunUntilComplete(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7100 | |
| 7101 | // We should have set parameters from the first header, not the second. |
| 7102 | TransportSecurityState* security_state = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7103 | default_context().transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 7104 | TransportSecurityState::STSState sts_state; |
| 7105 | EXPECT_TRUE( |
| 7106 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 7107 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 7108 | sts_state.upgrade_mode); |
| 7109 | EXPECT_FALSE(sts_state.include_subdomains); |
| 7110 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7111 | } |
| 7112 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7113 | // An ExpectCTReporter that records the number of times OnExpectCTFailed() was |
| 7114 | // called. |
| 7115 | class MockExpectCTReporter : public TransportSecurityState::ExpectCTReporter { |
| 7116 | public: |
| 7117 | MockExpectCTReporter() : num_failures_(0) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 7118 | ~MockExpectCTReporter() override = default; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7119 | |
| 7120 | void OnExpectCTFailed(const HostPortPair& host_port_pair, |
| 7121 | const GURL& report_uri, |
estark | ae028b46 | 2017-06-20 23:25:01 | [diff] [blame] | 7122 | base::Time expiration, |
estark | bf1b5296 | 2017-05-05 17:05:25 | [diff] [blame] | 7123 | const X509Certificate* validated_certificate_chain, |
| 7124 | const X509Certificate* served_certificate_chain, |
| 7125 | const SignedCertificateTimestampAndStatusList& |
| 7126 | signed_certificate_timestamps) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7127 | num_failures_++; |
| 7128 | } |
| 7129 | |
| 7130 | uint32_t num_failures() { return num_failures_; } |
| 7131 | |
| 7132 | private: |
| 7133 | uint32_t num_failures_; |
| 7134 | }; |
| 7135 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7136 | // A CTPolicyEnforcer that returns a default CTPolicyCompliance value |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7137 | // for every certificate. |
| 7138 | class MockCTPolicyEnforcer : public CTPolicyEnforcer { |
| 7139 | public: |
| 7140 | MockCTPolicyEnforcer() |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7141 | : default_result_(ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 7142 | ~MockCTPolicyEnforcer() override = default; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7143 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7144 | ct::CTPolicyCompliance CheckCompliance( |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7145 | X509Certificate* cert, |
Ryan Sleevi | 8a9c9c1 | 2018-05-09 02:36:23 | [diff] [blame] | 7146 | const ct::SCTList& verified_scts, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 7147 | const NetLogWithSource& net_log) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7148 | return default_result_; |
| 7149 | } |
| 7150 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7151 | void set_default_result(ct::CTPolicyCompliance default_result) { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7152 | default_result_ = default_result; |
| 7153 | } |
| 7154 | |
| 7155 | private: |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7156 | ct::CTPolicyCompliance default_result_; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7157 | }; |
| 7158 | |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7159 | // Tests that Expect CT headers for the preload list are processed correctly. |
| 7160 | TEST_F(URLRequestTestHTTP, PreloadExpectCTHeader) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 7161 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 7162 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7163 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7164 | https_test_server.SetSSLConfig( |
| 7165 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7166 | https_test_server.ServeFilesFromSourceDirectory( |
| 7167 | base::FilePath(kTestFilePath)); |
| 7168 | ASSERT_TRUE(https_test_server.Start()); |
| 7169 | |
| 7170 | MockExpectCTReporter reporter; |
| 7171 | TransportSecurityState transport_security_state; |
| 7172 | transport_security_state.enable_static_expect_ct_ = true; |
| 7173 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7174 | |
| 7175 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7176 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7177 | ASSERT_TRUE(cert); |
| 7178 | MockCertVerifier cert_verifier; |
| 7179 | CertVerifyResult verify_result; |
| 7180 | verify_result.verified_cert = cert; |
| 7181 | verify_result.is_issued_by_known_root = true; |
| 7182 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7183 | |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 7184 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to trigger an Expect |
| 7185 | // CT violation. |
| 7186 | DoNothingCTVerifier ct_verifier; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7187 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7188 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7189 | ct::CTPolicyCompliance::CT_POLICY_NOT_ENOUGH_SCTS); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7190 | |
| 7191 | TestNetworkDelegate network_delegate; |
| 7192 | // Use a MockHostResolver (which by default maps all hosts to |
| 7193 | // 127.0.0.1) so that the request can be sent to a site on the Expect |
| 7194 | // CT preload list. |
| 7195 | MockHostResolver host_resolver; |
| 7196 | TestURLRequestContext context(true); |
| 7197 | context.set_host_resolver(&host_resolver); |
| 7198 | context.set_transport_security_state(&transport_security_state); |
| 7199 | context.set_network_delegate(&network_delegate); |
| 7200 | context.set_cert_verifier(&cert_verifier); |
| 7201 | context.set_cert_transparency_verifier(&ct_verifier); |
vabr | 13d0074 | 2017-06-05 10:31:46 | [diff] [blame] | 7202 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7203 | context.Init(); |
| 7204 | |
| 7205 | // Now send a request to trigger the violation. |
| 7206 | TestDelegate d; |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7207 | GURL url = https_test_server.GetURL("/expect-ct-header-preload.html"); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7208 | GURL::Replacements replace_host; |
| 7209 | replace_host.SetHostStr(kExpectCTStaticHostname); |
| 7210 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7211 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
| 7212 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7213 | violating_request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7214 | d.RunUntilComplete(); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7215 | |
| 7216 | EXPECT_EQ(1u, reporter.num_failures()); |
| 7217 | } |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7218 | |
| 7219 | // Tests that Expect CT HTTP headers are processed correctly. |
| 7220 | TEST_F(URLRequestTestHTTP, ExpectCTHeader) { |
| 7221 | base::test::ScopedFeatureList feature_list; |
| 7222 | feature_list.InitAndEnableFeature( |
| 7223 | TransportSecurityState::kDynamicExpectCTFeature); |
| 7224 | |
| 7225 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7226 | https_test_server.SetSSLConfig( |
| 7227 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7228 | https_test_server.ServeFilesFromSourceDirectory( |
| 7229 | base::FilePath(kTestFilePath)); |
| 7230 | ASSERT_TRUE(https_test_server.Start()); |
| 7231 | |
| 7232 | MockExpectCTReporter reporter; |
| 7233 | TransportSecurityState transport_security_state; |
| 7234 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7235 | |
| 7236 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7237 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7238 | ASSERT_TRUE(cert); |
| 7239 | MockCertVerifier cert_verifier; |
| 7240 | CertVerifyResult verify_result; |
| 7241 | verify_result.verified_cert = cert; |
| 7242 | verify_result.is_issued_by_known_root = true; |
| 7243 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7244 | |
| 7245 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 7246 | // compliance. |
| 7247 | DoNothingCTVerifier ct_verifier; |
| 7248 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7249 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7250 | ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7251 | |
| 7252 | TestNetworkDelegate network_delegate; |
| 7253 | // Use a MockHostResolver (which by default maps all hosts to |
| 7254 | // 127.0.0.1). |
| 7255 | MockHostResolver host_resolver; |
| 7256 | TestURLRequestContext context(true); |
| 7257 | context.set_host_resolver(&host_resolver); |
| 7258 | context.set_transport_security_state(&transport_security_state); |
| 7259 | context.set_network_delegate(&network_delegate); |
| 7260 | context.set_cert_verifier(&cert_verifier); |
| 7261 | context.set_cert_transparency_verifier(&ct_verifier); |
| 7262 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 7263 | context.Init(); |
| 7264 | |
| 7265 | // Now send a request to trigger the header processing. |
| 7266 | TestDelegate d; |
| 7267 | GURL url = https_test_server.GetURL("/expect-ct-header.html"); |
| 7268 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7269 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7270 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7271 | d.RunUntilComplete(); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7272 | |
| 7273 | TransportSecurityState::ExpectCTState state; |
| 7274 | ASSERT_TRUE( |
| 7275 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 7276 | EXPECT_TRUE(state.enforce); |
| 7277 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 7278 | } |
| 7279 | |
| 7280 | // Tests that if multiple Expect CT HTTP headers are sent, they are all |
| 7281 | // processed. |
| 7282 | TEST_F(URLRequestTestHTTP, MultipleExpectCTHeaders) { |
| 7283 | base::test::ScopedFeatureList feature_list; |
| 7284 | feature_list.InitAndEnableFeature( |
| 7285 | TransportSecurityState::kDynamicExpectCTFeature); |
| 7286 | |
| 7287 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7288 | https_test_server.SetSSLConfig( |
| 7289 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7290 | https_test_server.ServeFilesFromSourceDirectory( |
| 7291 | base::FilePath(kTestFilePath)); |
| 7292 | ASSERT_TRUE(https_test_server.Start()); |
| 7293 | |
| 7294 | MockExpectCTReporter reporter; |
| 7295 | TransportSecurityState transport_security_state; |
| 7296 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7297 | |
| 7298 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7299 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7300 | ASSERT_TRUE(cert); |
| 7301 | MockCertVerifier cert_verifier; |
| 7302 | CertVerifyResult verify_result; |
| 7303 | verify_result.verified_cert = cert; |
| 7304 | verify_result.is_issued_by_known_root = true; |
| 7305 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7306 | |
| 7307 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 7308 | // compliance. |
| 7309 | DoNothingCTVerifier ct_verifier; |
| 7310 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7311 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7312 | ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7313 | |
| 7314 | TestNetworkDelegate network_delegate; |
| 7315 | // Use a MockHostResolver (which by default maps all hosts to |
| 7316 | // 127.0.0.1). |
| 7317 | MockHostResolver host_resolver; |
| 7318 | TestURLRequestContext context(true); |
| 7319 | context.set_host_resolver(&host_resolver); |
| 7320 | context.set_transport_security_state(&transport_security_state); |
| 7321 | context.set_network_delegate(&network_delegate); |
| 7322 | context.set_cert_verifier(&cert_verifier); |
| 7323 | context.set_cert_transparency_verifier(&ct_verifier); |
| 7324 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 7325 | context.Init(); |
| 7326 | |
| 7327 | // Now send a request to trigger the header processing. |
| 7328 | TestDelegate d; |
| 7329 | GURL url = https_test_server.GetURL("/expect-ct-header-multiple.html"); |
| 7330 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7331 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7332 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7333 | d.RunUntilComplete(); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7334 | |
| 7335 | TransportSecurityState::ExpectCTState state; |
| 7336 | ASSERT_TRUE( |
| 7337 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 7338 | EXPECT_TRUE(state.enforce); |
| 7339 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 7340 | } |
| 7341 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 7342 | #endif // !defined(OS_IOS) |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7343 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7344 | #if BUILDFLAG(ENABLE_REPORTING) |
| 7345 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7346 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_DontReportIfNetworkNotAccessed) { |
| 7347 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7348 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7349 | ASSERT_TRUE(https_test_server.Start()); |
| 7350 | GURL request_url = https_test_server.GetURL("/cachetime"); |
| 7351 | |
| 7352 | TestNetworkErrorLoggingService nel_service; |
| 7353 | TestURLRequestContext context(true); |
| 7354 | context.set_network_error_logging_service(&nel_service); |
| 7355 | context.Init(); |
| 7356 | |
| 7357 | // Populate the cache. |
| 7358 | TestDelegate d; |
| 7359 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7360 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7361 | request->Start(); |
| 7362 | d.RunUntilComplete(); |
| 7363 | |
| 7364 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7365 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7366 | nel_service.errors()[0]; |
| 7367 | EXPECT_EQ(request_url, error.uri); |
| 7368 | EXPECT_EQ(200, error.status_code); |
| 7369 | EXPECT_EQ(OK, error.type); |
| 7370 | |
| 7371 | request = context.CreateRequest(request_url, DEFAULT_PRIORITY, &d, |
| 7372 | TRAFFIC_ANNOTATION_FOR_TESTS); |
| 7373 | request->Start(); |
| 7374 | d.RunUntilComplete(); |
| 7375 | |
| 7376 | EXPECT_FALSE(request->response_info().network_accessed); |
| 7377 | EXPECT_TRUE(request->response_info().was_cached); |
| 7378 | // No additional NEL report was generated. |
| 7379 | EXPECT_EQ(1u, nel_service.errors().size()); |
| 7380 | } |
| 7381 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7382 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_BasicSuccess) { |
| 7383 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7384 | https_test_server.ServeFilesFromSourceDirectory( |
| 7385 | base::FilePath(kTestFilePath)); |
| 7386 | ASSERT_TRUE(https_test_server.Start()); |
| 7387 | GURL request_url = https_test_server.GetURL("/simple.html"); |
| 7388 | |
| 7389 | TestNetworkErrorLoggingService nel_service; |
| 7390 | TestURLRequestContext context(true); |
| 7391 | context.set_network_error_logging_service(&nel_service); |
| 7392 | context.Init(); |
| 7393 | |
| 7394 | TestDelegate d; |
| 7395 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7396 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7397 | request->Start(); |
| 7398 | d.RunUntilComplete(); |
| 7399 | |
| 7400 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7401 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7402 | nel_service.errors()[0]; |
| 7403 | EXPECT_EQ(request_url, error.uri); |
| 7404 | EXPECT_EQ(200, error.status_code); |
| 7405 | EXPECT_EQ(OK, error.type); |
| 7406 | } |
| 7407 | |
| 7408 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_BasicError) { |
| 7409 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7410 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7411 | ASSERT_TRUE(https_test_server.Start()); |
| 7412 | GURL request_url = https_test_server.GetURL("/close-socket"); |
| 7413 | |
| 7414 | TestNetworkErrorLoggingService nel_service; |
| 7415 | TestURLRequestContext context(true); |
| 7416 | context.set_network_error_logging_service(&nel_service); |
| 7417 | context.Init(); |
| 7418 | |
| 7419 | TestDelegate d; |
| 7420 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7421 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7422 | request->Start(); |
| 7423 | d.RunUntilComplete(); |
| 7424 | |
| 7425 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7426 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7427 | nel_service.errors()[0]; |
| 7428 | EXPECT_EQ(request_url, error.uri); |
| 7429 | EXPECT_EQ(0, error.status_code); |
| 7430 | EXPECT_EQ(ERR_EMPTY_RESPONSE, error.type); |
| 7431 | } |
| 7432 | |
| 7433 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_Redirect) { |
| 7434 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7435 | https_test_server.ServeFilesFromSourceDirectory( |
| 7436 | base::FilePath(kTestFilePath)); |
| 7437 | ASSERT_TRUE(https_test_server.Start()); |
| 7438 | GURL request_url = https_test_server.GetURL("/redirect-test.html"); |
| 7439 | GURL redirect_url = https_test_server.GetURL("/with-headers.html"); |
| 7440 | |
| 7441 | TestNetworkErrorLoggingService nel_service; |
| 7442 | TestURLRequestContext context(true); |
| 7443 | context.set_network_error_logging_service(&nel_service); |
| 7444 | context.Init(); |
| 7445 | |
| 7446 | TestDelegate d; |
| 7447 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7448 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7449 | request->Start(); |
| 7450 | d.RunUntilComplete(); |
| 7451 | |
| 7452 | ASSERT_EQ(2u, nel_service.errors().size()); |
| 7453 | const TestNetworkErrorLoggingService::RequestDetails& error1 = |
| 7454 | nel_service.errors()[0]; |
| 7455 | EXPECT_EQ(request_url, error1.uri); |
| 7456 | EXPECT_EQ(302, error1.status_code); |
| 7457 | EXPECT_EQ(OK, error1.type); |
| 7458 | const TestNetworkErrorLoggingService::RequestDetails& error2 = |
| 7459 | nel_service.errors()[1]; |
| 7460 | EXPECT_EQ(redirect_url, error2.uri); |
| 7461 | EXPECT_EQ(200, error2.status_code); |
| 7462 | EXPECT_EQ(OK, error2.type); |
| 7463 | } |
| 7464 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7465 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_RedirectWithoutLocationHeader) { |
| 7466 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7467 | https_test_server.ServeFilesFromSourceDirectory( |
| 7468 | base::FilePath(kTestFilePath)); |
| 7469 | ASSERT_TRUE(https_test_server.Start()); |
| 7470 | GURL request_url = https_test_server.GetURL("/308-without-location-header"); |
| 7471 | |
| 7472 | TestNetworkErrorLoggingService nel_service; |
| 7473 | TestURLRequestContext context(true); |
| 7474 | context.set_network_error_logging_service(&nel_service); |
| 7475 | context.Init(); |
| 7476 | |
| 7477 | TestDelegate d; |
| 7478 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7479 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7480 | request->Start(); |
| 7481 | d.RunUntilComplete(); |
| 7482 | |
| 7483 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7484 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7485 | nel_service.errors()[0]; |
| 7486 | EXPECT_EQ(request_url, error.uri); |
| 7487 | EXPECT_EQ(308, error.status_code); |
| 7488 | // The body of the response was successfully read. |
| 7489 | EXPECT_EQ(OK, error.type); |
| 7490 | } |
| 7491 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7492 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_Auth) { |
| 7493 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7494 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7495 | ASSERT_TRUE(https_test_server.Start()); |
| 7496 | GURL request_url = https_test_server.GetURL("/auth-basic"); |
| 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_credentials(AuthCredentials(kUser, kSecret)); |
| 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(2u, nel_service.errors().size()); |
| 7511 | const TestNetworkErrorLoggingService::RequestDetails& error1 = |
| 7512 | nel_service.errors()[0]; |
| 7513 | EXPECT_EQ(request_url, error1.uri); |
| 7514 | EXPECT_EQ(401, error1.status_code); |
| 7515 | EXPECT_EQ(OK, error1.type); |
| 7516 | const TestNetworkErrorLoggingService::RequestDetails& error2 = |
| 7517 | nel_service.errors()[1]; |
| 7518 | EXPECT_EQ(request_url, error2.uri); |
| 7519 | EXPECT_EQ(200, error2.status_code); |
| 7520 | EXPECT_EQ(OK, error2.type); |
| 7521 | } |
| 7522 | |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7523 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_304Response) { |
| 7524 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7525 | https_test_server.AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7526 | ASSERT_TRUE(https_test_server.Start()); |
| 7527 | GURL request_url = https_test_server.GetURL("/auth-basic"); |
| 7528 | |
| 7529 | TestNetworkErrorLoggingService nel_service; |
| 7530 | TestURLRequestContext context(true); |
| 7531 | context.set_network_error_logging_service(&nel_service); |
| 7532 | context.Init(); |
| 7533 | |
| 7534 | // populate the cache |
| 7535 | { |
| 7536 | TestDelegate d; |
| 7537 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7538 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 7539 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7540 | r->Start(); |
| 7541 | d.RunUntilComplete(); |
| 7542 | } |
| 7543 | ASSERT_EQ(2u, nel_service.errors().size()); |
| 7544 | const TestNetworkErrorLoggingService::RequestDetails& error1 = |
| 7545 | nel_service.errors()[0]; |
| 7546 | EXPECT_EQ(request_url, error1.uri); |
| 7547 | EXPECT_EQ(401, error1.status_code); |
| 7548 | EXPECT_EQ(OK, error1.type); |
| 7549 | const TestNetworkErrorLoggingService::RequestDetails& error2 = |
| 7550 | nel_service.errors()[1]; |
| 7551 | EXPECT_EQ(request_url, error2.uri); |
| 7552 | EXPECT_EQ(200, error2.status_code); |
| 7553 | EXPECT_EQ(OK, error2.type); |
| 7554 | |
| 7555 | // repeat request with end-to-end validation. since auth-basic results in a |
| 7556 | // cachable page, we expect this test to result in a 304. in which case, the |
| 7557 | // response should be fetched from the cache. |
| 7558 | { |
| 7559 | TestDelegate d; |
| 7560 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7561 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 7562 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7563 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 7564 | r->Start(); |
| 7565 | d.RunUntilComplete(); |
| 7566 | |
| 7567 | // Should be the same cached document. |
| 7568 | EXPECT_TRUE(r->was_cached()); |
| 7569 | } |
| 7570 | ASSERT_EQ(3u, nel_service.errors().size()); |
| 7571 | const TestNetworkErrorLoggingService::RequestDetails& error3 = |
| 7572 | nel_service.errors()[2]; |
| 7573 | EXPECT_EQ(request_url, error3.uri); |
| 7574 | EXPECT_EQ(304, error3.status_code); |
| 7575 | EXPECT_EQ(OK, error3.type); |
| 7576 | } |
| 7577 | |
| 7578 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_CancelInResponseStarted) { |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7579 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7580 | https_test_server.ServeFilesFromSourceDirectory( |
| 7581 | base::FilePath(kTestFilePath)); |
| 7582 | ASSERT_TRUE(https_test_server.Start()); |
| 7583 | GURL request_url = https_test_server.GetURL("/simple.html"); |
| 7584 | |
| 7585 | TestNetworkErrorLoggingService nel_service; |
| 7586 | TestURLRequestContext context(true); |
| 7587 | context.set_network_error_logging_service(&nel_service); |
| 7588 | context.Init(); |
| 7589 | |
| 7590 | TestDelegate d; |
| 7591 | d.set_cancel_in_response_started(true); |
| 7592 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7593 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7594 | request->Start(); |
| 7595 | d.RunUntilComplete(); |
| 7596 | |
| 7597 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7598 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7599 | nel_service.errors()[0]; |
| 7600 | EXPECT_EQ(request_url, error.uri); |
| 7601 | EXPECT_EQ(200, error.status_code); |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 7602 | // Headers were received and the body should have been read but was not. |
| 7603 | EXPECT_EQ(ERR_ABORTED, error.type); |
| 7604 | } |
| 7605 | |
| 7606 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_CancelOnDataReceived) { |
| 7607 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7608 | https_test_server.ServeFilesFromSourceDirectory( |
| 7609 | base::FilePath(kTestFilePath)); |
| 7610 | ASSERT_TRUE(https_test_server.Start()); |
| 7611 | GURL request_url = https_test_server.GetURL("/simple.html"); |
| 7612 | |
| 7613 | TestNetworkErrorLoggingService nel_service; |
| 7614 | TestURLRequestContext context(true); |
| 7615 | context.set_network_error_logging_service(&nel_service); |
| 7616 | context.Init(); |
| 7617 | |
| 7618 | TestDelegate d; |
| 7619 | d.set_cancel_in_received_data(true); |
| 7620 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7621 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7622 | request->Start(); |
| 7623 | d.RunUntilComplete(); |
| 7624 | |
| 7625 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7626 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7627 | nel_service.errors()[0]; |
| 7628 | EXPECT_EQ(request_url, error.uri); |
| 7629 | EXPECT_EQ(200, error.status_code); |
| 7630 | // Data was received but the body was not completely read. |
| 7631 | EXPECT_EQ(ERR_ABORTED, error.type); |
| 7632 | } |
| 7633 | |
| 7634 | TEST_F(URLRequestTestHTTP, NetworkErrorLogging_CancelRedirect) { |
| 7635 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7636 | https_test_server.ServeFilesFromSourceDirectory( |
| 7637 | base::FilePath(kTestFilePath)); |
| 7638 | ASSERT_TRUE(https_test_server.Start()); |
| 7639 | GURL request_url = https_test_server.GetURL("/redirect-test.html"); |
| 7640 | |
| 7641 | TestNetworkErrorLoggingService nel_service; |
| 7642 | TestURLRequestContext context(true); |
| 7643 | context.set_network_error_logging_service(&nel_service); |
| 7644 | context.Init(); |
| 7645 | |
| 7646 | TestDelegate d; |
| 7647 | d.set_cancel_in_received_redirect(true); |
| 7648 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7649 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7650 | request->Start(); |
| 7651 | d.RunUntilComplete(); |
| 7652 | |
| 7653 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7654 | const TestNetworkErrorLoggingService::RequestDetails& error = |
| 7655 | nel_service.errors()[0]; |
| 7656 | EXPECT_EQ(request_url, error.uri); |
| 7657 | EXPECT_EQ(302, error.status_code); |
| 7658 | // A valid HTTP response was received, even though the request was cancelled. |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 7659 | EXPECT_EQ(OK, error.type); |
| 7660 | } |
| 7661 | |
| 7662 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 7663 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7664 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7665 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7666 | |
| 7667 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7668 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7669 | http_test_server()->GetURL("/content-type-normalization.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7670 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7671 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7672 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7673 | |
| 7674 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7675 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7676 | EXPECT_EQ("text/html", mime_type); |
| 7677 | |
| 7678 | std::string charset; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7679 | req->GetCharset(&charset); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7680 | EXPECT_EQ("utf-8", charset); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7681 | req->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7682 | } |
| 7683 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7684 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) { |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7685 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7686 | GURL data_url("data:,foo"); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7687 | DataProtocolHandler data_protocol_handler; |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7688 | EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7689 | |
| 7690 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7691 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7692 | } |
| 7693 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7694 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7695 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) { |
| 7696 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
| 7697 | GURL file_url("file:///foo.txt"); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 7698 | FileProtocolHandler file_protocol_handler( |
| 7699 | base::ThreadTaskRunnerHandle::Get()); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7700 | EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url)); |
| 7701 | |
| 7702 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7703 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(file_url)); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7704 | } |
| 7705 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7706 | TEST_F(URLRequestTestHTTP, RestrictFileRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7707 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7708 | |
| 7709 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7710 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7711 | http_test_server()->GetURL("/redirect-to-file.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7712 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7713 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7714 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7715 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7716 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7717 | |
| 7718 | // The redirect should have been rejected before reporting it to the caller. |
| 7719 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7720 | } |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7721 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7722 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7723 | TEST_F(URLRequestTestHTTP, RestrictDataRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7724 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7725 | |
| 7726 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7727 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7728 | http_test_server()->GetURL("/redirect-to-data.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7729 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7730 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7731 | d.RunUntilComplete(); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7732 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7733 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7734 | |
| 7735 | // The redirect should have been rejected before reporting it to the |
| 7736 | // caller. See https://crbug.com/723796 |
| 7737 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7738 | } |
| 7739 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7740 | // Test that redirects to invalid URLs are rejected. See |
| 7741 | // https://crbug.com/462272. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7742 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7743 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7744 | |
| 7745 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7746 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7747 | http_test_server()->GetURL("/redirect-to-invalid-url.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7748 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7749 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7750 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7751 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7752 | EXPECT_EQ(1, d.response_started_count()); |
| 7753 | EXPECT_EQ(ERR_INVALID_REDIRECT, d.request_status()); |
| 7754 | |
| 7755 | // The redirect should have been rejected before reporting it to the caller. |
| 7756 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7757 | } |
| 7758 | |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7759 | // Make sure redirects are cached, despite not reading their bodies. |
| 7760 | TEST_F(URLRequestTestHTTP, CacheRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7761 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7762 | GURL redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7763 | http_test_server()->GetURL("/redirect302-to-echo-cacheable"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7764 | |
| 7765 | { |
| 7766 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7767 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7768 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7769 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7770 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7771 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7772 | EXPECT_EQ(1, d.received_redirect_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7773 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7774 | } |
| 7775 | |
| 7776 | { |
| 7777 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7778 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7779 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7780 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 7781 | d.RunUntilRedirect(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7782 | |
| 7783 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7784 | EXPECT_EQ(0, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7785 | EXPECT_TRUE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7786 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 7787 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 7788 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7789 | d.RunUntilComplete(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7790 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7791 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7792 | EXPECT_EQ(OK, d.request_status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7793 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7794 | } |
| 7795 | } |
| 7796 | |
| 7797 | // Make sure a request isn't cached when a NetworkDelegate forces a redirect |
| 7798 | // when the headers are read, since the body won't have been read. |
| 7799 | TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7800 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7801 | // URL that is normally cached. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7802 | GURL initial_url = http_test_server()->GetURL("/cachetime"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7803 | |
| 7804 | { |
| 7805 | // Set up the TestNetworkDelegate tp force a redirect. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7806 | GURL redirect_to_url = http_test_server()->GetURL("/echo"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7807 | default_network_delegate_.set_redirect_on_headers_received_url( |
| 7808 | redirect_to_url); |
| 7809 | |
| 7810 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7811 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7812 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7813 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7814 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7815 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7816 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7817 | EXPECT_EQ(redirect_to_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7818 | } |
| 7819 | |
| 7820 | { |
| 7821 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7822 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7823 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7824 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7825 | d.RunUntilComplete(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7826 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7827 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7828 | EXPECT_FALSE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7829 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7830 | EXPECT_EQ(initial_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7831 | } |
| 7832 | } |
| 7833 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7834 | // Tests that redirection to an unsafe URL is allowed when it has been marked as |
| 7835 | // safe. |
| 7836 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7837 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7838 | |
| 7839 | GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url"); |
| 7840 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7841 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7842 | |
| 7843 | TestDelegate d; |
| 7844 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7845 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7846 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 7847 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7848 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7849 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7850 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7851 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7852 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7853 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7854 | EXPECT_EQ(unsafe_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7855 | EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received()); |
| 7856 | } |
| 7857 | } |
| 7858 | |
| 7859 | // Tests that a redirect to a different unsafe URL is blocked, even after adding |
| 7860 | // some other URL to the whitelist. |
| 7861 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7862 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7863 | |
| 7864 | GURL unsafe_url("data:text/html,something"); |
| 7865 | GURL different_unsafe_url("data:text/html,something-else"); |
| 7866 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7867 | default_network_delegate_.set_allowed_unsafe_redirect_url( |
| 7868 | different_unsafe_url); |
| 7869 | |
| 7870 | TestDelegate d; |
| 7871 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7872 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7873 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 7874 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7875 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7876 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7877 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7878 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7879 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7880 | |
| 7881 | // The redirect should have been rejected before reporting it to the caller. |
| 7882 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7883 | } |
| 7884 | } |
| 7885 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7886 | // Redirects from an URL with fragment to an unsafe URL with fragment should |
| 7887 | // be allowed, and the reference fragment of the target URL should be preserved. |
| 7888 | TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7889 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7890 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7891 | GURL original_url(http_test_server()->GetURL("/original#fragment1")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7892 | GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 7893 | GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 7894 | |
| 7895 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7896 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7897 | |
| 7898 | TestDelegate d; |
| 7899 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7900 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7901 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7902 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7903 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7904 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7905 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7906 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7907 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7908 | EXPECT_EQ(original_url, r->original_url()); |
| 7909 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7910 | } |
| 7911 | } |
| 7912 | |
| 7913 | // When a delegate has specified a safe redirect URL, but it does not match the |
| 7914 | // redirect target, then do not prevent the reference fragment from being added. |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7915 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7916 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7917 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7918 | GURL original_url(http_test_server()->GetURL("/original#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7919 | GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7920 | GURL redirect_url(http_test_server()->GetURL("/target")); |
| 7921 | GURL expected_redirect_url( |
| 7922 | http_test_server()->GetURL("/target#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7923 | |
| 7924 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 7925 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7926 | |
| 7927 | TestDelegate d; |
| 7928 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7929 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7930 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7931 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7932 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7933 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7934 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7935 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7936 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7937 | EXPECT_EQ(original_url, r->original_url()); |
| 7938 | EXPECT_EQ(expected_redirect_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7939 | } |
| 7940 | } |
| 7941 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7942 | // When a delegate has specified a safe redirect URL, assume that the redirect |
| 7943 | // URL should not be changed. In particular, the reference fragment should not |
| 7944 | // be modified. |
| 7945 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7946 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7947 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7948 | GURL original_url( |
| 7949 | http_test_server()->GetURL("/original#should-not-be-appended")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7950 | GURL redirect_url("data:text/html,expect-no-reference-fragment"); |
| 7951 | |
| 7952 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 7953 | default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url); |
| 7954 | |
| 7955 | TestDelegate d; |
| 7956 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7957 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7958 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7959 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7960 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7961 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7962 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7963 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7964 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7965 | EXPECT_EQ(original_url, r->original_url()); |
| 7966 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7967 | } |
| 7968 | } |
| 7969 | |
| 7970 | // When a URLRequestRedirectJob is created, the redirection must be followed and |
| 7971 | // the reference fragment of the target URL must not be modified. |
| 7972 | TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7973 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7974 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7975 | GURL original_url( |
| 7976 | http_test_server()->GetURL("/original#should-not-be-appended")); |
| 7977 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7978 | |
| 7979 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7980 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7981 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7982 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7983 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7984 | r.get(), &default_network_delegate_, redirect_url, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7985 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 7986 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7987 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7988 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7989 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7990 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7991 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7992 | EXPECT_EQ(original_url, r->original_url()); |
| 7993 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7994 | } |
| 7995 | |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7996 | TEST_F(URLRequestTestHTTP, UnsupportedReferrerScheme) { |
| 7997 | ASSERT_TRUE(http_test_server()->Start()); |
| 7998 | |
| 7999 | const std::string referrer("foobar://totally.legit.referrer"); |
| 8000 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8001 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8002 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8003 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 8004 | req->SetReferrer(referrer); |
| 8005 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8006 | d.RunUntilComplete(); |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 8007 | |
| 8008 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 8009 | } |
| 8010 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8011 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8012 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8013 | |
| 8014 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8015 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8016 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8017 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8018 | req->SetReferrer("http://user:[email protected]/"); |
| 8019 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8020 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8021 | |
| 8022 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 8023 | } |
| 8024 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8025 | TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8026 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8027 | |
| 8028 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8029 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8030 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8031 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8032 | req->SetReferrer("http://foo.com/test#fragment"); |
| 8033 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8034 | d.RunUntilComplete(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8035 | |
| 8036 | EXPECT_EQ(std::string("http://foo.com/test"), d.data_received()); |
| 8037 | } |
| 8038 | |
| 8039 | TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8040 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8041 | |
| 8042 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8043 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8044 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8045 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8046 | req->SetReferrer("http://foo.com/test#fragment"); |
| 8047 | req->SetReferrer(""); |
| 8048 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8049 | d.RunUntilComplete(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8050 | |
| 8051 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 8052 | } |
| 8053 | |
Mike West | fbd4411 | 2019-05-08 18:23:37 | [diff] [blame] | 8054 | TEST_F(URLRequestTestHTTP, CapRefererDisabled) { |
| 8055 | ASSERT_TRUE(http_test_server()->Start()); |
| 8056 | |
| 8057 | // Create a string, and pad it out to ~10k with a very exciting path. |
| 8058 | std::string long_referer_header = "http://foo.com/"; |
| 8059 | long_referer_header.resize(10000, 'a'); |
| 8060 | |
| 8061 | // If the feature isn't enabled, a long `referer` will remain long. |
| 8062 | TestDelegate d; |
| 8063 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8064 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8065 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8066 | req->SetReferrer(long_referer_header); |
| 8067 | req->Start(); |
| 8068 | d.RunUntilComplete(); |
| 8069 | |
| 8070 | EXPECT_EQ(long_referer_header, d.data_received()); |
| 8071 | } |
| 8072 | |
| 8073 | TEST_F(URLRequestTestHTTP, CapRefererHeaderLengthEnabled) { |
| 8074 | ASSERT_TRUE(http_test_server()->Start()); |
| 8075 | |
| 8076 | // Create a string, and pad it out to ~10k with a very exciting path. |
| 8077 | std::string long_referer_header = "http://foo.com/"; |
| 8078 | long_referer_header.resize(10000, 'a'); |
| 8079 | |
| 8080 | // If the feature is enabled without params, a `referer` longer than 4096 |
| 8081 | // bytes will be shortened. |
| 8082 | { |
| 8083 | TestDelegate d; |
| 8084 | base::test::ScopedFeatureList feature_list; |
| 8085 | feature_list.InitAndEnableFeature(features::kCapRefererHeaderLength); |
| 8086 | |
| 8087 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8088 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8089 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8090 | req->SetReferrer(long_referer_header); |
| 8091 | req->Start(); |
| 8092 | d.RunUntilComplete(); |
| 8093 | |
| 8094 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 8095 | } |
| 8096 | |
| 8097 | // If the feature is enabled with params, they will govern the shortening |
| 8098 | // behavior as expected. The following three tests verify behavior for a |
| 8099 | // param larger than the referrer length, exactly the same as the string |
| 8100 | // length, and shorter than the string length. |
| 8101 | { |
| 8102 | TestDelegate d; |
| 8103 | std::map<std::string, std::string> params; |
| 8104 | params["MaxRefererHeaderLength"] = |
| 8105 | base::NumberToString(long_referer_header.length() + 1); |
| 8106 | |
| 8107 | base::test::ScopedFeatureList feature_list; |
| 8108 | feature_list.InitAndEnableFeatureWithParameters( |
| 8109 | features::kCapRefererHeaderLength, params); |
| 8110 | |
| 8111 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8112 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8113 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8114 | req->SetReferrer(long_referer_header); |
| 8115 | req->Start(); |
| 8116 | d.RunUntilComplete(); |
| 8117 | |
| 8118 | EXPECT_EQ(long_referer_header, d.data_received()); |
| 8119 | } |
| 8120 | |
| 8121 | { |
| 8122 | TestDelegate d; |
| 8123 | std::map<std::string, std::string> params; |
| 8124 | params["MaxRefererHeaderLength"] = |
| 8125 | base::NumberToString(long_referer_header.length()); |
| 8126 | |
| 8127 | base::test::ScopedFeatureList feature_list; |
| 8128 | feature_list.InitAndEnableFeatureWithParameters( |
| 8129 | features::kCapRefererHeaderLength, params); |
| 8130 | |
| 8131 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8132 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8133 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8134 | req->SetReferrer(long_referer_header); |
| 8135 | req->Start(); |
| 8136 | d.RunUntilComplete(); |
| 8137 | |
| 8138 | EXPECT_EQ(long_referer_header, d.data_received()); |
| 8139 | } |
| 8140 | |
| 8141 | { |
| 8142 | TestDelegate d; |
| 8143 | std::map<std::string, std::string> params; |
| 8144 | params["MaxRefererHeaderLength"] = |
| 8145 | base::NumberToString(long_referer_header.length() - 1); |
| 8146 | |
| 8147 | base::test::ScopedFeatureList feature_list; |
| 8148 | feature_list.InitAndEnableFeatureWithParameters( |
| 8149 | features::kCapRefererHeaderLength, params); |
| 8150 | |
| 8151 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8152 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8153 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8154 | req->SetReferrer(long_referer_header); |
| 8155 | req->Start(); |
| 8156 | d.RunUntilComplete(); |
| 8157 | |
| 8158 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 8159 | } |
| 8160 | } |
| 8161 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8162 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8163 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8164 | |
| 8165 | TestDelegate d; |
| 8166 | { |
| 8167 | d.set_cancel_in_received_redirect(true); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8168 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8169 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 8170 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8171 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8172 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8173 | |
| 8174 | EXPECT_EQ(1, d.response_started_count()); |
| 8175 | EXPECT_EQ(0, d.bytes_received()); |
| 8176 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8177 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8178 | } |
| 8179 | } |
| 8180 | |
| 8181 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8182 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8183 | |
| 8184 | TestDelegate d; |
| 8185 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8186 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8187 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8188 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8189 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8190 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 8191 | d.RunUntilRedirect(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8192 | |
| 8193 | EXPECT_EQ(1, d.received_redirect_count()); |
Shivani Sharma | c18f976 | 2017-10-23 16:43:23 | [diff] [blame] | 8194 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8195 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 8196 | d.ClearFullRequestHeaders(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8197 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8198 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 8199 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8200 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8201 | |
| 8202 | EXPECT_EQ(1, d.response_started_count()); |
| 8203 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8204 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8205 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 8206 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 8207 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 8208 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8209 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 8210 | |
| 8211 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 8212 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8213 | EXPECT_EQ(contents, d.data_received()); |
| 8214 | } |
| 8215 | } |
| 8216 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8217 | TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8218 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8219 | |
| 8220 | TestDelegate d; |
| 8221 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8222 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8223 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8224 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8225 | |
| 8226 | EXPECT_FALSE(d.have_full_request_headers()); |
| 8227 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8228 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 8229 | d.RunUntilRedirect(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8230 | |
| 8231 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8232 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8233 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 8234 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8235 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8236 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8237 | GURL target_url(http_test_server()->GetURL("/with-headers.html")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8238 | EXPECT_EQ(1, d.response_started_count()); |
| 8239 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8240 | CheckFullRequestHeaders(d.full_request_headers(), target_url); |
| 8241 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8242 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8243 | |
| 8244 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 8245 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 8246 | path = path.Append(kTestFilePath); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8247 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 8248 | |
| 8249 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 8250 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8251 | EXPECT_EQ(contents, d.data_received()); |
| 8252 | } |
| 8253 | } |
| 8254 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8255 | TEST_F(URLRequestTestHTTP, DeferredRedirect_ModifiedHeaders) { |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8256 | ASSERT_TRUE(http_test_server()->Start()); |
| 8257 | |
| 8258 | TestDelegate d; |
| 8259 | { |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8260 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8261 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8262 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8263 | |
| 8264 | // Set initial headers for the request. |
| 8265 | req->SetExtraRequestHeaderByName("Header1", "Value1", true /* overwrite */); |
| 8266 | req->SetExtraRequestHeaderByName("Header2", "Value2", true /* overwrite */); |
| 8267 | |
| 8268 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 8269 | d.RunUntilRedirect(); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8270 | |
| 8271 | // Initial request should only have initial headers. |
| 8272 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8273 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8274 | const HttpRequestHeaders& sent_headers1 = d.full_request_headers(); |
| 8275 | std::string sent_value; |
| 8276 | EXPECT_TRUE(sent_headers1.GetHeader("Header1", &sent_value)); |
| 8277 | EXPECT_EQ("Value1", sent_value); |
| 8278 | EXPECT_TRUE(sent_headers1.GetHeader("Header2", &sent_value)); |
| 8279 | EXPECT_EQ("Value2", sent_value); |
| 8280 | EXPECT_FALSE(sent_headers1.GetHeader("Header3", &sent_value)); |
| 8281 | d.ClearFullRequestHeaders(); |
| 8282 | |
| 8283 | // Overwrite Header2 and add Header3. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8284 | net::HttpRequestHeaders modified_headers; |
| 8285 | modified_headers.SetHeader("Header2", ""); |
| 8286 | modified_headers.SetHeader("Header3", "Value3"); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8287 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8288 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 8289 | modified_headers); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8290 | d.RunUntilComplete(); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8291 | |
| 8292 | EXPECT_EQ(1, d.response_started_count()); |
| 8293 | EXPECT_FALSE(d.received_data_before_response()); |
| 8294 | EXPECT_EQ(OK, d.request_status()); |
| 8295 | |
| 8296 | // Redirected request should also have modified headers. |
| 8297 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8298 | const HttpRequestHeaders& sent_headers2 = d.full_request_headers(); |
| 8299 | EXPECT_TRUE(sent_headers2.GetHeader("Header1", &sent_value)); |
| 8300 | EXPECT_EQ("Value1", sent_value); |
| 8301 | EXPECT_TRUE(sent_headers2.GetHeader("Header2", &sent_value)); |
| 8302 | EXPECT_EQ("", sent_value); |
| 8303 | EXPECT_TRUE(sent_headers2.GetHeader("Header3", &sent_value)); |
| 8304 | EXPECT_EQ("Value3", sent_value); |
| 8305 | } |
| 8306 | } |
| 8307 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 8308 | TEST_F(URLRequestTestHTTP, DeferredRedirect_RemovedHeaders) { |
| 8309 | ASSERT_TRUE(http_test_server()->Start()); |
| 8310 | |
| 8311 | TestDelegate d; |
| 8312 | { |
| 8313 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
| 8314 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 8315 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8316 | |
| 8317 | // Set initial headers for the request. |
| 8318 | req->SetExtraRequestHeaderByName("Header1", "Value1", true /* overwrite */); |
| 8319 | req->SetExtraRequestHeaderByName("Header2", "Value2", true /* overwrite */); |
| 8320 | |
| 8321 | req->Start(); |
| 8322 | d.RunUntilRedirect(); |
| 8323 | |
| 8324 | // Initial request should have initial headers. |
| 8325 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8326 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8327 | const HttpRequestHeaders& sent_headers1 = d.full_request_headers(); |
| 8328 | std::string sent_value; |
| 8329 | EXPECT_TRUE(sent_headers1.GetHeader("Header1", &sent_value)); |
| 8330 | EXPECT_EQ("Value1", sent_value); |
| 8331 | EXPECT_TRUE(sent_headers1.GetHeader("Header2", &sent_value)); |
| 8332 | EXPECT_EQ("Value2", sent_value); |
| 8333 | d.ClearFullRequestHeaders(); |
| 8334 | |
| 8335 | // Keep Header1 and remove Header2. |
| 8336 | std::vector<std::string> removed_headers({"Header2"}); |
| 8337 | req->FollowDeferredRedirect(removed_headers, |
| 8338 | base::nullopt /* modified_headers */); |
| 8339 | d.RunUntilComplete(); |
| 8340 | |
| 8341 | EXPECT_EQ(1, d.response_started_count()); |
| 8342 | EXPECT_FALSE(d.received_data_before_response()); |
| 8343 | EXPECT_EQ(OK, d.request_status()); |
| 8344 | |
| 8345 | // Redirected request should also have |
| 8346 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8347 | const HttpRequestHeaders& sent_headers2 = d.full_request_headers(); |
| 8348 | EXPECT_TRUE(sent_headers2.GetHeader("Header1", &sent_value)); |
| 8349 | EXPECT_EQ("Value1", sent_value); |
| 8350 | EXPECT_FALSE(sent_headers2.GetHeader("Header2", &sent_value)); |
| 8351 | } |
| 8352 | } |
| 8353 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8354 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8355 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8356 | |
| 8357 | TestDelegate d; |
| 8358 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8359 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8360 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 8361 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8362 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 8363 | d.RunUntilRedirect(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8364 | |
| 8365 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8366 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8367 | req->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8368 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8369 | |
| 8370 | EXPECT_EQ(1, d.response_started_count()); |
| 8371 | EXPECT_EQ(0, d.bytes_received()); |
| 8372 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8373 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8374 | } |
| 8375 | } |
| 8376 | |
| 8377 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8378 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8379 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8380 | // Populate the cache. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8381 | { |
| 8382 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8383 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8384 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8385 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8386 | HttpRequestHeaders headers; |
| 8387 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8388 | req->SetExtraRequestHeaders(headers); |
| 8389 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8390 | d.RunUntilComplete(); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8391 | |
| 8392 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8393 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8394 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8395 | } |
| 8396 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8397 | // Expect a cache hit. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8398 | { |
| 8399 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8400 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8401 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8402 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8403 | HttpRequestHeaders headers; |
| 8404 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8405 | req->SetExtraRequestHeaders(headers); |
| 8406 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8407 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8408 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8409 | EXPECT_TRUE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8410 | |
| 8411 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8412 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8413 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8414 | } |
| 8415 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8416 | // Expect a cache miss. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8417 | { |
| 8418 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8419 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8420 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8421 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8422 | HttpRequestHeaders headers; |
| 8423 | headers.SetHeader("foo", "2"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8424 | req->SetExtraRequestHeaders(headers); |
| 8425 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8426 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8427 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8428 | EXPECT_FALSE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8429 | |
| 8430 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8431 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8432 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8433 | } |
| 8434 | } |
| 8435 | |
| 8436 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8437 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8438 | |
| 8439 | // populate the cache |
| 8440 | { |
| 8441 | TestDelegate d; |
| 8442 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8443 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8444 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8445 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8446 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8447 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8448 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8449 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8450 | |
| 8451 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8452 | } |
| 8453 | |
| 8454 | // repeat request with end-to-end validation. since auth-basic results in a |
| 8455 | // cachable page, we expect this test to result in a 304. in which case, the |
| 8456 | // response should be fetched from the cache. |
| 8457 | { |
| 8458 | TestDelegate d; |
| 8459 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8460 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8461 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8462 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8463 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8464 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 8465 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8466 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8467 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8468 | |
| 8469 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8470 | |
| 8471 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8472 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8473 | } |
| 8474 | } |
| 8475 | |
| 8476 | // Check that Set-Cookie headers in 401 responses are respected. |
| 8477 | // http://crbug.com/6450 |
| 8478 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8479 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8480 | |
| 8481 | GURL url_requiring_auth = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8482 | http_test_server()->GetURL("/auth-basic?set-cookie-if-challenged"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8483 | |
| 8484 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 8485 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 8486 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8487 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8488 | TestURLRequestContext context(true); |
| 8489 | context.set_network_delegate(&network_delegate); |
| 8490 | context.Init(); |
| 8491 | |
| 8492 | TestDelegate d; |
| 8493 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8494 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8495 | std::unique_ptr<URLRequest> r( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8496 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &d, |
| 8497 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8498 | r->set_site_for_cookies(url_requiring_auth); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8499 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8500 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8501 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8502 | |
| 8503 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8504 | |
| 8505 | // Make sure we sent the cookie in the restarted transaction. |
| 8506 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 8507 | != std::string::npos); |
| 8508 | } |
| 8509 | |
| 8510 | // Same test as above, except this time the restart is initiated earlier |
| 8511 | // (without user intervention since identity is embedded in the URL). |
| 8512 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8513 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8514 | TestURLRequestContext context(true); |
| 8515 | context.set_network_delegate(&network_delegate); |
| 8516 | context.Init(); |
| 8517 | |
| 8518 | TestDelegate d; |
| 8519 | |
| 8520 | GURL::Replacements replacements; |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 8521 | replacements.SetUsernameStr("user2"); |
| 8522 | replacements.SetPasswordStr("secret"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8523 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 8524 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8525 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 8526 | url_with_identity, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8527 | r->set_site_for_cookies(url_with_identity); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8528 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8529 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8530 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8531 | |
| 8532 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 8533 | |
| 8534 | // Make sure we sent the cookie in the restarted transaction. |
| 8535 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 8536 | != std::string::npos); |
| 8537 | } |
| 8538 | } |
| 8539 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8540 | TEST_F(URLRequestTest, CatchFilteredCookies) { |
| 8541 | HttpTestServer test_server; |
| 8542 | ASSERT_TRUE(test_server.Start()); |
| 8543 | |
| 8544 | FilteringTestLayeredNetworkDelegate network_delegate( |
| 8545 | std::make_unique<TestNetworkDelegate>()); |
| 8546 | network_delegate.SetCookieFilter("not_stored_cookie"); |
| 8547 | network_delegate.set_block_get_cookies(); |
| 8548 | TestURLRequestContext context(true); |
| 8549 | context.set_network_delegate(&network_delegate); |
| 8550 | context.Init(); |
| 8551 | // Make sure cookies blocked from being stored are caught. |
| 8552 | { |
| 8553 | TestDelegate d; |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8554 | GURL test_url = test_server.GetURL("/set-cookie?not_stored_cookie=true"); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8555 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8556 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8557 | req->set_site_for_cookies(test_url); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8558 | req->Start(); |
| 8559 | d.RunUntilComplete(); |
| 8560 | |
| 8561 | ASSERT_EQ(1u, req->not_stored_cookies().size()); |
| 8562 | EXPECT_EQ("not_stored_cookie", |
| 8563 | req->not_stored_cookies().front().cookie->Name()); |
| 8564 | EXPECT_EQ( |
| 8565 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES, |
| 8566 | req->not_stored_cookies().front().status); |
| 8567 | } |
| 8568 | // Set a cookie to be blocked later |
| 8569 | { |
| 8570 | TestDelegate d; |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8571 | GURL test_url = test_server.GetURL("/set-cookie?not_sent_cookies=true"); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8572 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8573 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8574 | req->set_site_for_cookies(test_url); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8575 | req->Start(); |
| 8576 | d.RunUntilComplete(); |
| 8577 | } |
| 8578 | { |
| 8579 | TestDelegate d; |
| 8580 | // Make sure cookies blocked from being sent are caught. |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8581 | GURL test_url = test_server.GetURL("/echoheader?Cookie"); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8582 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8583 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8584 | req->set_site_for_cookies(test_url); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8585 | req->Start(); |
| 8586 | d.RunUntilComplete(); |
| 8587 | |
| 8588 | EXPECT_TRUE(d.data_received().find("not_sent_cookies=true") == |
| 8589 | std::string::npos); |
| 8590 | |
| 8591 | ASSERT_EQ(1u, req->not_sent_cookies().size()); |
| 8592 | EXPECT_EQ("not_sent_cookies", |
| 8593 | req->not_sent_cookies().front().cookie.Name()); |
| 8594 | EXPECT_EQ( |
| 8595 | net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_USER_PREFERENCES, |
| 8596 | req->not_sent_cookies().front().status); |
| 8597 | } |
| 8598 | } |
| 8599 | |
| 8600 | TEST_F(URLRequestTestHTTP, AuthChallengeWithFilteredCookies) { |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8601 | ASSERT_TRUE(http_test_server()->Start()); |
| 8602 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8603 | GURL url_requiring_auth = |
| 8604 | http_test_server()->GetURL("/auth-basic?set-cookie-if-challenged"); |
| 8605 | GURL url_requiring_auth_wo_cookies = |
| 8606 | http_test_server()->GetURL("/auth-basic"); |
| 8607 | // Check not_stored_cookies is populated first round trip, and cleared on the |
| 8608 | // second. |
| 8609 | { |
| 8610 | FilteringTestLayeredNetworkDelegate filtering_network_delegate( |
| 8611 | std::make_unique<TestNetworkDelegate>()); |
| 8612 | filtering_network_delegate.SetCookieFilter("got_challenged"); |
| 8613 | TestURLRequestContext context(true); |
| 8614 | context.set_network_delegate(&filtering_network_delegate); |
| 8615 | context.Init(); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8616 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8617 | TestDelegate delegate; |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8618 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8619 | std::unique_ptr<URLRequest> request( |
| 8620 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &delegate, |
| 8621 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8622 | request->set_site_for_cookies(url_requiring_auth); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8623 | request->Start(); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8624 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8625 | delegate.RunUntilAuthRequired(); |
| 8626 | // Make sure it was blocked once. |
| 8627 | EXPECT_EQ(1, filtering_network_delegate.blocked_set_cookie_count()); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8628 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8629 | // The number of cookies blocked from the most recent round trip. |
| 8630 | ASSERT_EQ(1u, request->not_stored_cookies().size()); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8631 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8632 | // Now check the second round trip |
| 8633 | request->SetAuth(AuthCredentials(kUser, kSecret)); |
| 8634 | delegate.RunUntilComplete(); |
| 8635 | EXPECT_THAT(delegate.request_status(), IsOk()); |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8636 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8637 | // There are DCHECKs in URLRequestHttpJob that would fail if |
| 8638 | // not_sent_cookies and not_stored_cookies were not cleared properly. |
| 8639 | |
| 8640 | // Make sure the cookie was actually filtered and not sent. |
| 8641 | EXPECT_EQ(std::string::npos, |
| 8642 | delegate.data_received().find("Cookie: got_challenged=true")); |
| 8643 | |
| 8644 | // The number of cookies blocked from the most recent round trip. |
| 8645 | ASSERT_EQ(0u, request->not_stored_cookies().size()); |
| 8646 | } |
| 8647 | |
| 8648 | // Check not_sent_cookies on first round trip (and cleared for the second). |
| 8649 | { |
| 8650 | FilteringTestLayeredNetworkDelegate filtering_network_delegate( |
| 8651 | std::make_unique<TestNetworkDelegate>()); |
| 8652 | filtering_network_delegate.set_block_get_cookies(); |
| 8653 | TestURLRequestContext context(true); |
| 8654 | context.set_network_delegate(&filtering_network_delegate); |
| 8655 | |
| 8656 | std::unique_ptr<CookieMonster> cm = |
| 8657 | std::make_unique<CookieMonster>(nullptr, nullptr); |
| 8658 | cm->SetCookieWithOptionsAsync(url_requiring_auth_wo_cookies, |
| 8659 | "another_cookie=true", CookieOptions(), |
| 8660 | CookieStore::SetCookiesCallback()); |
| 8661 | context.set_cookie_store(cm.get()); |
| 8662 | context.Init(); |
| 8663 | |
| 8664 | TestDelegate delegate; |
| 8665 | |
| 8666 | std::unique_ptr<URLRequest> request( |
| 8667 | context.CreateRequest(url_requiring_auth_wo_cookies, DEFAULT_PRIORITY, |
| 8668 | &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 8669 | request->set_site_for_cookies(url_requiring_auth_wo_cookies); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8670 | request->Start(); |
| 8671 | |
| 8672 | delegate.RunUntilAuthRequired(); |
| 8673 | |
| 8674 | ASSERT_EQ(1u, request->not_sent_cookies().size()); |
| 8675 | EXPECT_EQ("another_cookie", |
| 8676 | request->not_sent_cookies().front().cookie.Name()); |
| 8677 | EXPECT_EQ("true", request->not_sent_cookies().front().cookie.Value()); |
| 8678 | |
| 8679 | // Check not_sent_cookies on second roundtrip. |
| 8680 | request->set_not_sent_cookies({}); |
| 8681 | cm->DeleteAllAsync(CookieStore::DeleteCallback()); |
| 8682 | cm->SetCookieWithOptionsAsync(url_requiring_auth_wo_cookies, |
| 8683 | "one_more_cookie=true", CookieOptions(), |
| 8684 | CookieStore::SetCookiesCallback()); |
| 8685 | |
| 8686 | request->SetAuth(AuthCredentials(kUser, kSecret)); |
| 8687 | delegate.RunUntilComplete(); |
| 8688 | EXPECT_THAT(delegate.request_status(), IsOk()); |
| 8689 | |
| 8690 | // There are DCHECKs in URLRequestHttpJob that would fail if |
| 8691 | // not_sent_cookies and not_stored_cookies we not cleared properly. |
| 8692 | |
| 8693 | // Make sure the cookie was actually filtered. |
| 8694 | EXPECT_EQ(std::string::npos, |
| 8695 | delegate.data_received().find("Cookie: one_more_cookie=true")); |
| 8696 | // got_challenged was set after the first request and blocked on the second, |
| 8697 | // so it should only have been blocked this time |
| 8698 | EXPECT_EQ(2, filtering_network_delegate.blocked_get_cookie_count()); |
| 8699 | |
| 8700 | // // The number of cookies blocked from the most recent round trip. |
| 8701 | ASSERT_EQ(1u, request->not_sent_cookies().size()); |
| 8702 | EXPECT_EQ("one_more_cookie", |
| 8703 | request->not_sent_cookies().front().cookie.Name()); |
| 8704 | } |
Aaron Tagliaboschi | 028009e | 2019-03-07 16:28:06 | [diff] [blame] | 8705 | } |
| 8706 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8707 | // Tests that load timing works as expected with auth and the cache. |
| 8708 | TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8709 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8710 | |
| 8711 | // populate the cache |
| 8712 | { |
| 8713 | TestDelegate d; |
| 8714 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8715 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8716 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8717 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8718 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8719 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8720 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8721 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8722 | |
| 8723 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8724 | |
| 8725 | LoadTimingInfo load_timing_info_before_auth; |
| 8726 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth( |
| 8727 | &load_timing_info_before_auth)); |
| 8728 | TestLoadTimingNotReused(load_timing_info_before_auth, |
| 8729 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 8730 | |
| 8731 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8732 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8733 | // The test server does not support keep alive sockets, so the second |
| 8734 | // request with auth should use a new socket. |
| 8735 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 8736 | EXPECT_NE(load_timing_info_before_auth.socket_log_id, |
| 8737 | load_timing_info.socket_log_id); |
| 8738 | EXPECT_LE(load_timing_info_before_auth.receive_headers_end, |
| 8739 | load_timing_info.connect_timing.connect_start); |
| 8740 | } |
| 8741 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8742 | // Repeat request with end-to-end validation. Since auth-basic results in a |
| 8743 | // 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] | 8744 | // response should be fetched from the cache. |
| 8745 | { |
| 8746 | TestDelegate d; |
| 8747 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8748 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8749 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8750 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8751 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8752 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 8753 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8754 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8755 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8756 | |
| 8757 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8758 | |
| 8759 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8760 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8761 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8762 | // Since there was a request that went over the wire, the load timing |
| 8763 | // information should include connection times. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8764 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8765 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8766 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8767 | } |
| 8768 | } |
| 8769 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8770 | // In this test, we do a POST which the server will 302 redirect. |
| 8771 | // The subsequent transaction should use GET, and should not send the |
| 8772 | // Content-Type header. |
| 8773 | // http://code.google.com/p/chromium/issues/detail?id=843 |
| 8774 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8775 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8776 | |
| 8777 | const char kData[] = "hello world"; |
| 8778 | |
| 8779 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8780 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8781 | http_test_server()->GetURL("/redirect-to-echoall"), DEFAULT_PRIORITY, &d, |
| 8782 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8783 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8784 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8785 | |
| 8786 | // Set headers (some of which are specific to the POST). |
| 8787 | HttpRequestHeaders headers; |
Yeol | 45b9e0a | 2018-07-27 02:03:29 | [diff] [blame] | 8788 | headers.SetHeader("Content-Type", |
| 8789 | "multipart/form-data;" |
| 8790 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ"); |
| 8791 | headers.SetHeader("Accept", |
| 8792 | "text/xml,application/xml,application/xhtml+xml," |
| 8793 | "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); |
| 8794 | headers.SetHeader("Accept-Language", "en-US,en"); |
| 8795 | headers.SetHeader("Accept-Charset", "ISO-8859-1,*,utf-8"); |
| 8796 | headers.SetHeader("Content-Length", "11"); |
| 8797 | headers.SetHeader("Origin", "http://localhost:1337/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8798 | req->SetExtraRequestHeaders(headers); |
| 8799 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8800 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8801 | |
| 8802 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8803 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8804 | EXPECT_EQ("text/html", mime_type); |
| 8805 | |
| 8806 | const std::string& data = d.data_received(); |
| 8807 | |
| 8808 | // Check that the post-specific headers were stripped: |
| 8809 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 8810 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8811 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8812 | |
| 8813 | // These extra request headers should not have been stripped. |
| 8814 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 8815 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 8816 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 8817 | } |
| 8818 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8819 | // The following tests check that we handle mutating the request for HTTP |
| 8820 | // redirects as expected. |
| 8821 | // See https://crbug.com/56373, https://crbug.com/102130, and |
| 8822 | // https://crbug.com/465517. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8823 | |
| 8824 | TEST_F(URLRequestTestHTTP, Redirect301Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8825 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8826 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8827 | const GURL url = http_test_server()->GetURL("/redirect301-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8828 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8829 | http_test_server()->GetURL("/redirect301-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8830 | |
| 8831 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8832 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8833 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8834 | |
| 8835 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8836 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8837 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8838 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8839 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8840 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8841 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8842 | } |
| 8843 | |
| 8844 | TEST_F(URLRequestTestHTTP, Redirect302Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8845 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8846 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8847 | const GURL url = http_test_server()->GetURL("/redirect302-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8848 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8849 | http_test_server()->GetURL("/redirect302-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8850 | |
| 8851 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8852 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8853 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8854 | |
| 8855 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8856 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8857 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8858 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8859 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8860 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8861 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8862 | } |
| 8863 | |
| 8864 | TEST_F(URLRequestTestHTTP, Redirect303Tests) { |
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 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8867 | const GURL url = http_test_server()->GetURL("/redirect303-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8868 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8869 | http_test_server()->GetURL("/redirect303-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8870 | |
| 8871 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8872 | HTTPRedirectMethodTest(url, "PUT", "GET", true); |
| 8873 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8874 | |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8875 | HTTPRedirectOriginHeaderTest(url, "CONNECT", "GET", std::string()); |
| 8876 | HTTPRedirectOriginHeaderTest(https_redirect_url, "CONNECT", "GET", |
| 8877 | std::string()); |
| 8878 | HTTPRedirectOriginHeaderTest(url, "DELETE", "GET", std::string()); |
| 8879 | HTTPRedirectOriginHeaderTest(https_redirect_url, "DELETE", "GET", |
| 8880 | std::string()); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8881 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8882 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8883 | HTTPRedirectOriginHeaderTest(url, "HEAD", "HEAD", url.GetOrigin().spec()); |
| 8884 | HTTPRedirectOriginHeaderTest(https_redirect_url, "HEAD", "HEAD", "null"); |
| 8885 | HTTPRedirectOriginHeaderTest(url, "OPTIONS", "GET", std::string()); |
| 8886 | HTTPRedirectOriginHeaderTest(https_redirect_url, "OPTIONS", "GET", |
| 8887 | std::string()); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8888 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8889 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8890 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8891 | HTTPRedirectOriginHeaderTest(url, "PUT", "GET", std::string()); |
| 8892 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "GET", std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8893 | } |
| 8894 | |
| 8895 | TEST_F(URLRequestTestHTTP, Redirect307Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8896 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8897 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8898 | const GURL url = http_test_server()->GetURL("/redirect307-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8899 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8900 | http_test_server()->GetURL("/redirect307-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8901 | |
| 8902 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 8903 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8904 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8905 | |
| 8906 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8907 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 8908 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 8909 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8910 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8911 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8912 | } |
| 8913 | |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8914 | TEST_F(URLRequestTestHTTP, Redirect308Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8915 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8916 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8917 | const GURL url = http_test_server()->GetURL("/redirect308-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8918 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8919 | http_test_server()->GetURL("/redirect308-to-https"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8920 | |
| 8921 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 8922 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8923 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8924 | |
| 8925 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8926 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 8927 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 8928 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8929 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8930 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8931 | } |
| 8932 | |
| 8933 | // Make sure that 308 responses without bodies are not treated as redirects. |
| 8934 | // Certain legacy apis that pre-date the response code expect this behavior |
| 8935 | // (Like Google Drive). |
| 8936 | TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8937 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8938 | |
| 8939 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8940 | const GURL url = http_test_server()->GetURL("/308-without-location-header"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8941 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8942 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8943 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8944 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8945 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8946 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8947 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8948 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8949 | EXPECT_EQ(308, request->response_headers()->response_code()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8950 | EXPECT_EQ("This is not a redirect.", d.data_received()); |
| 8951 | } |
| 8952 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8953 | TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8954 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8955 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8956 | GURL original_url( |
| 8957 | http_test_server()->GetURL("/redirect302-to-echo#fragment")); |
| 8958 | GURL expected_url(http_test_server()->GetURL("/echo#fragment")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8959 | |
| 8960 | TestDelegate d; |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8961 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8962 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8963 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8964 | r->Start(); |
| 8965 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8966 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8967 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8968 | EXPECT_EQ(OK, d.request_status()); |
| 8969 | EXPECT_EQ(original_url, r->original_url()); |
| 8970 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8971 | } |
| 8972 | |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 8973 | TEST_F(URLRequestTestHTTP, RedirectWithFilteredCookies) { |
| 8974 | ASSERT_TRUE(http_test_server()->Start()); |
| 8975 | |
| 8976 | // FilteringTestLayeredNetworkDelegate filters by name, so the names of the |
| 8977 | // two cookies have to be the same. The values have been set to different |
| 8978 | // strings (the value of the server-redirect cookies is "true" and set-cookie |
| 8979 | // is "other") to differentiate between the two round trips. |
| 8980 | GURL redirect_to( |
| 8981 | http_test_server()->GetURL("/set-cookie?server-redirect=other")); |
| 8982 | |
| 8983 | GURL original_url(http_test_server()->GetURL("/server-redirect-with-cookie?" + |
| 8984 | redirect_to.spec())); |
| 8985 | |
| 8986 | GURL original_url_wo_cookie( |
| 8987 | http_test_server()->GetURL("/server-redirect?" + redirect_to.spec())); |
| 8988 | // Check not_stored_cookies on first round trip. |
| 8989 | { |
| 8990 | FilteringTestLayeredNetworkDelegate filtering_network_delegate( |
| 8991 | std::make_unique<TestNetworkDelegate>()); // Must outlive URLRequest. |
| 8992 | filtering_network_delegate.SetCookieFilter( |
| 8993 | "server-redirect"); // Filter the cookie server-redirect sets. |
| 8994 | TestURLRequestContext context(true); |
| 8995 | context.set_network_delegate(&filtering_network_delegate); |
| 8996 | context.Init(); |
| 8997 | |
| 8998 | TestDelegate delegate; |
| 8999 | std::unique_ptr<URLRequest> request( |
| 9000 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &delegate, |
| 9001 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9002 | |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 9003 | request->set_site_for_cookies(original_url); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9004 | request->Start(); |
| 9005 | delegate.RunUntilRedirect(); |
| 9006 | |
| 9007 | // Make sure it was blocked once. |
| 9008 | EXPECT_EQ(1, filtering_network_delegate.blocked_set_cookie_count()); |
| 9009 | |
| 9010 | // The number of cookies blocked from the most recent round trip. |
| 9011 | ASSERT_EQ(1u, request->not_stored_cookies().size()); |
| 9012 | EXPECT_EQ("server-redirect", |
| 9013 | request->not_stored_cookies().front().cookie->Name()); |
| 9014 | EXPECT_EQ("true", request->not_stored_cookies().front().cookie->Value()); |
| 9015 | |
| 9016 | // Check not_stored_cookies on second round trip (and clearing from the |
| 9017 | // first). |
| 9018 | request->FollowDeferredRedirect(base::nullopt, base::nullopt); |
| 9019 | delegate.RunUntilComplete(); |
| 9020 | EXPECT_THAT(delegate.request_status(), IsOk()); |
| 9021 | |
| 9022 | // There are DCHECKs in URLRequestHttpJob that would fail if |
| 9023 | // not_sent_cookies and not_stored_cookies we not cleared properly. |
| 9024 | |
| 9025 | // Make sure it was blocked twice. |
| 9026 | EXPECT_EQ(2, filtering_network_delegate.blocked_set_cookie_count()); |
| 9027 | |
| 9028 | // The number of cookies blocked from the most recent round trip. |
| 9029 | ASSERT_EQ(1u, request->not_stored_cookies().size()); |
| 9030 | EXPECT_EQ("server-redirect", |
| 9031 | request->not_stored_cookies().front().cookie->Name()); |
| 9032 | EXPECT_EQ("other", request->not_stored_cookies().front().cookie->Value()); |
| 9033 | } |
| 9034 | |
| 9035 | // Check not_sent_cookies on first round trip. |
| 9036 | { |
| 9037 | FilteringTestLayeredNetworkDelegate filtering_network_delegate( |
| 9038 | std::make_unique<TestNetworkDelegate>()); |
| 9039 | filtering_network_delegate.set_block_get_cookies(); |
| 9040 | TestURLRequestContext context(true); |
| 9041 | context.set_network_delegate(&filtering_network_delegate); |
| 9042 | std::unique_ptr<CookieMonster> cm = |
| 9043 | std::make_unique<CookieMonster>(nullptr, nullptr); |
| 9044 | cm->SetCookieWithOptionsAsync(original_url, "another_cookie=true", |
| 9045 | CookieOptions(), |
| 9046 | CookieStore::SetCookiesCallback()); |
| 9047 | context.set_cookie_store(cm.get()); |
| 9048 | context.Init(); |
| 9049 | |
| 9050 | TestDelegate delegate; |
| 9051 | std::unique_ptr<URLRequest> request( |
| 9052 | context.CreateRequest(original_url_wo_cookie, DEFAULT_PRIORITY, |
| 9053 | &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9054 | |
Lily Chen | 3ac7cf4 | 2019-05-30 23:29:29 | [diff] [blame] | 9055 | request->set_site_for_cookies(original_url_wo_cookie); |
Aaron Tagliaboschi | fd4f3f01 | 2019-05-30 23:05:54 | [diff] [blame] | 9056 | request->Start(); |
| 9057 | |
| 9058 | delegate.RunUntilRedirect(); |
| 9059 | |
| 9060 | ASSERT_EQ(1u, request->not_sent_cookies().size()); |
| 9061 | EXPECT_EQ("another_cookie", |
| 9062 | request->not_sent_cookies().front().cookie.Name()); |
| 9063 | |
| 9064 | // Check not_sent_cookies on second round trip |
| 9065 | request->set_not_sent_cookies({}); |
| 9066 | cm->DeleteAllAsync(CookieStore::DeleteCallback()); |
| 9067 | cm->SetCookieWithOptionsAsync(original_url_wo_cookie, |
| 9068 | "one_more_cookie=true", CookieOptions(), |
| 9069 | CookieStore::SetCookiesCallback()); |
| 9070 | |
| 9071 | request->FollowDeferredRedirect(base::nullopt, base::nullopt); |
| 9072 | delegate.RunUntilComplete(); |
| 9073 | EXPECT_THAT(delegate.request_status(), IsOk()); |
| 9074 | |
| 9075 | // There are DCHECKs in URLRequestHttpJob that would fail if |
| 9076 | // not_sent_cookies and not_stored_cookies we not cleared properly. |
| 9077 | |
| 9078 | EXPECT_EQ(2, filtering_network_delegate.blocked_get_cookie_count()); |
| 9079 | |
| 9080 | // The number of cookies blocked from the most recent round trip. |
| 9081 | ASSERT_EQ(1u, request->not_sent_cookies().size()); |
| 9082 | EXPECT_EQ("one_more_cookie", |
| 9083 | request->not_sent_cookies().front().cookie.Name()); |
| 9084 | EXPECT_EQ("true", request->not_sent_cookies().front().cookie.Value()); |
| 9085 | } |
| 9086 | } |
| 9087 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9088 | TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9089 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9090 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9091 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9092 | GURL first_party_url("http://example.com"); |
| 9093 | |
| 9094 | TestDelegate d; |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 9095 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 9096 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9097 | r->set_site_for_cookies(first_party_url); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9098 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 9099 | r->Start(); |
| 9100 | d.RunUntilComplete(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9101 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 9102 | EXPECT_EQ(2U, r->url_chain().size()); |
| 9103 | EXPECT_EQ(OK, d.request_status()); |
| 9104 | EXPECT_EQ(first_party_url, r->site_for_cookies()); |
| 9105 | } |
| 9106 | |
| 9107 | TEST_F(URLRequestTestHTTP, RedirectPreserveTopFrameOrigin) { |
| 9108 | ASSERT_TRUE(http_test_server()->Start()); |
| 9109 | |
| 9110 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 9111 | url::Origin top_frame_origin = |
| 9112 | url::Origin::Create(GURL("http://example.com")); |
| 9113 | TestDelegate d; |
| 9114 | |
| 9115 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 9116 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9117 | r->set_top_frame_origin(top_frame_origin); |
| 9118 | |
| 9119 | r->Start(); |
| 9120 | d.RunUntilComplete(); |
| 9121 | |
| 9122 | EXPECT_EQ(2U, r->url_chain().size()); |
| 9123 | EXPECT_EQ(OK, d.request_status()); |
| 9124 | EXPECT_EQ(top_frame_origin, *r->top_frame_origin()); |
| 9125 | } |
| 9126 | |
| 9127 | TEST_F(URLRequestTestHTTP, RedirectPreserveUnsetTopFrameOrigin) { |
| 9128 | ASSERT_TRUE(http_test_server()->Start()); |
| 9129 | |
| 9130 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 9131 | TestDelegate d; |
| 9132 | |
| 9133 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 9134 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9135 | // Since we're not setting a top frame origin, we wouldn't expect one after |
| 9136 | // the redirect. |
| 9137 | r->Start(); |
| 9138 | d.RunUntilComplete(); |
| 9139 | |
| 9140 | EXPECT_EQ(2U, r->url_chain().size()); |
| 9141 | EXPECT_EQ(OK, d.request_status()); |
| 9142 | EXPECT_FALSE(r->top_frame_origin()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9143 | } |
| 9144 | |
Josh Karlin | f3caee164 | 2018-12-10 22:54:27 | [diff] [blame] | 9145 | TEST_F(URLRequestTestHTTP, RedirectPreserveTopFrameURL) { |
| 9146 | ASSERT_TRUE(http_test_server()->Start()); |
| 9147 | |
| 9148 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 9149 | url::Origin top_frame_origin = |
| 9150 | url::Origin::Create(GURL("http://example.com")); |
| 9151 | TestDelegate d; |
| 9152 | { |
| 9153 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 9154 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9155 | r->set_top_frame_origin(top_frame_origin); |
| 9156 | |
| 9157 | r->Start(); |
| 9158 | d.RunUntilComplete(); |
| 9159 | |
| 9160 | EXPECT_EQ(2U, r->url_chain().size()); |
| 9161 | EXPECT_EQ(OK, d.request_status()); |
| 9162 | EXPECT_EQ(top_frame_origin, *r->top_frame_origin()); |
| 9163 | } |
| 9164 | } |
| 9165 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9166 | TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9167 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9168 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9169 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9170 | GURL original_first_party_url("http://example.com"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9171 | GURL expected_first_party_url(http_test_server()->GetURL("/echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9172 | |
| 9173 | TestDelegate d; |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 9174 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9175 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9176 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 9177 | r->set_site_for_cookies(original_first_party_url); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9178 | r->set_first_party_url_policy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9179 | URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 9180 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9181 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9182 | d.RunUntilComplete(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9183 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9184 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9185 | EXPECT_EQ(OK, d.request_status()); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 9186 | EXPECT_EQ(expected_first_party_url, r->site_for_cookies()); |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 9187 | } |
| 9188 | |
| 9189 | TEST_F(URLRequestTestHTTP, RedirectUpdateTopFrameOrigin) { |
| 9190 | ASSERT_TRUE(http_test_server()->Start()); |
| 9191 | |
| 9192 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 9193 | url::Origin original_top_frame_origin = |
| 9194 | url::Origin::Create(GURL("http://example.com")); |
| 9195 | url::Origin expected_top_frame_origin = |
| 9196 | url::Origin::Create(GURL(http_test_server()->GetURL("/echo"))); |
| 9197 | |
| 9198 | TestDelegate d; |
| 9199 | |
| 9200 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 9201 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9202 | r->set_top_frame_origin(original_top_frame_origin); |
| 9203 | r->set_first_party_url_policy(URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 9204 | |
| 9205 | r->Start(); |
| 9206 | d.RunUntilComplete(); |
| 9207 | |
| 9208 | EXPECT_EQ(2U, r->url_chain().size()); |
| 9209 | EXPECT_EQ(OK, d.request_status()); |
| 9210 | EXPECT_EQ(expected_top_frame_origin, *r->top_frame_origin()); |
| 9211 | } |
| 9212 | |
| 9213 | TEST_F(URLRequestTestHTTP, RedirectIgnoreUnsetTopFrameOrigin) { |
| 9214 | ASSERT_TRUE(http_test_server()->Start()); |
| 9215 | |
| 9216 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 9217 | TestDelegate d; |
| 9218 | |
| 9219 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 9220 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9221 | // Since we're not setting a top frame origin, we wouldn't expect one after |
| 9222 | // the redirect. |
| 9223 | r->set_first_party_url_policy(URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 9224 | |
| 9225 | r->Start(); |
| 9226 | d.RunUntilComplete(); |
| 9227 | |
| 9228 | EXPECT_EQ(2U, r->url_chain().size()); |
| 9229 | EXPECT_EQ(OK, d.request_status()); |
| 9230 | EXPECT_FALSE(r->top_frame_origin()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 9231 | } |
| 9232 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9233 | TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9234 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9235 | |
| 9236 | const char kData[] = "hello world"; |
| 9237 | |
| 9238 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9239 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9240 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9241 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9242 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 9243 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9244 | HttpRequestHeaders headers; |
| 9245 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 9246 | base::NumberToString(base::size(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9247 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9248 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9249 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9250 | req.get(), &default_network_delegate_, |
| 9251 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9252 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 9253 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9254 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9255 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9256 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9257 | EXPECT_EQ("GET", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9258 | } |
| 9259 | |
| 9260 | TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9261 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9262 | |
| 9263 | const char kData[] = "hello world"; |
| 9264 | |
| 9265 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9266 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9267 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9268 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9269 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 9270 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9271 | HttpRequestHeaders headers; |
| 9272 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 9273 | base::NumberToString(base::size(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9274 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9275 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9276 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9277 | req.get(), &default_network_delegate_, |
| 9278 | http_test_server()->GetURL("/echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 9279 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9280 | "Very Good Reason")); |
| 9281 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9282 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9283 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9284 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9285 | EXPECT_EQ("POST", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9286 | EXPECT_EQ(kData, d.data_received()); |
| 9287 | } |
| 9288 | |
| 9289 | // Check that default A-L header is sent. |
| 9290 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9291 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9292 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 9293 | StaticHttpUserAgentSettings settings("en", std::string()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9294 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9295 | TestURLRequestContext context(true); |
| 9296 | context.set_network_delegate(&network_delegate); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9297 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9298 | context.Init(); |
| 9299 | |
| 9300 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9301 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9302 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9303 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9304 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9305 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9306 | EXPECT_EQ("en", d.data_received()); |
| 9307 | } |
| 9308 | |
| 9309 | // Check that an empty A-L header is not sent. http://crbug.com/77365. |
| 9310 | TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9311 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9312 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 9313 | std::string empty_string; // Avoid most vexing parse on line below. |
| 9314 | StaticHttpUserAgentSettings settings(empty_string, empty_string); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9315 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9316 | TestURLRequestContext context(true); |
| 9317 | context.set_network_delegate(&network_delegate); |
| 9318 | context.Init(); |
| 9319 | // We override the language after initialization because empty entries |
| 9320 | // get overridden by Init(). |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9321 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9322 | |
| 9323 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9324 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9325 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9326 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9327 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9328 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9329 | EXPECT_EQ("None", d.data_received()); |
| 9330 | } |
| 9331 | |
| 9332 | // Check that if request overrides the A-L header, the default is not appended. |
| 9333 | // See http://crbug.com/20894 |
| 9334 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9335 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9336 | |
| 9337 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9338 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9339 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9340 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9341 | HttpRequestHeaders headers; |
| 9342 | headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9343 | req->SetExtraRequestHeaders(headers); |
| 9344 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9345 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9346 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 9347 | } |
| 9348 | |
| 9349 | // Check that default A-E header is sent. |
| 9350 | TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9351 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9352 | |
| 9353 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9354 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9355 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9356 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9357 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9358 | req->SetExtraRequestHeaders(headers); |
| 9359 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9360 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9361 | EXPECT_TRUE(ContainsString(d.data_received(), "gzip")); |
| 9362 | } |
| 9363 | |
| 9364 | // Check that if request overrides the A-E header, the default is not appended. |
| 9365 | // See http://crbug.com/47381 |
| 9366 | TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9367 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9368 | |
| 9369 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9370 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9371 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9372 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9373 | HttpRequestHeaders headers; |
| 9374 | headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9375 | req->SetExtraRequestHeaders(headers); |
| 9376 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9377 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9378 | EXPECT_FALSE(ContainsString(d.data_received(), "gzip")); |
| 9379 | EXPECT_TRUE(ContainsString(d.data_received(), "identity")); |
| 9380 | } |
| 9381 | |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 9382 | // Check that setting the A-C header sends the proper header. |
| 9383 | TEST_F(URLRequestTestHTTP, SetAcceptCharset) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9384 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9385 | |
| 9386 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9387 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9388 | http_test_server()->GetURL("/echoheader?Accept-Charset"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9389 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9390 | HttpRequestHeaders headers; |
| 9391 | headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9392 | req->SetExtraRequestHeaders(headers); |
| 9393 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9394 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9395 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 9396 | } |
| 9397 | |
| 9398 | // Check that default User-Agent header is sent. |
| 9399 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9400 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9401 | |
| 9402 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9403 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9404 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9405 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9406 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9407 | d.RunUntilComplete(); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9408 | EXPECT_EQ(default_context().http_user_agent_settings()->GetUserAgent(), |
[email protected] | 051a4b3 | 2014-01-09 04:02:37 | [diff] [blame] | 9409 | d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9410 | } |
| 9411 | |
| 9412 | // Check that if request overrides the User-Agent header, |
| 9413 | // the default is not appended. |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 9414 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 9415 | #if defined(OS_IOS) |
| 9416 | #define MAYBE_OverrideUserAgent FLAKY_OverrideUserAgent |
| 9417 | #else |
| 9418 | #define MAYBE_OverrideUserAgent OverrideUserAgent |
| 9419 | #endif |
| 9420 | TEST_F(URLRequestTestHTTP, MAYBE_OverrideUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9421 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9422 | |
| 9423 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9424 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9425 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9426 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9427 | HttpRequestHeaders headers; |
| 9428 | headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9429 | req->SetExtraRequestHeaders(headers); |
| 9430 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9431 | d.RunUntilComplete(); |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 9432 | EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9433 | } |
| 9434 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9435 | // Check that a NULL HttpUserAgentSettings causes the corresponding empty |
| 9436 | // User-Agent header to be sent but does not send the Accept-Language and |
| 9437 | // Accept-Charset headers. |
| 9438 | TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9439 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9440 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9441 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9442 | TestURLRequestContext context(true); |
| 9443 | context.set_network_delegate(&network_delegate); |
| 9444 | context.Init(); |
| 9445 | // We override the HttpUserAgentSettings after initialization because empty |
| 9446 | // entries get overridden by Init(). |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 9447 | context.set_http_user_agent_settings(nullptr); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9448 | |
| 9449 | struct { |
| 9450 | const char* request; |
| 9451 | const char* expected_response; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9452 | } tests[] = {{"/echoheader?Accept-Language", "None"}, |
| 9453 | {"/echoheader?Accept-Charset", "None"}, |
| 9454 | {"/echoheader?User-Agent", ""}}; |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9455 | |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 9456 | for (size_t i = 0; i < base::size(tests); i++) { |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9457 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9458 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9459 | http_test_server()->GetURL(tests[i].request), DEFAULT_PRIORITY, &d, |
| 9460 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9461 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9462 | d.RunUntilComplete(); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9463 | EXPECT_EQ(tests[i].expected_response, d.data_received()) |
| 9464 | << " Request = \"" << tests[i].request << "\""; |
| 9465 | } |
| 9466 | } |
| 9467 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9468 | // Make sure that URLRequest passes on its priority updates to |
| 9469 | // newly-created jobs after the first one. |
| 9470 | TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9471 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9472 | |
| 9473 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9474 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9475 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9476 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9477 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9478 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9479 | std::unique_ptr<URLRequestRedirectJob> redirect_job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9480 | req.get(), &default_network_delegate_, |
| 9481 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9482 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 9483 | AddTestInterceptor()->set_main_intercept_job(std::move(redirect_job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9484 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9485 | req->SetPriority(LOW); |
| 9486 | req->Start(); |
| 9487 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9488 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9489 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9490 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9491 | req.get(), &default_network_delegate_, &job_priority)); |
| 9492 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9493 | |
| 9494 | // Should trigger |job| to be started. |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9495 | d.RunUntilComplete(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9496 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9497 | } |
| 9498 | |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9499 | // Check that creating a network request while entering/exiting suspend mode |
| 9500 | // fails as it should. This is the only case where an HttpTransactionFactory |
| 9501 | // does not return an HttpTransaction. |
| 9502 | TEST_F(URLRequestTestHTTP, NetworkSuspendTest) { |
| 9503 | // Create a new HttpNetworkLayer that thinks it's suspended. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9504 | std::unique_ptr<HttpNetworkLayer> network_layer(new HttpNetworkLayer( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9505 | default_context().http_transaction_factory()->GetSession())); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9506 | network_layer->OnSuspend(); |
| 9507 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 9508 | HttpCache http_cache(std::move(network_layer), |
zhongyi | 3ceab9f | 2017-02-04 02:06:03 | [diff] [blame] | 9509 | HttpCache::DefaultBackend::InMemory(0), |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 9510 | false /* is_main_cache */); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9511 | |
| 9512 | TestURLRequestContext context(true); |
| 9513 | context.set_http_transaction_factory(&http_cache); |
| 9514 | context.Init(); |
| 9515 | |
| 9516 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9517 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9518 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 9519 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9520 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9521 | d.RunUntilComplete(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9522 | |
| 9523 | EXPECT_TRUE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9524 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, d.request_status()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9525 | } |
| 9526 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9527 | namespace { |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9528 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9529 | // HttpTransactionFactory that synchronously fails to create transactions. |
| 9530 | class FailingHttpTransactionFactory : public HttpTransactionFactory { |
| 9531 | public: |
| 9532 | explicit FailingHttpTransactionFactory(HttpNetworkSession* network_session) |
| 9533 | : network_session_(network_session) {} |
| 9534 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 9535 | ~FailingHttpTransactionFactory() override = default; |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9536 | |
| 9537 | // HttpTransactionFactory methods: |
| 9538 | int CreateTransaction(RequestPriority priority, |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9539 | std::unique_ptr<HttpTransaction>* trans) override { |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9540 | return ERR_FAILED; |
| 9541 | } |
| 9542 | |
| 9543 | HttpCache* GetCache() override { return nullptr; } |
| 9544 | |
| 9545 | HttpNetworkSession* GetSession() override { return network_session_; } |
| 9546 | |
| 9547 | private: |
| 9548 | HttpNetworkSession* network_session_; |
| 9549 | |
| 9550 | DISALLOW_COPY_AND_ASSIGN(FailingHttpTransactionFactory); |
| 9551 | }; |
| 9552 | |
| 9553 | } // namespace |
| 9554 | |
| 9555 | // Check that when a request that fails to create an HttpTransaction can be |
| 9556 | // cancelled while the failure notification is pending, and doesn't send two |
| 9557 | // failure notifications. |
| 9558 | // |
| 9559 | // This currently only happens when in suspend mode and there's no cache, but |
| 9560 | // just use a special HttpTransactionFactory, to avoid depending on those |
| 9561 | // behaviors. |
| 9562 | TEST_F(URLRequestTestHTTP, NetworkCancelAfterCreateTransactionFailsTest) { |
| 9563 | FailingHttpTransactionFactory http_transaction_factory( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9564 | default_context().http_transaction_factory()->GetSession()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9565 | TestURLRequestContext context(true); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9566 | context.set_http_transaction_factory(&http_transaction_factory); |
| 9567 | context.set_network_delegate(default_network_delegate()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9568 | context.Init(); |
| 9569 | |
| 9570 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9571 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9572 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 9573 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9574 | // Don't send cookies (Collecting cookies is asynchronous, and need request to |
| 9575 | // try to create an HttpNetworkTransaction synchronously on start). |
| 9576 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9577 | req->Start(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9578 | req->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9579 | d.RunUntilComplete(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9580 | // Run pending error task, if there is one. |
| 9581 | base::RunLoop().RunUntilIdle(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9582 | |
| 9583 | EXPECT_TRUE(d.request_failed()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9584 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9585 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9586 | |
| 9587 | // NetworkDelegate should see the cancellation, but not the error. |
| 9588 | EXPECT_EQ(1, default_network_delegate()->canceled_requests()); |
| 9589 | EXPECT_EQ(0, default_network_delegate()->error_count()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9590 | } |
| 9591 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9592 | TEST_F(URLRequestTestHTTP, NetworkAccessedSetOnNetworkRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9593 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9594 | |
| 9595 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9596 | GURL test_url(http_test_server()->GetURL("/")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9597 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9598 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9599 | |
| 9600 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9601 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9602 | |
| 9603 | EXPECT_TRUE(req->response_info().network_accessed); |
| 9604 | } |
| 9605 | |
| 9606 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnCachedResponse) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9607 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9608 | |
| 9609 | // Populate the cache. |
| 9610 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9611 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9612 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 9613 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9614 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9615 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9616 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9617 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9618 | EXPECT_TRUE(req->response_info().network_accessed); |
| 9619 | EXPECT_FALSE(req->response_info().was_cached); |
| 9620 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9621 | req = default_context().CreateRequest( |
| 9622 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 9623 | TRAFFIC_ANNOTATION_FOR_TESTS); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9624 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9625 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9626 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9627 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9628 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9629 | EXPECT_TRUE(req->response_info().was_cached); |
| 9630 | } |
| 9631 | |
| 9632 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnLoadOnlyFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9633 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9634 | |
| 9635 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9636 | GURL test_url(http_test_server()->GetURL("/")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9637 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9638 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jkarlin | a067deed | 2016-10-27 14:48:33 | [diff] [blame] | 9639 | req->SetLoadFlags(LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9640 | |
| 9641 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9642 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9643 | |
| 9644 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9645 | } |
| 9646 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 9647 | // Test that a single job with a THROTTLED priority completes |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 9648 | // correctly in the absence of contention. |
| 9649 | TEST_F(URLRequestTestHTTP, ThrottledPriority) { |
| 9650 | ASSERT_TRUE(http_test_server()->Start()); |
| 9651 | |
| 9652 | TestDelegate d; |
| 9653 | GURL test_url(http_test_server()->GetURL("/")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9654 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9655 | test_url, THROTTLED, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 9656 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9657 | d.RunUntilComplete(); |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 9658 | |
| 9659 | EXPECT_TRUE(req->status().is_success()); |
| 9660 | } |
| 9661 | |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9662 | TEST_F(URLRequestTestHTTP, RawBodyBytesNoContentEncoding) { |
| 9663 | ASSERT_TRUE(http_test_server()->Start()); |
| 9664 | |
| 9665 | TestDelegate d; |
| 9666 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9667 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 9668 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9669 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9670 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9671 | |
| 9672 | EXPECT_EQ(5, req->GetRawBodyBytes()); |
| 9673 | } |
| 9674 | |
| 9675 | TEST_F(URLRequestTestHTTP, RawBodyBytesGzipEncoding) { |
| 9676 | ASSERT_TRUE(http_test_server()->Start()); |
| 9677 | |
| 9678 | TestDelegate d; |
| 9679 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9680 | http_test_server()->GetURL("/gzip-encoded"), DEFAULT_PRIORITY, &d, |
| 9681 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9682 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9683 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9684 | |
| 9685 | EXPECT_EQ(30, req->GetRawBodyBytes()); |
| 9686 | } |
| 9687 | |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9688 | // Check that if NetworkDelegate::OnBeforeStartTransaction returns an error, |
| 9689 | // the delegate isn't called back synchronously. |
| 9690 | TEST_F(URLRequestTestHTTP, TesBeforeStartTransactionFails) { |
| 9691 | ASSERT_TRUE(http_test_server()->Start()); |
| 9692 | default_network_delegate_.set_before_start_transaction_fails(); |
| 9693 | |
| 9694 | TestDelegate d; |
| 9695 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9696 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 9697 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9698 | req->Start(); |
| 9699 | DCHECK(!d.response_completed()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9700 | d.RunUntilComplete(); |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9701 | DCHECK(d.response_completed()); |
| 9702 | EXPECT_EQ(ERR_FAILED, d.request_status()); |
| 9703 | } |
| 9704 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9705 | class URLRequestInterceptorTestHTTP : public URLRequestTestHTTP { |
| 9706 | public: |
| 9707 | // TODO(bengr): Merge this with the URLRequestInterceptorHTTPTest fixture, |
| 9708 | // ideally remove the dependency on URLRequestTestJob, and maybe move these |
| 9709 | // tests into the factory tests. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 9710 | URLRequestInterceptorTestHTTP() |
| 9711 | : URLRequestTestHTTP(), interceptor_(nullptr) {} |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9712 | |
| 9713 | void SetUpFactory() override { |
| 9714 | interceptor_ = new MockURLRequestInterceptor(); |
| 9715 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9716 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9717 | } |
| 9718 | |
| 9719 | MockURLRequestInterceptor* interceptor() const { |
| 9720 | return interceptor_; |
| 9721 | } |
| 9722 | |
| 9723 | private: |
| 9724 | MockURLRequestInterceptor* interceptor_; |
| 9725 | }; |
| 9726 | |
| 9727 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9728 | NetworkDelegateNotificationOnRedirectIntercept) { |
| 9729 | interceptor()->set_intercept_redirect(true); |
| 9730 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 9731 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 9732 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9733 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9734 | |
| 9735 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9736 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9737 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 9738 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9739 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9740 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9741 | |
| 9742 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 9743 | // Check we got one good response |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9744 | int status = d.request_status(); |
| 9745 | EXPECT_EQ(OK, status); |
| 9746 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9747 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9748 | |
| 9749 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 9750 | EXPECT_EQ(1, d.response_started_count()); |
| 9751 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9752 | |
| 9753 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9754 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9755 | EXPECT_EQ(1, default_network_delegate()->headers_received_count()); |
| 9756 | } |
| 9757 | |
| 9758 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9759 | NetworkDelegateNotificationOnErrorIntercept) { |
| 9760 | // Intercept that error and respond with an OK response. |
| 9761 | interceptor()->set_intercept_final_response(true); |
| 9762 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 9763 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 9764 | default_network_delegate()->set_can_be_intercepted_on_error(true); |
| 9765 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9766 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9767 | |
| 9768 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9769 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9770 | http_test_server()->GetURL("/two-content-lengths.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9771 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9772 | req->set_method("GET"); |
| 9773 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9774 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9775 | |
| 9776 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 9777 | |
| 9778 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9779 | int status = d.request_status(); |
| 9780 | EXPECT_EQ(OK, status); |
| 9781 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9782 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9783 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 9784 | EXPECT_EQ(1, d.response_started_count()); |
| 9785 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9786 | |
| 9787 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9788 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9789 | EXPECT_EQ(0, default_network_delegate()->headers_received_count()); |
| 9790 | } |
| 9791 | |
| 9792 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9793 | NetworkDelegateNotificationOnResponseIntercept) { |
| 9794 | // Intercept that error and respond with an OK response. |
| 9795 | interceptor()->set_intercept_final_response(true); |
| 9796 | |
| 9797 | // Intercept with a real URLRequestHttpJob. |
| 9798 | interceptor()->set_use_url_request_http_job(true); |
| 9799 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9800 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9801 | |
| 9802 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9803 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9804 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 9805 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9806 | req->set_method("GET"); |
| 9807 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9808 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9809 | |
| 9810 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 9811 | |
| 9812 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9813 | int status = d.request_status(); |
| 9814 | EXPECT_EQ(OK, status); |
| 9815 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9816 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9817 | EXPECT_EQ("hello", d.data_received()); |
| 9818 | EXPECT_EQ(1, d.response_started_count()); |
| 9819 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9820 | |
| 9821 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9822 | EXPECT_EQ(2, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9823 | EXPECT_EQ(2, default_network_delegate()->headers_received_count()); |
| 9824 | } |
| 9825 | |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9826 | class URLRequestTestReferrerPolicy : public URLRequestTest { |
| 9827 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 9828 | URLRequestTestReferrerPolicy() = default; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9829 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9830 | void InstantiateSameOriginServers(net::EmbeddedTestServer::Type type) { |
| 9831 | origin_server_.reset(new EmbeddedTestServer(type)); |
| 9832 | if (type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9833 | origin_server_->AddDefaultHandlers( |
| 9834 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9835 | } else { |
| 9836 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9837 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9838 | ASSERT_TRUE(origin_server_->Start()); |
| 9839 | } |
| 9840 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9841 | void InstantiateCrossOriginServers(net::EmbeddedTestServer::Type origin_type, |
| 9842 | net::EmbeddedTestServer::Type dest_type) { |
| 9843 | origin_server_.reset(new EmbeddedTestServer(origin_type)); |
| 9844 | if (origin_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9845 | origin_server_->AddDefaultHandlers( |
| 9846 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9847 | } else { |
| 9848 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9849 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9850 | ASSERT_TRUE(origin_server_->Start()); |
| 9851 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9852 | destination_server_.reset(new EmbeddedTestServer(dest_type)); |
| 9853 | if (dest_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9854 | destination_server_->AddDefaultHandlers( |
| 9855 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9856 | } else { |
| 9857 | destination_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9858 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9859 | ASSERT_TRUE(destination_server_->Start()); |
| 9860 | } |
| 9861 | |
| 9862 | void VerifyReferrerAfterRedirect(URLRequest::ReferrerPolicy policy, |
| 9863 | const GURL& referrer, |
| 9864 | const GURL& expected) { |
| 9865 | // Create and execute the request: we'll only have a |destination_server_| |
| 9866 | // if the origins are meant to be distinct. Otherwise, we'll use the |
| 9867 | // |origin_server_| for both endpoints. |
| 9868 | GURL destination_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9869 | destination_server_ ? destination_server_->GetURL("/echoheader?Referer") |
| 9870 | : origin_server_->GetURL("/echoheader?Referer"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9871 | GURL origin_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9872 | origin_server_->GetURL("/server-redirect?" + destination_url.spec()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9873 | |
| 9874 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 9875 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9876 | origin_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9877 | req->set_referrer_policy(policy); |
| 9878 | req->SetReferrer(referrer.spec()); |
| 9879 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9880 | d.RunUntilComplete(); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9881 | |
| 9882 | EXPECT_EQ(1, d.response_started_count()); |
| 9883 | EXPECT_EQ(1, d.received_redirect_count()); |
| 9884 | EXPECT_EQ(destination_url, req->url()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9885 | EXPECT_EQ(OK, d.request_status()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9886 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9887 | |
| 9888 | EXPECT_EQ(expected.spec(), req->referrer()); |
| 9889 | if (expected.is_empty()) |
| 9890 | EXPECT_EQ("None", d.data_received()); |
| 9891 | else |
| 9892 | EXPECT_EQ(expected.spec(), d.data_received()); |
| 9893 | } |
| 9894 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9895 | EmbeddedTestServer* origin_server() const { return origin_server_.get(); } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9896 | |
| 9897 | private: |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9898 | std::unique_ptr<EmbeddedTestServer> origin_server_; |
| 9899 | std::unique_ptr<EmbeddedTestServer> destination_server_; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9900 | }; |
| 9901 | |
| 9902 | TEST_F(URLRequestTestReferrerPolicy, HTTPToSameOriginHTTP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9903 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTP); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9904 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9905 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9906 | VerifyReferrerAfterRedirect( |
| 9907 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9908 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9909 | |
| 9910 | VerifyReferrerAfterRedirect( |
| 9911 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9912 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9913 | |
| 9914 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9915 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9916 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9917 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9918 | referrer); |
| 9919 | |
| 9920 | // The original referrer set on the request is expected to obey the referrer |
| 9921 | // policy and already be stripped to the origin; thus this test case just |
| 9922 | // checks that this policy doesn't cause the referrer to change when following |
| 9923 | // a redirect. |
| 9924 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9925 | referrer.GetOrigin()); |
| 9926 | |
| 9927 | VerifyReferrerAfterRedirect( |
| 9928 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9929 | referrer); |
| 9930 | |
| 9931 | // The original referrer set on the request is expected to obey the referrer |
| 9932 | // policy and already be stripped to the origin; thus this test case just |
| 9933 | // checks that this policy doesn't cause the referrer to change when following |
| 9934 | // a redirect. |
| 9935 | VerifyReferrerAfterRedirect( |
| 9936 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9937 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9938 | |
| 9939 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9940 | } |
| 9941 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9942 | TEST_F(URLRequestTestReferrerPolicy, HTTPToCrossOriginHTTP) { |
| 9943 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 9944 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9945 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9946 | |
| 9947 | VerifyReferrerAfterRedirect( |
| 9948 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9949 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9950 | |
| 9951 | VerifyReferrerAfterRedirect( |
| 9952 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9953 | referrer, referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9954 | |
| 9955 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9956 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9957 | referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9958 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9959 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9960 | referrer); |
| 9961 | |
| 9962 | // The original referrer set on the request is expected to obey the referrer |
| 9963 | // policy and already be stripped to the origin; thus this test case just |
| 9964 | // checks that this policy doesn't cause the referrer to change when following |
| 9965 | // a redirect. |
| 9966 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9967 | referrer.GetOrigin()); |
| 9968 | |
| 9969 | VerifyReferrerAfterRedirect( |
| 9970 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9971 | |
| 9972 | // The original referrer set on the request is expected to obey the referrer |
| 9973 | // policy and already be stripped to the origin; thus this test case just |
| 9974 | // checks that this policy doesn't cause the referrer to change when following |
| 9975 | // a redirect. |
| 9976 | VerifyReferrerAfterRedirect( |
| 9977 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9978 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9979 | |
| 9980 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9981 | } |
| 9982 | |
| 9983 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToSameOriginHTTPS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9984 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9985 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9986 | |
| 9987 | VerifyReferrerAfterRedirect( |
| 9988 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9989 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9990 | |
| 9991 | VerifyReferrerAfterRedirect( |
| 9992 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9993 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9994 | |
| 9995 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9996 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9997 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9998 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9999 | referrer); |
| 10000 | |
| 10001 | // The original referrer set on the request is expected to obey the referrer |
| 10002 | // policy and already be stripped to the origin; thus this test case just |
| 10003 | // checks that this policy doesn't cause the referrer to change when following |
| 10004 | // a redirect. |
| 10005 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 10006 | referrer.GetOrigin()); |
| 10007 | |
| 10008 | VerifyReferrerAfterRedirect( |
| 10009 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 10010 | referrer); |
| 10011 | |
| 10012 | // The original referrer set on the request is expected to obey the referrer |
| 10013 | // policy and already be stripped to the origin; thus this test case just |
| 10014 | // checks that this policy doesn't cause the referrer to change when following |
| 10015 | // a redirect. |
| 10016 | VerifyReferrerAfterRedirect( |
| 10017 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 10018 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 10019 | |
| 10020 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10021 | } |
| 10022 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10023 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToCrossOriginHTTPS) { |
| 10024 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 10025 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10026 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10027 | |
| 10028 | VerifyReferrerAfterRedirect( |
| 10029 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10030 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10031 | |
| 10032 | VerifyReferrerAfterRedirect( |
| 10033 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10034 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10035 | |
| 10036 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10037 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10038 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10039 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10040 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 10041 | referrer); |
| 10042 | |
| 10043 | // The original referrer set on the request is expected to obey the referrer |
| 10044 | // policy and already be stripped to the origin; thus this test case just |
| 10045 | // checks that this policy doesn't cause the referrer to change when following |
| 10046 | // a redirect. |
| 10047 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 10048 | referrer.GetOrigin()); |
| 10049 | |
| 10050 | VerifyReferrerAfterRedirect( |
| 10051 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 10052 | |
| 10053 | // The original referrer set on the request is expected to obey the referrer |
| 10054 | // policy and already be stripped to the origin; thus this test case just |
| 10055 | // checks that this policy doesn't cause the referrer to change when following |
| 10056 | // a redirect. |
| 10057 | VerifyReferrerAfterRedirect( |
| 10058 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 10059 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 10060 | |
| 10061 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10062 | } |
| 10063 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10064 | TEST_F(URLRequestTestReferrerPolicy, HTTPToHTTPS) { |
| 10065 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 10066 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10067 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10068 | |
| 10069 | VerifyReferrerAfterRedirect( |
| 10070 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10071 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10072 | |
| 10073 | VerifyReferrerAfterRedirect( |
| 10074 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10075 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10076 | |
| 10077 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10078 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10079 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10080 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10081 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 10082 | referrer); |
| 10083 | |
| 10084 | // The original referrer set on the request is expected to obey the referrer |
| 10085 | // policy and already be stripped to the origin; thus this test case just |
| 10086 | // checks that this policy doesn't cause the referrer to change when following |
| 10087 | // a redirect. |
| 10088 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 10089 | referrer.GetOrigin()); |
| 10090 | |
| 10091 | VerifyReferrerAfterRedirect( |
| 10092 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 10093 | |
| 10094 | // The original referrer set on the request is expected to obey the referrer |
| 10095 | // policy and already be stripped to the origin; thus this test case just |
| 10096 | // checks that this policy doesn't cause the referrer to change when following |
| 10097 | // a redirect. |
| 10098 | VerifyReferrerAfterRedirect( |
| 10099 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 10100 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 10101 | |
| 10102 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10103 | } |
| 10104 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10105 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToHTTP) { |
| 10106 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 10107 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10108 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10109 | |
| 10110 | VerifyReferrerAfterRedirect( |
| 10111 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10112 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10113 | |
| 10114 | VerifyReferrerAfterRedirect( |
| 10115 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10116 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10117 | |
| 10118 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10119 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10120 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10121 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 10122 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 10123 | referrer); |
| 10124 | |
| 10125 | // The original referrer set on the request is expected to obey the referrer |
| 10126 | // policy and already be stripped to the origin; thus this test case just |
| 10127 | // checks that this policy doesn't cause the referrer to change when following |
| 10128 | // a redirect. |
| 10129 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 10130 | referrer.GetOrigin()); |
| 10131 | |
| 10132 | VerifyReferrerAfterRedirect( |
| 10133 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 10134 | |
| 10135 | // The original referrer set on the request is expected to obey the referrer |
| 10136 | // policy and already be stripped to the origin, though it should be |
| 10137 | // subsequently cleared during the downgrading redirect. |
| 10138 | VerifyReferrerAfterRedirect( |
| 10139 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 10140 | referrer.GetOrigin(), GURL()); |
| 10141 | |
| 10142 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 10143 | } |
| 10144 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 10145 | class HTTPSRequestTest : public TestWithScopedTaskEnvironment { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 10146 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 10147 | HTTPSRequestTest() : default_context_(true) { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10148 | default_context_.set_network_delegate(&default_network_delegate_); |
| 10149 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 10150 | } |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10151 | ~HTTPSRequestTest() override { |
| 10152 | SetTransportSecurityStateSourceForTesting(nullptr); |
| 10153 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 10154 | |
| 10155 | protected: |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 10156 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10157 | TestURLRequestContext default_context_; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10158 | }; |
| 10159 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 10160 | TEST_F(HTTPSRequestTest, HTTPSGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10161 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10162 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10163 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 10164 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10165 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10166 | TestDelegate d; |
| 10167 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10168 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10169 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10170 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10171 | r->Start(); |
| 10172 | EXPECT_TRUE(r->is_pending()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10173 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10174 | d.RunUntilComplete(); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10175 | |
| 10176 | EXPECT_EQ(1, d.response_started_count()); |
| 10177 | EXPECT_FALSE(d.received_data_before_response()); |
| 10178 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10179 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 10180 | EXPECT_EQ(test_server.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 10181 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 10182 | EXPECT_EQ(test_server.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 10183 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10184 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 10185 | } |
| 10186 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 10187 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10188 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10189 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 10190 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10191 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 10192 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10193 | |
| 10194 | bool err_allowed = true; |
| 10195 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 10196 | TestDelegate d; |
| 10197 | { |
| 10198 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10199 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10200 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10201 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10202 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10203 | r->Start(); |
| 10204 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10205 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10206 | d.RunUntilComplete(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10207 | |
| 10208 | EXPECT_EQ(1, d.response_started_count()); |
| 10209 | EXPECT_FALSE(d.received_data_before_response()); |
| 10210 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 10211 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10212 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10213 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 10214 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10215 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 10216 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10217 | } |
| 10218 | } |
| 10219 | } |
| 10220 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 10221 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10222 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10223 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED); |
| 10224 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10225 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 10226 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10227 | |
| 10228 | // Iterate from false to true, just so that we do the opposite of the |
| 10229 | // previous test in order to increase test coverage. |
| 10230 | bool err_allowed = false; |
| 10231 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 10232 | TestDelegate d; |
| 10233 | { |
| 10234 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10235 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10236 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10237 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10238 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10239 | r->Start(); |
| 10240 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10241 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10242 | d.RunUntilComplete(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10243 | |
| 10244 | EXPECT_EQ(1, d.response_started_count()); |
| 10245 | EXPECT_FALSE(d.received_data_before_response()); |
| 10246 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 10247 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10248 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10249 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 10250 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10251 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 10252 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10253 | } |
| 10254 | } |
| 10255 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 10256 | |
Emily Stark | 79fba584 | 2019-04-25 04:59:36 | [diff] [blame] | 10257 | // A TestDelegate used to test that an appropriate net error code is provided |
| 10258 | // when an SSL certificate error occurs. |
| 10259 | class SSLNetErrorTestDelegate : public TestDelegate { |
| 10260 | public: |
| 10261 | void OnSSLCertificateError(URLRequest* request, |
| 10262 | int net_error, |
| 10263 | const SSLInfo& ssl_info, |
| 10264 | bool fatal) override { |
| 10265 | net_error_ = net_error; |
| 10266 | on_ssl_certificate_error_called_ = true; |
| 10267 | TestDelegate::OnSSLCertificateError(request, net_error, ssl_info, fatal); |
| 10268 | } |
| 10269 | |
| 10270 | bool on_ssl_certificate_error_called() { |
| 10271 | return on_ssl_certificate_error_called_; |
| 10272 | } |
| 10273 | |
| 10274 | int net_error() { return net_error_; } |
| 10275 | |
| 10276 | private: |
| 10277 | bool on_ssl_certificate_error_called_ = false; |
| 10278 | int net_error_ = net::OK; |
| 10279 | }; |
| 10280 | |
| 10281 | // Tests that the URLRequest::Delegate receives an appropriate net error code |
| 10282 | // when an SSL certificate error occurs. |
| 10283 | TEST_F(HTTPSRequestTest, SSLNetErrorReportedToDelegate) { |
| 10284 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10285 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED); |
| 10286 | test_server.AddDefaultHandlers( |
| 10287 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10288 | ASSERT_TRUE(test_server.Start()); |
| 10289 | |
| 10290 | SSLNetErrorTestDelegate d; |
| 10291 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10292 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10293 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10294 | r->Start(); |
| 10295 | EXPECT_TRUE(r->is_pending()); |
| 10296 | d.RunUntilComplete(); |
| 10297 | |
| 10298 | EXPECT_TRUE(d.on_ssl_certificate_error_called()); |
| 10299 | EXPECT_EQ(net::ERR_CERT_DATE_INVALID, d.net_error()); |
| 10300 | } |
| 10301 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10302 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 10303 | // security state. (see http://crbug.com/550977). |
| 10304 | #if !defined(OS_IOS) |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10305 | // This tests that a load of a domain with preloaded HSTS and HPKP with a |
| 10306 | // certificate error sets the |certificate_errors_are_fatal| flag correctly. |
| 10307 | // This flag will cause the interstitial to be fatal. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10308 | TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10309 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 10310 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10311 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10312 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 10313 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10314 | ASSERT_TRUE(test_server.Start()); |
| 10315 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10316 | // We require that the URL be hsts-hpkp-preloaded.test. This is a test domain |
| 10317 | // that has a preloaded HSTS+HPKP entry in the TransportSecurityState. This |
| 10318 | // means that we have to use a MockHostResolver in order to direct |
| 10319 | // hsts-hpkp-preloaded.test to the testserver. By default, MockHostResolver |
| 10320 | // maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10321 | |
| 10322 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 10323 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10324 | TestURLRequestContext context(true); |
| 10325 | context.set_network_delegate(&network_delegate); |
| 10326 | context.set_host_resolver(&host_resolver); |
| 10327 | TransportSecurityState transport_security_state; |
| 10328 | context.set_transport_security_state(&transport_security_state); |
| 10329 | context.Init(); |
| 10330 | |
| 10331 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10332 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10333 | GURL(base::StringPrintf("https://hsts-hpkp-preloaded.test:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 10334 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10335 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10336 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10337 | r->Start(); |
| 10338 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10339 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10340 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10341 | |
| 10342 | EXPECT_EQ(1, d.response_started_count()); |
| 10343 | EXPECT_FALSE(d.received_data_before_response()); |
| 10344 | EXPECT_TRUE(d.have_certificate_errors()); |
| 10345 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 10346 | } |
| 10347 | |
| 10348 | // This tests that cached HTTPS page loads do not cause any updates to the |
| 10349 | // TransportSecurityState. |
| 10350 | TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10351 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 10352 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10353 | // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't |
| 10354 | // matter. It just has to be any error. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10355 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10356 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 10357 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10358 | ASSERT_TRUE(test_server.Start()); |
| 10359 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10360 | // We require that the URL be hsts-hpkp-preloaded.test. This is a test domain |
| 10361 | // that has a preloaded HSTS+HPKP entry in the TransportSecurityState. This |
| 10362 | // means that we have to use a MockHostResolver in order to direct |
| 10363 | // hsts-hpkp-preloaded.test to the testserver. By default, MockHostResolver |
| 10364 | // maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10365 | |
| 10366 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 10367 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10368 | TestURLRequestContext context(true); |
| 10369 | context.set_network_delegate(&network_delegate); |
| 10370 | context.set_host_resolver(&host_resolver); |
| 10371 | TransportSecurityState transport_security_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10372 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10373 | TransportSecurityState::STSState static_sts_state; |
| 10374 | TransportSecurityState::PKPState static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10375 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10376 | "hsts-hpkp-preloaded.test", &static_sts_state, &static_pkp_state)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10377 | context.set_transport_security_state(&transport_security_state); |
| 10378 | context.Init(); |
| 10379 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10380 | TransportSecurityState::STSState dynamic_sts_state; |
| 10381 | TransportSecurityState::PKPState dynamic_pkp_state; |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10382 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
| 10383 | "hsts-hpkp-preloaded.test", &dynamic_sts_state)); |
| 10384 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
| 10385 | "hsts-hpkp-preloaded.test", &dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10386 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10387 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10388 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10389 | GURL(base::StringPrintf("https://hsts-hpkp-preloaded.test:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 10390 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10391 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10392 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10393 | r->Start(); |
| 10394 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10395 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10396 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10397 | |
| 10398 | EXPECT_EQ(1, d.response_started_count()); |
| 10399 | EXPECT_FALSE(d.received_data_before_response()); |
| 10400 | EXPECT_TRUE(d.have_certificate_errors()); |
| 10401 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 10402 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10403 | // Get a fresh copy of the states, and check that they haven't changed. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10404 | TransportSecurityState::STSState new_static_sts_state; |
| 10405 | TransportSecurityState::PKPState new_static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10406 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10407 | "hsts-hpkp-preloaded.test", &new_static_sts_state, |
| 10408 | &new_static_pkp_state)); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10409 | TransportSecurityState::STSState new_dynamic_sts_state; |
| 10410 | TransportSecurityState::PKPState new_dynamic_pkp_state; |
| 10411 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10412 | "hsts-hpkp-preloaded.test", &new_dynamic_sts_state)); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10413 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10414 | "hsts-hpkp-preloaded.test", &new_dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10415 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10416 | EXPECT_EQ(new_static_sts_state.upgrade_mode, static_sts_state.upgrade_mode); |
| 10417 | EXPECT_EQ(new_static_sts_state.include_subdomains, |
| 10418 | static_sts_state.include_subdomains); |
| 10419 | EXPECT_EQ(new_static_pkp_state.include_subdomains, |
| 10420 | static_pkp_state.include_subdomains); |
rsleevi | 91d4c9c | 2016-05-14 20:28:48 | [diff] [blame] | 10421 | EXPECT_EQ(new_static_pkp_state.spki_hashes, static_pkp_state.spki_hashes); |
| 10422 | EXPECT_EQ(new_static_pkp_state.bad_spki_hashes, |
| 10423 | static_pkp_state.bad_spki_hashes); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10424 | } |
| 10425 | |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10426 | // Make sure HSTS preserves a POST request's method and body. |
| 10427 | TEST_F(HTTPSRequestTest, HSTSPreservesPosts) { |
| 10428 | static const char kData[] = "hello world"; |
| 10429 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10430 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10431 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10432 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10433 | ASSERT_TRUE(test_server.Start()); |
| 10434 | |
| 10435 | |
| 10436 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 10437 | // address, so a MockHostResolver is needed to redirect www.somewhere.com to |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10438 | // the EmbeddedTestServer. By default, MockHostResolver maps all hosts |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10439 | // to 127.0.0.1. |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10440 | MockHostResolver host_resolver; |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10441 | |
| 10442 | // Force https for www.somewhere.com. |
| 10443 | TransportSecurityState transport_security_state; |
[email protected] | 474f079e | 2013-03-02 19:11:20 | [diff] [blame] | 10444 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 10445 | bool include_subdomains = false; |
| 10446 | transport_security_state.AddHSTS("www.somewhere.com", expiry, |
| 10447 | include_subdomains); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10448 | |
| 10449 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 10450 | |
| 10451 | TestURLRequestContext context(true); |
| 10452 | context.set_host_resolver(&host_resolver); |
| 10453 | context.set_transport_security_state(&transport_security_state); |
| 10454 | context.set_network_delegate(&network_delegate); |
| 10455 | context.Init(); |
| 10456 | |
| 10457 | TestDelegate d; |
| 10458 | // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will |
| 10459 | // cause a certificate error. Ignore the error. |
| 10460 | d.set_allow_certificate_errors(true); |
| 10461 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10462 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10463 | GURL(base::StringPrintf("http://www.somewhere.com:%d/echo", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 10464 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10465 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10466 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 10467 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10468 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10469 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10470 | d.RunUntilComplete(); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10471 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10472 | EXPECT_EQ("https", req->url().scheme()); |
| 10473 | EXPECT_EQ("POST", req->method()); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10474 | EXPECT_EQ(kData, d.data_received()); |
[email protected] | 07d9bfd | 2013-06-27 14:16:40 | [diff] [blame] | 10475 | |
| 10476 | LoadTimingInfo load_timing_info; |
| 10477 | network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info); |
| 10478 | // LoadTimingInfo of HSTS redirects is similar to that of network cache hits |
| 10479 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10480 | } |
| 10481 | |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10482 | // Make sure that the CORS headers are added to cross-origin HSTS redirects. |
| 10483 | TEST_F(HTTPSRequestTest, HSTSCrossOriginAddHeaders) { |
| 10484 | static const char kOriginHeaderValue[] = "http://www.example.com"; |
| 10485 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10486 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10487 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10488 | ASSERT_TRUE(test_server.Start()); |
| 10489 | |
| 10490 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 10491 | // address, so a MockHostResolver is needed to redirect example.net to the |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10492 | // EmbeddedTestServer. MockHostResolver maps all hosts to 127.0.0.1 by |
| 10493 | // default. |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10494 | MockHostResolver host_resolver; |
| 10495 | |
| 10496 | TransportSecurityState transport_security_state; |
| 10497 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 10498 | bool include_subdomains = false; |
| 10499 | transport_security_state.AddHSTS("example.net", expiry, include_subdomains); |
| 10500 | |
| 10501 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 10502 | |
| 10503 | MockCertVerifier cert_verifier; |
| 10504 | cert_verifier.set_default_result(OK); |
| 10505 | |
| 10506 | TestURLRequestContext context(true); |
| 10507 | context.set_host_resolver(&host_resolver); |
| 10508 | context.set_transport_security_state(&transport_security_state); |
| 10509 | context.set_network_delegate(&network_delegate); |
| 10510 | context.set_cert_verifier(&cert_verifier); |
| 10511 | context.Init(); |
| 10512 | |
| 10513 | GURL hsts_http_url(base::StringPrintf("http://example.net:%d/somehstssite", |
| 10514 | test_server.host_port_pair().port())); |
| 10515 | url::Replacements<char> replacements; |
| 10516 | const char kNewScheme[] = "https"; |
| 10517 | replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme))); |
| 10518 | GURL hsts_https_url = hsts_http_url.ReplaceComponents(replacements); |
| 10519 | |
| 10520 | TestDelegate d; |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10521 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10522 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 10523 | hsts_http_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10524 | // Set Origin header to simulate a cross-origin request. |
| 10525 | HttpRequestHeaders request_headers; |
| 10526 | request_headers.SetHeader("Origin", kOriginHeaderValue); |
| 10527 | req->SetExtraRequestHeaders(request_headers); |
| 10528 | |
| 10529 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10530 | d.RunUntilRedirect(); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10531 | |
| 10532 | EXPECT_EQ(1, d.received_redirect_count()); |
| 10533 | |
| 10534 | const HttpResponseHeaders* headers = req->response_headers(); |
| 10535 | std::string redirect_location; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 10536 | EXPECT_TRUE( |
| 10537 | headers->EnumerateHeader(nullptr, "Location", &redirect_location)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10538 | EXPECT_EQ(hsts_https_url.spec(), redirect_location); |
| 10539 | |
| 10540 | std::string received_cors_header; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 10541 | EXPECT_TRUE(headers->EnumerateHeader(nullptr, "Access-Control-Allow-Origin", |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10542 | &received_cors_header)); |
| 10543 | EXPECT_EQ(kOriginHeaderValue, received_cors_header); |
| 10544 | } |
| 10545 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10546 | namespace { |
| 10547 | |
| 10548 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 10549 | public: |
| 10550 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10551 | set_on_complete(base::DoNothing()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10552 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 10553 | void OnCertificateRequested(URLRequest* request, |
| 10554 | SSLCertRequestInfo* cert_request_info) override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10555 | on_certificate_requested_count_++; |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10556 | std::move(on_certificate_requested_).Run(); |
| 10557 | } |
| 10558 | void RunUntilCertificateRequested() { |
| 10559 | base::RunLoop run_loop; |
| 10560 | on_certificate_requested_ = run_loop.QuitClosure(); |
| 10561 | run_loop.Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10562 | } |
| 10563 | int on_certificate_requested_count() { |
| 10564 | return on_certificate_requested_count_; |
| 10565 | } |
| 10566 | private: |
| 10567 | int on_certificate_requested_count_; |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10568 | base::OnceClosure on_certificate_requested_; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10569 | }; |
| 10570 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10571 | class TestSSLPrivateKey : public SSLPrivateKey { |
| 10572 | public: |
| 10573 | explicit TestSSLPrivateKey(scoped_refptr<SSLPrivateKey> key) |
| 10574 | : key_(std::move(key)) {} |
| 10575 | |
| 10576 | void set_fail_signing(bool fail_signing) { fail_signing_ = fail_signing; } |
| 10577 | int sign_count() const { return sign_count_; } |
| 10578 | |
David Benjamin | b65b073 | 2018-11-09 20:33:53 | [diff] [blame] | 10579 | std::string GetProviderName() override { return key_->GetProviderName(); } |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10580 | std::vector<uint16_t> GetAlgorithmPreferences() override { |
| 10581 | return key_->GetAlgorithmPreferences(); |
| 10582 | } |
| 10583 | void Sign(uint16_t algorithm, |
| 10584 | base::span<const uint8_t> input, |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 10585 | SignCallback callback) override { |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10586 | sign_count_++; |
| 10587 | if (fail_signing_) { |
| 10588 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 10589 | FROM_HERE, base::BindOnce(std::move(callback), |
| 10590 | ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED, |
| 10591 | std::vector<uint8_t>())); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10592 | } else { |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 10593 | key_->Sign(algorithm, input, std::move(callback)); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10594 | } |
| 10595 | } |
| 10596 | |
| 10597 | private: |
| 10598 | ~TestSSLPrivateKey() override = default; |
| 10599 | |
| 10600 | scoped_refptr<SSLPrivateKey> key_; |
| 10601 | bool fail_signing_ = false; |
| 10602 | int sign_count_ = 0; |
| 10603 | }; |
| 10604 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10605 | } // namespace |
| 10606 | |
| 10607 | // TODO(davidben): Test the rest of the code. Specifically, |
| 10608 | // - Filtering which certificates to select. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10609 | // - Getting a certificate request in an SSL renegotiation sending the |
| 10610 | // HTTP request. |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10611 | TEST_F(HTTPSRequestTest, ClientAuthNoCertificate) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10612 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10613 | net::SSLServerConfig ssl_config; |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 10614 | ssl_config.client_cert_type = |
| 10615 | SSLServerConfig::ClientCertType::OPTIONAL_CLIENT_CERT; |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10616 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10617 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10618 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10619 | ASSERT_TRUE(test_server.Start()); |
| 10620 | |
| 10621 | SSLClientAuthTestDelegate d; |
| 10622 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10623 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10624 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10625 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10626 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10627 | r->Start(); |
| 10628 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10629 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10630 | d.RunUntilCertificateRequested(); |
| 10631 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10632 | |
| 10633 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10634 | EXPECT_FALSE(d.received_data_before_response()); |
| 10635 | EXPECT_EQ(0, d.bytes_received()); |
| 10636 | |
| 10637 | // Send no certificate. |
| 10638 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 10639 | // all platforms so we can test sending a cert as well. |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 10640 | r->ContinueWithCertificate(nullptr, nullptr); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10641 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10642 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10643 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10644 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10645 | EXPECT_EQ(1, d.response_started_count()); |
| 10646 | EXPECT_FALSE(d.received_data_before_response()); |
| 10647 | EXPECT_NE(0, d.bytes_received()); |
| 10648 | } |
| 10649 | } |
| 10650 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10651 | TEST_F(HTTPSRequestTest, ClientAuth) { |
| 10652 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 10653 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 10654 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 10655 | ASSERT_TRUE(identity); |
| 10656 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 10657 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 10658 | |
| 10659 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10660 | net::SSLServerConfig ssl_config; |
| 10661 | ssl_config.client_cert_type = |
| 10662 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 10663 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 10664 | test_server.AddDefaultHandlers( |
| 10665 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10666 | ASSERT_TRUE(test_server.Start()); |
| 10667 | |
| 10668 | { |
| 10669 | SSLClientAuthTestDelegate d; |
| 10670 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10671 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10672 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10673 | |
| 10674 | r->Start(); |
| 10675 | EXPECT_TRUE(r->is_pending()); |
| 10676 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10677 | d.RunUntilCertificateRequested(); |
| 10678 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10679 | |
| 10680 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10681 | EXPECT_FALSE(d.received_data_before_response()); |
| 10682 | EXPECT_EQ(0, d.bytes_received()); |
| 10683 | |
| 10684 | // Send a certificate. |
| 10685 | r->ContinueWithCertificate(identity->certificate(), private_key); |
| 10686 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10687 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10688 | |
| 10689 | EXPECT_EQ(OK, d.request_status()); |
| 10690 | EXPECT_EQ(1, d.response_started_count()); |
| 10691 | EXPECT_FALSE(d.received_data_before_response()); |
| 10692 | EXPECT_NE(0, d.bytes_received()); |
| 10693 | |
| 10694 | // The private key should have been used. |
| 10695 | EXPECT_EQ(1, private_key->sign_count()); |
| 10696 | } |
| 10697 | |
| 10698 | // Close all connections and clear the session cache to force a new handshake. |
| 10699 | default_context_.http_transaction_factory() |
| 10700 | ->GetSession() |
| 10701 | ->CloseAllConnections(); |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10702 | default_context_.http_transaction_factory() |
| 10703 | ->GetSession() |
| 10704 | ->ClearSSLSessionCache(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10705 | |
| 10706 | // Connecting again should not call OnCertificateRequested. The identity is |
| 10707 | // taken from the client auth cache. |
| 10708 | { |
| 10709 | SSLClientAuthTestDelegate d; |
| 10710 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10711 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10712 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10713 | |
| 10714 | r->Start(); |
| 10715 | EXPECT_TRUE(r->is_pending()); |
| 10716 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10717 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10718 | |
| 10719 | EXPECT_EQ(OK, d.request_status()); |
| 10720 | EXPECT_EQ(0, d.on_certificate_requested_count()); |
| 10721 | EXPECT_FALSE(d.received_data_before_response()); |
| 10722 | EXPECT_EQ(1, d.response_started_count()); |
| 10723 | EXPECT_FALSE(d.received_data_before_response()); |
| 10724 | EXPECT_NE(0, d.bytes_received()); |
| 10725 | |
| 10726 | // The private key should have been used. |
| 10727 | EXPECT_EQ(2, private_key->sign_count()); |
| 10728 | } |
| 10729 | } |
| 10730 | |
| 10731 | // Test that private keys that fail to sign anything get evicted from the cache. |
| 10732 | TEST_F(HTTPSRequestTest, ClientAuthFailSigning) { |
| 10733 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 10734 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 10735 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 10736 | ASSERT_TRUE(identity); |
| 10737 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 10738 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 10739 | private_key->set_fail_signing(true); |
| 10740 | |
| 10741 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10742 | net::SSLServerConfig ssl_config; |
| 10743 | ssl_config.client_cert_type = |
| 10744 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 10745 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 10746 | test_server.AddDefaultHandlers( |
| 10747 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10748 | ASSERT_TRUE(test_server.Start()); |
| 10749 | |
| 10750 | { |
| 10751 | SSLClientAuthTestDelegate d; |
| 10752 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10753 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10754 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10755 | |
| 10756 | r->Start(); |
| 10757 | EXPECT_TRUE(r->is_pending()); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10758 | |
| 10759 | d.RunUntilCertificateRequested(); |
| 10760 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10761 | |
| 10762 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10763 | EXPECT_FALSE(d.received_data_before_response()); |
| 10764 | EXPECT_EQ(0, d.bytes_received()); |
| 10765 | |
| 10766 | // Send a certificate. |
| 10767 | r->ContinueWithCertificate(identity->certificate(), private_key); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10768 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10769 | |
| 10770 | // The private key cannot sign anything, so we report an error. |
| 10771 | EXPECT_EQ(ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED, d.request_status()); |
| 10772 | EXPECT_EQ(1, d.response_started_count()); |
| 10773 | EXPECT_FALSE(d.received_data_before_response()); |
| 10774 | EXPECT_EQ(0, d.bytes_received()); |
| 10775 | |
| 10776 | // The private key should have been used. |
| 10777 | EXPECT_EQ(1, private_key->sign_count()); |
| 10778 | } |
| 10779 | |
| 10780 | // Close all connections and clear the session cache to force a new handshake. |
| 10781 | default_context_.http_transaction_factory() |
| 10782 | ->GetSession() |
| 10783 | ->CloseAllConnections(); |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10784 | default_context_.http_transaction_factory() |
| 10785 | ->GetSession() |
| 10786 | ->ClearSSLSessionCache(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10787 | |
| 10788 | // The bad identity should have been evicted from the cache, so connecting |
| 10789 | // again should call OnCertificateRequested again. |
| 10790 | { |
| 10791 | SSLClientAuthTestDelegate d; |
| 10792 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10793 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10794 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10795 | |
| 10796 | r->Start(); |
| 10797 | EXPECT_TRUE(r->is_pending()); |
| 10798 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10799 | d.RunUntilCertificateRequested(); |
| 10800 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10801 | |
| 10802 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10803 | EXPECT_FALSE(d.received_data_before_response()); |
| 10804 | EXPECT_EQ(0, d.bytes_received()); |
| 10805 | |
| 10806 | // There should have been no additional uses of the private key. |
| 10807 | EXPECT_EQ(1, private_key->sign_count()); |
| 10808 | } |
| 10809 | } |
| 10810 | |
| 10811 | // Test that cached private keys that fail to sign anything trigger a |
| 10812 | // retry. This is so we handle unplugged smartcards |
| 10813 | // gracefully. https://crbug.com/813022. |
| 10814 | TEST_F(HTTPSRequestTest, ClientAuthFailSigningRetry) { |
| 10815 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 10816 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 10817 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 10818 | ASSERT_TRUE(identity); |
| 10819 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 10820 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 10821 | |
| 10822 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10823 | net::SSLServerConfig ssl_config; |
| 10824 | ssl_config.client_cert_type = |
| 10825 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 10826 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 10827 | test_server.AddDefaultHandlers( |
| 10828 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10829 | ASSERT_TRUE(test_server.Start()); |
| 10830 | |
| 10831 | // Connect with a client certificate to put it in the client auth cache. |
| 10832 | { |
| 10833 | SSLClientAuthTestDelegate d; |
| 10834 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10835 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10836 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10837 | |
| 10838 | r->Start(); |
| 10839 | EXPECT_TRUE(r->is_pending()); |
| 10840 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10841 | d.RunUntilCertificateRequested(); |
| 10842 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10843 | |
| 10844 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10845 | EXPECT_FALSE(d.received_data_before_response()); |
| 10846 | EXPECT_EQ(0, d.bytes_received()); |
| 10847 | |
| 10848 | r->ContinueWithCertificate(identity->certificate(), private_key); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10849 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10850 | |
| 10851 | EXPECT_EQ(OK, d.request_status()); |
| 10852 | EXPECT_EQ(1, d.response_started_count()); |
| 10853 | EXPECT_FALSE(d.received_data_before_response()); |
| 10854 | EXPECT_NE(0, d.bytes_received()); |
| 10855 | |
| 10856 | // The private key should have been used. |
| 10857 | EXPECT_EQ(1, private_key->sign_count()); |
| 10858 | } |
| 10859 | |
| 10860 | // Close all connections and clear the session cache to force a new handshake. |
| 10861 | default_context_.http_transaction_factory() |
| 10862 | ->GetSession() |
| 10863 | ->CloseAllConnections(); |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10864 | default_context_.http_transaction_factory() |
| 10865 | ->GetSession() |
| 10866 | ->ClearSSLSessionCache(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10867 | |
| 10868 | // Cause the private key to fail. Connecting again should attempt to use it, |
| 10869 | // notice the failure, and then request a new identity via |
| 10870 | // OnCertificateRequested. |
| 10871 | private_key->set_fail_signing(true); |
| 10872 | |
| 10873 | { |
| 10874 | SSLClientAuthTestDelegate d; |
| 10875 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10876 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10877 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10878 | |
| 10879 | r->Start(); |
| 10880 | EXPECT_TRUE(r->is_pending()); |
| 10881 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 10882 | d.RunUntilCertificateRequested(); |
| 10883 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10884 | |
| 10885 | // There was an additional signing call on the private key (the one which |
| 10886 | // failed). |
| 10887 | EXPECT_EQ(2, private_key->sign_count()); |
| 10888 | |
| 10889 | // That caused another OnCertificateRequested call. |
| 10890 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10891 | EXPECT_FALSE(d.received_data_before_response()); |
| 10892 | EXPECT_EQ(0, d.bytes_received()); |
| 10893 | } |
| 10894 | } |
| 10895 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10896 | TEST_F(HTTPSRequestTest, ResumeTest) { |
| 10897 | // Test that we attempt a session resume when making two connections to the |
| 10898 | // same host. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10899 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10900 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10901 | SpawnedTestServer test_server( |
| 10902 | SpawnedTestServer::TYPE_HTTPS, |
| 10903 | ssl_options, |
| 10904 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10905 | ASSERT_TRUE(test_server.Start()); |
| 10906 | |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10907 | default_context_.http_transaction_factory() |
| 10908 | ->GetSession() |
| 10909 | ->ClearSSLSessionCache(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10910 | |
| 10911 | { |
| 10912 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10913 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10914 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10915 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10916 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10917 | r->Start(); |
| 10918 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10919 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10920 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10921 | |
| 10922 | EXPECT_EQ(1, d.response_started_count()); |
| 10923 | } |
| 10924 | |
| 10925 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 10926 | CloseAllConnections(); |
| 10927 | |
| 10928 | { |
| 10929 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10930 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10931 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10932 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10933 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10934 | r->Start(); |
| 10935 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10936 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10937 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10938 | |
| 10939 | // The response will look like; |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10940 | // lookup uvw (TLS 1.3's compatibility session ID) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10941 | // insert abc |
| 10942 | // lookup abc |
| 10943 | // insert xyz |
| 10944 | // |
| 10945 | // With a newline at the end which makes the split think that there are |
| 10946 | // four lines. |
| 10947 | |
| 10948 | EXPECT_EQ(1, d.response_started_count()); |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 10949 | std::vector<std::string> lines = base::SplitString( |
| 10950 | d.data_received(), "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10951 | ASSERT_EQ(5u, lines.size()) << d.data_received(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10952 | |
| 10953 | std::string session_id; |
| 10954 | |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10955 | for (size_t i = 0; i < 3; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 10956 | std::vector<std::string> parts = base::SplitString( |
| 10957 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10958 | ASSERT_EQ(2u, parts.size()); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10959 | if (i % 2 == 1) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10960 | EXPECT_EQ("insert", parts[0]); |
| 10961 | session_id = parts[1]; |
| 10962 | } else { |
| 10963 | EXPECT_EQ("lookup", parts[0]); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10964 | if (i != 0) |
| 10965 | EXPECT_EQ(session_id, parts[1]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10966 | } |
| 10967 | } |
| 10968 | } |
| 10969 | } |
| 10970 | |
| 10971 | TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { |
| 10972 | // Test that sessions aren't resumed when the value of ssl_session_cache_shard |
| 10973 | // differs. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10974 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10975 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10976 | SpawnedTestServer test_server( |
| 10977 | SpawnedTestServer::TYPE_HTTPS, |
| 10978 | ssl_options, |
| 10979 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10980 | ASSERT_TRUE(test_server.Start()); |
| 10981 | |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 10982 | default_context_.http_transaction_factory() |
| 10983 | ->GetSession() |
| 10984 | ->ClearSSLSessionCache(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10985 | |
| 10986 | { |
| 10987 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10988 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 10989 | test_server.GetURL("/"), DEFAULT_PRIORITY, &d, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10990 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10991 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10992 | r->Start(); |
| 10993 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10994 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10995 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10996 | |
| 10997 | EXPECT_EQ(1, d.response_started_count()); |
| 10998 | } |
| 10999 | |
| 11000 | // Now create a new HttpCache with a different ssl_session_cache_shard value. |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 11001 | HttpNetworkSession::Context session_context; |
| 11002 | session_context.host_resolver = default_context_.host_resolver(); |
| 11003 | session_context.cert_verifier = default_context_.cert_verifier(); |
| 11004 | session_context.transport_security_state = |
| 11005 | default_context_.transport_security_state(); |
| 11006 | session_context.cert_transparency_verifier = |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 11007 | default_context_.cert_transparency_verifier(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 11008 | session_context.ct_policy_enforcer = default_context_.ct_policy_enforcer(); |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 11009 | session_context.proxy_resolution_service = default_context_.proxy_resolution_service(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 11010 | session_context.ssl_config_service = default_context_.ssl_config_service(); |
| 11011 | session_context.http_auth_handler_factory = |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11012 | default_context_.http_auth_handler_factory(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 11013 | session_context.http_server_properties = |
| 11014 | default_context_.http_server_properties(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11015 | |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 11016 | HttpNetworkSession network_session(HttpNetworkSession::Params(), |
| 11017 | session_context); |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 11018 | std::unique_ptr<HttpCache> cache( |
| 11019 | new HttpCache(&network_session, HttpCache::DefaultBackend::InMemory(0), |
| 11020 | false /* is_main_cache */)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11021 | |
| 11022 | default_context_.set_http_transaction_factory(cache.get()); |
| 11023 | |
| 11024 | { |
| 11025 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11026 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11027 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 11028 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11029 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11030 | r->Start(); |
| 11031 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11032 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11033 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11034 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11035 | EXPECT_EQ(1, d.response_started_count()); |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 11036 | EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, r->ssl_info().handshake_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11037 | } |
| 11038 | } |
| 11039 | |
Daniel McArdle | da3fa94 | 2019-02-15 16:41:21 | [diff] [blame] | 11040 | // Test that sessions started with privacy mode enabled cannot be resumed when |
| 11041 | // it is disabled, and vice versa. |
| 11042 | TEST_F(HTTPSRequestTest, NoSessionResumptionBetweenPrivacyModes) { |
| 11043 | // Start a server. |
| 11044 | SpawnedTestServer test_server( |
| 11045 | SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::SSLOptions(), |
| 11046 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 11047 | ASSERT_TRUE(test_server.Start()); |
| 11048 | const auto url = test_server.GetURL("/"); |
| 11049 | |
| 11050 | auto ConnectAndCheckHandshake = [this, url](bool allow_credentials, |
| 11051 | auto expected_handshake) { |
| 11052 | // Construct request and indirectly set the privacy mode. |
| 11053 | TestDelegate d; |
| 11054 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 11055 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11056 | r->set_allow_credentials(allow_credentials); |
| 11057 | |
| 11058 | // Start the request and check the SSL handshake type. |
| 11059 | r->Start(); |
| 11060 | EXPECT_TRUE(r->is_pending()); |
| 11061 | d.RunUntilComplete(); |
| 11062 | EXPECT_EQ(1, d.response_started_count()); |
| 11063 | EXPECT_EQ(expected_handshake, r->ssl_info().handshake_type); |
| 11064 | }; |
| 11065 | |
| 11066 | // Exhaustively check all pairs of privacy mode values. Note that we are using |
| 11067 | // allow_credentials to indirectly enable/disable privacy mode. |
| 11068 | const bool kAllowCredentialsValues[] = {false, true}; |
| 11069 | for (const auto allow_creds_1 : kAllowCredentialsValues) { |
| 11070 | for (const auto allow_creds_2 : kAllowCredentialsValues) { |
| 11071 | SCOPED_TRACE(base::StringPrintf("allow_creds_1=%d, allow_creds_2=%d", |
| 11072 | allow_creds_1, allow_creds_2)); |
| 11073 | |
| 11074 | // The session cache starts off empty, so we expect a full handshake. |
| 11075 | ConnectAndCheckHandshake(allow_creds_1, SSLInfo::HANDSHAKE_FULL); |
| 11076 | |
| 11077 | // The second handshake depends on whether we are using the same session |
| 11078 | // cache as the first request. |
| 11079 | ConnectAndCheckHandshake(allow_creds_2, allow_creds_1 == allow_creds_2 |
| 11080 | ? SSLInfo::HANDSHAKE_RESUME |
| 11081 | : SSLInfo::HANDSHAKE_FULL); |
| 11082 | // Flush both session caches. |
| 11083 | auto* network_session = |
| 11084 | default_context_.http_transaction_factory()->GetSession(); |
| 11085 | network_session->ClearSSLSessionCache(); |
| 11086 | } |
| 11087 | } |
| 11088 | } |
| 11089 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 11090 | class HTTPSFallbackTest : public TestWithScopedTaskEnvironment { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11091 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 11092 | HTTPSFallbackTest() : context_(true) { |
Nick Harper | 2243e800 | 2018-09-28 20:33:26 | [diff] [blame] | 11093 | ssl_config_service_ = std::make_unique<TestSSLConfigService>(); |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 11094 | context_.set_ssl_config_service(ssl_config_service_.get()); |
| 11095 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 11096 | ~HTTPSFallbackTest() override = default; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11097 | |
| 11098 | protected: |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 11099 | TestSSLConfigService* ssl_config_service() { |
| 11100 | return ssl_config_service_.get(); |
| 11101 | } |
| 11102 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11103 | void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) { |
| 11104 | DCHECK(!request_); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 11105 | context_.Init(); |
| 11106 | delegate_.set_allow_certificate_errors(true); |
| 11107 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11108 | SpawnedTestServer test_server( |
| 11109 | SpawnedTestServer::TYPE_HTTPS, |
| 11110 | ssl_options, |
| 11111 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 11112 | ASSERT_TRUE(test_server.Start()); |
| 11113 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11114 | request_ = context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11115 | &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11116 | request_->Start(); |
| 11117 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11118 | delegate_.RunUntilComplete(); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11119 | } |
| 11120 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 11121 | void ExpectConnection(int version) { |
| 11122 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 11123 | EXPECT_NE(0, delegate_.bytes_received()); |
| 11124 | EXPECT_EQ(version, SSLConnectionStatusToVersion( |
| 11125 | request_->ssl_info().connection_status)); |
| 11126 | } |
| 11127 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11128 | void ExpectFailure(int error) { |
| 11129 | EXPECT_EQ(1, delegate_.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 11130 | EXPECT_EQ(error, delegate_.request_status()); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11131 | } |
| 11132 | |
| 11133 | private: |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11134 | std::unique_ptr<TestSSLConfigService> ssl_config_service_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11135 | TestDelegate delegate_; |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 11136 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11137 | std::unique_ptr<URLRequest> request_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11138 | }; |
| 11139 | |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 11140 | // Tests the TLS 1.0 fallback doesn't happen. |
| 11141 | TEST_F(HTTPSFallbackTest, TLSv1NoFallback) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11142 | SpawnedTestServer::SSLOptions ssl_options( |
| 11143 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 11144 | ssl_options.tls_intolerant = |
| 11145 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 11146 | |
| 11147 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 11148 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 11149 | } |
| 11150 | |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 11151 | // Tests the TLS 1.1 fallback doesn't happen. |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 11152 | TEST_F(HTTPSFallbackTest, TLSv1_1NoFallback) { |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 11153 | SpawnedTestServer::SSLOptions ssl_options( |
| 11154 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 11155 | ssl_options.tls_intolerant = |
| 11156 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2; |
| 11157 | |
| 11158 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 11159 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
| 11160 | } |
| 11161 | |
David Benjamin | d61bd53 | 2019-04-23 21:11:37 | [diff] [blame] | 11162 | // Tests the TLS 1.2 fallback doesn't happen. |
| 11163 | TEST_F(HTTPSFallbackTest, TLSv1_2NoFallback) { |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 11164 | SpawnedTestServer::SSLOptions ssl_options( |
| 11165 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 11166 | ssl_options.tls_intolerant = |
| 11167 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_3; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 11168 | |
| 11169 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
David Benjamin | d61bd53 | 2019-04-23 21:11:37 | [diff] [blame] | 11170 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 11171 | } |
| 11172 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 11173 | class HTTPSSessionTest : public TestWithScopedTaskEnvironment { |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11174 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 11175 | HTTPSSessionTest() : default_context_(true) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 11176 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11177 | |
| 11178 | default_context_.set_network_delegate(&default_network_delegate_); |
| 11179 | default_context_.set_cert_verifier(&cert_verifier_); |
| 11180 | default_context_.Init(); |
| 11181 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 11182 | ~HTTPSSessionTest() override = default; |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11183 | |
| 11184 | protected: |
| 11185 | MockCertVerifier cert_verifier_; |
| 11186 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 11187 | TestURLRequestContext default_context_; |
| 11188 | }; |
| 11189 | |
| 11190 | // Tests that session resumption is not attempted if an invalid certificate |
| 11191 | // is presented. |
| 11192 | TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) { |
| 11193 | SpawnedTestServer::SSLOptions ssl_options; |
| 11194 | ssl_options.record_resume = true; |
| 11195 | SpawnedTestServer test_server( |
| 11196 | SpawnedTestServer::TYPE_HTTPS, |
| 11197 | ssl_options, |
| 11198 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 11199 | ASSERT_TRUE(test_server.Start()); |
| 11200 | |
Daniel McArdle | 3a663d6 | 2019-01-31 00:48:47 | [diff] [blame] | 11201 | default_context_.http_transaction_factory() |
| 11202 | ->GetSession() |
| 11203 | ->ClearSSLSessionCache(); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11204 | |
| 11205 | // Simulate the certificate being expired and attempt a connection. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 11206 | cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11207 | { |
| 11208 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11209 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 11210 | test_server.GetURL("/"), DEFAULT_PRIORITY, &d, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11211 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11212 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11213 | r->Start(); |
| 11214 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11215 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11216 | d.RunUntilComplete(); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11217 | |
| 11218 | EXPECT_EQ(1, d.response_started_count()); |
| 11219 | } |
| 11220 | |
| 11221 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 11222 | CloseAllConnections(); |
| 11223 | |
| 11224 | // Now change the certificate to be acceptable (so that the response is |
| 11225 | // loaded), and ensure that no session id is presented to the peer. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 11226 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11227 | { |
| 11228 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11229 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11230 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 11231 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11232 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11233 | r->Start(); |
| 11234 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11235 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11236 | d.RunUntilComplete(); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11237 | |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11238 | EXPECT_EQ(1, d.response_started_count()); |
Jesse Selover | 94c9a94 | 2019-01-16 01:18:04 | [diff] [blame] | 11239 | EXPECT_EQ(SSLInfo::HANDSHAKE_FULL, r->ssl_info().handshake_type); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 11240 | } |
| 11241 | } |
| 11242 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11243 | // This the fingerprint of the "Testing CA" certificate used by the testserver. |
| 11244 | // See net/data/ssl/certificates/ocsp-test-root.pem. |
David Benjamin | 9cedc3a5 | 2017-08-20 21:30:58 | [diff] [blame] | 11245 | static const SHA256HashValue kOCSPTestCertFingerprint = {{ |
| 11246 | 0x0c, 0xa9, 0x05, 0x11, 0xb0, 0xa2, 0xc0, 0x1d, 0x40, 0x6a, 0x99, |
| 11247 | 0x04, 0x21, 0x36, 0x45, 0x3f, 0x59, 0x12, 0x5c, 0x80, 0x64, 0x2d, |
| 11248 | 0x46, 0x6a, 0x3b, 0x78, 0x9e, 0x84, 0xea, 0x54, 0x0f, 0x8b, |
mattm | 3b4376f1 | 2016-10-03 21:07:15 | [diff] [blame] | 11249 | }}; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11250 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11251 | // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the |
| 11252 | // testserver. |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11253 | static const SHA256HashValue kOCSPTestCertSPKI = {{ |
| 11254 | 0x05, 0xa8, 0xf6, 0xfd, 0x8e, 0x10, 0xfe, 0x92, 0x2f, 0x22, 0x75, |
| 11255 | 0x46, 0x40, 0xf4, 0xc4, 0x57, 0x06, 0x0d, 0x95, 0xfd, 0x60, 0x31, |
| 11256 | 0x3b, 0xf3, 0xfc, 0x12, 0x47, 0xe7, 0x66, 0x1a, 0x82, 0xa3, |
| 11257 | }}; |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11258 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11259 | // This is the policy OID contained in the certificates that testserver |
| 11260 | // generates. |
| 11261 | static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1"; |
| 11262 | |
| 11263 | class HTTPSOCSPTest : public HTTPSRequestTest { |
| 11264 | public: |
| 11265 | HTTPSOCSPTest() |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 11266 | : context_(true), |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11267 | ev_test_policy_( |
| 11268 | new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(), |
| 11269 | kOCSPTestCertFingerprint, |
| 11270 | kOCSPTestCertPolicy)) { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11271 | } |
| 11272 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 11273 | void SetUp() override { |
Matt Mueller | a4aaa9d | 2019-04-24 21:09:14 | [diff] [blame] | 11274 | cert_net_fetcher_ = base::MakeRefCounted<CertNetFetcherImpl>(); |
| 11275 | cert_verifier_ = CertVerifier::CreateDefault(cert_net_fetcher_); |
| 11276 | context_.set_cert_verifier(cert_verifier_.get()); |
Ryan Sleevi | 8a9c9c1 | 2018-05-09 02:36:23 | [diff] [blame] | 11277 | context_.SetCTPolicyEnforcer(std::make_unique<DefaultCTPolicyEnforcer>()); |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 11278 | context_.Init(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11279 | |
Matt Mueller | a4aaa9d | 2019-04-24 21:09:14 | [diff] [blame] | 11280 | cert_net_fetcher_->SetURLRequestContext(&context_); |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11281 | context_.cert_verifier()->SetConfig(GetCertVerifierConfig()); |
| 11282 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 11283 | scoped_refptr<X509Certificate> root_cert = |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11284 | ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
Matt Mueller | 09c8fd9a | 2019-05-04 00:37:24 | [diff] [blame] | 11285 | ASSERT_TRUE(root_cert); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 11286 | test_root_.reset(new ScopedTestRoot(root_cert.get())); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11287 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 11288 | #if defined(USE_NSS_CERTS) |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 11289 | SetURLRequestContextForNSSHttpIO(&context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11290 | #endif |
| 11291 | } |
| 11292 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11293 | void DoConnectionWithDelegate( |
| 11294 | const SpawnedTestServer::SSLOptions& ssl_options, |
| 11295 | TestDelegate* delegate, |
| 11296 | SSLInfo* out_ssl_info) { |
| 11297 | // Always overwrite |out_ssl_info|. |
| 11298 | out_ssl_info->Reset(); |
| 11299 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11300 | SpawnedTestServer test_server( |
| 11301 | SpawnedTestServer::TYPE_HTTPS, |
| 11302 | ssl_options, |
| 11303 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11304 | ASSERT_TRUE(test_server.Start()); |
| 11305 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11306 | delegate->set_allow_certificate_errors(true); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11307 | std::unique_ptr<URLRequest> r( |
| 11308 | context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
| 11309 | delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11310 | r->Start(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11311 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11312 | delegate->RunUntilComplete(); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11313 | EXPECT_EQ(1, delegate->response_started_count()); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11314 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11315 | *out_ssl_info = r->ssl_info(); |
| 11316 | } |
| 11317 | |
| 11318 | void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options, |
| 11319 | CertStatus* out_cert_status) { |
| 11320 | // Always overwrite |out_cert_status|. |
| 11321 | *out_cert_status = 0; |
| 11322 | |
| 11323 | TestDelegate d; |
| 11324 | SSLInfo ssl_info; |
| 11325 | ASSERT_NO_FATAL_FAILURE( |
| 11326 | DoConnectionWithDelegate(ssl_options, &d, &ssl_info)); |
| 11327 | |
| 11328 | *out_cert_status = ssl_info.cert_status; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11329 | } |
| 11330 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 11331 | ~HTTPSOCSPTest() override { |
Matt Mueller | a4aaa9d | 2019-04-24 21:09:14 | [diff] [blame] | 11332 | cert_net_fetcher_->Shutdown(); |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 11333 | #if defined(USE_NSS_CERTS) |
Matt Mueller | a17ffaf | 2018-03-28 20:08:18 | [diff] [blame] | 11334 | SetURLRequestContextForNSSHttpIO(nullptr); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11335 | #endif |
| 11336 | } |
| 11337 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11338 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11339 | // GetCertVerifierConfig() configures the URLRequestContext that will be used |
| 11340 | // for making connections to the testserver. This can be overridden in test |
| 11341 | // subclasses for different behaviour. |
| 11342 | virtual CertVerifier::Config GetCertVerifierConfig() { |
| 11343 | CertVerifier::Config config; |
| 11344 | config.enable_rev_checking = true; |
| 11345 | return config; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11346 | } |
| 11347 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11348 | std::unique_ptr<ScopedTestRoot> test_root_; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11349 | std::unique_ptr<TestSSLConfigService> ssl_config_service_; |
Matt Mueller | a4aaa9d | 2019-04-24 21:09:14 | [diff] [blame] | 11350 | scoped_refptr<CertNetFetcherImpl> cert_net_fetcher_; |
| 11351 | std::unique_ptr<CertVerifier> cert_verifier_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 11352 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11353 | std::unique_ptr<ScopedTestEVPolicy> ev_test_policy_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11354 | }; |
| 11355 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11356 | static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() { |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11357 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11358 | // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't |
| 11359 | // have that ability on other platforms. |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11360 | // TODO(eroman): Should this also be the return value for |
| 11361 | // CertVerifyProcBuiltin? |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11362 | return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 11363 | #else |
| 11364 | return 0; |
| 11365 | #endif |
| 11366 | } |
| 11367 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11368 | // SystemSupportsHardFailRevocationChecking returns true iff the current |
| 11369 | // operating system supports revocation checking and can distinguish between |
| 11370 | // situations where a given certificate lacks any revocation information (eg: |
| 11371 | // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when |
| 11372 | // revocation information cannot be obtained (eg: the CRL was unreachable). |
| 11373 | // If it does not, then tests which rely on 'hard fail' behaviour should be |
| 11374 | // skipped. |
| 11375 | static bool SystemSupportsHardFailRevocationChecking() { |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11376 | #if defined(OS_WIN) || defined(USE_NSS_CERTS) || \ |
| 11377 | defined(USE_BUILTIN_CERT_VERIFIER) |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11378 | return true; |
| 11379 | #else |
| 11380 | return false; |
| 11381 | #endif |
| 11382 | } |
| 11383 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11384 | // SystemUsesChromiumEVMetadata returns true iff the current operating system |
| 11385 | // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then |
| 11386 | // several tests are effected because our testing EV certificate won't be |
| 11387 | // recognised as EV. |
| 11388 | static bool SystemUsesChromiumEVMetadata() { |
Eric Roman | 5f8d9d2 | 2017-10-17 02:32:52 | [diff] [blame] | 11389 | #if defined(PLATFORM_USES_CHROMIUM_EV_METADATA) |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 11390 | return true; |
Eric Roman | 5f8d9d2 | 2017-10-17 02:32:52 | [diff] [blame] | 11391 | #else |
| 11392 | return false; |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 11393 | #endif |
| 11394 | } |
| 11395 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11396 | // Returns the expected CertStatus for tests that expect an online revocation |
| 11397 | // check failure as a result of checking a test EV cert, which will not |
| 11398 | // actually trigger an online revocation check on some platforms. |
| 11399 | static CertStatus ExpectedCertStatusForFailedOnlineEVRevocationCheck() { |
| 11400 | if (SystemUsesChromiumEVMetadata()) { |
| 11401 | return ExpectedCertStatusForFailedOnlineRevocationCheck(); |
| 11402 | } else { |
| 11403 | // If SystemUsesChromiumEVMetadata is false, revocation checking will not |
| 11404 | // be enabled, and thus there will not be a revocation check to fail. |
| 11405 | return 0u; |
| 11406 | } |
| 11407 | } |
| 11408 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11409 | static bool SystemSupportsOCSP() { |
Sergey Ulanov | ee7c8db | 2017-12-08 00:18:39 | [diff] [blame] | 11410 | #if defined(OS_ANDROID) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11411 | // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. |
| 11412 | return false; |
| 11413 | #else |
| 11414 | return true; |
| 11415 | #endif |
| 11416 | } |
| 11417 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11418 | static bool SystemSupportsOCSPStapling() { |
Matt Mueller | 7f60dc4 | 2019-04-26 22:47:10 | [diff] [blame] | 11419 | #if defined(OS_ANDROID) |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11420 | return false; |
Matt Mueller | 7f60dc4 | 2019-04-26 22:47:10 | [diff] [blame] | 11421 | #elif defined(OS_MACOSX) |
| 11422 | // The SecTrustSetOCSPResponse function exists since macOS 10.9+, but does |
| 11423 | // not actually do anything until 10.12. |
| 11424 | if (base::mac::IsAtLeastOS10_12()) |
| 11425 | return true; |
| 11426 | return false; |
| 11427 | #else |
| 11428 | return true; |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11429 | #endif |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11430 | } |
| 11431 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11432 | TEST_F(HTTPSOCSPTest, Valid) { |
| 11433 | if (!SystemSupportsOCSP()) { |
| 11434 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11435 | return; |
| 11436 | } |
| 11437 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11438 | SpawnedTestServer::SSLOptions ssl_options( |
| 11439 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11440 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11441 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11442 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11443 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11444 | |
| 11445 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11446 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11447 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11448 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11449 | |
| 11450 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11451 | } |
| 11452 | |
| 11453 | TEST_F(HTTPSOCSPTest, Revoked) { |
| 11454 | if (!SystemSupportsOCSP()) { |
| 11455 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11456 | return; |
| 11457 | } |
| 11458 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11459 | SpawnedTestServer::SSLOptions ssl_options( |
| 11460 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11461 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11462 | |
| 11463 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11464 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11465 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11466 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11467 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11468 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11469 | } |
| 11470 | |
| 11471 | TEST_F(HTTPSOCSPTest, Invalid) { |
| 11472 | if (!SystemSupportsOCSP()) { |
| 11473 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11474 | return; |
| 11475 | } |
| 11476 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11477 | SpawnedTestServer::SSLOptions ssl_options( |
| 11478 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11479 | ssl_options.ocsp_status = |
| 11480 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11481 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11482 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11483 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11484 | |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11485 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 11486 | // TODO(649017): This test uses soft-fail revocation checking, but returns an |
| 11487 | // invalid OCSP response (can't parse). CertVerifyProcBuiltin currently |
| 11488 | // doesn't consider this a candidate for soft-fail (only considers |
| 11489 | // network-level failures as skippable). |
| 11490 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 11491 | cert_status & CERT_STATUS_UNABLE_TO_CHECK_REVOCATION); |
| 11492 | #else |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11493 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11494 | cert_status & CERT_STATUS_ALL_ERRORS); |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11495 | #endif |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11496 | |
| 11497 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 11498 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11499 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11500 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11501 | |
Matt Mueller | 5339d86 | 2018-05-02 18:53:57 | [diff] [blame] | 11502 | TEST_F(HTTPSOCSPTest, IntermediateValid) { |
| 11503 | if (!SystemSupportsOCSP()) { |
| 11504 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11505 | return; |
| 11506 | } |
| 11507 | |
| 11508 | SpawnedTestServer::SSLOptions ssl_options( |
| 11509 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11510 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11511 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11512 | |
| 11513 | CertStatus cert_status; |
| 11514 | DoConnection(ssl_options, &cert_status); |
| 11515 | |
| 11516 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11517 | |
| 11518 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11519 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11520 | |
| 11521 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11522 | } |
| 11523 | |
| 11524 | TEST_F(HTTPSOCSPTest, IntermediateResponseOldButStillValid) { |
| 11525 | if (!SystemSupportsOCSP()) { |
| 11526 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11527 | return; |
| 11528 | } |
| 11529 | |
| 11530 | SpawnedTestServer::SSLOptions ssl_options( |
| 11531 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11532 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11533 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11534 | // Use an OCSP response for the intermediate that would be too old for a leaf |
| 11535 | // cert, but is still valid for an intermediate. |
| 11536 | ssl_options.ocsp_intermediate_date = |
| 11537 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG; |
| 11538 | |
| 11539 | CertStatus cert_status; |
| 11540 | DoConnection(ssl_options, &cert_status); |
| 11541 | |
| 11542 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11543 | |
| 11544 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11545 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11546 | |
| 11547 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11548 | } |
| 11549 | |
| 11550 | TEST_F(HTTPSOCSPTest, IntermediateResponseTooOld) { |
| 11551 | if (!SystemSupportsOCSP()) { |
| 11552 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11553 | return; |
| 11554 | } |
| 11555 | |
| 11556 | SpawnedTestServer::SSLOptions ssl_options( |
| 11557 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11558 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11559 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11560 | ssl_options.ocsp_intermediate_date = |
| 11561 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONGER; |
| 11562 | |
| 11563 | CertStatus cert_status; |
| 11564 | DoConnection(ssl_options, &cert_status); |
| 11565 | |
| 11566 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 11567 | // The builtin verifier enforces the baseline requirements for max age of an |
| 11568 | // intermediate's OCSP response. |
| 11569 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 11570 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11571 | EXPECT_EQ(0u, cert_status & CERT_STATUS_IS_EV); |
| 11572 | #else |
| 11573 | // The platform verifiers are more lenient. |
| 11574 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11575 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11576 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11577 | #endif |
| 11578 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11579 | } |
| 11580 | |
| 11581 | TEST_F(HTTPSOCSPTest, IntermediateRevoked) { |
| 11582 | if (!SystemSupportsOCSP()) { |
| 11583 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11584 | return; |
| 11585 | } |
| 11586 | |
| 11587 | SpawnedTestServer::SSLOptions ssl_options( |
| 11588 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11589 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11590 | ssl_options.ocsp_intermediate_status = |
| 11591 | SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 11592 | |
| 11593 | CertStatus cert_status; |
| 11594 | DoConnection(ssl_options, &cert_status); |
| 11595 | |
| 11596 | #if defined(OS_WIN) |
| 11597 | // TODO(mattm): why does CertVerifyProcWin accept this? |
| 11598 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11599 | #else |
| 11600 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11601 | #endif |
| 11602 | EXPECT_EQ(0u, cert_status & CERT_STATUS_IS_EV); |
| 11603 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11604 | } |
| 11605 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11606 | TEST_F(HTTPSOCSPTest, ValidStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11607 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11608 | LOG(WARNING) |
| 11609 | << "Skipping test because system doesn't support OCSP stapling"; |
| 11610 | return; |
| 11611 | } |
| 11612 | |
| 11613 | SpawnedTestServer::SSLOptions ssl_options( |
| 11614 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11615 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11616 | ssl_options.staple_ocsp_response = true; |
| 11617 | ssl_options.ocsp_server_unavailable = true; |
| 11618 | |
| 11619 | CertStatus cert_status; |
| 11620 | DoConnection(ssl_options, &cert_status); |
| 11621 | |
| 11622 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11623 | |
| 11624 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11625 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11626 | |
| 11627 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11628 | } |
| 11629 | |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 11630 | // Disabled on NSS ports. See https://crbug.com/431716. |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 11631 | #if defined(USE_NSS_CERTS) |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 11632 | #define MAYBE_RevokedStapled DISABLED_RevokedStapled |
| 11633 | #else |
| 11634 | #define MAYBE_RevokedStapled RevokedStapled |
| 11635 | #endif |
| 11636 | TEST_F(HTTPSOCSPTest, MAYBE_RevokedStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11637 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11638 | LOG(WARNING) |
| 11639 | << "Skipping test because system doesn't support OCSP stapling"; |
| 11640 | return; |
| 11641 | } |
| 11642 | |
| 11643 | SpawnedTestServer::SSLOptions ssl_options( |
| 11644 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11645 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 11646 | ssl_options.staple_ocsp_response = true; |
| 11647 | ssl_options.ocsp_server_unavailable = true; |
| 11648 | |
| 11649 | CertStatus cert_status; |
| 11650 | DoConnection(ssl_options, &cert_status); |
| 11651 | |
| 11652 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11653 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11654 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11655 | } |
| 11656 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11657 | static const struct OCSPVerifyTestData { |
| 11658 | std::vector<SpawnedTestServer::SSLOptions::OCSPSingleResponse> ocsp_responses; |
| 11659 | SpawnedTestServer::SSLOptions::OCSPProduced ocsp_produced; |
| 11660 | OCSPVerifyResult::ResponseStatus response_status; |
| 11661 | bool has_revocation_status; |
| 11662 | OCSPRevocationStatus cert_status; |
| 11663 | } kOCSPVerifyData[] = { |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11664 | // 0 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11665 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11666 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11667 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11668 | OCSPVerifyResult::PROVIDED, |
| 11669 | true, |
| 11670 | OCSPRevocationStatus::GOOD}, |
| 11671 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11672 | // 1 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11673 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11674 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 11675 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11676 | OCSPVerifyResult::INVALID_DATE, |
| 11677 | false, |
| 11678 | OCSPRevocationStatus::UNKNOWN}, |
| 11679 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11680 | // 2 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11681 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11682 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11683 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11684 | OCSPVerifyResult::INVALID_DATE, |
| 11685 | false, |
| 11686 | OCSPRevocationStatus::UNKNOWN}, |
| 11687 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11688 | // 3 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11689 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11690 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11691 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11692 | OCSPVerifyResult::INVALID_DATE, |
| 11693 | false, |
| 11694 | OCSPRevocationStatus::UNKNOWN}, |
| 11695 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11696 | // 4 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11697 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11698 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11699 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11700 | OCSPVerifyResult::INVALID_DATE, |
| 11701 | false, |
| 11702 | OCSPRevocationStatus::UNKNOWN}, |
| 11703 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11704 | // 5 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11705 | {{{SpawnedTestServer::SSLOptions::OCSP_TRY_LATER, |
| 11706 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11707 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11708 | OCSPVerifyResult::ERROR_RESPONSE, |
| 11709 | false, |
| 11710 | OCSPRevocationStatus::UNKNOWN}, |
| 11711 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11712 | // 6 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11713 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE, |
| 11714 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11715 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11716 | OCSPVerifyResult::PARSE_RESPONSE_ERROR, |
| 11717 | false, |
| 11718 | OCSPRevocationStatus::UNKNOWN}, |
| 11719 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11720 | // 7 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11721 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE_DATA, |
| 11722 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11723 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11724 | OCSPVerifyResult::PARSE_RESPONSE_DATA_ERROR, |
| 11725 | false, |
| 11726 | OCSPRevocationStatus::UNKNOWN}, |
| 11727 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11728 | // 8 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11729 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11730 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11731 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11732 | OCSPVerifyResult::INVALID_DATE, |
| 11733 | false, |
| 11734 | OCSPRevocationStatus::UNKNOWN}, |
| 11735 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11736 | // 9 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11737 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11738 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11739 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11740 | OCSPVerifyResult::PROVIDED, |
| 11741 | true, |
| 11742 | OCSPRevocationStatus::UNKNOWN}, |
| 11743 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11744 | // 10 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11745 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11746 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 11747 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11748 | OCSPVerifyResult::INVALID_DATE, |
| 11749 | false, |
| 11750 | OCSPRevocationStatus::UNKNOWN}, |
| 11751 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11752 | // 11 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11753 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11754 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11755 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11756 | OCSPVerifyResult::INVALID_DATE, |
| 11757 | false, |
| 11758 | OCSPRevocationStatus::UNKNOWN}, |
| 11759 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11760 | // 12 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11761 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11762 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11763 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_BEFORE_CERT, |
| 11764 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11765 | false, |
| 11766 | OCSPRevocationStatus::UNKNOWN}, |
| 11767 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11768 | // 13 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11769 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11770 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11771 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 11772 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11773 | false, |
| 11774 | OCSPRevocationStatus::UNKNOWN}, |
| 11775 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11776 | // 14 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11777 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11778 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11779 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 11780 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11781 | false, |
| 11782 | OCSPRevocationStatus::UNKNOWN}, |
| 11783 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11784 | // 15 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11785 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11786 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11787 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11788 | OCSPVerifyResult::PROVIDED, |
| 11789 | true, |
| 11790 | OCSPRevocationStatus::GOOD}, |
| 11791 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11792 | // 16 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11793 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11794 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 11795 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11796 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11797 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11798 | OCSPVerifyResult::PROVIDED, |
| 11799 | true, |
| 11800 | OCSPRevocationStatus::GOOD}, |
| 11801 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11802 | // 17 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11803 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11804 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 11805 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11806 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11807 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11808 | OCSPVerifyResult::PROVIDED, |
| 11809 | true, |
| 11810 | OCSPRevocationStatus::GOOD}, |
| 11811 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11812 | // 18 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11813 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11814 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 11815 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11816 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11817 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11818 | OCSPVerifyResult::PROVIDED, |
| 11819 | true, |
| 11820 | OCSPRevocationStatus::GOOD}, |
| 11821 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11822 | // 19 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11823 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11824 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 11825 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11826 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 11827 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11828 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11829 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11830 | OCSPVerifyResult::INVALID_DATE, |
| 11831 | false, |
| 11832 | OCSPRevocationStatus::UNKNOWN}, |
| 11833 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11834 | // 20 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11835 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11836 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11837 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11838 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11839 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11840 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11841 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11842 | OCSPVerifyResult::PROVIDED, |
| 11843 | true, |
| 11844 | OCSPRevocationStatus::REVOKED}, |
| 11845 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11846 | // 21 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11847 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11848 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11849 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11850 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11851 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11852 | OCSPVerifyResult::PROVIDED, |
| 11853 | true, |
| 11854 | OCSPRevocationStatus::UNKNOWN}, |
| 11855 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11856 | // 22 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11857 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11858 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11859 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11860 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 11861 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11862 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11863 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11864 | OCSPVerifyResult::PROVIDED, |
| 11865 | true, |
| 11866 | OCSPRevocationStatus::UNKNOWN}, |
| 11867 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11868 | // 23 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11869 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 11870 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11871 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11872 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 11873 | false, |
| 11874 | OCSPRevocationStatus::UNKNOWN}, |
| 11875 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11876 | // 24 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11877 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 11878 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11879 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11880 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 11881 | false, |
| 11882 | OCSPRevocationStatus::UNKNOWN}, |
| 11883 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11884 | // These tests fail when using NSS for certificate verification, as NSS fails |
| 11885 | // and doesn't return the partial path. As a result the OCSP checks being done |
| 11886 | // at the CertVerifyProc layer cannot access the issuer certificate. |
| 11887 | #if !defined(USE_NSS_CERTS) |
| 11888 | // 25 |
| 11889 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11890 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11891 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11892 | OCSPVerifyResult::PROVIDED, |
| 11893 | true, |
| 11894 | OCSPRevocationStatus::REVOKED}, |
| 11895 | |
| 11896 | // 26 |
| 11897 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11898 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 11899 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11900 | OCSPVerifyResult::INVALID_DATE, |
| 11901 | false, |
| 11902 | OCSPRevocationStatus::UNKNOWN}, |
| 11903 | |
| 11904 | // 27 |
| 11905 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11906 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11907 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11908 | OCSPVerifyResult::INVALID_DATE, |
| 11909 | false, |
| 11910 | OCSPRevocationStatus::UNKNOWN}, |
| 11911 | #endif |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11912 | }; |
| 11913 | |
| 11914 | class HTTPSOCSPVerifyTest |
| 11915 | : public HTTPSOCSPTest, |
| 11916 | public testing::WithParamInterface<OCSPVerifyTestData> {}; |
| 11917 | |
Christos Froussios | 09e6ac4 | 2019-04-05 14:08:20 | [diff] [blame] | 11918 | // TODO(crbug.com/949958): The test is flaky on Mac |
| 11919 | #if defined(OS_MACOSX) |
| 11920 | #define MAYBE_VerifyResult DISABLED_VerifyResult |
| 11921 | #else |
| 11922 | #define MAYBE_VerifyResult VerifyResult |
| 11923 | #endif |
| 11924 | TEST_P(HTTPSOCSPVerifyTest, MAYBE_VerifyResult) { |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11925 | SpawnedTestServer::SSLOptions ssl_options( |
| 11926 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11927 | OCSPVerifyTestData test = GetParam(); |
| 11928 | |
| 11929 | ssl_options.ocsp_responses = test.ocsp_responses; |
| 11930 | ssl_options.ocsp_produced = test.ocsp_produced; |
| 11931 | ssl_options.staple_ocsp_response = true; |
| 11932 | |
| 11933 | SSLInfo ssl_info; |
| 11934 | OCSPErrorTestDelegate delegate; |
| 11935 | ASSERT_NO_FATAL_FAILURE( |
| 11936 | DoConnectionWithDelegate(ssl_options, &delegate, &ssl_info)); |
| 11937 | |
| 11938 | // The SSLInfo must be extracted from |delegate| on error, due to how |
| 11939 | // URLRequest caches certificate errors. |
| 11940 | if (delegate.have_certificate_errors()) { |
| 11941 | ASSERT_TRUE(delegate.on_ssl_certificate_error_called()); |
| 11942 | ssl_info = delegate.ssl_info(); |
| 11943 | } |
| 11944 | |
| 11945 | EXPECT_EQ(test.response_status, ssl_info.ocsp_result.response_status); |
| 11946 | |
| 11947 | if (test.has_revocation_status) |
| 11948 | EXPECT_EQ(test.cert_status, ssl_info.ocsp_result.revocation_status); |
| 11949 | } |
| 11950 | |
Victor Costan | 8fb98f6f | 2019-02-01 17:08:29 | [diff] [blame] | 11951 | INSTANTIATE_TEST_SUITE_P(OCSPVerify, |
| 11952 | HTTPSOCSPVerifyTest, |
| 11953 | testing::ValuesIn(kOCSPVerifyData)); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11954 | |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11955 | class HTTPSAIATest : public HTTPSOCSPTest { |
| 11956 | public: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11957 | CertVerifier::Config GetCertVerifierConfig() override { |
| 11958 | CertVerifier::Config config; |
| 11959 | return config; |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11960 | } |
| 11961 | }; |
| 11962 | |
| 11963 | TEST_F(HTTPSAIATest, AIAFetching) { |
| 11964 | SpawnedTestServer::SSLOptions ssl_options( |
| 11965 | SpawnedTestServer::SSLOptions::CERT_AUTO_AIA_INTERMEDIATE); |
| 11966 | SpawnedTestServer test_server( |
| 11967 | SpawnedTestServer::TYPE_HTTPS, ssl_options, |
| 11968 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 11969 | ASSERT_TRUE(test_server.Start()); |
| 11970 | |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11971 | // Unmark the certificate's OID as EV, which will disable revocation |
| 11972 | // checking. |
| 11973 | ev_test_policy_.reset(); |
| 11974 | |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11975 | TestDelegate d; |
| 11976 | d.set_allow_certificate_errors(true); |
| 11977 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11978 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 11979 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11980 | |
| 11981 | r->Start(); |
| 11982 | EXPECT_TRUE(r->is_pending()); |
| 11983 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11984 | d.RunUntilComplete(); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11985 | |
| 11986 | EXPECT_EQ(1, d.response_started_count()); |
| 11987 | |
| 11988 | CertStatus cert_status = r->ssl_info().cert_status; |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 11989 | EXPECT_EQ(OK, d.request_status()); |
| 11990 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11991 | ASSERT_TRUE(r->ssl_info().cert); |
| 11992 | EXPECT_EQ(2u, r->ssl_info().cert->intermediate_buffers().size()); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11993 | ASSERT_TRUE(r->ssl_info().unverified_cert); |
Matt Mueller | a419327 | 2017-12-07 00:23:34 | [diff] [blame] | 11994 | EXPECT_EQ(0u, r->ssl_info().unverified_cert->intermediate_buffers().size()); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11995 | } |
| 11996 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11997 | class HTTPSHardFailTest : public HTTPSOCSPTest { |
| 11998 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11999 | CertVerifier::Config GetCertVerifierConfig() override { |
| 12000 | CertVerifier::Config config; |
| 12001 | config.require_rev_checking_local_anchors = true; |
| 12002 | return config; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 12003 | } |
| 12004 | }; |
| 12005 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 12006 | TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) { |
| 12007 | if (!SystemSupportsOCSP()) { |
| 12008 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12009 | return; |
| 12010 | } |
| 12011 | |
| 12012 | if (!SystemSupportsHardFailRevocationChecking()) { |
| 12013 | LOG(WARNING) << "Skipping test because system doesn't support hard fail " |
| 12014 | << "revocation checking"; |
| 12015 | return; |
| 12016 | } |
| 12017 | |
| 12018 | SpawnedTestServer::SSLOptions ssl_options( |
| 12019 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12020 | ssl_options.ocsp_status = |
| 12021 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 12022 | |
| 12023 | CertStatus cert_status; |
| 12024 | DoConnection(ssl_options, &cert_status); |
| 12025 | |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 12026 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 12027 | // TODO(crbug.com/649017): Should we consider invalid response as |
| 12028 | // affirmatively revoked? |
| 12029 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 12030 | cert_status & CERT_STATUS_UNABLE_TO_CHECK_REVOCATION); |
| 12031 | #else |
| 12032 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_REVOKED); |
| 12033 | #endif |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 12034 | |
| 12035 | // Without a positive OCSP response, we shouldn't show the EV status. |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 12036 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 12037 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 12038 | } |
| 12039 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12040 | class HTTPSEVCRLSetTest : public HTTPSOCSPTest { |
| 12041 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 12042 | CertVerifier::Config GetCertVerifierConfig() override { |
| 12043 | CertVerifier::Config config; |
| 12044 | return config; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12045 | } |
| 12046 | }; |
| 12047 | |
| 12048 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) { |
| 12049 | if (!SystemSupportsOCSP()) { |
| 12050 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12051 | return; |
| 12052 | } |
| 12053 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 12054 | SpawnedTestServer::SSLOptions ssl_options( |
| 12055 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12056 | ssl_options.ocsp_status = |
| 12057 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12058 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 12059 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 12060 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12061 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 12062 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12063 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 12064 | |
| 12065 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 12066 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 12067 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12068 | } |
| 12069 | |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 12070 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) { |
| 12071 | if (!SystemSupportsOCSP()) { |
| 12072 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12073 | return; |
| 12074 | } |
| 12075 | |
| 12076 | SpawnedTestServer::SSLOptions ssl_options( |
| 12077 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12078 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 12079 | |
| 12080 | CertStatus cert_status; |
| 12081 | DoConnection(ssl_options, &cert_status); |
| 12082 | |
mattm | 1a282f5 | 2016-11-10 21:49:42 | [diff] [blame] | 12083 | // Currently only works for Windows and OS X. When using NSS, it's not |
| 12084 | // possible to determine whether the check failed because of actual |
| 12085 | // revocation or because there was an OCSP failure. |
| 12086 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 12087 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12088 | #else |
| 12089 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12090 | #endif |
| 12091 | |
| 12092 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 12093 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 12094 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 12095 | } |
| 12096 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12097 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) { |
| 12098 | if (!SystemSupportsOCSP()) { |
| 12099 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12100 | return; |
| 12101 | } |
| 12102 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 12103 | SpawnedTestServer::SSLOptions ssl_options( |
| 12104 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12105 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12106 | |
| 12107 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 12108 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12109 | |
| 12110 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12111 | |
| 12112 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 12113 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 12114 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 12115 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12116 | } |
| 12117 | |
| 12118 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) { |
| 12119 | if (!SystemSupportsOCSP()) { |
| 12120 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12121 | return; |
| 12122 | } |
| 12123 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 12124 | SpawnedTestServer::SSLOptions ssl_options( |
| 12125 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12126 | ssl_options.ocsp_status = |
| 12127 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12128 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12129 | cert_verifier_config.crl_set = CRLSet::ExpiredCRLSetForTesting(); |
| 12130 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12131 | |
| 12132 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 12133 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12134 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 12135 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12136 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 12137 | |
| 12138 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 12139 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 12140 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12141 | } |
| 12142 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12143 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) { |
| 12144 | if (!SystemSupportsOCSP()) { |
| 12145 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12146 | return; |
| 12147 | } |
| 12148 | |
| 12149 | SpawnedTestServer::SSLOptions ssl_options( |
| 12150 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12151 | ssl_options.ocsp_status = |
| 12152 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12153 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12154 | cert_verifier_config.crl_set = |
| 12155 | CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "", "", {}); |
| 12156 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12157 | |
| 12158 | CertStatus cert_status; |
| 12159 | DoConnection(ssl_options, &cert_status); |
| 12160 | |
| 12161 | // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a |
| 12162 | // revocation check for EV. |
| 12163 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12164 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 12165 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 12166 | EXPECT_FALSE( |
| 12167 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 12168 | } |
| 12169 | |
| 12170 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) { |
| 12171 | if (!SystemSupportsOCSP()) { |
| 12172 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12173 | return; |
| 12174 | } |
| 12175 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 12176 | SpawnedTestServer::SSLOptions ssl_options( |
| 12177 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12178 | ssl_options.ocsp_status = |
| 12179 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12180 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12181 | cert_verifier_config.crl_set = CRLSet::EmptyCRLSetForTesting(); |
| 12182 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12183 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12184 | CertStatus cert_status = 0; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 12185 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12186 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12187 | // Even with a fresh CRLSet, we should still do online revocation checks when |
| 12188 | // the certificate chain isn't covered by the CRLSet, which it isn't in this |
| 12189 | // test. |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 12190 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12191 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12192 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12193 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12194 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12195 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12196 | } |
| 12197 | |
| 12198 | class HTTPSCRLSetTest : public HTTPSOCSPTest { |
| 12199 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 12200 | CertVerifier::Config GetCertVerifierConfig() override { |
| 12201 | CertVerifier::Config config; |
| 12202 | return config; |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12203 | } |
| 12204 | |
| 12205 | void SetUp() override { |
| 12206 | HTTPSOCSPTest::SetUp(); |
| 12207 | |
| 12208 | // Unmark the certificate's OID as EV, which should disable revocation |
| 12209 | // checking (as per the user preference). |
| 12210 | ev_test_policy_.reset(); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12211 | } |
| 12212 | }; |
| 12213 | |
| 12214 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 12215 | SpawnedTestServer::SSLOptions ssl_options( |
| 12216 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12217 | ssl_options.ocsp_status = |
| 12218 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12219 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12220 | cert_verifier_config.crl_set = CRLSet::ExpiredCRLSetForTesting(); |
| 12221 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12222 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 12223 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 12224 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12225 | |
| 12226 | // If we're not trying EV verification then, even if the CRLSet has expired, |
| 12227 | // we don't fall back to online revocation checks. |
| 12228 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12229 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 12230 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 12231 | } |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12232 | |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12233 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSetAndRevoked) { |
| 12234 | // Test that when online revocation checking is disabled, and the leaf |
| 12235 | // certificate is not EV, that no revocation checking actually happens. |
| 12236 | if (!SystemSupportsOCSP()) { |
| 12237 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12238 | return; |
| 12239 | } |
| 12240 | |
| 12241 | SpawnedTestServer::SSLOptions ssl_options( |
| 12242 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12243 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12244 | |
| 12245 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12246 | cert_verifier_config.crl_set = CRLSet::ExpiredCRLSetForTesting(); |
| 12247 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12248 | |
| 12249 | CertStatus cert_status; |
| 12250 | DoConnection(ssl_options, &cert_status); |
| 12251 | |
| 12252 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12253 | |
| 12254 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 12255 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 12256 | } |
| 12257 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12258 | TEST_F(HTTPSCRLSetTest, CRLSetRevoked) { |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 12259 | #if defined(OS_ANDROID) |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12260 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 12261 | return; |
| 12262 | #endif |
| 12263 | |
| 12264 | SpawnedTestServer::SSLOptions ssl_options( |
| 12265 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12266 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 12267 | ssl_options.cert_serial = 10; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12268 | |
| 12269 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12270 | cert_verifier_config.crl_set = |
| 12271 | CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "\x0a", "", {}); |
| 12272 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12273 | |
| 12274 | CertStatus cert_status = 0; |
| 12275 | DoConnection(ssl_options, &cert_status); |
| 12276 | |
| 12277 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 12278 | // reflected without online revocation checking. |
| 12279 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12280 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12281 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12282 | } |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 12283 | |
| 12284 | TEST_F(HTTPSCRLSetTest, CRLSetRevokedBySubject) { |
| 12285 | #if defined(OS_ANDROID) |
| 12286 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 12287 | return; |
| 12288 | #endif |
| 12289 | |
| 12290 | SpawnedTestServer::SSLOptions ssl_options( |
| 12291 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12292 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 12293 | static const char kCommonName[] = "Test CN"; |
| 12294 | ssl_options.cert_common_name = kCommonName; |
| 12295 | |
| 12296 | { |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12297 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12298 | cert_verifier_config.crl_set = |
| 12299 | CRLSet::ForTesting(false, nullptr, "", kCommonName, {}); |
| 12300 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 12301 | |
| 12302 | CertStatus cert_status = 0; |
| 12303 | DoConnection(ssl_options, &cert_status); |
| 12304 | |
| 12305 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 12306 | // reflected without online revocation checking. |
| 12307 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12308 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12309 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 12310 | } |
| 12311 | |
| 12312 | const uint8_t kTestServerSPKISHA256[32] = { |
| 12313 | 0xb3, 0x91, 0xac, 0x73, 0x32, 0x54, 0x7f, 0x7b, 0x8a, 0x62, 0x77, |
| 12314 | 0x73, 0x1d, 0x45, 0x7b, 0x23, 0x46, 0x69, 0xef, 0x6f, 0x05, 0x3d, |
| 12315 | 0x07, 0x22, 0x15, 0x18, 0xd6, 0x10, 0x8b, 0xa1, 0x49, 0x33, |
| 12316 | }; |
| 12317 | const std::string spki_hash( |
| 12318 | reinterpret_cast<const char*>(kTestServerSPKISHA256), |
| 12319 | sizeof(kTestServerSPKISHA256)); |
| 12320 | |
| 12321 | { |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 12322 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 12323 | cert_verifier_config.crl_set = |
| 12324 | CRLSet::ForTesting(false, nullptr, "", kCommonName, {spki_hash}); |
| 12325 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 12326 | |
| 12327 | CertStatus cert_status = 0; |
| 12328 | DoConnection(ssl_options, &cert_status); |
| 12329 | |
| 12330 | // When the correct SPKI hash is specified, the connection should succeed |
| 12331 | // even though the subject is listed in the CRLSet. |
| 12332 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12333 | } |
| 12334 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 12335 | #endif // !defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 12336 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 12337 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 12338 | !defined(OS_FUCHSIA) |
| 12339 | // FTP uses a second TCP connection with the port number allocated dynamically |
| 12340 | // on the server side, so it would be hard to make RemoteTestServer proxy FTP |
| 12341 | // connections reliably. FTP tests are disabled on platforms that use |
| 12342 | // RemoteTestServer. See http://crbug.com/495220 |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 12343 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12344 | public: |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 12345 | URLRequestTestFTP() |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 12346 | : ftp_test_server_(SpawnedTestServer::TYPE_FTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12347 | base::FilePath(kTestFilePath)) { |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12348 | // Can't use |default_context_|'s HostResolver to set up the |
| 12349 | // FTPTransactionFactory because it hasn't been created yet. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12350 | default_context().set_host_resolver(&host_resolver_); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12351 | } |
| 12352 | |
| 12353 | // URLRequestTest interface: |
| 12354 | void SetUpFactory() override { |
| 12355 | // Add FTP support to the default URLRequestContext. |
| 12356 | job_factory_impl_->SetProtocolHandler( |
Emily Stark | 0122798 | 2019-06-06 18:45:56 | [diff] [blame^] | 12357 | "ftp", FtpProtocolHandler::Create(&host_resolver_, &ftp_auth_cache_)); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12358 | } |
| 12359 | |
| 12360 | std::string GetTestFileContents() { |
| 12361 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 12362 | EXPECT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &path)); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12363 | path = path.Append(kTestFilePath); |
| 12364 | path = path.AppendASCII(kFtpTestFile); |
| 12365 | std::string contents; |
| 12366 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
| 12367 | return contents; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12368 | } |
| 12369 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 12370 | protected: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12371 | MockHostResolver host_resolver_; |
Emily Stark | 0122798 | 2019-06-06 18:45:56 | [diff] [blame^] | 12372 | FtpAuthCache ftp_auth_cache_; |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12373 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12374 | SpawnedTestServer ftp_test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 12375 | }; |
| 12376 | |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12377 | // Make sure an FTP request using an unsafe ports fails. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 12378 | TEST_F(URLRequestTestFTP, UnsafePort) { |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12379 | GURL url("ftp://127.0.0.1:7"); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12380 | |
| 12381 | TestDelegate d; |
| 12382 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12383 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12384 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12385 | r->Start(); |
| 12386 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12387 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12388 | d.RunUntilComplete(); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12389 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12390 | EXPECT_FALSE(r->is_pending()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 12391 | EXPECT_EQ(ERR_UNSAFE_PORT, d.request_status()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12392 | } |
| 12393 | } |
| 12394 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12395 | TEST_F(URLRequestTestFTP, FTPDirectoryListing) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12396 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12397 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12398 | TestDelegate d; |
| 12399 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12400 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12401 | ftp_test_server_.GetURL("/"), DEFAULT_PRIORITY, &d, |
| 12402 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12403 | r->Start(); |
| 12404 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12405 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12406 | d.RunUntilComplete(); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12407 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12408 | EXPECT_FALSE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12409 | EXPECT_EQ(1, d.response_started_count()); |
| 12410 | EXPECT_FALSE(d.received_data_before_response()); |
| 12411 | EXPECT_LT(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12412 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12413 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12414 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12415 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12416 | } |
| 12417 | } |
| 12418 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12419 | TEST_F(URLRequestTestFTP, FTPGetTestAnonymous) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12420 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12421 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12422 | TestDelegate d; |
| 12423 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12424 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12425 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, &d, |
| 12426 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12427 | r->Start(); |
| 12428 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12429 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12430 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12431 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12432 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12433 | EXPECT_EQ(1, d.response_started_count()); |
| 12434 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12435 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12436 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12437 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12438 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12439 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12440 | } |
| 12441 | } |
| 12442 | |
Mike West | 80462a1 | 2018-11-27 16:05:06 | [diff] [blame] | 12443 | TEST_F(URLRequestTestFTP, FTPMimeType) { |
| 12444 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 12445 | |
| 12446 | struct { |
| 12447 | const char* path; |
| 12448 | const char* mime; |
| 12449 | } test_cases[] = { |
| 12450 | {"/", "text/vnd.chromium.ftp-dir"}, |
| 12451 | {kFtpTestFile, "application/octet-stream"}, |
| 12452 | }; |
| 12453 | |
| 12454 | for (const auto test : test_cases) { |
| 12455 | TestDelegate d; |
| 12456 | |
| 12457 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 12458 | ftp_test_server_.GetURL(test.path), DEFAULT_PRIORITY, &d, |
| 12459 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12460 | r->Start(); |
| 12461 | EXPECT_TRUE(r->is_pending()); |
| 12462 | |
| 12463 | d.RunUntilComplete(); |
| 12464 | |
| 12465 | std::string mime; |
| 12466 | r->GetMimeType(&mime); |
| 12467 | EXPECT_EQ(test.mime, mime); |
| 12468 | } |
| 12469 | } |
| 12470 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12471 | TEST_F(URLRequestTestFTP, FTPGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12472 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12473 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12474 | TestDelegate d; |
| 12475 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12476 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12477 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12478 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12479 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12480 | r->Start(); |
| 12481 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12482 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12483 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12484 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12485 | EXPECT_FALSE(r->is_pending()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12486 | EXPECT_EQ(1, d.response_started_count()); |
| 12487 | EXPECT_FALSE(d.received_data_before_response()); |
| 12488 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12489 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12490 | r->GetResponseRemoteEndpoint().ToStringWithoutPort()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12491 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
Tsuyoshi Horo | 01faed6 | 2019-02-20 22:11:37 | [diff] [blame] | 12492 | r->GetResponseRemoteEndpoint().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 12493 | |
| 12494 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12495 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 12496 | TestLoadTimingNoHttpResponse(load_timing_info); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12497 | } |
| 12498 | } |
| 12499 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12500 | TEST_F(URLRequestTestFTP, FTPCheckWrongPassword) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12501 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12502 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12503 | TestDelegate d; |
| 12504 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12505 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12506 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12507 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12508 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12509 | r->Start(); |
| 12510 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12511 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12512 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12513 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12514 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12515 | EXPECT_EQ(1, d.response_started_count()); |
| 12516 | EXPECT_FALSE(d.received_data_before_response()); |
| 12517 | EXPECT_EQ(d.bytes_received(), 0); |
| 12518 | } |
| 12519 | } |
| 12520 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12521 | TEST_F(URLRequestTestFTP, FTPCheckWrongPasswordRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12522 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12523 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12524 | TestDelegate d; |
| 12525 | // Set correct login credentials. The delegate will be asked for them when |
| 12526 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 12527 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12528 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12529 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12530 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12531 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12532 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12533 | r->Start(); |
| 12534 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12535 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12536 | d.RunUntilComplete(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12537 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12538 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12539 | EXPECT_EQ(1, d.response_started_count()); |
| 12540 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12541 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12542 | } |
| 12543 | } |
| 12544 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12545 | TEST_F(URLRequestTestFTP, FTPCheckWrongUser) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12546 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12547 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12548 | TestDelegate d; |
| 12549 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12550 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12551 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 12552 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12553 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12554 | r->Start(); |
| 12555 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12556 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12557 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12558 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12559 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12560 | EXPECT_EQ(1, d.response_started_count()); |
| 12561 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12562 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12563 | } |
| 12564 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12565 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12566 | TEST_F(URLRequestTestFTP, FTPCheckWrongUserRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12567 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12568 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12569 | TestDelegate d; |
| 12570 | // Set correct login credentials. The delegate will be asked for them when |
| 12571 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 12572 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12573 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12574 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12575 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 12576 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12577 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12578 | r->Start(); |
| 12579 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12580 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12581 | d.RunUntilComplete(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12582 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12583 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12584 | EXPECT_EQ(1, d.response_started_count()); |
| 12585 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12586 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12587 | } |
| 12588 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12589 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12590 | TEST_F(URLRequestTestFTP, FTPCacheURLCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12591 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12592 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12593 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12594 | { |
| 12595 | // Pass correct login identity in the URL. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12596 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12597 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12598 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12599 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12600 | r->Start(); |
| 12601 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12602 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12603 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12604 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12605 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12606 | EXPECT_EQ(1, d->response_started_count()); |
| 12607 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12608 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12609 | } |
| 12610 | |
| 12611 | d.reset(new TestDelegate); |
| 12612 | { |
| 12613 | // This request should use cached identity from previous request. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12614 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12615 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 12616 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12617 | r->Start(); |
| 12618 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12619 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12620 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12621 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12622 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12623 | EXPECT_EQ(1, d->response_started_count()); |
| 12624 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12625 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12626 | } |
| 12627 | } |
| 12628 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12629 | TEST_F(URLRequestTestFTP, FTPCacheLoginBoxCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12630 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12631 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12632 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12633 | // Set correct login credentials. The delegate will be asked for them when |
| 12634 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 12635 | d->set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12636 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12637 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12638 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12639 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12640 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12641 | r->Start(); |
| 12642 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12643 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12644 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12645 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12646 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12647 | EXPECT_EQ(1, d->response_started_count()); |
| 12648 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12649 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12650 | } |
| 12651 | |
| 12652 | // Use a new delegate without explicit credentials. The cached ones should be |
| 12653 | // used. |
| 12654 | d.reset(new TestDelegate); |
| 12655 | { |
| 12656 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 12657 | // ones. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12658 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12659 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 12660 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12661 | r->Start(); |
| 12662 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12663 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12664 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12665 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12666 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12667 | EXPECT_EQ(1, d->response_started_count()); |
| 12668 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12669 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12670 | } |
| 12671 | } |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 12672 | |
| 12673 | TEST_F(URLRequestTestFTP, RawBodyBytes) { |
| 12674 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 12675 | |
| 12676 | TestDelegate d; |
| 12677 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12678 | ftp_test_server_.GetURL("simple.html"), DEFAULT_PRIORITY, &d, |
| 12679 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 12680 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12681 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 12682 | |
| 12683 | EXPECT_EQ(6, req->GetRawBodyBytes()); |
| 12684 | } |
| 12685 | |
Matt Menke | f1b97e0 | 2019-05-13 23:52:41 | [diff] [blame] | 12686 | class URLRequestTestFTPOverHttpProxy : public URLRequestTestFTP { |
| 12687 | public: |
| 12688 | // Test interface: |
| 12689 | void SetUp() override { |
| 12690 | proxy_resolution_service_ = ProxyResolutionService::CreateFixed( |
| 12691 | "localhost", TRAFFIC_ANNOTATION_FOR_TESTS); |
| 12692 | default_context_->set_proxy_resolution_service( |
| 12693 | proxy_resolution_service_.get()); |
| 12694 | URLRequestTestFTP::SetUp(); |
| 12695 | } |
| 12696 | |
| 12697 | private: |
| 12698 | std::unique_ptr<ProxyResolutionService> proxy_resolution_service_; |
| 12699 | }; |
| 12700 | |
| 12701 | // Check that FTP is not supported over an HTTP proxy. |
| 12702 | TEST_F(URLRequestTestFTPOverHttpProxy, Fails) { |
| 12703 | TestDelegate delegate; |
| 12704 | std::unique_ptr<URLRequest> request( |
| 12705 | default_context_->CreateRequest(GURL("ftp://foo.test/"), DEFAULT_PRIORITY, |
| 12706 | &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12707 | request->Start(); |
| 12708 | delegate.RunUntilComplete(); |
| 12709 | |
| 12710 | EXPECT_THAT(delegate.request_status(), IsError(ERR_NO_SUPPORTED_PROXIES)); |
| 12711 | } |
| 12712 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 12713 | #endif // !BUILDFLAG(DISABLE_FTP_SUPPORT) |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 12714 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12715 | TEST_F(URLRequestTest, NetworkAccessedClearOnDataRequest) { |
| 12716 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12717 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12718 | GURL("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12719 | |
| 12720 | EXPECT_FALSE(req->response_info().network_accessed); |
| 12721 | |
| 12722 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12723 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12724 | |
| 12725 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 12726 | EXPECT_FALSE(req->response_info().network_accessed); |
| 12727 | } |
| 12728 | |
| 12729 | TEST_F(URLRequestTest, NetworkAccessedSetOnHostResolutionFailure) { |
| 12730 | MockHostResolver host_resolver; |
| 12731 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 12732 | TestURLRequestContext context(true); |
| 12733 | context.set_network_delegate(&network_delegate); |
| 12734 | context.set_host_resolver(&host_resolver); |
| 12735 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 12736 | context.Init(); |
| 12737 | |
| 12738 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12739 | std::unique_ptr<URLRequest> req( |
| 12740 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 12741 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12742 | |
| 12743 | EXPECT_FALSE(req->response_info().network_accessed); |
| 12744 | |
| 12745 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12746 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12747 | EXPECT_TRUE(req->response_info().network_accessed); |
| 12748 | } |
| 12749 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 12750 | // Test that URLRequest is canceled correctly. |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12751 | // See http://crbug.com/508900 |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 12752 | TEST_F(URLRequestTest, URLRequestRedirectJobCancelRequest) { |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12753 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12754 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12755 | GURL("http://not-a-real-domain/"), DEFAULT_PRIORITY, &d, |
| 12756 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12757 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12758 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12759 | req.get(), &default_network_delegate_, |
| 12760 | GURL("http://this-should-never-be-navigated-to/"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 12761 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Jumbo shrimp")); |
| 12762 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12763 | |
| 12764 | req->Start(); |
| 12765 | req->Cancel(); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12766 | base::RunLoop().RunUntilIdle(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 12767 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12768 | EXPECT_EQ(0, d.received_redirect_count()); |
| 12769 | } |
| 12770 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12771 | TEST_F(URLRequestTestHTTP, HeadersCallbacks) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12772 | ASSERT_TRUE(http_test_server()->Start()); |
| 12773 | TestURLRequestContext context; |
| 12774 | GURL url(http_test_server()->GetURL("/cachetime")); |
| 12775 | TestDelegate delegate; |
| 12776 | HttpRequestHeaders extra_headers; |
| 12777 | extra_headers.SetHeader("X-Foo", "bar"); |
| 12778 | |
| 12779 | { |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12780 | HttpRawRequestHeaders raw_req_headers; |
| 12781 | scoped_refptr<const HttpResponseHeaders> raw_resp_headers; |
| 12782 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12783 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12784 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12785 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12786 | r->SetRequestHeadersCallback(base::Bind( |
| 12787 | &HttpRawRequestHeaders::Assign, base::Unretained(&raw_req_headers))); |
| 12788 | r->SetResponseHeadersCallback(base::Bind( |
| 12789 | [](scoped_refptr<const HttpResponseHeaders>* left, |
| 12790 | scoped_refptr<const HttpResponseHeaders> right) { *left = right; }, |
| 12791 | base::Unretained(&raw_resp_headers))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12792 | r->Start(); |
| 12793 | while (!delegate.response_started_count()) |
| 12794 | base::RunLoop().RunUntilIdle(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12795 | EXPECT_FALSE(raw_req_headers.headers().empty()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12796 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12797 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12798 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12799 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12800 | EXPECT_EQ("gzip, deflate", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12801 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Connection", &value)); |
| 12802 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Host", &value)); |
| 12803 | EXPECT_EQ("GET /cachetime HTTP/1.1\r\n", raw_req_headers.request_line()); |
| 12804 | EXPECT_EQ(raw_resp_headers.get(), r->response_headers()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12805 | } |
| 12806 | { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12807 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12808 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12809 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12810 | r->SetRequestHeadersCallback(base::Bind([](HttpRawRequestHeaders) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12811 | FAIL() << "Callback should not be called unless request is sent"; |
| 12812 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12813 | r->SetResponseHeadersCallback( |
| 12814 | base::Bind([](scoped_refptr<const HttpResponseHeaders>) { |
| 12815 | FAIL() << "Callback should not be called unless request is sent"; |
| 12816 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12817 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12818 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12819 | EXPECT_TRUE(r->was_cached()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12820 | } |
| 12821 | } |
| 12822 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12823 | TEST_F(URLRequestTestHTTP, HeadersCallbacksWithRedirect) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12824 | ASSERT_TRUE(http_test_server()->Start()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12825 | HttpRawRequestHeaders raw_req_headers; |
| 12826 | scoped_refptr<const HttpResponseHeaders> raw_resp_headers; |
| 12827 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12828 | TestURLRequestContext context; |
| 12829 | TestDelegate delegate; |
| 12830 | HttpRequestHeaders extra_headers; |
| 12831 | extra_headers.SetHeader("X-Foo", "bar"); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12832 | GURL url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12833 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12834 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12835 | r->SetExtraRequestHeaders(extra_headers); |
| 12836 | r->SetRequestHeadersCallback(base::Bind(&HttpRawRequestHeaders::Assign, |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12837 | base::Unretained(&raw_req_headers))); |
| 12838 | r->SetResponseHeadersCallback(base::Bind( |
| 12839 | [](scoped_refptr<const HttpResponseHeaders>* left, |
| 12840 | scoped_refptr<const HttpResponseHeaders> right) { *left = right; }, |
| 12841 | base::Unretained(&raw_resp_headers))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12842 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 12843 | delegate.RunUntilRedirect(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12844 | |
| 12845 | ASSERT_EQ(1, delegate.received_redirect_count()); |
| 12846 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12847 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12848 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12849 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12850 | EXPECT_EQ("gzip, deflate", value); |
| 12851 | EXPECT_EQ(1, delegate.received_redirect_count()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12852 | EXPECT_EQ("GET /redirect-test.html HTTP/1.1\r\n", |
| 12853 | raw_req_headers.request_line()); |
| 12854 | EXPECT_TRUE(raw_resp_headers->HasHeader("Location")); |
| 12855 | EXPECT_EQ(302, raw_resp_headers->response_code()); |
| 12856 | EXPECT_EQ("Redirect", raw_resp_headers->GetStatusText()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12857 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12858 | raw_req_headers = HttpRawRequestHeaders(); |
| 12859 | raw_resp_headers = nullptr; |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame] | 12860 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 12861 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12862 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12863 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12864 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12865 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12866 | EXPECT_EQ("gzip, deflate", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12867 | EXPECT_EQ("GET /with-headers.html HTTP/1.1\r\n", |
| 12868 | raw_req_headers.request_line()); |
| 12869 | EXPECT_EQ(r->response_headers(), raw_resp_headers.get()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12870 | } |
| 12871 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12872 | TEST_F(URLRequestTest, HeadersCallbacksConnectFailed) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12873 | TestDelegate request_delegate; |
| 12874 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12875 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12876 | GURL("http://127.0.0.1:9/"), DEFAULT_PRIORITY, &request_delegate, |
| 12877 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12878 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
| 12879 | FAIL() << "Callback should not be called unless request is sent"; |
| 12880 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12881 | r->SetResponseHeadersCallback( |
| 12882 | base::Bind([](scoped_refptr<const net::HttpResponseHeaders>) { |
| 12883 | FAIL() << "Callback should not be called unless request is sent"; |
| 12884 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12885 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12886 | request_delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12887 | EXPECT_FALSE(r->is_pending()); |
| 12888 | } |
| 12889 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12890 | TEST_F(URLRequestTestHTTP, HeadersCallbacksAuthRetry) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12891 | ASSERT_TRUE(http_test_server()->Start()); |
| 12892 | GURL url(http_test_server()->GetURL("/auth-basic")); |
| 12893 | |
| 12894 | TestURLRequestContext context; |
| 12895 | TestDelegate delegate; |
| 12896 | |
| 12897 | delegate.set_credentials(AuthCredentials(kUser, kSecret)); |
| 12898 | HttpRequestHeaders extra_headers; |
| 12899 | extra_headers.SetHeader("X-Foo", "bar"); |
| 12900 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12901 | using ReqHeadersVector = std::vector<std::unique_ptr<HttpRawRequestHeaders>>; |
| 12902 | ReqHeadersVector raw_req_headers; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12903 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12904 | using RespHeadersVector = |
| 12905 | std::vector<scoped_refptr<const HttpResponseHeaders>>; |
| 12906 | RespHeadersVector raw_resp_headers; |
| 12907 | |
| 12908 | auto req_headers_callback = base::Bind( |
| 12909 | [](ReqHeadersVector* vec, HttpRawRequestHeaders headers) { |
| 12910 | vec->emplace_back(new HttpRawRequestHeaders(std::move(headers))); |
| 12911 | }, |
| 12912 | &raw_req_headers); |
| 12913 | auto resp_headers_callback = base::Bind( |
| 12914 | [](RespHeadersVector* vec, |
| 12915 | scoped_refptr<const HttpResponseHeaders> headers) { |
| 12916 | vec->push_back(headers); |
| 12917 | }, |
| 12918 | &raw_resp_headers); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12919 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12920 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12921 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12922 | r->SetRequestHeadersCallback(req_headers_callback); |
| 12923 | r->SetResponseHeadersCallback(resp_headers_callback); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12924 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12925 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12926 | EXPECT_FALSE(r->is_pending()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12927 | ASSERT_EQ(raw_req_headers.size(), 2u); |
| 12928 | ASSERT_EQ(raw_resp_headers.size(), 2u); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12929 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12930 | EXPECT_FALSE(raw_req_headers[0]->FindHeaderForTest("Authorization", &value)); |
| 12931 | EXPECT_TRUE(raw_req_headers[0]->FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12932 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12933 | EXPECT_TRUE(raw_req_headers[1]->FindHeaderForTest("Authorization", &value)); |
| 12934 | EXPECT_TRUE(raw_req_headers[1]->FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12935 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12936 | EXPECT_EQ(raw_resp_headers[1], r->response_headers()); |
| 12937 | EXPECT_NE(raw_resp_headers[0], raw_resp_headers[1]); |
| 12938 | EXPECT_EQ(401, raw_resp_headers[0]->response_code()); |
| 12939 | EXPECT_EQ("Unauthorized", raw_resp_headers[0]->GetStatusText()); |
| 12940 | |
| 12941 | std::unique_ptr<URLRequest> r2(context.CreateRequest( |
| 12942 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12943 | r2->SetExtraRequestHeaders(extra_headers); |
| 12944 | r2->SetRequestHeadersCallback(req_headers_callback); |
| 12945 | r2->SetResponseHeadersCallback(resp_headers_callback); |
| 12946 | r2->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 12947 | r2->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12948 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12949 | EXPECT_FALSE(r2->is_pending()); |
| 12950 | ASSERT_EQ(raw_req_headers.size(), 3u); |
| 12951 | ASSERT_EQ(raw_resp_headers.size(), 3u); |
| 12952 | EXPECT_TRUE(raw_req_headers[2]->FindHeaderForTest("If-None-Match", &value)); |
| 12953 | EXPECT_NE(raw_resp_headers[2].get(), r2->response_headers()); |
| 12954 | EXPECT_EQ(304, raw_resp_headers[2]->response_code()); |
| 12955 | EXPECT_EQ("Not Modified", raw_resp_headers[2]->GetStatusText()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12956 | } |
| 12957 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12958 | TEST_F(URLRequestTest, HeadersCallbacksNonHTTP) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12959 | GURL data_url("data:text/html,<html><body>Hello!</body></html>"); |
| 12960 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12961 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12962 | data_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12963 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12964 | FAIL() << "Callback should not be called for non-HTTP schemes"; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12965 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12966 | r->SetResponseHeadersCallback( |
| 12967 | base::Bind([](scoped_refptr<const net::HttpResponseHeaders>) { |
| 12968 | FAIL() << "Callback should not be called for non-HTTP schemes"; |
| 12969 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12970 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12971 | d.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12972 | EXPECT_FALSE(r->is_pending()); |
| 12973 | } |
| 12974 | |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12975 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSet) { |
| 12976 | TestDelegate d; |
| 12977 | BlockingNetworkDelegate network_delegate( |
| 12978 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 12979 | const GURL kOriginalUrl("https://original.test"); |
| 12980 | const GURL kRedirectUrl("http://redirect.test"); |
| 12981 | network_delegate.set_redirect_url(kRedirectUrl); |
| 12982 | TestURLRequestContext context(true /* delay_initialization */); |
| 12983 | context.set_network_delegate(&network_delegate); |
| 12984 | context.Init(); |
| 12985 | |
| 12986 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12987 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12988 | r->set_upgrade_if_insecure(true); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12989 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 12990 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12991 | GURL::Replacements replacements; |
| 12992 | // Check that the redirect URL was upgraded to HTTPS since upgrade_if_insecure |
| 12993 | // was set. |
| 12994 | replacements.SetSchemeStr("https"); |
| 12995 | EXPECT_EQ(kRedirectUrl.ReplaceComponents(replacements), |
| 12996 | d.redirect_info().new_url); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 12997 | EXPECT_TRUE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12998 | } |
| 12999 | |
| 13000 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSetExplicitPort80) { |
| 13001 | TestDelegate d; |
| 13002 | BlockingNetworkDelegate network_delegate( |
| 13003 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 13004 | const GURL kOriginalUrl("https://original.test"); |
| 13005 | const GURL kRedirectUrl("http://redirect.test:80"); |
| 13006 | network_delegate.set_redirect_url(kRedirectUrl); |
| 13007 | TestURLRequestContext context(true /* delay_initialization */); |
| 13008 | context.set_network_delegate(&network_delegate); |
| 13009 | context.Init(); |
| 13010 | |
| 13011 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 13012 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13013 | r->set_upgrade_if_insecure(true); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13014 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 13015 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13016 | GURL::Replacements replacements; |
| 13017 | // The URL host should have not been changed. |
| 13018 | EXPECT_EQ(d.redirect_info().new_url.host(), kRedirectUrl.host()); |
| 13019 | // The scheme should now be https, and the effective port should now be 443. |
| 13020 | EXPECT_TRUE(d.redirect_info().new_url.SchemeIs("https")); |
| 13021 | EXPECT_EQ(d.redirect_info().new_url.EffectiveIntPort(), 443); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 13022 | EXPECT_TRUE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13023 | } |
| 13024 | |
| 13025 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSetNonStandardPort) { |
| 13026 | TestDelegate d; |
| 13027 | BlockingNetworkDelegate network_delegate( |
| 13028 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 13029 | const GURL kOriginalUrl("https://original.test"); |
| 13030 | const GURL kRedirectUrl("http://redirect.test:1234"); |
| 13031 | network_delegate.set_redirect_url(kRedirectUrl); |
| 13032 | TestURLRequestContext context(true /* delay_initialization */); |
| 13033 | context.set_network_delegate(&network_delegate); |
| 13034 | context.Init(); |
| 13035 | |
| 13036 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 13037 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13038 | r->set_upgrade_if_insecure(true); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13039 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 13040 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13041 | GURL::Replacements replacements; |
| 13042 | // Check that the redirect URL was upgraded to HTTPS since upgrade_if_insecure |
| 13043 | // was set, nonstandard port should not have been modified. |
| 13044 | replacements.SetSchemeStr("https"); |
| 13045 | EXPECT_EQ(kRedirectUrl.ReplaceComponents(replacements), |
| 13046 | d.redirect_info().new_url); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 13047 | EXPECT_TRUE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13048 | } |
| 13049 | |
| 13050 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagNotSet) { |
| 13051 | TestDelegate d; |
| 13052 | BlockingNetworkDelegate network_delegate( |
| 13053 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 13054 | const GURL kOriginalUrl("https://original.test"); |
| 13055 | const GURL kRedirectUrl("http://redirect.test"); |
| 13056 | network_delegate.set_redirect_url(kRedirectUrl); |
| 13057 | TestURLRequestContext context(true /* delay_initialization */); |
| 13058 | context.set_network_delegate(&network_delegate); |
| 13059 | context.Init(); |
| 13060 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 13061 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13062 | r->set_upgrade_if_insecure(false); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13063 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 13064 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13065 | // The redirect URL should not be changed if the upgrade_if_insecure flag is |
| 13066 | // not set. |
| 13067 | EXPECT_EQ(kRedirectUrl, d.redirect_info().new_url); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 13068 | EXPECT_FALSE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 13069 | } |
| 13070 | |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 13071 | // Test that URLRequests get properly tagged. |
| 13072 | #if defined(OS_ANDROID) |
| 13073 | TEST_F(URLRequestTestHTTP, TestTagging) { |
Peter Collingbourne | 8ccab72 | 2019-02-12 18:10:48 | [diff] [blame] | 13074 | if (!CanGetTaggedBytes()) { |
| 13075 | DVLOG(0) << "Skipping test - GetTaggedBytes unsupported."; |
| 13076 | return; |
| 13077 | } |
| 13078 | |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 13079 | ASSERT_TRUE(http_test_server()->Start()); |
| 13080 | |
| 13081 | // The tag under which the system reports untagged traffic. |
| 13082 | static const int32_t UNTAGGED_TAG = 0; |
| 13083 | |
| 13084 | uint64_t old_traffic = GetTaggedBytes(UNTAGGED_TAG); |
| 13085 | |
| 13086 | // Untagged traffic should be tagged with tag UNTAGGED_TAG. |
| 13087 | TestDelegate delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 13088 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 13089 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &delegate, |
| 13090 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13091 | EXPECT_EQ(SocketTag(), req->socket_tag()); |
| 13092 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 13093 | delegate.RunUntilComplete(); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 13094 | |
| 13095 | EXPECT_GT(GetTaggedBytes(UNTAGGED_TAG), old_traffic); |
| 13096 | |
| 13097 | int32_t tag_val1 = 0x12345678; |
| 13098 | SocketTag tag1(SocketTag::UNSET_UID, tag_val1); |
| 13099 | old_traffic = GetTaggedBytes(tag_val1); |
| 13100 | |
| 13101 | // Test specific tag value. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 13102 | req = default_context().CreateRequest(http_test_server()->GetURL("/"), |
| 13103 | DEFAULT_PRIORITY, &delegate, |
| 13104 | TRAFFIC_ANNOTATION_FOR_TESTS); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 13105 | req->set_socket_tag(tag1); |
| 13106 | EXPECT_EQ(tag1, req->socket_tag()); |
| 13107 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 13108 | delegate.RunUntilComplete(); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 13109 | |
| 13110 | EXPECT_GT(GetTaggedBytes(tag_val1), old_traffic); |
| 13111 | } |
| 13112 | #endif |
| 13113 | |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 13114 | // Provides a response to the 0RTT request indicating whether it was received |
| 13115 | // as early data, sending HTTP_TOO_EARLY if enabled. |
| 13116 | class ZeroRTTResponse : public test_server::BasicHttpResponse { |
| 13117 | public: |
| 13118 | ZeroRTTResponse(bool zero_rtt, bool send_too_early) |
| 13119 | : zero_rtt_(zero_rtt), send_too_early_(send_too_early) {} |
| 13120 | ~ZeroRTTResponse() override {} |
| 13121 | |
| 13122 | void SendResponse(const test_server::SendBytesCallback& send, |
| 13123 | const test_server::SendCompleteCallback& done) override { |
| 13124 | AddCustomHeader("Vary", "Early-Data"); |
| 13125 | set_content_type("text/plain"); |
| 13126 | AddCustomHeader("Cache-Control", "no-cache"); |
| 13127 | if (zero_rtt_) { |
| 13128 | if (send_too_early_) |
| 13129 | set_code(HTTP_TOO_EARLY); |
| 13130 | set_content("1"); |
| 13131 | } else { |
| 13132 | set_content("0"); |
| 13133 | } |
| 13134 | |
| 13135 | // Since the EmbeddedTestServer doesn't keep the socket open by default, |
| 13136 | // it is explicitly kept alive to allow the remaining leg of the 0RTT |
| 13137 | // handshake to be received after the early data. |
| 13138 | send.Run(ToResponseString(), base::DoNothing()); |
| 13139 | } |
| 13140 | |
| 13141 | private: |
| 13142 | bool zero_rtt_; |
| 13143 | bool send_too_early_; |
| 13144 | |
| 13145 | DISALLOW_COPY_AND_ASSIGN(ZeroRTTResponse); |
| 13146 | }; |
| 13147 | |
| 13148 | std::unique_ptr<test_server::HttpResponse> HandleZeroRTTRequest( |
| 13149 | const test_server::HttpRequest& request) { |
| 13150 | if (request.GetURL().path() != "/zerortt") |
| 13151 | return nullptr; |
| 13152 | auto iter = request.headers.find("Early-Data"); |
| 13153 | bool zero_rtt = iter != request.headers.end() && iter->second == "1"; |
| 13154 | return std::make_unique<ZeroRTTResponse>(zero_rtt, false); |
| 13155 | } |
| 13156 | |
| 13157 | class HTTPSEarlyDataTest : public TestWithScopedTaskEnvironment { |
| 13158 | public: |
| 13159 | HTTPSEarlyDataTest() |
| 13160 | : context_(true), test_server_(net::EmbeddedTestServer::TYPE_HTTPS) { |
| 13161 | auto params = std::make_unique<HttpNetworkSession::Params>(); |
| 13162 | params->enable_early_data = true; |
| 13163 | context_.set_http_network_session_params(std::move(params)); |
| 13164 | |
| 13165 | context_.set_network_delegate(&network_delegate_); |
| 13166 | cert_verifier_.set_default_result(OK); |
| 13167 | context_.set_cert_verifier(&cert_verifier_); |
| 13168 | |
| 13169 | ssl_config_service_ = std::make_unique<TestSSLConfigService>(); |
| 13170 | ssl_config_service_->set_max_version(SSL_PROTOCOL_VERSION_TLS1_3); |
| 13171 | context_.set_ssl_config_service(ssl_config_service_.get()); |
| 13172 | |
| 13173 | context_.Init(); |
| 13174 | |
| 13175 | ssl_config_.version_max = SSL_PROTOCOL_VERSION_TLS1_3; |
| 13176 | ssl_config_.early_data_enabled = true; |
| 13177 | test_server_.SetSSLConfig(net::EmbeddedTestServer::CERT_OK, ssl_config_); |
| 13178 | test_server_.AddDefaultHandlers( |
| 13179 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 13180 | test_server_.RegisterRequestHandler( |
| 13181 | base::BindRepeating(&HandleZeroRTTRequest)); |
| 13182 | } |
| 13183 | |
| 13184 | ~HTTPSEarlyDataTest() override = default; |
| 13185 | |
| 13186 | void ResetSSLConfig(net::EmbeddedTestServer::ServerCertificate cert, |
| 13187 | uint16_t version) { |
| 13188 | ssl_config_.version_max = version; |
| 13189 | test_server_.ResetSSLConfig(cert, ssl_config_); |
| 13190 | } |
| 13191 | |
| 13192 | protected: |
| 13193 | MockCertVerifier cert_verifier_; |
| 13194 | TestNetworkDelegate network_delegate_; // Must outlive URLRequest. |
| 13195 | std::unique_ptr<TestSSLConfigService> ssl_config_service_; |
| 13196 | TestURLRequestContext context_; |
| 13197 | |
| 13198 | SSLServerConfig ssl_config_; |
| 13199 | EmbeddedTestServer test_server_; |
| 13200 | }; |
| 13201 | |
| 13202 | // TLSEarlyDataTest tests that we handle early data correctly. |
| 13203 | TEST_F(HTTPSEarlyDataTest, TLSEarlyDataTest) { |
| 13204 | ASSERT_TRUE(test_server_.Start()); |
| 13205 | context_.http_transaction_factory()->GetSession()->ClearSSLSessionCache(); |
| 13206 | |
| 13207 | { |
| 13208 | TestDelegate d; |
| 13209 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13210 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13211 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13212 | r->Start(); |
| 13213 | EXPECT_TRUE(r->is_pending()); |
| 13214 | |
| 13215 | base::RunLoop().Run(); |
| 13216 | |
| 13217 | EXPECT_EQ(1, d.response_started_count()); |
| 13218 | |
| 13219 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13220 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13221 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13222 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13223 | r->ssl_info().cert.get())); |
| 13224 | |
| 13225 | // The Early-Data header should be omitted in the initial request, and the |
| 13226 | // handler should return "0". |
| 13227 | EXPECT_EQ("0", d.data_received()); |
| 13228 | } |
| 13229 | |
| 13230 | context_.http_transaction_factory()->GetSession()->CloseAllConnections(); |
| 13231 | |
| 13232 | { |
| 13233 | TestDelegate d; |
| 13234 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13235 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13236 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13237 | |
| 13238 | r->Start(); |
| 13239 | EXPECT_TRUE(r->is_pending()); |
| 13240 | |
| 13241 | base::RunLoop().Run(); |
| 13242 | |
| 13243 | EXPECT_EQ(1, d.response_started_count()); |
| 13244 | |
| 13245 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13246 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13247 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13248 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13249 | r->ssl_info().cert.get())); |
| 13250 | |
| 13251 | // The Early-Data header should be a single '1' in the resumed request, and |
| 13252 | // the handler should return "1". |
| 13253 | EXPECT_EQ("1", d.data_received()); |
| 13254 | } |
| 13255 | } |
| 13256 | |
| 13257 | // TLSEarlyDataTest tests that we handle early data correctly for POST. |
| 13258 | TEST_F(HTTPSEarlyDataTest, TLSEarlyDataPOSTTest) { |
| 13259 | ASSERT_TRUE(test_server_.Start()); |
| 13260 | context_.http_transaction_factory()->GetSession()->ClearSSLSessionCache(); |
| 13261 | |
| 13262 | { |
| 13263 | TestDelegate d; |
| 13264 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13265 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13266 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13267 | r->Start(); |
| 13268 | EXPECT_TRUE(r->is_pending()); |
| 13269 | |
| 13270 | base::RunLoop().Run(); |
| 13271 | |
| 13272 | EXPECT_EQ(1, d.response_started_count()); |
| 13273 | |
| 13274 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13275 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13276 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13277 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13278 | r->ssl_info().cert.get())); |
| 13279 | |
| 13280 | // The Early-Data header should be omitted in the initial request, and the |
| 13281 | // handler should return "0". |
| 13282 | EXPECT_EQ("0", d.data_received()); |
| 13283 | } |
| 13284 | |
| 13285 | context_.http_transaction_factory()->GetSession()->CloseAllConnections(); |
| 13286 | |
| 13287 | { |
| 13288 | TestDelegate d; |
| 13289 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13290 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13291 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13292 | r->set_method("POST"); |
| 13293 | r->Start(); |
| 13294 | EXPECT_TRUE(r->is_pending()); |
| 13295 | |
| 13296 | base::RunLoop().Run(); |
| 13297 | |
| 13298 | EXPECT_EQ(1, d.response_started_count()); |
| 13299 | |
| 13300 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13301 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13302 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13303 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13304 | r->ssl_info().cert.get())); |
| 13305 | |
| 13306 | // The Early-Data header should be omitted in the request, since we don't |
| 13307 | // send POSTs over early data, and the handler should return "0". |
| 13308 | EXPECT_EQ("0", d.data_received()); |
| 13309 | } |
| 13310 | } |
| 13311 | |
| 13312 | std::unique_ptr<test_server::HttpResponse> HandleTooEarly( |
| 13313 | bool* sent_425, |
| 13314 | const test_server::HttpRequest& request) { |
| 13315 | if (request.GetURL().path() != "/tooearly") |
| 13316 | return nullptr; |
| 13317 | auto iter = request.headers.find("Early-Data"); |
| 13318 | bool zero_rtt = iter != request.headers.end() && iter->second == "1"; |
| 13319 | if (zero_rtt) |
| 13320 | *sent_425 = true; |
| 13321 | return std::make_unique<ZeroRTTResponse>(zero_rtt, true); |
| 13322 | } |
| 13323 | |
| 13324 | // Test that we handle 425 (Too Early) correctly. |
| 13325 | TEST_F(HTTPSEarlyDataTest, TLSEarlyDataTooEarlyTest) { |
| 13326 | bool sent_425 = false; |
| 13327 | test_server_.RegisterRequestHandler( |
| 13328 | base::BindRepeating(&HandleTooEarly, base::Unretained(&sent_425))); |
| 13329 | ASSERT_TRUE(test_server_.Start()); |
| 13330 | context_.http_transaction_factory()->GetSession()->ClearSSLSessionCache(); |
| 13331 | |
| 13332 | { |
| 13333 | TestDelegate d; |
| 13334 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13335 | test_server_.GetURL("/tooearly"), DEFAULT_PRIORITY, &d, |
| 13336 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13337 | r->Start(); |
| 13338 | EXPECT_TRUE(r->is_pending()); |
| 13339 | |
| 13340 | d.RunUntilComplete(); |
| 13341 | |
| 13342 | EXPECT_EQ(1, d.response_started_count()); |
| 13343 | |
| 13344 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13345 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13346 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13347 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13348 | r->ssl_info().cert.get())); |
| 13349 | |
| 13350 | // The Early-Data header should be omitted in the initial request, and the |
| 13351 | // handler should return "0". |
| 13352 | EXPECT_EQ("0", d.data_received()); |
| 13353 | EXPECT_FALSE(sent_425); |
| 13354 | } |
| 13355 | |
| 13356 | context_.http_transaction_factory()->GetSession()->CloseAllConnections(); |
| 13357 | |
| 13358 | { |
| 13359 | TestDelegate d; |
| 13360 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13361 | test_server_.GetURL("/tooearly"), DEFAULT_PRIORITY, &d, |
| 13362 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13363 | |
| 13364 | r->Start(); |
| 13365 | EXPECT_TRUE(r->is_pending()); |
| 13366 | |
| 13367 | d.RunUntilComplete(); |
| 13368 | |
| 13369 | EXPECT_EQ(1, d.response_started_count()); |
| 13370 | |
| 13371 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13372 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13373 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13374 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13375 | r->ssl_info().cert.get())); |
| 13376 | |
| 13377 | // The resumption request will encounter a 425 error and retry without early |
| 13378 | // data, and the handler should return "0". |
| 13379 | EXPECT_EQ("0", d.data_received()); |
| 13380 | EXPECT_TRUE(sent_425); |
| 13381 | } |
| 13382 | } |
| 13383 | |
| 13384 | // TLSEarlyDataRejectTest tests that we gracefully handle an early data reject |
| 13385 | // and retry without early data. |
| 13386 | TEST_F(HTTPSEarlyDataTest, TLSEarlyDataRejectTest) { |
| 13387 | ASSERT_TRUE(test_server_.Start()); |
| 13388 | context_.http_transaction_factory()->GetSession()->ClearSSLSessionCache(); |
| 13389 | |
| 13390 | { |
| 13391 | TestDelegate d; |
| 13392 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13393 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13394 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13395 | |
| 13396 | r->Start(); |
| 13397 | EXPECT_TRUE(r->is_pending()); |
| 13398 | |
| 13399 | d.RunUntilComplete(); |
| 13400 | |
| 13401 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13402 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13403 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13404 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13405 | r->ssl_info().cert.get())); |
| 13406 | |
| 13407 | // The Early-Data header should be omitted in the initial request, and the |
| 13408 | // handler should return "0". |
| 13409 | EXPECT_EQ("0", d.data_received()); |
| 13410 | } |
| 13411 | |
| 13412 | context_.http_transaction_factory()->GetSession()->CloseAllConnections(); |
| 13413 | |
| 13414 | // The certificate in the resumption is changed to confirm that the |
| 13415 | // certificate change is observed. |
| 13416 | scoped_refptr<X509Certificate> old_cert = test_server_.GetCertificate(); |
| 13417 | ResetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED, |
| 13418 | SSL_PROTOCOL_VERSION_TLS1_3); |
| 13419 | |
| 13420 | { |
| 13421 | TestDelegate d; |
| 13422 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13423 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13424 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13425 | |
| 13426 | r->Start(); |
| 13427 | EXPECT_TRUE(r->is_pending()); |
| 13428 | |
| 13429 | d.RunUntilComplete(); |
| 13430 | |
| 13431 | EXPECT_EQ(1, d.response_started_count()); |
| 13432 | |
| 13433 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13434 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13435 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13436 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13437 | r->ssl_info().cert.get())); |
| 13438 | EXPECT_FALSE(old_cert->EqualsIncludingChain(r->ssl_info().cert.get())); |
| 13439 | |
| 13440 | // The Early-Data header should be omitted in the rejected request, and the |
| 13441 | // handler should return "0". |
| 13442 | EXPECT_EQ("0", d.data_received()); |
| 13443 | } |
| 13444 | } |
| 13445 | |
| 13446 | // TLSEarlyDataTLS12RejectTest tests that we gracefully handle an early data |
| 13447 | // reject from a TLS 1.2 server and retry without early data. |
| 13448 | TEST_F(HTTPSEarlyDataTest, TLSEarlyDataTLS12RejectTest) { |
| 13449 | ASSERT_TRUE(test_server_.Start()); |
| 13450 | context_.http_transaction_factory()->GetSession()->ClearSSLSessionCache(); |
| 13451 | |
| 13452 | { |
| 13453 | TestDelegate d; |
| 13454 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13455 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13456 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13457 | |
| 13458 | r->Start(); |
| 13459 | EXPECT_TRUE(r->is_pending()); |
| 13460 | |
| 13461 | d.RunUntilComplete(); |
| 13462 | |
| 13463 | EXPECT_EQ(1, d.response_started_count()); |
| 13464 | |
| 13465 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_3, |
| 13466 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13467 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13468 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13469 | r->ssl_info().cert.get())); |
| 13470 | |
| 13471 | // The Early-Data header should be omitted in the initial request, and the |
| 13472 | // handler should return "0". |
| 13473 | EXPECT_EQ("0", d.data_received()); |
| 13474 | } |
| 13475 | |
| 13476 | context_.http_transaction_factory()->GetSession()->CloseAllConnections(); |
| 13477 | |
| 13478 | // The certificate in the resumption is changed to confirm that the |
| 13479 | // certificate change is observed. |
| 13480 | scoped_refptr<X509Certificate> old_cert = test_server_.GetCertificate(); |
| 13481 | ResetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED, |
| 13482 | SSL_PROTOCOL_VERSION_TLS1_2); |
| 13483 | |
| 13484 | { |
| 13485 | TestDelegate d; |
| 13486 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
| 13487 | test_server_.GetURL("/zerortt"), DEFAULT_PRIORITY, &d, |
| 13488 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13489 | |
| 13490 | r->Start(); |
| 13491 | EXPECT_TRUE(r->is_pending()); |
| 13492 | |
| 13493 | d.RunUntilComplete(); |
| 13494 | |
| 13495 | EXPECT_EQ(1, d.response_started_count()); |
| 13496 | |
| 13497 | EXPECT_EQ(SSL_CONNECTION_VERSION_TLS1_2, |
| 13498 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 13499 | EXPECT_TRUE(r->ssl_info().unverified_cert.get()); |
| 13500 | EXPECT_TRUE(test_server_.GetCertificate()->EqualsIncludingChain( |
| 13501 | r->ssl_info().cert.get())); |
| 13502 | EXPECT_FALSE(old_cert->EqualsIncludingChain(r->ssl_info().cert.get())); |
| 13503 | |
| 13504 | // The Early-Data header should be omitted in the rejected request, and the |
| 13505 | // handler should return "0". |
| 13506 | EXPECT_EQ("0", d.data_received()); |
| 13507 | } |
| 13508 | } |
| 13509 | |
Emily Stark | 08f6c97 | 2019-05-28 17:24:27 | [diff] [blame] | 13510 | // Tests that AuthChallengeInfo is available on the request. |
| 13511 | TEST_F(URLRequestTestHTTP, AuthChallengeInfo) { |
| 13512 | ASSERT_TRUE(http_test_server()->Start()); |
| 13513 | GURL url(http_test_server()->GetURL("/auth-basic")); |
| 13514 | |
| 13515 | TestURLRequestContext context; |
| 13516 | TestDelegate delegate; |
| 13517 | |
| 13518 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 13519 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 13520 | r->Start(); |
| 13521 | delegate.RunUntilComplete(); |
| 13522 | ASSERT_TRUE(r->auth_challenge_info().has_value()); |
| 13523 | EXPECT_FALSE(r->auth_challenge_info()->is_proxy); |
| 13524 | EXPECT_EQ(url::Origin::Create(url), r->auth_challenge_info()->challenger); |
| 13525 | EXPECT_EQ("basic", r->auth_challenge_info()->scheme); |
| 13526 | EXPECT_EQ("testrealm", r->auth_challenge_info()->realm); |
| 13527 | EXPECT_EQ("Basic realm=\"testrealm\"", r->auth_challenge_info()->challenge); |
| 13528 | EXPECT_EQ("/auth-basic", r->auth_challenge_info()->path); |
| 13529 | } |
| 13530 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 13531 | } // namespace net |