[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 |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9 | #include "build/build_config.h" |
| 10 | |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 11 | #if defined(OS_WIN) |
robliao | eb9bfd64 | 2017-05-18 17:35:16 | [diff] [blame] | 12 | #include <objbase.h> |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 13 | #include <shlobj.h> |
Fan Yang | 5a88d5d | 2017-10-24 01:14:29 | [diff] [blame] | 14 | #include <windows.h> |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 15 | #include <wrl/client.h> |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 16 | #endif |
| 17 | |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 18 | #include <stdint.h> |
| 19 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 20 | #include <algorithm> |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 21 | #include <limits> |
Devlin Cronin | e4bcb40e | 2018-06-05 18:02:47 | [diff] [blame] | 22 | #include <memory> |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 23 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 24 | #include "base/base64url.h" |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 25 | #include "base/bind.h" |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 26 | #include "base/compiler_specific.h" |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 27 | #include "base/files/file_path.h" |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame] | 28 | #include "base/files/file_util.h" |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 29 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 30 | #include "base/format_macros.h" |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 31 | #include "base/json/json_reader.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 32 | #include "base/location.h" |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 33 | #include "base/memory/weak_ptr.h" |
Alexander Timin | 4f9c35c | 2018-11-01 20:15:20 | [diff] [blame] | 34 | #include "base/message_loop/message_loop.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 35 | #include "base/path_service.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 36 | #include "base/power_monitor/power_monitor.h" |
| 37 | #include "base/power_monitor/power_monitor_source.h" |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 38 | #include "base/run_loop.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 39 | #include "base/single_thread_task_runner.h" |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 40 | #include "base/stl_util.h" |
[email protected] | 4dc3ad4f | 2013-06-11 07:15:50 | [diff] [blame] | 41 | #include "base/strings/string_number_conversions.h" |
[email protected] | d069c11a | 2013-04-13 00:01:55 | [diff] [blame] | 42 | #include "base/strings/string_piece.h" |
[email protected] | d778e042 | 2013-03-06 18:10:22 | [diff] [blame] | 43 | #include "base/strings/string_split.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 44 | #include "base/strings/string_util.h" |
| 45 | #include "base/strings/stringprintf.h" |
[email protected] | 750b2f3c | 2013-06-07 18:41:05 | [diff] [blame] | 46 | #include "base/strings/utf_string_conversions.h" |
Devlin Cronin | e4bcb40e | 2018-06-05 18:02:47 | [diff] [blame] | 47 | #include "base/test/metrics/histogram_tester.h" |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 48 | #include "base/test/scoped_feature_list.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 49 | #include "base/threading/thread_task_runner_handle.h" |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 50 | #include "base/values.h" |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 51 | #include "build/buildflag.h" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 52 | #include "net/base/chunked_upload_data_stream.h" |
tfarina | 43a416b | 2016-01-06 21:48:07 | [diff] [blame] | 53 | #include "net/base/directory_listing.h" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 54 | #include "net/base/elements_upload_data_stream.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 55 | #include "net/base/load_flags.h" |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 56 | #include "net/base/load_timing_info.h" |
| 57 | #include "net/base/load_timing_info_test_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 58 | #include "net/base/net_errors.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 59 | #include "net/base/net_module.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 60 | #include "net/base/proxy_server.h" |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 61 | #include "net/base/request_priority.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 62 | #include "net/base/test_completion_callback.h" |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 63 | #include "net/base/upload_bytes_element_reader.h" |
| 64 | #include "net/base/upload_data_stream.h" |
| 65 | #include "net/base/upload_file_element_reader.h" |
tfarina | 7a4a7fd | 2016-01-20 14:23:44 | [diff] [blame] | 66 | #include "net/base/url_util.h" |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 67 | #include "net/cert/crl_set.h" |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 68 | #include "net/cert/ct_policy_enforcer.h" |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 69 | #include "net/cert/ct_policy_status.h" |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 70 | #include "net/cert/do_nothing_ct_verifier.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 71 | #include "net/cert/ev_root_ca_metadata.h" |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 72 | #include "net/cert/mock_cert_verifier.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 73 | #include "net/cert/multi_log_ct_verifier.h" |
eranm | dcec963 | 2016-10-10 14:16:10 | [diff] [blame] | 74 | #include "net/cert/signed_certificate_timestamp_and_status.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 75 | #include "net/cert/test_root_certs.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 76 | #include "net/cookies/cookie_monster.h" |
| 77 | #include "net/cookies/cookie_store_test_helpers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 78 | #include "net/disk_cache/disk_cache.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 79 | #include "net/dns/mock_host_resolver.h" |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 80 | #include "net/http/http_byte_range.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 81 | #include "net/http/http_cache.h" |
| 82 | #include "net/http/http_network_layer.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 83 | #include "net/http/http_network_session.h" |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 84 | #include "net/http/http_request_headers.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 85 | #include "net/http/http_response_headers.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 86 | #include "net/http/http_server_properties_impl.h" |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 87 | #include "net/http/http_util.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 88 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 89 | #include "net/log/net_log_source.h" |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 90 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 91 | #include "net/log/test_net_log_entry.h" |
| 92 | #include "net/log/test_net_log_util.h" |
Scott Violet | 0caaaf43 | 2018-03-24 00:43:59 | [diff] [blame] | 93 | #include "net/net_buildflags.h" |
Lily Houghton | ffe89daa0 | 2018-03-09 18:30:03 | [diff] [blame] | 94 | #include "net/proxy_resolution/proxy_resolution_service.h" |
Ryan Hamilton | a3ee93a7 | 2018-08-01 22:03:08 | [diff] [blame] | 95 | #include "net/quic/mock_crypto_client_stream_factory.h" |
| 96 | #include "net/quic/quic_server_info.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 97 | #include "net/socket/socket_test_util.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 98 | #include "net/socket/ssl_client_socket.h" |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 99 | #include "net/ssl/channel_id_service.h" |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 100 | #include "net/ssl/client_cert_identity_test_util.h" |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 101 | #include "net/ssl/default_channel_id_store.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 102 | #include "net/ssl/ssl_connection_status_flags.h" |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 103 | #include "net/ssl/ssl_private_key.h" |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 104 | #include "net/ssl/ssl_server_config.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 105 | #include "net/test/cert_test_util.h" |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 106 | #include "net/test/embedded_test_server/embedded_test_server.h" |
| 107 | #include "net/test/embedded_test_server/http_request.h" |
| 108 | #include "net/test/embedded_test_server/http_response.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 109 | #include "net/test/gtest_util.h" |
[email protected] | 89b3252 | 2013-05-07 20:04:21 | [diff] [blame] | 110 | #include "net/test/spawned_test_server/spawned_test_server.h" |
rsleevi | a69c79a | 2016-06-22 03:28:43 | [diff] [blame] | 111 | #include "net/test/test_data_directory.h" |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 112 | #include "net/test/test_with_scoped_task_environment.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 113 | #include "net/test/url_request/url_request_failed_job.h" |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 114 | #include "net/test/url_request/url_request_mock_http_job.h" |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 115 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 116 | #include "net/url_request/data_protocol_handler.h" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 117 | #include "net/url_request/static_http_user_agent_settings.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 118 | #include "net/url_request/url_request.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 119 | #include "net/url_request/url_request_filter.h" |
[email protected] | bcb84f8b | 2009-08-31 16:20:14 | [diff] [blame] | 120 | #include "net/url_request/url_request_http_job.h" |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 121 | #include "net/url_request/url_request_http_job_histogram.h" |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 122 | #include "net/url_request/url_request_intercepting_job_factory.h" |
| 123 | #include "net/url_request/url_request_interceptor.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 124 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 125 | #include "net/url_request/url_request_redirect_job.h" |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 126 | #include "net/url_request/url_request_test_job.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 127 | #include "net/url_request/url_request_test_util.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 128 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 129 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 130 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 131 | |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 132 | #if defined(OS_FUCHSIA) |
| 133 | #define USE_BUILTIN_CERT_VERIFIER |
| 134 | #endif |
| 135 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 136 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 137 | #include "net/base/filename_util.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 138 | #include "net/url_request/file_protocol_handler.h" |
| 139 | #include "net/url_request/url_request_file_dir_job.h" |
| 140 | #endif |
| 141 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 142 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 143 | #include "net/ftp/ftp_network_layer.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 144 | #include "net/url_request/ftp_protocol_handler.h" |
| 145 | #endif |
| 146 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 147 | #if defined(OS_WIN) |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 148 | #include "base/win/scoped_com_initializer.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 149 | #endif |
| 150 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 151 | #if BUILDFLAG(ENABLE_REPORTING) |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 152 | #include "net/network_error_logging/network_error_logging_service.h" |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 153 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 154 | |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 155 | #if defined(OS_ANDROID) || defined(USE_BUILTIN_CERT_VERIFIER) |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 156 | #include "net/cert/cert_net_fetcher.h" |
| 157 | #include "net/cert_net/cert_net_fetcher_impl.h" |
| 158 | #endif |
| 159 | |
| 160 | #if defined(USE_NSS_CERTS) |
| 161 | #include "net/cert_net/nss_ocsp.h" |
| 162 | #endif |
| 163 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 164 | using net::test::IsError; |
| 165 | using net::test::IsOk; |
| 166 | |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 167 | using base::ASCIIToUTF16; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 168 | using base::Time; |
halton.huo | e4e4574 | 2014-12-08 07:55:46 | [diff] [blame] | 169 | using std::string; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 170 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 171 | namespace net { |
| 172 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 173 | namespace { |
| 174 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 175 | namespace test_default { |
| 176 | #include "net/http/transport_security_state_static_unittest_default.h" |
xunjieli | 815ad5b | 2017-07-18 15:51:35 | [diff] [blame] | 177 | } |
| 178 | |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 179 | const base::string16 kChrome(ASCIIToUTF16("chrome")); |
| 180 | const base::string16 kSecret(ASCIIToUTF16("secret")); |
| 181 | const base::string16 kUser(ASCIIToUTF16("user")); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 182 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 183 | const base::FilePath::CharType kTestFilePath[] = |
| 184 | FILE_PATH_LITERAL("net/data/url_request_unittest"); |
| 185 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 186 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 187 | !defined(OS_FUCHSIA) |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 188 | // Test file used in most FTP tests. |
| 189 | const char kFtpTestFile[] = "BullRunSpeech.txt"; |
| 190 | #endif |
| 191 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 192 | // Tests load timing information in the case a fresh connection was used, with |
| 193 | // no proxy. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 194 | void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info, |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 195 | int connect_timing_flags) { |
| 196 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 197 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 198 | |
| 199 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 200 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 201 | |
| 202 | EXPECT_LE(load_timing_info.request_start, |
| 203 | load_timing_info.connect_timing.connect_start); |
| 204 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 205 | connect_timing_flags); |
| 206 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 207 | load_timing_info.send_start); |
| 208 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 209 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_start); |
| 210 | EXPECT_LE(load_timing_info.receive_headers_start, |
| 211 | load_timing_info.receive_headers_end); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 212 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 213 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 214 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 215 | } |
| 216 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 217 | // Same as above, but with proxy times. |
| 218 | void TestLoadTimingNotReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 219 | const LoadTimingInfo& load_timing_info, |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 220 | int connect_timing_flags) { |
| 221 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 222 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 223 | |
| 224 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 225 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 226 | |
| 227 | EXPECT_LE(load_timing_info.request_start, |
| 228 | load_timing_info.proxy_resolve_start); |
| 229 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 230 | load_timing_info.proxy_resolve_end); |
| 231 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 232 | load_timing_info.connect_timing.connect_start); |
| 233 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 234 | connect_timing_flags); |
| 235 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 236 | load_timing_info.send_start); |
| 237 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 238 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_start); |
| 239 | EXPECT_LE(load_timing_info.receive_headers_start, |
| 240 | load_timing_info.receive_headers_end); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | // Same as above, but with a reused socket and proxy times. |
| 244 | void TestLoadTimingReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 245 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 246 | EXPECT_TRUE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 247 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 248 | |
| 249 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 250 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 251 | |
| 252 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 253 | |
| 254 | EXPECT_LE(load_timing_info.request_start, |
| 255 | load_timing_info.proxy_resolve_start); |
| 256 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 257 | load_timing_info.proxy_resolve_end); |
| 258 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 259 | load_timing_info.send_start); |
| 260 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 261 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_start); |
| 262 | EXPECT_LE(load_timing_info.receive_headers_start, |
| 263 | load_timing_info.receive_headers_end); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 264 | } |
| 265 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 266 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 267 | // Tests load timing information in the case of a cache hit, when no cache |
| 268 | // validation request was sent over the wire. |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 269 | base::StringPiece TestNetResourceProvider(int key) { |
| 270 | return "header"; |
| 271 | } |
| 272 | |
| 273 | void FillBuffer(char* buffer, size_t len) { |
| 274 | static bool called = false; |
| 275 | if (!called) { |
| 276 | called = true; |
| 277 | int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 278 | srand(seed); |
| 279 | } |
| 280 | |
| 281 | for (size_t i = 0; i < len; i++) { |
| 282 | buffer[i] = static_cast<char>(rand()); |
| 283 | if (!buffer[i]) |
| 284 | buffer[i] = 'g'; |
| 285 | } |
| 286 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 287 | #endif |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 288 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 289 | void TestLoadTimingCacheHitNoNetwork( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 290 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 291 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 292 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 293 | |
| 294 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 295 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 296 | |
| 297 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 298 | EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start); |
| 299 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 300 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_start); |
| 301 | EXPECT_LE(load_timing_info.receive_headers_start, |
| 302 | load_timing_info.receive_headers_end); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 303 | |
| 304 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 305 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 306 | } |
| 307 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 308 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 309 | !defined(OS_FUCHSIA) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 310 | // Tests load timing in the case that there is no HTTP response. This can be |
| 311 | // used to test in the case of errors or non-HTTP requests. |
| 312 | void TestLoadTimingNoHttpResponse( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 313 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 314 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 315 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 316 | |
| 317 | // Only the request times should be non-null. |
| 318 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 319 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 320 | |
| 321 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 322 | |
| 323 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 324 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 325 | EXPECT_TRUE(load_timing_info.send_start.is_null()); |
| 326 | EXPECT_TRUE(load_timing_info.send_end.is_null()); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 327 | EXPECT_TRUE(load_timing_info.receive_headers_start.is_null()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 328 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 329 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 330 | #endif |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 331 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 332 | // Test power monitor source that can simulate entering suspend mode. Can't use |
| 333 | // the one in base/ because it insists on bringing its own MessageLoop. |
| 334 | class TestPowerMonitorSource : public base::PowerMonitorSource { |
| 335 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 336 | TestPowerMonitorSource() = default; |
| 337 | ~TestPowerMonitorSource() override = default; |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 338 | |
Colin Blundell | c46597f | 2018-06-14 16:43:10 | [diff] [blame] | 339 | void Shutdown() override {} |
| 340 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 341 | void Suspend() { ProcessPowerEvent(SUSPEND_EVENT); } |
| 342 | |
| 343 | void Resume() { ProcessPowerEvent(RESUME_EVENT); } |
| 344 | |
| 345 | bool IsOnBatteryPowerImpl() override { return false; } |
| 346 | |
| 347 | private: |
| 348 | DISALLOW_COPY_AND_ASSIGN(TestPowerMonitorSource); |
| 349 | }; |
| 350 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 351 | // Job that allows monitoring of its priority. |
| 352 | class PriorityMonitoringURLRequestJob : public URLRequestTestJob { |
| 353 | public: |
| 354 | // The latest priority of the job is always written to |request_priority_|. |
| 355 | PriorityMonitoringURLRequestJob(URLRequest* request, |
| 356 | NetworkDelegate* network_delegate, |
| 357 | RequestPriority* request_priority) |
| 358 | : URLRequestTestJob(request, network_delegate), |
| 359 | request_priority_(request_priority) { |
| 360 | *request_priority_ = DEFAULT_PRIORITY; |
| 361 | } |
| 362 | |
| 363 | void SetPriority(RequestPriority priority) override { |
| 364 | *request_priority_ = priority; |
| 365 | URLRequestTestJob::SetPriority(priority); |
| 366 | } |
| 367 | |
| 368 | private: |
| 369 | RequestPriority* const request_priority_; |
| 370 | }; |
| 371 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 372 | // Do a case-insensitive search through |haystack| for |needle|. |
| 373 | bool ContainsString(const std::string& haystack, const char* needle) { |
brettw | a2027fb | 2015-07-14 02:24:50 | [diff] [blame] | 374 | std::string::const_iterator it = std::search( |
| 375 | haystack.begin(), haystack.end(), needle, needle + strlen(needle), |
| 376 | base::CaseInsensitiveCompareASCII<char>()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 377 | return it != haystack.end(); |
| 378 | } |
| 379 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 380 | std::unique_ptr<UploadDataStream> CreateSimpleUploadData(const char* data) { |
| 381 | std::unique_ptr<UploadElementReader> reader( |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 382 | new UploadBytesElementReader(data, strlen(data))); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 383 | return ElementsUploadDataStream::CreateWithReader(std::move(reader), 0); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 384 | } |
| 385 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 386 | // Verify that the SSLInfo of a successful SSL connection has valid values. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 387 | void CheckSSLInfo(const SSLInfo& ssl_info) { |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 388 | // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated. |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 389 | uint16_t cipher_suite = |
| 390 | SSLConnectionStatusToCipherSuite(ssl_info.connection_status); |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 391 | EXPECT_NE(0U, cipher_suite); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 392 | } |
| 393 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 394 | void CheckFullRequestHeaders(const HttpRequestHeaders& headers, |
| 395 | const GURL& host_url) { |
| 396 | std::string sent_value; |
| 397 | |
| 398 | EXPECT_TRUE(headers.GetHeader("Host", &sent_value)); |
| 399 | EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value); |
| 400 | |
| 401 | EXPECT_TRUE(headers.GetHeader("Connection", &sent_value)); |
| 402 | EXPECT_EQ("keep-alive", sent_value); |
| 403 | } |
| 404 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 405 | // A network delegate that allows the user to choose a subset of request stages |
| 406 | // to block in. When blocking, the delegate can do one of the following: |
| 407 | // * synchronously return a pre-specified error code, or |
| 408 | // * asynchronously return that value via an automatically called callback, |
| 409 | // or |
| 410 | // * block and wait for the user to do a callback. |
| 411 | // Additionally, the user may also specify a redirect URL -- then each request |
| 412 | // with the current URL different from the redirect target will be redirected |
| 413 | // to that target, in the on-before-URL-request stage, independent of whether |
| 414 | // the delegate blocks in ON_BEFORE_URL_REQUEST or not. |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 415 | class BlockingNetworkDelegate : public TestNetworkDelegate { |
| 416 | public: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 417 | // Stages in which the delegate can block. |
| 418 | enum Stage { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 419 | NOT_BLOCKED = 0, |
| 420 | ON_BEFORE_URL_REQUEST = 1 << 0, |
| 421 | ON_BEFORE_SEND_HEADERS = 1 << 1, |
| 422 | ON_HEADERS_RECEIVED = 1 << 2, |
| 423 | ON_AUTH_REQUIRED = 1 << 3 |
| 424 | }; |
| 425 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 426 | // Behavior during blocked stages. During other stages, just |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 427 | // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 428 | enum BlockMode { |
| 429 | SYNCHRONOUS, // No callback, returns specified return values. |
| 430 | AUTO_CALLBACK, // |this| posts a task to run the callback using the |
| 431 | // specified return codes. |
| 432 | USER_CALLBACK, // User takes care of doing a callback. |retval_| and |
| 433 | // |auth_retval_| are ignored. In every blocking stage the |
| 434 | // message loop is quit. |
| 435 | }; |
| 436 | |
| 437 | // Creates a delegate which does not block at all. |
| 438 | explicit BlockingNetworkDelegate(BlockMode block_mode); |
| 439 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 440 | // Runs the message loop until the delegate blocks. |
| 441 | void RunUntilBlocked(); |
| 442 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 443 | // For users to trigger a callback returning |response|. |
| 444 | // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks. |
| 445 | // Only call if |block_mode_| == USER_CALLBACK. |
| 446 | void DoCallback(int response); |
| 447 | void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response); |
| 448 | |
| 449 | // Setters. |
| 450 | void set_retval(int retval) { |
| 451 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 452 | ASSERT_NE(ERR_IO_PENDING, retval); |
| 453 | ASSERT_NE(OK, retval); |
| 454 | retval_ = retval; |
[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 | // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then |
| 458 | // |auth_credentials_| will be passed with the response. |
| 459 | void set_auth_retval(AuthRequiredResponse auth_retval) { |
| 460 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 461 | ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval); |
| 462 | auth_retval_ = auth_retval; |
| 463 | } |
| 464 | void set_auth_credentials(const AuthCredentials& auth_credentials) { |
| 465 | auth_credentials_ = auth_credentials; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 466 | } |
| 467 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 468 | void set_redirect_url(const GURL& url) { |
| 469 | redirect_url_ = url; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 470 | } |
| 471 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 472 | void set_block_on(int block_on) { |
| 473 | block_on_ = block_on; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 474 | } |
| 475 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 476 | // Allows the user to check in which state did we block. |
| 477 | Stage stage_blocked_for_callback() const { |
| 478 | EXPECT_EQ(USER_CALLBACK, block_mode_); |
| 479 | return stage_blocked_for_callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | private: |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 483 | void OnBlocked(); |
| 484 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 485 | void RunCallback(int response, CompletionOnceCallback callback); |
| 486 | void RunAuthCallback(AuthRequiredResponse response, AuthCallback callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 487 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 488 | // TestNetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 489 | int OnBeforeURLRequest(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 490 | CompletionOnceCallback callback, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 491 | GURL* new_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 492 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 493 | int OnBeforeStartTransaction(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 494 | CompletionOnceCallback callback, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 495 | HttpRequestHeaders* headers) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 496 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 497 | int OnHeadersReceived( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 498 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 499 | CompletionOnceCallback callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 500 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 501 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 502 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 503 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 504 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 505 | URLRequest* request, |
| 506 | const AuthChallengeInfo& auth_info, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 507 | AuthCallback callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 508 | AuthCredentials* credentials) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 509 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 510 | // Resets the callbacks and |stage_blocked_for_callback_|. |
| 511 | void Reset(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 512 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 513 | // Checks whether we should block in |stage|. If yes, returns an error code |
| 514 | // and optionally sets up callback based on |block_mode_|. If no, returns OK. |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 515 | int MaybeBlockStage(Stage stage, CompletionOnceCallback callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 516 | |
| 517 | // Configuration parameters, can be adjusted by public methods: |
| 518 | const BlockMode block_mode_; |
| 519 | |
| 520 | // Values returned on blocking stages when mode is SYNCHRONOUS or |
| 521 | // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING. |
| 522 | int retval_; // To be returned in non-auth stages. |
| 523 | AuthRequiredResponse auth_retval_; |
| 524 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 525 | GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 526 | int block_on_; // Bit mask: in which stages to block. |
| 527 | |
| 528 | // |auth_credentials_| will be copied to |*target_auth_credential_| on |
| 529 | // callback. |
| 530 | AuthCredentials auth_credentials_; |
| 531 | AuthCredentials* target_auth_credentials_; |
| 532 | |
| 533 | // Internal variables, not set by not the user: |
| 534 | // Last blocked stage waiting for user callback (unused if |block_mode_| != |
| 535 | // USER_CALLBACK). |
| 536 | Stage stage_blocked_for_callback_; |
| 537 | |
| 538 | // Callback objects stored during blocking stages. |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 539 | CompletionOnceCallback callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 540 | AuthCallback auth_callback_; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 541 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 542 | // Closure to run to exit RunUntilBlocked(). |
| 543 | base::OnceClosure on_blocked_; |
| 544 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 545 | base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; |
| 546 | |
| 547 | DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 548 | }; |
| 549 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 550 | BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode) |
| 551 | : block_mode_(block_mode), |
| 552 | retval_(OK), |
| 553 | auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION), |
| 554 | block_on_(0), |
| 555 | target_auth_credentials_(NULL), |
| 556 | stage_blocked_for_callback_(NOT_BLOCKED), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 557 | weak_factory_(this) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 558 | } |
| 559 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 560 | void BlockingNetworkDelegate::RunUntilBlocked() { |
| 561 | base::RunLoop run_loop; |
| 562 | on_blocked_ = run_loop.QuitClosure(); |
| 563 | run_loop.Run(); |
| 564 | } |
| 565 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 566 | void BlockingNetworkDelegate::DoCallback(int response) { |
| 567 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 568 | ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 569 | ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 570 | CompletionOnceCallback callback = std::move(callback_); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 571 | Reset(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 572 | |
| 573 | // |callback| may trigger completion of a request, so post it as a task, so |
| 574 | // it will run under a subsequent TestDelegate::RunUntilComplete() loop. |
| 575 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 576 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::RunCallback, |
| 577 | weak_factory_.GetWeakPtr(), response, |
| 578 | std::move(callback))); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | void BlockingNetworkDelegate::DoAuthCallback( |
| 582 | NetworkDelegate::AuthRequiredResponse response) { |
| 583 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 584 | ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 585 | AuthCallback auth_callback = std::move(auth_callback_); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 586 | Reset(); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 587 | RunAuthCallback(response, std::move(auth_callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 588 | } |
| 589 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 590 | void BlockingNetworkDelegate::OnBlocked() { |
| 591 | // If this fails due to |on_blocked_| being null then OnBlocked() was run by |
| 592 | // a RunLoop other than RunUntilBlocked(), indicating a bug in the calling |
| 593 | // test. |
| 594 | std::move(on_blocked_).Run(); |
| 595 | } |
| 596 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 597 | void BlockingNetworkDelegate::RunCallback(int response, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 598 | CompletionOnceCallback callback) { |
| 599 | std::move(callback).Run(response); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 603 | AuthCallback callback) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 604 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) { |
| 605 | ASSERT_TRUE(target_auth_credentials_ != NULL); |
| 606 | *target_auth_credentials_ = auth_credentials_; |
| 607 | } |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 608 | std::move(callback).Run(response); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 609 | } |
| 610 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 611 | int BlockingNetworkDelegate::OnBeforeURLRequest(URLRequest* request, |
| 612 | CompletionOnceCallback callback, |
| 613 | GURL* new_url) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 614 | if (redirect_url_ == request->url()) |
| 615 | return OK; // We've already seen this request and redirected elsewhere. |
| 616 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 617 | // TestNetworkDelegate always completes synchronously. |
| 618 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeURLRequest( |
| 619 | request, base::NullCallback(), new_url)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 620 | |
| 621 | if (!redirect_url_.is_empty()) |
| 622 | *new_url = redirect_url_; |
| 623 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 624 | return MaybeBlockStage(ON_BEFORE_URL_REQUEST, std::move(callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 625 | } |
| 626 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 627 | int BlockingNetworkDelegate::OnBeforeStartTransaction( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 628 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 629 | CompletionOnceCallback callback, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 630 | HttpRequestHeaders* headers) { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 631 | // TestNetworkDelegate always completes synchronously. |
| 632 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeStartTransaction( |
| 633 | request, base::NullCallback(), headers)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 634 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 635 | return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, std::move(callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | int BlockingNetworkDelegate::OnHeadersReceived( |
| 639 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 640 | CompletionOnceCallback callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 641 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 642 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
| 643 | GURL* allowed_unsafe_redirect_url) { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 644 | // TestNetworkDelegate always completes synchronously. |
| 645 | CHECK_NE(ERR_IO_PENDING, |
| 646 | TestNetworkDelegate::OnHeadersReceived( |
| 647 | request, base::NullCallback(), original_response_headers, |
| 648 | override_response_headers, allowed_unsafe_redirect_url)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 649 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 650 | return MaybeBlockStage(ON_HEADERS_RECEIVED, std::move(callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired( |
| 654 | URLRequest* request, |
| 655 | const AuthChallengeInfo& auth_info, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 656 | AuthCallback callback, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 657 | AuthCredentials* credentials) { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 658 | // TestNetworkDelegate always completes synchronously. |
| 659 | CHECK_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, |
| 660 | TestNetworkDelegate::OnAuthRequired( |
| 661 | request, auth_info, base::NullCallback(), credentials)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 662 | // Check that the user has provided callback for the previous blocked stage. |
| 663 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 664 | |
| 665 | if ((block_on_ & ON_AUTH_REQUIRED) == 0) { |
| 666 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 667 | } |
| 668 | |
| 669 | target_auth_credentials_ = credentials; |
| 670 | |
| 671 | switch (block_mode_) { |
| 672 | case SYNCHRONOUS: |
| 673 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) |
| 674 | *target_auth_credentials_ = auth_credentials_; |
| 675 | return auth_retval_; |
| 676 | |
| 677 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 678 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 679 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::RunAuthCallback, |
| 680 | weak_factory_.GetWeakPtr(), auth_retval_, |
| 681 | std::move(callback))); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 682 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 683 | |
| 684 | case USER_CALLBACK: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 685 | auth_callback_ = std::move(callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 686 | stage_blocked_for_callback_ = ON_AUTH_REQUIRED; |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 687 | // We may reach here via a callback prior to RunUntilBlocked(), so post |
| 688 | // a task to fetch and run the |on_blocked_| closure. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 689 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 690 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::OnBlocked, |
| 691 | weak_factory_.GetWeakPtr())); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 692 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 693 | } |
| 694 | NOTREACHED(); |
| 695 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value. |
| 696 | } |
| 697 | |
| 698 | void BlockingNetworkDelegate::Reset() { |
| 699 | EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 700 | stage_blocked_for_callback_ = NOT_BLOCKED; |
| 701 | callback_.Reset(); |
| 702 | auth_callback_.Reset(); |
| 703 | } |
| 704 | |
| 705 | int BlockingNetworkDelegate::MaybeBlockStage( |
| 706 | BlockingNetworkDelegate::Stage stage, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 707 | CompletionOnceCallback callback) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 708 | // Check that the user has provided callback for the previous blocked stage. |
| 709 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 710 | |
| 711 | if ((block_on_ & stage) == 0) { |
| 712 | return OK; |
| 713 | } |
| 714 | |
| 715 | switch (block_mode_) { |
| 716 | case SYNCHRONOUS: |
| 717 | EXPECT_NE(OK, retval_); |
| 718 | return retval_; |
| 719 | |
| 720 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 721 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 722 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::RunCallback, |
| 723 | weak_factory_.GetWeakPtr(), retval_, |
| 724 | std::move(callback))); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 725 | return ERR_IO_PENDING; |
| 726 | |
| 727 | case USER_CALLBACK: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 728 | callback_ = std::move(callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 729 | stage_blocked_for_callback_ = stage; |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 730 | // We may reach here via a callback prior to RunUntilBlocked(), so post |
| 731 | // a task to fetch and run the |on_blocked_| closure. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 732 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 733 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::OnBlocked, |
| 734 | weak_factory_.GetWeakPtr())); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 735 | return ERR_IO_PENDING; |
| 736 | } |
| 737 | NOTREACHED(); |
| 738 | return 0; |
| 739 | } |
| 740 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 741 | class TestURLRequestContextWithProxy : public TestURLRequestContext { |
| 742 | public: |
| 743 | // Does not own |delegate|. |
| 744 | TestURLRequestContextWithProxy(const std::string& proxy, |
| 745 | NetworkDelegate* delegate) |
| 746 | : TestURLRequestContext(true) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 747 | context_storage_.set_proxy_resolution_service( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 748 | ProxyResolutionService::CreateFixed(proxy, |
| 749 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 750 | set_network_delegate(delegate); |
| 751 | Init(); |
| 752 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 753 | ~TestURLRequestContextWithProxy() override = default; |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 754 | }; |
| 755 | |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 756 | // A mock ReportSenderInterface that just remembers the latest report |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 757 | // URI and report to be sent. |
| 758 | class MockCertificateReportSender |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 759 | : public TransportSecurityState::ReportSenderInterface { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 760 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 761 | MockCertificateReportSender() = default; |
| 762 | ~MockCertificateReportSender() override = default; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 763 | |
meacer | 5d4dc5a | 2017-04-27 20:37:48 | [diff] [blame] | 764 | void Send(const GURL& report_uri, |
| 765 | base::StringPiece content_type, |
| 766 | base::StringPiece report, |
| 767 | const base::Callback<void()>& success_callback, |
| 768 | const base::Callback<void(const GURL&, int, int)>& error_callback) |
| 769 | override { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 770 | latest_report_uri_ = report_uri; |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 771 | report.CopyToString(&latest_report_); |
| 772 | content_type.CopyToString(&latest_content_type_); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 773 | } |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 774 | const GURL& latest_report_uri() { return latest_report_uri_; } |
| 775 | const std::string& latest_report() { return latest_report_; } |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 776 | const std::string& latest_content_type() { return latest_content_type_; } |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 777 | |
| 778 | private: |
| 779 | GURL latest_report_uri_; |
| 780 | std::string latest_report_; |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 781 | std::string latest_content_type_; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 782 | }; |
| 783 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 784 | // OCSPErrorTestDelegate caches the SSLInfo passed to OnSSLCertificateError. |
| 785 | // This is needed because after the certificate failure, the URLRequest will |
| 786 | // retry the connection, and return a partial SSLInfo with a cached cert status. |
| 787 | // The partial SSLInfo does not have the OCSP information filled out. |
| 788 | class OCSPErrorTestDelegate : public TestDelegate { |
| 789 | public: |
| 790 | void OnSSLCertificateError(URLRequest* request, |
| 791 | const SSLInfo& ssl_info, |
| 792 | bool fatal) override { |
| 793 | ssl_info_ = ssl_info; |
| 794 | on_ssl_certificate_error_called_ = true; |
| 795 | TestDelegate::OnSSLCertificateError(request, ssl_info, fatal); |
| 796 | } |
| 797 | |
| 798 | bool on_ssl_certificate_error_called() { |
| 799 | return on_ssl_certificate_error_called_; |
| 800 | } |
| 801 | |
| 802 | SSLInfo ssl_info() { return ssl_info_; } |
| 803 | |
| 804 | private: |
| 805 | bool on_ssl_certificate_error_called_ = false; |
| 806 | SSLInfo ssl_info_; |
| 807 | }; |
| 808 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 809 | } // namespace |
| 810 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 811 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 812 | class URLRequestTest : public PlatformTest, public WithScopedTaskEnvironment { |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 813 | public: |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 814 | URLRequestTest() |
| 815 | : default_context_(std::make_unique<TestURLRequestContext>(true)) { |
| 816 | default_context_->set_network_delegate(&default_network_delegate_); |
| 817 | default_context_->set_net_log(&net_log_); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 818 | job_factory_impl_ = new URLRequestJobFactoryImpl(); |
| 819 | job_factory_.reset(job_factory_impl_); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 820 | } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 821 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 822 | ~URLRequestTest() override { |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 823 | // URLRequestJobs may post clean-up tasks on destruction. |
| 824 | base::RunLoop().RunUntilIdle(); |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 825 | |
| 826 | SetTransportSecurityStateSourceForTesting(nullptr); |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 827 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 828 | |
dcheng | 2339883c | 2014-12-23 00:23:05 | [diff] [blame] | 829 | void SetUp() override { |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 830 | SetUpFactory(); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 831 | default_context_->set_job_factory(job_factory_.get()); |
| 832 | default_context_->Init(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 833 | PlatformTest::SetUp(); |
| 834 | } |
| 835 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 836 | void TearDown() override { default_context_.reset(); } |
| 837 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 838 | virtual void SetUpFactory() { |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 839 | job_factory_impl_->SetProtocolHandler( |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 840 | "data", std::make_unique<DataProtocolHandler>()); |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 841 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 842 | job_factory_impl_->SetProtocolHandler( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 843 | "file", std::make_unique<FileProtocolHandler>( |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 844 | base::ThreadTaskRunnerHandle::Get())); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 845 | #endif |
| 846 | } |
| 847 | |
| 848 | TestNetworkDelegate* default_network_delegate() { |
| 849 | return &default_network_delegate_; |
| 850 | } |
| 851 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 852 | TestURLRequestContext& default_context() const { return *default_context_; } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 853 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 854 | // Adds the TestJobInterceptor to the default context. |
| 855 | TestJobInterceptor* AddTestInterceptor() { |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 856 | TestJobInterceptor* protocol_handler_ = new TestJobInterceptor(); |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 857 | job_factory_impl_->SetProtocolHandler("http", nullptr); |
| 858 | job_factory_impl_->SetProtocolHandler("http", |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 859 | base::WrapUnique(protocol_handler_)); |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 860 | return protocol_handler_; |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 861 | } |
| 862 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 863 | // Creates a temp test file and writes |data| to the file. The file will be |
| 864 | // deleted after the test completes. |
| 865 | void CreateTestFile(const char* data, |
| 866 | size_t data_size, |
| 867 | base::FilePath* test_file) { |
| 868 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 869 | // Get an absolute path since |temp_dir| can contain a symbolic link. As of |
| 870 | // now, Mac and Android bots return a path with a symbolic link. |
| 871 | base::FilePath absolute_temp_dir = |
| 872 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
| 873 | |
| 874 | ASSERT_TRUE(base::CreateTemporaryFileInDir(absolute_temp_dir, test_file)); |
| 875 | ASSERT_EQ(static_cast<int>(data_size), |
| 876 | base::WriteFile(*test_file, data, data_size)); |
| 877 | } |
| 878 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 879 | protected: |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 880 | TestNetLog net_log_; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 881 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 882 | URLRequestJobFactoryImpl* job_factory_impl_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 883 | std::unique_ptr<URLRequestJobFactory> job_factory_; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 884 | std::unique_ptr<TestURLRequestContext> default_context_; |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 885 | base::ScopedTempDir temp_dir_; |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 886 | }; |
| 887 | |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 888 | // This NetworkDelegate is picky about what files are accessible. Only |
| 889 | // whitelisted files are allowed. |
| 890 | class CookieBlockingNetworkDelegate : public TestNetworkDelegate { |
| 891 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 892 | CookieBlockingNetworkDelegate() = default; |
| 893 | ; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 894 | |
| 895 | // Adds |directory| to the access white list. |
| 896 | void AddToWhitelist(const base::FilePath& directory) { |
| 897 | whitelist_.insert(directory); |
| 898 | } |
| 899 | |
| 900 | private: |
| 901 | // Returns true if |path| matches the white list. |
| 902 | bool OnCanAccessFileInternal(const base::FilePath& path) const { |
| 903 | for (const auto& directory : whitelist_) { |
| 904 | if (directory == path || directory.IsParent(path)) |
| 905 | return true; |
| 906 | } |
| 907 | return false; |
| 908 | } |
| 909 | |
| 910 | // Returns true only if both |original_path| and |absolute_path| match the |
| 911 | // white list. |
| 912 | bool OnCanAccessFile(const URLRequest& request, |
| 913 | const base::FilePath& original_path, |
| 914 | const base::FilePath& absolute_path) const override { |
| 915 | return (OnCanAccessFileInternal(original_path) && |
| 916 | OnCanAccessFileInternal(absolute_path)); |
| 917 | } |
| 918 | |
| 919 | std::set<base::FilePath> whitelist_; |
| 920 | |
| 921 | DISALLOW_COPY_AND_ASSIGN(CookieBlockingNetworkDelegate); |
| 922 | }; |
| 923 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 924 | TEST_F(URLRequestTest, AboutBlankTest) { |
| 925 | TestDelegate d; |
| 926 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 927 | std::unique_ptr<URLRequest> r( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 928 | default_context().CreateRequest(GURL("about:blank"), DEFAULT_PRIORITY, |
| 929 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 930 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 931 | r->Start(); |
| 932 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 933 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 934 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 935 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 936 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 937 | EXPECT_FALSE(d.received_data_before_response()); |
| 938 | EXPECT_EQ(d.bytes_received(), 0); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 939 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 940 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 941 | |
| 942 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 943 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | |
| 947 | TEST_F(URLRequestTest, DataURLImageTest) { |
| 948 | TestDelegate d; |
| 949 | { |
| 950 | // Use our nice little Chrome logo. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 951 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 952 | GURL("data:image/png;base64," |
| 953 | "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUB" |
| 954 | "BjG3w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQ" |
| 955 | "xIEVxitD5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI" |
| 956 | "5MpCVdW0gO7tvNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKz" |
| 957 | "Mx1+fg9bAgK6zHq9cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7Ck" |
| 958 | "IMDxQpF7r/MWq12UctI1dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0" |
| 959 | "WYUqt57yWf2McHTObYPbVD+ZwbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2H" |
| 960 | "QQcm43XwmtoYM6vVKleh0uoWvnzW3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nk" |
| 961 | "DFHF9ZS+uYVjRUasMeHUmyLYtcklTvzWGFZnNOXczThvpKIzjcahSqIzkvDLayDq6" |
| 962 | "D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTbxXX1T5xsV9tm9r4TQwHLiZw/pdDZJ" |
| 963 | "ea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5MrVvMzNaI3+ERHfrFzPKQukrQ" |
| 964 | "GI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gVMd6q5c8GdosynKmSeRuGz" |
| 965 | "pjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq/uxxZKzNiZFGD0wRC" |
| 966 | "3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7FUx1qLnV7MGF40" |
| 967 | "smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufBJbsysjjW4" |
| 968 | "kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJMnlm2O" |
| 969 | "34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfmm" |
| 970 | "oRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV" |
| 971 | "5EnhORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 972 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 973 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 974 | r->Start(); |
| 975 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 976 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 977 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 978 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 979 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 980 | EXPECT_FALSE(d.received_data_before_response()); |
| 981 | EXPECT_EQ(d.bytes_received(), 911); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 982 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 983 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 984 | |
| 985 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 986 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 990 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 991 | TEST_F(URLRequestTest, FileTest) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 992 | const char kTestFileContent[] = "Hello"; |
| 993 | base::FilePath test_file; |
| 994 | ASSERT_NO_FATAL_FAILURE( |
| 995 | CreateTestFile(kTestFileContent, sizeof(kTestFileContent), &test_file)); |
kraush | 5a64582 | 2016-04-07 18:35:04 | [diff] [blame] | 996 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 997 | GURL test_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 998 | |
| 999 | TestDelegate d; |
| 1000 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1001 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1002 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1003 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1004 | r->Start(); |
| 1005 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1006 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1007 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1008 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1009 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1010 | EXPECT_EQ(1, d.response_started_count()); |
| 1011 | EXPECT_FALSE(d.received_data_before_response()); |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1012 | EXPECT_EQ(d.bytes_received(), static_cast<int>(sizeof(kTestFileContent))); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1013 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 1014 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 1015 | |
| 1016 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1017 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1021 | TEST_F(URLRequestTest, FileTestCancel) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1022 | const char kTestFileContent[] = "Hello"; |
| 1023 | base::FilePath test_file; |
| 1024 | ASSERT_NO_FATAL_FAILURE( |
| 1025 | CreateTestFile(kTestFileContent, sizeof(kTestFileContent), &test_file)); |
| 1026 | |
| 1027 | GURL test_url = FilePathToFileURL(test_file); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1028 | |
| 1029 | TestDelegate d; |
| 1030 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1031 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1032 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1033 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1034 | r->Start(); |
| 1035 | EXPECT_TRUE(r->is_pending()); |
| 1036 | r->Cancel(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1037 | } |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 1038 | // Async cancellation should be safe even when URLRequest has been already |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1039 | // destroyed. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1040 | base::RunLoop().RunUntilIdle(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1041 | } |
| 1042 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1043 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 1044 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1045 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1046 | FillBuffer(buffer.get(), buffer_size); |
| 1047 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1048 | base::FilePath test_file; |
| 1049 | ASSERT_NO_FATAL_FAILURE( |
| 1050 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1051 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1052 | |
| 1053 | const size_t first_byte_position = 500; |
| 1054 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 1055 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 1056 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 1057 | buffer.get() + last_byte_position + 1); |
| 1058 | |
| 1059 | TestDelegate d; |
| 1060 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1061 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1062 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1063 | |
| 1064 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1065 | headers.SetHeader( |
| 1066 | HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1067 | HttpByteRange::Bounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1068 | first_byte_position, last_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1069 | r->SetExtraRequestHeaders(headers); |
| 1070 | r->Start(); |
| 1071 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1072 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1073 | d.RunUntilComplete(); |
| 1074 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1075 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1076 | EXPECT_EQ(1, d.response_started_count()); |
| 1077 | EXPECT_FALSE(d.received_data_before_response()); |
| 1078 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 1079 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 1080 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 1081 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 1085 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1086 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1087 | FillBuffer(buffer.get(), buffer_size); |
| 1088 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1089 | base::FilePath test_file; |
| 1090 | ASSERT_NO_FATAL_FAILURE( |
| 1091 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1092 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1093 | |
| 1094 | const size_t first_byte_position = 500; |
| 1095 | const size_t last_byte_position = buffer_size - 1; |
| 1096 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 1097 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 1098 | buffer.get() + last_byte_position + 1); |
| 1099 | |
| 1100 | TestDelegate d; |
| 1101 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1102 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1103 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1104 | |
| 1105 | HttpRequestHeaders headers; |
| 1106 | headers.SetHeader(HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1107 | HttpByteRange::RightUnbounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1108 | first_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1109 | r->SetExtraRequestHeaders(headers); |
| 1110 | r->Start(); |
| 1111 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1112 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1113 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1114 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1115 | EXPECT_EQ(1, d.response_started_count()); |
| 1116 | EXPECT_FALSE(d.received_data_before_response()); |
| 1117 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 1118 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 1119 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 1120 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 1124 | const size_t buffer_size = 400000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1125 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1126 | FillBuffer(buffer.get(), buffer_size); |
| 1127 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1128 | base::FilePath test_file; |
| 1129 | ASSERT_NO_FATAL_FAILURE( |
| 1130 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1131 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1132 | |
| 1133 | TestDelegate d; |
| 1134 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1135 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1136 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1137 | |
| 1138 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1139 | headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1140 | r->SetExtraRequestHeaders(headers); |
| 1141 | r->Start(); |
| 1142 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1143 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1144 | d.RunUntilComplete(); |
| 1145 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1146 | EXPECT_TRUE(d.request_failed()); |
| 1147 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1148 | } |
| 1149 | |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1150 | TEST_F(URLRequestTest, AllowFileURLs) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1151 | std::string test_data("monkey"); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1152 | base::FilePath test_file; |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1153 | ASSERT_NO_FATAL_FAILURE( |
| 1154 | CreateTestFile(test_data.data(), test_data.size(), &test_file)); |
| 1155 | |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1156 | // The directory part of the path returned from CreateTemporaryFileInDir() |
| 1157 | // can be slightly different from |absolute_temp_dir| on Windows. |
| 1158 | // Example: C:\\Users\\CHROME~2 -> C:\\Users\\chrome-bot |
| 1159 | // Hence the test should use the directory name of |test_file|, rather than |
| 1160 | // |absolute_temp_dir|, for whitelisting. |
| 1161 | base::FilePath real_temp_dir = test_file.DirName(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1162 | GURL test_file_url = FilePathToFileURL(test_file); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1163 | { |
| 1164 | TestDelegate d; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1165 | CookieBlockingNetworkDelegate network_delegate; |
| 1166 | network_delegate.AddToWhitelist(real_temp_dir); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1167 | default_context().set_network_delegate(&network_delegate); |
| 1168 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1169 | test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1170 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1171 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1172 | // This should be allowed as the file path is whitelisted. |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1173 | EXPECT_FALSE(d.request_failed()); |
| 1174 | EXPECT_EQ(test_data, d.data_received()); |
| 1175 | } |
| 1176 | |
| 1177 | { |
| 1178 | TestDelegate d; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1179 | CookieBlockingNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1180 | default_context().set_network_delegate(&network_delegate); |
| 1181 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1182 | test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1183 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1184 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1185 | // This should be rejected as the file path is not whitelisted. |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1186 | EXPECT_TRUE(d.request_failed()); |
| 1187 | EXPECT_EQ("", d.data_received()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1188 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1189 | } |
| 1190 | } |
| 1191 | |
Wez | 3553913 | 2018-07-17 11:26:05 | [diff] [blame] | 1192 | #if defined(OS_POSIX) // Because of symbolic links. |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1193 | |
| 1194 | TEST_F(URLRequestTest, SymlinksToFiles) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1195 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1196 | // Get an absolute path since temp_dir can contain a symbolic link. |
| 1197 | base::FilePath absolute_temp_dir = |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1198 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1199 | |
| 1200 | // Create a good directory (will be whitelisted) and a good file. |
| 1201 | base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); |
| 1202 | ASSERT_TRUE(base::CreateDirectory(good_dir)); |
| 1203 | base::FilePath good_file; |
| 1204 | ASSERT_TRUE(base::CreateTemporaryFileInDir(good_dir, &good_file)); |
| 1205 | std::string good_data("good"); |
| 1206 | base::WriteFile(good_file, good_data.data(), good_data.size()); |
| 1207 | // See the comment in AllowFileURLs() for why this is done. |
| 1208 | base::FilePath real_good_dir = good_file.DirName(); |
| 1209 | |
| 1210 | // Create a bad directory (will not be whitelisted) and a bad file. |
| 1211 | base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); |
| 1212 | ASSERT_TRUE(base::CreateDirectory(bad_dir)); |
| 1213 | base::FilePath bad_file; |
| 1214 | ASSERT_TRUE(base::CreateTemporaryFileInDir(bad_dir, &bad_file)); |
| 1215 | std::string bad_data("bad"); |
| 1216 | base::WriteFile(bad_file, bad_data.data(), bad_data.size()); |
| 1217 | |
| 1218 | // This symlink will point to the good file. Access to the symlink will be |
| 1219 | // allowed as both the symlink and the destination file are in the same |
| 1220 | // good directory. |
| 1221 | base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); |
| 1222 | ASSERT_TRUE(base::CreateSymbolicLink(good_file, good_symlink)); |
| 1223 | GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1224 | // This symlink will point to the bad file. Even though the symlink is in |
| 1225 | // the good directory, access to the symlink will be rejected since it |
| 1226 | // points to the bad file. |
| 1227 | base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); |
| 1228 | ASSERT_TRUE(base::CreateSymbolicLink(bad_file, bad_symlink)); |
| 1229 | GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1230 | |
| 1231 | CookieBlockingNetworkDelegate network_delegate; |
| 1232 | network_delegate.AddToWhitelist(real_good_dir); |
| 1233 | { |
| 1234 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1235 | default_context().set_network_delegate(&network_delegate); |
| 1236 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1237 | good_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1238 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1239 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1240 | // good_file_url should be allowed. |
| 1241 | EXPECT_FALSE(d.request_failed()); |
| 1242 | EXPECT_EQ(good_data, d.data_received()); |
| 1243 | } |
| 1244 | |
| 1245 | { |
| 1246 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1247 | default_context().set_network_delegate(&network_delegate); |
| 1248 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1249 | bad_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1250 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1251 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1252 | // bad_file_url should be rejected. |
| 1253 | EXPECT_TRUE(d.request_failed()); |
| 1254 | EXPECT_EQ("", d.data_received()); |
| 1255 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | TEST_F(URLRequestTest, SymlinksToDirs) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1260 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1261 | // Get an absolute path since temp_dir can contain a symbolic link. |
| 1262 | base::FilePath absolute_temp_dir = |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1263 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1264 | |
| 1265 | // Create a good directory (will be whitelisted). |
| 1266 | base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); |
| 1267 | ASSERT_TRUE(base::CreateDirectory(good_dir)); |
| 1268 | |
| 1269 | // Create a bad directory (will not be whitelisted). |
| 1270 | base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); |
| 1271 | ASSERT_TRUE(base::CreateDirectory(bad_dir)); |
| 1272 | |
| 1273 | // This symlink will point to the good directory. Access to the symlink |
| 1274 | // will be allowed as the symlink is in the good dir that'll be white |
| 1275 | // listed. |
| 1276 | base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); |
| 1277 | ASSERT_TRUE(base::CreateSymbolicLink(good_dir, good_symlink)); |
| 1278 | GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1279 | // This symlink will point to the bad directory. Even though the symlink is |
| 1280 | // in the good directory, access to the symlink will be rejected since it |
| 1281 | // points to the bad directory. |
| 1282 | base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); |
| 1283 | ASSERT_TRUE(base::CreateSymbolicLink(bad_dir, bad_symlink)); |
| 1284 | GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1285 | |
| 1286 | CookieBlockingNetworkDelegate network_delegate; |
| 1287 | network_delegate.AddToWhitelist(good_dir); |
| 1288 | { |
| 1289 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1290 | default_context().set_network_delegate(&network_delegate); |
| 1291 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1292 | good_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1293 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1294 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1295 | // good_file_url should be allowed. |
| 1296 | EXPECT_FALSE(d.request_failed()); |
| 1297 | ASSERT_NE(d.data_received().find("good_symlink"), std::string::npos); |
| 1298 | } |
| 1299 | |
| 1300 | { |
| 1301 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1302 | default_context().set_network_delegate(&network_delegate); |
| 1303 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1304 | bad_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1305 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1306 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1307 | // bad_file_url should be rejected. |
| 1308 | EXPECT_TRUE(d.request_failed()); |
| 1309 | EXPECT_EQ("", d.data_received()); |
| 1310 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
| 1311 | } |
| 1312 | } |
| 1313 | |
Wez | 3553913 | 2018-07-17 11:26:05 | [diff] [blame] | 1314 | #endif // defined(OS_POSIX) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1315 | |
| 1316 | TEST_F(URLRequestTest, FileDirCancelTest) { |
| 1317 | // Put in mock resource provider. |
| 1318 | NetModule::SetResourceProvider(TestNetResourceProvider); |
| 1319 | |
| 1320 | TestDelegate d; |
| 1321 | { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1322 | base::FilePath file_path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1323 | base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1324 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 1325 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
| 1326 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1327 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1328 | FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d, |
| 1329 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1330 | req->Start(); |
| 1331 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1332 | |
| 1333 | d.set_cancel_in_received_data_pending(true); |
| 1334 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1335 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | // Take out mock resource provider. |
| 1339 | NetModule::SetResourceProvider(NULL); |
| 1340 | } |
| 1341 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1342 | TEST_F(URLRequestTest, FileDirOutputSanity) { |
| 1343 | // Verify the general sanity of the the output of the file: |
| 1344 | // directory lister by checking for the output of a known existing |
| 1345 | // file. |
| 1346 | const char sentinel_name[] = "filedir-sentinel"; |
| 1347 | |
| 1348 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1349 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1350 | path = path.Append(kTestFilePath); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1351 | |
| 1352 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1353 | std::unique_ptr<URLRequest> req( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1354 | default_context().CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 1355 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1356 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1357 | d.RunUntilComplete(); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1358 | |
| 1359 | // Generate entry for the sentinel file. |
| 1360 | base::FilePath sentinel_path = path.AppendASCII(sentinel_name); |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 1361 | base::File::Info info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 1362 | EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1363 | EXPECT_GT(info.size, 0); |
| 1364 | std::string sentinel_output = GetDirectoryListingEntry( |
| 1365 | base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1366 | std::string(sentinel_name), false /* is_dir */, info.size, |
| 1367 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1368 | info.last_modified); |
| 1369 | |
| 1370 | ASSERT_LT(0, d.bytes_received()); |
| 1371 | ASSERT_FALSE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1372 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1373 | // Check for the entry generated for the "sentinel" file. |
| 1374 | const std::string& data = d.data_received(); |
| 1375 | ASSERT_NE(data.find(sentinel_output), std::string::npos); |
| 1376 | } |
| 1377 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1378 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 1379 | // There is an implicit redirect when loading a file path that matches a |
| 1380 | // directory and does not end with a slash. Ensure that following such |
| 1381 | // redirects does not crash. See http://crbug.com/18686. |
| 1382 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1383 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1384 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1385 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1386 | |
| 1387 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1388 | std::unique_ptr<URLRequest> req( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1389 | default_context().CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 1390 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1391 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1392 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1393 | |
| 1394 | ASSERT_EQ(1, d.received_redirect_count()); |
| 1395 | ASSERT_LT(0, d.bytes_received()); |
| 1396 | ASSERT_FALSE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1397 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | #if defined(OS_WIN) |
| 1401 | // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
| 1402 | TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
| 1403 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1404 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1405 | GURL("file:///"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1406 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1407 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1408 | |
| 1409 | ASSERT_EQ(1, d.received_redirect_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1410 | EXPECT_NE(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1411 | } |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1412 | #endif // defined(OS_WIN) |
| 1413 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 1414 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1415 | |
| 1416 | TEST_F(URLRequestTest, InvalidUrlTest) { |
| 1417 | TestDelegate d; |
| 1418 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1419 | std::unique_ptr<URLRequest> r( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1420 | default_context().CreateRequest(GURL("invalid url"), DEFAULT_PRIORITY, |
| 1421 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1422 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1423 | r->Start(); |
| 1424 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1425 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1426 | d.RunUntilComplete(); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1427 | EXPECT_TRUE(d.request_failed()); |
| 1428 | } |
| 1429 | } |
| 1430 | |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1431 | TEST_F(URLRequestTest, InvalidReferrerTest) { |
| 1432 | TestURLRequestContext context; |
| 1433 | TestNetworkDelegate network_delegate; |
| 1434 | network_delegate.set_cancel_request_with_policy_violating_referrer(true); |
| 1435 | context.set_network_delegate(&network_delegate); |
| 1436 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1437 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1438 | context.CreateRequest(GURL("http://localhost/"), DEFAULT_PRIORITY, &d, |
| 1439 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1440 | req->SetReferrer("https://somewhere.com/"); |
| 1441 | |
| 1442 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1443 | d.RunUntilComplete(); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1444 | EXPECT_TRUE(d.request_failed()); |
| 1445 | } |
| 1446 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1447 | #if defined(OS_WIN) |
| 1448 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
| 1449 | base::FilePath app_path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1450 | base::PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1451 | app_path = app_path.Append(kTestFilePath); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1452 | app_path = app_path.AppendASCII("with-headers.html"); |
| 1453 | |
| 1454 | std::wstring lnk_path = app_path.value() + L".lnk"; |
| 1455 | |
| 1456 | base::win::ScopedCOMInitializer com_initializer; |
| 1457 | |
| 1458 | // Temporarily create a shortcut for test |
| 1459 | { |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 1460 | Microsoft::WRL::ComPtr<IShellLink> shell; |
robliao | eb9bfd64 | 2017-05-18 17:35:16 | [diff] [blame] | 1461 | ASSERT_TRUE(SUCCEEDED(::CoCreateInstance( |
| 1462 | CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shell)))); |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 1463 | Microsoft::WRL::ComPtr<IPersistFile> persist; |
robliao | 310fa98b | 2017-05-11 17:14:00 | [diff] [blame] | 1464 | ASSERT_TRUE(SUCCEEDED(shell.CopyTo(persist.GetAddressOf()))); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1465 | EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str()))); |
| 1466 | EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); |
| 1467 | EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE))); |
| 1468 | } |
| 1469 | |
| 1470 | TestDelegate d; |
| 1471 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 1472 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1473 | FilePathToFileURL(base::FilePath(lnk_path)), DEFAULT_PRIORITY, &d, |
| 1474 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1475 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1476 | r->Start(); |
| 1477 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1478 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1479 | d.RunUntilComplete(); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1480 | |
| 1481 | WIN32_FILE_ATTRIBUTE_DATA data; |
| 1482 | GetFileAttributesEx(app_path.value().c_str(), |
| 1483 | GetFileExInfoStandard, &data); |
| 1484 | HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ, |
| 1485 | FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 1486 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 1487 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1488 | std::unique_ptr<char[]> buffer(new char[data.nFileSizeLow]); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1489 | DWORD read_size; |
| 1490 | BOOL result; |
| 1491 | result = ReadFile(file, buffer.get(), data.nFileSizeLow, |
| 1492 | &read_size, NULL); |
| 1493 | std::string content(buffer.get(), read_size); |
| 1494 | CloseHandle(file); |
| 1495 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1496 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1497 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1498 | EXPECT_EQ(content, d.data_received()); |
| 1499 | } |
| 1500 | |
| 1501 | // Clean the shortcut |
| 1502 | DeleteFile(lnk_path.c_str()); |
| 1503 | } |
| 1504 | #endif // defined(OS_WIN) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1505 | |
| 1506 | // Custom URLRequestJobs for use with interceptor tests |
| 1507 | class RestartTestJob : public URLRequestTestJob { |
| 1508 | public: |
| 1509 | RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1510 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1511 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1512 | void StartAsync() override { this->NotifyRestartRequired(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1513 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1514 | ~RestartTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1515 | }; |
| 1516 | |
| 1517 | class CancelTestJob : public URLRequestTestJob { |
| 1518 | public: |
| 1519 | explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1520 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1521 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1522 | void StartAsync() override { request_->Cancel(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1523 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1524 | ~CancelTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1525 | }; |
| 1526 | |
| 1527 | class CancelThenRestartTestJob : public URLRequestTestJob { |
| 1528 | public: |
| 1529 | explicit CancelThenRestartTestJob(URLRequest* request, |
| 1530 | NetworkDelegate* network_delegate) |
| 1531 | : URLRequestTestJob(request, network_delegate, true) { |
| 1532 | } |
| 1533 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1534 | void StartAsync() override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1535 | request_->Cancel(); |
| 1536 | this->NotifyRestartRequired(); |
| 1537 | } |
| 1538 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1539 | ~CancelThenRestartTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1540 | }; |
| 1541 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1542 | // An Interceptor for use with interceptor tests. |
| 1543 | class MockURLRequestInterceptor : public URLRequestInterceptor { |
| 1544 | public: |
| 1545 | // Static getters for canned response header and data strings. |
| 1546 | static std::string ok_data() { |
| 1547 | return URLRequestTestJob::test_data_1(); |
| 1548 | } |
| 1549 | |
| 1550 | static std::string ok_headers() { |
| 1551 | return URLRequestTestJob::test_headers(); |
| 1552 | } |
| 1553 | |
| 1554 | static std::string redirect_data() { |
| 1555 | return std::string(); |
| 1556 | } |
| 1557 | |
| 1558 | static std::string redirect_headers() { |
| 1559 | return URLRequestTestJob::test_redirect_headers(); |
| 1560 | } |
| 1561 | |
| 1562 | static std::string error_data() { |
| 1563 | return std::string("ohhh nooooo mr. bill!"); |
| 1564 | } |
| 1565 | |
| 1566 | static std::string error_headers() { |
| 1567 | return URLRequestTestJob::test_error_headers(); |
| 1568 | } |
| 1569 | |
| 1570 | MockURLRequestInterceptor() |
| 1571 | : intercept_main_request_(false), restart_main_request_(false), |
| 1572 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 1573 | simulate_main_network_error_(false), |
| 1574 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 1575 | intercept_final_response_(false), cancel_final_request_(false), |
| 1576 | use_url_request_http_job_(false), |
| 1577 | did_intercept_main_(false), did_restart_main_(false), |
| 1578 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 1579 | did_simulate_error_main_(false), |
| 1580 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 1581 | did_intercept_final_(false), did_cancel_final_(false) { |
| 1582 | } |
| 1583 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1584 | ~MockURLRequestInterceptor() override = default; |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1585 | |
| 1586 | // URLRequestInterceptor implementation: |
| 1587 | URLRequestJob* MaybeInterceptRequest( |
| 1588 | URLRequest* request, |
| 1589 | NetworkDelegate* network_delegate) const override { |
| 1590 | if (restart_main_request_) { |
| 1591 | restart_main_request_ = false; |
| 1592 | did_restart_main_ = true; |
| 1593 | return new RestartTestJob(request, network_delegate); |
| 1594 | } |
| 1595 | if (cancel_main_request_) { |
| 1596 | cancel_main_request_ = false; |
| 1597 | did_cancel_main_ = true; |
| 1598 | return new CancelTestJob(request, network_delegate); |
| 1599 | } |
| 1600 | if (cancel_then_restart_main_request_) { |
| 1601 | cancel_then_restart_main_request_ = false; |
| 1602 | did_cancel_then_restart_main_ = true; |
| 1603 | return new CancelThenRestartTestJob(request, network_delegate); |
| 1604 | } |
| 1605 | if (simulate_main_network_error_) { |
| 1606 | simulate_main_network_error_ = false; |
| 1607 | did_simulate_error_main_ = true; |
| 1608 | if (use_url_request_http_job_) { |
| 1609 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1610 | } |
| 1611 | // This job will result in error since the requested URL is not one of the |
| 1612 | // URLs supported by these tests. |
| 1613 | return new URLRequestTestJob(request, network_delegate, true); |
| 1614 | } |
| 1615 | if (!intercept_main_request_) |
| 1616 | return nullptr; |
| 1617 | intercept_main_request_ = false; |
| 1618 | did_intercept_main_ = true; |
| 1619 | URLRequestTestJob* job = new URLRequestTestJob(request, |
| 1620 | network_delegate, |
| 1621 | main_headers_, |
| 1622 | main_data_, |
| 1623 | true); |
| 1624 | job->set_load_timing_info(main_request_load_timing_info_); |
| 1625 | return job; |
| 1626 | } |
| 1627 | |
| 1628 | URLRequestJob* MaybeInterceptRedirect(URLRequest* request, |
| 1629 | NetworkDelegate* network_delegate, |
| 1630 | const GURL& location) const override { |
| 1631 | if (cancel_redirect_request_) { |
| 1632 | cancel_redirect_request_ = false; |
| 1633 | did_cancel_redirect_ = true; |
| 1634 | return new CancelTestJob(request, network_delegate); |
| 1635 | } |
| 1636 | if (!intercept_redirect_) |
| 1637 | return nullptr; |
| 1638 | intercept_redirect_ = false; |
| 1639 | did_intercept_redirect_ = true; |
| 1640 | if (use_url_request_http_job_) { |
| 1641 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1642 | } |
| 1643 | return new URLRequestTestJob(request, |
| 1644 | network_delegate, |
| 1645 | redirect_headers_, |
| 1646 | redirect_data_, |
| 1647 | true); |
| 1648 | } |
| 1649 | |
| 1650 | URLRequestJob* MaybeInterceptResponse( |
| 1651 | URLRequest* request, |
| 1652 | NetworkDelegate* network_delegate) const override { |
| 1653 | if (cancel_final_request_) { |
| 1654 | cancel_final_request_ = false; |
| 1655 | did_cancel_final_ = true; |
| 1656 | return new CancelTestJob(request, network_delegate); |
| 1657 | } |
| 1658 | if (!intercept_final_response_) |
| 1659 | return nullptr; |
| 1660 | intercept_final_response_ = false; |
| 1661 | did_intercept_final_ = true; |
| 1662 | if (use_url_request_http_job_) { |
| 1663 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1664 | } |
| 1665 | return new URLRequestTestJob(request, |
| 1666 | network_delegate, |
| 1667 | final_headers_, |
| 1668 | final_data_, |
| 1669 | true); |
| 1670 | } |
| 1671 | |
| 1672 | void set_intercept_main_request(bool intercept_main_request) { |
| 1673 | intercept_main_request_ = intercept_main_request; |
| 1674 | } |
| 1675 | |
| 1676 | void set_main_headers(const std::string& main_headers) { |
| 1677 | main_headers_ = main_headers; |
| 1678 | } |
| 1679 | |
| 1680 | void set_main_data(const std::string& main_data) { |
| 1681 | main_data_ = main_data; |
| 1682 | } |
| 1683 | |
| 1684 | void set_main_request_load_timing_info( |
| 1685 | const LoadTimingInfo& main_request_load_timing_info) { |
| 1686 | main_request_load_timing_info_ = main_request_load_timing_info; |
| 1687 | } |
| 1688 | |
| 1689 | void set_restart_main_request(bool restart_main_request) { |
| 1690 | restart_main_request_ = restart_main_request; |
| 1691 | } |
| 1692 | |
| 1693 | void set_cancel_main_request(bool cancel_main_request) { |
| 1694 | cancel_main_request_ = cancel_main_request; |
| 1695 | } |
| 1696 | |
| 1697 | void set_cancel_then_restart_main_request( |
| 1698 | bool cancel_then_restart_main_request) { |
| 1699 | cancel_then_restart_main_request_ = cancel_then_restart_main_request; |
| 1700 | } |
| 1701 | |
| 1702 | void set_simulate_main_network_error(bool simulate_main_network_error) { |
| 1703 | simulate_main_network_error_ = simulate_main_network_error; |
| 1704 | } |
| 1705 | |
| 1706 | void set_intercept_redirect(bool intercept_redirect) { |
| 1707 | intercept_redirect_ = intercept_redirect; |
| 1708 | } |
| 1709 | |
| 1710 | void set_redirect_headers(const std::string& redirect_headers) { |
| 1711 | redirect_headers_ = redirect_headers; |
| 1712 | } |
| 1713 | |
| 1714 | void set_redirect_data(const std::string& redirect_data) { |
| 1715 | redirect_data_ = redirect_data; |
| 1716 | } |
| 1717 | |
| 1718 | void set_cancel_redirect_request(bool cancel_redirect_request) { |
| 1719 | cancel_redirect_request_ = cancel_redirect_request; |
| 1720 | } |
| 1721 | |
| 1722 | void set_intercept_final_response(bool intercept_final_response) { |
| 1723 | intercept_final_response_ = intercept_final_response; |
| 1724 | } |
| 1725 | |
| 1726 | void set_final_headers(const std::string& final_headers) { |
| 1727 | final_headers_ = final_headers; |
| 1728 | } |
| 1729 | |
| 1730 | void set_final_data(const std::string& final_data) { |
| 1731 | final_data_ = final_data; |
| 1732 | } |
| 1733 | |
| 1734 | void set_cancel_final_request(bool cancel_final_request) { |
| 1735 | cancel_final_request_ = cancel_final_request; |
| 1736 | } |
| 1737 | |
| 1738 | void set_use_url_request_http_job(bool use_url_request_http_job) { |
| 1739 | use_url_request_http_job_ = use_url_request_http_job; |
| 1740 | } |
| 1741 | |
| 1742 | bool did_intercept_main() const { |
| 1743 | return did_intercept_main_; |
| 1744 | } |
| 1745 | |
| 1746 | bool did_restart_main() const { |
| 1747 | return did_restart_main_; |
| 1748 | } |
| 1749 | |
| 1750 | bool did_cancel_main() const { |
| 1751 | return did_cancel_main_; |
| 1752 | } |
| 1753 | |
| 1754 | bool did_cancel_then_restart_main() const { |
| 1755 | return did_cancel_then_restart_main_; |
| 1756 | } |
| 1757 | |
| 1758 | bool did_simulate_error_main() const { |
| 1759 | return did_simulate_error_main_; |
| 1760 | } |
| 1761 | |
| 1762 | bool did_intercept_redirect() const { |
| 1763 | return did_intercept_redirect_; |
| 1764 | } |
| 1765 | |
| 1766 | bool did_cancel_redirect() const { |
| 1767 | return did_cancel_redirect_; |
| 1768 | } |
| 1769 | |
| 1770 | bool did_intercept_final() const { |
| 1771 | return did_intercept_final_; |
| 1772 | } |
| 1773 | |
| 1774 | bool did_cancel_final() const { |
| 1775 | return did_cancel_final_; |
| 1776 | } |
| 1777 | |
| 1778 | private: |
| 1779 | // Indicate whether to intercept the main request, and if so specify the |
| 1780 | // response to return and the LoadTimingInfo to use. |
| 1781 | mutable bool intercept_main_request_; |
| 1782 | mutable std::string main_headers_; |
| 1783 | mutable std::string main_data_; |
| 1784 | mutable LoadTimingInfo main_request_load_timing_info_; |
| 1785 | |
| 1786 | // These indicate actions that can be taken within MaybeInterceptRequest. |
| 1787 | mutable bool restart_main_request_; |
| 1788 | mutable bool cancel_main_request_; |
| 1789 | mutable bool cancel_then_restart_main_request_; |
| 1790 | mutable bool simulate_main_network_error_; |
| 1791 | |
| 1792 | // Indicate whether to intercept redirects, and if so specify the response to |
| 1793 | // return. |
| 1794 | mutable bool intercept_redirect_; |
| 1795 | mutable std::string redirect_headers_; |
| 1796 | mutable std::string redirect_data_; |
| 1797 | |
| 1798 | // Cancel the request within MaybeInterceptRedirect. |
| 1799 | mutable bool cancel_redirect_request_; |
| 1800 | |
| 1801 | // Indicate whether to intercept the final response, and if so specify the |
| 1802 | // response to return. |
| 1803 | mutable bool intercept_final_response_; |
| 1804 | mutable std::string final_headers_; |
| 1805 | mutable std::string final_data_; |
| 1806 | |
| 1807 | // Cancel the final request within MaybeInterceptResponse. |
| 1808 | mutable bool cancel_final_request_; |
| 1809 | |
| 1810 | // Instruct the interceptor to use a real URLRequestHTTPJob. |
| 1811 | mutable bool use_url_request_http_job_; |
| 1812 | |
| 1813 | // These indicate if the interceptor did something or not. |
| 1814 | mutable bool did_intercept_main_; |
| 1815 | mutable bool did_restart_main_; |
| 1816 | mutable bool did_cancel_main_; |
| 1817 | mutable bool did_cancel_then_restart_main_; |
| 1818 | mutable bool did_simulate_error_main_; |
| 1819 | mutable bool did_intercept_redirect_; |
| 1820 | mutable bool did_cancel_redirect_; |
| 1821 | mutable bool did_intercept_final_; |
| 1822 | mutable bool did_cancel_final_; |
| 1823 | }; |
| 1824 | |
| 1825 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
| 1826 | class URLRequestInterceptorTest : public URLRequestTest { |
| 1827 | public: |
| 1828 | URLRequestInterceptorTest() : URLRequestTest(), interceptor_(NULL) { |
| 1829 | } |
| 1830 | |
| 1831 | ~URLRequestInterceptorTest() override { |
| 1832 | // URLRequestJobs may post clean-up tasks on destruction. |
| 1833 | base::RunLoop().RunUntilIdle(); |
| 1834 | } |
| 1835 | |
| 1836 | void SetUpFactory() override { |
| 1837 | interceptor_ = new MockURLRequestInterceptor(); |
| 1838 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1839 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | MockURLRequestInterceptor* interceptor() const { |
| 1843 | return interceptor_; |
| 1844 | } |
| 1845 | |
| 1846 | private: |
| 1847 | MockURLRequestInterceptor* interceptor_; |
| 1848 | }; |
| 1849 | |
| 1850 | TEST_F(URLRequestInterceptorTest, Intercept) { |
| 1851 | // Intercept the main request and respond with a simple response. |
| 1852 | interceptor()->set_intercept_main_request(true); |
| 1853 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1854 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1855 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1856 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1857 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1858 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1859 | base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); |
| 1860 | base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); |
| 1861 | base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); |
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 1862 | req->SetUserData(&user_data0, base::WrapUnique(user_data0)); |
avi | 0d99147 | 2017-04-27 07:04:04 | [diff] [blame] | 1863 | req->SetUserData(&user_data1, base::WrapUnique(user_data1)); |
| 1864 | req->SetUserData(&user_data2, base::WrapUnique(user_data2)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1865 | req->set_method("GET"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1866 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1867 | d.RunUntilComplete(); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1868 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1869 | // Make sure we can retrieve our specific user data. |
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 1870 | EXPECT_EQ(user_data0, req->GetUserData(&user_data0)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1871 | EXPECT_EQ(user_data1, req->GetUserData(&user_data1)); |
| 1872 | EXPECT_EQ(user_data2, req->GetUserData(&user_data2)); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1873 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1874 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1875 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1876 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1877 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1878 | EXPECT_EQ(1, d.response_started_count()); |
| 1879 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1880 | } |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1881 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1882 | TEST_F(URLRequestInterceptorTest, InterceptRedirect) { |
| 1883 | // Intercept the main request and respond with a redirect. |
| 1884 | interceptor()->set_intercept_main_request(true); |
| 1885 | interceptor()->set_main_headers( |
| 1886 | MockURLRequestInterceptor::redirect_headers()); |
| 1887 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 1888 | |
| 1889 | // Intercept that redirect and respond with a final OK response. |
| 1890 | interceptor()->set_intercept_redirect(true); |
| 1891 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 1892 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 1893 | |
| 1894 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1895 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1896 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1897 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1898 | req->set_method("GET"); |
| 1899 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1900 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1901 | |
| 1902 | // Check that the interceptor got called as expected. |
| 1903 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1904 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 1905 | |
| 1906 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1907 | int status = d.request_status(); |
| 1908 | EXPECT_EQ(OK, status); |
| 1909 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1910 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1911 | |
| 1912 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1913 | EXPECT_EQ(1, d.response_started_count()); |
| 1914 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1915 | } |
| 1916 | |
| 1917 | TEST_F(URLRequestInterceptorTest, InterceptServerError) { |
| 1918 | // Intercept the main request to generate a server error response. |
| 1919 | interceptor()->set_intercept_main_request(true); |
| 1920 | interceptor()->set_main_headers(MockURLRequestInterceptor::error_headers()); |
| 1921 | interceptor()->set_main_data(MockURLRequestInterceptor::error_data()); |
| 1922 | |
| 1923 | // Intercept that error and respond with an OK response. |
| 1924 | interceptor()->set_intercept_final_response(true); |
| 1925 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1926 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1927 | |
| 1928 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1929 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1930 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1931 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1932 | req->set_method("GET"); |
| 1933 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1934 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1935 | |
| 1936 | // Check that the interceptor got called as expected. |
| 1937 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1938 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1939 | |
| 1940 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1941 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1942 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1943 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1944 | EXPECT_EQ(1, d.response_started_count()); |
| 1945 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1946 | } |
| 1947 | |
| 1948 | TEST_F(URLRequestInterceptorTest, InterceptNetworkError) { |
| 1949 | // Intercept the main request to simulate a network error. |
| 1950 | interceptor()->set_simulate_main_network_error(true); |
| 1951 | |
| 1952 | // Intercept that error and respond with an OK response. |
| 1953 | interceptor()->set_intercept_final_response(true); |
| 1954 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1955 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1956 | |
| 1957 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1958 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1959 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1960 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1961 | req->set_method("GET"); |
| 1962 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1963 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1964 | |
| 1965 | // Check that the interceptor got called as expected. |
| 1966 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 1967 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1968 | |
| 1969 | // Check that we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1970 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1971 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1972 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1973 | EXPECT_EQ(1, d.response_started_count()); |
| 1974 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1975 | } |
| 1976 | |
| 1977 | TEST_F(URLRequestInterceptorTest, InterceptRestartRequired) { |
| 1978 | // Restart the main request. |
| 1979 | interceptor()->set_restart_main_request(true); |
| 1980 | |
| 1981 | // then intercept the new main request and respond with an OK response |
| 1982 | interceptor()->set_intercept_main_request(true); |
| 1983 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1984 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
| 1985 | |
| 1986 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1987 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1988 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1989 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1990 | req->set_method("GET"); |
| 1991 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1992 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1993 | |
| 1994 | // Check that the interceptor got called as expected. |
| 1995 | EXPECT_TRUE(interceptor()->did_restart_main()); |
| 1996 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1997 | |
| 1998 | // Check that we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1999 | int status = d.request_status(); |
| 2000 | EXPECT_EQ(OK, status); |
| 2001 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2002 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 2003 | |
| 2004 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 2005 | EXPECT_EQ(1, d.response_started_count()); |
| 2006 | EXPECT_EQ(0, d.received_redirect_count()); |
| 2007 | } |
| 2008 | |
| 2009 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelMain) { |
| 2010 | // Intercept the main request and cancel from within the restarted job. |
| 2011 | interceptor()->set_cancel_main_request(true); |
| 2012 | |
| 2013 | // Set up to intercept the final response and override it with an OK response. |
| 2014 | interceptor()->set_intercept_final_response(true); |
| 2015 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2016 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 2017 | |
| 2018 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2019 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2020 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2021 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2022 | req->set_method("GET"); |
| 2023 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2024 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2025 | |
| 2026 | // Check that the interceptor got called as expected. |
| 2027 | EXPECT_TRUE(interceptor()->did_cancel_main()); |
| 2028 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2029 | |
| 2030 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2031 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelRedirect) { |
| 2035 | // Intercept the main request and respond with a redirect. |
| 2036 | interceptor()->set_intercept_main_request(true); |
| 2037 | interceptor()->set_main_headers( |
| 2038 | MockURLRequestInterceptor::redirect_headers()); |
| 2039 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 2040 | |
| 2041 | // Intercept the redirect and cancel from within that job. |
| 2042 | interceptor()->set_cancel_redirect_request(true); |
| 2043 | |
| 2044 | // Set up to intercept the final response and override it with an OK response. |
| 2045 | interceptor()->set_intercept_final_response(true); |
| 2046 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2047 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 2048 | |
| 2049 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2050 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2051 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2052 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2053 | req->set_method("GET"); |
| 2054 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2055 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2056 | |
| 2057 | // Check that the interceptor got called as expected. |
| 2058 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 2059 | EXPECT_TRUE(interceptor()->did_cancel_redirect()); |
| 2060 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2061 | |
| 2062 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2063 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelFinal) { |
| 2067 | // Intercept the main request to simulate a network error. |
| 2068 | interceptor()->set_simulate_main_network_error(true); |
| 2069 | |
| 2070 | // Set up to intercept final the response and cancel from within that job. |
| 2071 | interceptor()->set_cancel_final_request(true); |
| 2072 | |
| 2073 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2074 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2075 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2076 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2077 | req->set_method("GET"); |
| 2078 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2079 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2080 | |
| 2081 | // Check that the interceptor got called as expected. |
| 2082 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 2083 | EXPECT_TRUE(interceptor()->did_cancel_final()); |
| 2084 | |
| 2085 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2086 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2087 | } |
| 2088 | |
| 2089 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelInRestart) { |
| 2090 | // Intercept the main request and cancel then restart from within that job. |
| 2091 | interceptor()->set_cancel_then_restart_main_request(true); |
| 2092 | |
| 2093 | // Set up to intercept the final response and override it with an OK response. |
| 2094 | interceptor()->set_intercept_final_response(true); |
bengr | b50d631e | 2014-11-17 22:50:50 | [diff] [blame] | 2095 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2096 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2097 | |
| 2098 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2099 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2100 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2101 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2102 | req->set_method("GET"); |
| 2103 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2104 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2105 | |
| 2106 | // Check that the interceptor got called as expected. |
| 2107 | EXPECT_TRUE(interceptor()->did_cancel_then_restart_main()); |
| 2108 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2109 | |
| 2110 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2111 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not |
| 2115 | // reused. |connect_time_flags| is used to indicate if there should be dns |
| 2116 | // or SSL times, and |used_proxy| is used for proxy times. |
| 2117 | LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now, |
| 2118 | int connect_time_flags, |
| 2119 | bool used_proxy) { |
| 2120 | LoadTimingInfo load_timing; |
| 2121 | load_timing.socket_log_id = 1; |
| 2122 | |
| 2123 | if (used_proxy) { |
| 2124 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 2125 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 2126 | } |
| 2127 | |
| 2128 | LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing; |
| 2129 | if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) { |
| 2130 | connect_timing.dns_start = now + base::TimeDelta::FromDays(3); |
| 2131 | connect_timing.dns_end = now + base::TimeDelta::FromDays(4); |
| 2132 | } |
| 2133 | connect_timing.connect_start = now + base::TimeDelta::FromDays(5); |
| 2134 | if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) { |
| 2135 | connect_timing.ssl_start = now + base::TimeDelta::FromDays(6); |
| 2136 | connect_timing.ssl_end = now + base::TimeDelta::FromDays(7); |
| 2137 | } |
| 2138 | connect_timing.connect_end = now + base::TimeDelta::FromDays(8); |
| 2139 | |
| 2140 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 2141 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 2142 | load_timing.receive_headers_start = now + base::TimeDelta::FromDays(11); |
| 2143 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(12); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2144 | return load_timing; |
| 2145 | } |
| 2146 | |
| 2147 | // Same as above, but in the case of a reused socket. |
| 2148 | LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now, |
| 2149 | bool used_proxy) { |
| 2150 | LoadTimingInfo load_timing; |
| 2151 | load_timing.socket_log_id = 1; |
| 2152 | load_timing.socket_reused = true; |
| 2153 | |
| 2154 | if (used_proxy) { |
| 2155 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 2156 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 2157 | } |
| 2158 | |
| 2159 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 2160 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 2161 | load_timing.receive_headers_start = now + base::TimeDelta::FromDays(11); |
| 2162 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(12); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2163 | return load_timing; |
| 2164 | } |
| 2165 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2166 | LoadTimingInfo RunURLRequestInterceptorLoadTimingTest( |
| 2167 | const LoadTimingInfo& job_load_timing, |
| 2168 | const URLRequestContext& context, |
| 2169 | MockURLRequestInterceptor* interceptor) { |
| 2170 | interceptor->set_intercept_main_request(true); |
| 2171 | interceptor->set_main_request_load_timing_info(job_load_timing); |
| 2172 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2173 | std::unique_ptr<URLRequest> req( |
| 2174 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 2175 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2176 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2177 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2178 | |
| 2179 | LoadTimingInfo resulting_load_timing; |
| 2180 | req->GetLoadTimingInfo(&resulting_load_timing); |
| 2181 | |
| 2182 | // None of these should be modified by the URLRequest. |
| 2183 | EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); |
| 2184 | EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); |
| 2185 | EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); |
| 2186 | EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); |
Andrew Comminos | 1f2ff1cc | 2018-12-14 05:22:38 | [diff] [blame] | 2187 | EXPECT_EQ(job_load_timing.receive_headers_start, |
| 2188 | resulting_load_timing.receive_headers_start); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2189 | EXPECT_EQ(job_load_timing.receive_headers_end, |
| 2190 | resulting_load_timing.receive_headers_end); |
caseq | e8340bc9 | 2016-04-20 00:02:57 | [diff] [blame] | 2191 | EXPECT_EQ(job_load_timing.push_start, resulting_load_timing.push_start); |
| 2192 | EXPECT_EQ(job_load_timing.push_end, resulting_load_timing.push_end); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2193 | |
| 2194 | return resulting_load_timing; |
| 2195 | } |
| 2196 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2197 | // Basic test that the intercept + load timing tests work. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2198 | TEST_F(URLRequestInterceptorTest, InterceptLoadTiming) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2199 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2200 | LoadTimingInfo job_load_timing = |
| 2201 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); |
| 2202 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2203 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2204 | RunURLRequestInterceptorLoadTimingTest( |
| 2205 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2206 | |
| 2207 | // Nothing should have been changed by the URLRequest. |
| 2208 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 2209 | load_timing_result.proxy_resolve_start); |
| 2210 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 2211 | load_timing_result.proxy_resolve_end); |
| 2212 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 2213 | load_timing_result.connect_timing.dns_start); |
| 2214 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 2215 | load_timing_result.connect_timing.dns_end); |
| 2216 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 2217 | load_timing_result.connect_timing.connect_start); |
| 2218 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 2219 | load_timing_result.connect_timing.connect_end); |
| 2220 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 2221 | load_timing_result.connect_timing.ssl_start); |
| 2222 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 2223 | load_timing_result.connect_timing.ssl_end); |
| 2224 | |
| 2225 | // Redundant sanity check. |
| 2226 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2227 | } |
| 2228 | |
| 2229 | // Another basic test, with proxy and SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2230 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2231 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2232 | LoadTimingInfo job_load_timing = |
| 2233 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); |
| 2234 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2235 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2236 | RunURLRequestInterceptorLoadTimingTest( |
| 2237 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2238 | |
| 2239 | // Nothing should have been changed by the URLRequest. |
| 2240 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 2241 | load_timing_result.proxy_resolve_start); |
| 2242 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 2243 | load_timing_result.proxy_resolve_end); |
| 2244 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 2245 | load_timing_result.connect_timing.dns_start); |
| 2246 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 2247 | load_timing_result.connect_timing.dns_end); |
| 2248 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 2249 | load_timing_result.connect_timing.connect_start); |
| 2250 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 2251 | load_timing_result.connect_timing.connect_end); |
| 2252 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 2253 | load_timing_result.connect_timing.ssl_start); |
| 2254 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 2255 | load_timing_result.connect_timing.ssl_end); |
| 2256 | |
| 2257 | // Redundant sanity check. |
| 2258 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2259 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 2260 | } |
| 2261 | |
| 2262 | // Make sure that URLRequest correctly adjusts proxy times when they're before |
| 2263 | // |request_start|, due to already having a connected socket. This happens in |
[email protected] | cf4cae3 | 2014-05-27 00:39:10 | [diff] [blame] | 2264 | // the case of reusing a SPDY session. The connected socket is not considered |
| 2265 | // reused in this test (May be a preconnect). |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2266 | // |
| 2267 | // To mix things up from the test above, assumes DNS times but no SSL times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2268 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyProxyResolution) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2269 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2270 | LoadTimingInfo job_load_timing = |
| 2271 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); |
| 2272 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); |
| 2273 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); |
| 2274 | job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); |
| 2275 | job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); |
| 2276 | job_load_timing.connect_timing.connect_start = |
| 2277 | now - base::TimeDelta::FromDays(2); |
| 2278 | job_load_timing.connect_timing.connect_end = |
| 2279 | now - base::TimeDelta::FromDays(1); |
| 2280 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2281 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2282 | RunURLRequestInterceptorLoadTimingTest( |
| 2283 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2284 | |
| 2285 | // Proxy times, connect times, and DNS times should all be replaced with |
| 2286 | // request_start. |
| 2287 | EXPECT_EQ(load_timing_result.request_start, |
| 2288 | load_timing_result.proxy_resolve_start); |
| 2289 | EXPECT_EQ(load_timing_result.request_start, |
| 2290 | load_timing_result.proxy_resolve_end); |
| 2291 | EXPECT_EQ(load_timing_result.request_start, |
| 2292 | load_timing_result.connect_timing.dns_start); |
| 2293 | EXPECT_EQ(load_timing_result.request_start, |
| 2294 | load_timing_result.connect_timing.dns_end); |
| 2295 | EXPECT_EQ(load_timing_result.request_start, |
| 2296 | load_timing_result.connect_timing.connect_start); |
| 2297 | EXPECT_EQ(load_timing_result.request_start, |
| 2298 | load_timing_result.connect_timing.connect_end); |
| 2299 | |
| 2300 | // Other times should have been left null. |
| 2301 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2302 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 2303 | } |
| 2304 | |
| 2305 | // Same as above, but in the reused case. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2306 | TEST_F(URLRequestInterceptorTest, |
| 2307 | InterceptLoadTimingEarlyProxyResolutionReused) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2308 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2309 | LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); |
| 2310 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); |
| 2311 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); |
| 2312 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2313 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2314 | RunURLRequestInterceptorLoadTimingTest( |
| 2315 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2316 | |
| 2317 | // Proxy times and connect times should all be replaced with request_start. |
| 2318 | EXPECT_EQ(load_timing_result.request_start, |
| 2319 | load_timing_result.proxy_resolve_start); |
| 2320 | EXPECT_EQ(load_timing_result.request_start, |
| 2321 | load_timing_result.proxy_resolve_end); |
| 2322 | |
| 2323 | // Other times should have been left null. |
| 2324 | TestLoadTimingReusedWithProxy(load_timing_result); |
| 2325 | } |
| 2326 | |
| 2327 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2328 | // |request_start|, due to reusing a connected socket. The connected socket is |
| 2329 | // not considered reused in this test (May be a preconnect). |
| 2330 | // |
| 2331 | // To mix things up, the request has SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2332 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnect) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2333 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2334 | LoadTimingInfo job_load_timing = |
| 2335 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); |
| 2336 | job_load_timing.connect_timing.connect_start = |
| 2337 | now - base::TimeDelta::FromDays(1); |
| 2338 | job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); |
| 2339 | job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); |
| 2340 | job_load_timing.connect_timing.connect_end = |
| 2341 | now - base::TimeDelta::FromDays(4); |
| 2342 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2343 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2344 | RunURLRequestInterceptorLoadTimingTest( |
| 2345 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2346 | |
| 2347 | // Connect times, and SSL times should be replaced with request_start. |
| 2348 | EXPECT_EQ(load_timing_result.request_start, |
| 2349 | load_timing_result.connect_timing.connect_start); |
| 2350 | EXPECT_EQ(load_timing_result.request_start, |
| 2351 | load_timing_result.connect_timing.ssl_start); |
| 2352 | EXPECT_EQ(load_timing_result.request_start, |
| 2353 | load_timing_result.connect_timing.ssl_end); |
| 2354 | EXPECT_EQ(load_timing_result.request_start, |
| 2355 | load_timing_result.connect_timing.connect_end); |
| 2356 | |
| 2357 | // Other times should have been left null. |
| 2358 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); |
| 2359 | } |
| 2360 | |
| 2361 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2362 | // |request_start|, due to reusing a connected socket in the case that there |
| 2363 | // are also proxy times. The connected socket is not considered reused in this |
| 2364 | // test (May be a preconnect). |
| 2365 | // |
| 2366 | // In this test, there are no SSL or DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2367 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnectWithProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2368 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2369 | LoadTimingInfo job_load_timing = |
| 2370 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); |
| 2371 | job_load_timing.connect_timing.connect_start = |
| 2372 | now - base::TimeDelta::FromDays(1); |
| 2373 | job_load_timing.connect_timing.connect_end = |
| 2374 | now - base::TimeDelta::FromDays(2); |
| 2375 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2376 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2377 | RunURLRequestInterceptorLoadTimingTest( |
| 2378 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2379 | |
| 2380 | // Connect times should be replaced with proxy_resolve_end. |
| 2381 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2382 | load_timing_result.connect_timing.connect_start); |
| 2383 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2384 | load_timing_result.connect_timing.connect_end); |
| 2385 | |
| 2386 | // Other times should have been left null. |
| 2387 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2388 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 2389 | } |
| 2390 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2391 | // Check that two different URL requests have different identifiers. |
| 2392 | TEST_F(URLRequestTest, Identifiers) { |
| 2393 | TestDelegate d; |
| 2394 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2395 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2396 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2397 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2398 | std::unique_ptr<URLRequest> other_req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2399 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2400 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2401 | |
mmenke | 19378d2 | 2014-09-09 04:12:59 | [diff] [blame] | 2402 | ASSERT_NE(req->identifier(), other_req->identifier()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2403 | } |
| 2404 | |
blundell | b8163592f | 2015-12-16 14:22:42 | [diff] [blame] | 2405 | #if defined(OS_IOS) |
| 2406 | // TODO(droger): Check that a failure to connect to the proxy is reported to |
| 2407 | // the network delegate. crbug.com/496743 |
| 2408 | #define MAYBE_NetworkDelegateProxyError DISABLED_NetworkDelegateProxyError |
| 2409 | #else |
| 2410 | #define MAYBE_NetworkDelegateProxyError NetworkDelegateProxyError |
| 2411 | #endif |
| 2412 | TEST_F(URLRequestTest, MAYBE_NetworkDelegateProxyError) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2413 | MockHostResolver host_resolver; |
| 2414 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 2415 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2416 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2417 | TestURLRequestContextWithProxy context("myproxy:70", &network_delegate); |
| 2418 | |
| 2419 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2420 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2421 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2422 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2423 | req->set_method("GET"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2424 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2425 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2426 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2427 | |
| 2428 | // Check we see a failed request. |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 2429 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 2430 | EXPECT_FALSE(req->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2431 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2432 | |
| 2433 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2434 | EXPECT_THAT(network_delegate.last_error(), |
| 2435 | IsError(ERR_PROXY_CONNECTION_FAILED)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2436 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2437 | } |
| 2438 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2439 | // Make sure that NetworkDelegate::NotifyCompleted is called if |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2440 | // content is empty. |
| 2441 | TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) { |
| 2442 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2443 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2444 | GURL("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2445 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2446 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2447 | EXPECT_EQ("", d.data_received()); |
| 2448 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 2449 | } |
| 2450 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2451 | // Make sure that SetPriority actually sets the URLRequest's priority |
| 2452 | // correctly, both before and after start. |
| 2453 | TEST_F(URLRequestTest, SetPriorityBasic) { |
| 2454 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2455 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2456 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2457 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2458 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2459 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2460 | req->SetPriority(LOW); |
| 2461 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2462 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2463 | req->Start(); |
| 2464 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2465 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2466 | req->SetPriority(MEDIUM); |
| 2467 | EXPECT_EQ(MEDIUM, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | // Make sure that URLRequest calls SetPriority on a job before calling |
| 2471 | // Start on it. |
| 2472 | TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) { |
| 2473 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2474 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2475 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2476 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2477 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2478 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2479 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2480 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2481 | req.get(), &default_network_delegate_, &job_priority)); |
| 2482 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
| 2483 | EXPECT_EQ(DEFAULT_PRIORITY, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2484 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2485 | req->SetPriority(LOW); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2486 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2487 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2488 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2489 | } |
| 2490 | |
| 2491 | // Make sure that URLRequest passes on its priority updates to its |
| 2492 | // job. |
| 2493 | TEST_F(URLRequestTest, SetJobPriority) { |
| 2494 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2495 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2496 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2497 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2498 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2499 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2500 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2501 | req.get(), &default_network_delegate_, &job_priority)); |
| 2502 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2503 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2504 | req->SetPriority(LOW); |
| 2505 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2506 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2507 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2508 | req->SetPriority(MEDIUM); |
| 2509 | EXPECT_EQ(MEDIUM, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2510 | EXPECT_EQ(MEDIUM, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2511 | } |
| 2512 | |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2513 | // Setting the IGNORE_LIMITS load flag should be okay if the priority |
| 2514 | // is MAXIMUM_PRIORITY. |
| 2515 | TEST_F(URLRequestTest, PriorityIgnoreLimits) { |
| 2516 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2517 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2518 | GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY, &d, |
| 2519 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2520 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2521 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2522 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2523 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2524 | req.get(), &default_network_delegate_, &job_priority)); |
| 2525 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2526 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2527 | req->SetLoadFlags(LOAD_IGNORE_LIMITS); |
| 2528 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2529 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2530 | req->SetPriority(MAXIMUM_PRIORITY); |
| 2531 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2532 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2533 | req->Start(); |
| 2534 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2535 | EXPECT_EQ(MAXIMUM_PRIORITY, job_priority); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2536 | } |
| 2537 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2538 | namespace { |
| 2539 | |
| 2540 | // Less verbose way of running a simple testserver for the tests below. |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2541 | class HttpTestServer : public EmbeddedTestServer { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2542 | public: |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2543 | explicit HttpTestServer(const base::FilePath& document_root) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2544 | AddDefaultHandlers(document_root); |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2545 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2546 | |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2547 | HttpTestServer() { AddDefaultHandlers(base::FilePath()); } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2548 | }; |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2549 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2550 | } // namespace |
| 2551 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2552 | TEST_F(URLRequestTest, DelayedCookieCallback) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2553 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2554 | ASSERT_TRUE(test_server.Start()); |
| 2555 | |
| 2556 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2557 | std::unique_ptr<DelayedCookieMonster> delayed_cm(new DelayedCookieMonster()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2558 | context.set_cookie_store(delayed_cm.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2559 | |
| 2560 | // Set up a cookie. |
| 2561 | { |
| 2562 | TestNetworkDelegate network_delegate; |
| 2563 | context.set_network_delegate(&network_delegate); |
| 2564 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2565 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2566 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2567 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2568 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2569 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2570 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2571 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2572 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2573 | } |
| 2574 | |
| 2575 | // Verify that the cookie is set. |
| 2576 | { |
| 2577 | TestNetworkDelegate network_delegate; |
| 2578 | context.set_network_delegate(&network_delegate); |
| 2579 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2580 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2581 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2582 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2583 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2584 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2585 | |
| 2586 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2587 | != std::string::npos); |
| 2588 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2589 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2590 | } |
| 2591 | } |
| 2592 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2593 | TEST_F(URLRequestTest, DoNotSendCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2594 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2595 | ASSERT_TRUE(test_server.Start()); |
| 2596 | |
| 2597 | // Set up a cookie. |
| 2598 | { |
| 2599 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2600 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2601 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2602 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2603 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2604 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2605 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2606 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2607 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2608 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2609 | } |
| 2610 | |
| 2611 | // Verify that the cookie is set. |
| 2612 | { |
| 2613 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2614 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2615 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2616 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2617 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2618 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2619 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2620 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2621 | |
| 2622 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2623 | != std::string::npos); |
| 2624 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2625 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2626 | } |
| 2627 | |
| 2628 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 2629 | { |
| 2630 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2631 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2632 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2633 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2634 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2635 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2636 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
| 2637 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2638 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2639 | |
| 2640 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2641 | == std::string::npos); |
| 2642 | |
| 2643 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
| 2644 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2645 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2646 | } |
| 2647 | } |
| 2648 | |
| 2649 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2650 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2651 | ASSERT_TRUE(test_server.Start()); |
| 2652 | |
| 2653 | // Set up a cookie. |
| 2654 | { |
| 2655 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2656 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2657 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2658 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2659 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2660 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2661 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2662 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2663 | |
| 2664 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2665 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2666 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2667 | } |
| 2668 | |
| 2669 | // Try to set-up another cookie and update the previous cookie. |
| 2670 | { |
| 2671 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2672 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2673 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2674 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2675 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2676 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2677 | req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES); |
| 2678 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2679 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2680 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2681 | |
| 2682 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
| 2683 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2684 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2685 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2686 | } |
| 2687 | |
| 2688 | // Verify the cookies weren't saved or updated. |
| 2689 | { |
| 2690 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2691 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2692 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2693 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2694 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2695 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2696 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2697 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2698 | |
| 2699 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2700 | == std::string::npos); |
| 2701 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2702 | != std::string::npos); |
| 2703 | |
| 2704 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2705 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2706 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2707 | } |
| 2708 | } |
| 2709 | |
| 2710 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2711 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2712 | ASSERT_TRUE(test_server.Start()); |
| 2713 | |
| 2714 | // Set up a cookie. |
| 2715 | { |
| 2716 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2717 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2718 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2719 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2720 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2721 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2722 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2723 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2724 | |
| 2725 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2726 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2727 | } |
| 2728 | |
| 2729 | // Verify that the cookie is set. |
| 2730 | { |
| 2731 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2732 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2733 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2734 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2735 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2736 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2737 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2738 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2739 | |
| 2740 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2741 | != std::string::npos); |
| 2742 | |
| 2743 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2744 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2745 | TestNetLogEntry::List entries; |
| 2746 | net_log_.GetEntries(&entries); |
| 2747 | for (const auto& entry : entries) { |
| 2748 | EXPECT_NE(entry.type, |
| 2749 | NetLogEventType::COOKIE_GET_BLOCKED_BY_NETWORK_DELEGATE); |
| 2750 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2751 | } |
| 2752 | |
| 2753 | // Verify that the cookie isn't sent. |
| 2754 | { |
| 2755 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2756 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2757 | TestDelegate d; |
| 2758 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2759 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2760 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2761 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2762 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2763 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2764 | |
| 2765 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2766 | == std::string::npos); |
| 2767 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2768 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2769 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2770 | TestNetLogEntry::List entries; |
| 2771 | net_log_.GetEntries(&entries); |
| 2772 | ExpectLogContainsSomewhereAfter( |
| 2773 | entries, 0, NetLogEventType::COOKIE_GET_BLOCKED_BY_NETWORK_DELEGATE, |
| 2774 | NetLogEventPhase::NONE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2775 | } |
| 2776 | } |
| 2777 | |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 2778 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 2779 | #if defined(OS_IOS) |
| 2780 | #define MAYBE_DoNotSaveCookies_ViaPolicy FLAKY_DoNotSaveCookies_ViaPolicy |
| 2781 | #else |
| 2782 | #define MAYBE_DoNotSaveCookies_ViaPolicy DoNotSaveCookies_ViaPolicy |
| 2783 | #endif |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2784 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2785 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2786 | ASSERT_TRUE(test_server.Start()); |
| 2787 | |
| 2788 | // Set up a cookie. |
| 2789 | { |
| 2790 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2791 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2792 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2793 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2794 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2795 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2796 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2797 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2798 | |
| 2799 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2800 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2801 | TestNetLogEntry::List entries; |
| 2802 | net_log_.GetEntries(&entries); |
| 2803 | for (const auto& entry : entries) { |
| 2804 | EXPECT_NE(entry.type, |
| 2805 | NetLogEventType::COOKIE_SET_BLOCKED_BY_NETWORK_DELEGATE); |
| 2806 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2807 | } |
| 2808 | |
| 2809 | // Try to set-up another cookie and update the previous cookie. |
| 2810 | { |
| 2811 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2812 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2813 | TestDelegate d; |
| 2814 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2815 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2816 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2817 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2818 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2819 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2820 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2821 | |
| 2822 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2823 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
Helen Li | 830d5708d6 | 2018-08-22 16:29:18 | [diff] [blame] | 2824 | TestNetLogEntry::List entries; |
| 2825 | net_log_.GetEntries(&entries); |
| 2826 | ExpectLogContainsSomewhereAfter( |
| 2827 | entries, 0, NetLogEventType::COOKIE_SET_BLOCKED_BY_NETWORK_DELEGATE, |
| 2828 | NetLogEventPhase::NONE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2829 | } |
| 2830 | |
| 2831 | // Verify the cookies weren't saved or updated. |
| 2832 | { |
| 2833 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2834 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2835 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2836 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2837 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2838 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2839 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2840 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2841 | |
| 2842 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2843 | == std::string::npos); |
| 2844 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2845 | != std::string::npos); |
| 2846 | |
| 2847 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2848 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2849 | } |
| 2850 | } |
| 2851 | |
| 2852 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2853 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2854 | ASSERT_TRUE(test_server.Start()); |
| 2855 | |
| 2856 | // Set up an empty cookie. |
| 2857 | { |
| 2858 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2859 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2860 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2861 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2862 | test_server.GetURL("/set-cookie"), DEFAULT_PRIORITY, &d, |
| 2863 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2864 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2865 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2866 | |
| 2867 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2868 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2869 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2870 | } |
| 2871 | } |
| 2872 | |
| 2873 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2874 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2875 | ASSERT_TRUE(test_server.Start()); |
| 2876 | |
| 2877 | // Set up a cookie. |
| 2878 | { |
| 2879 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2880 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2881 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2882 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2883 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2884 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2885 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2886 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2887 | |
| 2888 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2889 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2890 | } |
| 2891 | |
| 2892 | // Verify that the cookie is set. |
| 2893 | { |
| 2894 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2895 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2896 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2897 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2898 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2899 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2900 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2901 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2902 | |
| 2903 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2904 | != std::string::npos); |
| 2905 | |
| 2906 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2907 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2908 | } |
| 2909 | |
| 2910 | // Verify that the cookie isn't sent. |
| 2911 | { |
| 2912 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2913 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2914 | TestDelegate d; |
| 2915 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2916 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2917 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2918 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2919 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2920 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2921 | |
| 2922 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2923 | == std::string::npos); |
| 2924 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2925 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2926 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2927 | } |
| 2928 | } |
| 2929 | |
| 2930 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2931 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2932 | ASSERT_TRUE(test_server.Start()); |
| 2933 | |
| 2934 | // Set up a cookie. |
| 2935 | { |
| 2936 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2937 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2938 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2939 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2940 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2941 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2942 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2943 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2944 | |
| 2945 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2946 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2947 | } |
| 2948 | |
| 2949 | // Try to set-up another cookie and update the previous cookie. |
| 2950 | { |
| 2951 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2952 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2953 | TestDelegate d; |
| 2954 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2955 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2956 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2957 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2958 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2959 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2960 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2961 | |
| 2962 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2963 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2964 | } |
| 2965 | |
| 2966 | // Verify the cookies weren't saved or updated. |
| 2967 | { |
| 2968 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2969 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2970 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2971 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2972 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2973 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2974 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2975 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2976 | |
| 2977 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2978 | == std::string::npos); |
| 2979 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2980 | != std::string::npos); |
| 2981 | |
| 2982 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2983 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2984 | } |
| 2985 | } |
| 2986 | |
mkwst | c5fa776 | 2016-03-28 09:28:23 | [diff] [blame] | 2987 | TEST_F(URLRequestTest, SameSiteCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2988 | HttpTestServer test_server; |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2989 | ASSERT_TRUE(test_server.Start()); |
| 2990 | |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2991 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 2992 | default_context().set_network_delegate(&network_delegate); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2993 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2994 | const std::string kHost = "example.test"; |
| 2995 | const std::string kSubHost = "subdomain.example.test"; |
| 2996 | const std::string kCrossHost = "cross-origin.test"; |
| 2997 | |
| 2998 | // Set up two 'SameSite' cookies on 'example.test' |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2999 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3000 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3001 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3002 | test_server.GetURL(kHost, |
| 3003 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 3004 | "LaxSameSiteCookie=1;SameSite=Lax"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3005 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3006 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3007 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3008 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3009 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3010 | EXPECT_EQ(2, network_delegate.set_cookie_count()); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3011 | } |
| 3012 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3013 | // Verify that both cookies are sent for same-site requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3014 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3015 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3016 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3017 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3018 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3019 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3020 | req->set_initiator(url::Origin::Create(test_server.GetURL(kHost, "/"))); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3021 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3022 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3023 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3024 | EXPECT_NE(std::string::npos, |
| 3025 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3026 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3027 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3028 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3029 | } |
| 3030 | |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3031 | // Verify that both cookies are sent when the request has no initiator (can |
| 3032 | // happen for main frame browser-initiated navigations). |
| 3033 | { |
| 3034 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3035 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3036 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3037 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3038 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3039 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3040 | d.RunUntilComplete(); |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3041 | |
| 3042 | EXPECT_NE(std::string::npos, |
| 3043 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3044 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 3045 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3046 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3047 | } |
| 3048 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3049 | // Verify that both cookies are sent for same-registrable-domain requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3050 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3051 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3052 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3053 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3054 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3055 | req->set_site_for_cookies(test_server.GetURL(kSubHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3056 | req->set_initiator(url::Origin::Create(test_server.GetURL(kSubHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3057 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3058 | d.RunUntilComplete(); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3059 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3060 | EXPECT_NE(std::string::npos, |
| 3061 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3062 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3063 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3064 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3065 | } |
| 3066 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3067 | // Verify that neither cookie is not sent for cross-site requests. |
| 3068 | { |
| 3069 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3070 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3071 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3072 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3073 | req->set_site_for_cookies(test_server.GetURL(kCrossHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3074 | req->set_initiator( |
| 3075 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3076 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3077 | d.RunUntilComplete(); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3078 | |
| 3079 | EXPECT_EQ(std::string::npos, |
| 3080 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3081 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 3082 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3083 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3084 | } |
| 3085 | |
| 3086 | // Verify that the lax cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3087 | // method is "safe". |
| 3088 | { |
| 3089 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3090 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3091 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3092 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3093 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3094 | req->set_initiator( |
| 3095 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3096 | req->set_method("GET"); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3097 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3098 | d.RunUntilComplete(); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3099 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3100 | EXPECT_EQ(std::string::npos, |
| 3101 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3102 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3103 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3104 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3105 | } |
| 3106 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3107 | // Verify that neither cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3108 | // method is unsafe (e.g. POST). |
| 3109 | { |
| 3110 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3111 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3112 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3113 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3114 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3115 | req->set_initiator( |
| 3116 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3117 | req->set_method("POST"); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3118 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3119 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3120 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3121 | EXPECT_EQ(std::string::npos, |
| 3122 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3123 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3124 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3125 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3126 | } |
| 3127 | } |
| 3128 | |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3129 | // Tests that __Secure- cookies can't be set on non-secure origins. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3130 | TEST_F(URLRequestTest, SecureCookiePrefixOnNonsecureOrigin) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3131 | EmbeddedTestServer http_server; |
| 3132 | http_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3133 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3134 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3135 | https_server.AddDefaultHandlers( |
| 3136 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3137 | ASSERT_TRUE(http_server.Start()); |
| 3138 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3139 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3140 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3141 | TestURLRequestContext context(true); |
| 3142 | context.set_network_delegate(&network_delegate); |
| 3143 | context.Init(); |
| 3144 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3145 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3146 | { |
| 3147 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3148 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3149 | http_server.GetURL("/set-cookie?__Secure-nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3150 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3151 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3152 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3153 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3154 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3155 | } |
| 3156 | |
| 3157 | // Verify that the cookie is not set. |
| 3158 | { |
| 3159 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3160 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3161 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3162 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3163 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3164 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3165 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3166 | EXPECT_EQ(d.data_received().find("__Secure-nonsecure-origin=1"), |
| 3167 | std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3168 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3169 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3170 | } |
| 3171 | } |
| 3172 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3173 | TEST_F(URLRequestTest, SecureCookiePrefixNonsecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3174 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3175 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3176 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3177 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3178 | |
| 3179 | TestNetworkDelegate network_delegate; |
| 3180 | TestURLRequestContext context(true); |
| 3181 | context.set_network_delegate(&network_delegate); |
| 3182 | context.Init(); |
| 3183 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3184 | // Try to set a non-Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3185 | { |
| 3186 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3187 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 3188 | https_server.GetURL("/set-cookie?__Secure-foo=1"), DEFAULT_PRIORITY, &d, |
| 3189 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3190 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3191 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3192 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3193 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3194 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3195 | } |
| 3196 | |
| 3197 | // Verify that the cookie is not set. |
| 3198 | { |
| 3199 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3200 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3201 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3202 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3203 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3204 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3205 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3206 | EXPECT_EQ(d.data_received().find("__Secure-foo=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3207 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3208 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3209 | } |
| 3210 | } |
| 3211 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3212 | TEST_F(URLRequestTest, SecureCookiePrefixSecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3213 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3214 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3215 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3216 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3217 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3218 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3219 | TestURLRequestContext context(true); |
| 3220 | context.set_network_delegate(&network_delegate); |
| 3221 | context.Init(); |
| 3222 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3223 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3224 | { |
| 3225 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3226 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3227 | https_server.GetURL("/set-cookie?__Secure-bar=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3228 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3229 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3230 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3231 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3232 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3233 | } |
| 3234 | |
| 3235 | // Verify that the cookie is set. |
| 3236 | { |
| 3237 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3238 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3239 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3240 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3241 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3242 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3243 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3244 | EXPECT_NE(d.data_received().find("__Secure-bar=1"), std::string::npos); |
| 3245 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3246 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3247 | } |
| 3248 | } |
| 3249 | |
| 3250 | // Tests that secure cookies can't be set on non-secure origins if strict secure |
| 3251 | // cookies are enabled. |
| 3252 | TEST_F(URLRequestTest, StrictSecureCookiesOnNonsecureOrigin) { |
| 3253 | EmbeddedTestServer http_server; |
| 3254 | http_server.AddDefaultHandlers( |
| 3255 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3256 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3257 | https_server.AddDefaultHandlers( |
| 3258 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3259 | ASSERT_TRUE(http_server.Start()); |
| 3260 | ASSERT_TRUE(https_server.Start()); |
| 3261 | |
Matt Menke | be9b6eb | 2018-10-18 12:11:44 | [diff] [blame] | 3262 | TestNetworkDelegate network_delegate; |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3263 | TestURLRequestContext context(true); |
| 3264 | context.set_network_delegate(&network_delegate); |
| 3265 | context.Init(); |
| 3266 | |
| 3267 | // Try to set a Secure cookie, with experimental features enabled. |
| 3268 | { |
| 3269 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3270 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3271 | http_server.GetURL("/set-cookie?nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3272 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3273 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3274 | d.RunUntilComplete(); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3275 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3276 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3277 | } |
| 3278 | |
| 3279 | // Verify that the cookie is not set. |
| 3280 | { |
| 3281 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3282 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3283 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3284 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3285 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3286 | d.RunUntilComplete(); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3287 | |
| 3288 | EXPECT_EQ(d.data_received().find("nonsecure-origin=1"), std::string::npos); |
| 3289 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3290 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3291 | } |
| 3292 | } |
| 3293 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3294 | // The parameter is true for same-site and false for cross-site requests. |
| 3295 | class URLRequestTestParameterizedSameSite |
| 3296 | : public URLRequestTest, |
| 3297 | public ::testing::WithParamInterface<bool> { |
| 3298 | protected: |
| 3299 | URLRequestTestParameterizedSameSite() { |
| 3300 | auto params = std::make_unique<HttpNetworkSession::Params>(); |
| 3301 | params->ignore_certificate_errors = true; |
| 3302 | context_.set_http_network_session_params(std::move(params)); |
| 3303 | context_.set_network_delegate(&network_delegate_); |
| 3304 | https_server_.AddDefaultHandlers( |
| 3305 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3306 | EXPECT_TRUE(https_server_.Start()); |
| 3307 | } |
| 3308 | |
| 3309 | // To be called after configuration of |context_| has been finalized. |
| 3310 | void InitContext() { context_.Init(); } |
| 3311 | |
| 3312 | const std::string kHost_ = "example.test"; |
| 3313 | const std::string kCrossHost_ = "cross-site.test"; |
| 3314 | TestURLRequestContext context_{true}; |
| 3315 | TestNetworkDelegate network_delegate_; |
| 3316 | base::HistogramTester histograms_; |
| 3317 | EmbeddedTestServer https_server_{EmbeddedTestServer::TYPE_HTTPS}; |
| 3318 | }; |
| 3319 | |
| 3320 | INSTANTIATE_TEST_CASE_P(URLRequestTest, |
| 3321 | URLRequestTestParameterizedSameSite, |
| 3322 | ::testing::Bool()); |
| 3323 | |
| 3324 | TEST_P(URLRequestTestParameterizedSameSite, CookieAgeMetrics) { |
| 3325 | const bool same_site = GetParam(); |
| 3326 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3327 | InitContext(); |
| 3328 | |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3329 | EmbeddedTestServer http_server; |
| 3330 | http_server.AddDefaultHandlers( |
| 3331 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3332 | ASSERT_TRUE(http_server.Start()); |
| 3333 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3334 | // Set two test cookies. |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3335 | { |
| 3336 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3337 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3338 | http_server.GetURL(kHost_, "/set-cookie?cookie=value&cookie2=value2"), |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3339 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3340 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3341 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3342 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3343 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3344 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 0); |
| 3345 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", 0); |
| 3346 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", 0); |
| 3347 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3348 | 0); |
| 3349 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3350 | 0); |
| 3351 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", 0); |
| 3352 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", 0); |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3353 | } |
| 3354 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3355 | // Make a secure request. |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3356 | { |
| 3357 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3358 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3359 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3360 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3361 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3362 | req->set_initiator( |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3363 | url::Origin::Create(https_server_.GetURL(kInitiatingHost, "/"))); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3364 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3365 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3366 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3367 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 0); |
| 3368 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", |
| 3369 | !same_site); |
| 3370 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", |
| 3371 | same_site); |
| 3372 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3373 | 0); |
| 3374 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3375 | 0); |
| 3376 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", |
| 3377 | same_site ? 0 : 2); |
| 3378 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", |
| 3379 | same_site ? 2 : 0); |
| 3380 | EXPECT_TRUE(d.data_received().find("cookie=value") != std::string::npos); |
| 3381 | EXPECT_TRUE(d.data_received().find("cookie2=value2") != std::string::npos); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3382 | } |
| 3383 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3384 | // Make a non-secure request. |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3385 | { |
| 3386 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3387 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3388 | http_server.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3389 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3390 | req->set_site_for_cookies(http_server.GetURL(kInitiatingHost, "/")); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3391 | req->set_initiator( |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3392 | url::Origin::Create(http_server.GetURL(kInitiatingHost, "/"))); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3393 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3394 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3395 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", |
| 3396 | !same_site); |
| 3397 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", |
| 3398 | same_site); |
| 3399 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", |
| 3400 | !same_site); |
| 3401 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", |
| 3402 | same_site); |
| 3403 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3404 | same_site ? 0 : 2); |
| 3405 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3406 | same_site ? 2 : 0); |
| 3407 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", |
| 3408 | same_site ? 0 : 2); |
| 3409 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", |
| 3410 | same_site ? 2 : 0); |
| 3411 | EXPECT_TRUE(d.data_received().find("cookie=value") != std::string::npos); |
| 3412 | EXPECT_TRUE(d.data_received().find("cookie2=value2") != std::string::npos); |
| 3413 | } |
| 3414 | } |
| 3415 | |
| 3416 | // Cookies with secure attribute (no HSTS) --> k1pSecureAttribute |
| 3417 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3418 | CookieNetworkSecurityMetricSecureAttribute) { |
| 3419 | const bool same_site = GetParam(); |
| 3420 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3421 | InitContext(); |
| 3422 | |
| 3423 | // Set cookies. |
| 3424 | { |
| 3425 | TestDelegate d; |
| 3426 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3427 | https_server_.GetURL(kHost_, |
| 3428 | "/set-cookie?session-cookie=value;Secure&" |
| 3429 | "longlived-cookie=value;Secure;domain=" + |
| 3430 | kHost_ + ";Max-Age=360000"), |
| 3431 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3432 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3433 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3434 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3435 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3436 | } |
| 3437 | |
| 3438 | // Verify that the cookies fall into the correct metrics bucket. |
| 3439 | { |
| 3440 | TestDelegate d; |
| 3441 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3442 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3443 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3444 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3445 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3446 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3447 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3448 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3449 | // Static cast of boolean required for MSVC 1911. |
| 3450 | histograms_.ExpectBucketCount( |
| 3451 | "Cookie.NetworkSecurity", |
| 3452 | static_cast<int>(CookieNetworkSecurity::k1pSecureAttribute) | |
| 3453 | static_cast<int>(!same_site), |
| 3454 | 2); |
| 3455 | } |
| 3456 | } |
| 3457 | |
| 3458 | // Short-lived host cookie --> k1pHSTSHostCookie |
| 3459 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3460 | CookieNetworkSecurityMetricShortlivedHostCookie) { |
| 3461 | const bool same_site = GetParam(); |
| 3462 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3463 | |
| 3464 | TransportSecurityState transport_security_state; |
| 3465 | transport_security_state.AddHSTS( |
| 3466 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3467 | false /* include_subdomains */); |
| 3468 | context_.set_transport_security_state(&transport_security_state); |
| 3469 | InitContext(); |
| 3470 | |
| 3471 | // Set cookie. |
| 3472 | { |
| 3473 | TestDelegate d; |
| 3474 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3475 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;Max-Age=3600"), |
| 3476 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3477 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3478 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3479 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3480 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3481 | } |
| 3482 | |
| 3483 | // Verify that the cookie falls into the correct metrics bucket. |
| 3484 | { |
| 3485 | TestDelegate d; |
| 3486 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3487 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3488 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3489 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3490 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3491 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3492 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3493 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3494 | // Static cast of boolean required for MSVC 1911. |
| 3495 | histograms_.ExpectBucketCount( |
| 3496 | "Cookie.NetworkSecurity", |
| 3497 | static_cast<int>(CookieNetworkSecurity::k1pHSTSHostCookie) | |
| 3498 | static_cast<int>(!same_site), |
| 3499 | 1); |
| 3500 | } |
| 3501 | } |
| 3502 | |
| 3503 | // Long-lived (either due to expiry or due to being a session cookie) host |
| 3504 | // cookies --> k1pExpiringHSTSHostCookie |
| 3505 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3506 | CookieNetworkSecurityMetricLonglivedHostCookie) { |
| 3507 | const bool same_site = GetParam(); |
| 3508 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3509 | |
| 3510 | TransportSecurityState transport_security_state; |
| 3511 | transport_security_state.AddHSTS( |
| 3512 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3513 | false /* include_subdomains */); |
| 3514 | context_.set_transport_security_state(&transport_security_state); |
| 3515 | InitContext(); |
| 3516 | |
| 3517 | // Set cookies. |
| 3518 | { |
| 3519 | TestDelegate d; |
| 3520 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3521 | https_server_.GetURL(kHost_, |
| 3522 | "/set-cookie?session-cookie=value&" |
| 3523 | "longlived-cookie=value;Max-Age=360000"), |
| 3524 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3525 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3526 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3527 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3528 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3529 | } |
| 3530 | |
| 3531 | // Verify that the cookies fall into the correct metrics bucket. |
| 3532 | { |
| 3533 | TestDelegate d; |
| 3534 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3535 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3536 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3537 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3538 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3539 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3540 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3541 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3542 | // Static cast of boolean required for MSVC 1911. |
| 3543 | histograms_.ExpectBucketCount( |
| 3544 | "Cookie.NetworkSecurity", |
| 3545 | static_cast<int>(CookieNetworkSecurity::k1pExpiringHSTSHostCookie) | |
| 3546 | static_cast<int>(!same_site), |
| 3547 | 2); |
| 3548 | } |
| 3549 | } |
| 3550 | |
| 3551 | // Domain cookie with HSTS subdomains with cookie expiry before HSTS expiry --> |
| 3552 | // k1pHSTSSubdomainsIncluded |
| 3553 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3554 | CookieNetworkSecurityMetricShortlivedDomainCookie) { |
| 3555 | const bool same_site = GetParam(); |
| 3556 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3557 | |
| 3558 | TransportSecurityState transport_security_state; |
| 3559 | transport_security_state.AddHSTS( |
| 3560 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3561 | true /* include_subdomains */); |
| 3562 | context_.set_transport_security_state(&transport_security_state); |
| 3563 | InitContext(); |
| 3564 | |
| 3565 | // Set cookie. |
| 3566 | { |
| 3567 | TestDelegate d; |
| 3568 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3569 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;domain=" + |
| 3570 | kHost_ + ";Max-Age=3600"), |
| 3571 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3572 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3573 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3574 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3575 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3576 | } |
| 3577 | |
| 3578 | // Verify that the cookie falls into the correct metrics bucket. |
| 3579 | { |
| 3580 | TestDelegate d; |
| 3581 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3582 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3583 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3584 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3585 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3586 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3587 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3588 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3589 | // Static cast of boolean required for MSVC 1911. |
| 3590 | histograms_.ExpectBucketCount( |
| 3591 | "Cookie.NetworkSecurity", |
| 3592 | static_cast<int>(CookieNetworkSecurity::k1pHSTSSubdomainsIncluded) | |
| 3593 | static_cast<int>(!same_site), |
| 3594 | 1); |
| 3595 | } |
| 3596 | } |
| 3597 | |
| 3598 | // Long-lived (either due to expiry or due to being a session cookie) domain |
| 3599 | // cookies with HSTS subdomains --> k1pExpiringHSTSSubdomainsIncluded |
| 3600 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3601 | CookieNetworkSecurityMetricLonglivedDomainCookie) { |
| 3602 | const bool same_site = GetParam(); |
| 3603 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3604 | |
| 3605 | TransportSecurityState transport_security_state; |
| 3606 | transport_security_state.AddHSTS( |
| 3607 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3608 | true /* include_subdomains */); |
| 3609 | context_.set_transport_security_state(&transport_security_state); |
| 3610 | InitContext(); |
| 3611 | |
| 3612 | // Set cookies. |
| 3613 | { |
| 3614 | TestDelegate d; |
| 3615 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3616 | https_server_.GetURL( |
| 3617 | kHost_, "/set-cookie?session-cookie=value;domain=" + kHost_ + "&" + |
| 3618 | "longlived-cookie=value;domain=" + kHost_ + |
| 3619 | ";Max-Age=360000"), |
| 3620 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3621 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3622 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3623 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3624 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3625 | } |
| 3626 | |
| 3627 | // Verify that the cookies fall into the correct metrics bucket. |
| 3628 | { |
| 3629 | TestDelegate d; |
| 3630 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3631 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3632 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3633 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3634 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3635 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3636 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3637 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3638 | // Static cast of boolean required for MSVC 1911. |
| 3639 | histograms_.ExpectBucketCount( |
| 3640 | "Cookie.NetworkSecurity", |
| 3641 | static_cast<int>( |
| 3642 | CookieNetworkSecurity::k1pExpiringHSTSSubdomainsIncluded) | |
| 3643 | static_cast<int>(!same_site), |
| 3644 | 2); |
| 3645 | } |
| 3646 | } |
| 3647 | |
| 3648 | // Domain cookie with HSTS subdomains not included --> k1pHSTSSpoofable |
| 3649 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3650 | CookieNetworkSecurityMetricSpoofableDomainCookie) { |
| 3651 | const bool same_site = GetParam(); |
| 3652 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3653 | |
| 3654 | TransportSecurityState transport_security_state; |
| 3655 | transport_security_state.AddHSTS( |
| 3656 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3657 | false /* include_subdomains */); |
| 3658 | context_.set_transport_security_state(&transport_security_state); |
| 3659 | InitContext(); |
| 3660 | |
| 3661 | // Set cookie. |
| 3662 | { |
| 3663 | TestDelegate d; |
| 3664 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3665 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;domain=" + |
| 3666 | kHost_ + ";Max-Age=3600"), |
| 3667 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3668 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3669 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3670 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3671 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3672 | } |
| 3673 | |
| 3674 | // Verify that the cookie falls into the correct metrics bucket. |
| 3675 | { |
| 3676 | TestDelegate d; |
| 3677 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3678 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3679 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3680 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3681 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3682 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3683 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3684 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3685 | // Static cast of boolean required for MSVC 1911. |
| 3686 | histograms_.ExpectBucketCount( |
| 3687 | "Cookie.NetworkSecurity", |
| 3688 | static_cast<int>(CookieNetworkSecurity::k1pHSTSSpoofable) | |
| 3689 | static_cast<int>(!same_site), |
| 3690 | 1); |
| 3691 | } |
| 3692 | } |
| 3693 | |
| 3694 | // Cookie without HSTS --> k1p(Non)SecureConnection |
| 3695 | TEST_P(URLRequestTestParameterizedSameSite, CookieNetworkSecurityMetricNoHSTS) { |
| 3696 | const bool same_site = GetParam(); |
| 3697 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3698 | InitContext(); |
| 3699 | |
| 3700 | EmbeddedTestServer http_server; |
| 3701 | http_server.AddDefaultHandlers( |
| 3702 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3703 | ASSERT_TRUE(http_server.Start()); |
| 3704 | |
| 3705 | // Set cookies. |
| 3706 | { |
| 3707 | TestDelegate d; |
| 3708 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3709 | https_server_.GetURL(kHost_, |
| 3710 | "/set-cookie?cookie=value;domain=" + kHost_ + |
| 3711 | ";Max-Age=3600&host-cookie=value"), |
| 3712 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3713 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3714 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3715 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3716 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3717 | } |
| 3718 | |
| 3719 | // Verify that the cookie falls into the correct metrics bucket. |
| 3720 | { |
| 3721 | TestDelegate d; |
| 3722 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3723 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3724 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3725 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3726 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3727 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3728 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3729 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3730 | // Static cast of boolean required for MSVC 1911. |
| 3731 | histograms_.ExpectBucketCount( |
| 3732 | "Cookie.NetworkSecurity", |
| 3733 | static_cast<int>(CookieNetworkSecurity::k1pSecureConnection) | |
| 3734 | static_cast<int>(!same_site), |
| 3735 | 2); |
| 3736 | } |
| 3737 | |
| 3738 | // Verify that the cookie falls into the correct metrics bucket. |
| 3739 | { |
| 3740 | TestDelegate d; |
| 3741 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3742 | http_server.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3743 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3744 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3745 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3746 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3747 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3748 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 4); |
| 3749 | // Static cast of boolean required for MSVC 1911. |
| 3750 | histograms_.ExpectBucketCount( |
| 3751 | "Cookie.NetworkSecurity", |
| 3752 | static_cast<int>(CookieNetworkSecurity::k1pSecureConnection) | |
| 3753 | static_cast<int>(!same_site), |
| 3754 | 2); |
| 3755 | // Static cast of boolean required for MSVC 1911. |
| 3756 | histograms_.ExpectBucketCount( |
| 3757 | "Cookie.NetworkSecurity", |
| 3758 | static_cast<int>(CookieNetworkSecurity::k1pNonsecureConnection) | |
| 3759 | static_cast<int>(!same_site), |
| 3760 | 2); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3761 | } |
| 3762 | } |
| 3763 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3764 | // Tests that a request is cancelled while entering suspend mode. Uses mocks |
| 3765 | // rather than a spawned test server because the connection used to talk to |
| 3766 | // the test server is affected by entering suspend mode on Android. |
| 3767 | TEST_F(URLRequestTest, CancelOnSuspend) { |
| 3768 | TestPowerMonitorSource* power_monitor_source = new TestPowerMonitorSource(); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3769 | base::PowerMonitor power_monitor(base::WrapUnique(power_monitor_source)); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3770 | |
| 3771 | URLRequestFailedJob::AddUrlHandler(); |
| 3772 | |
| 3773 | TestDelegate d; |
| 3774 | // Request that just hangs. |
| 3775 | GURL url(URLRequestFailedJob::GetMockHttpUrl(ERR_IO_PENDING)); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3776 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3777 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3778 | r->Start(); |
| 3779 | |
| 3780 | power_monitor_source->Suspend(); |
| 3781 | // Wait for the suspend notification to cause the request to fail. |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3782 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3783 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3784 | EXPECT_TRUE(d.request_failed()); |
| 3785 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 3786 | |
| 3787 | URLRequestFilter::GetInstance()->ClearHandlers(); |
| 3788 | |
| 3789 | // Shouldn't be needed, but just in case. |
| 3790 | power_monitor_source->Resume(); |
| 3791 | } |
| 3792 | |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3793 | // FixedDateNetworkDelegate swaps out the server's HTTP Date response header |
| 3794 | // value for the |fixed_date| argument given to the constructor. |
| 3795 | class FixedDateNetworkDelegate : public TestNetworkDelegate { |
| 3796 | public: |
| 3797 | explicit FixedDateNetworkDelegate(const std::string& fixed_date) |
| 3798 | : fixed_date_(fixed_date) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 3799 | ~FixedDateNetworkDelegate() override = default; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3800 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3801 | // NetworkDelegate implementation |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 3802 | int OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3803 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 3804 | CompletionOnceCallback callback, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3805 | const HttpResponseHeaders* original_response_headers, |
| 3806 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 3807 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3808 | |
| 3809 | private: |
| 3810 | std::string fixed_date_; |
| 3811 | |
| 3812 | DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); |
| 3813 | }; |
| 3814 | |
| 3815 | int FixedDateNetworkDelegate::OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3816 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 3817 | CompletionOnceCallback callback, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3818 | const HttpResponseHeaders* original_response_headers, |
| 3819 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3820 | GURL* allowed_unsafe_redirect_url) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3821 | HttpResponseHeaders* new_response_headers = |
| 3822 | new HttpResponseHeaders(original_response_headers->raw_headers()); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3823 | |
| 3824 | new_response_headers->RemoveHeader("Date"); |
| 3825 | new_response_headers->AddHeader("Date: " + fixed_date_); |
| 3826 | |
| 3827 | *override_response_headers = new_response_headers; |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 3828 | return TestNetworkDelegate::OnHeadersReceived( |
| 3829 | request, std::move(callback), original_response_headers, |
| 3830 | override_response_headers, allowed_unsafe_redirect_url); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3831 | } |
| 3832 | |
| 3833 | // Test that cookie expiration times are adjusted for server/client clock |
| 3834 | // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date |
| 3835 | // headers by defaulting to GMT. (crbug.com/135131) |
| 3836 | TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3837 | HttpTestServer test_server; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3838 | ASSERT_TRUE(test_server.Start()); |
| 3839 | |
| 3840 | // Set up an expired cookie. |
| 3841 | { |
| 3842 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3843 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3844 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3845 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3846 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3847 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3848 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3849 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3850 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3851 | } |
| 3852 | // Verify that the cookie is not set. |
| 3853 | { |
| 3854 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3855 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3856 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3857 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3858 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3859 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3860 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3861 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3862 | |
| 3863 | EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos); |
| 3864 | } |
| 3865 | // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier. |
| 3866 | { |
| 3867 | FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC"); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3868 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3869 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3870 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3871 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3872 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3873 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3874 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3875 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3876 | } |
| 3877 | // Verify that the cookie is set. |
| 3878 | { |
| 3879 | TestNetworkDelegate network_delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3880 | default_context().set_network_delegate(&network_delegate); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3881 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3882 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3883 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3884 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3885 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3886 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3887 | |
| 3888 | EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos); |
| 3889 | } |
| 3890 | } |
| 3891 | |
| 3892 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3893 | // Check that it is impossible to change the referrer in the extra headers of |
| 3894 | // an URLRequest. |
| 3895 | TEST_F(URLRequestTest, DoNotOverrideReferrer) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3896 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3897 | ASSERT_TRUE(test_server.Start()); |
| 3898 | |
| 3899 | // If extra headers contain referer and the request contains a referer, |
| 3900 | // only the latter shall be respected. |
| 3901 | { |
| 3902 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3903 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3904 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 3905 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3906 | req->SetReferrer("http://foo.com/"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3907 | |
| 3908 | HttpRequestHeaders headers; |
| 3909 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3910 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3911 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3912 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3913 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3914 | |
| 3915 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 3916 | } |
| 3917 | |
| 3918 | // If extra headers contain a referer but the request does not, no referer |
| 3919 | // shall be sent in the header. |
| 3920 | { |
| 3921 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3922 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3923 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 3924 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3925 | |
| 3926 | HttpRequestHeaders headers; |
| 3927 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3928 | req->SetExtraRequestHeaders(headers); |
| 3929 | req->SetLoadFlags(LOAD_VALIDATE_CACHE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3930 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3931 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3932 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3933 | |
| 3934 | EXPECT_EQ("None", d.data_received()); |
| 3935 | } |
| 3936 | } |
| 3937 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3938 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3939 | public: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 3940 | URLRequestTestHTTP() : test_server_(base::FilePath(kTestFilePath)) {} |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3941 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3942 | protected: |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3943 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 3944 | // |request_method| is the method to use for the initial request. |
| 3945 | // |redirect_method| is the method that is expected to be used for the second |
| 3946 | // request, after redirection. |
| 3947 | // If |include_data| is true, data is uploaded with the request. The |
| 3948 | // response body is expected to match it exactly, if and only if |
| 3949 | // |request_method| == |redirect_method|. |
| 3950 | void HTTPRedirectMethodTest(const GURL& redirect_url, |
| 3951 | const std::string& request_method, |
| 3952 | const std::string& redirect_method, |
| 3953 | bool include_data) { |
| 3954 | static const char kData[] = "hello world"; |
| 3955 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 3956 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3957 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3958 | req->set_method(request_method); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3959 | if (include_data) { |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 3960 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3961 | HttpRequestHeaders headers; |
| 3962 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 3963 | base::NumberToString(base::size(kData) - 1)); |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3964 | headers.SetHeader(HttpRequestHeaders::kContentType, "text/plain"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3965 | req->SetExtraRequestHeaders(headers); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3966 | } |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3967 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3968 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3969 | EXPECT_EQ(redirect_method, req->method()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3970 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3971 | if (include_data) { |
| 3972 | if (request_method == redirect_method) { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3973 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 3974 | HttpRequestHeaders::kContentLength)); |
| 3975 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 3976 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3977 | EXPECT_EQ(kData, d.data_received()); |
| 3978 | } else { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3979 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 3980 | HttpRequestHeaders::kContentLength)); |
| 3981 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 3982 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3983 | EXPECT_NE(kData, d.data_received()); |
| 3984 | } |
| 3985 | } |
| 3986 | if (HasFailure()) |
| 3987 | LOG(WARNING) << "Request method was: " << request_method; |
| 3988 | } |
| 3989 | |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 3990 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. It's also |
| 3991 | // used as the initial origin. |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3992 | // |request_method| is the method to use for the initial request. |
| 3993 | // |redirect_method| is the method that is expected to be used for the second |
| 3994 | // request, after redirection. |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 3995 | // |expected_origin_value| is the expected value for the Origin header after |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3996 | // redirection. If empty, expects that there will be no Origin header. |
| 3997 | void HTTPRedirectOriginHeaderTest(const GURL& redirect_url, |
| 3998 | const std::string& request_method, |
| 3999 | const std::string& redirect_method, |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4000 | const std::string& expected_origin_value) { |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4001 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4002 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4003 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4004 | req->set_method(request_method); |
| 4005 | req->SetExtraRequestHeaderByName(HttpRequestHeaders::kOrigin, |
| 4006 | redirect_url.GetOrigin().spec(), false); |
| 4007 | req->Start(); |
| 4008 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4009 | d.RunUntilComplete(); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4010 | |
| 4011 | EXPECT_EQ(redirect_method, req->method()); |
| 4012 | // Note that there is no check for request success here because, for |
| 4013 | // purposes of testing, the request very well may fail. For example, if the |
| 4014 | // test redirects to an HTTPS server from an HTTP origin, thus it is cross |
| 4015 | // origin, there is not an HTTPS server in this unit test framework, so the |
| 4016 | // request would fail. However, that's fine, as long as the request headers |
| 4017 | // are in order and pass the checks below. |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4018 | if (expected_origin_value.empty()) { |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4019 | EXPECT_FALSE( |
| 4020 | req->extra_request_headers().HasHeader(HttpRequestHeaders::kOrigin)); |
| 4021 | } else { |
| 4022 | std::string origin_header; |
| 4023 | EXPECT_TRUE(req->extra_request_headers().GetHeader( |
| 4024 | HttpRequestHeaders::kOrigin, &origin_header)); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4025 | EXPECT_EQ(expected_origin_value, origin_header); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4026 | } |
| 4027 | } |
| 4028 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4029 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4030 | const int kMsgSize = 20000; // multiple of 10 |
| 4031 | const int kIterations = 50; |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 4032 | char* uploadBytes = new char[kMsgSize+1]; |
| 4033 | char* ptr = uploadBytes; |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4034 | char marker = 'a'; |
| 4035 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 4036 | memcpy(ptr, "----------", 10); |
| 4037 | ptr += 10; |
| 4038 | if (idx % 100 == 0) { |
| 4039 | ptr--; |
| 4040 | *ptr++ = marker; |
| 4041 | if (++marker > 'z') |
| 4042 | marker = 'a'; |
| 4043 | } |
| 4044 | } |
| 4045 | uploadBytes[kMsgSize] = '\0'; |
| 4046 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4047 | for (int i = 0; i < kIterations; ++i) { |
| 4048 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4049 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4050 | test_server_.GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 4051 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4052 | r->set_method(method.c_str()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4053 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4054 | r->set_upload(CreateSimpleUploadData(uploadBytes)); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4055 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4056 | r->Start(); |
| 4057 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4058 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4059 | d.RunUntilComplete(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4060 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4061 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 4062 | << d.request_status(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4063 | |
| 4064 | EXPECT_FALSE(d.received_data_before_response()); |
| 4065 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4066 | } |
| 4067 | delete[] uploadBytes; |
| 4068 | } |
| 4069 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 4070 | bool DoManyCookiesRequest(int num_cookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4071 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4072 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4073 | test_server_.GetURL("/set-many-cookies?" + |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4074 | base::IntToString(num_cookies)), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4075 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4076 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4077 | r->Start(); |
| 4078 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4079 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4080 | d.RunUntilComplete(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4081 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4082 | if (d.request_status() != OK) { |
| 4083 | EXPECT_EQ(ERR_RESPONSE_HEADERS_TOO_BIG, d.request_status()); |
| 4084 | return false; |
| 4085 | } |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4086 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4087 | return true; |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4088 | } |
| 4089 | |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4090 | HttpTestServer* http_test_server() { return &test_server_; } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 4091 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4092 | private: |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4093 | HttpTestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4094 | }; |
| 4095 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4096 | namespace { |
| 4097 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4098 | std::unique_ptr<test_server::HttpResponse> HandleRedirectConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4099 | const test_server::HttpRequest& request) { |
| 4100 | if (request.headers.find("Host") == request.headers.end() || |
| 4101 | request.headers.at("Host") != "www.redirect.com" || |
| 4102 | request.method != test_server::METHOD_CONNECT) { |
| 4103 | return nullptr; |
| 4104 | } |
| 4105 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4106 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4107 | new test_server::BasicHttpResponse); |
| 4108 | http_response->set_code(HTTP_FOUND); |
| 4109 | http_response->AddCustomHeader("Location", |
| 4110 | "http://www.destination.com/foo.js"); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 4111 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4112 | } |
| 4113 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4114 | class TestSSLConfigService : public SSLConfigService { |
| 4115 | public: |
Nick Harper | 2243e800 | 2018-09-28 20:33:26 | [diff] [blame] | 4116 | TestSSLConfigService() |
| 4117 | : min_version_(kDefaultSSLVersionMin), |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4118 | max_version_(kDefaultSSLVersionMax) {} |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4119 | ~TestSSLConfigService() override = default; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4120 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4121 | void set_max_version(uint16_t version) { max_version_ = version; } |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4122 | void set_min_version(uint16_t version) { min_version_ = version; } |
| 4123 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4124 | // SSLConfigService: |
| 4125 | void GetSSLConfig(SSLConfig* config) override { |
| 4126 | *config = SSLConfig(); |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4127 | config->version_min = min_version_; |
| 4128 | config->version_max = max_version_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4129 | } |
| 4130 | |
Nick Harper | 89bc721 | 2018-07-31 19:07:57 | [diff] [blame] | 4131 | bool CanShareConnectionWithClientCerts( |
| 4132 | const std::string& hostname) const override { |
| 4133 | return false; |
| 4134 | } |
| 4135 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4136 | private: |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4137 | uint16_t min_version_; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4138 | uint16_t max_version_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4139 | }; |
| 4140 | |
Nick Harper | 89bc721 | 2018-07-31 19:07:57 | [diff] [blame] | 4141 | } // namespace |
| 4142 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4143 | // 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] | 4144 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4145 | // The EmbeddedTestServer will return a 302 response, which we should not |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4146 | // follow. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 4147 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4148 | http_test_server()->RegisterRequestHandler( |
Matt Menke | 2b2a6047 | 2018-02-07 02:12:12 | [diff] [blame] | 4149 | base::BindRepeating(&HandleRedirectConnect)); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4150 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4151 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4152 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4153 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4154 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4155 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4156 | TestDelegate d; |
| 4157 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4158 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4159 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 4160 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4161 | r->Start(); |
| 4162 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4163 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4164 | d.RunUntilComplete(); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4165 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4166 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4167 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4168 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4169 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4170 | // We should not have followed the redirect. |
| 4171 | EXPECT_EQ(0, d.received_redirect_count()); |
| 4172 | } |
| 4173 | } |
| 4174 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4175 | // This is the same as the previous test, but checks that the network delegate |
| 4176 | // registers the error. |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 4177 | TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4178 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4179 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4180 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4181 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4182 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4183 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4184 | TestDelegate d; |
| 4185 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4186 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4187 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 4188 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4189 | r->Start(); |
| 4190 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4191 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4192 | d.RunUntilComplete(); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4193 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4194 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4195 | EXPECT_FALSE(r->proxy_server().is_valid()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4196 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4197 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4198 | // We should not have followed the redirect. |
| 4199 | EXPECT_EQ(0, d.received_redirect_count()); |
| 4200 | |
| 4201 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4202 | EXPECT_THAT(network_delegate.last_error(), |
| 4203 | IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4204 | } |
| 4205 | } |
| 4206 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4207 | // Tests that we can block and asynchronously return OK in various stages. |
| 4208 | TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) { |
| 4209 | static const BlockingNetworkDelegate::Stage blocking_stages[] = { |
| 4210 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 4211 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 4212 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED |
| 4213 | }; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 4214 | static const size_t blocking_stages_length = base::size(blocking_stages); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4215 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4216 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4217 | |
| 4218 | TestDelegate d; |
| 4219 | BlockingNetworkDelegate network_delegate( |
| 4220 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4221 | network_delegate.set_block_on( |
| 4222 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST | |
| 4223 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS | |
| 4224 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 4225 | |
| 4226 | TestURLRequestContext context(true); |
| 4227 | context.set_network_delegate(&network_delegate); |
| 4228 | context.Init(); |
| 4229 | |
| 4230 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4231 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4232 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4233 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4234 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4235 | r->Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4236 | for (size_t i = 0; i < blocking_stages_length; ++i) { |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4237 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4238 | EXPECT_EQ(blocking_stages[i], |
| 4239 | network_delegate.stage_blocked_for_callback()); |
| 4240 | network_delegate.DoCallback(OK); |
| 4241 | } |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4242 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4243 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4244 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4245 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4246 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4247 | } |
| 4248 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4249 | } |
| 4250 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4251 | // Tests that the network delegate can block and cancel a request. |
| 4252 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4253 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4254 | |
| 4255 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4256 | BlockingNetworkDelegate network_delegate( |
| 4257 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4258 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 4259 | network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4260 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4261 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4262 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4263 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4264 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4265 | std::unique_ptr<URLRequest> r( |
| 4266 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4267 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4268 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4269 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4270 | d.RunUntilComplete(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4271 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4272 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4273 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4274 | EXPECT_EQ(ERR_EMPTY_RESPONSE, d.request_status()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4275 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4276 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4277 | } |
| 4278 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4279 | } |
| 4280 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4281 | // Helper function for NetworkDelegateCancelRequestAsynchronously and |
| 4282 | // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network |
| 4283 | // delegate operating in |block_mode| and a request for |url|. It blocks the |
| 4284 | // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT. |
| 4285 | void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode, |
| 4286 | BlockingNetworkDelegate::Stage stage, |
| 4287 | const GURL& url) { |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4288 | TestDelegate d; |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4289 | BlockingNetworkDelegate network_delegate(block_mode); |
| 4290 | network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT); |
| 4291 | network_delegate.set_block_on(stage); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4292 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4293 | TestURLRequestContext context(true); |
| 4294 | context.set_network_delegate(&network_delegate); |
| 4295 | context.Init(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4296 | |
| 4297 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4298 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4299 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4300 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4301 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4302 | d.RunUntilComplete(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4303 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4304 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4305 | if (stage == BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST || |
| 4306 | stage == BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS) { |
| 4307 | EXPECT_FALSE(r->proxy_server().is_valid()); |
| 4308 | } else if (stage == BlockingNetworkDelegate::ON_HEADERS_RECEIVED) { |
| 4309 | EXPECT_TRUE(r->proxy_server().is_direct()); |
| 4310 | } else { |
| 4311 | NOTREACHED(); |
| 4312 | } |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4313 | EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, d.request_status()); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4314 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4315 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4316 | } |
| 4317 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4318 | } |
| 4319 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4320 | // The following 3 tests check that the network delegate can cancel a request |
| 4321 | // synchronously in various stages of the request. |
| 4322 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4323 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4324 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4325 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4326 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4327 | } |
| 4328 | |
| 4329 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4330 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4331 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4332 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4333 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4334 | } |
| 4335 | |
| 4336 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4337 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4338 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4339 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4340 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4341 | } |
| 4342 | |
| 4343 | // The following 3 tests check that the network delegate can cancel a request |
| 4344 | // asynchronously in various stages of the request. |
| 4345 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4346 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4347 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4348 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4349 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4350 | } |
| 4351 | |
| 4352 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4353 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4354 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4355 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4356 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4357 | } |
| 4358 | |
| 4359 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4360 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4361 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4362 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4363 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4364 | } |
| 4365 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4366 | // Tests that the network delegate can block and redirect a request to a new |
| 4367 | // URL. |
| 4368 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4369 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4370 | |
| 4371 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4372 | BlockingNetworkDelegate network_delegate( |
| 4373 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4374 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4375 | GURL redirect_url("http://does.not.resolve.test/simple.html"); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4376 | network_delegate.set_redirect_url(redirect_url); |
| 4377 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4378 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4379 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4380 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4381 | { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4382 | GURL original_url("http://does.not.resolve.test/defaultresponse"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4383 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4384 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4385 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4386 | // Quit after hitting the redirect, so can check the headers. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4387 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 4388 | d.RunUntilRedirect(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4389 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4390 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4391 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4392 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4393 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4394 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 4395 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4396 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4397 | |
| 4398 | // Let the request finish. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame^] | 4399 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 4400 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4401 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4402 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4403 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4404 | http_test_server()->host_port_pair()), |
| 4405 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4406 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4407 | // through an untunneled proxy. |
| 4408 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4409 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4410 | http_test_server()->host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 4411 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4412 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4413 | EXPECT_EQ(redirect_url, r->url()); |
| 4414 | EXPECT_EQ(original_url, r->original_url()); |
| 4415 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4416 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4417 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4418 | } |
| 4419 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4420 | } |
| 4421 | |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4422 | // Tests that the network delegate can block and redirect a request to a new |
| 4423 | // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 4424 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4425 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4426 | |
| 4427 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4428 | BlockingNetworkDelegate network_delegate( |
| 4429 | BlockingNetworkDelegate::SYNCHRONOUS); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4430 | GURL redirect_url("http://does.not.resolve.test/simple.html"); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4431 | network_delegate.set_redirect_url(redirect_url); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4432 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4433 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4434 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4435 | |
| 4436 | { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4437 | GURL original_url("http://does.not.resolve.test/defaultresponse"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4438 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4439 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4440 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4441 | // Quit after hitting the redirect, so can check the headers. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4442 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 4443 | d.RunUntilRedirect(); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4444 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4445 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4446 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4447 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4448 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4449 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 4450 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4451 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4452 | |
| 4453 | // Let the request finish. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame^] | 4454 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 4455 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4456 | d.RunUntilComplete(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4457 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4458 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4459 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4460 | http_test_server()->host_port_pair()), |
| 4461 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4462 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4463 | // through an untunneled proxy. |
| 4464 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4465 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4466 | http_test_server()->host_port_pair())); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4467 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4468 | EXPECT_EQ(redirect_url, r->url()); |
| 4469 | EXPECT_EQ(original_url, r->original_url()); |
| 4470 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4471 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4472 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4473 | } |
| 4474 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4475 | } |
| 4476 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4477 | // Tests that redirects caused by the network delegate preserve POST data. |
| 4478 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4479 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4480 | |
| 4481 | const char kData[] = "hello world"; |
| 4482 | |
| 4483 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4484 | BlockingNetworkDelegate network_delegate( |
| 4485 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4486 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4487 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4488 | network_delegate.set_redirect_url(redirect_url); |
| 4489 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4490 | TestURLRequestContext context(true); |
| 4491 | context.set_network_delegate(&network_delegate); |
| 4492 | context.Init(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4493 | |
| 4494 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4495 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4496 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4497 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4498 | r->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4499 | r->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4500 | HttpRequestHeaders headers; |
| 4501 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 4502 | base::NumberToString(base::size(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4503 | r->SetExtraRequestHeaders(headers); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4504 | |
| 4505 | // Quit after hitting the redirect, so can check the headers. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4506 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 4507 | d.RunUntilRedirect(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4508 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4509 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4510 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4511 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4512 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4513 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 4514 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4515 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4516 | |
| 4517 | // Let the request finish. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame^] | 4518 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 4519 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4520 | d.RunUntilComplete(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4521 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4522 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4523 | EXPECT_EQ(redirect_url, r->url()); |
| 4524 | EXPECT_EQ(original_url, r->original_url()); |
| 4525 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4526 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4527 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4528 | EXPECT_EQ("POST", r->method()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4529 | EXPECT_EQ(kData, d.data_received()); |
| 4530 | } |
| 4531 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4532 | } |
| 4533 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4534 | // Tests that the network delegate can block and redirect a request to a new |
| 4535 | // URL during OnHeadersReceived. |
| 4536 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4537 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4538 | |
| 4539 | TestDelegate d; |
| 4540 | BlockingNetworkDelegate network_delegate( |
| 4541 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4542 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4543 | GURL redirect_url("http://does.not.resolve.test/simple.html"); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4544 | network_delegate.set_redirect_on_headers_received_url(redirect_url); |
| 4545 | |
| 4546 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4547 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4548 | |
| 4549 | { |
Eric Roman | da790f9 | 2018-11-07 19:17:15 | [diff] [blame] | 4550 | GURL original_url("http://does.not.resolve.test/defaultresponse"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4551 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4552 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4553 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4554 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4555 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4556 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4557 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4558 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4559 | http_test_server()->host_port_pair()), |
| 4560 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4561 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4562 | // through an untunneled proxy. |
| 4563 | EXPECT_EQ(2, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4564 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4565 | http_test_server()->host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4566 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4567 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4568 | EXPECT_EQ(redirect_url, r->url()); |
| 4569 | EXPECT_EQ(original_url, r->original_url()); |
| 4570 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4571 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 4572 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4573 | } |
| 4574 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4575 | } |
| 4576 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4577 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4578 | // by taking no action. This indicates that the NetworkDelegate does not want to |
| 4579 | // handle the challenge, and is passing the buck along to the |
| 4580 | // URLRequest::Delegate. |
| 4581 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4582 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4583 | |
| 4584 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4585 | BlockingNetworkDelegate network_delegate( |
| 4586 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4587 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4588 | TestURLRequestContext context(true); |
| 4589 | context.set_network_delegate(&network_delegate); |
| 4590 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4591 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4592 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4593 | |
| 4594 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4595 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4596 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4597 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4598 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4599 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4600 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4601 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4602 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4603 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4604 | EXPECT_TRUE(d.auth_required_called()); |
| 4605 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4606 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4607 | } |
| 4608 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4609 | } |
| 4610 | |
| 4611 | TEST_F(URLRequestTestHTTP, |
| 4612 | NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4613 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4614 | |
| 4615 | TestDelegate d; |
| 4616 | BlockingNetworkDelegate network_delegate( |
| 4617 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4618 | |
| 4619 | TestURLRequestContext context(true); |
| 4620 | context.set_network_delegate(&network_delegate); |
| 4621 | context.Init(); |
| 4622 | |
| 4623 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 4624 | |
| 4625 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4626 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4627 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4628 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4629 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4630 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4631 | d.RunUntilComplete(); |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4632 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4633 | { |
| 4634 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4635 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4636 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4637 | } |
| 4638 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4639 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4640 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4641 | EXPECT_TRUE(d.auth_required_called()); |
| 4642 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4643 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4644 | } |
| 4645 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4646 | } |
| 4647 | |
| 4648 | // Tests that the network delegate can synchronously complete OnAuthRequired |
[email protected] | 1e110eae | 2013-05-10 22:02:40 | [diff] [blame] | 4649 | // by setting credentials. |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4650 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4651 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4652 | |
| 4653 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4654 | BlockingNetworkDelegate network_delegate( |
| 4655 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4656 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4657 | network_delegate.set_auth_retval( |
| 4658 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4659 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4660 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4661 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4662 | TestURLRequestContext context(true); |
| 4663 | context.set_network_delegate(&network_delegate); |
| 4664 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4665 | |
| 4666 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4667 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4668 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4669 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4670 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4671 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4672 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4673 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4674 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4675 | EXPECT_FALSE(d.auth_required_called()); |
| 4676 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4677 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4678 | } |
| 4679 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4680 | } |
| 4681 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4682 | // Same as above, but also tests that GetFullRequestHeaders returns the proper |
| 4683 | // headers (for the first or second request) when called at the proper times. |
| 4684 | TEST_F(URLRequestTestHTTP, |
| 4685 | NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4686 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4687 | |
| 4688 | TestDelegate d; |
| 4689 | BlockingNetworkDelegate network_delegate( |
| 4690 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4691 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 4692 | network_delegate.set_auth_retval( |
| 4693 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4694 | |
| 4695 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
| 4696 | |
| 4697 | TestURLRequestContext context(true); |
| 4698 | context.set_network_delegate(&network_delegate); |
| 4699 | context.Init(); |
| 4700 | |
| 4701 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4702 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4703 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4704 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4705 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4706 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4707 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4708 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4709 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4710 | EXPECT_FALSE(d.auth_required_called()); |
| 4711 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4712 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4713 | |
| 4714 | { |
| 4715 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4716 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4717 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
| 4718 | } |
| 4719 | } |
| 4720 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4721 | } |
| 4722 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4723 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4724 | // by cancelling authentication. |
| 4725 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4726 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4727 | |
| 4728 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4729 | BlockingNetworkDelegate network_delegate( |
| 4730 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4731 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4732 | network_delegate.set_auth_retval( |
| 4733 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4734 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4735 | TestURLRequestContext context(true); |
| 4736 | context.set_network_delegate(&network_delegate); |
| 4737 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4738 | |
| 4739 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4740 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4741 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4742 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4743 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4744 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4745 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4746 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4747 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4748 | EXPECT_FALSE(d.auth_required_called()); |
| 4749 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4750 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4751 | } |
| 4752 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4753 | } |
| 4754 | |
| 4755 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4756 | // by taking no action. This indicates that the NetworkDelegate does not want |
| 4757 | // to handle the challenge, and is passing the buck along to the |
| 4758 | // URLRequest::Delegate. |
| 4759 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4760 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4761 | |
| 4762 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4763 | BlockingNetworkDelegate network_delegate( |
| 4764 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4765 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4766 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4767 | TestURLRequestContext context(true); |
| 4768 | context.set_network_delegate(&network_delegate); |
| 4769 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4770 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4771 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4772 | |
| 4773 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4774 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4775 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4776 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4777 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4778 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [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(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4782 | EXPECT_TRUE(d.auth_required_called()); |
| 4783 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4784 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4785 | } |
| 4786 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4787 | } |
| 4788 | |
| 4789 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4790 | // by setting credentials. |
| 4791 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4792 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4793 | |
| 4794 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4795 | BlockingNetworkDelegate network_delegate( |
| 4796 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4797 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4798 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4799 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4800 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4801 | AuthCredentials auth_credentials(kUser, kSecret); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4802 | network_delegate.set_auth_credentials(auth_credentials); |
| 4803 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4804 | TestURLRequestContext context(true); |
| 4805 | context.set_network_delegate(&network_delegate); |
| 4806 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4807 | |
| 4808 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4809 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4810 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4811 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[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] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4814 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4815 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4816 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4817 | EXPECT_FALSE(d.auth_required_called()); |
| 4818 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4819 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4820 | } |
| 4821 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4822 | } |
| 4823 | |
| 4824 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4825 | // by cancelling authentication. |
| 4826 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4827 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4828 | |
| 4829 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4830 | BlockingNetworkDelegate network_delegate( |
| 4831 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4832 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4833 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4834 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4835 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4836 | TestURLRequestContext context(true); |
| 4837 | context.set_network_delegate(&network_delegate); |
| 4838 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4839 | |
| 4840 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4841 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4842 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4843 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4844 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4845 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4846 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4847 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4848 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4849 | EXPECT_FALSE(d.auth_required_called()); |
| 4850 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4851 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4852 | } |
| 4853 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4854 | } |
| 4855 | |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 4856 | // Tests that NetworkDelegate header overrides from the 401 response do not |
| 4857 | // affect the 200 response. This is a regression test for |
| 4858 | // https://crbug.com/801237. |
| 4859 | TEST_F(URLRequestTestHTTP, NetworkDelegateOverrideHeadersWithAuth) { |
| 4860 | ASSERT_TRUE(http_test_server()->Start()); |
| 4861 | |
| 4862 | TestDelegate d; |
| 4863 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 4864 | default_network_delegate_.set_add_header_to_first_response(true); |
| 4865 | |
| 4866 | { |
| 4867 | GURL url(http_test_server()->GetURL("/auth-basic")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4868 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 4869 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 4870 | r->Start(); |
| 4871 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4872 | d.RunUntilComplete(); |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 4873 | |
| 4874 | EXPECT_EQ(OK, d.request_status()); |
| 4875 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4876 | EXPECT_TRUE(d.auth_required_called()); |
| 4877 | EXPECT_FALSE(r->response_headers()->HasHeader("X-Network-Delegate")); |
| 4878 | } |
| 4879 | |
| 4880 | { |
| 4881 | GURL url(http_test_server()->GetURL("/defaultresponse")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 4882 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 4883 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 4884 | r->Start(); |
| 4885 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4886 | d.RunUntilComplete(); |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 4887 | |
| 4888 | // Check that set_add_header_to_first_response normally adds a header. |
| 4889 | EXPECT_EQ(OK, d.request_status()); |
| 4890 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4891 | EXPECT_TRUE(r->response_headers()->HasHeader("X-Network-Delegate")); |
| 4892 | } |
| 4893 | } |
| 4894 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4895 | // Tests that we can handle when a network request was canceled while we were |
| 4896 | // waiting for the network delegate. |
| 4897 | // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 4898 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4899 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4900 | |
| 4901 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4902 | BlockingNetworkDelegate network_delegate( |
| 4903 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4904 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4905 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4906 | TestURLRequestContext context(true); |
| 4907 | context.set_network_delegate(&network_delegate); |
| 4908 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4909 | |
| 4910 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4911 | std::unique_ptr<URLRequest> r( |
| 4912 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4913 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4914 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4915 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4916 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4917 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 4918 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4919 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4920 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4921 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4922 | // Ensure that network delegate is notified. |
| 4923 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4924 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4925 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4926 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4927 | } |
| 4928 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4929 | } |
| 4930 | |
| 4931 | // Tests that we can handle when a network request was canceled while we were |
| 4932 | // waiting for the network delegate. |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 4933 | // Part 2: Request is cancelled while waiting for OnBeforeStartTransaction |
| 4934 | // callback. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4935 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4936 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4937 | |
| 4938 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4939 | BlockingNetworkDelegate network_delegate( |
| 4940 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4941 | network_delegate.set_block_on( |
| 4942 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4943 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4944 | TestURLRequestContext context(true); |
| 4945 | context.set_network_delegate(&network_delegate); |
| 4946 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4947 | |
| 4948 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4949 | std::unique_ptr<URLRequest> r( |
| 4950 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4951 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4952 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4953 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4954 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4955 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 4956 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4957 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4958 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4959 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4960 | // Ensure that network delegate is notified. |
| 4961 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4962 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4963 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4964 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4965 | } |
| 4966 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4967 | } |
| 4968 | |
| 4969 | // Tests that we can handle when a network request was canceled while we were |
| 4970 | // waiting for the network delegate. |
| 4971 | // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 4972 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4973 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4974 | |
| 4975 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4976 | BlockingNetworkDelegate network_delegate( |
| 4977 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4978 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4979 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4980 | TestURLRequestContext context(true); |
| 4981 | context.set_network_delegate(&network_delegate); |
| 4982 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4983 | |
| 4984 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4985 | std::unique_ptr<URLRequest> r( |
| 4986 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4987 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4988 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4989 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4990 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4991 | EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 4992 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4993 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4994 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4995 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4996 | // Ensure that network delegate is notified. |
| 4997 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4998 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4999 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5000 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5001 | } |
| 5002 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5003 | } |
| 5004 | |
| 5005 | // Tests that we can handle when a network request was canceled while we were |
| 5006 | // waiting for the network delegate. |
| 5007 | // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
[email protected] | bfe8b30 | 2013-02-15 12:16:02 | [diff] [blame] | 5008 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5009 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5010 | |
| 5011 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5012 | BlockingNetworkDelegate network_delegate( |
| 5013 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5014 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5015 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5016 | TestURLRequestContext context(true); |
| 5017 | context.set_network_delegate(&network_delegate); |
| 5018 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5019 | |
| 5020 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5021 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5022 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 5023 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5024 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5025 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5026 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5027 | EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED, |
| 5028 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5029 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5030 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5031 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5032 | // Ensure that network delegate is notified. |
| 5033 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5034 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5035 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5036 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5037 | } |
| 5038 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5039 | } |
| 5040 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5041 | namespace { |
| 5042 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5043 | std::unique_ptr<test_server::HttpResponse> HandleServerAuthConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5044 | const test_server::HttpRequest& request) { |
| 5045 | if (request.headers.find("Host") == request.headers.end() || |
| 5046 | request.headers.at("Host") != "www.server-auth.com" || |
| 5047 | request.method != test_server::METHOD_CONNECT) { |
| 5048 | return nullptr; |
| 5049 | } |
| 5050 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5051 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5052 | new test_server::BasicHttpResponse); |
| 5053 | http_response->set_code(HTTP_UNAUTHORIZED); |
| 5054 | http_response->AddCustomHeader("WWW-Authenticate", |
| 5055 | "Basic realm=\"WallyWorld\""); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 5056 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5057 | } |
| 5058 | |
| 5059 | } // namespace |
| 5060 | |
| 5061 | // 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] | 5062 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5063 | // The EmbeddedTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5064 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5065 | http_test_server()->RegisterRequestHandler( |
Matt Menke | 2b2a6047 | 2018-02-07 02:12:12 | [diff] [blame] | 5066 | base::BindRepeating(&HandleServerAuthConnect)); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5067 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5068 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5069 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 5070 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5071 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 5072 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5073 | TestDelegate d; |
| 5074 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5075 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5076 | GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d, |
| 5077 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5078 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5079 | r->Start(); |
| 5080 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5081 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5082 | d.RunUntilComplete(); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5083 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 5084 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 5085 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5086 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5087 | } |
| 5088 | } |
| 5089 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5090 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5091 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5092 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5093 | TestDelegate d; |
| 5094 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5095 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5096 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5097 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5098 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5099 | r->Start(); |
| 5100 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5101 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5102 | d.RunUntilComplete(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5103 | |
| 5104 | EXPECT_EQ(1, d.response_started_count()); |
| 5105 | EXPECT_FALSE(d.received_data_before_response()); |
| 5106 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5107 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5108 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5109 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5110 | r->GetSocketAddress().port()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 5111 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5112 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5113 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5114 | } |
| 5115 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5116 | // This test has the server send a large number of cookies to the client. |
| 5117 | // To ensure that no number of cookies causes a crash, a galloping binary |
| 5118 | // search is used to estimate that maximum number of cookies that are accepted |
| 5119 | // by the browser. Beyond the maximum number, the request will fail with |
| 5120 | // ERR_RESPONSE_HEADERS_TOO_BIG. |
Sergey Ulanov | 0fb900c | 2017-09-20 00:27:31 | [diff] [blame] | 5121 | #if defined(OS_WIN) || defined(OS_FUCHSIA) |
[email protected] | 69dd6fe | 2013-02-23 23:15:30 | [diff] [blame] | 5122 | // http://crbug.com/177916 |
| 5123 | #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies |
| 5124 | #else |
| 5125 | #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies |
| 5126 | #endif // defined(OS_WIN) |
| 5127 | TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5128 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5129 | |
| 5130 | int lower_bound = 0; |
| 5131 | int upper_bound = 1; |
| 5132 | |
| 5133 | // Double the number of cookies until the response header limits are |
| 5134 | // exceeded. |
| 5135 | while (DoManyCookiesRequest(upper_bound)) { |
| 5136 | lower_bound = upper_bound; |
| 5137 | upper_bound *= 2; |
| 5138 | ASSERT_LT(upper_bound, 1000000); |
| 5139 | } |
| 5140 | |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 5141 | int tolerance = static_cast<int>(upper_bound * 0.005); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5142 | if (tolerance < 2) |
| 5143 | tolerance = 2; |
| 5144 | |
| 5145 | // Perform a binary search to find the highest possible number of cookies, |
| 5146 | // within the desired tolerance. |
| 5147 | while (upper_bound - lower_bound >= tolerance) { |
| 5148 | int num_cookies = (lower_bound + upper_bound) / 2; |
| 5149 | |
| 5150 | if (DoManyCookiesRequest(num_cookies)) |
| 5151 | lower_bound = num_cookies; |
| 5152 | else |
| 5153 | upper_bound = num_cookies; |
| 5154 | } |
| 5155 | // Success: the test did not crash. |
| 5156 | } |
| 5157 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5158 | TEST_F(URLRequestTestHTTP, GetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5159 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5160 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5161 | TestDelegate d; |
| 5162 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5163 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5164 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5165 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5166 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5167 | r->Start(); |
| 5168 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5169 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5170 | d.RunUntilComplete(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5171 | |
| 5172 | EXPECT_EQ(1, d.response_started_count()); |
| 5173 | EXPECT_FALSE(d.received_data_before_response()); |
| 5174 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5175 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5176 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5177 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5178 | r->GetSocketAddress().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5179 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 5180 | } |
| 5181 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5182 | TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5183 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5184 | |
| 5185 | TestDelegate d; |
| 5186 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5187 | GURL test_url(http_test_server()->GetURL("/defaultresponse")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5188 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5189 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5190 | |
| 5191 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5192 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5193 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5194 | r->Start(); |
| 5195 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5196 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5197 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5198 | |
| 5199 | EXPECT_EQ(1, d.response_started_count()); |
| 5200 | EXPECT_FALSE(d.received_data_before_response()); |
| 5201 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5202 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5203 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5204 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5205 | r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5206 | |
| 5207 | EXPECT_TRUE(d.have_full_request_headers()); |
| 5208 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 5209 | } |
| 5210 | } |
| 5211 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5212 | TEST_F(URLRequestTestHTTP, GetTestLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5213 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5214 | |
| 5215 | TestDelegate d; |
| 5216 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5217 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5218 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5219 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5220 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5221 | r->Start(); |
| 5222 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5223 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5224 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5225 | |
| 5226 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5227 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5228 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5229 | |
| 5230 | EXPECT_EQ(1, d.response_started_count()); |
| 5231 | EXPECT_FALSE(d.received_data_before_response()); |
| 5232 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5233 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5234 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5235 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5236 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5237 | } |
| 5238 | } |
| 5239 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5240 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 5241 | #if !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5242 | TEST_F(URLRequestTestHTTP, GetZippedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5243 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5244 | base::FilePath(kTestFilePath)); |
| 5245 | |
| 5246 | ASSERT_TRUE(test_server.Start()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5247 | |
| 5248 | // Parameter that specifies the Content-Length field in the response: |
| 5249 | // C - Compressed length. |
| 5250 | // U - Uncompressed length. |
| 5251 | // L - Large length (larger than both C & U). |
| 5252 | // M - Medium length (between C & U). |
| 5253 | // S - Small length (smaller than both C & U). |
| 5254 | const char test_parameters[] = "CULMS"; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 5255 | const int num_tests = base::size(test_parameters) - 1; // Skip NULL. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5256 | // C & U should be OK. |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 5257 | // L & M are larger than the data sent, and show an error. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5258 | // S has too little data, but we seem to accept it. |
| 5259 | const bool test_expect_success[num_tests] = |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 5260 | { true, true, false, false, true }; |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5261 | |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5262 | base::FilePath file_path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 5263 | base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5264 | file_path = file_path.Append(kTestFilePath); |
| 5265 | file_path = file_path.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); |
| 5266 | std::string expected_content; |
| 5267 | ASSERT_TRUE(base::ReadFileToString(file_path, &expected_content)); |
| 5268 | |
| 5269 | for (int i = 0; i < num_tests; i++) { |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5270 | TestDelegate d; |
| 5271 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5272 | std::string test_file = base::StringPrintf( |
| 5273 | "compressedfiles/BullRunSpeech.txt?%c", test_parameters[i]); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5274 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5275 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5276 | TestURLRequestContext context(true); |
| 5277 | context.set_network_delegate(&network_delegate); |
| 5278 | context.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 5279 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5280 | std::unique_ptr<URLRequest> r( |
| 5281 | context.CreateRequest(test_server.GetURL(test_file), DEFAULT_PRIORITY, |
| 5282 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5283 | r->Start(); |
| 5284 | EXPECT_TRUE(r->is_pending()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5285 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5286 | d.RunUntilComplete(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5287 | |
| 5288 | EXPECT_EQ(1, d.response_started_count()); |
| 5289 | EXPECT_FALSE(d.received_data_before_response()); |
| 5290 | VLOG(1) << " Received " << d.bytes_received() << " bytes" |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5291 | << " error = " << d.request_status(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5292 | if (test_expect_success[i]) { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5293 | EXPECT_EQ(OK, d.request_status()) << " Parameter = \"" << test_file |
| 5294 | << "\""; |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5295 | if (test_parameters[i] == 'S') { |
| 5296 | // When content length is smaller than both compressed length and |
| 5297 | // uncompressed length, HttpStreamParser might not read the full |
| 5298 | // response body. |
| 5299 | continue; |
| 5300 | } |
| 5301 | EXPECT_EQ(expected_content, d.data_received()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5302 | } else { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5303 | EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, d.request_status()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5304 | << " Parameter = \"" << test_file << "\""; |
| 5305 | } |
| 5306 | } |
| 5307 | } |
| 5308 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5309 | #endif // !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5310 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5311 | TEST_F(URLRequestTestHTTP, RedirectLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5312 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5313 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5314 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5315 | GURL original_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5316 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5317 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5318 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5319 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5320 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5321 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5322 | |
| 5323 | EXPECT_EQ(1, d.response_started_count()); |
| 5324 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5325 | EXPECT_EQ(destination_url, req->url()); |
| 5326 | EXPECT_EQ(original_url, req->original_url()); |
| 5327 | ASSERT_EQ(2U, req->url_chain().size()); |
| 5328 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5329 | EXPECT_EQ(destination_url, req->url_chain()[1]); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5330 | |
| 5331 | LoadTimingInfo load_timing_info_before_redirect; |
| 5332 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect( |
| 5333 | &load_timing_info_before_redirect)); |
| 5334 | TestLoadTimingNotReused(load_timing_info_before_redirect, |
| 5335 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 5336 | |
| 5337 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5338 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5339 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5340 | |
| 5341 | // Check that a new socket was used on redirect, since the server does not |
| 5342 | // supposed keep-alive sockets, and that the times before the redirect are |
| 5343 | // before the ones recorded for the second request. |
| 5344 | EXPECT_NE(load_timing_info_before_redirect.socket_log_id, |
| 5345 | load_timing_info.socket_log_id); |
| 5346 | EXPECT_LE(load_timing_info_before_redirect.receive_headers_end, |
| 5347 | load_timing_info.connect_timing.connect_start); |
| 5348 | } |
| 5349 | |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5350 | TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5351 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5352 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5353 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5354 | GURL middle_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5355 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
| 5356 | GURL original_url = http_test_server()->GetURL("/server-redirect?" + |
| 5357 | middle_redirect_url.spec()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5358 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 5359 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5360 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5361 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5362 | d.RunUntilComplete(); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5363 | |
| 5364 | EXPECT_EQ(1, d.response_started_count()); |
| 5365 | EXPECT_EQ(2, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5366 | EXPECT_EQ(destination_url, req->url()); |
| 5367 | EXPECT_EQ(original_url, req->original_url()); |
| 5368 | ASSERT_EQ(3U, req->url_chain().size()); |
| 5369 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5370 | EXPECT_EQ(middle_redirect_url, req->url_chain()[1]); |
| 5371 | EXPECT_EQ(destination_url, req->url_chain()[2]); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5372 | } |
| 5373 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5374 | // First and second pieces of information logged by delegates to URLRequests. |
| 5375 | const char kFirstDelegateInfo[] = "Wonderful delegate"; |
| 5376 | const char kSecondDelegateInfo[] = "Exciting delegate"; |
| 5377 | |
| 5378 | // Logs delegate information to a URLRequest. The first string is logged |
| 5379 | // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is |
| 5380 | // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then |
| 5381 | // another asynchronous call is used to clear the delegate information |
| 5382 | // before calling a callback. The object then deletes itself. |
| 5383 | class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> { |
| 5384 | public: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5385 | using Callback = base::OnceCallback<void()>; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5386 | |
| 5387 | // Each time delegate information is added to the URLRequest, the resulting |
| 5388 | // load state is checked. The expected load state after each request is |
| 5389 | // passed in as an argument. |
| 5390 | static void Run(URLRequest* url_request, |
| 5391 | LoadState expected_first_load_state, |
| 5392 | LoadState expected_second_load_state, |
| 5393 | LoadState expected_third_load_state, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5394 | Callback callback) { |
tzik | 38f920a | 2018-08-01 04:41:20 | [diff] [blame] | 5395 | // base::MakeRefCounted<AsyncDelegateLogger> is unavailable here, since the |
| 5396 | // constructor of AsyncDelegateLogger is private. |
| 5397 | auto logger = base::WrapRefCounted(new AsyncDelegateLogger( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5398 | url_request, expected_first_load_state, expected_second_load_state, |
tzik | 38f920a | 2018-08-01 04:41:20 | [diff] [blame] | 5399 | expected_third_load_state, std::move(callback))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5400 | logger->Start(); |
| 5401 | } |
| 5402 | |
| 5403 | // Checks that the log entries, starting with log_position, contain the |
| 5404 | // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have |
| 5405 | // recorded. Returns the index of entry after the expected number of |
| 5406 | // events this logged, or entries.size() if there aren't enough entries. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5407 | static size_t CheckDelegateInfo(const TestNetLogEntry::List& entries, |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 5408 | size_t log_position) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5409 | // There should be 4 DELEGATE_INFO events: Two begins and two ends. |
| 5410 | if (log_position + 3 >= entries.size()) { |
| 5411 | ADD_FAILURE() << "Not enough log entries"; |
| 5412 | return entries.size(); |
| 5413 | } |
| 5414 | std::string delegate_info; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5415 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5416 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
rdsmith | 37a0dde | 2017-01-04 00:12:07 | [diff] [blame] | 5417 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_blocked_by", |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5418 | &delegate_info)); |
| 5419 | EXPECT_EQ(kFirstDelegateInfo, delegate_info); |
| 5420 | |
| 5421 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5422 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5423 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5424 | |
| 5425 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5426 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5427 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
rdsmith | 37a0dde | 2017-01-04 00:12:07 | [diff] [blame] | 5428 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_blocked_by", |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5429 | &delegate_info)); |
| 5430 | EXPECT_EQ(kSecondDelegateInfo, delegate_info); |
| 5431 | |
| 5432 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5433 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5434 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5435 | |
| 5436 | return log_position + 1; |
| 5437 | } |
| 5438 | |
| 5439 | private: |
| 5440 | friend class base::RefCounted<AsyncDelegateLogger>; |
| 5441 | |
| 5442 | AsyncDelegateLogger(URLRequest* url_request, |
| 5443 | LoadState expected_first_load_state, |
| 5444 | LoadState expected_second_load_state, |
| 5445 | LoadState expected_third_load_state, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5446 | Callback callback) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5447 | : url_request_(url_request), |
| 5448 | expected_first_load_state_(expected_first_load_state), |
| 5449 | expected_second_load_state_(expected_second_load_state), |
| 5450 | expected_third_load_state_(expected_third_load_state), |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5451 | callback_(std::move(callback)) {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5452 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5453 | ~AsyncDelegateLogger() = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5454 | |
| 5455 | void Start() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5456 | url_request_->LogBlockedBy(kFirstDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5457 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5458 | EXPECT_EQ(expected_first_load_state_, load_state.state); |
| 5459 | EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5460 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5461 | FROM_HERE, |
| 5462 | base::BindOnce(&AsyncDelegateLogger::LogSecondDelegate, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5463 | } |
| 5464 | |
| 5465 | void LogSecondDelegate() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5466 | url_request_->LogAndReportBlockedBy(kSecondDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5467 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5468 | EXPECT_EQ(expected_second_load_state_, load_state.state); |
| 5469 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) { |
| 5470 | EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5471 | } else { |
| 5472 | EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5473 | } |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5474 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5475 | FROM_HERE, base::BindOnce(&AsyncDelegateLogger::LogComplete, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5476 | } |
| 5477 | |
| 5478 | void LogComplete() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5479 | url_request_->LogUnblocked(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5480 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5481 | EXPECT_EQ(expected_third_load_state_, load_state.state); |
| 5482 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5483 | EXPECT_EQ(base::string16(), load_state.param); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5484 | std::move(callback_).Run(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5485 | } |
| 5486 | |
| 5487 | URLRequest* url_request_; |
| 5488 | const int expected_first_load_state_; |
| 5489 | const int expected_second_load_state_; |
| 5490 | const int expected_third_load_state_; |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5491 | Callback callback_; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5492 | |
| 5493 | DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger); |
| 5494 | }; |
| 5495 | |
| 5496 | // NetworkDelegate that logs delegate information before a request is started, |
| 5497 | // before headers are sent, when headers are read, and when auth information |
| 5498 | // is requested. Uses AsyncDelegateLogger. |
| 5499 | class AsyncLoggingNetworkDelegate : public TestNetworkDelegate { |
| 5500 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5501 | AsyncLoggingNetworkDelegate() = default; |
| 5502 | ~AsyncLoggingNetworkDelegate() override = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5503 | |
| 5504 | // NetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5505 | int OnBeforeURLRequest(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5506 | CompletionOnceCallback callback, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5507 | GURL* new_url) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5508 | // TestNetworkDelegate always completes synchronously. |
| 5509 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeURLRequest( |
| 5510 | request, base::NullCallback(), new_url)); |
| 5511 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5512 | } |
| 5513 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5514 | int OnBeforeStartTransaction(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5515 | CompletionOnceCallback callback, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5516 | HttpRequestHeaders* headers) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5517 | // TestNetworkDelegate always completes synchronously. |
| 5518 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeStartTransaction( |
| 5519 | request, base::NullCallback(), headers)); |
| 5520 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5521 | } |
| 5522 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5523 | int OnHeadersReceived( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5524 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5525 | CompletionOnceCallback callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5526 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5527 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5528 | GURL* allowed_unsafe_redirect_url) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5529 | // TestNetworkDelegate always completes synchronously. |
| 5530 | CHECK_NE(ERR_IO_PENDING, |
| 5531 | TestNetworkDelegate::OnHeadersReceived( |
| 5532 | request, base::NullCallback(), original_response_headers, |
| 5533 | override_response_headers, allowed_unsafe_redirect_url)); |
| 5534 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5535 | } |
| 5536 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5537 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5538 | URLRequest* request, |
| 5539 | const AuthChallengeInfo& auth_info, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5540 | AuthCallback callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5541 | AuthCredentials* credentials) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5542 | AsyncDelegateLogger::Run( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5543 | request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5544 | LOAD_STATE_WAITING_FOR_DELEGATE, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5545 | base::BindOnce(&AsyncLoggingNetworkDelegate::SetAuthAndResume, |
| 5546 | std::move(callback), credentials)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5547 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 5548 | } |
| 5549 | |
| 5550 | private: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5551 | static int RunCallbackAsynchronously(URLRequest* request, |
| 5552 | CompletionOnceCallback callback) { |
| 5553 | AsyncDelegateLogger::Run(request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5554 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5555 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5556 | base::BindOnce(std::move(callback), OK)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5557 | return ERR_IO_PENDING; |
| 5558 | } |
| 5559 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5560 | static void SetAuthAndResume(AuthCallback callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5561 | AuthCredentials* credentials) { |
| 5562 | *credentials = AuthCredentials(kUser, kSecret); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5563 | std::move(callback).Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5564 | } |
| 5565 | |
| 5566 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate); |
| 5567 | }; |
| 5568 | |
| 5569 | // URLRequest::Delegate that logs delegate information when the headers |
| 5570 | // are received, when each read completes, and during redirects. Uses |
| 5571 | // AsyncDelegateLogger. Can optionally cancel a request in any phase. |
| 5572 | // |
| 5573 | // Inherits from TestDelegate to reuse the TestDelegate code to handle |
| 5574 | // advancing to the next step in most cases, as well as cancellation. |
| 5575 | class AsyncLoggingUrlRequestDelegate : public TestDelegate { |
| 5576 | public: |
| 5577 | enum CancelStage { |
| 5578 | NO_CANCEL = 0, |
| 5579 | CANCEL_ON_RECEIVED_REDIRECT, |
| 5580 | CANCEL_ON_RESPONSE_STARTED, |
| 5581 | CANCEL_ON_READ_COMPLETED |
| 5582 | }; |
| 5583 | |
| 5584 | explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage) |
| 5585 | : cancel_stage_(cancel_stage) { |
| 5586 | if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT) |
| 5587 | set_cancel_in_received_redirect(true); |
| 5588 | else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED) |
| 5589 | set_cancel_in_response_started(true); |
| 5590 | else if (cancel_stage == CANCEL_ON_READ_COMPLETED) |
| 5591 | set_cancel_in_received_data(true); |
| 5592 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5593 | ~AsyncLoggingUrlRequestDelegate() override = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5594 | |
| 5595 | // URLRequest::Delegate implementation: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5596 | void OnReceivedRedirect(URLRequest* request, |
| 5597 | const RedirectInfo& redirect_info, |
| 5598 | bool* defer_redirect) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5599 | *defer_redirect = true; |
| 5600 | AsyncDelegateLogger::Run( |
| 5601 | request, |
| 5602 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5603 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5604 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5605 | base::Bind( |
| 5606 | &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5607 | base::Unretained(this), request, redirect_info)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5608 | } |
| 5609 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5610 | void OnResponseStarted(URLRequest* request, int net_error) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5611 | AsyncDelegateLogger::Run( |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5612 | request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5613 | LOAD_STATE_WAITING_FOR_DELEGATE, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5614 | base::Bind( |
| 5615 | &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete, |
| 5616 | base::Unretained(this), request, net_error)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5617 | } |
| 5618 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5619 | void OnReadCompleted(URLRequest* request, int bytes_read) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5620 | AsyncDelegateLogger::Run( |
| 5621 | request, |
| 5622 | LOAD_STATE_IDLE, |
| 5623 | LOAD_STATE_IDLE, |
| 5624 | LOAD_STATE_IDLE, |
| 5625 | base::Bind( |
| 5626 | &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete, |
| 5627 | base::Unretained(this), request, bytes_read)); |
| 5628 | } |
| 5629 | |
| 5630 | private: |
| 5631 | void OnReceivedRedirectLoggingComplete(URLRequest* request, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5632 | const RedirectInfo& redirect_info) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5633 | bool defer_redirect = false; |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5634 | TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5635 | // FollowDeferredRedirect should not be called after cancellation. |
| 5636 | if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT) |
| 5637 | return; |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame^] | 5638 | if (!defer_redirect) { |
| 5639 | request->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 5640 | base::nullopt /* modified_headers */); |
| 5641 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5642 | } |
| 5643 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5644 | void OnResponseStartedLoggingComplete(URLRequest* request, int net_error) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5645 | // The parent class continues the request. |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5646 | TestDelegate::OnResponseStarted(request, net_error); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5647 | } |
| 5648 | |
| 5649 | void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) { |
| 5650 | // The parent class continues the request. |
| 5651 | TestDelegate::OnReadCompleted(request, bytes_read); |
| 5652 | } |
| 5653 | |
| 5654 | const CancelStage cancel_stage_; |
| 5655 | |
| 5656 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate); |
| 5657 | }; |
| 5658 | |
| 5659 | // Tests handling of delegate info before a request starts. |
| 5660 | TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5661 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5662 | |
| 5663 | TestDelegate request_delegate; |
| 5664 | TestURLRequestContext context(true); |
| 5665 | context.set_network_delegate(NULL); |
| 5666 | context.set_net_log(&net_log_); |
| 5667 | context.Init(); |
| 5668 | |
| 5669 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5670 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5671 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, |
| 5672 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5673 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5674 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5675 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5676 | |
| 5677 | AsyncDelegateLogger::Run( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5678 | r.get(), |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5679 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5680 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5681 | LOAD_STATE_IDLE, |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5682 | base::Bind(&URLRequest::Start, base::Unretained(r.get()))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5683 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5684 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5685 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5686 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5687 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5688 | } |
| 5689 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5690 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5691 | net_log_.GetEntries(&entries); |
| 5692 | size_t log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5693 | entries, 0, NetLogEventType::DELEGATE_INFO, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5694 | |
| 5695 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position); |
| 5696 | |
| 5697 | // Nothing else should add any delegate info to the request. |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5698 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5699 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5700 | } |
| 5701 | |
| 5702 | // Tests handling of delegate info from a network delegate. |
| 5703 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5704 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5705 | |
| 5706 | TestDelegate request_delegate; |
| 5707 | AsyncLoggingNetworkDelegate network_delegate; |
| 5708 | TestURLRequestContext context(true); |
| 5709 | context.set_network_delegate(&network_delegate); |
| 5710 | context.set_net_log(&net_log_); |
| 5711 | context.Init(); |
| 5712 | |
| 5713 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5714 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5715 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, |
| 5716 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5717 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5718 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5719 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5720 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5721 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5722 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5723 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5724 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5725 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5726 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5727 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5728 | } |
| 5729 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5730 | |
| 5731 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5732 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5733 | net_log_.GetEntries(&entries); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5734 | static const NetLogEventType kExpectedEvents[] = { |
| 5735 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 5736 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 5737 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 5738 | }; |
| 5739 | for (NetLogEventType event : kExpectedEvents) { |
| 5740 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5741 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5742 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5743 | |
| 5744 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5745 | log_position + 1); |
| 5746 | |
| 5747 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5748 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5749 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5750 | } |
| 5751 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5752 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5753 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5754 | } |
| 5755 | |
| 5756 | // Tests handling of delegate info from a network delegate in the case of an |
| 5757 | // HTTP redirect. |
| 5758 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5759 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5760 | |
| 5761 | TestDelegate request_delegate; |
| 5762 | AsyncLoggingNetworkDelegate network_delegate; |
| 5763 | TestURLRequestContext context(true); |
| 5764 | context.set_network_delegate(&network_delegate); |
| 5765 | context.set_net_log(&net_log_); |
| 5766 | context.Init(); |
| 5767 | |
| 5768 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5769 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5770 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5771 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5772 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5773 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5774 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5775 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5776 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5777 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5778 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5779 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5780 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5781 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 5782 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5783 | } |
| 5784 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5785 | |
| 5786 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5787 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5788 | net_log_.GetEntries(&entries); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5789 | static const NetLogEventType kExpectedEvents[] = { |
| 5790 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 5791 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 5792 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 5793 | }; |
| 5794 | for (NetLogEventType event : kExpectedEvents) { |
| 5795 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5796 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5797 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5798 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5799 | log_position = |
| 5800 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5801 | |
| 5802 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5803 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5804 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5805 | } |
| 5806 | |
| 5807 | // The URLRequest::Delegate then gets informed about the redirect. |
| 5808 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5809 | entries, log_position + 1, |
| 5810 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5811 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5812 | |
| 5813 | // The NetworkDelegate logged information in the same three events as before. |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5814 | for (NetLogEventType event : kExpectedEvents) { |
| 5815 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5816 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5817 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5818 | |
| 5819 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5820 | log_position + 1); |
| 5821 | |
| 5822 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5823 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5824 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5825 | } |
| 5826 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5827 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5828 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5829 | } |
| 5830 | |
| 5831 | // Tests handling of delegate info from a network delegate in the case of HTTP |
| 5832 | // AUTH. |
| 5833 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5834 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5835 | |
| 5836 | TestDelegate request_delegate; |
| 5837 | AsyncLoggingNetworkDelegate network_delegate; |
| 5838 | TestURLRequestContext context(true); |
| 5839 | context.set_network_delegate(&network_delegate); |
| 5840 | context.set_net_log(&net_log_); |
| 5841 | context.Init(); |
| 5842 | |
| 5843 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5844 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5845 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, |
| 5846 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5847 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5848 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5849 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5850 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5851 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5852 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5853 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5854 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5855 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5856 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5857 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5858 | } |
| 5859 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5860 | |
| 5861 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5862 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5863 | net_log_.GetEntries(&entries); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5864 | static const NetLogEventType kExpectedEvents[] = { |
| 5865 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 5866 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 5867 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 5868 | NetLogEventType::NETWORK_DELEGATE_AUTH_REQUIRED, |
| 5869 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 5870 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 5871 | }; |
| 5872 | for (NetLogEventType event : kExpectedEvents) { |
| 5873 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5874 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5875 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5876 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5877 | log_position = |
| 5878 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5879 | |
| 5880 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5881 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5882 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5883 | } |
| 5884 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5885 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5886 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5887 | } |
| 5888 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5889 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 5890 | #if !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5891 | // Tests handling of delegate info from a URLRequest::Delegate. |
| 5892 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5893 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5894 | base::FilePath(kTestFilePath)); |
| 5895 | |
| 5896 | ASSERT_TRUE(test_server.Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5897 | |
| 5898 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 5899 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 5900 | TestURLRequestContext context(true); |
| 5901 | context.set_network_delegate(NULL); |
| 5902 | context.set_net_log(&net_log_); |
| 5903 | context.Init(); |
| 5904 | |
| 5905 | { |
| 5906 | // A chunked response with delays between chunks is used to make sure that |
| 5907 | // attempts by the URLRequest delegate to log information while reading the |
| 5908 | // body are ignored. Since they are ignored, this test is robust against |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5909 | // the possibility of multiple reads being combined in the unlikely event |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5910 | // that it occurs. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5911 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5912 | test_server.GetURL("/chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5913 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5914 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5915 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5916 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5917 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5918 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5919 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5920 | } |
| 5921 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5922 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5923 | net_log_.GetEntries(&entries); |
| 5924 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5925 | size_t log_position = 0; |
| 5926 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5927 | // The delegate info should only have been logged on header complete. Other |
| 5928 | // times it should silently be ignored. |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5929 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5930 | entries, 0, NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST)); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5931 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5932 | entries, log_position + 1, |
| 5933 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5934 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5935 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5936 | log_position = |
| 5937 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5938 | |
| 5939 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5940 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 5941 | entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5942 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5943 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5944 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5945 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5946 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5947 | entries, log_position + 1, |
| 5948 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5949 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5950 | #endif // !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5951 | |
| 5952 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 5953 | // an HTTP redirect. |
| 5954 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5955 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5956 | |
| 5957 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 5958 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 5959 | TestURLRequestContext context(true); |
| 5960 | context.set_network_delegate(NULL); |
| 5961 | context.set_net_log(&net_log_); |
| 5962 | context.Init(); |
| 5963 | |
| 5964 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5965 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5966 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5967 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5968 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5969 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5970 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5971 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5972 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5973 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5974 | } |
| 5975 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5976 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5977 | net_log_.GetEntries(&entries); |
| 5978 | |
| 5979 | // Delegate info should only have been logged in OnReceivedRedirect and |
| 5980 | // OnResponseStarted. |
| 5981 | size_t log_position = 0; |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5982 | static const NetLogEventType kExpectedEvents[] = { |
| 5983 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
| 5984 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 5985 | }; |
| 5986 | for (NetLogEventType event : kExpectedEvents) { |
| 5987 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
| 5988 | log_position = ExpectLogContainsSomewhereAfter(entries, log_position, event, |
| 5989 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5990 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5991 | log_position = |
| 5992 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5993 | |
| 5994 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 5995 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5996 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5997 | } |
| 5998 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5999 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6000 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6001 | } |
| 6002 | |
| 6003 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 6004 | // an HTTP redirect, with cancellation at various points. |
| 6005 | TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6006 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6007 | |
| 6008 | const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = { |
| 6009 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT, |
| 6010 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED, |
| 6011 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED, |
| 6012 | }; |
| 6013 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6014 | for (auto cancel_stage : kCancelStages) { |
| 6015 | AsyncLoggingUrlRequestDelegate request_delegate(cancel_stage); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 6016 | TestNetLog net_log; |
krasin | 0bfeb6b | 2017-01-13 21:48:04 | [diff] [blame] | 6017 | TestURLRequestContext context(true); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6018 | context.set_network_delegate(NULL); |
| 6019 | context.set_net_log(&net_log); |
| 6020 | context.Init(); |
| 6021 | |
| 6022 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6023 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6024 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6025 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6026 | LoadStateWithParam load_state = r->GetLoadState(); |
| 6027 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6028 | request_delegate.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6029 | EXPECT_EQ(ERR_ABORTED, request_delegate.request_status()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6030 | |
| 6031 | // Spin the message loop to run AsyncDelegateLogger task(s) posted after |
| 6032 | // the |request_delegate| completion task. |
| 6033 | base::RunLoop().RunUntilIdle(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6034 | } |
| 6035 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6036 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6037 | net_log.GetEntries(&entries); |
| 6038 | |
| 6039 | // Delegate info is always logged in both OnReceivedRedirect and |
| 6040 | // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the |
| 6041 | // OnResponseStarted delegate call is after cancellation, but logging is |
| 6042 | // still currently supported in that call. |
| 6043 | size_t log_position = 0; |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6044 | static const NetLogEventType kExpectedEvents[] = { |
| 6045 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
| 6046 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 6047 | }; |
| 6048 | for (NetLogEventType event : kExpectedEvents) { |
| 6049 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6050 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6051 | entries, log_position, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6052 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6053 | log_position = |
| 6054 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6055 | |
| 6056 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame] | 6057 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6058 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6059 | } |
| 6060 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6061 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6062 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6063 | } |
| 6064 | } |
| 6065 | |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6066 | namespace { |
| 6067 | |
| 6068 | const char kExtraHeader[] = "Allow-Snafu"; |
| 6069 | const char kExtraValue[] = "fubar"; |
| 6070 | |
| 6071 | class RedirectWithAdditionalHeadersDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 6072 | void OnReceivedRedirect(URLRequest* request, |
| 6073 | const RedirectInfo& redirect_info, |
| 6074 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6075 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6076 | request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false); |
| 6077 | } |
| 6078 | }; |
| 6079 | |
| 6080 | } // namespace |
| 6081 | |
| 6082 | TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6083 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6084 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6085 | GURL destination_url = |
| 6086 | http_test_server()->GetURL("/echoheader?" + std::string(kExtraHeader)); |
| 6087 | GURL original_url = |
| 6088 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6089 | RedirectWithAdditionalHeadersDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6090 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6091 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6092 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6093 | d.RunUntilComplete(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6094 | |
| 6095 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6096 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6097 | EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value)); |
| 6098 | EXPECT_EQ(kExtraValue, value); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6099 | EXPECT_FALSE(req->is_pending()); |
| 6100 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6101 | EXPECT_EQ(kExtraValue, d.data_received()); |
| 6102 | } |
| 6103 | |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6104 | namespace { |
| 6105 | |
| 6106 | const char kExtraHeaderToRemove[] = "To-Be-Removed"; |
| 6107 | |
| 6108 | class RedirectWithHeaderRemovalDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 6109 | void OnReceivedRedirect(URLRequest* request, |
| 6110 | const RedirectInfo& redirect_info, |
| 6111 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6112 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6113 | request->RemoveRequestHeaderByName(kExtraHeaderToRemove); |
| 6114 | } |
| 6115 | }; |
| 6116 | |
| 6117 | } // namespace |
| 6118 | |
| 6119 | TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6120 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6121 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6122 | GURL destination_url = http_test_server()->GetURL( |
| 6123 | "/echoheader?" + std::string(kExtraHeaderToRemove)); |
| 6124 | GURL original_url = |
| 6125 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6126 | RedirectWithHeaderRemovalDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6127 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6128 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6129 | req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false); |
| 6130 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6131 | d.RunUntilComplete(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6132 | |
| 6133 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6134 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6135 | EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6136 | EXPECT_FALSE(req->is_pending()); |
| 6137 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6138 | EXPECT_EQ("None", d.data_received()); |
| 6139 | } |
| 6140 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6141 | TEST_F(URLRequestTestHTTP, CancelAfterStart) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6142 | TestDelegate d; |
| 6143 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6144 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6145 | GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d, |
| 6146 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6147 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6148 | r->Start(); |
| 6149 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6150 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6151 | r->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6152 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6153 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6154 | |
| 6155 | // We expect to receive OnResponseStarted even though the request has been |
| 6156 | // cancelled. |
| 6157 | EXPECT_EQ(1, d.response_started_count()); |
| 6158 | EXPECT_EQ(0, d.bytes_received()); |
| 6159 | EXPECT_FALSE(d.received_data_before_response()); |
| 6160 | } |
| 6161 | } |
| 6162 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6163 | TEST_F(URLRequestTestHTTP, CancelInResponseStarted) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6164 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6165 | |
| 6166 | TestDelegate d; |
| 6167 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6168 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6169 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6170 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6171 | |
| 6172 | d.set_cancel_in_response_started(true); |
| 6173 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6174 | r->Start(); |
| 6175 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6176 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6177 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6178 | |
| 6179 | EXPECT_EQ(1, d.response_started_count()); |
| 6180 | EXPECT_EQ(0, d.bytes_received()); |
| 6181 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6182 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6183 | } |
| 6184 | } |
| 6185 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6186 | TEST_F(URLRequestTestHTTP, CancelOnDataReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6187 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6188 | |
| 6189 | TestDelegate d; |
| 6190 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6191 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6192 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 6193 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6194 | |
| 6195 | d.set_cancel_in_received_data(true); |
| 6196 | |
| 6197 | r->Start(); |
| 6198 | EXPECT_TRUE(r->is_pending()); |
| 6199 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6200 | d.RunUntilComplete(); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6201 | |
| 6202 | EXPECT_EQ(1, d.response_started_count()); |
| 6203 | EXPECT_NE(0, d.received_bytes_count()); |
| 6204 | EXPECT_FALSE(d.received_data_before_response()); |
| 6205 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
| 6206 | } |
| 6207 | } |
| 6208 | |
| 6209 | TEST_F(URLRequestTestHTTP, CancelDuringEofRead) { |
| 6210 | ASSERT_TRUE(http_test_server()->Start()); |
| 6211 | |
| 6212 | TestDelegate d; |
| 6213 | { |
| 6214 | // This returns an empty response (With headers). |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6215 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6216 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6217 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6218 | |
| 6219 | d.set_cancel_in_received_data(true); |
| 6220 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6221 | r->Start(); |
| 6222 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6223 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6224 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6225 | |
| 6226 | EXPECT_EQ(1, d.response_started_count()); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6227 | EXPECT_EQ(0, d.received_bytes_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6228 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6229 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6230 | } |
| 6231 | } |
| 6232 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6233 | TEST_F(URLRequestTestHTTP, CancelByDestroyingAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6234 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6235 | |
| 6236 | TestDelegate d; |
| 6237 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6238 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6239 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6240 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6241 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6242 | r->Start(); |
| 6243 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6244 | |
| 6245 | // The request will be implicitly canceled when it is destroyed. The |
| 6246 | // test delegate must not post a quit message when this happens because |
| 6247 | // this test doesn't actually have a message loop. The quit message would |
| 6248 | // get put on this thread's message queue and the next test would exit |
| 6249 | // early, causing problems. |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 6250 | d.set_on_complete(base::DoNothing()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6251 | } |
| 6252 | // expect things to just cleanup properly. |
| 6253 | |
kimwjdalsl | 2bb4ff0 | 2015-12-16 22:06:02 | [diff] [blame] | 6254 | // 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] | 6255 | // message loop |
| 6256 | EXPECT_FALSE(d.received_data_before_response()); |
| 6257 | EXPECT_EQ(0, d.bytes_received()); |
| 6258 | } |
| 6259 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6260 | TEST_F(URLRequestTestHTTP, CancelWhileReadingFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6261 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6262 | |
| 6263 | // populate cache |
| 6264 | { |
| 6265 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6266 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6267 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6268 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6269 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6270 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6271 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6272 | } |
| 6273 | |
| 6274 | // cancel read from cache (see bug 990242) |
| 6275 | { |
| 6276 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6277 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6278 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6279 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6280 | r->Start(); |
| 6281 | r->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6282 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6283 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6284 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6285 | EXPECT_EQ(1, d.response_started_count()); |
| 6286 | EXPECT_EQ(0, d.bytes_received()); |
| 6287 | EXPECT_FALSE(d.received_data_before_response()); |
| 6288 | } |
| 6289 | } |
| 6290 | |
| 6291 | TEST_F(URLRequestTestHTTP, PostTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6292 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6293 | HTTPUploadDataOperationTest("POST"); |
| 6294 | } |
| 6295 | |
| 6296 | TEST_F(URLRequestTestHTTP, PutTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6297 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6298 | HTTPUploadDataOperationTest("PUT"); |
| 6299 | } |
| 6300 | |
| 6301 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6302 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6303 | |
| 6304 | TestDelegate d; |
| 6305 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6306 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6307 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6308 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6309 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6310 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6311 | r->Start(); |
| 6312 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6313 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6314 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6315 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6316 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6317 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6318 | |
| 6319 | EXPECT_FALSE(d.received_data_before_response()); |
| 6320 | EXPECT_TRUE(d.data_received().empty()); |
| 6321 | } |
| 6322 | } |
| 6323 | |
| 6324 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6325 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6326 | |
| 6327 | TestDelegate d; |
| 6328 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6329 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6330 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6331 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6332 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6333 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6334 | base::FilePath dir; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 6335 | base::PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 6336 | base::SetCurrentDirectory(dir); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6337 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6338 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6339 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6340 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 6341 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 6342 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6343 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6344 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6345 | base::ThreadTaskRunnerHandle::Get().get(), path, 0, |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6346 | std::numeric_limits<uint64_t>::max(), base::Time())); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6347 | r->set_upload(std::make_unique<ElementsUploadDataStream>( |
| 6348 | std::move(element_readers), 0)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6349 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6350 | r->Start(); |
| 6351 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6352 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6353 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6354 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6355 | int64_t size64 = 0; |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 6356 | ASSERT_EQ(true, base::GetFileSize(path, &size64)); |
| 6357 | ASSERT_LE(size64, std::numeric_limits<int>::max()); |
| 6358 | int size = static_cast<int>(size64); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6359 | std::unique_ptr<char[]> buf(new char[size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6360 | |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 6361 | ASSERT_EQ(size, base::ReadFile(path, buf.get(), size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6362 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6363 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6364 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6365 | |
| 6366 | EXPECT_FALSE(d.received_data_before_response()); |
| 6367 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6368 | EXPECT_EQ(size, d.bytes_received()); |
| 6369 | EXPECT_EQ(std::string(&buf[0], size), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6370 | } |
| 6371 | } |
| 6372 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6373 | TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6374 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6375 | |
| 6376 | TestDelegate d; |
| 6377 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6378 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6379 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6380 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6381 | r->set_method("POST"); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6382 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6383 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6384 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6385 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 6386 | base::ThreadTaskRunnerHandle::Get().get(), |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6387 | base::FilePath(FILE_PATH_LITERAL( |
| 6388 | "c:\\path\\to\\non\\existant\\file.randomness.12345")), |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6389 | 0, std::numeric_limits<uint64_t>::max(), base::Time())); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6390 | r->set_upload(std::make_unique<ElementsUploadDataStream>( |
| 6391 | std::move(element_readers), 0)); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6392 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6393 | r->Start(); |
| 6394 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6395 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6396 | d.RunUntilComplete(); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6397 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6398 | EXPECT_TRUE(d.request_failed()); |
| 6399 | EXPECT_FALSE(d.received_data_before_response()); |
| 6400 | EXPECT_EQ(0, d.bytes_received()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6401 | EXPECT_EQ(ERR_FILE_NOT_FOUND, d.request_status()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6402 | } |
| 6403 | } |
| 6404 | |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6405 | namespace { |
| 6406 | |
| 6407 | // Adds a standard set of data to an upload for chunked upload integration |
| 6408 | // tests. |
| 6409 | void AddDataToUpload(ChunkedUploadDataStream::Writer* writer) { |
| 6410 | writer->AppendData("a", 1, false); |
| 6411 | writer->AppendData("bcd", 3, false); |
| 6412 | writer->AppendData("this is a longer chunk than before.", 35, false); |
| 6413 | writer->AppendData("\r\n\r\n", 4, false); |
| 6414 | writer->AppendData("0", 1, false); |
| 6415 | writer->AppendData("2323", 4, true); |
| 6416 | } |
| 6417 | |
| 6418 | // Checks that the upload data added in AddChunksToUpload() was echoed back from |
| 6419 | // the server. |
| 6420 | void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { |
| 6421 | // This should match the chunks sent by AddChunksToUpload(). |
| 6422 | const std::string expected_data = |
| 6423 | "abcdthis is a longer chunk than before.\r\n\r\n02323"; |
| 6424 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6425 | ASSERT_EQ(1, d->response_started_count()) << "request failed. Error: " |
| 6426 | << d->request_status(); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6427 | |
| 6428 | EXPECT_FALSE(d->received_data_before_response()); |
| 6429 | |
| 6430 | EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); |
| 6431 | EXPECT_EQ(expected_data, d->data_received()); |
| 6432 | } |
| 6433 | |
| 6434 | } // namespace |
| 6435 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6436 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6437 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6438 | |
| 6439 | TestDelegate d; |
| 6440 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6441 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6442 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6443 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6444 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6445 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6446 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6447 | upload_data_stream->CreateWriter(); |
| 6448 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6449 | r->set_method("POST"); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6450 | AddDataToUpload(writer.get()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6451 | r->Start(); |
| 6452 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6453 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6454 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6455 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6456 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6457 | } |
| 6458 | } |
| 6459 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6460 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6461 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6462 | |
| 6463 | TestDelegate d; |
| 6464 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6465 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6466 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6467 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6468 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6469 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6470 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6471 | upload_data_stream->CreateWriter(); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6472 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6473 | r->set_method("POST"); |
| 6474 | r->Start(); |
| 6475 | EXPECT_TRUE(r->is_pending()); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6476 | AddDataToUpload(writer.get()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6477 | d.RunUntilComplete(); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6478 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6479 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6480 | } |
| 6481 | } |
| 6482 | |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6483 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6484 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6485 | |
| 6486 | TestDelegate d; |
| 6487 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6488 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6489 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6490 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6491 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6492 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6493 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6494 | upload_data_stream->CreateWriter(); |
| 6495 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6496 | r->set_method("POST"); |
| 6497 | r->Start(); |
| 6498 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6499 | |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6500 | // Pump messages until we start sending headers.. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6501 | base::RunLoop().RunUntilIdle(); |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6502 | |
| 6503 | // And now wait for completion. |
| 6504 | base::RunLoop run_loop; |
| 6505 | d.set_on_complete(run_loop.QuitClosure()); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6506 | AddDataToUpload(writer.get()); |
Asanka Herath | f05753e | 2018-08-16 17:38:20 | [diff] [blame] | 6507 | run_loop.Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6508 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6509 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6510 | } |
| 6511 | } |
| 6512 | |
| 6513 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6514 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6515 | |
| 6516 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6517 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6518 | http_test_server()->GetURL("/with-headers.html"), DEFAULT_PRIORITY, &d, |
| 6519 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6520 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6521 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6522 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6523 | const HttpResponseHeaders* headers = req->response_headers(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6524 | |
| 6525 | // Simple sanity check that response_info() accesses the same data. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6526 | EXPECT_EQ(headers, req->response_info().headers.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6527 | |
| 6528 | std::string header; |
| 6529 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 6530 | EXPECT_EQ("private", header); |
| 6531 | |
| 6532 | header.clear(); |
| 6533 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 6534 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 6535 | |
| 6536 | // The response has two "X-Multiple-Entries" headers. |
| 6537 | // This verfies our output has them concatenated together. |
| 6538 | header.clear(); |
| 6539 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 6540 | EXPECT_EQ("a, b", header); |
| 6541 | } |
| 6542 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6543 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 6544 | // security state. (see http://crbug.com/550977). |
| 6545 | #if !defined(OS_IOS) |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6546 | TEST_F(URLRequestTestHTTP, ProcessSTS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6547 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6548 | https_test_server.SetSSLConfig( |
| 6549 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6550 | https_test_server.ServeFilesFromSourceDirectory( |
| 6551 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6552 | ASSERT_TRUE(https_test_server.Start()); |
| 6553 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6554 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6555 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6556 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6557 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6558 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6559 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6560 | d.RunUntilComplete(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6561 | |
| 6562 | TransportSecurityState* security_state = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6563 | default_context().transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6564 | TransportSecurityState::STSState sts_state; |
| 6565 | TransportSecurityState::PKPState pkp_state; |
| 6566 | EXPECT_TRUE( |
| 6567 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6568 | EXPECT_FALSE( |
| 6569 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6570 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6571 | sts_state.upgrade_mode); |
| 6572 | EXPECT_TRUE(sts_state.include_subdomains); |
| 6573 | EXPECT_FALSE(pkp_state.include_subdomains); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6574 | #if defined(OS_ANDROID) |
| 6575 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6576 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6577 | EXPECT_FALSE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6578 | #endif |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6579 | } |
| 6580 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6581 | TEST_F(URLRequestTestHTTP, STSNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6582 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6583 | https_test_server.ServeFilesFromSourceDirectory( |
| 6584 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6585 | ASSERT_TRUE(https_test_server.Start()); |
| 6586 | // Make sure this test fails if the test server is changed to not |
| 6587 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6588 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 6589 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6590 | |
| 6591 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6592 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6593 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6594 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6595 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6596 | d.RunUntilComplete(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6597 | TransportSecurityState* security_state = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6598 | default_context().transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6599 | TransportSecurityState::STSState sts_state; |
| 6600 | EXPECT_FALSE( |
| 6601 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6602 | } |
| 6603 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6604 | namespace { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 6605 | const char kExpectCTStaticHostname[] = "expect-ct.preloaded.test"; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6606 | const char kPKPReportUri[] = "http://report-uri.preloaded.test/pkp"; |
| 6607 | const char kPKPHost[] = "with-report-uri-pkp.preloaded.test"; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6608 | } // namespace |
| 6609 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6610 | // 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] | 6611 | TEST_F(URLRequestTestHTTP, ProcessPKPAndSendReport) { |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6612 | GURL report_uri(kPKPReportUri); |
Hiroki Nakagawa | 4e4967bcb | 2018-10-11 04:09:51 | [diff] [blame] | 6613 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6614 | https_test_server.SetSSLConfig( |
| 6615 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6616 | https_test_server.ServeFilesFromSourceDirectory( |
| 6617 | base::FilePath(kTestFilePath)); |
| 6618 | ASSERT_TRUE(https_test_server.Start()); |
| 6619 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6620 | std::string test_server_hostname = kPKPHost; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6621 | |
| 6622 | // Set up a pin for |test_server_hostname|. |
| 6623 | TransportSecurityState security_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6624 | security_state.EnableStaticPinsForTesting(); |
| 6625 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6626 | |
| 6627 | MockCertificateReportSender mock_report_sender; |
| 6628 | security_state.SetReportSender(&mock_report_sender); |
| 6629 | |
| 6630 | // Set up a MockCertVerifier to trigger a violation of the previously |
| 6631 | // set pin. |
| 6632 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6633 | ASSERT_TRUE(cert); |
| 6634 | |
| 6635 | MockCertVerifier cert_verifier; |
| 6636 | CertVerifyResult verify_result; |
| 6637 | verify_result.verified_cert = cert; |
| 6638 | verify_result.is_issued_by_known_root = true; |
| 6639 | HashValue hash3; |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6640 | ASSERT_TRUE( |
| 6641 | hash3.FromString("sha256/3333333333333333333333333333333333333333333=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6642 | verify_result.public_key_hashes.push_back(hash3); |
| 6643 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6644 | |
| 6645 | TestNetworkDelegate network_delegate; |
| 6646 | TestURLRequestContext context(true); |
| 6647 | context.set_transport_security_state(&security_state); |
| 6648 | context.set_network_delegate(&network_delegate); |
| 6649 | context.set_cert_verifier(&cert_verifier); |
| 6650 | context.Init(); |
| 6651 | |
| 6652 | // Now send a request to trigger the violation. |
| 6653 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6654 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6655 | https_test_server.GetURL(test_server_hostname, "/simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6656 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6657 | violating_request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6658 | d.RunUntilComplete(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6659 | |
| 6660 | // Check that a report was sent. |
| 6661 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 6662 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 6663 | EXPECT_EQ("application/json; charset=utf-8", |
| 6664 | mock_report_sender.latest_content_type()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6665 | std::unique_ptr<base::Value> value( |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6666 | base::JSONReader::Read(mock_report_sender.latest_report())); |
| 6667 | ASSERT_TRUE(value); |
jdoerrie | e48b26a | 2017-12-09 14:19:08 | [diff] [blame] | 6668 | ASSERT_TRUE(value->is_dict()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6669 | base::DictionaryValue* report_dict; |
| 6670 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 6671 | std::string report_hostname; |
| 6672 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 6673 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 6674 | } |
| 6675 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6676 | // Tests that reports do not get sent on requests to static pkp hosts that |
| 6677 | // don't have pin violations. |
| 6678 | TEST_F(URLRequestTestHTTP, ProcessPKPWithNoViolation) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6679 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6680 | https_test_server.SetSSLConfig( |
| 6681 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6682 | https_test_server.ServeFilesFromSourceDirectory( |
| 6683 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6684 | ASSERT_TRUE(https_test_server.Start()); |
| 6685 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6686 | std::string test_server_hostname = kPKPHost; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6687 | |
| 6688 | TransportSecurityState security_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6689 | security_state.EnableStaticPinsForTesting(); |
| 6690 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6691 | MockCertificateReportSender mock_report_sender; |
| 6692 | security_state.SetReportSender(&mock_report_sender); |
| 6693 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6694 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6695 | ASSERT_TRUE(cert); |
Hiroki Nakagawa | 4e4967bcb | 2018-10-11 04:09:51 | [diff] [blame] | 6696 | MockCertVerifier mock_cert_verifier; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6697 | CertVerifyResult verify_result; |
| 6698 | verify_result.verified_cert = cert; |
| 6699 | verify_result.is_issued_by_known_root = true; |
| 6700 | HashValue hash; |
| 6701 | // The expected value of GoodPin1 used by |test_default::kHSTSSource|. |
| 6702 | ASSERT_TRUE( |
| 6703 | hash.FromString("sha256/Nn8jk5By4Vkq6BeOVZ7R7AC6XUUBZsWmUbJR1f1Y5FY=")); |
| 6704 | verify_result.public_key_hashes.push_back(hash); |
| 6705 | mock_cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6706 | |
| 6707 | TestNetworkDelegate network_delegate; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6708 | TestURLRequestContext context(true); |
| 6709 | context.set_transport_security_state(&security_state); |
| 6710 | context.set_network_delegate(&network_delegate); |
| 6711 | context.set_cert_verifier(&mock_cert_verifier); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6712 | context.Init(); |
| 6713 | |
| 6714 | // Now send a request that does not trigger the violation. |
| 6715 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6716 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6717 | https_test_server.GetURL(test_server_hostname, "/simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6718 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6719 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6720 | d.RunUntilComplete(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6721 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6722 | // Check that the request succeeded, a report was not sent and the pkp was |
| 6723 | // not bypassed. |
| 6724 | EXPECT_EQ(OK, d.request_status()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6725 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 6726 | EXPECT_EQ(std::string(), mock_report_sender.latest_report()); |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6727 | TransportSecurityState::STSState sts_state; |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6728 | TransportSecurityState::PKPState pkp_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6729 | EXPECT_TRUE(security_state.GetStaticDomainState(test_server_hostname, |
| 6730 | &sts_state, &pkp_state)); |
| 6731 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
| 6732 | EXPECT_FALSE(request->ssl_info().pkp_bypassed); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6733 | } |
| 6734 | |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6735 | TEST_F(URLRequestTestHTTP, PKPBypassRecorded) { |
| 6736 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6737 | https_test_server.SetSSLConfig( |
| 6738 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6739 | https_test_server.ServeFilesFromSourceDirectory( |
| 6740 | base::FilePath(kTestFilePath)); |
| 6741 | ASSERT_TRUE(https_test_server.Start()); |
| 6742 | |
| 6743 | // Set up a MockCertVerifier to be a local root that violates the pin |
| 6744 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6745 | ASSERT_TRUE(cert); |
| 6746 | |
| 6747 | MockCertVerifier cert_verifier; |
| 6748 | CertVerifyResult verify_result; |
| 6749 | verify_result.verified_cert = cert; |
| 6750 | verify_result.is_issued_by_known_root = false; |
| 6751 | HashValue hash; |
| 6752 | ASSERT_TRUE( |
| 6753 | hash.FromString("sha256/1111111111111111111111111111111111111111111=")); |
| 6754 | verify_result.public_key_hashes.push_back(hash); |
| 6755 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6756 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6757 | std::string test_server_hostname = kPKPHost; |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6758 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6759 | // Set up PKP |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6760 | TransportSecurityState security_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6761 | security_state.EnableStaticPinsForTesting(); |
| 6762 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 6763 | MockCertificateReportSender mock_report_sender; |
| 6764 | security_state.SetReportSender(&mock_report_sender); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6765 | |
| 6766 | TestNetworkDelegate network_delegate; |
| 6767 | TestURLRequestContext context(true); |
| 6768 | context.set_transport_security_state(&security_state); |
| 6769 | context.set_network_delegate(&network_delegate); |
| 6770 | context.set_cert_verifier(&cert_verifier); |
| 6771 | context.Init(); |
| 6772 | |
| 6773 | TestDelegate d; |
| 6774 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6775 | https_test_server.GetURL(test_server_hostname, "/simple.html"), |
| 6776 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6777 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6778 | d.RunUntilComplete(); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6779 | |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6780 | // Check that the request succeeded, a report was not sent and the PKP was |
| 6781 | // bypassed. |
| 6782 | EXPECT_EQ(OK, d.request_status()); |
| 6783 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 6784 | EXPECT_EQ(std::string(), mock_report_sender.latest_report()); |
| 6785 | TransportSecurityState::STSState sts_state; |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6786 | TransportSecurityState::PKPState pkp_state; |
Matt Mueller | 230996f1 | 2018-10-22 19:39:44 | [diff] [blame] | 6787 | EXPECT_TRUE(security_state.GetStaticDomainState(test_server_hostname, |
| 6788 | &sts_state, &pkp_state)); |
| 6789 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 6790 | EXPECT_TRUE(request->ssl_info().pkp_bypassed); |
| 6791 | } |
| 6792 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6793 | TEST_F(URLRequestTestHTTP, ProcessSTSOnce) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6794 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6795 | https_test_server.SetSSLConfig( |
| 6796 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6797 | https_test_server.ServeFilesFromSourceDirectory( |
| 6798 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6799 | ASSERT_TRUE(https_test_server.Start()); |
| 6800 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6801 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6802 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6803 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6804 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6805 | https_test_server.GetURL("/hsts-multiple-headers.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6806 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6807 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6808 | d.RunUntilComplete(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6809 | |
| 6810 | // We should have set parameters from the first header, not the second. |
| 6811 | TransportSecurityState* security_state = |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 6812 | default_context().transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6813 | TransportSecurityState::STSState sts_state; |
| 6814 | EXPECT_TRUE( |
| 6815 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6816 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6817 | sts_state.upgrade_mode); |
| 6818 | EXPECT_FALSE(sts_state.include_subdomains); |
| 6819 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6820 | } |
| 6821 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6822 | // An ExpectCTReporter that records the number of times OnExpectCTFailed() was |
| 6823 | // called. |
| 6824 | class MockExpectCTReporter : public TransportSecurityState::ExpectCTReporter { |
| 6825 | public: |
| 6826 | MockExpectCTReporter() : num_failures_(0) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 6827 | ~MockExpectCTReporter() override = default; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6828 | |
| 6829 | void OnExpectCTFailed(const HostPortPair& host_port_pair, |
| 6830 | const GURL& report_uri, |
estark | ae028b46 | 2017-06-20 23:25:01 | [diff] [blame] | 6831 | base::Time expiration, |
estark | bf1b5296 | 2017-05-05 17:05:25 | [diff] [blame] | 6832 | const X509Certificate* validated_certificate_chain, |
| 6833 | const X509Certificate* served_certificate_chain, |
| 6834 | const SignedCertificateTimestampAndStatusList& |
| 6835 | signed_certificate_timestamps) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6836 | num_failures_++; |
| 6837 | } |
| 6838 | |
| 6839 | uint32_t num_failures() { return num_failures_; } |
| 6840 | |
| 6841 | private: |
| 6842 | uint32_t num_failures_; |
| 6843 | }; |
| 6844 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 6845 | // A CTPolicyEnforcer that returns a default CTPolicyCompliance value |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6846 | // for every certificate. |
| 6847 | class MockCTPolicyEnforcer : public CTPolicyEnforcer { |
| 6848 | public: |
| 6849 | MockCTPolicyEnforcer() |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 6850 | : default_result_(ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 6851 | ~MockCTPolicyEnforcer() override = default; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6852 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 6853 | ct::CTPolicyCompliance CheckCompliance( |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6854 | X509Certificate* cert, |
Ryan Sleevi | 8a9c9c1 | 2018-05-09 02:36:23 | [diff] [blame] | 6855 | const ct::SCTList& verified_scts, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 6856 | const NetLogWithSource& net_log) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6857 | return default_result_; |
| 6858 | } |
| 6859 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 6860 | void set_default_result(ct::CTPolicyCompliance default_result) { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6861 | default_result_ = default_result; |
| 6862 | } |
| 6863 | |
| 6864 | private: |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 6865 | ct::CTPolicyCompliance default_result_; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6866 | }; |
| 6867 | |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 6868 | // Tests that Expect CT headers for the preload list are processed correctly. |
| 6869 | TEST_F(URLRequestTestHTTP, PreloadExpectCTHeader) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 6870 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 6871 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6872 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6873 | https_test_server.SetSSLConfig( |
| 6874 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6875 | https_test_server.ServeFilesFromSourceDirectory( |
| 6876 | base::FilePath(kTestFilePath)); |
| 6877 | ASSERT_TRUE(https_test_server.Start()); |
| 6878 | |
| 6879 | MockExpectCTReporter reporter; |
| 6880 | TransportSecurityState transport_security_state; |
| 6881 | transport_security_state.enable_static_expect_ct_ = true; |
| 6882 | transport_security_state.SetExpectCTReporter(&reporter); |
| 6883 | |
| 6884 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 6885 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6886 | ASSERT_TRUE(cert); |
| 6887 | MockCertVerifier cert_verifier; |
| 6888 | CertVerifyResult verify_result; |
| 6889 | verify_result.verified_cert = cert; |
| 6890 | verify_result.is_issued_by_known_root = true; |
| 6891 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6892 | |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 6893 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to trigger an Expect |
| 6894 | // CT violation. |
| 6895 | DoNothingCTVerifier ct_verifier; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6896 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 6897 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 6898 | ct::CTPolicyCompliance::CT_POLICY_NOT_ENOUGH_SCTS); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6899 | |
| 6900 | TestNetworkDelegate network_delegate; |
| 6901 | // Use a MockHostResolver (which by default maps all hosts to |
| 6902 | // 127.0.0.1) so that the request can be sent to a site on the Expect |
| 6903 | // CT preload list. |
| 6904 | MockHostResolver host_resolver; |
| 6905 | TestURLRequestContext context(true); |
| 6906 | context.set_host_resolver(&host_resolver); |
| 6907 | context.set_transport_security_state(&transport_security_state); |
| 6908 | context.set_network_delegate(&network_delegate); |
| 6909 | context.set_cert_verifier(&cert_verifier); |
| 6910 | context.set_cert_transparency_verifier(&ct_verifier); |
vabr | 13d0074 | 2017-06-05 10:31:46 | [diff] [blame] | 6911 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6912 | context.Init(); |
| 6913 | |
| 6914 | // Now send a request to trigger the violation. |
| 6915 | TestDelegate d; |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 6916 | GURL url = https_test_server.GetURL("/expect-ct-header-preload.html"); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6917 | GURL::Replacements replace_host; |
| 6918 | replace_host.SetHostStr(kExpectCTStaticHostname); |
| 6919 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6920 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
| 6921 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6922 | violating_request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6923 | d.RunUntilComplete(); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 6924 | |
| 6925 | EXPECT_EQ(1u, reporter.num_failures()); |
| 6926 | } |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 6927 | |
| 6928 | // Tests that Expect CT HTTP headers are processed correctly. |
| 6929 | TEST_F(URLRequestTestHTTP, ExpectCTHeader) { |
| 6930 | base::test::ScopedFeatureList feature_list; |
| 6931 | feature_list.InitAndEnableFeature( |
| 6932 | TransportSecurityState::kDynamicExpectCTFeature); |
| 6933 | |
| 6934 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6935 | https_test_server.SetSSLConfig( |
| 6936 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6937 | https_test_server.ServeFilesFromSourceDirectory( |
| 6938 | base::FilePath(kTestFilePath)); |
| 6939 | ASSERT_TRUE(https_test_server.Start()); |
| 6940 | |
| 6941 | MockExpectCTReporter reporter; |
| 6942 | TransportSecurityState transport_security_state; |
| 6943 | transport_security_state.SetExpectCTReporter(&reporter); |
| 6944 | |
| 6945 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 6946 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6947 | ASSERT_TRUE(cert); |
| 6948 | MockCertVerifier cert_verifier; |
| 6949 | CertVerifyResult verify_result; |
| 6950 | verify_result.verified_cert = cert; |
| 6951 | verify_result.is_issued_by_known_root = true; |
| 6952 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6953 | |
| 6954 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 6955 | // compliance. |
| 6956 | DoNothingCTVerifier ct_verifier; |
| 6957 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 6958 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 6959 | ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 6960 | |
| 6961 | TestNetworkDelegate network_delegate; |
| 6962 | // Use a MockHostResolver (which by default maps all hosts to |
| 6963 | // 127.0.0.1). |
| 6964 | MockHostResolver host_resolver; |
| 6965 | TestURLRequestContext context(true); |
| 6966 | context.set_host_resolver(&host_resolver); |
| 6967 | context.set_transport_security_state(&transport_security_state); |
| 6968 | context.set_network_delegate(&network_delegate); |
| 6969 | context.set_cert_verifier(&cert_verifier); |
| 6970 | context.set_cert_transparency_verifier(&ct_verifier); |
| 6971 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 6972 | context.Init(); |
| 6973 | |
| 6974 | // Now send a request to trigger the header processing. |
| 6975 | TestDelegate d; |
| 6976 | GURL url = https_test_server.GetURL("/expect-ct-header.html"); |
| 6977 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 6978 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 6979 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6980 | d.RunUntilComplete(); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 6981 | |
| 6982 | TransportSecurityState::ExpectCTState state; |
| 6983 | ASSERT_TRUE( |
| 6984 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 6985 | EXPECT_TRUE(state.enforce); |
| 6986 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 6987 | } |
| 6988 | |
| 6989 | // Tests that if multiple Expect CT HTTP headers are sent, they are all |
| 6990 | // processed. |
| 6991 | TEST_F(URLRequestTestHTTP, MultipleExpectCTHeaders) { |
| 6992 | base::test::ScopedFeatureList feature_list; |
| 6993 | feature_list.InitAndEnableFeature( |
| 6994 | TransportSecurityState::kDynamicExpectCTFeature); |
| 6995 | |
| 6996 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6997 | https_test_server.SetSSLConfig( |
| 6998 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6999 | https_test_server.ServeFilesFromSourceDirectory( |
| 7000 | base::FilePath(kTestFilePath)); |
| 7001 | ASSERT_TRUE(https_test_server.Start()); |
| 7002 | |
| 7003 | MockExpectCTReporter reporter; |
| 7004 | TransportSecurityState transport_security_state; |
| 7005 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7006 | |
| 7007 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7008 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7009 | ASSERT_TRUE(cert); |
| 7010 | MockCertVerifier cert_verifier; |
| 7011 | CertVerifyResult verify_result; |
| 7012 | verify_result.verified_cert = cert; |
| 7013 | verify_result.is_issued_by_known_root = true; |
| 7014 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7015 | |
| 7016 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 7017 | // compliance. |
| 7018 | DoNothingCTVerifier ct_verifier; |
| 7019 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7020 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7021 | ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7022 | |
| 7023 | TestNetworkDelegate network_delegate; |
| 7024 | // Use a MockHostResolver (which by default maps all hosts to |
| 7025 | // 127.0.0.1). |
| 7026 | MockHostResolver host_resolver; |
| 7027 | TestURLRequestContext context(true); |
| 7028 | context.set_host_resolver(&host_resolver); |
| 7029 | context.set_transport_security_state(&transport_security_state); |
| 7030 | context.set_network_delegate(&network_delegate); |
| 7031 | context.set_cert_verifier(&cert_verifier); |
| 7032 | context.set_cert_transparency_verifier(&ct_verifier); |
| 7033 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 7034 | context.Init(); |
| 7035 | |
| 7036 | // Now send a request to trigger the header processing. |
| 7037 | TestDelegate d; |
| 7038 | GURL url = https_test_server.GetURL("/expect-ct-header-multiple.html"); |
| 7039 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7040 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7041 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7042 | d.RunUntilComplete(); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7043 | |
| 7044 | TransportSecurityState::ExpectCTState state; |
| 7045 | ASSERT_TRUE( |
| 7046 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 7047 | EXPECT_TRUE(state.enforce); |
| 7048 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 7049 | } |
| 7050 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 7051 | #endif // !defined(OS_IOS) |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7052 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7053 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7054 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7055 | |
| 7056 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7057 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7058 | http_test_server()->GetURL("/content-type-normalization.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7059 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7060 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7061 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7062 | |
| 7063 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7064 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7065 | EXPECT_EQ("text/html", mime_type); |
| 7066 | |
| 7067 | std::string charset; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7068 | req->GetCharset(&charset); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7069 | EXPECT_EQ("utf-8", charset); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7070 | req->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7071 | } |
| 7072 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7073 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) { |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7074 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7075 | GURL data_url("data:,foo"); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7076 | DataProtocolHandler data_protocol_handler; |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7077 | EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7078 | |
| 7079 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7080 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7081 | } |
| 7082 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7083 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7084 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) { |
| 7085 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
| 7086 | GURL file_url("file:///foo.txt"); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 7087 | FileProtocolHandler file_protocol_handler( |
| 7088 | base::ThreadTaskRunnerHandle::Get()); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7089 | EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url)); |
| 7090 | |
| 7091 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7092 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(file_url)); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7093 | } |
| 7094 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7095 | TEST_F(URLRequestTestHTTP, RestrictFileRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7096 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7097 | |
| 7098 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7099 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7100 | http_test_server()->GetURL("/redirect-to-file.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7101 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7102 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7103 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7104 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7105 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7106 | |
| 7107 | // The redirect should have been rejected before reporting it to the caller. |
| 7108 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7109 | } |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7110 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7111 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7112 | TEST_F(URLRequestTestHTTP, RestrictDataRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7113 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7114 | |
| 7115 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7116 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7117 | http_test_server()->GetURL("/redirect-to-data.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7118 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7119 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7120 | d.RunUntilComplete(); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7121 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7122 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7123 | |
| 7124 | // The redirect should have been rejected before reporting it to the |
| 7125 | // caller. See https://crbug.com/723796 |
| 7126 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7127 | } |
| 7128 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7129 | // Test that redirects to invalid URLs are rejected. See |
| 7130 | // https://crbug.com/462272. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7131 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7132 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7133 | |
| 7134 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7135 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7136 | http_test_server()->GetURL("/redirect-to-invalid-url.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7137 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7138 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7139 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7140 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7141 | EXPECT_EQ(1, d.response_started_count()); |
| 7142 | EXPECT_EQ(ERR_INVALID_REDIRECT, d.request_status()); |
| 7143 | |
| 7144 | // The redirect should have been rejected before reporting it to the caller. |
| 7145 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7146 | } |
| 7147 | |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7148 | // Make sure redirects are cached, despite not reading their bodies. |
| 7149 | TEST_F(URLRequestTestHTTP, CacheRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7150 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7151 | GURL redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7152 | http_test_server()->GetURL("/redirect302-to-echo-cacheable"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7153 | |
| 7154 | { |
| 7155 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7156 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7157 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7158 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7159 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7160 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7161 | EXPECT_EQ(1, d.received_redirect_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7162 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7163 | } |
| 7164 | |
| 7165 | { |
| 7166 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7167 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7168 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7169 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 7170 | d.RunUntilRedirect(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7171 | |
| 7172 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7173 | EXPECT_EQ(0, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7174 | EXPECT_TRUE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7175 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame^] | 7176 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 7177 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7178 | d.RunUntilComplete(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7179 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7180 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7181 | EXPECT_EQ(OK, d.request_status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7182 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7183 | } |
| 7184 | } |
| 7185 | |
| 7186 | // Make sure a request isn't cached when a NetworkDelegate forces a redirect |
| 7187 | // when the headers are read, since the body won't have been read. |
| 7188 | TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7189 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7190 | // URL that is normally cached. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7191 | GURL initial_url = http_test_server()->GetURL("/cachetime"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7192 | |
| 7193 | { |
| 7194 | // Set up the TestNetworkDelegate tp force a redirect. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7195 | GURL redirect_to_url = http_test_server()->GetURL("/echo"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7196 | default_network_delegate_.set_redirect_on_headers_received_url( |
| 7197 | redirect_to_url); |
| 7198 | |
| 7199 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7200 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7201 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7202 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7203 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7204 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7205 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7206 | EXPECT_EQ(redirect_to_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7207 | } |
| 7208 | |
| 7209 | { |
| 7210 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7211 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7212 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7213 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7214 | d.RunUntilComplete(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7215 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7216 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7217 | EXPECT_FALSE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7218 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7219 | EXPECT_EQ(initial_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7220 | } |
| 7221 | } |
| 7222 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7223 | // Tests that redirection to an unsafe URL is allowed when it has been marked as |
| 7224 | // safe. |
| 7225 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7226 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7227 | |
| 7228 | GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url"); |
| 7229 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7230 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7231 | |
| 7232 | TestDelegate d; |
| 7233 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7234 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7235 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 7236 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7237 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7238 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7239 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7240 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7241 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7242 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7243 | EXPECT_EQ(unsafe_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7244 | EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received()); |
| 7245 | } |
| 7246 | } |
| 7247 | |
| 7248 | // Tests that a redirect to a different unsafe URL is blocked, even after adding |
| 7249 | // some other URL to the whitelist. |
| 7250 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7251 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7252 | |
| 7253 | GURL unsafe_url("data:text/html,something"); |
| 7254 | GURL different_unsafe_url("data:text/html,something-else"); |
| 7255 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7256 | default_network_delegate_.set_allowed_unsafe_redirect_url( |
| 7257 | different_unsafe_url); |
| 7258 | |
| 7259 | TestDelegate d; |
| 7260 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7261 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7262 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 7263 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7264 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7265 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7266 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7267 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7268 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7269 | |
| 7270 | // The redirect should have been rejected before reporting it to the caller. |
| 7271 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7272 | } |
| 7273 | } |
| 7274 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7275 | // Redirects from an URL with fragment to an unsafe URL with fragment should |
| 7276 | // be allowed, and the reference fragment of the target URL should be preserved. |
| 7277 | TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7278 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7279 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7280 | GURL original_url(http_test_server()->GetURL("/original#fragment1")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7281 | GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 7282 | GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 7283 | |
| 7284 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 7285 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7286 | |
| 7287 | TestDelegate d; |
| 7288 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7289 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7290 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7291 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7292 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7293 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7294 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7295 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7296 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7297 | EXPECT_EQ(original_url, r->original_url()); |
| 7298 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7299 | } |
| 7300 | } |
| 7301 | |
| 7302 | // When a delegate has specified a safe redirect URL, but it does not match the |
| 7303 | // redirect target, then do not prevent the reference fragment from being added. |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7304 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7305 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7306 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7307 | GURL original_url(http_test_server()->GetURL("/original#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7308 | GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7309 | GURL redirect_url(http_test_server()->GetURL("/target")); |
| 7310 | GURL expected_redirect_url( |
| 7311 | http_test_server()->GetURL("/target#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7312 | |
| 7313 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 7314 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 7315 | |
| 7316 | TestDelegate d; |
| 7317 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7318 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7319 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7320 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7321 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7322 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7323 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7324 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7325 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7326 | EXPECT_EQ(original_url, r->original_url()); |
| 7327 | EXPECT_EQ(expected_redirect_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 7328 | } |
| 7329 | } |
| 7330 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7331 | // When a delegate has specified a safe redirect URL, assume that the redirect |
| 7332 | // URL should not be changed. In particular, the reference fragment should not |
| 7333 | // be modified. |
| 7334 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7335 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7336 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7337 | GURL original_url( |
| 7338 | http_test_server()->GetURL("/original#should-not-be-appended")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7339 | GURL redirect_url("data:text/html,expect-no-reference-fragment"); |
| 7340 | |
| 7341 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 7342 | default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url); |
| 7343 | |
| 7344 | TestDelegate d; |
| 7345 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7346 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7347 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7348 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7349 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7350 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7351 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7352 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7353 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7354 | EXPECT_EQ(original_url, r->original_url()); |
| 7355 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7356 | } |
| 7357 | } |
| 7358 | |
| 7359 | // When a URLRequestRedirectJob is created, the redirection must be followed and |
| 7360 | // the reference fragment of the target URL must not be modified. |
| 7361 | TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7362 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7363 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7364 | GURL original_url( |
| 7365 | http_test_server()->GetURL("/original#should-not-be-appended")); |
| 7366 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7367 | |
| 7368 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7369 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7370 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7371 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7372 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7373 | r.get(), &default_network_delegate_, redirect_url, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7374 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 7375 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7376 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7377 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7378 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7379 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7380 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7381 | EXPECT_EQ(original_url, r->original_url()); |
| 7382 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7383 | } |
| 7384 | |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7385 | TEST_F(URLRequestTestHTTP, UnsupportedReferrerScheme) { |
| 7386 | ASSERT_TRUE(http_test_server()->Start()); |
| 7387 | |
| 7388 | const std::string referrer("foobar://totally.legit.referrer"); |
| 7389 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7390 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7391 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7392 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7393 | req->SetReferrer(referrer); |
| 7394 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7395 | d.RunUntilComplete(); |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 7396 | |
| 7397 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 7398 | } |
| 7399 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7400 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7401 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7402 | |
| 7403 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7404 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7405 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7406 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7407 | req->SetReferrer("http://user:[email protected]/"); |
| 7408 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7409 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7410 | |
| 7411 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 7412 | } |
| 7413 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7414 | TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7415 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7416 | |
| 7417 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7418 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7419 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7420 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7421 | req->SetReferrer("http://foo.com/test#fragment"); |
| 7422 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7423 | d.RunUntilComplete(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7424 | |
| 7425 | EXPECT_EQ(std::string("http://foo.com/test"), d.data_received()); |
| 7426 | } |
| 7427 | |
| 7428 | TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7429 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7430 | |
| 7431 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7432 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7433 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 7434 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7435 | req->SetReferrer("http://foo.com/test#fragment"); |
| 7436 | req->SetReferrer(""); |
| 7437 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7438 | d.RunUntilComplete(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 7439 | |
| 7440 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 7441 | } |
| 7442 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7443 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7444 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7445 | |
| 7446 | TestDelegate d; |
| 7447 | { |
| 7448 | d.set_cancel_in_received_redirect(true); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7449 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7450 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 7451 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7452 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7453 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7454 | |
| 7455 | EXPECT_EQ(1, d.response_started_count()); |
| 7456 | EXPECT_EQ(0, d.bytes_received()); |
| 7457 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7458 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7459 | } |
| 7460 | } |
| 7461 | |
| 7462 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7463 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7464 | |
| 7465 | TestDelegate d; |
| 7466 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7467 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7468 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7469 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7470 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7471 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 7472 | d.RunUntilRedirect(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7473 | |
| 7474 | EXPECT_EQ(1, d.received_redirect_count()); |
Shivani Sharma | c18f976 | 2017-10-23 16:43:23 | [diff] [blame] | 7475 | EXPECT_TRUE(d.have_full_request_headers()); |
| 7476 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 7477 | d.ClearFullRequestHeaders(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7478 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame^] | 7479 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 7480 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7481 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7482 | |
| 7483 | EXPECT_EQ(1, d.response_started_count()); |
| 7484 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7485 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7486 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7487 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 7488 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 7489 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7490 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 7491 | |
| 7492 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 7493 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7494 | EXPECT_EQ(contents, d.data_received()); |
| 7495 | } |
| 7496 | } |
| 7497 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7498 | TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7499 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7500 | |
| 7501 | TestDelegate d; |
| 7502 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7503 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7504 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7505 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7506 | |
| 7507 | EXPECT_FALSE(d.have_full_request_headers()); |
| 7508 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7509 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 7510 | d.RunUntilRedirect(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7511 | |
| 7512 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7513 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame^] | 7514 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 7515 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7516 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7517 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7518 | GURL target_url(http_test_server()->GetURL("/with-headers.html")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7519 | EXPECT_EQ(1, d.response_started_count()); |
| 7520 | EXPECT_TRUE(d.have_full_request_headers()); |
| 7521 | CheckFullRequestHeaders(d.full_request_headers(), target_url); |
| 7522 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7523 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7524 | |
| 7525 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 7526 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 7527 | path = path.Append(kTestFilePath); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7528 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 7529 | |
| 7530 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 7531 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 7532 | EXPECT_EQ(contents, d.data_received()); |
| 7533 | } |
| 7534 | } |
| 7535 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame^] | 7536 | TEST_F(URLRequestTestHTTP, DeferredRedirect_ModifiedHeaders) { |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 7537 | ASSERT_TRUE(http_test_server()->Start()); |
| 7538 | |
| 7539 | TestDelegate d; |
| 7540 | { |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 7541 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7542 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 7543 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7544 | |
| 7545 | // Set initial headers for the request. |
| 7546 | req->SetExtraRequestHeaderByName("Header1", "Value1", true /* overwrite */); |
| 7547 | req->SetExtraRequestHeaderByName("Header2", "Value2", true /* overwrite */); |
| 7548 | |
| 7549 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 7550 | d.RunUntilRedirect(); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 7551 | |
| 7552 | // Initial request should only have initial headers. |
| 7553 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7554 | EXPECT_TRUE(d.have_full_request_headers()); |
| 7555 | const HttpRequestHeaders& sent_headers1 = d.full_request_headers(); |
| 7556 | std::string sent_value; |
| 7557 | EXPECT_TRUE(sent_headers1.GetHeader("Header1", &sent_value)); |
| 7558 | EXPECT_EQ("Value1", sent_value); |
| 7559 | EXPECT_TRUE(sent_headers1.GetHeader("Header2", &sent_value)); |
| 7560 | EXPECT_EQ("Value2", sent_value); |
| 7561 | EXPECT_FALSE(sent_headers1.GetHeader("Header3", &sent_value)); |
| 7562 | d.ClearFullRequestHeaders(); |
| 7563 | |
| 7564 | // Overwrite Header2 and add Header3. |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame^] | 7565 | net::HttpRequestHeaders modified_headers; |
| 7566 | modified_headers.SetHeader("Header2", ""); |
| 7567 | modified_headers.SetHeader("Header3", "Value3"); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 7568 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame^] | 7569 | req->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 7570 | modified_headers); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7571 | d.RunUntilComplete(); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 7572 | |
| 7573 | EXPECT_EQ(1, d.response_started_count()); |
| 7574 | EXPECT_FALSE(d.received_data_before_response()); |
| 7575 | EXPECT_EQ(OK, d.request_status()); |
| 7576 | |
| 7577 | // Redirected request should also have modified headers. |
| 7578 | EXPECT_TRUE(d.have_full_request_headers()); |
| 7579 | const HttpRequestHeaders& sent_headers2 = d.full_request_headers(); |
| 7580 | EXPECT_TRUE(sent_headers2.GetHeader("Header1", &sent_value)); |
| 7581 | EXPECT_EQ("Value1", sent_value); |
| 7582 | EXPECT_TRUE(sent_headers2.GetHeader("Header2", &sent_value)); |
| 7583 | EXPECT_EQ("", sent_value); |
| 7584 | EXPECT_TRUE(sent_headers2.GetHeader("Header3", &sent_value)); |
| 7585 | EXPECT_EQ("Value3", sent_value); |
| 7586 | } |
| 7587 | } |
| 7588 | |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame^] | 7589 | TEST_F(URLRequestTestHTTP, DeferredRedirect_RemovedHeaders) { |
| 7590 | ASSERT_TRUE(http_test_server()->Start()); |
| 7591 | |
| 7592 | TestDelegate d; |
| 7593 | { |
| 7594 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
| 7595 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
| 7596 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7597 | |
| 7598 | // Set initial headers for the request. |
| 7599 | req->SetExtraRequestHeaderByName("Header1", "Value1", true /* overwrite */); |
| 7600 | req->SetExtraRequestHeaderByName("Header2", "Value2", true /* overwrite */); |
| 7601 | |
| 7602 | req->Start(); |
| 7603 | d.RunUntilRedirect(); |
| 7604 | |
| 7605 | // Initial request should have initial headers. |
| 7606 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7607 | EXPECT_TRUE(d.have_full_request_headers()); |
| 7608 | const HttpRequestHeaders& sent_headers1 = d.full_request_headers(); |
| 7609 | std::string sent_value; |
| 7610 | EXPECT_TRUE(sent_headers1.GetHeader("Header1", &sent_value)); |
| 7611 | EXPECT_EQ("Value1", sent_value); |
| 7612 | EXPECT_TRUE(sent_headers1.GetHeader("Header2", &sent_value)); |
| 7613 | EXPECT_EQ("Value2", sent_value); |
| 7614 | d.ClearFullRequestHeaders(); |
| 7615 | |
| 7616 | // Keep Header1 and remove Header2. |
| 7617 | std::vector<std::string> removed_headers({"Header2"}); |
| 7618 | req->FollowDeferredRedirect(removed_headers, |
| 7619 | base::nullopt /* modified_headers */); |
| 7620 | d.RunUntilComplete(); |
| 7621 | |
| 7622 | EXPECT_EQ(1, d.response_started_count()); |
| 7623 | EXPECT_FALSE(d.received_data_before_response()); |
| 7624 | EXPECT_EQ(OK, d.request_status()); |
| 7625 | |
| 7626 | // Redirected request should also have |
| 7627 | EXPECT_TRUE(d.have_full_request_headers()); |
| 7628 | const HttpRequestHeaders& sent_headers2 = d.full_request_headers(); |
| 7629 | EXPECT_TRUE(sent_headers2.GetHeader("Header1", &sent_value)); |
| 7630 | EXPECT_EQ("Value1", sent_value); |
| 7631 | EXPECT_FALSE(sent_headers2.GetHeader("Header2", &sent_value)); |
| 7632 | } |
| 7633 | } |
| 7634 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7635 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7636 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7637 | |
| 7638 | TestDelegate d; |
| 7639 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7640 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7641 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 7642 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7643 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 7644 | d.RunUntilRedirect(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7645 | |
| 7646 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7647 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7648 | req->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7649 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7650 | |
| 7651 | EXPECT_EQ(1, d.response_started_count()); |
| 7652 | EXPECT_EQ(0, d.bytes_received()); |
| 7653 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7654 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7655 | } |
| 7656 | } |
| 7657 | |
| 7658 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7659 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7660 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7661 | // Populate the cache. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7662 | { |
| 7663 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7664 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7665 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7666 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7667 | HttpRequestHeaders headers; |
| 7668 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7669 | req->SetExtraRequestHeaders(headers); |
| 7670 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7671 | d.RunUntilComplete(); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7672 | |
| 7673 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7674 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7675 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7676 | } |
| 7677 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7678 | // Expect a cache hit. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7679 | { |
| 7680 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7681 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7682 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7683 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7684 | HttpRequestHeaders headers; |
| 7685 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7686 | req->SetExtraRequestHeaders(headers); |
| 7687 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7688 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7689 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7690 | EXPECT_TRUE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7691 | |
| 7692 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7693 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7694 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7695 | } |
| 7696 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7697 | // Expect a cache miss. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7698 | { |
| 7699 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7700 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7701 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7702 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7703 | HttpRequestHeaders headers; |
| 7704 | headers.SetHeader("foo", "2"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7705 | req->SetExtraRequestHeaders(headers); |
| 7706 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7707 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7708 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7709 | EXPECT_FALSE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7710 | |
| 7711 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7712 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7713 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7714 | } |
| 7715 | } |
| 7716 | |
| 7717 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7718 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7719 | |
| 7720 | // populate the cache |
| 7721 | { |
| 7722 | TestDelegate d; |
| 7723 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7724 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7725 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7726 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 7727 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7728 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7729 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7730 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7731 | |
| 7732 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7733 | } |
| 7734 | |
| 7735 | // repeat request with end-to-end validation. since auth-basic results in a |
| 7736 | // cachable page, we expect this test to result in a 304. in which case, the |
| 7737 | // response should be fetched from the cache. |
| 7738 | { |
| 7739 | TestDelegate d; |
| 7740 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7741 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7742 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7743 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 7744 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7745 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 7746 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7747 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7748 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7749 | |
| 7750 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7751 | |
| 7752 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7753 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7754 | } |
| 7755 | } |
| 7756 | |
| 7757 | // Check that Set-Cookie headers in 401 responses are respected. |
| 7758 | // http://crbug.com/6450 |
| 7759 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7760 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7761 | |
| 7762 | GURL url_requiring_auth = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7763 | http_test_server()->GetURL("/auth-basic?set-cookie-if-challenged"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7764 | |
| 7765 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 7766 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 7767 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7768 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7769 | TestURLRequestContext context(true); |
| 7770 | context.set_network_delegate(&network_delegate); |
| 7771 | context.Init(); |
| 7772 | |
| 7773 | TestDelegate d; |
| 7774 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7775 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7776 | std::unique_ptr<URLRequest> r( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7777 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &d, |
| 7778 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7779 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7780 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7781 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7782 | |
| 7783 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7784 | |
| 7785 | // Make sure we sent the cookie in the restarted transaction. |
| 7786 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 7787 | != std::string::npos); |
| 7788 | } |
| 7789 | |
| 7790 | // Same test as above, except this time the restart is initiated earlier |
| 7791 | // (without user intervention since identity is embedded in the URL). |
| 7792 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7793 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7794 | TestURLRequestContext context(true); |
| 7795 | context.set_network_delegate(&network_delegate); |
| 7796 | context.Init(); |
| 7797 | |
| 7798 | TestDelegate d; |
| 7799 | |
| 7800 | GURL::Replacements replacements; |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 7801 | replacements.SetUsernameStr("user2"); |
| 7802 | replacements.SetPasswordStr("secret"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7803 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 7804 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7805 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 7806 | url_with_identity, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7807 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7808 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7809 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7810 | |
| 7811 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 7812 | |
| 7813 | // Make sure we sent the cookie in the restarted transaction. |
| 7814 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 7815 | != std::string::npos); |
| 7816 | } |
| 7817 | } |
| 7818 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7819 | // Tests that load timing works as expected with auth and the cache. |
| 7820 | TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7821 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7822 | |
| 7823 | // populate the cache |
| 7824 | { |
| 7825 | TestDelegate d; |
| 7826 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7827 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7828 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7829 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 7830 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7831 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7832 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7833 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7834 | |
| 7835 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7836 | |
| 7837 | LoadTimingInfo load_timing_info_before_auth; |
| 7838 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth( |
| 7839 | &load_timing_info_before_auth)); |
| 7840 | TestLoadTimingNotReused(load_timing_info_before_auth, |
| 7841 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 7842 | |
| 7843 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7844 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7845 | // The test server does not support keep alive sockets, so the second |
| 7846 | // request with auth should use a new socket. |
| 7847 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 7848 | EXPECT_NE(load_timing_info_before_auth.socket_log_id, |
| 7849 | load_timing_info.socket_log_id); |
| 7850 | EXPECT_LE(load_timing_info_before_auth.receive_headers_end, |
| 7851 | load_timing_info.connect_timing.connect_start); |
| 7852 | } |
| 7853 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7854 | // Repeat request with end-to-end validation. Since auth-basic results in a |
| 7855 | // 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] | 7856 | // response should be fetched from the cache. |
| 7857 | { |
| 7858 | TestDelegate d; |
| 7859 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 7860 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7861 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7862 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 7863 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7864 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 7865 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7866 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7867 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7868 | |
| 7869 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 7870 | |
| 7871 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7872 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7873 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7874 | // Since there was a request that went over the wire, the load timing |
| 7875 | // information should include connection times. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7876 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7877 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7878 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7879 | } |
| 7880 | } |
| 7881 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7882 | // In this test, we do a POST which the server will 302 redirect. |
| 7883 | // The subsequent transaction should use GET, and should not send the |
| 7884 | // Content-Type header. |
| 7885 | // http://code.google.com/p/chromium/issues/detail?id=843 |
| 7886 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7887 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7888 | |
| 7889 | const char kData[] = "hello world"; |
| 7890 | |
| 7891 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 7892 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7893 | http_test_server()->GetURL("/redirect-to-echoall"), DEFAULT_PRIORITY, &d, |
| 7894 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7895 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 7896 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7897 | |
| 7898 | // Set headers (some of which are specific to the POST). |
| 7899 | HttpRequestHeaders headers; |
Yeol | 45b9e0a | 2018-07-27 02:03:29 | [diff] [blame] | 7900 | headers.SetHeader("Content-Type", |
| 7901 | "multipart/form-data;" |
| 7902 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ"); |
| 7903 | headers.SetHeader("Accept", |
| 7904 | "text/xml,application/xml,application/xhtml+xml," |
| 7905 | "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); |
| 7906 | headers.SetHeader("Accept-Language", "en-US,en"); |
| 7907 | headers.SetHeader("Accept-Charset", "ISO-8859-1,*,utf-8"); |
| 7908 | headers.SetHeader("Content-Length", "11"); |
| 7909 | headers.SetHeader("Origin", "http://localhost:1337/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7910 | req->SetExtraRequestHeaders(headers); |
| 7911 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7912 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7913 | |
| 7914 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7915 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7916 | EXPECT_EQ("text/html", mime_type); |
| 7917 | |
| 7918 | const std::string& data = d.data_received(); |
| 7919 | |
| 7920 | // Check that the post-specific headers were stripped: |
| 7921 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 7922 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 7923 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7924 | |
| 7925 | // These extra request headers should not have been stripped. |
| 7926 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 7927 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 7928 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 7929 | } |
| 7930 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7931 | // The following tests check that we handle mutating the request for HTTP |
| 7932 | // redirects as expected. |
| 7933 | // See https://crbug.com/56373, https://crbug.com/102130, and |
| 7934 | // https://crbug.com/465517. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7935 | |
| 7936 | TEST_F(URLRequestTestHTTP, Redirect301Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7937 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7938 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7939 | const GURL url = http_test_server()->GetURL("/redirect301-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7940 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7941 | http_test_server()->GetURL("/redirect301-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7942 | |
| 7943 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 7944 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 7945 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7946 | |
| 7947 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 7948 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 7949 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 7950 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 7951 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 7952 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 7953 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7954 | } |
| 7955 | |
| 7956 | TEST_F(URLRequestTestHTTP, Redirect302Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7957 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7958 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7959 | const GURL url = http_test_server()->GetURL("/redirect302-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7960 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7961 | http_test_server()->GetURL("/redirect302-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7962 | |
| 7963 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 7964 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 7965 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7966 | |
| 7967 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 7968 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 7969 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 7970 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 7971 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 7972 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 7973 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7974 | } |
| 7975 | |
| 7976 | TEST_F(URLRequestTestHTTP, Redirect303Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7977 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7978 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7979 | const GURL url = http_test_server()->GetURL("/redirect303-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7980 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7981 | http_test_server()->GetURL("/redirect303-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7982 | |
| 7983 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 7984 | HTTPRedirectMethodTest(url, "PUT", "GET", true); |
| 7985 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7986 | |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 7987 | HTTPRedirectOriginHeaderTest(url, "CONNECT", "GET", std::string()); |
| 7988 | HTTPRedirectOriginHeaderTest(https_redirect_url, "CONNECT", "GET", |
| 7989 | std::string()); |
| 7990 | HTTPRedirectOriginHeaderTest(url, "DELETE", "GET", std::string()); |
| 7991 | HTTPRedirectOriginHeaderTest(https_redirect_url, "DELETE", "GET", |
| 7992 | std::string()); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7993 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 7994 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 7995 | HTTPRedirectOriginHeaderTest(url, "HEAD", "HEAD", url.GetOrigin().spec()); |
| 7996 | HTTPRedirectOriginHeaderTest(https_redirect_url, "HEAD", "HEAD", "null"); |
| 7997 | HTTPRedirectOriginHeaderTest(url, "OPTIONS", "GET", std::string()); |
| 7998 | HTTPRedirectOriginHeaderTest(https_redirect_url, "OPTIONS", "GET", |
| 7999 | std::string()); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8000 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8001 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8002 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8003 | HTTPRedirectOriginHeaderTest(url, "PUT", "GET", std::string()); |
| 8004 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "GET", std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8005 | } |
| 8006 | |
| 8007 | TEST_F(URLRequestTestHTTP, Redirect307Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8008 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8009 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8010 | const GURL url = http_test_server()->GetURL("/redirect307-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8011 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8012 | http_test_server()->GetURL("/redirect307-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8013 | |
| 8014 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 8015 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8016 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8017 | |
| 8018 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8019 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 8020 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 8021 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8022 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8023 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8024 | } |
| 8025 | |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8026 | TEST_F(URLRequestTestHTTP, Redirect308Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8027 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8028 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8029 | const GURL url = http_test_server()->GetURL("/redirect308-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8030 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8031 | http_test_server()->GetURL("/redirect308-to-https"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8032 | |
| 8033 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 8034 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8035 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8036 | |
| 8037 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8038 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 8039 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 8040 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8041 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8042 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8043 | } |
| 8044 | |
| 8045 | // Make sure that 308 responses without bodies are not treated as redirects. |
| 8046 | // Certain legacy apis that pre-date the response code expect this behavior |
| 8047 | // (Like Google Drive). |
| 8048 | TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8049 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8050 | |
| 8051 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8052 | const GURL url = http_test_server()->GetURL("/308-without-location-header"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8053 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8054 | std::unique_ptr<URLRequest> request(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8055 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8056 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8057 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8058 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8059 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8060 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8061 | EXPECT_EQ(308, request->response_headers()->response_code()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8062 | EXPECT_EQ("This is not a redirect.", d.data_received()); |
| 8063 | } |
| 8064 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8065 | TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8066 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8067 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8068 | GURL original_url( |
| 8069 | http_test_server()->GetURL("/redirect302-to-echo#fragment")); |
| 8070 | GURL expected_url(http_test_server()->GetURL("/echo#fragment")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8071 | |
| 8072 | TestDelegate d; |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8073 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8074 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8075 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8076 | r->Start(); |
| 8077 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8078 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8079 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8080 | EXPECT_EQ(OK, d.request_status()); |
| 8081 | EXPECT_EQ(original_url, r->original_url()); |
| 8082 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8083 | } |
| 8084 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8085 | TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8086 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8087 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8088 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8089 | GURL first_party_url("http://example.com"); |
| 8090 | |
| 8091 | TestDelegate d; |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8092 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8093 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8094 | r->set_site_for_cookies(first_party_url); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8095 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8096 | r->Start(); |
| 8097 | d.RunUntilComplete(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8098 | |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8099 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8100 | EXPECT_EQ(OK, d.request_status()); |
| 8101 | EXPECT_EQ(first_party_url, r->site_for_cookies()); |
| 8102 | } |
| 8103 | |
| 8104 | TEST_F(URLRequestTestHTTP, RedirectPreserveTopFrameOrigin) { |
| 8105 | ASSERT_TRUE(http_test_server()->Start()); |
| 8106 | |
| 8107 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 8108 | url::Origin top_frame_origin = |
| 8109 | url::Origin::Create(GURL("http://example.com")); |
| 8110 | TestDelegate d; |
| 8111 | |
| 8112 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8113 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8114 | r->set_top_frame_origin(top_frame_origin); |
| 8115 | |
| 8116 | r->Start(); |
| 8117 | d.RunUntilComplete(); |
| 8118 | |
| 8119 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8120 | EXPECT_EQ(OK, d.request_status()); |
| 8121 | EXPECT_EQ(top_frame_origin, *r->top_frame_origin()); |
| 8122 | } |
| 8123 | |
| 8124 | TEST_F(URLRequestTestHTTP, RedirectPreserveUnsetTopFrameOrigin) { |
| 8125 | ASSERT_TRUE(http_test_server()->Start()); |
| 8126 | |
| 8127 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 8128 | TestDelegate d; |
| 8129 | |
| 8130 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8131 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8132 | // Since we're not setting a top frame origin, we wouldn't expect one after |
| 8133 | // the redirect. |
| 8134 | r->Start(); |
| 8135 | d.RunUntilComplete(); |
| 8136 | |
| 8137 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8138 | EXPECT_EQ(OK, d.request_status()); |
| 8139 | EXPECT_FALSE(r->top_frame_origin()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8140 | } |
| 8141 | |
Josh Karlin | f3caee164 | 2018-12-10 22:54:27 | [diff] [blame] | 8142 | TEST_F(URLRequestTestHTTP, RedirectPreserveTopFrameURL) { |
| 8143 | ASSERT_TRUE(http_test_server()->Start()); |
| 8144 | |
| 8145 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 8146 | url::Origin top_frame_origin = |
| 8147 | url::Origin::Create(GURL("http://example.com")); |
| 8148 | TestDelegate d; |
| 8149 | { |
| 8150 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8151 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8152 | r->set_top_frame_origin(top_frame_origin); |
| 8153 | |
| 8154 | r->Start(); |
| 8155 | d.RunUntilComplete(); |
| 8156 | |
| 8157 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8158 | EXPECT_EQ(OK, d.request_status()); |
| 8159 | EXPECT_EQ(top_frame_origin, *r->top_frame_origin()); |
| 8160 | } |
| 8161 | } |
| 8162 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8163 | TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8164 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8165 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8166 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8167 | GURL original_first_party_url("http://example.com"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8168 | GURL expected_first_party_url(http_test_server()->GetURL("/echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8169 | |
| 8170 | TestDelegate d; |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8171 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8172 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8173 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8174 | r->set_site_for_cookies(original_first_party_url); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8175 | r->set_first_party_url_policy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8176 | URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 8177 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8178 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8179 | d.RunUntilComplete(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8180 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8181 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8182 | EXPECT_EQ(OK, d.request_status()); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8183 | EXPECT_EQ(expected_first_party_url, r->site_for_cookies()); |
Josh Karlin | be37f91 | 2018-12-07 01:05:14 | [diff] [blame] | 8184 | } |
| 8185 | |
| 8186 | TEST_F(URLRequestTestHTTP, RedirectUpdateTopFrameOrigin) { |
| 8187 | ASSERT_TRUE(http_test_server()->Start()); |
| 8188 | |
| 8189 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 8190 | url::Origin original_top_frame_origin = |
| 8191 | url::Origin::Create(GURL("http://example.com")); |
| 8192 | url::Origin expected_top_frame_origin = |
| 8193 | url::Origin::Create(GURL(http_test_server()->GetURL("/echo"))); |
| 8194 | |
| 8195 | TestDelegate d; |
| 8196 | |
| 8197 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8198 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8199 | r->set_top_frame_origin(original_top_frame_origin); |
| 8200 | r->set_first_party_url_policy(URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 8201 | |
| 8202 | r->Start(); |
| 8203 | d.RunUntilComplete(); |
| 8204 | |
| 8205 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8206 | EXPECT_EQ(OK, d.request_status()); |
| 8207 | EXPECT_EQ(expected_top_frame_origin, *r->top_frame_origin()); |
| 8208 | } |
| 8209 | |
| 8210 | TEST_F(URLRequestTestHTTP, RedirectIgnoreUnsetTopFrameOrigin) { |
| 8211 | ASSERT_TRUE(http_test_server()->Start()); |
| 8212 | |
| 8213 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
| 8214 | TestDelegate d; |
| 8215 | |
| 8216 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 8217 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8218 | // Since we're not setting a top frame origin, we wouldn't expect one after |
| 8219 | // the redirect. |
| 8220 | r->set_first_party_url_policy(URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 8221 | |
| 8222 | r->Start(); |
| 8223 | d.RunUntilComplete(); |
| 8224 | |
| 8225 | EXPECT_EQ(2U, r->url_chain().size()); |
| 8226 | EXPECT_EQ(OK, d.request_status()); |
| 8227 | EXPECT_FALSE(r->top_frame_origin()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8228 | } |
| 8229 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8230 | TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8231 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8232 | |
| 8233 | const char kData[] = "hello world"; |
| 8234 | |
| 8235 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8236 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8237 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 8238 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8239 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8240 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8241 | HttpRequestHeaders headers; |
| 8242 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8243 | base::NumberToString(base::size(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8244 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8245 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8246 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8247 | req.get(), &default_network_delegate_, |
| 8248 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8249 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 8250 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8251 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8252 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8253 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8254 | EXPECT_EQ("GET", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8255 | } |
| 8256 | |
| 8257 | TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8258 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8259 | |
| 8260 | const char kData[] = "hello world"; |
| 8261 | |
| 8262 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8263 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8264 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 8265 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8266 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8267 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8268 | HttpRequestHeaders headers; |
| 8269 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8270 | base::NumberToString(base::size(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8271 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8272 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8273 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8274 | req.get(), &default_network_delegate_, |
| 8275 | http_test_server()->GetURL("/echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 8276 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8277 | "Very Good Reason")); |
| 8278 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8279 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8280 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8281 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8282 | EXPECT_EQ("POST", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8283 | EXPECT_EQ(kData, d.data_received()); |
| 8284 | } |
| 8285 | |
| 8286 | // Check that default A-L header is sent. |
| 8287 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8288 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8289 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 8290 | StaticHttpUserAgentSettings settings("en", std::string()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8291 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8292 | TestURLRequestContext context(true); |
| 8293 | context.set_network_delegate(&network_delegate); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8294 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8295 | context.Init(); |
| 8296 | |
| 8297 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8298 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8299 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8300 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8301 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8302 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8303 | EXPECT_EQ("en", d.data_received()); |
| 8304 | } |
| 8305 | |
| 8306 | // Check that an empty A-L header is not sent. http://crbug.com/77365. |
| 8307 | TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8308 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8309 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 8310 | std::string empty_string; // Avoid most vexing parse on line below. |
| 8311 | StaticHttpUserAgentSettings settings(empty_string, empty_string); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8312 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8313 | TestURLRequestContext context(true); |
| 8314 | context.set_network_delegate(&network_delegate); |
| 8315 | context.Init(); |
| 8316 | // We override the language after initialization because empty entries |
| 8317 | // get overridden by Init(). |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8318 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8319 | |
| 8320 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8321 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8322 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8323 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8324 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8325 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8326 | EXPECT_EQ("None", d.data_received()); |
| 8327 | } |
| 8328 | |
| 8329 | // Check that if request overrides the A-L header, the default is not appended. |
| 8330 | // See http://crbug.com/20894 |
| 8331 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8332 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8333 | |
| 8334 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8335 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8336 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8337 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8338 | HttpRequestHeaders headers; |
| 8339 | headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8340 | req->SetExtraRequestHeaders(headers); |
| 8341 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8342 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8343 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 8344 | } |
| 8345 | |
| 8346 | // Check that default A-E header is sent. |
| 8347 | TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8348 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8349 | |
| 8350 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8351 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8352 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8353 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8354 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8355 | req->SetExtraRequestHeaders(headers); |
| 8356 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8357 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8358 | EXPECT_TRUE(ContainsString(d.data_received(), "gzip")); |
| 8359 | } |
| 8360 | |
| 8361 | // Check that if request overrides the A-E header, the default is not appended. |
| 8362 | // See http://crbug.com/47381 |
| 8363 | TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8364 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8365 | |
| 8366 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8367 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8368 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8369 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8370 | HttpRequestHeaders headers; |
| 8371 | headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8372 | req->SetExtraRequestHeaders(headers); |
| 8373 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8374 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8375 | EXPECT_FALSE(ContainsString(d.data_received(), "gzip")); |
| 8376 | EXPECT_TRUE(ContainsString(d.data_received(), "identity")); |
| 8377 | } |
| 8378 | |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 8379 | // Check that setting the A-C header sends the proper header. |
| 8380 | TEST_F(URLRequestTestHTTP, SetAcceptCharset) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8381 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8382 | |
| 8383 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8384 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8385 | http_test_server()->GetURL("/echoheader?Accept-Charset"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8386 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8387 | HttpRequestHeaders headers; |
| 8388 | headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8389 | req->SetExtraRequestHeaders(headers); |
| 8390 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8391 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8392 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 8393 | } |
| 8394 | |
| 8395 | // Check that default User-Agent header is sent. |
| 8396 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8397 | ASSERT_TRUE(http_test_server()->Start()); |
[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("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8402 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8403 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8404 | d.RunUntilComplete(); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8405 | EXPECT_EQ(default_context().http_user_agent_settings()->GetUserAgent(), |
[email protected] | 051a4b3 | 2014-01-09 04:02:37 | [diff] [blame] | 8406 | d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8407 | } |
| 8408 | |
| 8409 | // Check that if request overrides the User-Agent header, |
| 8410 | // the default is not appended. |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 8411 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 8412 | #if defined(OS_IOS) |
| 8413 | #define MAYBE_OverrideUserAgent FLAKY_OverrideUserAgent |
| 8414 | #else |
| 8415 | #define MAYBE_OverrideUserAgent OverrideUserAgent |
| 8416 | #endif |
| 8417 | TEST_F(URLRequestTestHTTP, MAYBE_OverrideUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8418 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8419 | |
| 8420 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8421 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8422 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8423 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8424 | HttpRequestHeaders headers; |
| 8425 | headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8426 | req->SetExtraRequestHeaders(headers); |
| 8427 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8428 | d.RunUntilComplete(); |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 8429 | EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8430 | } |
| 8431 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8432 | // Check that a NULL HttpUserAgentSettings causes the corresponding empty |
| 8433 | // User-Agent header to be sent but does not send the Accept-Language and |
| 8434 | // Accept-Charset headers. |
| 8435 | TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8436 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8437 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8438 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8439 | TestURLRequestContext context(true); |
| 8440 | context.set_network_delegate(&network_delegate); |
| 8441 | context.Init(); |
| 8442 | // We override the HttpUserAgentSettings after initialization because empty |
| 8443 | // entries get overridden by Init(). |
| 8444 | context.set_http_user_agent_settings(NULL); |
| 8445 | |
| 8446 | struct { |
| 8447 | const char* request; |
| 8448 | const char* expected_response; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8449 | } tests[] = {{"/echoheader?Accept-Language", "None"}, |
| 8450 | {"/echoheader?Accept-Charset", "None"}, |
| 8451 | {"/echoheader?User-Agent", ""}}; |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8452 | |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 8453 | for (size_t i = 0; i < base::size(tests); i++) { |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8454 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8455 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8456 | http_test_server()->GetURL(tests[i].request), DEFAULT_PRIORITY, &d, |
| 8457 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8458 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8459 | d.RunUntilComplete(); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8460 | EXPECT_EQ(tests[i].expected_response, d.data_received()) |
| 8461 | << " Request = \"" << tests[i].request << "\""; |
| 8462 | } |
| 8463 | } |
| 8464 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8465 | // Make sure that URLRequest passes on its priority updates to |
| 8466 | // newly-created jobs after the first one. |
| 8467 | TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8468 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8469 | |
| 8470 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8471 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8472 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 8473 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8474 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8475 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8476 | std::unique_ptr<URLRequestRedirectJob> redirect_job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8477 | req.get(), &default_network_delegate_, |
| 8478 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8479 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 8480 | AddTestInterceptor()->set_main_intercept_job(std::move(redirect_job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8481 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8482 | req->SetPriority(LOW); |
| 8483 | req->Start(); |
| 8484 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8485 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8486 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8487 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8488 | req.get(), &default_network_delegate_, &job_priority)); |
| 8489 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8490 | |
| 8491 | // Should trigger |job| to be started. |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8492 | d.RunUntilComplete(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8493 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 8494 | } |
| 8495 | |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8496 | // Check that creating a network request while entering/exiting suspend mode |
| 8497 | // fails as it should. This is the only case where an HttpTransactionFactory |
| 8498 | // does not return an HttpTransaction. |
| 8499 | TEST_F(URLRequestTestHTTP, NetworkSuspendTest) { |
| 8500 | // Create a new HttpNetworkLayer that thinks it's suspended. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8501 | std::unique_ptr<HttpNetworkLayer> network_layer(new HttpNetworkLayer( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8502 | default_context().http_transaction_factory()->GetSession())); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8503 | network_layer->OnSuspend(); |
| 8504 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 8505 | HttpCache http_cache(std::move(network_layer), |
zhongyi | 3ceab9f | 2017-02-04 02:06:03 | [diff] [blame] | 8506 | HttpCache::DefaultBackend::InMemory(0), |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 8507 | false /* is_main_cache */); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8508 | |
| 8509 | TestURLRequestContext context(true); |
| 8510 | context.set_http_transaction_factory(&http_cache); |
| 8511 | context.Init(); |
| 8512 | |
| 8513 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8514 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8515 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 8516 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8517 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8518 | d.RunUntilComplete(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8519 | |
| 8520 | EXPECT_TRUE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8521 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, d.request_status()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8522 | } |
| 8523 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8524 | namespace { |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8525 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8526 | // HttpTransactionFactory that synchronously fails to create transactions. |
| 8527 | class FailingHttpTransactionFactory : public HttpTransactionFactory { |
| 8528 | public: |
| 8529 | explicit FailingHttpTransactionFactory(HttpNetworkSession* network_session) |
| 8530 | : network_session_(network_session) {} |
| 8531 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 8532 | ~FailingHttpTransactionFactory() override = default; |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8533 | |
| 8534 | // HttpTransactionFactory methods: |
| 8535 | int CreateTransaction(RequestPriority priority, |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8536 | std::unique_ptr<HttpTransaction>* trans) override { |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8537 | return ERR_FAILED; |
| 8538 | } |
| 8539 | |
| 8540 | HttpCache* GetCache() override { return nullptr; } |
| 8541 | |
| 8542 | HttpNetworkSession* GetSession() override { return network_session_; } |
| 8543 | |
| 8544 | private: |
| 8545 | HttpNetworkSession* network_session_; |
| 8546 | |
| 8547 | DISALLOW_COPY_AND_ASSIGN(FailingHttpTransactionFactory); |
| 8548 | }; |
| 8549 | |
| 8550 | } // namespace |
| 8551 | |
| 8552 | // Check that when a request that fails to create an HttpTransaction can be |
| 8553 | // cancelled while the failure notification is pending, and doesn't send two |
| 8554 | // failure notifications. |
| 8555 | // |
| 8556 | // This currently only happens when in suspend mode and there's no cache, but |
| 8557 | // just use a special HttpTransactionFactory, to avoid depending on those |
| 8558 | // behaviors. |
| 8559 | TEST_F(URLRequestTestHTTP, NetworkCancelAfterCreateTransactionFailsTest) { |
| 8560 | FailingHttpTransactionFactory http_transaction_factory( |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8561 | default_context().http_transaction_factory()->GetSession()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8562 | TestURLRequestContext context(true); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8563 | context.set_http_transaction_factory(&http_transaction_factory); |
| 8564 | context.set_network_delegate(default_network_delegate()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8565 | context.Init(); |
| 8566 | |
| 8567 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8568 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8569 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 8570 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8571 | // Don't send cookies (Collecting cookies is asynchronous, and need request to |
| 8572 | // try to create an HttpNetworkTransaction synchronously on start). |
| 8573 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8574 | req->Start(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8575 | req->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8576 | d.RunUntilComplete(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8577 | // Run pending error task, if there is one. |
| 8578 | base::RunLoop().RunUntilIdle(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8579 | |
| 8580 | EXPECT_TRUE(d.request_failed()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8581 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8582 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 8583 | |
| 8584 | // NetworkDelegate should see the cancellation, but not the error. |
| 8585 | EXPECT_EQ(1, default_network_delegate()->canceled_requests()); |
| 8586 | EXPECT_EQ(0, default_network_delegate()->error_count()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 8587 | } |
| 8588 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8589 | TEST_F(URLRequestTestHTTP, NetworkAccessedSetOnNetworkRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8590 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8591 | |
| 8592 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8593 | GURL test_url(http_test_server()->GetURL("/")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8594 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8595 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8596 | |
| 8597 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8598 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8599 | |
| 8600 | EXPECT_TRUE(req->response_info().network_accessed); |
| 8601 | } |
| 8602 | |
| 8603 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnCachedResponse) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8604 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8605 | |
| 8606 | // Populate the cache. |
| 8607 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8608 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8609 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 8610 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8611 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8612 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8613 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8614 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8615 | EXPECT_TRUE(req->response_info().network_accessed); |
| 8616 | EXPECT_FALSE(req->response_info().was_cached); |
| 8617 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8618 | req = default_context().CreateRequest( |
| 8619 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 8620 | TRAFFIC_ANNOTATION_FOR_TESTS); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8621 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8622 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8623 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8624 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8625 | EXPECT_FALSE(req->response_info().network_accessed); |
| 8626 | EXPECT_TRUE(req->response_info().was_cached); |
| 8627 | } |
| 8628 | |
| 8629 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnLoadOnlyFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8630 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8631 | |
| 8632 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8633 | GURL test_url(http_test_server()->GetURL("/")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8634 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8635 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jkarlin | a067deed | 2016-10-27 14:48:33 | [diff] [blame] | 8636 | req->SetLoadFlags(LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8637 | |
| 8638 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8639 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 8640 | |
| 8641 | EXPECT_FALSE(req->response_info().network_accessed); |
| 8642 | } |
| 8643 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 8644 | // Test that a single job with a THROTTLED priority completes |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 8645 | // correctly in the absence of contention. |
| 8646 | TEST_F(URLRequestTestHTTP, ThrottledPriority) { |
| 8647 | ASSERT_TRUE(http_test_server()->Start()); |
| 8648 | |
| 8649 | TestDelegate d; |
| 8650 | GURL test_url(http_test_server()->GetURL("/")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8651 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8652 | test_url, THROTTLED, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 8653 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8654 | d.RunUntilComplete(); |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 8655 | |
| 8656 | EXPECT_TRUE(req->status().is_success()); |
| 8657 | } |
| 8658 | |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 8659 | TEST_F(URLRequestTestHTTP, RawBodyBytesNoContentEncoding) { |
| 8660 | ASSERT_TRUE(http_test_server()->Start()); |
| 8661 | |
| 8662 | TestDelegate d; |
| 8663 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8664 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 8665 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 8666 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8667 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 8668 | |
| 8669 | EXPECT_EQ(5, req->GetRawBodyBytes()); |
| 8670 | } |
| 8671 | |
| 8672 | TEST_F(URLRequestTestHTTP, RawBodyBytesGzipEncoding) { |
| 8673 | ASSERT_TRUE(http_test_server()->Start()); |
| 8674 | |
| 8675 | TestDelegate d; |
| 8676 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8677 | http_test_server()->GetURL("/gzip-encoded"), DEFAULT_PRIORITY, &d, |
| 8678 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 8679 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8680 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 8681 | |
| 8682 | EXPECT_EQ(30, req->GetRawBodyBytes()); |
| 8683 | } |
| 8684 | |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 8685 | // Check that if NetworkDelegate::OnBeforeStartTransaction returns an error, |
| 8686 | // the delegate isn't called back synchronously. |
| 8687 | TEST_F(URLRequestTestHTTP, TesBeforeStartTransactionFails) { |
| 8688 | ASSERT_TRUE(http_test_server()->Start()); |
| 8689 | default_network_delegate_.set_before_start_transaction_fails(); |
| 8690 | |
| 8691 | TestDelegate d; |
| 8692 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8693 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 8694 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 8695 | req->Start(); |
| 8696 | DCHECK(!d.response_completed()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8697 | d.RunUntilComplete(); |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 8698 | DCHECK(d.response_completed()); |
| 8699 | EXPECT_EQ(ERR_FAILED, d.request_status()); |
| 8700 | } |
| 8701 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8702 | class URLRequestInterceptorTestHTTP : public URLRequestTestHTTP { |
| 8703 | public: |
| 8704 | // TODO(bengr): Merge this with the URLRequestInterceptorHTTPTest fixture, |
| 8705 | // ideally remove the dependency on URLRequestTestJob, and maybe move these |
| 8706 | // tests into the factory tests. |
| 8707 | URLRequestInterceptorTestHTTP() : URLRequestTestHTTP(), interceptor_(NULL) { |
| 8708 | } |
| 8709 | |
| 8710 | void SetUpFactory() override { |
| 8711 | interceptor_ = new MockURLRequestInterceptor(); |
| 8712 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8713 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8714 | } |
| 8715 | |
| 8716 | MockURLRequestInterceptor* interceptor() const { |
| 8717 | return interceptor_; |
| 8718 | } |
| 8719 | |
| 8720 | private: |
| 8721 | MockURLRequestInterceptor* interceptor_; |
| 8722 | }; |
| 8723 | |
| 8724 | TEST_F(URLRequestInterceptorTestHTTP, |
| 8725 | NetworkDelegateNotificationOnRedirectIntercept) { |
| 8726 | interceptor()->set_intercept_redirect(true); |
| 8727 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 8728 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 8729 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8730 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8731 | |
| 8732 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8733 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8734 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 8735 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8736 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8737 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8738 | |
| 8739 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 8740 | // Check we got one good response |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8741 | int status = d.request_status(); |
| 8742 | EXPECT_EQ(OK, status); |
| 8743 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8744 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 8745 | |
| 8746 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 8747 | EXPECT_EQ(1, d.response_started_count()); |
| 8748 | EXPECT_EQ(0, d.received_redirect_count()); |
| 8749 | |
| 8750 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 8751 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8752 | EXPECT_EQ(1, default_network_delegate()->headers_received_count()); |
| 8753 | } |
| 8754 | |
| 8755 | TEST_F(URLRequestInterceptorTestHTTP, |
| 8756 | NetworkDelegateNotificationOnErrorIntercept) { |
| 8757 | // Intercept that error and respond with an OK response. |
| 8758 | interceptor()->set_intercept_final_response(true); |
| 8759 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 8760 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 8761 | default_network_delegate()->set_can_be_intercepted_on_error(true); |
| 8762 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8763 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8764 | |
| 8765 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8766 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8767 | http_test_server()->GetURL("/two-content-lengths.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8768 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8769 | req->set_method("GET"); |
| 8770 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8771 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8772 | |
| 8773 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 8774 | |
| 8775 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8776 | int status = d.request_status(); |
| 8777 | EXPECT_EQ(OK, status); |
| 8778 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8779 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 8780 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 8781 | EXPECT_EQ(1, d.response_started_count()); |
| 8782 | EXPECT_EQ(0, d.received_redirect_count()); |
| 8783 | |
| 8784 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 8785 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8786 | EXPECT_EQ(0, default_network_delegate()->headers_received_count()); |
| 8787 | } |
| 8788 | |
| 8789 | TEST_F(URLRequestInterceptorTestHTTP, |
| 8790 | NetworkDelegateNotificationOnResponseIntercept) { |
| 8791 | // Intercept that error and respond with an OK response. |
| 8792 | interceptor()->set_intercept_final_response(true); |
| 8793 | |
| 8794 | // Intercept with a real URLRequestHttpJob. |
| 8795 | interceptor()->set_use_url_request_http_job(true); |
| 8796 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8797 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8798 | |
| 8799 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8800 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8801 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 8802 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8803 | req->set_method("GET"); |
| 8804 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8805 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8806 | |
| 8807 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 8808 | |
| 8809 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8810 | int status = d.request_status(); |
| 8811 | EXPECT_EQ(OK, status); |
| 8812 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8813 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 8814 | EXPECT_EQ("hello", d.data_received()); |
| 8815 | EXPECT_EQ(1, d.response_started_count()); |
| 8816 | EXPECT_EQ(0, d.received_redirect_count()); |
| 8817 | |
| 8818 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 8819 | EXPECT_EQ(2, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 8820 | EXPECT_EQ(2, default_network_delegate()->headers_received_count()); |
| 8821 | } |
| 8822 | |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8823 | class URLRequestTestReferrerPolicy : public URLRequestTest { |
| 8824 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 8825 | URLRequestTestReferrerPolicy() = default; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8826 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8827 | void InstantiateSameOriginServers(net::EmbeddedTestServer::Type type) { |
| 8828 | origin_server_.reset(new EmbeddedTestServer(type)); |
| 8829 | if (type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 8830 | origin_server_->AddDefaultHandlers( |
| 8831 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 8832 | } else { |
| 8833 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 8834 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8835 | ASSERT_TRUE(origin_server_->Start()); |
| 8836 | } |
| 8837 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8838 | void InstantiateCrossOriginServers(net::EmbeddedTestServer::Type origin_type, |
| 8839 | net::EmbeddedTestServer::Type dest_type) { |
| 8840 | origin_server_.reset(new EmbeddedTestServer(origin_type)); |
| 8841 | if (origin_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 8842 | origin_server_->AddDefaultHandlers( |
| 8843 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 8844 | } else { |
| 8845 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 8846 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8847 | ASSERT_TRUE(origin_server_->Start()); |
| 8848 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8849 | destination_server_.reset(new EmbeddedTestServer(dest_type)); |
| 8850 | if (dest_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 8851 | destination_server_->AddDefaultHandlers( |
| 8852 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 8853 | } else { |
| 8854 | destination_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 8855 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8856 | ASSERT_TRUE(destination_server_->Start()); |
| 8857 | } |
| 8858 | |
| 8859 | void VerifyReferrerAfterRedirect(URLRequest::ReferrerPolicy policy, |
| 8860 | const GURL& referrer, |
| 8861 | const GURL& expected) { |
| 8862 | // Create and execute the request: we'll only have a |destination_server_| |
| 8863 | // if the origins are meant to be distinct. Otherwise, we'll use the |
| 8864 | // |origin_server_| for both endpoints. |
| 8865 | GURL destination_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8866 | destination_server_ ? destination_server_->GetURL("/echoheader?Referer") |
| 8867 | : origin_server_->GetURL("/echoheader?Referer"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8868 | GURL origin_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8869 | origin_server_->GetURL("/server-redirect?" + destination_url.spec()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8870 | |
| 8871 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 8872 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8873 | origin_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8874 | req->set_referrer_policy(policy); |
| 8875 | req->SetReferrer(referrer.spec()); |
| 8876 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8877 | d.RunUntilComplete(); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8878 | |
| 8879 | EXPECT_EQ(1, d.response_started_count()); |
| 8880 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8881 | EXPECT_EQ(destination_url, req->url()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8882 | EXPECT_EQ(OK, d.request_status()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8883 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 8884 | |
| 8885 | EXPECT_EQ(expected.spec(), req->referrer()); |
| 8886 | if (expected.is_empty()) |
| 8887 | EXPECT_EQ("None", d.data_received()); |
| 8888 | else |
| 8889 | EXPECT_EQ(expected.spec(), d.data_received()); |
| 8890 | } |
| 8891 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8892 | EmbeddedTestServer* origin_server() const { return origin_server_.get(); } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8893 | |
| 8894 | private: |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8895 | std::unique_ptr<EmbeddedTestServer> origin_server_; |
| 8896 | std::unique_ptr<EmbeddedTestServer> destination_server_; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8897 | }; |
| 8898 | |
| 8899 | TEST_F(URLRequestTestReferrerPolicy, HTTPToSameOriginHTTP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8900 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTP); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8901 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 8902 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8903 | VerifyReferrerAfterRedirect( |
| 8904 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 8905 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8906 | |
| 8907 | VerifyReferrerAfterRedirect( |
| 8908 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 8909 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8910 | |
| 8911 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 8912 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8913 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 8914 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 8915 | referrer); |
| 8916 | |
| 8917 | // The original referrer set on the request is expected to obey the referrer |
| 8918 | // policy and already be stripped to the origin; thus this test case just |
| 8919 | // checks that this policy doesn't cause the referrer to change when following |
| 8920 | // a redirect. |
| 8921 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 8922 | referrer.GetOrigin()); |
| 8923 | |
| 8924 | VerifyReferrerAfterRedirect( |
| 8925 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 8926 | referrer); |
| 8927 | |
| 8928 | // The original referrer set on the request is expected to obey the referrer |
| 8929 | // policy and already be stripped to the origin; thus this test case just |
| 8930 | // checks that this policy doesn't cause the referrer to change when following |
| 8931 | // a redirect. |
| 8932 | VerifyReferrerAfterRedirect( |
| 8933 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 8934 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 8935 | |
| 8936 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8937 | } |
| 8938 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8939 | TEST_F(URLRequestTestReferrerPolicy, HTTPToCrossOriginHTTP) { |
| 8940 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 8941 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 8942 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8943 | |
| 8944 | VerifyReferrerAfterRedirect( |
| 8945 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 8946 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8947 | |
| 8948 | VerifyReferrerAfterRedirect( |
| 8949 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 8950 | referrer, referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8951 | |
| 8952 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 8953 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 8954 | referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8955 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 8956 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 8957 | referrer); |
| 8958 | |
| 8959 | // The original referrer set on the request is expected to obey the referrer |
| 8960 | // policy and already be stripped to the origin; thus this test case just |
| 8961 | // checks that this policy doesn't cause the referrer to change when following |
| 8962 | // a redirect. |
| 8963 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 8964 | referrer.GetOrigin()); |
| 8965 | |
| 8966 | VerifyReferrerAfterRedirect( |
| 8967 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 8968 | |
| 8969 | // The original referrer set on the request is expected to obey the referrer |
| 8970 | // policy and already be stripped to the origin; thus this test case just |
| 8971 | // checks that this policy doesn't cause the referrer to change when following |
| 8972 | // a redirect. |
| 8973 | VerifyReferrerAfterRedirect( |
| 8974 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 8975 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 8976 | |
| 8977 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8978 | } |
| 8979 | |
| 8980 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToSameOriginHTTPS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8981 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 8982 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8983 | |
| 8984 | VerifyReferrerAfterRedirect( |
| 8985 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 8986 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8987 | |
| 8988 | VerifyReferrerAfterRedirect( |
| 8989 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 8990 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8991 | |
| 8992 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 8993 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 8994 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 8995 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 8996 | referrer); |
| 8997 | |
| 8998 | // The original referrer set on the request is expected to obey the referrer |
| 8999 | // policy and already be stripped to the origin; thus this test case just |
| 9000 | // checks that this policy doesn't cause the referrer to change when following |
| 9001 | // a redirect. |
| 9002 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9003 | referrer.GetOrigin()); |
| 9004 | |
| 9005 | VerifyReferrerAfterRedirect( |
| 9006 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9007 | referrer); |
| 9008 | |
| 9009 | // The original referrer set on the request is expected to obey the referrer |
| 9010 | // policy and already be stripped to the origin; thus this test case just |
| 9011 | // checks that this policy doesn't cause the referrer to change when following |
| 9012 | // a redirect. |
| 9013 | VerifyReferrerAfterRedirect( |
| 9014 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9015 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9016 | |
| 9017 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9018 | } |
| 9019 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9020 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToCrossOriginHTTPS) { |
| 9021 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 9022 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9023 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9024 | |
| 9025 | VerifyReferrerAfterRedirect( |
| 9026 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9027 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9028 | |
| 9029 | VerifyReferrerAfterRedirect( |
| 9030 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9031 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9032 | |
| 9033 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9034 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9035 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9036 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9037 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9038 | referrer); |
| 9039 | |
| 9040 | // The original referrer set on the request is expected to obey the referrer |
| 9041 | // policy and already be stripped to the origin; thus this test case just |
| 9042 | // checks that this policy doesn't cause the referrer to change when following |
| 9043 | // a redirect. |
| 9044 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9045 | referrer.GetOrigin()); |
| 9046 | |
| 9047 | VerifyReferrerAfterRedirect( |
| 9048 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9049 | |
| 9050 | // The original referrer set on the request is expected to obey the referrer |
| 9051 | // policy and already be stripped to the origin; thus this test case just |
| 9052 | // checks that this policy doesn't cause the referrer to change when following |
| 9053 | // a redirect. |
| 9054 | VerifyReferrerAfterRedirect( |
| 9055 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9056 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9057 | |
| 9058 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9059 | } |
| 9060 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9061 | TEST_F(URLRequestTestReferrerPolicy, HTTPToHTTPS) { |
| 9062 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 9063 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9064 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9065 | |
| 9066 | VerifyReferrerAfterRedirect( |
| 9067 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9068 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9069 | |
| 9070 | VerifyReferrerAfterRedirect( |
| 9071 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9072 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9073 | |
| 9074 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9075 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9076 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9077 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9078 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9079 | referrer); |
| 9080 | |
| 9081 | // The original referrer set on the request is expected to obey the referrer |
| 9082 | // policy and already be stripped to the origin; thus this test case just |
| 9083 | // checks that this policy doesn't cause the referrer to change when following |
| 9084 | // a redirect. |
| 9085 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9086 | referrer.GetOrigin()); |
| 9087 | |
| 9088 | VerifyReferrerAfterRedirect( |
| 9089 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9090 | |
| 9091 | // The original referrer set on the request is expected to obey the referrer |
| 9092 | // policy and already be stripped to the origin; thus this test case just |
| 9093 | // checks that this policy doesn't cause the referrer to change when following |
| 9094 | // a redirect. |
| 9095 | VerifyReferrerAfterRedirect( |
| 9096 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9097 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9098 | |
| 9099 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9100 | } |
| 9101 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9102 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToHTTP) { |
| 9103 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 9104 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9105 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9106 | |
| 9107 | VerifyReferrerAfterRedirect( |
| 9108 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9109 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9110 | |
| 9111 | VerifyReferrerAfterRedirect( |
| 9112 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9113 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9114 | |
| 9115 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9116 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9117 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9118 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9119 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9120 | referrer); |
| 9121 | |
| 9122 | // The original referrer set on the request is expected to obey the referrer |
| 9123 | // policy and already be stripped to the origin; thus this test case just |
| 9124 | // checks that this policy doesn't cause the referrer to change when following |
| 9125 | // a redirect. |
| 9126 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9127 | referrer.GetOrigin()); |
| 9128 | |
| 9129 | VerifyReferrerAfterRedirect( |
| 9130 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9131 | |
| 9132 | // The original referrer set on the request is expected to obey the referrer |
| 9133 | // policy and already be stripped to the origin, though it should be |
| 9134 | // subsequently cleared during the downgrading redirect. |
| 9135 | VerifyReferrerAfterRedirect( |
| 9136 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9137 | referrer.GetOrigin(), GURL()); |
| 9138 | |
| 9139 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9140 | } |
| 9141 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 9142 | class HTTPSRequestTest : public TestWithScopedTaskEnvironment { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9143 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 9144 | HTTPSRequestTest() : default_context_(true) { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9145 | default_context_.set_network_delegate(&default_network_delegate_); |
| 9146 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9147 | } |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9148 | ~HTTPSRequestTest() override { |
| 9149 | SetTransportSecurityStateSourceForTesting(nullptr); |
| 9150 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9151 | |
| 9152 | protected: |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9153 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9154 | TestURLRequestContext default_context_; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9155 | }; |
| 9156 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 9157 | TEST_F(HTTPSRequestTest, HTTPSGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9158 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9159 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9160 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9161 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9162 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9163 | TestDelegate d; |
| 9164 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9165 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9166 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9167 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9168 | r->Start(); |
| 9169 | EXPECT_TRUE(r->is_pending()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9170 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9171 | d.RunUntilComplete(); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9172 | |
| 9173 | EXPECT_EQ(1, d.response_started_count()); |
| 9174 | EXPECT_FALSE(d.received_data_before_response()); |
| 9175 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9176 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 9177 | EXPECT_EQ(test_server.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9178 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 9179 | EXPECT_EQ(test_server.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9180 | r->GetSocketAddress().port()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9181 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9182 | } |
| 9183 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 9184 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9185 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9186 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9187 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9188 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9189 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9190 | |
| 9191 | bool err_allowed = true; |
| 9192 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 9193 | TestDelegate d; |
| 9194 | { |
| 9195 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9196 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9197 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9198 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9199 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9200 | r->Start(); |
| 9201 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9202 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9203 | d.RunUntilComplete(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9204 | |
| 9205 | EXPECT_EQ(1, d.response_started_count()); |
| 9206 | EXPECT_FALSE(d.received_data_before_response()); |
| 9207 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9208 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9209 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9210 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9211 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9212 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9213 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9214 | } |
| 9215 | } |
| 9216 | } |
| 9217 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 9218 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9219 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9220 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED); |
| 9221 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9222 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9223 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9224 | |
| 9225 | // Iterate from false to true, just so that we do the opposite of the |
| 9226 | // previous test in order to increase test coverage. |
| 9227 | bool err_allowed = false; |
| 9228 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 9229 | TestDelegate d; |
| 9230 | { |
| 9231 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9232 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9233 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9234 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9235 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9236 | r->Start(); |
| 9237 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9238 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9239 | d.RunUntilComplete(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9240 | |
| 9241 | EXPECT_EQ(1, d.response_started_count()); |
| 9242 | EXPECT_FALSE(d.received_data_before_response()); |
| 9243 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9244 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9245 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9246 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9247 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9248 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9249 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9250 | } |
| 9251 | } |
| 9252 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 9253 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9254 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 9255 | // security state. (see http://crbug.com/550977). |
| 9256 | #if !defined(OS_IOS) |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9257 | // This tests that a load of a domain with preloaded HSTS and HPKP with a |
| 9258 | // certificate error sets the |certificate_errors_are_fatal| flag correctly. |
| 9259 | // This flag will cause the interstitial to be fatal. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9260 | TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9261 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 9262 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9263 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9264 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9265 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9266 | ASSERT_TRUE(test_server.Start()); |
| 9267 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9268 | // We require that the URL be hsts-hpkp-preloaded.test. This is a test domain |
| 9269 | // that has a preloaded HSTS+HPKP entry in the TransportSecurityState. This |
| 9270 | // means that we have to use a MockHostResolver in order to direct |
| 9271 | // hsts-hpkp-preloaded.test to the testserver. By default, MockHostResolver |
| 9272 | // maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9273 | |
| 9274 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9275 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9276 | TestURLRequestContext context(true); |
| 9277 | context.set_network_delegate(&network_delegate); |
| 9278 | context.set_host_resolver(&host_resolver); |
| 9279 | TransportSecurityState transport_security_state; |
| 9280 | context.set_transport_security_state(&transport_security_state); |
| 9281 | context.Init(); |
| 9282 | |
| 9283 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9284 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9285 | GURL(base::StringPrintf("https://hsts-hpkp-preloaded.test:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9286 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9287 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9288 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9289 | r->Start(); |
| 9290 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9291 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9292 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9293 | |
| 9294 | EXPECT_EQ(1, d.response_started_count()); |
| 9295 | EXPECT_FALSE(d.received_data_before_response()); |
| 9296 | EXPECT_TRUE(d.have_certificate_errors()); |
| 9297 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 9298 | } |
| 9299 | |
| 9300 | // This tests that cached HTTPS page loads do not cause any updates to the |
| 9301 | // TransportSecurityState. |
| 9302 | TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9303 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 9304 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9305 | // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't |
| 9306 | // matter. It just has to be any error. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9307 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9308 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9309 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9310 | ASSERT_TRUE(test_server.Start()); |
| 9311 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9312 | // We require that the URL be hsts-hpkp-preloaded.test. This is a test domain |
| 9313 | // that has a preloaded HSTS+HPKP entry in the TransportSecurityState. This |
| 9314 | // means that we have to use a MockHostResolver in order to direct |
| 9315 | // hsts-hpkp-preloaded.test to the testserver. By default, MockHostResolver |
| 9316 | // maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9317 | |
| 9318 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9319 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9320 | TestURLRequestContext context(true); |
| 9321 | context.set_network_delegate(&network_delegate); |
| 9322 | context.set_host_resolver(&host_resolver); |
| 9323 | TransportSecurityState transport_security_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9324 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9325 | TransportSecurityState::STSState static_sts_state; |
| 9326 | TransportSecurityState::PKPState static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9327 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9328 | "hsts-hpkp-preloaded.test", &static_sts_state, &static_pkp_state)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9329 | context.set_transport_security_state(&transport_security_state); |
| 9330 | context.Init(); |
| 9331 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9332 | TransportSecurityState::STSState dynamic_sts_state; |
| 9333 | TransportSecurityState::PKPState dynamic_pkp_state; |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9334 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
| 9335 | "hsts-hpkp-preloaded.test", &dynamic_sts_state)); |
| 9336 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
| 9337 | "hsts-hpkp-preloaded.test", &dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9338 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9339 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9340 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9341 | GURL(base::StringPrintf("https://hsts-hpkp-preloaded.test:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9342 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9343 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9344 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9345 | r->Start(); |
| 9346 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9347 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9348 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9349 | |
| 9350 | EXPECT_EQ(1, d.response_started_count()); |
| 9351 | EXPECT_FALSE(d.received_data_before_response()); |
| 9352 | EXPECT_TRUE(d.have_certificate_errors()); |
| 9353 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 9354 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9355 | // Get a fresh copy of the states, and check that they haven't changed. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9356 | TransportSecurityState::STSState new_static_sts_state; |
| 9357 | TransportSecurityState::PKPState new_static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9358 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9359 | "hsts-hpkp-preloaded.test", &new_static_sts_state, |
| 9360 | &new_static_pkp_state)); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9361 | TransportSecurityState::STSState new_dynamic_sts_state; |
| 9362 | TransportSecurityState::PKPState new_dynamic_pkp_state; |
| 9363 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9364 | "hsts-hpkp-preloaded.test", &new_dynamic_sts_state)); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9365 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9366 | "hsts-hpkp-preloaded.test", &new_dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 9367 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 9368 | EXPECT_EQ(new_static_sts_state.upgrade_mode, static_sts_state.upgrade_mode); |
| 9369 | EXPECT_EQ(new_static_sts_state.include_subdomains, |
| 9370 | static_sts_state.include_subdomains); |
| 9371 | EXPECT_EQ(new_static_pkp_state.include_subdomains, |
| 9372 | static_pkp_state.include_subdomains); |
rsleevi | 91d4c9c | 2016-05-14 20:28:48 | [diff] [blame] | 9373 | EXPECT_EQ(new_static_pkp_state.spki_hashes, static_pkp_state.spki_hashes); |
| 9374 | EXPECT_EQ(new_static_pkp_state.bad_spki_hashes, |
| 9375 | static_pkp_state.bad_spki_hashes); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9376 | } |
| 9377 | |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9378 | // Make sure HSTS preserves a POST request's method and body. |
| 9379 | TEST_F(HTTPSRequestTest, HSTSPreservesPosts) { |
| 9380 | static const char kData[] = "hello world"; |
| 9381 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9382 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9383 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9384 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9385 | ASSERT_TRUE(test_server.Start()); |
| 9386 | |
| 9387 | |
| 9388 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 9389 | // address, so a MockHostResolver is needed to redirect www.somewhere.com to |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9390 | // the EmbeddedTestServer. By default, MockHostResolver maps all hosts |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9391 | // to 127.0.0.1. |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9392 | MockHostResolver host_resolver; |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9393 | |
| 9394 | // Force https for www.somewhere.com. |
| 9395 | TransportSecurityState transport_security_state; |
[email protected] | 474f079e | 2013-03-02 19:11:20 | [diff] [blame] | 9396 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 9397 | bool include_subdomains = false; |
| 9398 | transport_security_state.AddHSTS("www.somewhere.com", expiry, |
| 9399 | include_subdomains); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9400 | |
| 9401 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 9402 | |
| 9403 | TestURLRequestContext context(true); |
| 9404 | context.set_host_resolver(&host_resolver); |
| 9405 | context.set_transport_security_state(&transport_security_state); |
| 9406 | context.set_network_delegate(&network_delegate); |
| 9407 | context.Init(); |
| 9408 | |
| 9409 | TestDelegate d; |
| 9410 | // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will |
| 9411 | // cause a certificate error. Ignore the error. |
| 9412 | d.set_allow_certificate_errors(true); |
| 9413 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9414 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9415 | GURL(base::StringPrintf("http://www.somewhere.com:%d/echo", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9416 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9417 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9418 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 9419 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9420 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9421 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9422 | d.RunUntilComplete(); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9423 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9424 | EXPECT_EQ("https", req->url().scheme()); |
| 9425 | EXPECT_EQ("POST", req->method()); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9426 | EXPECT_EQ(kData, d.data_received()); |
[email protected] | 07d9bfd | 2013-06-27 14:16:40 | [diff] [blame] | 9427 | |
| 9428 | LoadTimingInfo load_timing_info; |
| 9429 | network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info); |
| 9430 | // LoadTimingInfo of HSTS redirects is similar to that of network cache hits |
| 9431 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 9432 | } |
| 9433 | |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9434 | // Make sure that the CORS headers are added to cross-origin HSTS redirects. |
| 9435 | TEST_F(HTTPSRequestTest, HSTSCrossOriginAddHeaders) { |
| 9436 | static const char kOriginHeaderValue[] = "http://www.example.com"; |
| 9437 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9438 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9439 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9440 | ASSERT_TRUE(test_server.Start()); |
| 9441 | |
| 9442 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 9443 | // address, so a MockHostResolver is needed to redirect example.net to the |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9444 | // EmbeddedTestServer. MockHostResolver maps all hosts to 127.0.0.1 by |
| 9445 | // default. |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9446 | MockHostResolver host_resolver; |
| 9447 | |
| 9448 | TransportSecurityState transport_security_state; |
| 9449 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 9450 | bool include_subdomains = false; |
| 9451 | transport_security_state.AddHSTS("example.net", expiry, include_subdomains); |
| 9452 | |
| 9453 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 9454 | |
| 9455 | MockCertVerifier cert_verifier; |
| 9456 | cert_verifier.set_default_result(OK); |
| 9457 | |
| 9458 | TestURLRequestContext context(true); |
| 9459 | context.set_host_resolver(&host_resolver); |
| 9460 | context.set_transport_security_state(&transport_security_state); |
| 9461 | context.set_network_delegate(&network_delegate); |
| 9462 | context.set_cert_verifier(&cert_verifier); |
| 9463 | context.Init(); |
| 9464 | |
| 9465 | GURL hsts_http_url(base::StringPrintf("http://example.net:%d/somehstssite", |
| 9466 | test_server.host_port_pair().port())); |
| 9467 | url::Replacements<char> replacements; |
| 9468 | const char kNewScheme[] = "https"; |
| 9469 | replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme))); |
| 9470 | GURL hsts_https_url = hsts_http_url.ReplaceComponents(replacements); |
| 9471 | |
| 9472 | TestDelegate d; |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9473 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9474 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 9475 | hsts_http_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9476 | // Set Origin header to simulate a cross-origin request. |
| 9477 | HttpRequestHeaders request_headers; |
| 9478 | request_headers.SetHeader("Origin", kOriginHeaderValue); |
| 9479 | req->SetExtraRequestHeaders(request_headers); |
| 9480 | |
| 9481 | req->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 9482 | d.RunUntilRedirect(); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 9483 | |
| 9484 | EXPECT_EQ(1, d.received_redirect_count()); |
| 9485 | |
| 9486 | const HttpResponseHeaders* headers = req->response_headers(); |
| 9487 | std::string redirect_location; |
| 9488 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Location", &redirect_location)); |
| 9489 | EXPECT_EQ(hsts_https_url.spec(), redirect_location); |
| 9490 | |
| 9491 | std::string received_cors_header; |
| 9492 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Access-Control-Allow-Origin", |
| 9493 | &received_cors_header)); |
| 9494 | EXPECT_EQ(kOriginHeaderValue, received_cors_header); |
| 9495 | } |
| 9496 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9497 | namespace { |
| 9498 | |
| 9499 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 9500 | public: |
| 9501 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 9502 | set_on_complete(base::DoNothing()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9503 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 9504 | void OnCertificateRequested(URLRequest* request, |
| 9505 | SSLCertRequestInfo* cert_request_info) override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9506 | on_certificate_requested_count_++; |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 9507 | std::move(on_certificate_requested_).Run(); |
| 9508 | } |
| 9509 | void RunUntilCertificateRequested() { |
| 9510 | base::RunLoop run_loop; |
| 9511 | on_certificate_requested_ = run_loop.QuitClosure(); |
| 9512 | run_loop.Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9513 | } |
| 9514 | int on_certificate_requested_count() { |
| 9515 | return on_certificate_requested_count_; |
| 9516 | } |
| 9517 | private: |
| 9518 | int on_certificate_requested_count_; |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 9519 | base::OnceClosure on_certificate_requested_; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9520 | }; |
| 9521 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9522 | class TestSSLPrivateKey : public SSLPrivateKey { |
| 9523 | public: |
| 9524 | explicit TestSSLPrivateKey(scoped_refptr<SSLPrivateKey> key) |
| 9525 | : key_(std::move(key)) {} |
| 9526 | |
| 9527 | void set_fail_signing(bool fail_signing) { fail_signing_ = fail_signing; } |
| 9528 | int sign_count() const { return sign_count_; } |
| 9529 | |
David Benjamin | b65b073 | 2018-11-09 20:33:53 | [diff] [blame] | 9530 | std::string GetProviderName() override { return key_->GetProviderName(); } |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9531 | std::vector<uint16_t> GetAlgorithmPreferences() override { |
| 9532 | return key_->GetAlgorithmPreferences(); |
| 9533 | } |
| 9534 | void Sign(uint16_t algorithm, |
| 9535 | base::span<const uint8_t> input, |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 9536 | SignCallback callback) override { |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9537 | sign_count_++; |
| 9538 | if (fail_signing_) { |
| 9539 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 9540 | FROM_HERE, base::BindOnce(std::move(callback), |
| 9541 | ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED, |
| 9542 | std::vector<uint8_t>())); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9543 | } else { |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 9544 | key_->Sign(algorithm, input, std::move(callback)); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9545 | } |
| 9546 | } |
| 9547 | |
| 9548 | private: |
| 9549 | ~TestSSLPrivateKey() override = default; |
| 9550 | |
| 9551 | scoped_refptr<SSLPrivateKey> key_; |
| 9552 | bool fail_signing_ = false; |
| 9553 | int sign_count_ = 0; |
| 9554 | }; |
| 9555 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9556 | } // namespace |
| 9557 | |
| 9558 | // TODO(davidben): Test the rest of the code. Specifically, |
| 9559 | // - Filtering which certificates to select. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9560 | // - Getting a certificate request in an SSL renegotiation sending the |
| 9561 | // HTTP request. |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9562 | TEST_F(HTTPSRequestTest, ClientAuthNoCertificate) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9563 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9564 | net::SSLServerConfig ssl_config; |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 9565 | ssl_config.client_cert_type = |
| 9566 | SSLServerConfig::ClientCertType::OPTIONAL_CLIENT_CERT; |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9567 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9568 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9569 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9570 | ASSERT_TRUE(test_server.Start()); |
| 9571 | |
| 9572 | SSLClientAuthTestDelegate d; |
| 9573 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9574 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9575 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9576 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9577 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9578 | r->Start(); |
| 9579 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9580 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 9581 | d.RunUntilCertificateRequested(); |
| 9582 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9583 | |
| 9584 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 9585 | EXPECT_FALSE(d.received_data_before_response()); |
| 9586 | EXPECT_EQ(0, d.bytes_received()); |
| 9587 | |
| 9588 | // Send no certificate. |
| 9589 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 9590 | // all platforms so we can test sending a cert as well. |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 9591 | r->ContinueWithCertificate(nullptr, nullptr); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9592 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9593 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9594 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9595 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9596 | EXPECT_EQ(1, d.response_started_count()); |
| 9597 | EXPECT_FALSE(d.received_data_before_response()); |
| 9598 | EXPECT_NE(0, d.bytes_received()); |
| 9599 | } |
| 9600 | } |
| 9601 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9602 | TEST_F(HTTPSRequestTest, ClientAuth) { |
| 9603 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 9604 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 9605 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 9606 | ASSERT_TRUE(identity); |
| 9607 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 9608 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 9609 | |
| 9610 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9611 | net::SSLServerConfig ssl_config; |
| 9612 | ssl_config.client_cert_type = |
| 9613 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 9614 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 9615 | test_server.AddDefaultHandlers( |
| 9616 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9617 | ASSERT_TRUE(test_server.Start()); |
| 9618 | |
| 9619 | { |
| 9620 | SSLClientAuthTestDelegate d; |
| 9621 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 9622 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9623 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9624 | |
| 9625 | r->Start(); |
| 9626 | EXPECT_TRUE(r->is_pending()); |
| 9627 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 9628 | d.RunUntilCertificateRequested(); |
| 9629 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9630 | |
| 9631 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 9632 | EXPECT_FALSE(d.received_data_before_response()); |
| 9633 | EXPECT_EQ(0, d.bytes_received()); |
| 9634 | |
| 9635 | // Send a certificate. |
| 9636 | r->ContinueWithCertificate(identity->certificate(), private_key); |
| 9637 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9638 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9639 | |
| 9640 | EXPECT_EQ(OK, d.request_status()); |
| 9641 | EXPECT_EQ(1, d.response_started_count()); |
| 9642 | EXPECT_FALSE(d.received_data_before_response()); |
| 9643 | EXPECT_NE(0, d.bytes_received()); |
| 9644 | |
| 9645 | // The private key should have been used. |
| 9646 | EXPECT_EQ(1, private_key->sign_count()); |
| 9647 | } |
| 9648 | |
| 9649 | // Close all connections and clear the session cache to force a new handshake. |
| 9650 | default_context_.http_transaction_factory() |
| 9651 | ->GetSession() |
| 9652 | ->CloseAllConnections(); |
| 9653 | SSLClientSocket::ClearSessionCache(); |
| 9654 | |
| 9655 | // Connecting again should not call OnCertificateRequested. The identity is |
| 9656 | // taken from the client auth cache. |
| 9657 | { |
| 9658 | SSLClientAuthTestDelegate d; |
| 9659 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 9660 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9661 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9662 | |
| 9663 | r->Start(); |
| 9664 | EXPECT_TRUE(r->is_pending()); |
| 9665 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9666 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9667 | |
| 9668 | EXPECT_EQ(OK, d.request_status()); |
| 9669 | EXPECT_EQ(0, d.on_certificate_requested_count()); |
| 9670 | EXPECT_FALSE(d.received_data_before_response()); |
| 9671 | EXPECT_EQ(1, d.response_started_count()); |
| 9672 | EXPECT_FALSE(d.received_data_before_response()); |
| 9673 | EXPECT_NE(0, d.bytes_received()); |
| 9674 | |
| 9675 | // The private key should have been used. |
| 9676 | EXPECT_EQ(2, private_key->sign_count()); |
| 9677 | } |
| 9678 | } |
| 9679 | |
| 9680 | // Test that private keys that fail to sign anything get evicted from the cache. |
| 9681 | TEST_F(HTTPSRequestTest, ClientAuthFailSigning) { |
| 9682 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 9683 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 9684 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 9685 | ASSERT_TRUE(identity); |
| 9686 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 9687 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 9688 | private_key->set_fail_signing(true); |
| 9689 | |
| 9690 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9691 | net::SSLServerConfig ssl_config; |
| 9692 | ssl_config.client_cert_type = |
| 9693 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 9694 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 9695 | test_server.AddDefaultHandlers( |
| 9696 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9697 | ASSERT_TRUE(test_server.Start()); |
| 9698 | |
| 9699 | { |
| 9700 | SSLClientAuthTestDelegate d; |
| 9701 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 9702 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9703 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9704 | |
| 9705 | r->Start(); |
| 9706 | EXPECT_TRUE(r->is_pending()); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 9707 | |
| 9708 | d.RunUntilCertificateRequested(); |
| 9709 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9710 | |
| 9711 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 9712 | EXPECT_FALSE(d.received_data_before_response()); |
| 9713 | EXPECT_EQ(0, d.bytes_received()); |
| 9714 | |
| 9715 | // Send a certificate. |
| 9716 | r->ContinueWithCertificate(identity->certificate(), private_key); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9717 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9718 | |
| 9719 | // The private key cannot sign anything, so we report an error. |
| 9720 | EXPECT_EQ(ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED, d.request_status()); |
| 9721 | EXPECT_EQ(1, d.response_started_count()); |
| 9722 | EXPECT_FALSE(d.received_data_before_response()); |
| 9723 | EXPECT_EQ(0, d.bytes_received()); |
| 9724 | |
| 9725 | // The private key should have been used. |
| 9726 | EXPECT_EQ(1, private_key->sign_count()); |
| 9727 | } |
| 9728 | |
| 9729 | // Close all connections and clear the session cache to force a new handshake. |
| 9730 | default_context_.http_transaction_factory() |
| 9731 | ->GetSession() |
| 9732 | ->CloseAllConnections(); |
| 9733 | SSLClientSocket::ClearSessionCache(); |
| 9734 | |
| 9735 | // The bad identity should have been evicted from the cache, so connecting |
| 9736 | // again should call OnCertificateRequested again. |
| 9737 | { |
| 9738 | SSLClientAuthTestDelegate d; |
| 9739 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 9740 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9741 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9742 | |
| 9743 | r->Start(); |
| 9744 | EXPECT_TRUE(r->is_pending()); |
| 9745 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 9746 | d.RunUntilCertificateRequested(); |
| 9747 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9748 | |
| 9749 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 9750 | EXPECT_FALSE(d.received_data_before_response()); |
| 9751 | EXPECT_EQ(0, d.bytes_received()); |
| 9752 | |
| 9753 | // There should have been no additional uses of the private key. |
| 9754 | EXPECT_EQ(1, private_key->sign_count()); |
| 9755 | } |
| 9756 | } |
| 9757 | |
| 9758 | // Test that cached private keys that fail to sign anything trigger a |
| 9759 | // retry. This is so we handle unplugged smartcards |
| 9760 | // gracefully. https://crbug.com/813022. |
| 9761 | TEST_F(HTTPSRequestTest, ClientAuthFailSigningRetry) { |
| 9762 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 9763 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 9764 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 9765 | ASSERT_TRUE(identity); |
| 9766 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 9767 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 9768 | |
| 9769 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9770 | net::SSLServerConfig ssl_config; |
| 9771 | ssl_config.client_cert_type = |
| 9772 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 9773 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 9774 | test_server.AddDefaultHandlers( |
| 9775 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9776 | ASSERT_TRUE(test_server.Start()); |
| 9777 | |
| 9778 | // Connect with a client certificate to put it in the client auth cache. |
| 9779 | { |
| 9780 | SSLClientAuthTestDelegate d; |
| 9781 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 9782 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9783 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9784 | |
| 9785 | r->Start(); |
| 9786 | EXPECT_TRUE(r->is_pending()); |
| 9787 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 9788 | d.RunUntilCertificateRequested(); |
| 9789 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9790 | |
| 9791 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 9792 | EXPECT_FALSE(d.received_data_before_response()); |
| 9793 | EXPECT_EQ(0, d.bytes_received()); |
| 9794 | |
| 9795 | r->ContinueWithCertificate(identity->certificate(), private_key); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9796 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9797 | |
| 9798 | EXPECT_EQ(OK, d.request_status()); |
| 9799 | EXPECT_EQ(1, d.response_started_count()); |
| 9800 | EXPECT_FALSE(d.received_data_before_response()); |
| 9801 | EXPECT_NE(0, d.bytes_received()); |
| 9802 | |
| 9803 | // The private key should have been used. |
| 9804 | EXPECT_EQ(1, private_key->sign_count()); |
| 9805 | } |
| 9806 | |
| 9807 | // Close all connections and clear the session cache to force a new handshake. |
| 9808 | default_context_.http_transaction_factory() |
| 9809 | ->GetSession() |
| 9810 | ->CloseAllConnections(); |
| 9811 | SSLClientSocket::ClearSessionCache(); |
| 9812 | |
| 9813 | // Cause the private key to fail. Connecting again should attempt to use it, |
| 9814 | // notice the failure, and then request a new identity via |
| 9815 | // OnCertificateRequested. |
| 9816 | private_key->set_fail_signing(true); |
| 9817 | |
| 9818 | { |
| 9819 | SSLClientAuthTestDelegate d; |
| 9820 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 9821 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9822 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 9823 | |
| 9824 | r->Start(); |
| 9825 | EXPECT_TRUE(r->is_pending()); |
| 9826 | |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 9827 | d.RunUntilCertificateRequested(); |
| 9828 | EXPECT_TRUE(r->is_pending()); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 9829 | |
| 9830 | // There was an additional signing call on the private key (the one which |
| 9831 | // failed). |
| 9832 | EXPECT_EQ(2, private_key->sign_count()); |
| 9833 | |
| 9834 | // That caused another OnCertificateRequested call. |
| 9835 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 9836 | EXPECT_FALSE(d.received_data_before_response()); |
| 9837 | EXPECT_EQ(0, d.bytes_received()); |
| 9838 | } |
| 9839 | } |
| 9840 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9841 | TEST_F(HTTPSRequestTest, ResumeTest) { |
| 9842 | // Test that we attempt a session resume when making two connections to the |
| 9843 | // same host. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9844 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9845 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9846 | SpawnedTestServer test_server( |
| 9847 | SpawnedTestServer::TYPE_HTTPS, |
| 9848 | ssl_options, |
| 9849 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9850 | ASSERT_TRUE(test_server.Start()); |
| 9851 | |
| 9852 | SSLClientSocket::ClearSessionCache(); |
| 9853 | |
| 9854 | { |
| 9855 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9856 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9857 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 9858 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9859 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9860 | r->Start(); |
| 9861 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9862 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9863 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9864 | |
| 9865 | EXPECT_EQ(1, d.response_started_count()); |
| 9866 | } |
| 9867 | |
| 9868 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 9869 | CloseAllConnections(); |
| 9870 | |
| 9871 | { |
| 9872 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9873 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9874 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 9875 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9876 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9877 | r->Start(); |
| 9878 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9879 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9880 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9881 | |
| 9882 | // The response will look like; |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 9883 | // lookup uvw (TLS 1.3's compatibility session ID) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9884 | // insert abc |
| 9885 | // lookup abc |
| 9886 | // insert xyz |
| 9887 | // |
| 9888 | // With a newline at the end which makes the split think that there are |
| 9889 | // four lines. |
| 9890 | |
| 9891 | EXPECT_EQ(1, d.response_started_count()); |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 9892 | std::vector<std::string> lines = base::SplitString( |
| 9893 | d.data_received(), "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 9894 | ASSERT_EQ(5u, lines.size()) << d.data_received(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9895 | |
| 9896 | std::string session_id; |
| 9897 | |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 9898 | for (size_t i = 0; i < 3; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 9899 | std::vector<std::string> parts = base::SplitString( |
| 9900 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9901 | ASSERT_EQ(2u, parts.size()); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 9902 | if (i % 2 == 1) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9903 | EXPECT_EQ("insert", parts[0]); |
| 9904 | session_id = parts[1]; |
| 9905 | } else { |
| 9906 | EXPECT_EQ("lookup", parts[0]); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 9907 | if (i != 0) |
| 9908 | EXPECT_EQ(session_id, parts[1]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9909 | } |
| 9910 | } |
| 9911 | } |
| 9912 | } |
| 9913 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 9914 | // AssertTwoDistinctSessionsInserted checks that |session_info|, which must be |
| 9915 | // the result of fetching "ssl-session-cache" from the test server, indicates |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 9916 | // that exactly two different sessions were inserted, with no real lookups. |
| 9917 | // There will be a fake TLS 1.3 session ID lookup that can be ignored. |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 9918 | static void AssertTwoDistinctSessionsInserted(const string& session_info) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 9919 | std::vector<std::string> lines = base::SplitString( |
| 9920 | session_info, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 9921 | |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 9922 | size_t inserts = 0; |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 9923 | std::string session_id; |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 9924 | for (size_t i = 0; i < lines.size() - 1; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 9925 | std::vector<std::string> parts = base::SplitString( |
| 9926 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 9927 | ASSERT_EQ(2u, parts.size()); |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 9928 | if (parts[0] == "insert") { |
| 9929 | inserts++; |
| 9930 | if (i == 0) { |
| 9931 | session_id = parts[1]; |
| 9932 | } else { |
| 9933 | EXPECT_NE(session_id, parts[1]); |
| 9934 | } |
| 9935 | } else if (parts[0] == "lookup") { |
| 9936 | // The fake TLS 1.3 session ID will produce fake lookups. Check that the |
| 9937 | // inserted ID was not looked up. |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 9938 | EXPECT_NE(session_id, parts[1]); |
| 9939 | } |
| 9940 | } |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 9941 | ASSERT_EQ(2u, inserts) << session_info; |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 9942 | } |
| 9943 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9944 | TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { |
| 9945 | // Test that sessions aren't resumed when the value of ssl_session_cache_shard |
| 9946 | // differs. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9947 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9948 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9949 | SpawnedTestServer test_server( |
| 9950 | SpawnedTestServer::TYPE_HTTPS, |
| 9951 | ssl_options, |
| 9952 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9953 | ASSERT_TRUE(test_server.Start()); |
| 9954 | |
| 9955 | SSLClientSocket::ClearSessionCache(); |
| 9956 | |
| 9957 | { |
| 9958 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9959 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9960 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 9961 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9962 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9963 | r->Start(); |
| 9964 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9965 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9966 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9967 | |
| 9968 | EXPECT_EQ(1, d.response_started_count()); |
| 9969 | } |
| 9970 | |
| 9971 | // Now create a new HttpCache with a different ssl_session_cache_shard value. |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 9972 | HttpNetworkSession::Context session_context; |
| 9973 | session_context.host_resolver = default_context_.host_resolver(); |
| 9974 | session_context.cert_verifier = default_context_.cert_verifier(); |
| 9975 | session_context.transport_security_state = |
| 9976 | default_context_.transport_security_state(); |
| 9977 | session_context.cert_transparency_verifier = |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 9978 | default_context_.cert_transparency_verifier(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 9979 | session_context.ct_policy_enforcer = default_context_.ct_policy_enforcer(); |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 9980 | session_context.proxy_resolution_service = default_context_.proxy_resolution_service(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 9981 | session_context.ssl_config_service = default_context_.ssl_config_service(); |
| 9982 | session_context.http_auth_handler_factory = |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9983 | default_context_.http_auth_handler_factory(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 9984 | session_context.http_server_properties = |
| 9985 | default_context_.http_server_properties(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9986 | |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 9987 | HttpNetworkSession network_session(HttpNetworkSession::Params(), |
| 9988 | session_context); |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 9989 | std::unique_ptr<HttpCache> cache( |
| 9990 | new HttpCache(&network_session, HttpCache::DefaultBackend::InMemory(0), |
| 9991 | false /* is_main_cache */)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9992 | |
| 9993 | default_context_.set_http_transaction_factory(cache.get()); |
| 9994 | |
| 9995 | { |
| 9996 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9997 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9998 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 9999 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10000 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10001 | r->Start(); |
| 10002 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10003 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10004 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10005 | |
| 10006 | // The response will look like; |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10007 | // lookup uvw (TLS 1.3's compatibility session ID) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10008 | // insert abc |
| 10009 | // insert xyz |
| 10010 | // |
| 10011 | // With a newline at the end which makes the split think that there are |
| 10012 | // three lines. |
| 10013 | |
| 10014 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10015 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10016 | } |
| 10017 | } |
| 10018 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 10019 | class HTTPSFallbackTest : public TestWithScopedTaskEnvironment { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10020 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 10021 | HTTPSFallbackTest() : context_(true) { |
Nick Harper | 2243e800 | 2018-09-28 20:33:26 | [diff] [blame] | 10022 | ssl_config_service_ = std::make_unique<TestSSLConfigService>(); |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10023 | context_.set_ssl_config_service(ssl_config_service_.get()); |
| 10024 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 10025 | ~HTTPSFallbackTest() override = default; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10026 | |
| 10027 | protected: |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10028 | TestSSLConfigService* ssl_config_service() { |
| 10029 | return ssl_config_service_.get(); |
| 10030 | } |
| 10031 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10032 | void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) { |
| 10033 | DCHECK(!request_); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10034 | context_.Init(); |
| 10035 | delegate_.set_allow_certificate_errors(true); |
| 10036 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10037 | SpawnedTestServer test_server( |
| 10038 | SpawnedTestServer::TYPE_HTTPS, |
| 10039 | ssl_options, |
| 10040 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10041 | ASSERT_TRUE(test_server.Start()); |
| 10042 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10043 | request_ = context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10044 | &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10045 | request_->Start(); |
| 10046 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10047 | delegate_.RunUntilComplete(); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10048 | } |
| 10049 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10050 | void ExpectConnection(int version) { |
| 10051 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 10052 | EXPECT_NE(0, delegate_.bytes_received()); |
| 10053 | EXPECT_EQ(version, SSLConnectionStatusToVersion( |
| 10054 | request_->ssl_info().connection_status)); |
| 10055 | } |
| 10056 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10057 | void ExpectFailure(int error) { |
| 10058 | EXPECT_EQ(1, delegate_.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 10059 | EXPECT_EQ(error, delegate_.request_status()); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10060 | } |
| 10061 | |
| 10062 | private: |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 10063 | std::unique_ptr<TestSSLConfigService> ssl_config_service_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10064 | TestDelegate delegate_; |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 10065 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10066 | std::unique_ptr<URLRequest> request_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10067 | }; |
| 10068 | |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10069 | // Tests the TLS 1.0 fallback doesn't happen. |
| 10070 | TEST_F(HTTPSFallbackTest, TLSv1NoFallback) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10071 | SpawnedTestServer::SSLOptions ssl_options( |
| 10072 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10073 | ssl_options.tls_intolerant = |
| 10074 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 10075 | |
| 10076 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 10077 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10078 | } |
| 10079 | |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 10080 | // Tests the TLS 1.1 fallback doesn't happen. |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 10081 | TEST_F(HTTPSFallbackTest, TLSv1_1NoFallback) { |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10082 | SpawnedTestServer::SSLOptions ssl_options( |
| 10083 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10084 | ssl_options.tls_intolerant = |
| 10085 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2; |
| 10086 | |
| 10087 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10088 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
| 10089 | } |
| 10090 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10091 | // Tests that TLS 1.3 interference results in a dedicated error code. |
| 10092 | TEST_F(HTTPSFallbackTest, TLSv1_3Interference) { |
| 10093 | SpawnedTestServer::SSLOptions ssl_options( |
| 10094 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10095 | ssl_options.tls_intolerant = |
| 10096 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_3; |
| 10097 | ssl_config_service()->set_max_version(SSL_PROTOCOL_VERSION_TLS1_3); |
| 10098 | |
| 10099 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 10100 | ExpectFailure(ERR_SSL_VERSION_INTERFERENCE); |
| 10101 | } |
| 10102 | |
| 10103 | // Tests that disabling TLS 1.3 leaves TLS 1.3 interference unnoticed. |
| 10104 | TEST_F(HTTPSFallbackTest, TLSv1_3InterferenceDisableVersion) { |
| 10105 | SpawnedTestServer::SSLOptions ssl_options( |
| 10106 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10107 | ssl_options.tls_intolerant = |
| 10108 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_3; |
| 10109 | ssl_config_service()->set_max_version(SSL_PROTOCOL_VERSION_TLS1_2); |
| 10110 | |
| 10111 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 10112 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1_2); |
| 10113 | } |
| 10114 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 10115 | class HTTPSSessionTest : public TestWithScopedTaskEnvironment { |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10116 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 10117 | HTTPSSessionTest() : default_context_(true) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10118 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10119 | |
| 10120 | default_context_.set_network_delegate(&default_network_delegate_); |
| 10121 | default_context_.set_cert_verifier(&cert_verifier_); |
| 10122 | default_context_.Init(); |
| 10123 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 10124 | ~HTTPSSessionTest() override = default; |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10125 | |
| 10126 | protected: |
| 10127 | MockCertVerifier cert_verifier_; |
| 10128 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 10129 | TestURLRequestContext default_context_; |
| 10130 | }; |
| 10131 | |
| 10132 | // Tests that session resumption is not attempted if an invalid certificate |
| 10133 | // is presented. |
| 10134 | TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) { |
| 10135 | SpawnedTestServer::SSLOptions ssl_options; |
| 10136 | ssl_options.record_resume = true; |
| 10137 | SpawnedTestServer test_server( |
| 10138 | SpawnedTestServer::TYPE_HTTPS, |
| 10139 | ssl_options, |
| 10140 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10141 | ASSERT_TRUE(test_server.Start()); |
| 10142 | |
| 10143 | SSLClientSocket::ClearSessionCache(); |
| 10144 | |
| 10145 | // Simulate the certificate being expired and attempt a connection. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10146 | cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10147 | { |
| 10148 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10149 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10150 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10151 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10152 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10153 | r->Start(); |
| 10154 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10155 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10156 | d.RunUntilComplete(); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10157 | |
| 10158 | EXPECT_EQ(1, d.response_started_count()); |
| 10159 | } |
| 10160 | |
| 10161 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 10162 | CloseAllConnections(); |
| 10163 | |
| 10164 | // Now change the certificate to be acceptable (so that the response is |
| 10165 | // loaded), and ensure that no session id is presented to the peer. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10166 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10167 | { |
| 10168 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10169 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10170 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10171 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10172 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10173 | r->Start(); |
| 10174 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10175 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10176 | d.RunUntilComplete(); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10177 | |
| 10178 | // The response will look like; |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10179 | // lookup uvw (TLS 1.3's compatibility session ID) |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10180 | // insert abc |
| 10181 | // insert xyz |
| 10182 | // |
| 10183 | // With a newline at the end which makes the split think that there are |
| 10184 | // three lines. |
| 10185 | // |
| 10186 | // If a session was presented (eg: a bug), then the response would look |
| 10187 | // like; |
Steven Valdez | 0ef94d0 | 2018-11-19 23:28:13 | [diff] [blame] | 10188 | // lookup uvw (TLS 1.3's compatibility session ID) |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10189 | // insert abc |
| 10190 | // lookup abc |
| 10191 | // insert xyz |
| 10192 | |
| 10193 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10194 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10195 | } |
| 10196 | } |
| 10197 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10198 | // This the fingerprint of the "Testing CA" certificate used by the testserver. |
| 10199 | // See net/data/ssl/certificates/ocsp-test-root.pem. |
David Benjamin | 9cedc3a5 | 2017-08-20 21:30:58 | [diff] [blame] | 10200 | static const SHA256HashValue kOCSPTestCertFingerprint = {{ |
| 10201 | 0x0c, 0xa9, 0x05, 0x11, 0xb0, 0xa2, 0xc0, 0x1d, 0x40, 0x6a, 0x99, |
| 10202 | 0x04, 0x21, 0x36, 0x45, 0x3f, 0x59, 0x12, 0x5c, 0x80, 0x64, 0x2d, |
| 10203 | 0x46, 0x6a, 0x3b, 0x78, 0x9e, 0x84, 0xea, 0x54, 0x0f, 0x8b, |
mattm | 3b4376f1 | 2016-10-03 21:07:15 | [diff] [blame] | 10204 | }}; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10205 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 10206 | // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the |
| 10207 | // testserver. |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 10208 | static const SHA256HashValue kOCSPTestCertSPKI = {{ |
| 10209 | 0x05, 0xa8, 0xf6, 0xfd, 0x8e, 0x10, 0xfe, 0x92, 0x2f, 0x22, 0x75, |
| 10210 | 0x46, 0x40, 0xf4, 0xc4, 0x57, 0x06, 0x0d, 0x95, 0xfd, 0x60, 0x31, |
| 10211 | 0x3b, 0xf3, 0xfc, 0x12, 0x47, 0xe7, 0x66, 0x1a, 0x82, 0xa3, |
| 10212 | }}; |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 10213 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10214 | // This is the policy OID contained in the certificates that testserver |
| 10215 | // generates. |
| 10216 | static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1"; |
| 10217 | |
| 10218 | class HTTPSOCSPTest : public HTTPSRequestTest { |
| 10219 | public: |
| 10220 | HTTPSOCSPTest() |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10221 | : context_(true), |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 10222 | ev_test_policy_( |
| 10223 | new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(), |
| 10224 | kOCSPTestCertFingerprint, |
| 10225 | kOCSPTestCertPolicy)) { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10226 | } |
| 10227 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 10228 | void SetUp() override { |
Ryan Sleevi | 8a9c9c1 | 2018-05-09 02:36:23 | [diff] [blame] | 10229 | context_.SetCTPolicyEnforcer(std::make_unique<DefaultCTPolicyEnforcer>()); |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10230 | context_.Init(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10231 | |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 10232 | context_.cert_verifier()->SetConfig(GetCertVerifierConfig()); |
| 10233 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10234 | scoped_refptr<X509Certificate> root_cert = |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10235 | ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
dcheng | c2e01e8 | 2014-08-27 00:24:42 | [diff] [blame] | 10236 | CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert.get()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 10237 | test_root_.reset(new ScopedTestRoot(root_cert.get())); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10238 | |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 10239 | #if defined(OS_ANDROID) || defined(USE_BUILTIN_CERT_VERIFIER) |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 10240 | SetGlobalCertNetFetcherForTesting(net::CreateCertNetFetcher(&context_)); |
| 10241 | #endif |
| 10242 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 10243 | #if defined(USE_NSS_CERTS) |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10244 | SetURLRequestContextForNSSHttpIO(&context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10245 | #endif |
| 10246 | } |
| 10247 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10248 | void DoConnectionWithDelegate( |
| 10249 | const SpawnedTestServer::SSLOptions& ssl_options, |
| 10250 | TestDelegate* delegate, |
| 10251 | SSLInfo* out_ssl_info) { |
| 10252 | // Always overwrite |out_ssl_info|. |
| 10253 | out_ssl_info->Reset(); |
| 10254 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10255 | SpawnedTestServer test_server( |
| 10256 | SpawnedTestServer::TYPE_HTTPS, |
| 10257 | ssl_options, |
| 10258 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10259 | ASSERT_TRUE(test_server.Start()); |
| 10260 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10261 | delegate->set_allow_certificate_errors(true); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10262 | std::unique_ptr<URLRequest> r( |
| 10263 | context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
| 10264 | delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10265 | r->Start(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10266 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10267 | delegate->RunUntilComplete(); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10268 | EXPECT_EQ(1, delegate->response_started_count()); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10269 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10270 | *out_ssl_info = r->ssl_info(); |
| 10271 | } |
| 10272 | |
| 10273 | void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options, |
| 10274 | CertStatus* out_cert_status) { |
| 10275 | // Always overwrite |out_cert_status|. |
| 10276 | *out_cert_status = 0; |
| 10277 | |
| 10278 | TestDelegate d; |
| 10279 | SSLInfo ssl_info; |
| 10280 | ASSERT_NO_FATAL_FAILURE( |
| 10281 | DoConnectionWithDelegate(ssl_options, &d, &ssl_info)); |
| 10282 | |
| 10283 | *out_cert_status = ssl_info.cert_status; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10284 | } |
| 10285 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 10286 | ~HTTPSOCSPTest() override { |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 10287 | #if defined(OS_ANDROID) || defined(USE_BUILTIN_CERT_VERIFIER) |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 10288 | ShutdownGlobalCertNetFetcher(); |
| 10289 | #endif |
| 10290 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 10291 | #if defined(USE_NSS_CERTS) |
Matt Mueller | a17ffaf | 2018-03-28 20:08:18 | [diff] [blame] | 10292 | SetURLRequestContextForNSSHttpIO(nullptr); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10293 | #endif |
| 10294 | } |
| 10295 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10296 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 10297 | // GetCertVerifierConfig() configures the URLRequestContext that will be used |
| 10298 | // for making connections to the testserver. This can be overridden in test |
| 10299 | // subclasses for different behaviour. |
| 10300 | virtual CertVerifier::Config GetCertVerifierConfig() { |
| 10301 | CertVerifier::Config config; |
| 10302 | config.enable_rev_checking = true; |
| 10303 | return config; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10304 | } |
| 10305 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10306 | std::unique_ptr<ScopedTestRoot> test_root_; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 10307 | std::unique_ptr<TestSSLConfigService> ssl_config_service_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10308 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10309 | std::unique_ptr<ScopedTestEVPolicy> ev_test_policy_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10310 | }; |
| 10311 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10312 | static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() { |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 10313 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10314 | // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't |
| 10315 | // have that ability on other platforms. |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10316 | // TODO(eroman): Should this also be the return value for |
| 10317 | // CertVerifyProcBuiltin? |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10318 | return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 10319 | #else |
| 10320 | return 0; |
| 10321 | #endif |
| 10322 | } |
| 10323 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10324 | // SystemSupportsHardFailRevocationChecking returns true iff the current |
| 10325 | // operating system supports revocation checking and can distinguish between |
| 10326 | // situations where a given certificate lacks any revocation information (eg: |
| 10327 | // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when |
| 10328 | // revocation information cannot be obtained (eg: the CRL was unreachable). |
| 10329 | // If it does not, then tests which rely on 'hard fail' behaviour should be |
| 10330 | // skipped. |
| 10331 | static bool SystemSupportsHardFailRevocationChecking() { |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10332 | #if defined(OS_WIN) || defined(USE_NSS_CERTS) || \ |
| 10333 | defined(USE_BUILTIN_CERT_VERIFIER) |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10334 | return true; |
| 10335 | #else |
| 10336 | return false; |
| 10337 | #endif |
| 10338 | } |
| 10339 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10340 | // SystemUsesChromiumEVMetadata returns true iff the current operating system |
| 10341 | // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then |
| 10342 | // several tests are effected because our testing EV certificate won't be |
| 10343 | // recognised as EV. |
| 10344 | static bool SystemUsesChromiumEVMetadata() { |
Eric Roman | 5f8d9d2 | 2017-10-17 02:32:52 | [diff] [blame] | 10345 | #if defined(PLATFORM_USES_CHROMIUM_EV_METADATA) |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 10346 | return true; |
Eric Roman | 5f8d9d2 | 2017-10-17 02:32:52 | [diff] [blame] | 10347 | #else |
| 10348 | return false; |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 10349 | #endif |
| 10350 | } |
| 10351 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 10352 | // Returns the expected CertStatus for tests that expect an online revocation |
| 10353 | // check failure as a result of checking a test EV cert, which will not |
| 10354 | // actually trigger an online revocation check on some platforms. |
| 10355 | static CertStatus ExpectedCertStatusForFailedOnlineEVRevocationCheck() { |
| 10356 | if (SystemUsesChromiumEVMetadata()) { |
| 10357 | return ExpectedCertStatusForFailedOnlineRevocationCheck(); |
| 10358 | } else { |
| 10359 | // If SystemUsesChromiumEVMetadata is false, revocation checking will not |
| 10360 | // be enabled, and thus there will not be a revocation check to fail. |
| 10361 | return 0u; |
| 10362 | } |
| 10363 | } |
| 10364 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 10365 | static bool SystemSupportsOCSP() { |
Sergey Ulanov | ee7c8db | 2017-12-08 00:18:39 | [diff] [blame] | 10366 | #if defined(OS_ANDROID) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10367 | // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. |
| 10368 | return false; |
| 10369 | #else |
| 10370 | return true; |
| 10371 | #endif |
| 10372 | } |
| 10373 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10374 | static bool SystemSupportsOCSPStapling() { |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10375 | #if defined(USE_NSS_CERTS) || defined(OS_WIN) || \ |
| 10376 | defined(USE_BUILTIN_CERT_VERIFIER) |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10377 | return true; |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10378 | #else |
| 10379 | return false; |
| 10380 | #endif |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10381 | } |
| 10382 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10383 | TEST_F(HTTPSOCSPTest, Valid) { |
| 10384 | if (!SystemSupportsOCSP()) { |
| 10385 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10386 | return; |
| 10387 | } |
| 10388 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10389 | SpawnedTestServer::SSLOptions ssl_options( |
| 10390 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10391 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10392 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 10393 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 10394 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10395 | |
| 10396 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10397 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10398 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 10399 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10400 | |
| 10401 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10402 | } |
| 10403 | |
| 10404 | TEST_F(HTTPSOCSPTest, Revoked) { |
| 10405 | if (!SystemSupportsOCSP()) { |
| 10406 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10407 | return; |
| 10408 | } |
| 10409 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10410 | SpawnedTestServer::SSLOptions ssl_options( |
| 10411 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10412 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10413 | |
| 10414 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 10415 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10416 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10417 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10418 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 10419 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10420 | } |
| 10421 | |
| 10422 | TEST_F(HTTPSOCSPTest, Invalid) { |
| 10423 | if (!SystemSupportsOCSP()) { |
| 10424 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10425 | return; |
| 10426 | } |
| 10427 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10428 | SpawnedTestServer::SSLOptions ssl_options( |
| 10429 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10430 | ssl_options.ocsp_status = |
| 10431 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10432 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 10433 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 10434 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10435 | |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10436 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 10437 | // TODO(649017): This test uses soft-fail revocation checking, but returns an |
| 10438 | // invalid OCSP response (can't parse). CertVerifyProcBuiltin currently |
| 10439 | // doesn't consider this a candidate for soft-fail (only considers |
| 10440 | // network-level failures as skippable). |
| 10441 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 10442 | cert_status & CERT_STATUS_UNABLE_TO_CHECK_REVOCATION); |
| 10443 | #else |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10444 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10445 | cert_status & CERT_STATUS_ALL_ERRORS); |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10446 | #endif |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10447 | |
| 10448 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 10449 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 10450 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10451 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10452 | |
Matt Mueller | 5339d86 | 2018-05-02 18:53:57 | [diff] [blame] | 10453 | TEST_F(HTTPSOCSPTest, IntermediateValid) { |
| 10454 | if (!SystemSupportsOCSP()) { |
| 10455 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10456 | return; |
| 10457 | } |
| 10458 | |
| 10459 | SpawnedTestServer::SSLOptions ssl_options( |
| 10460 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 10461 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 10462 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 10463 | |
| 10464 | CertStatus cert_status; |
| 10465 | DoConnection(ssl_options, &cert_status); |
| 10466 | |
| 10467 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10468 | |
| 10469 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 10470 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 10471 | |
| 10472 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10473 | } |
| 10474 | |
| 10475 | TEST_F(HTTPSOCSPTest, IntermediateResponseOldButStillValid) { |
| 10476 | if (!SystemSupportsOCSP()) { |
| 10477 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10478 | return; |
| 10479 | } |
| 10480 | |
| 10481 | SpawnedTestServer::SSLOptions ssl_options( |
| 10482 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 10483 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 10484 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 10485 | // Use an OCSP response for the intermediate that would be too old for a leaf |
| 10486 | // cert, but is still valid for an intermediate. |
| 10487 | ssl_options.ocsp_intermediate_date = |
| 10488 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG; |
| 10489 | |
| 10490 | CertStatus cert_status; |
| 10491 | DoConnection(ssl_options, &cert_status); |
| 10492 | |
| 10493 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10494 | |
| 10495 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 10496 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 10497 | |
| 10498 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10499 | } |
| 10500 | |
| 10501 | TEST_F(HTTPSOCSPTest, IntermediateResponseTooOld) { |
| 10502 | if (!SystemSupportsOCSP()) { |
| 10503 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10504 | return; |
| 10505 | } |
| 10506 | |
| 10507 | SpawnedTestServer::SSLOptions ssl_options( |
| 10508 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 10509 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 10510 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 10511 | ssl_options.ocsp_intermediate_date = |
| 10512 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONGER; |
| 10513 | |
| 10514 | CertStatus cert_status; |
| 10515 | DoConnection(ssl_options, &cert_status); |
| 10516 | |
| 10517 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 10518 | // The builtin verifier enforces the baseline requirements for max age of an |
| 10519 | // intermediate's OCSP response. |
| 10520 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 10521 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 10522 | EXPECT_EQ(0u, cert_status & CERT_STATUS_IS_EV); |
| 10523 | #else |
| 10524 | // The platform verifiers are more lenient. |
| 10525 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10526 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 10527 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 10528 | #endif |
| 10529 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10530 | } |
| 10531 | |
| 10532 | TEST_F(HTTPSOCSPTest, IntermediateRevoked) { |
| 10533 | if (!SystemSupportsOCSP()) { |
| 10534 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10535 | return; |
| 10536 | } |
| 10537 | |
| 10538 | SpawnedTestServer::SSLOptions ssl_options( |
| 10539 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 10540 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 10541 | ssl_options.ocsp_intermediate_status = |
| 10542 | SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 10543 | |
| 10544 | CertStatus cert_status; |
| 10545 | DoConnection(ssl_options, &cert_status); |
| 10546 | |
| 10547 | #if defined(OS_WIN) |
| 10548 | // TODO(mattm): why does CertVerifyProcWin accept this? |
| 10549 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10550 | #else |
| 10551 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10552 | #endif |
| 10553 | EXPECT_EQ(0u, cert_status & CERT_STATUS_IS_EV); |
| 10554 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10555 | } |
| 10556 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10557 | TEST_F(HTTPSOCSPTest, ValidStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10558 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10559 | LOG(WARNING) |
| 10560 | << "Skipping test because system doesn't support OCSP stapling"; |
| 10561 | return; |
| 10562 | } |
| 10563 | |
| 10564 | SpawnedTestServer::SSLOptions ssl_options( |
| 10565 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10566 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 10567 | ssl_options.staple_ocsp_response = true; |
| 10568 | ssl_options.ocsp_server_unavailable = true; |
| 10569 | |
| 10570 | CertStatus cert_status; |
| 10571 | DoConnection(ssl_options, &cert_status); |
| 10572 | |
| 10573 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10574 | |
| 10575 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 10576 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 10577 | |
| 10578 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10579 | } |
| 10580 | |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 10581 | // Disabled on NSS ports. See https://crbug.com/431716. |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 10582 | #if defined(USE_NSS_CERTS) |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 10583 | #define MAYBE_RevokedStapled DISABLED_RevokedStapled |
| 10584 | #else |
| 10585 | #define MAYBE_RevokedStapled RevokedStapled |
| 10586 | #endif |
| 10587 | TEST_F(HTTPSOCSPTest, MAYBE_RevokedStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 10588 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 10589 | LOG(WARNING) |
| 10590 | << "Skipping test because system doesn't support OCSP stapling"; |
| 10591 | return; |
| 10592 | } |
| 10593 | |
| 10594 | SpawnedTestServer::SSLOptions ssl_options( |
| 10595 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10596 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 10597 | ssl_options.staple_ocsp_response = true; |
| 10598 | ssl_options.ocsp_server_unavailable = true; |
| 10599 | |
| 10600 | CertStatus cert_status; |
| 10601 | DoConnection(ssl_options, &cert_status); |
| 10602 | |
| 10603 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10604 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 10605 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10606 | } |
| 10607 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10608 | static const struct OCSPVerifyTestData { |
| 10609 | std::vector<SpawnedTestServer::SSLOptions::OCSPSingleResponse> ocsp_responses; |
| 10610 | SpawnedTestServer::SSLOptions::OCSPProduced ocsp_produced; |
| 10611 | OCSPVerifyResult::ResponseStatus response_status; |
| 10612 | bool has_revocation_status; |
| 10613 | OCSPRevocationStatus cert_status; |
| 10614 | } kOCSPVerifyData[] = { |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10615 | // 0 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10616 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10617 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10618 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10619 | OCSPVerifyResult::PROVIDED, |
| 10620 | true, |
| 10621 | OCSPRevocationStatus::GOOD}, |
| 10622 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10623 | // 1 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10624 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10625 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 10626 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10627 | OCSPVerifyResult::INVALID_DATE, |
| 10628 | false, |
| 10629 | OCSPRevocationStatus::UNKNOWN}, |
| 10630 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10631 | // 2 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10632 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10633 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 10634 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10635 | OCSPVerifyResult::INVALID_DATE, |
| 10636 | false, |
| 10637 | OCSPRevocationStatus::UNKNOWN}, |
| 10638 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10639 | // 3 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10640 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10641 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 10642 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10643 | OCSPVerifyResult::INVALID_DATE, |
| 10644 | false, |
| 10645 | OCSPRevocationStatus::UNKNOWN}, |
| 10646 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10647 | // 4 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10648 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10649 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 10650 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10651 | OCSPVerifyResult::INVALID_DATE, |
| 10652 | false, |
| 10653 | OCSPRevocationStatus::UNKNOWN}, |
| 10654 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10655 | // 5 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10656 | {{{SpawnedTestServer::SSLOptions::OCSP_TRY_LATER, |
| 10657 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10658 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10659 | OCSPVerifyResult::ERROR_RESPONSE, |
| 10660 | false, |
| 10661 | OCSPRevocationStatus::UNKNOWN}, |
| 10662 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10663 | // 6 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10664 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE, |
| 10665 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10666 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10667 | OCSPVerifyResult::PARSE_RESPONSE_ERROR, |
| 10668 | false, |
| 10669 | OCSPRevocationStatus::UNKNOWN}, |
| 10670 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10671 | // 7 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10672 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE_DATA, |
| 10673 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10674 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10675 | OCSPVerifyResult::PARSE_RESPONSE_DATA_ERROR, |
| 10676 | false, |
| 10677 | OCSPRevocationStatus::UNKNOWN}, |
| 10678 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10679 | // 8 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10680 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 10681 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 10682 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10683 | OCSPVerifyResult::INVALID_DATE, |
| 10684 | false, |
| 10685 | OCSPRevocationStatus::UNKNOWN}, |
| 10686 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10687 | // 9 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10688 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 10689 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10690 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10691 | OCSPVerifyResult::PROVIDED, |
| 10692 | true, |
| 10693 | OCSPRevocationStatus::UNKNOWN}, |
| 10694 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10695 | // 10 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10696 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 10697 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 10698 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10699 | OCSPVerifyResult::INVALID_DATE, |
| 10700 | false, |
| 10701 | OCSPRevocationStatus::UNKNOWN}, |
| 10702 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10703 | // 11 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10704 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 10705 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 10706 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10707 | OCSPVerifyResult::INVALID_DATE, |
| 10708 | false, |
| 10709 | OCSPRevocationStatus::UNKNOWN}, |
| 10710 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10711 | // 12 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10712 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10713 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10714 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_BEFORE_CERT, |
| 10715 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 10716 | false, |
| 10717 | OCSPRevocationStatus::UNKNOWN}, |
| 10718 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10719 | // 13 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10720 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10721 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10722 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 10723 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 10724 | false, |
| 10725 | OCSPRevocationStatus::UNKNOWN}, |
| 10726 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10727 | // 14 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10728 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10729 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10730 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 10731 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 10732 | false, |
| 10733 | OCSPRevocationStatus::UNKNOWN}, |
| 10734 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10735 | // 15 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10736 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10737 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10738 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10739 | OCSPVerifyResult::PROVIDED, |
| 10740 | true, |
| 10741 | OCSPRevocationStatus::GOOD}, |
| 10742 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10743 | // 16 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10744 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10745 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 10746 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10747 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10748 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10749 | OCSPVerifyResult::PROVIDED, |
| 10750 | true, |
| 10751 | OCSPRevocationStatus::GOOD}, |
| 10752 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10753 | // 17 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10754 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10755 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 10756 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10757 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10758 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10759 | OCSPVerifyResult::PROVIDED, |
| 10760 | true, |
| 10761 | OCSPRevocationStatus::GOOD}, |
| 10762 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10763 | // 18 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10764 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10765 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 10766 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10767 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10768 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10769 | OCSPVerifyResult::PROVIDED, |
| 10770 | true, |
| 10771 | OCSPRevocationStatus::GOOD}, |
| 10772 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10773 | // 19 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10774 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10775 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 10776 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10777 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 10778 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10779 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 10780 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10781 | OCSPVerifyResult::INVALID_DATE, |
| 10782 | false, |
| 10783 | OCSPRevocationStatus::UNKNOWN}, |
| 10784 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10785 | // 20 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10786 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 10787 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 10788 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 10789 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 10790 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10791 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10792 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10793 | OCSPVerifyResult::PROVIDED, |
| 10794 | true, |
| 10795 | OCSPRevocationStatus::REVOKED}, |
| 10796 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10797 | // 21 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10798 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 10799 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 10800 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10801 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10802 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10803 | OCSPVerifyResult::PROVIDED, |
| 10804 | true, |
| 10805 | OCSPRevocationStatus::UNKNOWN}, |
| 10806 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10807 | // 22 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10808 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 10809 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 10810 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 10811 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 10812 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 10813 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10814 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10815 | OCSPVerifyResult::PROVIDED, |
| 10816 | true, |
| 10817 | OCSPRevocationStatus::UNKNOWN}, |
| 10818 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10819 | // 23 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10820 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 10821 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10822 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10823 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 10824 | false, |
| 10825 | OCSPRevocationStatus::UNKNOWN}, |
| 10826 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10827 | // 24 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10828 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 10829 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 10830 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10831 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 10832 | false, |
| 10833 | OCSPRevocationStatus::UNKNOWN}, |
| 10834 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 10835 | // These tests fail when using NSS for certificate verification, as NSS fails |
| 10836 | // and doesn't return the partial path. As a result the OCSP checks being done |
| 10837 | // at the CertVerifyProc layer cannot access the issuer certificate. |
| 10838 | #if !defined(USE_NSS_CERTS) |
| 10839 | // 25 |
| 10840 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 10841 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 10842 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10843 | OCSPVerifyResult::PROVIDED, |
| 10844 | true, |
| 10845 | OCSPRevocationStatus::REVOKED}, |
| 10846 | |
| 10847 | // 26 |
| 10848 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 10849 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 10850 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10851 | OCSPVerifyResult::INVALID_DATE, |
| 10852 | false, |
| 10853 | OCSPRevocationStatus::UNKNOWN}, |
| 10854 | |
| 10855 | // 27 |
| 10856 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 10857 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 10858 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 10859 | OCSPVerifyResult::INVALID_DATE, |
| 10860 | false, |
| 10861 | OCSPRevocationStatus::UNKNOWN}, |
| 10862 | #endif |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10863 | }; |
| 10864 | |
| 10865 | class HTTPSOCSPVerifyTest |
| 10866 | : public HTTPSOCSPTest, |
| 10867 | public testing::WithParamInterface<OCSPVerifyTestData> {}; |
| 10868 | |
| 10869 | TEST_P(HTTPSOCSPVerifyTest, VerifyResult) { |
| 10870 | SpawnedTestServer::SSLOptions ssl_options( |
| 10871 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 10872 | OCSPVerifyTestData test = GetParam(); |
| 10873 | |
| 10874 | ssl_options.ocsp_responses = test.ocsp_responses; |
| 10875 | ssl_options.ocsp_produced = test.ocsp_produced; |
| 10876 | ssl_options.staple_ocsp_response = true; |
| 10877 | |
| 10878 | SSLInfo ssl_info; |
| 10879 | OCSPErrorTestDelegate delegate; |
| 10880 | ASSERT_NO_FATAL_FAILURE( |
| 10881 | DoConnectionWithDelegate(ssl_options, &delegate, &ssl_info)); |
| 10882 | |
| 10883 | // The SSLInfo must be extracted from |delegate| on error, due to how |
| 10884 | // URLRequest caches certificate errors. |
| 10885 | if (delegate.have_certificate_errors()) { |
| 10886 | ASSERT_TRUE(delegate.on_ssl_certificate_error_called()); |
| 10887 | ssl_info = delegate.ssl_info(); |
| 10888 | } |
| 10889 | |
| 10890 | EXPECT_EQ(test.response_status, ssl_info.ocsp_result.response_status); |
| 10891 | |
| 10892 | if (test.has_revocation_status) |
| 10893 | EXPECT_EQ(test.cert_status, ssl_info.ocsp_result.revocation_status); |
| 10894 | } |
| 10895 | |
| 10896 | INSTANTIATE_TEST_CASE_P(OCSPVerify, |
| 10897 | HTTPSOCSPVerifyTest, |
| 10898 | testing::ValuesIn(kOCSPVerifyData)); |
| 10899 | |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 10900 | class HTTPSAIATest : public HTTPSOCSPTest { |
| 10901 | public: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 10902 | CertVerifier::Config GetCertVerifierConfig() override { |
| 10903 | CertVerifier::Config config; |
| 10904 | return config; |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 10905 | } |
| 10906 | }; |
| 10907 | |
| 10908 | TEST_F(HTTPSAIATest, AIAFetching) { |
| 10909 | SpawnedTestServer::SSLOptions ssl_options( |
| 10910 | SpawnedTestServer::SSLOptions::CERT_AUTO_AIA_INTERMEDIATE); |
| 10911 | SpawnedTestServer test_server( |
| 10912 | SpawnedTestServer::TYPE_HTTPS, ssl_options, |
| 10913 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10914 | ASSERT_TRUE(test_server.Start()); |
| 10915 | |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 10916 | // Unmark the certificate's OID as EV, which will disable revocation |
| 10917 | // checking. |
| 10918 | ev_test_policy_.reset(); |
| 10919 | |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 10920 | TestDelegate d; |
| 10921 | d.set_allow_certificate_errors(true); |
| 10922 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10923 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10924 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 10925 | |
| 10926 | r->Start(); |
| 10927 | EXPECT_TRUE(r->is_pending()); |
| 10928 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10929 | d.RunUntilComplete(); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 10930 | |
| 10931 | EXPECT_EQ(1, d.response_started_count()); |
| 10932 | |
| 10933 | CertStatus cert_status = r->ssl_info().cert_status; |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 10934 | EXPECT_EQ(OK, d.request_status()); |
| 10935 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 10936 | ASSERT_TRUE(r->ssl_info().cert); |
| 10937 | EXPECT_EQ(2u, r->ssl_info().cert->intermediate_buffers().size()); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 10938 | ASSERT_TRUE(r->ssl_info().unverified_cert); |
Matt Mueller | a419327 | 2017-12-07 00:23:34 | [diff] [blame] | 10939 | EXPECT_EQ(0u, r->ssl_info().unverified_cert->intermediate_buffers().size()); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 10940 | } |
| 10941 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10942 | class HTTPSHardFailTest : public HTTPSOCSPTest { |
| 10943 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 10944 | CertVerifier::Config GetCertVerifierConfig() override { |
| 10945 | CertVerifier::Config config; |
| 10946 | config.require_rev_checking_local_anchors = true; |
| 10947 | return config; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10948 | } |
| 10949 | }; |
| 10950 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10951 | TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) { |
| 10952 | if (!SystemSupportsOCSP()) { |
| 10953 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10954 | return; |
| 10955 | } |
| 10956 | |
| 10957 | if (!SystemSupportsHardFailRevocationChecking()) { |
| 10958 | LOG(WARNING) << "Skipping test because system doesn't support hard fail " |
| 10959 | << "revocation checking"; |
| 10960 | return; |
| 10961 | } |
| 10962 | |
| 10963 | SpawnedTestServer::SSLOptions ssl_options( |
| 10964 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10965 | ssl_options.ocsp_status = |
| 10966 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10967 | |
| 10968 | CertStatus cert_status; |
| 10969 | DoConnection(ssl_options, &cert_status); |
| 10970 | |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10971 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 10972 | // TODO(crbug.com/649017): Should we consider invalid response as |
| 10973 | // affirmatively revoked? |
| 10974 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 10975 | cert_status & CERT_STATUS_UNABLE_TO_CHECK_REVOCATION); |
| 10976 | #else |
| 10977 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_REVOKED); |
| 10978 | #endif |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10979 | |
| 10980 | // Without a positive OCSP response, we shouldn't show the EV status. |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10981 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10982 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 10983 | } |
| 10984 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10985 | class HTTPSEVCRLSetTest : public HTTPSOCSPTest { |
| 10986 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 10987 | CertVerifier::Config GetCertVerifierConfig() override { |
| 10988 | CertVerifier::Config config; |
| 10989 | return config; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10990 | } |
| 10991 | }; |
| 10992 | |
| 10993 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) { |
| 10994 | if (!SystemSupportsOCSP()) { |
| 10995 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 10996 | return; |
| 10997 | } |
| 10998 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10999 | SpawnedTestServer::SSLOptions ssl_options( |
| 11000 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11001 | ssl_options.ocsp_status = |
| 11002 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11003 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11004 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11005 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11006 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11007 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11008 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11009 | |
| 11010 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11011 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11012 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11013 | } |
| 11014 | |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11015 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) { |
| 11016 | if (!SystemSupportsOCSP()) { |
| 11017 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11018 | return; |
| 11019 | } |
| 11020 | |
| 11021 | SpawnedTestServer::SSLOptions ssl_options( |
| 11022 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11023 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11024 | |
| 11025 | CertStatus cert_status; |
| 11026 | DoConnection(ssl_options, &cert_status); |
| 11027 | |
mattm | 1a282f5 | 2016-11-10 21:49:42 | [diff] [blame] | 11028 | // Currently only works for Windows and OS X. When using NSS, it's not |
| 11029 | // possible to determine whether the check failed because of actual |
| 11030 | // revocation or because there was an OCSP failure. |
| 11031 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11032 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11033 | #else |
| 11034 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11035 | #endif |
| 11036 | |
| 11037 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11038 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11039 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 11040 | } |
| 11041 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11042 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) { |
| 11043 | if (!SystemSupportsOCSP()) { |
| 11044 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11045 | return; |
| 11046 | } |
| 11047 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11048 | SpawnedTestServer::SSLOptions ssl_options( |
| 11049 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11050 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11051 | |
| 11052 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11053 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11054 | |
| 11055 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11056 | |
| 11057 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11058 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11059 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11060 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11061 | } |
| 11062 | |
| 11063 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) { |
| 11064 | if (!SystemSupportsOCSP()) { |
| 11065 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11066 | return; |
| 11067 | } |
| 11068 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11069 | SpawnedTestServer::SSLOptions ssl_options( |
| 11070 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11071 | ssl_options.ocsp_status = |
| 11072 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11073 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11074 | cert_verifier_config.crl_set = CRLSet::ExpiredCRLSetForTesting(); |
| 11075 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11076 | |
| 11077 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11078 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11079 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11080 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11081 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11082 | |
| 11083 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11084 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11085 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11086 | } |
| 11087 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11088 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) { |
| 11089 | if (!SystemSupportsOCSP()) { |
| 11090 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11091 | return; |
| 11092 | } |
| 11093 | |
| 11094 | SpawnedTestServer::SSLOptions ssl_options( |
| 11095 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11096 | ssl_options.ocsp_status = |
| 11097 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11098 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11099 | cert_verifier_config.crl_set = |
| 11100 | CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "", "", {}); |
| 11101 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11102 | |
| 11103 | CertStatus cert_status; |
| 11104 | DoConnection(ssl_options, &cert_status); |
| 11105 | |
| 11106 | // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a |
| 11107 | // revocation check for EV. |
| 11108 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11109 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11110 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11111 | EXPECT_FALSE( |
| 11112 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 11113 | } |
| 11114 | |
| 11115 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) { |
| 11116 | if (!SystemSupportsOCSP()) { |
| 11117 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11118 | return; |
| 11119 | } |
| 11120 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11121 | SpawnedTestServer::SSLOptions ssl_options( |
| 11122 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11123 | ssl_options.ocsp_status = |
| 11124 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11125 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11126 | cert_verifier_config.crl_set = CRLSet::EmptyCRLSetForTesting(); |
| 11127 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11128 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11129 | CertStatus cert_status = 0; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11130 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11131 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11132 | // Even with a fresh CRLSet, we should still do online revocation checks when |
| 11133 | // the certificate chain isn't covered by the CRLSet, which it isn't in this |
| 11134 | // test. |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11135 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11136 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11137 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11138 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11139 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11140 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11141 | } |
| 11142 | |
| 11143 | class HTTPSCRLSetTest : public HTTPSOCSPTest { |
| 11144 | protected: |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 11145 | CertVerifier::Config GetCertVerifierConfig() override { |
| 11146 | CertVerifier::Config config; |
| 11147 | return config; |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11148 | } |
| 11149 | |
| 11150 | void SetUp() override { |
| 11151 | HTTPSOCSPTest::SetUp(); |
| 11152 | |
| 11153 | // Unmark the certificate's OID as EV, which should disable revocation |
| 11154 | // checking (as per the user preference). |
| 11155 | ev_test_policy_.reset(); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11156 | } |
| 11157 | }; |
| 11158 | |
| 11159 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11160 | SpawnedTestServer::SSLOptions ssl_options( |
| 11161 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11162 | ssl_options.ocsp_status = |
| 11163 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11164 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11165 | cert_verifier_config.crl_set = CRLSet::ExpiredCRLSetForTesting(); |
| 11166 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11167 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11168 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11169 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11170 | |
| 11171 | // If we're not trying EV verification then, even if the CRLSet has expired, |
| 11172 | // we don't fall back to online revocation checks. |
| 11173 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11174 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11175 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11176 | } |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11177 | |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11178 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSetAndRevoked) { |
| 11179 | // Test that when online revocation checking is disabled, and the leaf |
| 11180 | // certificate is not EV, that no revocation checking actually happens. |
| 11181 | if (!SystemSupportsOCSP()) { |
| 11182 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11183 | return; |
| 11184 | } |
| 11185 | |
| 11186 | SpawnedTestServer::SSLOptions ssl_options( |
| 11187 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11188 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11189 | |
| 11190 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11191 | cert_verifier_config.crl_set = CRLSet::ExpiredCRLSetForTesting(); |
| 11192 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11193 | |
| 11194 | CertStatus cert_status; |
| 11195 | DoConnection(ssl_options, &cert_status); |
| 11196 | |
| 11197 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11198 | |
| 11199 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11200 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11201 | } |
| 11202 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11203 | TEST_F(HTTPSCRLSetTest, CRLSetRevoked) { |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 11204 | #if defined(OS_ANDROID) |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11205 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 11206 | return; |
| 11207 | #endif |
| 11208 | |
| 11209 | SpawnedTestServer::SSLOptions ssl_options( |
| 11210 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11211 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11212 | ssl_options.cert_serial = 10; |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11213 | |
| 11214 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11215 | cert_verifier_config.crl_set = |
| 11216 | CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "\x0a", "", {}); |
| 11217 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11218 | |
| 11219 | CertStatus cert_status = 0; |
| 11220 | DoConnection(ssl_options, &cert_status); |
| 11221 | |
| 11222 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 11223 | // reflected without online revocation checking. |
| 11224 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11225 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11226 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11227 | } |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 11228 | |
| 11229 | TEST_F(HTTPSCRLSetTest, CRLSetRevokedBySubject) { |
| 11230 | #if defined(OS_ANDROID) |
| 11231 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 11232 | return; |
| 11233 | #endif |
| 11234 | |
| 11235 | SpawnedTestServer::SSLOptions ssl_options( |
| 11236 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11237 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11238 | static const char kCommonName[] = "Test CN"; |
| 11239 | ssl_options.cert_common_name = kCommonName; |
| 11240 | |
| 11241 | { |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11242 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11243 | cert_verifier_config.crl_set = |
| 11244 | CRLSet::ForTesting(false, nullptr, "", kCommonName, {}); |
| 11245 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 11246 | |
| 11247 | CertStatus cert_status = 0; |
| 11248 | DoConnection(ssl_options, &cert_status); |
| 11249 | |
| 11250 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 11251 | // reflected without online revocation checking. |
| 11252 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11253 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11254 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 11255 | } |
| 11256 | |
| 11257 | const uint8_t kTestServerSPKISHA256[32] = { |
| 11258 | 0xb3, 0x91, 0xac, 0x73, 0x32, 0x54, 0x7f, 0x7b, 0x8a, 0x62, 0x77, |
| 11259 | 0x73, 0x1d, 0x45, 0x7b, 0x23, 0x46, 0x69, 0xef, 0x6f, 0x05, 0x3d, |
| 11260 | 0x07, 0x22, 0x15, 0x18, 0xd6, 0x10, 0x8b, 0xa1, 0x49, 0x33, |
| 11261 | }; |
| 11262 | const std::string spki_hash( |
| 11263 | reinterpret_cast<const char*>(kTestServerSPKISHA256), |
| 11264 | sizeof(kTestServerSPKISHA256)); |
| 11265 | |
| 11266 | { |
Ryan Sleevi | b369d71 | 2018-08-20 16:43:22 | [diff] [blame] | 11267 | CertVerifier::Config cert_verifier_config = GetCertVerifierConfig(); |
| 11268 | cert_verifier_config.crl_set = |
| 11269 | CRLSet::ForTesting(false, nullptr, "", kCommonName, {spki_hash}); |
| 11270 | context_.cert_verifier()->SetConfig(cert_verifier_config); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 11271 | |
| 11272 | CertStatus cert_status = 0; |
| 11273 | DoConnection(ssl_options, &cert_status); |
| 11274 | |
| 11275 | // When the correct SPKI hash is specified, the connection should succeed |
| 11276 | // even though the subject is listed in the CRLSet. |
| 11277 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11278 | } |
| 11279 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 11280 | #endif // !defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11281 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 11282 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 11283 | !defined(OS_FUCHSIA) |
| 11284 | // FTP uses a second TCP connection with the port number allocated dynamically |
| 11285 | // on the server side, so it would be hard to make RemoteTestServer proxy FTP |
| 11286 | // connections reliably. FTP tests are disabled on platforms that use |
| 11287 | // RemoteTestServer. See http://crbug.com/495220 |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 11288 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11289 | public: |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 11290 | URLRequestTestFTP() |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 11291 | : ftp_test_server_(SpawnedTestServer::TYPE_FTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11292 | base::FilePath(kTestFilePath)) { |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11293 | // Can't use |default_context_|'s HostResolver to set up the |
| 11294 | // FTPTransactionFactory because it hasn't been created yet. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11295 | default_context().set_host_resolver(&host_resolver_); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11296 | } |
| 11297 | |
| 11298 | // URLRequestTest interface: |
| 11299 | void SetUpFactory() override { |
| 11300 | // Add FTP support to the default URLRequestContext. |
| 11301 | job_factory_impl_->SetProtocolHandler( |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 11302 | "ftp", FtpProtocolHandler::Create(&host_resolver_)); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11303 | } |
| 11304 | |
| 11305 | std::string GetTestFileContents() { |
| 11306 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 11307 | EXPECT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &path)); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11308 | path = path.Append(kTestFilePath); |
| 11309 | path = path.AppendASCII(kFtpTestFile); |
| 11310 | std::string contents; |
| 11311 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
| 11312 | return contents; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11313 | } |
| 11314 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 11315 | protected: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11316 | MockHostResolver host_resolver_; |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11317 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11318 | SpawnedTestServer ftp_test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 11319 | }; |
| 11320 | |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11321 | // Make sure an FTP request using an unsafe ports fails. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 11322 | TEST_F(URLRequestTestFTP, UnsafePort) { |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11323 | GURL url("ftp://127.0.0.1:7"); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11324 | |
| 11325 | TestDelegate d; |
| 11326 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11327 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11328 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11329 | r->Start(); |
| 11330 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11331 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11332 | d.RunUntilComplete(); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11333 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11334 | EXPECT_FALSE(r->is_pending()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 11335 | EXPECT_EQ(ERR_UNSAFE_PORT, d.request_status()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 11336 | } |
| 11337 | } |
| 11338 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11339 | TEST_F(URLRequestTestFTP, FTPDirectoryListing) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11340 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11341 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11342 | TestDelegate d; |
| 11343 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11344 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11345 | ftp_test_server_.GetURL("/"), DEFAULT_PRIORITY, &d, |
| 11346 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11347 | r->Start(); |
| 11348 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11349 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11350 | d.RunUntilComplete(); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11351 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11352 | EXPECT_FALSE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11353 | EXPECT_EQ(1, d.response_started_count()); |
| 11354 | EXPECT_FALSE(d.received_data_before_response()); |
| 11355 | EXPECT_LT(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11356 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11357 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11358 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11359 | r->GetSocketAddress().port()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 11360 | } |
| 11361 | } |
| 11362 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11363 | TEST_F(URLRequestTestFTP, FTPGetTestAnonymous) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11364 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11365 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11366 | TestDelegate d; |
| 11367 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11368 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11369 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, &d, |
| 11370 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11371 | r->Start(); |
| 11372 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11373 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11374 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11375 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11376 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11377 | EXPECT_EQ(1, d.response_started_count()); |
| 11378 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11379 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11380 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11381 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11382 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11383 | r->GetSocketAddress().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11384 | } |
| 11385 | } |
| 11386 | |
Mike West | 80462a1 | 2018-11-27 16:05:06 | [diff] [blame] | 11387 | TEST_F(URLRequestTestFTP, FTPMimeType) { |
| 11388 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 11389 | |
| 11390 | struct { |
| 11391 | const char* path; |
| 11392 | const char* mime; |
| 11393 | } test_cases[] = { |
| 11394 | {"/", "text/vnd.chromium.ftp-dir"}, |
| 11395 | {kFtpTestFile, "application/octet-stream"}, |
| 11396 | }; |
| 11397 | |
| 11398 | for (const auto test : test_cases) { |
| 11399 | TestDelegate d; |
| 11400 | |
| 11401 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
| 11402 | ftp_test_server_.GetURL(test.path), DEFAULT_PRIORITY, &d, |
| 11403 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11404 | r->Start(); |
| 11405 | EXPECT_TRUE(r->is_pending()); |
| 11406 | |
| 11407 | d.RunUntilComplete(); |
| 11408 | |
| 11409 | std::string mime; |
| 11410 | r->GetMimeType(&mime); |
| 11411 | EXPECT_EQ(test.mime, mime); |
| 11412 | } |
| 11413 | } |
| 11414 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11415 | TEST_F(URLRequestTestFTP, FTPGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11416 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11417 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11418 | TestDelegate d; |
| 11419 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11420 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11421 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11422 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11423 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11424 | r->Start(); |
| 11425 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11426 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11427 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11428 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11429 | EXPECT_FALSE(r->is_pending()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11430 | EXPECT_EQ(1, d.response_started_count()); |
| 11431 | EXPECT_FALSE(d.received_data_before_response()); |
| 11432 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11433 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11434 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11435 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11436 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 11437 | |
| 11438 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11439 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 11440 | TestLoadTimingNoHttpResponse(load_timing_info); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11441 | } |
| 11442 | } |
| 11443 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11444 | TEST_F(URLRequestTestFTP, FTPCheckWrongPassword) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11445 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11446 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11447 | TestDelegate d; |
| 11448 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11449 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11450 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11451 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11452 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11453 | r->Start(); |
| 11454 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11455 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11456 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11457 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11458 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11459 | EXPECT_EQ(1, d.response_started_count()); |
| 11460 | EXPECT_FALSE(d.received_data_before_response()); |
| 11461 | EXPECT_EQ(d.bytes_received(), 0); |
| 11462 | } |
| 11463 | } |
| 11464 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11465 | TEST_F(URLRequestTestFTP, FTPCheckWrongPasswordRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11466 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11467 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11468 | TestDelegate d; |
| 11469 | // Set correct login credentials. The delegate will be asked for them when |
| 11470 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 11471 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11472 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11473 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11474 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11475 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11476 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11477 | r->Start(); |
| 11478 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11479 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11480 | d.RunUntilComplete(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11481 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11482 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11483 | EXPECT_EQ(1, d.response_started_count()); |
| 11484 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11485 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11486 | } |
| 11487 | } |
| 11488 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11489 | TEST_F(URLRequestTestFTP, FTPCheckWrongUser) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11490 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11491 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11492 | TestDelegate d; |
| 11493 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11494 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11495 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 11496 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11497 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11498 | r->Start(); |
| 11499 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11500 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11501 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11502 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11503 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11504 | EXPECT_EQ(1, d.response_started_count()); |
| 11505 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11506 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 11507 | } |
| 11508 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11509 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11510 | TEST_F(URLRequestTestFTP, FTPCheckWrongUserRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11511 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11512 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11513 | TestDelegate d; |
| 11514 | // Set correct login credentials. The delegate will be asked for them when |
| 11515 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 11516 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11517 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11518 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11519 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 11520 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11521 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11522 | r->Start(); |
| 11523 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11524 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11525 | d.RunUntilComplete(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11526 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11527 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11528 | EXPECT_EQ(1, d.response_started_count()); |
| 11529 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11530 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 11531 | } |
| 11532 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11533 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11534 | TEST_F(URLRequestTestFTP, FTPCacheURLCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11535 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11536 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11537 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11538 | { |
| 11539 | // Pass correct login identity in the URL. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11540 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11541 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11542 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11543 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11544 | r->Start(); |
| 11545 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11546 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11547 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11548 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11549 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11550 | EXPECT_EQ(1, d->response_started_count()); |
| 11551 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11552 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11553 | } |
| 11554 | |
| 11555 | d.reset(new TestDelegate); |
| 11556 | { |
| 11557 | // This request should use cached identity from previous request. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11558 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11559 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 11560 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11561 | r->Start(); |
| 11562 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11563 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11564 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11565 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11566 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11567 | EXPECT_EQ(1, d->response_started_count()); |
| 11568 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11569 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11570 | } |
| 11571 | } |
| 11572 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11573 | TEST_F(URLRequestTestFTP, FTPCacheLoginBoxCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11574 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 11575 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11576 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11577 | // Set correct login credentials. The delegate will be asked for them when |
| 11578 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 11579 | d->set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11580 | { |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11581 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 11582 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 11583 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11584 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11585 | r->Start(); |
| 11586 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11587 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11588 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11589 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11590 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11591 | EXPECT_EQ(1, d->response_started_count()); |
| 11592 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11593 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11594 | } |
| 11595 | |
| 11596 | // Use a new delegate without explicit credentials. The cached ones should be |
| 11597 | // used. |
| 11598 | d.reset(new TestDelegate); |
| 11599 | { |
| 11600 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 11601 | // ones. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11602 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11603 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 11604 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11605 | r->Start(); |
| 11606 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11607 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11608 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11609 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 11610 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11611 | EXPECT_EQ(1, d->response_started_count()); |
| 11612 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 11613 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 11614 | } |
| 11615 | } |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 11616 | |
| 11617 | TEST_F(URLRequestTestFTP, RawBodyBytes) { |
| 11618 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 11619 | |
| 11620 | TestDelegate d; |
| 11621 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11622 | ftp_test_server_.GetURL("simple.html"), DEFAULT_PRIORITY, &d, |
| 11623 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 11624 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11625 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 11626 | |
| 11627 | EXPECT_EQ(6, req->GetRawBodyBytes()); |
| 11628 | } |
| 11629 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 11630 | #endif // !BUILDFLAG(DISABLE_FTP_SUPPORT) |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 11631 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 11632 | TEST_F(URLRequestTest, NetworkAccessedClearOnDataRequest) { |
| 11633 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11634 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11635 | GURL("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 11636 | |
| 11637 | EXPECT_FALSE(req->response_info().network_accessed); |
| 11638 | |
| 11639 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11640 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 11641 | |
| 11642 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 11643 | EXPECT_FALSE(req->response_info().network_accessed); |
| 11644 | } |
| 11645 | |
| 11646 | TEST_F(URLRequestTest, NetworkAccessedSetOnHostResolutionFailure) { |
| 11647 | MockHostResolver host_resolver; |
| 11648 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 11649 | TestURLRequestContext context(true); |
| 11650 | context.set_network_delegate(&network_delegate); |
| 11651 | context.set_host_resolver(&host_resolver); |
| 11652 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 11653 | context.Init(); |
| 11654 | |
| 11655 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11656 | std::unique_ptr<URLRequest> req( |
| 11657 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 11658 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 11659 | |
| 11660 | EXPECT_FALSE(req->response_info().network_accessed); |
| 11661 | |
| 11662 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11663 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 11664 | EXPECT_TRUE(req->response_info().network_accessed); |
| 11665 | } |
| 11666 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 11667 | // Test that URLRequest is canceled correctly. |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11668 | // See http://crbug.com/508900 |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 11669 | TEST_F(URLRequestTest, URLRequestRedirectJobCancelRequest) { |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11670 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11671 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11672 | GURL("http://not-a-real-domain/"), DEFAULT_PRIORITY, &d, |
| 11673 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11674 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 11675 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11676 | req.get(), &default_network_delegate_, |
| 11677 | GURL("http://this-should-never-be-navigated-to/"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 11678 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Jumbo shrimp")); |
| 11679 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11680 | |
| 11681 | req->Start(); |
| 11682 | req->Cancel(); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11683 | base::RunLoop().RunUntilIdle(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 11684 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 11685 | EXPECT_EQ(0, d.received_redirect_count()); |
| 11686 | } |
| 11687 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11688 | TEST_F(URLRequestTestHTTP, HeadersCallbacks) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11689 | ASSERT_TRUE(http_test_server()->Start()); |
| 11690 | TestURLRequestContext context; |
| 11691 | GURL url(http_test_server()->GetURL("/cachetime")); |
| 11692 | TestDelegate delegate; |
| 11693 | HttpRequestHeaders extra_headers; |
| 11694 | extra_headers.SetHeader("X-Foo", "bar"); |
| 11695 | |
| 11696 | { |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11697 | HttpRawRequestHeaders raw_req_headers; |
| 11698 | scoped_refptr<const HttpResponseHeaders> raw_resp_headers; |
| 11699 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11700 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 11701 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11702 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11703 | r->SetRequestHeadersCallback(base::Bind( |
| 11704 | &HttpRawRequestHeaders::Assign, base::Unretained(&raw_req_headers))); |
| 11705 | r->SetResponseHeadersCallback(base::Bind( |
| 11706 | [](scoped_refptr<const HttpResponseHeaders>* left, |
| 11707 | scoped_refptr<const HttpResponseHeaders> right) { *left = right; }, |
| 11708 | base::Unretained(&raw_resp_headers))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11709 | r->Start(); |
| 11710 | while (!delegate.response_started_count()) |
| 11711 | base::RunLoop().RunUntilIdle(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11712 | EXPECT_FALSE(raw_req_headers.headers().empty()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11713 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11714 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11715 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11716 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11717 | EXPECT_EQ("gzip, deflate", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11718 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Connection", &value)); |
| 11719 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Host", &value)); |
| 11720 | EXPECT_EQ("GET /cachetime HTTP/1.1\r\n", raw_req_headers.request_line()); |
| 11721 | EXPECT_EQ(raw_resp_headers.get(), r->response_headers()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11722 | } |
| 11723 | { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11724 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 11725 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11726 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11727 | r->SetRequestHeadersCallback(base::Bind([](HttpRawRequestHeaders) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11728 | FAIL() << "Callback should not be called unless request is sent"; |
| 11729 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11730 | r->SetResponseHeadersCallback( |
| 11731 | base::Bind([](scoped_refptr<const HttpResponseHeaders>) { |
| 11732 | FAIL() << "Callback should not be called unless request is sent"; |
| 11733 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11734 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11735 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11736 | EXPECT_TRUE(r->was_cached()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11737 | } |
| 11738 | } |
| 11739 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11740 | TEST_F(URLRequestTestHTTP, HeadersCallbacksWithRedirect) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11741 | ASSERT_TRUE(http_test_server()->Start()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11742 | HttpRawRequestHeaders raw_req_headers; |
| 11743 | scoped_refptr<const HttpResponseHeaders> raw_resp_headers; |
| 11744 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11745 | TestURLRequestContext context; |
| 11746 | TestDelegate delegate; |
| 11747 | HttpRequestHeaders extra_headers; |
| 11748 | extra_headers.SetHeader("X-Foo", "bar"); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11749 | GURL url(http_test_server()->GetURL("/redirect-test.html")); |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11750 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11751 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11752 | r->SetExtraRequestHeaders(extra_headers); |
| 11753 | r->SetRequestHeadersCallback(base::Bind(&HttpRawRequestHeaders::Assign, |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11754 | base::Unretained(&raw_req_headers))); |
| 11755 | r->SetResponseHeadersCallback(base::Bind( |
| 11756 | [](scoped_refptr<const HttpResponseHeaders>* left, |
| 11757 | scoped_refptr<const HttpResponseHeaders> right) { *left = right; }, |
| 11758 | base::Unretained(&raw_resp_headers))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11759 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 11760 | delegate.RunUntilRedirect(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11761 | |
| 11762 | ASSERT_EQ(1, delegate.received_redirect_count()); |
| 11763 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11764 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11765 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11766 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11767 | EXPECT_EQ("gzip, deflate", value); |
| 11768 | EXPECT_EQ(1, delegate.received_redirect_count()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11769 | EXPECT_EQ("GET /redirect-test.html HTTP/1.1\r\n", |
| 11770 | raw_req_headers.request_line()); |
| 11771 | EXPECT_TRUE(raw_resp_headers->HasHeader("Location")); |
| 11772 | EXPECT_EQ(302, raw_resp_headers->response_code()); |
| 11773 | EXPECT_EQ("Redirect", raw_resp_headers->GetStatusText()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11774 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11775 | raw_req_headers = HttpRawRequestHeaders(); |
| 11776 | raw_resp_headers = nullptr; |
Arthur Sonzogni | b8465ff7 | 2019-01-04 18:44:35 | [diff] [blame^] | 11777 | r->FollowDeferredRedirect(base::nullopt /* removed_headers */, |
| 11778 | base::nullopt /* modified_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11779 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11780 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11781 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11782 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11783 | EXPECT_EQ("gzip, deflate", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11784 | EXPECT_EQ("GET /with-headers.html HTTP/1.1\r\n", |
| 11785 | raw_req_headers.request_line()); |
| 11786 | EXPECT_EQ(r->response_headers(), raw_resp_headers.get()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11787 | } |
| 11788 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11789 | TEST_F(URLRequestTest, HeadersCallbacksConnectFailed) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11790 | TestDelegate request_delegate; |
| 11791 | |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11792 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11793 | GURL("http://127.0.0.1:9/"), DEFAULT_PRIORITY, &request_delegate, |
| 11794 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11795 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
| 11796 | FAIL() << "Callback should not be called unless request is sent"; |
| 11797 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11798 | r->SetResponseHeadersCallback( |
| 11799 | base::Bind([](scoped_refptr<const net::HttpResponseHeaders>) { |
| 11800 | FAIL() << "Callback should not be called unless request is sent"; |
| 11801 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11802 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11803 | request_delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11804 | EXPECT_FALSE(r->is_pending()); |
| 11805 | } |
| 11806 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11807 | TEST_F(URLRequestTestHTTP, HeadersCallbacksAuthRetry) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11808 | ASSERT_TRUE(http_test_server()->Start()); |
| 11809 | GURL url(http_test_server()->GetURL("/auth-basic")); |
| 11810 | |
| 11811 | TestURLRequestContext context; |
| 11812 | TestDelegate delegate; |
| 11813 | |
| 11814 | delegate.set_credentials(AuthCredentials(kUser, kSecret)); |
| 11815 | HttpRequestHeaders extra_headers; |
| 11816 | extra_headers.SetHeader("X-Foo", "bar"); |
| 11817 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11818 | using ReqHeadersVector = std::vector<std::unique_ptr<HttpRawRequestHeaders>>; |
| 11819 | ReqHeadersVector raw_req_headers; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11820 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11821 | using RespHeadersVector = |
| 11822 | std::vector<scoped_refptr<const HttpResponseHeaders>>; |
| 11823 | RespHeadersVector raw_resp_headers; |
| 11824 | |
| 11825 | auto req_headers_callback = base::Bind( |
| 11826 | [](ReqHeadersVector* vec, HttpRawRequestHeaders headers) { |
| 11827 | vec->emplace_back(new HttpRawRequestHeaders(std::move(headers))); |
| 11828 | }, |
| 11829 | &raw_req_headers); |
| 11830 | auto resp_headers_callback = base::Bind( |
| 11831 | [](RespHeadersVector* vec, |
| 11832 | scoped_refptr<const HttpResponseHeaders> headers) { |
| 11833 | vec->push_back(headers); |
| 11834 | }, |
| 11835 | &raw_resp_headers); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11836 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 11837 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11838 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11839 | r->SetRequestHeadersCallback(req_headers_callback); |
| 11840 | r->SetResponseHeadersCallback(resp_headers_callback); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11841 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11842 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11843 | EXPECT_FALSE(r->is_pending()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11844 | ASSERT_EQ(raw_req_headers.size(), 2u); |
| 11845 | ASSERT_EQ(raw_resp_headers.size(), 2u); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11846 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11847 | EXPECT_FALSE(raw_req_headers[0]->FindHeaderForTest("Authorization", &value)); |
| 11848 | EXPECT_TRUE(raw_req_headers[0]->FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11849 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11850 | EXPECT_TRUE(raw_req_headers[1]->FindHeaderForTest("Authorization", &value)); |
| 11851 | EXPECT_TRUE(raw_req_headers[1]->FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11852 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11853 | EXPECT_EQ(raw_resp_headers[1], r->response_headers()); |
| 11854 | EXPECT_NE(raw_resp_headers[0], raw_resp_headers[1]); |
| 11855 | EXPECT_EQ(401, raw_resp_headers[0]->response_code()); |
| 11856 | EXPECT_EQ("Unauthorized", raw_resp_headers[0]->GetStatusText()); |
| 11857 | |
| 11858 | std::unique_ptr<URLRequest> r2(context.CreateRequest( |
| 11859 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11860 | r2->SetExtraRequestHeaders(extra_headers); |
| 11861 | r2->SetRequestHeadersCallback(req_headers_callback); |
| 11862 | r2->SetResponseHeadersCallback(resp_headers_callback); |
| 11863 | r2->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 11864 | r2->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11865 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11866 | EXPECT_FALSE(r2->is_pending()); |
| 11867 | ASSERT_EQ(raw_req_headers.size(), 3u); |
| 11868 | ASSERT_EQ(raw_resp_headers.size(), 3u); |
| 11869 | EXPECT_TRUE(raw_req_headers[2]->FindHeaderForTest("If-None-Match", &value)); |
| 11870 | EXPECT_NE(raw_resp_headers[2].get(), r2->response_headers()); |
| 11871 | EXPECT_EQ(304, raw_resp_headers[2]->response_code()); |
| 11872 | EXPECT_EQ("Not Modified", raw_resp_headers[2]->GetStatusText()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11873 | } |
| 11874 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11875 | TEST_F(URLRequestTest, HeadersCallbacksNonHTTP) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11876 | GURL data_url("data:text/html,<html><body>Hello!</body></html>"); |
| 11877 | TestDelegate d; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 11878 | std::unique_ptr<URLRequest> r(default_context().CreateRequest( |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11879 | data_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11880 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11881 | FAIL() << "Callback should not be called for non-HTTP schemes"; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11882 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 11883 | r->SetResponseHeadersCallback( |
| 11884 | base::Bind([](scoped_refptr<const net::HttpResponseHeaders>) { |
| 11885 | FAIL() << "Callback should not be called for non-HTTP schemes"; |
| 11886 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11887 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11888 | d.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 11889 | EXPECT_FALSE(r->is_pending()); |
| 11890 | } |
| 11891 | |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 11892 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSet) { |
| 11893 | TestDelegate d; |
| 11894 | BlockingNetworkDelegate network_delegate( |
| 11895 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 11896 | const GURL kOriginalUrl("https://original.test"); |
| 11897 | const GURL kRedirectUrl("http://redirect.test"); |
| 11898 | network_delegate.set_redirect_url(kRedirectUrl); |
| 11899 | TestURLRequestContext context(true /* delay_initialization */); |
| 11900 | context.set_network_delegate(&network_delegate); |
| 11901 | context.Init(); |
| 11902 | |
| 11903 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 11904 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11905 | r->set_upgrade_if_insecure(true); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 11906 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 11907 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 11908 | GURL::Replacements replacements; |
| 11909 | // Check that the redirect URL was upgraded to HTTPS since upgrade_if_insecure |
| 11910 | // was set. |
| 11911 | replacements.SetSchemeStr("https"); |
| 11912 | EXPECT_EQ(kRedirectUrl.ReplaceComponents(replacements), |
| 11913 | d.redirect_info().new_url); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 11914 | EXPECT_TRUE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 11915 | } |
| 11916 | |
| 11917 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSetExplicitPort80) { |
| 11918 | TestDelegate d; |
| 11919 | BlockingNetworkDelegate network_delegate( |
| 11920 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 11921 | const GURL kOriginalUrl("https://original.test"); |
| 11922 | const GURL kRedirectUrl("http://redirect.test:80"); |
| 11923 | network_delegate.set_redirect_url(kRedirectUrl); |
| 11924 | TestURLRequestContext context(true /* delay_initialization */); |
| 11925 | context.set_network_delegate(&network_delegate); |
| 11926 | context.Init(); |
| 11927 | |
| 11928 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 11929 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11930 | r->set_upgrade_if_insecure(true); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 11931 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 11932 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 11933 | GURL::Replacements replacements; |
| 11934 | // The URL host should have not been changed. |
| 11935 | EXPECT_EQ(d.redirect_info().new_url.host(), kRedirectUrl.host()); |
| 11936 | // The scheme should now be https, and the effective port should now be 443. |
| 11937 | EXPECT_TRUE(d.redirect_info().new_url.SchemeIs("https")); |
| 11938 | EXPECT_EQ(d.redirect_info().new_url.EffectiveIntPort(), 443); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 11939 | EXPECT_TRUE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 11940 | } |
| 11941 | |
| 11942 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSetNonStandardPort) { |
| 11943 | TestDelegate d; |
| 11944 | BlockingNetworkDelegate network_delegate( |
| 11945 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 11946 | const GURL kOriginalUrl("https://original.test"); |
| 11947 | const GURL kRedirectUrl("http://redirect.test:1234"); |
| 11948 | network_delegate.set_redirect_url(kRedirectUrl); |
| 11949 | TestURLRequestContext context(true /* delay_initialization */); |
| 11950 | context.set_network_delegate(&network_delegate); |
| 11951 | context.Init(); |
| 11952 | |
| 11953 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 11954 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11955 | r->set_upgrade_if_insecure(true); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 11956 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 11957 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 11958 | GURL::Replacements replacements; |
| 11959 | // Check that the redirect URL was upgraded to HTTPS since upgrade_if_insecure |
| 11960 | // was set, nonstandard port should not have been modified. |
| 11961 | replacements.SetSchemeStr("https"); |
| 11962 | EXPECT_EQ(kRedirectUrl.ReplaceComponents(replacements), |
| 11963 | d.redirect_info().new_url); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 11964 | EXPECT_TRUE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 11965 | } |
| 11966 | |
| 11967 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagNotSet) { |
| 11968 | TestDelegate d; |
| 11969 | BlockingNetworkDelegate network_delegate( |
| 11970 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 11971 | const GURL kOriginalUrl("https://original.test"); |
| 11972 | const GURL kRedirectUrl("http://redirect.test"); |
| 11973 | network_delegate.set_redirect_url(kRedirectUrl); |
| 11974 | TestURLRequestContext context(true /* delay_initialization */); |
| 11975 | context.set_network_delegate(&network_delegate); |
| 11976 | context.Init(); |
| 11977 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 11978 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 11979 | r->set_upgrade_if_insecure(false); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 11980 | r->Start(); |
Wez | 0e71711 | 2018-06-18 23:09:22 | [diff] [blame] | 11981 | d.RunUntilRedirect(); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 11982 | // The redirect URL should not be changed if the upgrade_if_insecure flag is |
| 11983 | // not set. |
| 11984 | EXPECT_EQ(kRedirectUrl, d.redirect_info().new_url); |
Carlos IL | 0d67fc7 | 2018-06-22 17:18:44 | [diff] [blame] | 11985 | EXPECT_FALSE(d.redirect_info().insecure_scheme_was_upgraded); |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 11986 | } |
| 11987 | |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 11988 | // Test that URLRequests get properly tagged. |
| 11989 | #if defined(OS_ANDROID) |
| 11990 | TEST_F(URLRequestTestHTTP, TestTagging) { |
| 11991 | ASSERT_TRUE(http_test_server()->Start()); |
| 11992 | |
| 11993 | // The tag under which the system reports untagged traffic. |
| 11994 | static const int32_t UNTAGGED_TAG = 0; |
| 11995 | |
| 11996 | uint64_t old_traffic = GetTaggedBytes(UNTAGGED_TAG); |
| 11997 | |
| 11998 | // Untagged traffic should be tagged with tag UNTAGGED_TAG. |
| 11999 | TestDelegate delegate; |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12000 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12001 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &delegate, |
| 12002 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12003 | EXPECT_EQ(SocketTag(), req->socket_tag()); |
| 12004 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12005 | delegate.RunUntilComplete(); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12006 | |
| 12007 | EXPECT_GT(GetTaggedBytes(UNTAGGED_TAG), old_traffic); |
| 12008 | |
| 12009 | int32_t tag_val1 = 0x12345678; |
| 12010 | SocketTag tag1(SocketTag::UNSET_UID, tag_val1); |
| 12011 | old_traffic = GetTaggedBytes(tag_val1); |
| 12012 | |
| 12013 | // Test specific tag value. |
Ryan Sleevi | b8449e0 | 2018-07-15 04:31:07 | [diff] [blame] | 12014 | req = default_context().CreateRequest(http_test_server()->GetURL("/"), |
| 12015 | DEFAULT_PRIORITY, &delegate, |
| 12016 | TRAFFIC_ANNOTATION_FOR_TESTS); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12017 | req->set_socket_tag(tag1); |
| 12018 | EXPECT_EQ(tag1, req->socket_tag()); |
| 12019 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12020 | delegate.RunUntilComplete(); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12021 | |
| 12022 | EXPECT_GT(GetTaggedBytes(tag_val1), old_traffic); |
| 12023 | } |
| 12024 | #endif |
| 12025 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 12026 | } // namespace net |