[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 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 5 | #include <utility> |
| 6 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 7 | // This must be before Windows headers |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 8 | #include "build/build_config.h" |
| 9 | |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 10 | #if defined(OS_WIN) |
robliao | eb9bfd64 | 2017-05-18 17:35:16 | [diff] [blame] | 11 | #include <objbase.h> |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 12 | #include <shlobj.h> |
Fan Yang | 5a88d5d | 2017-10-24 01:14:29 | [diff] [blame] | 13 | #include <windows.h> |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 14 | #include <wrl/client.h> |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 15 | #endif |
| 16 | |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 17 | #include <stdint.h> |
| 18 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 19 | #include <algorithm> |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 20 | #include <limits> |
Devlin Cronin | e4bcb40e | 2018-06-05 18:02:47 | [diff] [blame] | 21 | #include <memory> |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 22 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 23 | #include "base/base64url.h" |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 24 | #include "base/bind.h" |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 25 | #include "base/compiler_specific.h" |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 26 | #include "base/files/file_path.h" |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame] | 27 | #include "base/files/file_util.h" |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 28 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 29 | #include "base/format_macros.h" |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 30 | #include "base/json/json_reader.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 31 | #include "base/location.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 32 | #include "base/macros.h" |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 33 | #include "base/memory/weak_ptr.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [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" |
[email protected] | 4dc3ad4f | 2013-06-11 07:15:50 | [diff] [blame] | 40 | #include "base/strings/string_number_conversions.h" |
[email protected] | d069c11a | 2013-04-13 00:01:55 | [diff] [blame] | 41 | #include "base/strings/string_piece.h" |
[email protected] | d778e042 | 2013-03-06 18:10:22 | [diff] [blame] | 42 | #include "base/strings/string_split.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 43 | #include "base/strings/string_util.h" |
| 44 | #include "base/strings/stringprintf.h" |
[email protected] | 750b2f3c | 2013-06-07 18:41:05 | [diff] [blame] | 45 | #include "base/strings/utf_string_conversions.h" |
Devlin Cronin | e4bcb40e | 2018-06-05 18:02:47 | [diff] [blame] | 46 | #include "base/test/metrics/histogram_tester.h" |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 47 | #include "base/test/scoped_feature_list.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 48 | #include "base/threading/thread_task_runner_handle.h" |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 49 | #include "base/values.h" |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 50 | #include "build/buildflag.h" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 51 | #include "net/base/chunked_upload_data_stream.h" |
tfarina | 43a416b | 2016-01-06 21:48:07 | [diff] [blame] | 52 | #include "net/base/directory_listing.h" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 53 | #include "net/base/elements_upload_data_stream.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 54 | #include "net/base/load_flags.h" |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 55 | #include "net/base/load_timing_info.h" |
| 56 | #include "net/base/load_timing_info_test_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 57 | #include "net/base/net_errors.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 58 | #include "net/base/net_module.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 59 | #include "net/base/proxy_server.h" |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 60 | #include "net/base/request_priority.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 61 | #include "net/base/test_completion_callback.h" |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 62 | #include "net/base/upload_bytes_element_reader.h" |
| 63 | #include "net/base/upload_data_stream.h" |
| 64 | #include "net/base/upload_file_element_reader.h" |
tfarina | 7a4a7fd | 2016-01-20 14:23:44 | [diff] [blame] | 65 | #include "net/base/url_util.h" |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 66 | #include "net/cert/ct_policy_enforcer.h" |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 67 | #include "net/cert/ct_policy_status.h" |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 68 | #include "net/cert/do_nothing_ct_verifier.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 69 | #include "net/cert/ev_root_ca_metadata.h" |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 70 | #include "net/cert/mock_cert_verifier.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 71 | #include "net/cert/multi_log_ct_verifier.h" |
eranm | dcec963 | 2016-10-10 14:16:10 | [diff] [blame] | 72 | #include "net/cert/signed_certificate_timestamp_and_status.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 73 | #include "net/cert/test_root_certs.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 74 | #include "net/cookies/cookie_monster.h" |
| 75 | #include "net/cookies/cookie_store_test_helpers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 76 | #include "net/disk_cache/disk_cache.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 77 | #include "net/dns/mock_host_resolver.h" |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 78 | #include "net/http/http_byte_range.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 79 | #include "net/http/http_cache.h" |
| 80 | #include "net/http/http_network_layer.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 81 | #include "net/http/http_network_session.h" |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 82 | #include "net/http/http_request_headers.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 83 | #include "net/http/http_response_headers.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 84 | #include "net/http/http_server_properties_impl.h" |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 85 | #include "net/http/http_util.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 86 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 87 | #include "net/log/net_log_source.h" |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 88 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 89 | #include "net/log/test_net_log_entry.h" |
| 90 | #include "net/log/test_net_log_util.h" |
Scott Violet | 0caaaf43 | 2018-03-24 00:43:59 | [diff] [blame] | 91 | #include "net/net_buildflags.h" |
Lily Houghton | ffe89daa0 | 2018-03-09 18:30:03 | [diff] [blame] | 92 | #include "net/proxy_resolution/proxy_resolution_service.h" |
zhongyi | 18ddddd | 2017-02-03 19:06:36 | [diff] [blame] | 93 | #include "net/quic/chromium/mock_crypto_client_stream_factory.h" |
| 94 | #include "net/quic/chromium/quic_server_info.h" |
| 95 | #include "net/socket/socket_test_util.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 96 | #include "net/socket/ssl_client_socket.h" |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 97 | #include "net/ssl/channel_id_service.h" |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 98 | #include "net/ssl/client_cert_identity_test_util.h" |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 99 | #include "net/ssl/default_channel_id_store.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 100 | #include "net/ssl/ssl_connection_status_flags.h" |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 101 | #include "net/ssl/ssl_private_key.h" |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 102 | #include "net/ssl/ssl_server_config.h" |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 103 | #include "net/ssl/token_binding.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 104 | #include "net/test/cert_test_util.h" |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 105 | #include "net/test/embedded_test_server/embedded_test_server.h" |
| 106 | #include "net/test/embedded_test_server/http_request.h" |
| 107 | #include "net/test/embedded_test_server/http_response.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 108 | #include "net/test/gtest_util.h" |
[email protected] | 89b3252 | 2013-05-07 20:04:21 | [diff] [blame] | 109 | #include "net/test/spawned_test_server/spawned_test_server.h" |
rsleevi | a69c79a | 2016-06-22 03:28:43 | [diff] [blame] | 110 | #include "net/test/test_data_directory.h" |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 111 | #include "net/test/test_with_scoped_task_environment.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 112 | #include "net/test/url_request/url_request_failed_job.h" |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 113 | #include "net/test/url_request/url_request_mock_http_job.h" |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 114 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 115 | #include "net/url_request/data_protocol_handler.h" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 116 | #include "net/url_request/static_http_user_agent_settings.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 117 | #include "net/url_request/url_request.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 118 | #include "net/url_request/url_request_filter.h" |
[email protected] | bcb84f8b | 2009-08-31 16:20:14 | [diff] [blame] | 119 | #include "net/url_request/url_request_http_job.h" |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 120 | #include "net/url_request/url_request_http_job_histogram.h" |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 121 | #include "net/url_request/url_request_intercepting_job_factory.h" |
| 122 | #include "net/url_request/url_request_interceptor.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 123 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 124 | #include "net/url_request/url_request_redirect_job.h" |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 125 | #include "net/url_request/url_request_test_job.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 126 | #include "net/url_request/url_request_test_util.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 127 | #include "testing/gmock/include/gmock/gmock.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 128 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 129 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 130 | |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 131 | #if defined(OS_FUCHSIA) |
| 132 | #define USE_BUILTIN_CERT_VERIFIER |
| 133 | #endif |
| 134 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 135 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 136 | #include "net/base/filename_util.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 137 | #include "net/url_request/file_protocol_handler.h" |
| 138 | #include "net/url_request/url_request_file_dir_job.h" |
| 139 | #endif |
| 140 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 141 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 142 | #include "net/ftp/ftp_network_layer.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 143 | #include "net/url_request/ftp_protocol_handler.h" |
| 144 | #endif |
| 145 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 146 | #if defined(OS_WIN) |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 147 | #include "base/win/scoped_com_initializer.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 148 | #endif |
| 149 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 150 | #if BUILDFLAG(ENABLE_REPORTING) |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 151 | #include "net/network_error_logging/network_error_logging_service.h" |
Julia Tuttle | 91a655d | 2018-01-26 18:03:03 | [diff] [blame] | 152 | #include "net/reporting/reporting_policy.h" |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 153 | #include "net/reporting/reporting_service.h" |
| 154 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 155 | |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 156 | #if defined(OS_ANDROID) || defined(USE_BUILTIN_CERT_VERIFIER) |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 157 | #include "net/cert/cert_net_fetcher.h" |
| 158 | #include "net/cert_net/cert_net_fetcher_impl.h" |
| 159 | #endif |
| 160 | |
| 161 | #if defined(USE_NSS_CERTS) |
| 162 | #include "net/cert_net/nss_ocsp.h" |
| 163 | #endif |
| 164 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 165 | using net::test::IsError; |
| 166 | using net::test::IsOk; |
| 167 | |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 168 | using base::ASCIIToUTF16; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 169 | using base::Time; |
halton.huo | e4e4574 | 2014-12-08 07:55:46 | [diff] [blame] | 170 | using std::string; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 171 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 172 | namespace net { |
| 173 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 174 | namespace { |
| 175 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 176 | namespace test_default { |
| 177 | #include "net/http/transport_security_state_static_unittest_default.h" |
xunjieli | 815ad5b | 2017-07-18 15:51:35 | [diff] [blame] | 178 | } |
| 179 | |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 180 | const base::string16 kChrome(ASCIIToUTF16("chrome")); |
| 181 | const base::string16 kSecret(ASCIIToUTF16("secret")); |
| 182 | const base::string16 kUser(ASCIIToUTF16("user")); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 183 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 184 | const base::FilePath::CharType kTestFilePath[] = |
| 185 | FILE_PATH_LITERAL("net/data/url_request_unittest"); |
| 186 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 187 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 188 | !defined(OS_FUCHSIA) |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 189 | // Test file used in most FTP tests. |
| 190 | const char kFtpTestFile[] = "BullRunSpeech.txt"; |
| 191 | #endif |
| 192 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 193 | // Tests load timing information in the case a fresh connection was used, with |
| 194 | // no proxy. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 195 | void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info, |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 196 | int connect_timing_flags) { |
| 197 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 198 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 199 | |
| 200 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 201 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 202 | |
| 203 | EXPECT_LE(load_timing_info.request_start, |
| 204 | load_timing_info.connect_timing.connect_start); |
| 205 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 206 | connect_timing_flags); |
| 207 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 208 | load_timing_info.send_start); |
| 209 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 210 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 211 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 212 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 213 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 214 | } |
| 215 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 216 | // Same as above, but with proxy times. |
| 217 | void TestLoadTimingNotReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 218 | const LoadTimingInfo& load_timing_info, |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 219 | int connect_timing_flags) { |
| 220 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 221 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 222 | |
| 223 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 224 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 225 | |
| 226 | EXPECT_LE(load_timing_info.request_start, |
| 227 | load_timing_info.proxy_resolve_start); |
| 228 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 229 | load_timing_info.proxy_resolve_end); |
| 230 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 231 | load_timing_info.connect_timing.connect_start); |
| 232 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 233 | connect_timing_flags); |
| 234 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 235 | load_timing_info.send_start); |
| 236 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 237 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 238 | } |
| 239 | |
| 240 | // Same as above, but with a reused socket and proxy times. |
| 241 | void TestLoadTimingReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 242 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 243 | EXPECT_TRUE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 244 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 245 | |
| 246 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 247 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 248 | |
| 249 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 250 | |
| 251 | EXPECT_LE(load_timing_info.request_start, |
| 252 | load_timing_info.proxy_resolve_start); |
| 253 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 254 | load_timing_info.proxy_resolve_end); |
| 255 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 256 | load_timing_info.send_start); |
| 257 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 258 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 259 | } |
| 260 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 261 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 262 | // Tests load timing information in the case of a cache hit, when no cache |
| 263 | // validation request was sent over the wire. |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 264 | base::StringPiece TestNetResourceProvider(int key) { |
| 265 | return "header"; |
| 266 | } |
| 267 | |
| 268 | void FillBuffer(char* buffer, size_t len) { |
| 269 | static bool called = false; |
| 270 | if (!called) { |
| 271 | called = true; |
| 272 | int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 273 | srand(seed); |
| 274 | } |
| 275 | |
| 276 | for (size_t i = 0; i < len; i++) { |
| 277 | buffer[i] = static_cast<char>(rand()); |
| 278 | if (!buffer[i]) |
| 279 | buffer[i] = 'g'; |
| 280 | } |
| 281 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 282 | #endif |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 283 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 284 | void TestLoadTimingCacheHitNoNetwork( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 285 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 286 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 287 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 288 | |
| 289 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 290 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 291 | |
| 292 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 293 | EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start); |
| 294 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 295 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 296 | |
| 297 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 298 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 299 | } |
| 300 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 301 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 302 | !defined(OS_FUCHSIA) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 303 | // Tests load timing in the case that there is no HTTP response. This can be |
| 304 | // used to test in the case of errors or non-HTTP requests. |
| 305 | void TestLoadTimingNoHttpResponse( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 306 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 307 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 308 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 309 | |
| 310 | // Only the request times should be non-null. |
| 311 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 312 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 313 | |
| 314 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 315 | |
| 316 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 317 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 318 | EXPECT_TRUE(load_timing_info.send_start.is_null()); |
| 319 | EXPECT_TRUE(load_timing_info.send_end.is_null()); |
| 320 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 321 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 322 | #endif |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 323 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 324 | // Test power monitor source that can simulate entering suspend mode. Can't use |
| 325 | // the one in base/ because it insists on bringing its own MessageLoop. |
| 326 | class TestPowerMonitorSource : public base::PowerMonitorSource { |
| 327 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 328 | TestPowerMonitorSource() = default; |
| 329 | ~TestPowerMonitorSource() override = default; |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 330 | |
| 331 | void Suspend() { ProcessPowerEvent(SUSPEND_EVENT); } |
| 332 | |
| 333 | void Resume() { ProcessPowerEvent(RESUME_EVENT); } |
| 334 | |
| 335 | bool IsOnBatteryPowerImpl() override { return false; } |
| 336 | |
| 337 | private: |
| 338 | DISALLOW_COPY_AND_ASSIGN(TestPowerMonitorSource); |
| 339 | }; |
| 340 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 341 | // Job that allows monitoring of its priority. |
| 342 | class PriorityMonitoringURLRequestJob : public URLRequestTestJob { |
| 343 | public: |
| 344 | // The latest priority of the job is always written to |request_priority_|. |
| 345 | PriorityMonitoringURLRequestJob(URLRequest* request, |
| 346 | NetworkDelegate* network_delegate, |
| 347 | RequestPriority* request_priority) |
| 348 | : URLRequestTestJob(request, network_delegate), |
| 349 | request_priority_(request_priority) { |
| 350 | *request_priority_ = DEFAULT_PRIORITY; |
| 351 | } |
| 352 | |
| 353 | void SetPriority(RequestPriority priority) override { |
| 354 | *request_priority_ = priority; |
| 355 | URLRequestTestJob::SetPriority(priority); |
| 356 | } |
| 357 | |
| 358 | private: |
| 359 | RequestPriority* const request_priority_; |
| 360 | }; |
| 361 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 362 | // Do a case-insensitive search through |haystack| for |needle|. |
| 363 | bool ContainsString(const std::string& haystack, const char* needle) { |
brettw | a2027fb | 2015-07-14 02:24:50 | [diff] [blame] | 364 | std::string::const_iterator it = std::search( |
| 365 | haystack.begin(), haystack.end(), needle, needle + strlen(needle), |
| 366 | base::CaseInsensitiveCompareASCII<char>()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 367 | return it != haystack.end(); |
| 368 | } |
| 369 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 370 | std::unique_ptr<UploadDataStream> CreateSimpleUploadData(const char* data) { |
| 371 | std::unique_ptr<UploadElementReader> reader( |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 372 | new UploadBytesElementReader(data, strlen(data))); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 373 | return ElementsUploadDataStream::CreateWithReader(std::move(reader), 0); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 374 | } |
| 375 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 376 | // Verify that the SSLInfo of a successful SSL connection has valid values. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 377 | void CheckSSLInfo(const SSLInfo& ssl_info) { |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 378 | // -1 means unknown. 0 means no encryption. |
| 379 | EXPECT_GT(ssl_info.security_bits, 0); |
| 380 | |
| 381 | // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated. |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 382 | uint16_t cipher_suite = |
| 383 | SSLConnectionStatusToCipherSuite(ssl_info.connection_status); |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 384 | EXPECT_NE(0U, cipher_suite); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 385 | } |
| 386 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 387 | void CheckFullRequestHeaders(const HttpRequestHeaders& headers, |
| 388 | const GURL& host_url) { |
| 389 | std::string sent_value; |
| 390 | |
| 391 | EXPECT_TRUE(headers.GetHeader("Host", &sent_value)); |
| 392 | EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value); |
| 393 | |
| 394 | EXPECT_TRUE(headers.GetHeader("Connection", &sent_value)); |
| 395 | EXPECT_EQ("keep-alive", sent_value); |
| 396 | } |
| 397 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 398 | // A network delegate that allows the user to choose a subset of request stages |
| 399 | // to block in. When blocking, the delegate can do one of the following: |
| 400 | // * synchronously return a pre-specified error code, or |
| 401 | // * asynchronously return that value via an automatically called callback, |
| 402 | // or |
| 403 | // * block and wait for the user to do a callback. |
| 404 | // Additionally, the user may also specify a redirect URL -- then each request |
| 405 | // with the current URL different from the redirect target will be redirected |
| 406 | // to that target, in the on-before-URL-request stage, independent of whether |
| 407 | // the delegate blocks in ON_BEFORE_URL_REQUEST or not. |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 408 | class BlockingNetworkDelegate : public TestNetworkDelegate { |
| 409 | public: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 410 | // Stages in which the delegate can block. |
| 411 | enum Stage { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 412 | NOT_BLOCKED = 0, |
| 413 | ON_BEFORE_URL_REQUEST = 1 << 0, |
| 414 | ON_BEFORE_SEND_HEADERS = 1 << 1, |
| 415 | ON_HEADERS_RECEIVED = 1 << 2, |
| 416 | ON_AUTH_REQUIRED = 1 << 3 |
| 417 | }; |
| 418 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 419 | // Behavior during blocked stages. During other stages, just |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 420 | // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 421 | enum BlockMode { |
| 422 | SYNCHRONOUS, // No callback, returns specified return values. |
| 423 | AUTO_CALLBACK, // |this| posts a task to run the callback using the |
| 424 | // specified return codes. |
| 425 | USER_CALLBACK, // User takes care of doing a callback. |retval_| and |
| 426 | // |auth_retval_| are ignored. In every blocking stage the |
| 427 | // message loop is quit. |
| 428 | }; |
| 429 | |
| 430 | // Creates a delegate which does not block at all. |
| 431 | explicit BlockingNetworkDelegate(BlockMode block_mode); |
| 432 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 433 | // Runs the message loop until the delegate blocks. |
| 434 | void RunUntilBlocked(); |
| 435 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 436 | // For users to trigger a callback returning |response|. |
| 437 | // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks. |
| 438 | // Only call if |block_mode_| == USER_CALLBACK. |
| 439 | void DoCallback(int response); |
| 440 | void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response); |
| 441 | |
| 442 | // Setters. |
| 443 | void set_retval(int retval) { |
| 444 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 445 | ASSERT_NE(ERR_IO_PENDING, retval); |
| 446 | ASSERT_NE(OK, retval); |
| 447 | retval_ = retval; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 448 | } |
| 449 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 450 | // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then |
| 451 | // |auth_credentials_| will be passed with the response. |
| 452 | void set_auth_retval(AuthRequiredResponse auth_retval) { |
| 453 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 454 | ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval); |
| 455 | auth_retval_ = auth_retval; |
| 456 | } |
| 457 | void set_auth_credentials(const AuthCredentials& auth_credentials) { |
| 458 | auth_credentials_ = auth_credentials; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 459 | } |
| 460 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 461 | void set_redirect_url(const GURL& url) { |
| 462 | redirect_url_ = url; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 463 | } |
| 464 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 465 | void set_block_on(int block_on) { |
| 466 | block_on_ = block_on; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 467 | } |
| 468 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 469 | // Allows the user to check in which state did we block. |
| 470 | Stage stage_blocked_for_callback() const { |
| 471 | EXPECT_EQ(USER_CALLBACK, block_mode_); |
| 472 | return stage_blocked_for_callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | private: |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 476 | void OnBlocked(); |
| 477 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 478 | void RunCallback(int response, CompletionOnceCallback callback); |
| 479 | void RunAuthCallback(AuthRequiredResponse response, AuthCallback callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 480 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 481 | // TestNetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 482 | int OnBeforeURLRequest(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 483 | CompletionOnceCallback callback, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 484 | GURL* new_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 485 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 486 | int OnBeforeStartTransaction(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 487 | CompletionOnceCallback callback, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 488 | HttpRequestHeaders* headers) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 489 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 490 | int OnHeadersReceived( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 491 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 492 | CompletionOnceCallback callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 493 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 494 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 495 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 496 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 497 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 498 | URLRequest* request, |
| 499 | const AuthChallengeInfo& auth_info, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 500 | AuthCallback callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 501 | AuthCredentials* credentials) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 502 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 503 | // Resets the callbacks and |stage_blocked_for_callback_|. |
| 504 | void Reset(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 505 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 506 | // Checks whether we should block in |stage|. If yes, returns an error code |
| 507 | // and optionally sets up callback based on |block_mode_|. If no, returns OK. |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 508 | int MaybeBlockStage(Stage stage, CompletionOnceCallback callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 509 | |
| 510 | // Configuration parameters, can be adjusted by public methods: |
| 511 | const BlockMode block_mode_; |
| 512 | |
| 513 | // Values returned on blocking stages when mode is SYNCHRONOUS or |
| 514 | // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING. |
| 515 | int retval_; // To be returned in non-auth stages. |
| 516 | AuthRequiredResponse auth_retval_; |
| 517 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 518 | GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 519 | int block_on_; // Bit mask: in which stages to block. |
| 520 | |
| 521 | // |auth_credentials_| will be copied to |*target_auth_credential_| on |
| 522 | // callback. |
| 523 | AuthCredentials auth_credentials_; |
| 524 | AuthCredentials* target_auth_credentials_; |
| 525 | |
| 526 | // Internal variables, not set by not the user: |
| 527 | // Last blocked stage waiting for user callback (unused if |block_mode_| != |
| 528 | // USER_CALLBACK). |
| 529 | Stage stage_blocked_for_callback_; |
| 530 | |
| 531 | // Callback objects stored during blocking stages. |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 532 | CompletionOnceCallback callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 533 | AuthCallback auth_callback_; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 534 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 535 | // Closure to run to exit RunUntilBlocked(). |
| 536 | base::OnceClosure on_blocked_; |
| 537 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 538 | base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; |
| 539 | |
| 540 | DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 541 | }; |
| 542 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 543 | BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode) |
| 544 | : block_mode_(block_mode), |
| 545 | retval_(OK), |
| 546 | auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION), |
| 547 | block_on_(0), |
| 548 | target_auth_credentials_(NULL), |
| 549 | stage_blocked_for_callback_(NOT_BLOCKED), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 550 | weak_factory_(this) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 551 | } |
| 552 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 553 | void BlockingNetworkDelegate::RunUntilBlocked() { |
| 554 | base::RunLoop run_loop; |
| 555 | on_blocked_ = run_loop.QuitClosure(); |
| 556 | run_loop.Run(); |
| 557 | } |
| 558 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 559 | void BlockingNetworkDelegate::DoCallback(int response) { |
| 560 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 561 | ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 562 | ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 563 | CompletionOnceCallback callback = std::move(callback_); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 564 | Reset(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 565 | |
| 566 | // |callback| may trigger completion of a request, so post it as a task, so |
| 567 | // it will run under a subsequent TestDelegate::RunUntilComplete() loop. |
| 568 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 569 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::RunCallback, |
| 570 | weak_factory_.GetWeakPtr(), response, |
| 571 | std::move(callback))); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | void BlockingNetworkDelegate::DoAuthCallback( |
| 575 | NetworkDelegate::AuthRequiredResponse response) { |
| 576 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 577 | ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 578 | AuthCallback auth_callback = std::move(auth_callback_); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 579 | Reset(); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 580 | RunAuthCallback(response, std::move(auth_callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 581 | } |
| 582 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 583 | void BlockingNetworkDelegate::OnBlocked() { |
| 584 | // If this fails due to |on_blocked_| being null then OnBlocked() was run by |
| 585 | // a RunLoop other than RunUntilBlocked(), indicating a bug in the calling |
| 586 | // test. |
| 587 | std::move(on_blocked_).Run(); |
| 588 | } |
| 589 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 590 | void BlockingNetworkDelegate::RunCallback(int response, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 591 | CompletionOnceCallback callback) { |
| 592 | std::move(callback).Run(response); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 596 | AuthCallback callback) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 597 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) { |
| 598 | ASSERT_TRUE(target_auth_credentials_ != NULL); |
| 599 | *target_auth_credentials_ = auth_credentials_; |
| 600 | } |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 601 | std::move(callback).Run(response); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 602 | } |
| 603 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 604 | int BlockingNetworkDelegate::OnBeforeURLRequest(URLRequest* request, |
| 605 | CompletionOnceCallback callback, |
| 606 | GURL* new_url) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 607 | if (redirect_url_ == request->url()) |
| 608 | return OK; // We've already seen this request and redirected elsewhere. |
| 609 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 610 | // TestNetworkDelegate always completes synchronously. |
| 611 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeURLRequest( |
| 612 | request, base::NullCallback(), new_url)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 613 | |
| 614 | if (!redirect_url_.is_empty()) |
| 615 | *new_url = redirect_url_; |
| 616 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 617 | return MaybeBlockStage(ON_BEFORE_URL_REQUEST, std::move(callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 618 | } |
| 619 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 620 | int BlockingNetworkDelegate::OnBeforeStartTransaction( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 621 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 622 | CompletionOnceCallback callback, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 623 | HttpRequestHeaders* headers) { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 624 | // TestNetworkDelegate always completes synchronously. |
| 625 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeStartTransaction( |
| 626 | request, base::NullCallback(), headers)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 627 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 628 | return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, std::move(callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | int BlockingNetworkDelegate::OnHeadersReceived( |
| 632 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 633 | CompletionOnceCallback callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 634 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 635 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
| 636 | GURL* allowed_unsafe_redirect_url) { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 637 | // TestNetworkDelegate always completes synchronously. |
| 638 | CHECK_NE(ERR_IO_PENDING, |
| 639 | TestNetworkDelegate::OnHeadersReceived( |
| 640 | request, base::NullCallback(), original_response_headers, |
| 641 | override_response_headers, allowed_unsafe_redirect_url)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 642 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 643 | return MaybeBlockStage(ON_HEADERS_RECEIVED, std::move(callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired( |
| 647 | URLRequest* request, |
| 648 | const AuthChallengeInfo& auth_info, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 649 | AuthCallback callback, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 650 | AuthCredentials* credentials) { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 651 | // TestNetworkDelegate always completes synchronously. |
| 652 | CHECK_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, |
| 653 | TestNetworkDelegate::OnAuthRequired( |
| 654 | request, auth_info, base::NullCallback(), credentials)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 655 | // Check that the user has provided callback for the previous blocked stage. |
| 656 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 657 | |
| 658 | if ((block_on_ & ON_AUTH_REQUIRED) == 0) { |
| 659 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 660 | } |
| 661 | |
| 662 | target_auth_credentials_ = credentials; |
| 663 | |
| 664 | switch (block_mode_) { |
| 665 | case SYNCHRONOUS: |
| 666 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) |
| 667 | *target_auth_credentials_ = auth_credentials_; |
| 668 | return auth_retval_; |
| 669 | |
| 670 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 671 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 672 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::RunAuthCallback, |
| 673 | weak_factory_.GetWeakPtr(), auth_retval_, |
| 674 | std::move(callback))); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 675 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 676 | |
| 677 | case USER_CALLBACK: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 678 | auth_callback_ = std::move(callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 679 | stage_blocked_for_callback_ = ON_AUTH_REQUIRED; |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 680 | // We may reach here via a callback prior to RunUntilBlocked(), so post |
| 681 | // a task to fetch and run the |on_blocked_| closure. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 682 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 683 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::OnBlocked, |
| 684 | weak_factory_.GetWeakPtr())); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 685 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 686 | } |
| 687 | NOTREACHED(); |
| 688 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value. |
| 689 | } |
| 690 | |
| 691 | void BlockingNetworkDelegate::Reset() { |
| 692 | EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 693 | stage_blocked_for_callback_ = NOT_BLOCKED; |
| 694 | callback_.Reset(); |
| 695 | auth_callback_.Reset(); |
| 696 | } |
| 697 | |
| 698 | int BlockingNetworkDelegate::MaybeBlockStage( |
| 699 | BlockingNetworkDelegate::Stage stage, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 700 | CompletionOnceCallback callback) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 701 | // Check that the user has provided callback for the previous blocked stage. |
| 702 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 703 | |
| 704 | if ((block_on_ & stage) == 0) { |
| 705 | return OK; |
| 706 | } |
| 707 | |
| 708 | switch (block_mode_) { |
| 709 | case SYNCHRONOUS: |
| 710 | EXPECT_NE(OK, retval_); |
| 711 | return retval_; |
| 712 | |
| 713 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 714 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 715 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::RunCallback, |
| 716 | weak_factory_.GetWeakPtr(), retval_, |
| 717 | std::move(callback))); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 718 | return ERR_IO_PENDING; |
| 719 | |
| 720 | case USER_CALLBACK: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 721 | callback_ = std::move(callback); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 722 | stage_blocked_for_callback_ = stage; |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 723 | // We may reach here via a callback prior to RunUntilBlocked(), so post |
| 724 | // a task to fetch and run the |on_blocked_| closure. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 725 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 726 | FROM_HERE, base::BindOnce(&BlockingNetworkDelegate::OnBlocked, |
| 727 | weak_factory_.GetWeakPtr())); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 728 | return ERR_IO_PENDING; |
| 729 | } |
| 730 | NOTREACHED(); |
| 731 | return 0; |
| 732 | } |
| 733 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 734 | class TestURLRequestContextWithProxy : public TestURLRequestContext { |
| 735 | public: |
| 736 | // Does not own |delegate|. |
| 737 | TestURLRequestContextWithProxy(const std::string& proxy, |
| 738 | NetworkDelegate* delegate) |
| 739 | : TestURLRequestContext(true) { |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 740 | context_storage_.set_proxy_resolution_service( |
Ramin Halavati | ca8d525 | 2018-03-12 05:33:49 | [diff] [blame] | 741 | ProxyResolutionService::CreateFixed(proxy, |
| 742 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 743 | set_network_delegate(delegate); |
| 744 | Init(); |
| 745 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 746 | ~TestURLRequestContextWithProxy() override = default; |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 747 | }; |
| 748 | |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 749 | // A mock ReportSenderInterface that just remembers the latest report |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 750 | // URI and report to be sent. |
| 751 | class MockCertificateReportSender |
stefanocs | bd5be520 | 2016-06-10 03:37:55 | [diff] [blame] | 752 | : public TransportSecurityState::ReportSenderInterface { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 753 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 754 | MockCertificateReportSender() = default; |
| 755 | ~MockCertificateReportSender() override = default; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 756 | |
meacer | 5d4dc5a | 2017-04-27 20:37:48 | [diff] [blame] | 757 | void Send(const GURL& report_uri, |
| 758 | base::StringPiece content_type, |
| 759 | base::StringPiece report, |
| 760 | const base::Callback<void()>& success_callback, |
| 761 | const base::Callback<void(const GURL&, int, int)>& error_callback) |
| 762 | override { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 763 | latest_report_uri_ = report_uri; |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 764 | report.CopyToString(&latest_report_); |
| 765 | content_type.CopyToString(&latest_content_type_); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 766 | } |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 767 | const GURL& latest_report_uri() { return latest_report_uri_; } |
| 768 | const std::string& latest_report() { return latest_report_; } |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 769 | const std::string& latest_content_type() { return latest_content_type_; } |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 770 | |
| 771 | private: |
| 772 | GURL latest_report_uri_; |
| 773 | std::string latest_report_; |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 774 | std::string latest_content_type_; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 775 | }; |
| 776 | |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 777 | class TestExperimentalFeaturesNetworkDelegate : public TestNetworkDelegate { |
| 778 | public: |
| 779 | bool OnAreExperimentalCookieFeaturesEnabled() const override { return true; } |
| 780 | }; |
| 781 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 782 | // OCSPErrorTestDelegate caches the SSLInfo passed to OnSSLCertificateError. |
| 783 | // This is needed because after the certificate failure, the URLRequest will |
| 784 | // retry the connection, and return a partial SSLInfo with a cached cert status. |
| 785 | // The partial SSLInfo does not have the OCSP information filled out. |
| 786 | class OCSPErrorTestDelegate : public TestDelegate { |
| 787 | public: |
| 788 | void OnSSLCertificateError(URLRequest* request, |
| 789 | const SSLInfo& ssl_info, |
| 790 | bool fatal) override { |
| 791 | ssl_info_ = ssl_info; |
| 792 | on_ssl_certificate_error_called_ = true; |
| 793 | TestDelegate::OnSSLCertificateError(request, ssl_info, fatal); |
| 794 | } |
| 795 | |
| 796 | bool on_ssl_certificate_error_called() { |
| 797 | return on_ssl_certificate_error_called_; |
| 798 | } |
| 799 | |
| 800 | SSLInfo ssl_info() { return ssl_info_; } |
| 801 | |
| 802 | private: |
| 803 | bool on_ssl_certificate_error_called_ = false; |
| 804 | SSLInfo ssl_info_; |
| 805 | }; |
| 806 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 807 | } // namespace |
| 808 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 809 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 810 | class URLRequestTest : public PlatformTest, public WithScopedTaskEnvironment { |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 811 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 812 | URLRequestTest() : default_context_(true) { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 813 | default_context_.set_network_delegate(&default_network_delegate_); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 814 | default_context_.set_net_log(&net_log_); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 815 | job_factory_impl_ = new URLRequestJobFactoryImpl(); |
| 816 | job_factory_.reset(job_factory_impl_); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 817 | } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 818 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 819 | ~URLRequestTest() override { |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 820 | // URLRequestJobs may post clean-up tasks on destruction. |
| 821 | base::RunLoop().RunUntilIdle(); |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 822 | |
| 823 | SetTransportSecurityStateSourceForTesting(nullptr); |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 824 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 825 | |
dcheng | 2339883c | 2014-12-23 00:23:05 | [diff] [blame] | 826 | void SetUp() override { |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 827 | SetUpFactory(); |
| 828 | default_context_.set_job_factory(job_factory_.get()); |
| 829 | default_context_.Init(); |
| 830 | PlatformTest::SetUp(); |
| 831 | } |
| 832 | |
| 833 | virtual void SetUpFactory() { |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 834 | job_factory_impl_->SetProtocolHandler( |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 835 | "data", std::make_unique<DataProtocolHandler>()); |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 836 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 837 | job_factory_impl_->SetProtocolHandler( |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 838 | "file", std::make_unique<FileProtocolHandler>( |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 839 | base::ThreadTaskRunnerHandle::Get())); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 840 | #endif |
| 841 | } |
| 842 | |
| 843 | TestNetworkDelegate* default_network_delegate() { |
| 844 | return &default_network_delegate_; |
| 845 | } |
| 846 | |
| 847 | const TestURLRequestContext& default_context() const { |
| 848 | return default_context_; |
| 849 | } |
| 850 | |
| 851 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 852 | // Adds the TestJobInterceptor to the default context. |
| 853 | TestJobInterceptor* AddTestInterceptor() { |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 854 | TestJobInterceptor* protocol_handler_ = new TestJobInterceptor(); |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 855 | job_factory_impl_->SetProtocolHandler("http", nullptr); |
| 856 | job_factory_impl_->SetProtocolHandler("http", |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 857 | base::WrapUnique(protocol_handler_)); |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 858 | return protocol_handler_; |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 859 | } |
| 860 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 861 | // Creates a temp test file and writes |data| to the file. The file will be |
| 862 | // deleted after the test completes. |
| 863 | void CreateTestFile(const char* data, |
| 864 | size_t data_size, |
| 865 | base::FilePath* test_file) { |
| 866 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 867 | // Get an absolute path since |temp_dir| can contain a symbolic link. As of |
| 868 | // now, Mac and Android bots return a path with a symbolic link. |
| 869 | base::FilePath absolute_temp_dir = |
| 870 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
| 871 | |
| 872 | ASSERT_TRUE(base::CreateTemporaryFileInDir(absolute_temp_dir, test_file)); |
| 873 | ASSERT_EQ(static_cast<int>(data_size), |
| 874 | base::WriteFile(*test_file, data, data_size)); |
| 875 | } |
| 876 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 877 | protected: |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 878 | TestNetLog net_log_; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 879 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 880 | URLRequestJobFactoryImpl* job_factory_impl_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 881 | std::unique_ptr<URLRequestJobFactory> job_factory_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 882 | TestURLRequestContext default_context_; |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 883 | base::ScopedTempDir temp_dir_; |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 884 | }; |
| 885 | |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 886 | // This NetworkDelegate is picky about what files are accessible. Only |
| 887 | // whitelisted files are allowed. |
| 888 | class CookieBlockingNetworkDelegate : public TestNetworkDelegate { |
| 889 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 890 | CookieBlockingNetworkDelegate() = default; |
| 891 | ; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 892 | |
| 893 | // Adds |directory| to the access white list. |
| 894 | void AddToWhitelist(const base::FilePath& directory) { |
| 895 | whitelist_.insert(directory); |
| 896 | } |
| 897 | |
| 898 | private: |
| 899 | // Returns true if |path| matches the white list. |
| 900 | bool OnCanAccessFileInternal(const base::FilePath& path) const { |
| 901 | for (const auto& directory : whitelist_) { |
| 902 | if (directory == path || directory.IsParent(path)) |
| 903 | return true; |
| 904 | } |
| 905 | return false; |
| 906 | } |
| 907 | |
| 908 | // Returns true only if both |original_path| and |absolute_path| match the |
| 909 | // white list. |
| 910 | bool OnCanAccessFile(const URLRequest& request, |
| 911 | const base::FilePath& original_path, |
| 912 | const base::FilePath& absolute_path) const override { |
| 913 | return (OnCanAccessFileInternal(original_path) && |
| 914 | OnCanAccessFileInternal(absolute_path)); |
| 915 | } |
| 916 | |
| 917 | std::set<base::FilePath> whitelist_; |
| 918 | |
| 919 | DISALLOW_COPY_AND_ASSIGN(CookieBlockingNetworkDelegate); |
| 920 | }; |
| 921 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 922 | TEST_F(URLRequestTest, AboutBlankTest) { |
| 923 | TestDelegate d; |
| 924 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 925 | std::unique_ptr<URLRequest> r( |
| 926 | default_context_.CreateRequest(GURL("about:blank"), DEFAULT_PRIORITY, |
| 927 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 928 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 929 | r->Start(); |
| 930 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 931 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 932 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 933 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 934 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 935 | EXPECT_FALSE(d.received_data_before_response()); |
| 936 | EXPECT_EQ(d.bytes_received(), 0); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 937 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 938 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 939 | |
| 940 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 941 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | |
| 945 | TEST_F(URLRequestTest, DataURLImageTest) { |
| 946 | TestDelegate d; |
| 947 | { |
| 948 | // Use our nice little Chrome logo. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 949 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 950 | GURL("data:image/png;base64," |
| 951 | "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUB" |
| 952 | "BjG3w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQ" |
| 953 | "xIEVxitD5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI" |
| 954 | "5MpCVdW0gO7tvNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKz" |
| 955 | "Mx1+fg9bAgK6zHq9cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7Ck" |
| 956 | "IMDxQpF7r/MWq12UctI1dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0" |
| 957 | "WYUqt57yWf2McHTObYPbVD+ZwbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2H" |
| 958 | "QQcm43XwmtoYM6vVKleh0uoWvnzW3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nk" |
| 959 | "DFHF9ZS+uYVjRUasMeHUmyLYtcklTvzWGFZnNOXczThvpKIzjcahSqIzkvDLayDq6" |
| 960 | "D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTbxXX1T5xsV9tm9r4TQwHLiZw/pdDZJ" |
| 961 | "ea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5MrVvMzNaI3+ERHfrFzPKQukrQ" |
| 962 | "GI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gVMd6q5c8GdosynKmSeRuGz" |
| 963 | "pjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq/uxxZKzNiZFGD0wRC" |
| 964 | "3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7FUx1qLnV7MGF40" |
| 965 | "smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufBJbsysjjW4" |
| 966 | "kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJMnlm2O" |
| 967 | "34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfmm" |
| 968 | "oRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV" |
| 969 | "5EnhORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 970 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 971 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 972 | r->Start(); |
| 973 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 974 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 975 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 976 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 977 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 978 | EXPECT_FALSE(d.received_data_before_response()); |
| 979 | EXPECT_EQ(d.bytes_received(), 911); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 980 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 981 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 982 | |
| 983 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 984 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 988 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 989 | TEST_F(URLRequestTest, FileTest) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 990 | const char kTestFileContent[] = "Hello"; |
| 991 | base::FilePath test_file; |
| 992 | ASSERT_NO_FATAL_FAILURE( |
| 993 | CreateTestFile(kTestFileContent, sizeof(kTestFileContent), &test_file)); |
kraush | 5a64582 | 2016-04-07 18:35:04 | [diff] [blame] | 994 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 995 | GURL test_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 996 | |
| 997 | TestDelegate d; |
| 998 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 999 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1000 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1001 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1002 | r->Start(); |
| 1003 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1004 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1005 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1006 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1007 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1008 | EXPECT_EQ(1, d.response_started_count()); |
| 1009 | EXPECT_FALSE(d.received_data_before_response()); |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1010 | EXPECT_EQ(d.bytes_received(), static_cast<int>(sizeof(kTestFileContent))); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1011 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 1012 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 1013 | |
| 1014 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1015 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1019 | TEST_F(URLRequestTest, FileTestCancel) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1020 | const char kTestFileContent[] = "Hello"; |
| 1021 | base::FilePath test_file; |
| 1022 | ASSERT_NO_FATAL_FAILURE( |
| 1023 | CreateTestFile(kTestFileContent, sizeof(kTestFileContent), &test_file)); |
| 1024 | |
| 1025 | GURL test_url = FilePathToFileURL(test_file); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1026 | |
| 1027 | TestDelegate d; |
| 1028 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1029 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1030 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1031 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1032 | r->Start(); |
| 1033 | EXPECT_TRUE(r->is_pending()); |
| 1034 | r->Cancel(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1035 | } |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 1036 | // Async cancellation should be safe even when URLRequest has been already |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1037 | // destroyed. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1038 | base::RunLoop().RunUntilIdle(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 1039 | } |
| 1040 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1041 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 1042 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1043 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1044 | FillBuffer(buffer.get(), buffer_size); |
| 1045 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1046 | base::FilePath test_file; |
| 1047 | ASSERT_NO_FATAL_FAILURE( |
| 1048 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1049 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1050 | |
| 1051 | const size_t first_byte_position = 500; |
| 1052 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 1053 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 1054 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 1055 | buffer.get() + last_byte_position + 1); |
| 1056 | |
| 1057 | TestDelegate d; |
| 1058 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1059 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1060 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1061 | |
| 1062 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1063 | headers.SetHeader( |
| 1064 | HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1065 | HttpByteRange::Bounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1066 | first_byte_position, last_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1067 | r->SetExtraRequestHeaders(headers); |
| 1068 | r->Start(); |
| 1069 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1070 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1071 | d.RunUntilComplete(); |
| 1072 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1073 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1074 | EXPECT_EQ(1, d.response_started_count()); |
| 1075 | EXPECT_FALSE(d.received_data_before_response()); |
| 1076 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 1077 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 1078 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 1079 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 1083 | const size_t buffer_size = 4000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1084 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1085 | FillBuffer(buffer.get(), buffer_size); |
| 1086 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1087 | base::FilePath test_file; |
| 1088 | ASSERT_NO_FATAL_FAILURE( |
| 1089 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1090 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1091 | |
| 1092 | const size_t first_byte_position = 500; |
| 1093 | const size_t last_byte_position = buffer_size - 1; |
| 1094 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 1095 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 1096 | buffer.get() + last_byte_position + 1); |
| 1097 | |
| 1098 | TestDelegate d; |
| 1099 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1100 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1101 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1102 | |
| 1103 | HttpRequestHeaders headers; |
| 1104 | headers.SetHeader(HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1105 | HttpByteRange::RightUnbounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1106 | first_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1107 | r->SetExtraRequestHeaders(headers); |
| 1108 | r->Start(); |
| 1109 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1110 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1111 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1112 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1113 | EXPECT_EQ(1, d.response_started_count()); |
| 1114 | EXPECT_FALSE(d.received_data_before_response()); |
| 1115 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 1116 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 1117 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 1118 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 1122 | const size_t buffer_size = 400000; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1123 | std::unique_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1124 | FillBuffer(buffer.get(), buffer_size); |
| 1125 | |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1126 | base::FilePath test_file; |
| 1127 | ASSERT_NO_FATAL_FAILURE( |
| 1128 | CreateTestFile(buffer.get(), buffer_size, &test_file)); |
| 1129 | GURL temp_url = FilePathToFileURL(test_file); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1130 | |
| 1131 | TestDelegate d; |
| 1132 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1133 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1134 | temp_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1135 | |
| 1136 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 1137 | headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1138 | r->SetExtraRequestHeaders(headers); |
| 1139 | r->Start(); |
| 1140 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1141 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1142 | d.RunUntilComplete(); |
| 1143 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1144 | EXPECT_TRUE(d.request_failed()); |
| 1145 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1146 | } |
| 1147 | |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1148 | TEST_F(URLRequestTest, AllowFileURLs) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1149 | std::string test_data("monkey"); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1150 | base::FilePath test_file; |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1151 | ASSERT_NO_FATAL_FAILURE( |
| 1152 | CreateTestFile(test_data.data(), test_data.size(), &test_file)); |
| 1153 | |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1154 | // The directory part of the path returned from CreateTemporaryFileInDir() |
| 1155 | // can be slightly different from |absolute_temp_dir| on Windows. |
| 1156 | // Example: C:\\Users\\CHROME~2 -> C:\\Users\\chrome-bot |
| 1157 | // Hence the test should use the directory name of |test_file|, rather than |
| 1158 | // |absolute_temp_dir|, for whitelisting. |
| 1159 | base::FilePath real_temp_dir = test_file.DirName(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1160 | GURL test_file_url = FilePathToFileURL(test_file); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1161 | { |
| 1162 | TestDelegate d; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1163 | CookieBlockingNetworkDelegate network_delegate; |
| 1164 | network_delegate.AddToWhitelist(real_temp_dir); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1165 | default_context_.set_network_delegate(&network_delegate); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1166 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1167 | test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1168 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1169 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1170 | // This should be allowed as the file path is whitelisted. |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1171 | EXPECT_FALSE(d.request_failed()); |
| 1172 | EXPECT_EQ(test_data, d.data_received()); |
| 1173 | } |
| 1174 | |
| 1175 | { |
| 1176 | TestDelegate d; |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1177 | CookieBlockingNetworkDelegate network_delegate; |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1178 | default_context_.set_network_delegate(&network_delegate); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1179 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1180 | test_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1181 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1182 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1183 | // This should be rejected as the file path is not whitelisted. |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1184 | EXPECT_TRUE(d.request_failed()); |
| 1185 | EXPECT_EQ("", d.data_received()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1186 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1187 | } |
| 1188 | } |
| 1189 | |
Kevin Marshall | a9f05ec | 2017-07-14 02:10:05 | [diff] [blame] | 1190 | #if defined(OS_POSIX) && !defined(OS_FUCHSIA) // Because of symbolic links. |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1191 | |
| 1192 | TEST_F(URLRequestTest, SymlinksToFiles) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1193 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1194 | // Get an absolute path since temp_dir can contain a symbolic link. |
| 1195 | base::FilePath absolute_temp_dir = |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1196 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1197 | |
| 1198 | // Create a good directory (will be whitelisted) and a good file. |
| 1199 | base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); |
| 1200 | ASSERT_TRUE(base::CreateDirectory(good_dir)); |
| 1201 | base::FilePath good_file; |
| 1202 | ASSERT_TRUE(base::CreateTemporaryFileInDir(good_dir, &good_file)); |
| 1203 | std::string good_data("good"); |
| 1204 | base::WriteFile(good_file, good_data.data(), good_data.size()); |
| 1205 | // See the comment in AllowFileURLs() for why this is done. |
| 1206 | base::FilePath real_good_dir = good_file.DirName(); |
| 1207 | |
| 1208 | // Create a bad directory (will not be whitelisted) and a bad file. |
| 1209 | base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); |
| 1210 | ASSERT_TRUE(base::CreateDirectory(bad_dir)); |
| 1211 | base::FilePath bad_file; |
| 1212 | ASSERT_TRUE(base::CreateTemporaryFileInDir(bad_dir, &bad_file)); |
| 1213 | std::string bad_data("bad"); |
| 1214 | base::WriteFile(bad_file, bad_data.data(), bad_data.size()); |
| 1215 | |
| 1216 | // This symlink will point to the good file. Access to the symlink will be |
| 1217 | // allowed as both the symlink and the destination file are in the same |
| 1218 | // good directory. |
| 1219 | base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); |
| 1220 | ASSERT_TRUE(base::CreateSymbolicLink(good_file, good_symlink)); |
| 1221 | GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1222 | // This symlink will point to the bad file. Even though the symlink is in |
| 1223 | // the good directory, access to the symlink will be rejected since it |
| 1224 | // points to the bad file. |
| 1225 | base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); |
| 1226 | ASSERT_TRUE(base::CreateSymbolicLink(bad_file, bad_symlink)); |
| 1227 | GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1228 | |
| 1229 | CookieBlockingNetworkDelegate network_delegate; |
| 1230 | network_delegate.AddToWhitelist(real_good_dir); |
| 1231 | { |
| 1232 | TestDelegate d; |
| 1233 | default_context_.set_network_delegate(&network_delegate); |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1234 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1235 | good_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1236 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1237 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1238 | // good_file_url should be allowed. |
| 1239 | EXPECT_FALSE(d.request_failed()); |
| 1240 | EXPECT_EQ(good_data, d.data_received()); |
| 1241 | } |
| 1242 | |
| 1243 | { |
| 1244 | TestDelegate d; |
| 1245 | default_context_.set_network_delegate(&network_delegate); |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1246 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1247 | bad_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1248 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1249 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1250 | // bad_file_url should be rejected. |
| 1251 | EXPECT_TRUE(d.request_failed()); |
| 1252 | EXPECT_EQ("", d.data_received()); |
| 1253 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | TEST_F(URLRequestTest, SymlinksToDirs) { |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1258 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1259 | // Get an absolute path since temp_dir can contain a symbolic link. |
| 1260 | base::FilePath absolute_temp_dir = |
Sergey Ulanov | 2e49f49 | 2017-09-14 19:37:51 | [diff] [blame] | 1261 | base::MakeAbsoluteFilePath(temp_dir_.GetPath()); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1262 | |
| 1263 | // Create a good directory (will be whitelisted). |
| 1264 | base::FilePath good_dir = absolute_temp_dir.AppendASCII("good"); |
| 1265 | ASSERT_TRUE(base::CreateDirectory(good_dir)); |
| 1266 | |
| 1267 | // Create a bad directory (will not be whitelisted). |
| 1268 | base::FilePath bad_dir = absolute_temp_dir.AppendASCII("bad"); |
| 1269 | ASSERT_TRUE(base::CreateDirectory(bad_dir)); |
| 1270 | |
| 1271 | // This symlink will point to the good directory. Access to the symlink |
| 1272 | // will be allowed as the symlink is in the good dir that'll be white |
| 1273 | // listed. |
| 1274 | base::FilePath good_symlink = good_dir.AppendASCII("good_symlink"); |
| 1275 | ASSERT_TRUE(base::CreateSymbolicLink(good_dir, good_symlink)); |
| 1276 | GURL good_file_url = FilePathToFileURL(good_symlink); |
| 1277 | // This symlink will point to the bad directory. Even though the symlink is |
| 1278 | // in the good directory, access to the symlink will be rejected since it |
| 1279 | // points to the bad directory. |
| 1280 | base::FilePath bad_symlink = good_dir.AppendASCII("bad_symlink"); |
| 1281 | ASSERT_TRUE(base::CreateSymbolicLink(bad_dir, bad_symlink)); |
| 1282 | GURL bad_file_url = FilePathToFileURL(bad_symlink); |
| 1283 | |
| 1284 | CookieBlockingNetworkDelegate network_delegate; |
| 1285 | network_delegate.AddToWhitelist(good_dir); |
| 1286 | { |
| 1287 | TestDelegate d; |
| 1288 | default_context_.set_network_delegate(&network_delegate); |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1289 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1290 | good_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1291 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1292 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1293 | // good_file_url should be allowed. |
| 1294 | EXPECT_FALSE(d.request_failed()); |
| 1295 | ASSERT_NE(d.data_received().find("good_symlink"), std::string::npos); |
| 1296 | } |
| 1297 | |
| 1298 | { |
| 1299 | TestDelegate d; |
| 1300 | default_context_.set_network_delegate(&network_delegate); |
Ramin Halavati | 91cbba34 | 2017-07-19 13:13:37 | [diff] [blame] | 1301 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1302 | bad_file_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1303 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1304 | d.RunUntilComplete(); |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1305 | // bad_file_url should be rejected. |
| 1306 | EXPECT_TRUE(d.request_failed()); |
| 1307 | EXPECT_EQ("", d.data_received()); |
| 1308 | EXPECT_EQ(ERR_ACCESS_DENIED, d.request_status()); |
| 1309 | } |
| 1310 | } |
| 1311 | |
Kevin Marshall | a9f05ec | 2017-07-14 02:10:05 | [diff] [blame] | 1312 | #endif // defined(OS_POSIX) && !defined(OS_FUCHSIA) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1313 | |
| 1314 | TEST_F(URLRequestTest, FileDirCancelTest) { |
| 1315 | // Put in mock resource provider. |
| 1316 | NetModule::SetResourceProvider(TestNetResourceProvider); |
| 1317 | |
| 1318 | TestDelegate d; |
| 1319 | { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1320 | base::FilePath file_path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1321 | base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1322 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 1323 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
| 1324 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1325 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1326 | FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d, |
| 1327 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1328 | req->Start(); |
| 1329 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1330 | |
| 1331 | d.set_cancel_in_received_data_pending(true); |
| 1332 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1333 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | // Take out mock resource provider. |
| 1337 | NetModule::SetResourceProvider(NULL); |
| 1338 | } |
| 1339 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1340 | TEST_F(URLRequestTest, FileDirOutputSanity) { |
| 1341 | // Verify the general sanity of the the output of the file: |
| 1342 | // directory lister by checking for the output of a known existing |
| 1343 | // file. |
| 1344 | const char sentinel_name[] = "filedir-sentinel"; |
| 1345 | |
| 1346 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1347 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1348 | path = path.Append(kTestFilePath); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1349 | |
| 1350 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1351 | std::unique_ptr<URLRequest> req( |
| 1352 | default_context_.CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 1353 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1354 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1355 | d.RunUntilComplete(); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1356 | |
| 1357 | // Generate entry for the sentinel file. |
| 1358 | base::FilePath sentinel_path = path.AppendASCII(sentinel_name); |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 1359 | base::File::Info info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 1360 | EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1361 | EXPECT_GT(info.size, 0); |
| 1362 | std::string sentinel_output = GetDirectoryListingEntry( |
| 1363 | base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
satorux | ddac044 | 2017-05-29 06:06:18 | [diff] [blame] | 1364 | std::string(sentinel_name), false /* is_dir */, info.size, |
| 1365 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1366 | info.last_modified); |
| 1367 | |
| 1368 | ASSERT_LT(0, d.bytes_received()); |
| 1369 | ASSERT_FALSE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1370 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1371 | // Check for the entry generated for the "sentinel" file. |
| 1372 | const std::string& data = d.data_received(); |
| 1373 | ASSERT_NE(data.find(sentinel_output), std::string::npos); |
| 1374 | } |
| 1375 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1376 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 1377 | // There is an implicit redirect when loading a file path that matches a |
| 1378 | // directory and does not end with a slash. Ensure that following such |
| 1379 | // redirects does not crash. See http://crbug.com/18686. |
| 1380 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1381 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1382 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1383 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1384 | |
| 1385 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1386 | std::unique_ptr<URLRequest> req( |
| 1387 | default_context_.CreateRequest(FilePathToFileURL(path), DEFAULT_PRIORITY, |
| 1388 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1389 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1390 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1391 | |
| 1392 | ASSERT_EQ(1, d.received_redirect_count()); |
| 1393 | ASSERT_LT(0, d.bytes_received()); |
| 1394 | ASSERT_FALSE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1395 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | #if defined(OS_WIN) |
| 1399 | // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
| 1400 | TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
| 1401 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1402 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1403 | GURL("file:///"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1404 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1405 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1406 | |
| 1407 | ASSERT_EQ(1, d.received_redirect_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1408 | EXPECT_NE(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1409 | } |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1410 | #endif // defined(OS_WIN) |
| 1411 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 1412 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1413 | |
| 1414 | TEST_F(URLRequestTest, InvalidUrlTest) { |
| 1415 | TestDelegate d; |
| 1416 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1417 | std::unique_ptr<URLRequest> r( |
| 1418 | default_context_.CreateRequest(GURL("invalid url"), DEFAULT_PRIORITY, |
| 1419 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1420 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1421 | r->Start(); |
| 1422 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1423 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1424 | d.RunUntilComplete(); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1425 | EXPECT_TRUE(d.request_failed()); |
| 1426 | } |
| 1427 | } |
| 1428 | |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1429 | TEST_F(URLRequestTest, InvalidReferrerTest) { |
| 1430 | TestURLRequestContext context; |
| 1431 | TestNetworkDelegate network_delegate; |
| 1432 | network_delegate.set_cancel_request_with_policy_violating_referrer(true); |
| 1433 | context.set_network_delegate(&network_delegate); |
| 1434 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1435 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1436 | context.CreateRequest(GURL("http://localhost/"), DEFAULT_PRIORITY, &d, |
| 1437 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1438 | req->SetReferrer("https://somewhere.com/"); |
| 1439 | |
| 1440 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1441 | d.RunUntilComplete(); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1442 | EXPECT_TRUE(d.request_failed()); |
| 1443 | } |
| 1444 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1445 | #if defined(OS_WIN) |
| 1446 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
| 1447 | base::FilePath app_path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 1448 | base::PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1449 | app_path = app_path.Append(kTestFilePath); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1450 | app_path = app_path.AppendASCII("with-headers.html"); |
| 1451 | |
| 1452 | std::wstring lnk_path = app_path.value() + L".lnk"; |
| 1453 | |
| 1454 | base::win::ScopedCOMInitializer com_initializer; |
| 1455 | |
| 1456 | // Temporarily create a shortcut for test |
| 1457 | { |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 1458 | Microsoft::WRL::ComPtr<IShellLink> shell; |
robliao | eb9bfd64 | 2017-05-18 17:35:16 | [diff] [blame] | 1459 | ASSERT_TRUE(SUCCEEDED(::CoCreateInstance( |
| 1460 | CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shell)))); |
Robert Liao | c88f99d1 | 2017-10-17 21:48:33 | [diff] [blame] | 1461 | Microsoft::WRL::ComPtr<IPersistFile> persist; |
robliao | 310fa98b | 2017-05-11 17:14:00 | [diff] [blame] | 1462 | ASSERT_TRUE(SUCCEEDED(shell.CopyTo(persist.GetAddressOf()))); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1463 | EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str()))); |
| 1464 | EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); |
| 1465 | EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE))); |
| 1466 | } |
| 1467 | |
| 1468 | TestDelegate d; |
| 1469 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1470 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1471 | FilePathToFileURL(base::FilePath(lnk_path)), DEFAULT_PRIORITY, &d, |
| 1472 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1473 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1474 | r->Start(); |
| 1475 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1476 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1477 | d.RunUntilComplete(); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1478 | |
| 1479 | WIN32_FILE_ATTRIBUTE_DATA data; |
| 1480 | GetFileAttributesEx(app_path.value().c_str(), |
| 1481 | GetFileExInfoStandard, &data); |
| 1482 | HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ, |
| 1483 | FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 1484 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 1485 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1486 | std::unique_ptr<char[]> buffer(new char[data.nFileSizeLow]); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1487 | DWORD read_size; |
| 1488 | BOOL result; |
| 1489 | result = ReadFile(file, buffer.get(), data.nFileSizeLow, |
| 1490 | &read_size, NULL); |
| 1491 | std::string content(buffer.get(), read_size); |
| 1492 | CloseHandle(file); |
| 1493 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1494 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1495 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1496 | EXPECT_EQ(content, d.data_received()); |
| 1497 | } |
| 1498 | |
| 1499 | // Clean the shortcut |
| 1500 | DeleteFile(lnk_path.c_str()); |
| 1501 | } |
| 1502 | #endif // defined(OS_WIN) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1503 | |
| 1504 | // Custom URLRequestJobs for use with interceptor tests |
| 1505 | class RestartTestJob : public URLRequestTestJob { |
| 1506 | public: |
| 1507 | RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1508 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1509 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1510 | void StartAsync() override { this->NotifyRestartRequired(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1511 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1512 | ~RestartTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1513 | }; |
| 1514 | |
| 1515 | class CancelTestJob : public URLRequestTestJob { |
| 1516 | public: |
| 1517 | explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1518 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1519 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1520 | void StartAsync() override { request_->Cancel(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1521 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1522 | ~CancelTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1523 | }; |
| 1524 | |
| 1525 | class CancelThenRestartTestJob : public URLRequestTestJob { |
| 1526 | public: |
| 1527 | explicit CancelThenRestartTestJob(URLRequest* request, |
| 1528 | NetworkDelegate* network_delegate) |
| 1529 | : URLRequestTestJob(request, network_delegate, true) { |
| 1530 | } |
| 1531 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1532 | void StartAsync() override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1533 | request_->Cancel(); |
| 1534 | this->NotifyRestartRequired(); |
| 1535 | } |
| 1536 | private: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1537 | ~CancelThenRestartTestJob() override = default; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1538 | }; |
| 1539 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1540 | // An Interceptor for use with interceptor tests. |
| 1541 | class MockURLRequestInterceptor : public URLRequestInterceptor { |
| 1542 | public: |
| 1543 | // Static getters for canned response header and data strings. |
| 1544 | static std::string ok_data() { |
| 1545 | return URLRequestTestJob::test_data_1(); |
| 1546 | } |
| 1547 | |
| 1548 | static std::string ok_headers() { |
| 1549 | return URLRequestTestJob::test_headers(); |
| 1550 | } |
| 1551 | |
| 1552 | static std::string redirect_data() { |
| 1553 | return std::string(); |
| 1554 | } |
| 1555 | |
| 1556 | static std::string redirect_headers() { |
| 1557 | return URLRequestTestJob::test_redirect_headers(); |
| 1558 | } |
| 1559 | |
| 1560 | static std::string error_data() { |
| 1561 | return std::string("ohhh nooooo mr. bill!"); |
| 1562 | } |
| 1563 | |
| 1564 | static std::string error_headers() { |
| 1565 | return URLRequestTestJob::test_error_headers(); |
| 1566 | } |
| 1567 | |
| 1568 | MockURLRequestInterceptor() |
| 1569 | : intercept_main_request_(false), restart_main_request_(false), |
| 1570 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 1571 | simulate_main_network_error_(false), |
| 1572 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 1573 | intercept_final_response_(false), cancel_final_request_(false), |
| 1574 | use_url_request_http_job_(false), |
| 1575 | did_intercept_main_(false), did_restart_main_(false), |
| 1576 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 1577 | did_simulate_error_main_(false), |
| 1578 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 1579 | did_intercept_final_(false), did_cancel_final_(false) { |
| 1580 | } |
| 1581 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 1582 | ~MockURLRequestInterceptor() override = default; |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1583 | |
| 1584 | // URLRequestInterceptor implementation: |
| 1585 | URLRequestJob* MaybeInterceptRequest( |
| 1586 | URLRequest* request, |
| 1587 | NetworkDelegate* network_delegate) const override { |
| 1588 | if (restart_main_request_) { |
| 1589 | restart_main_request_ = false; |
| 1590 | did_restart_main_ = true; |
| 1591 | return new RestartTestJob(request, network_delegate); |
| 1592 | } |
| 1593 | if (cancel_main_request_) { |
| 1594 | cancel_main_request_ = false; |
| 1595 | did_cancel_main_ = true; |
| 1596 | return new CancelTestJob(request, network_delegate); |
| 1597 | } |
| 1598 | if (cancel_then_restart_main_request_) { |
| 1599 | cancel_then_restart_main_request_ = false; |
| 1600 | did_cancel_then_restart_main_ = true; |
| 1601 | return new CancelThenRestartTestJob(request, network_delegate); |
| 1602 | } |
| 1603 | if (simulate_main_network_error_) { |
| 1604 | simulate_main_network_error_ = false; |
| 1605 | did_simulate_error_main_ = true; |
| 1606 | if (use_url_request_http_job_) { |
| 1607 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1608 | } |
| 1609 | // This job will result in error since the requested URL is not one of the |
| 1610 | // URLs supported by these tests. |
| 1611 | return new URLRequestTestJob(request, network_delegate, true); |
| 1612 | } |
| 1613 | if (!intercept_main_request_) |
| 1614 | return nullptr; |
| 1615 | intercept_main_request_ = false; |
| 1616 | did_intercept_main_ = true; |
| 1617 | URLRequestTestJob* job = new URLRequestTestJob(request, |
| 1618 | network_delegate, |
| 1619 | main_headers_, |
| 1620 | main_data_, |
| 1621 | true); |
| 1622 | job->set_load_timing_info(main_request_load_timing_info_); |
| 1623 | return job; |
| 1624 | } |
| 1625 | |
| 1626 | URLRequestJob* MaybeInterceptRedirect(URLRequest* request, |
| 1627 | NetworkDelegate* network_delegate, |
| 1628 | const GURL& location) const override { |
| 1629 | if (cancel_redirect_request_) { |
| 1630 | cancel_redirect_request_ = false; |
| 1631 | did_cancel_redirect_ = true; |
| 1632 | return new CancelTestJob(request, network_delegate); |
| 1633 | } |
| 1634 | if (!intercept_redirect_) |
| 1635 | return nullptr; |
| 1636 | intercept_redirect_ = false; |
| 1637 | did_intercept_redirect_ = true; |
| 1638 | if (use_url_request_http_job_) { |
| 1639 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1640 | } |
| 1641 | return new URLRequestTestJob(request, |
| 1642 | network_delegate, |
| 1643 | redirect_headers_, |
| 1644 | redirect_data_, |
| 1645 | true); |
| 1646 | } |
| 1647 | |
| 1648 | URLRequestJob* MaybeInterceptResponse( |
| 1649 | URLRequest* request, |
| 1650 | NetworkDelegate* network_delegate) const override { |
| 1651 | if (cancel_final_request_) { |
| 1652 | cancel_final_request_ = false; |
| 1653 | did_cancel_final_ = true; |
| 1654 | return new CancelTestJob(request, network_delegate); |
| 1655 | } |
| 1656 | if (!intercept_final_response_) |
| 1657 | return nullptr; |
| 1658 | intercept_final_response_ = false; |
| 1659 | did_intercept_final_ = true; |
| 1660 | if (use_url_request_http_job_) { |
| 1661 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1662 | } |
| 1663 | return new URLRequestTestJob(request, |
| 1664 | network_delegate, |
| 1665 | final_headers_, |
| 1666 | final_data_, |
| 1667 | true); |
| 1668 | } |
| 1669 | |
| 1670 | void set_intercept_main_request(bool intercept_main_request) { |
| 1671 | intercept_main_request_ = intercept_main_request; |
| 1672 | } |
| 1673 | |
| 1674 | void set_main_headers(const std::string& main_headers) { |
| 1675 | main_headers_ = main_headers; |
| 1676 | } |
| 1677 | |
| 1678 | void set_main_data(const std::string& main_data) { |
| 1679 | main_data_ = main_data; |
| 1680 | } |
| 1681 | |
| 1682 | void set_main_request_load_timing_info( |
| 1683 | const LoadTimingInfo& main_request_load_timing_info) { |
| 1684 | main_request_load_timing_info_ = main_request_load_timing_info; |
| 1685 | } |
| 1686 | |
| 1687 | void set_restart_main_request(bool restart_main_request) { |
| 1688 | restart_main_request_ = restart_main_request; |
| 1689 | } |
| 1690 | |
| 1691 | void set_cancel_main_request(bool cancel_main_request) { |
| 1692 | cancel_main_request_ = cancel_main_request; |
| 1693 | } |
| 1694 | |
| 1695 | void set_cancel_then_restart_main_request( |
| 1696 | bool cancel_then_restart_main_request) { |
| 1697 | cancel_then_restart_main_request_ = cancel_then_restart_main_request; |
| 1698 | } |
| 1699 | |
| 1700 | void set_simulate_main_network_error(bool simulate_main_network_error) { |
| 1701 | simulate_main_network_error_ = simulate_main_network_error; |
| 1702 | } |
| 1703 | |
| 1704 | void set_intercept_redirect(bool intercept_redirect) { |
| 1705 | intercept_redirect_ = intercept_redirect; |
| 1706 | } |
| 1707 | |
| 1708 | void set_redirect_headers(const std::string& redirect_headers) { |
| 1709 | redirect_headers_ = redirect_headers; |
| 1710 | } |
| 1711 | |
| 1712 | void set_redirect_data(const std::string& redirect_data) { |
| 1713 | redirect_data_ = redirect_data; |
| 1714 | } |
| 1715 | |
| 1716 | void set_cancel_redirect_request(bool cancel_redirect_request) { |
| 1717 | cancel_redirect_request_ = cancel_redirect_request; |
| 1718 | } |
| 1719 | |
| 1720 | void set_intercept_final_response(bool intercept_final_response) { |
| 1721 | intercept_final_response_ = intercept_final_response; |
| 1722 | } |
| 1723 | |
| 1724 | void set_final_headers(const std::string& final_headers) { |
| 1725 | final_headers_ = final_headers; |
| 1726 | } |
| 1727 | |
| 1728 | void set_final_data(const std::string& final_data) { |
| 1729 | final_data_ = final_data; |
| 1730 | } |
| 1731 | |
| 1732 | void set_cancel_final_request(bool cancel_final_request) { |
| 1733 | cancel_final_request_ = cancel_final_request; |
| 1734 | } |
| 1735 | |
| 1736 | void set_use_url_request_http_job(bool use_url_request_http_job) { |
| 1737 | use_url_request_http_job_ = use_url_request_http_job; |
| 1738 | } |
| 1739 | |
| 1740 | bool did_intercept_main() const { |
| 1741 | return did_intercept_main_; |
| 1742 | } |
| 1743 | |
| 1744 | bool did_restart_main() const { |
| 1745 | return did_restart_main_; |
| 1746 | } |
| 1747 | |
| 1748 | bool did_cancel_main() const { |
| 1749 | return did_cancel_main_; |
| 1750 | } |
| 1751 | |
| 1752 | bool did_cancel_then_restart_main() const { |
| 1753 | return did_cancel_then_restart_main_; |
| 1754 | } |
| 1755 | |
| 1756 | bool did_simulate_error_main() const { |
| 1757 | return did_simulate_error_main_; |
| 1758 | } |
| 1759 | |
| 1760 | bool did_intercept_redirect() const { |
| 1761 | return did_intercept_redirect_; |
| 1762 | } |
| 1763 | |
| 1764 | bool did_cancel_redirect() const { |
| 1765 | return did_cancel_redirect_; |
| 1766 | } |
| 1767 | |
| 1768 | bool did_intercept_final() const { |
| 1769 | return did_intercept_final_; |
| 1770 | } |
| 1771 | |
| 1772 | bool did_cancel_final() const { |
| 1773 | return did_cancel_final_; |
| 1774 | } |
| 1775 | |
| 1776 | private: |
| 1777 | // Indicate whether to intercept the main request, and if so specify the |
| 1778 | // response to return and the LoadTimingInfo to use. |
| 1779 | mutable bool intercept_main_request_; |
| 1780 | mutable std::string main_headers_; |
| 1781 | mutable std::string main_data_; |
| 1782 | mutable LoadTimingInfo main_request_load_timing_info_; |
| 1783 | |
| 1784 | // These indicate actions that can be taken within MaybeInterceptRequest. |
| 1785 | mutable bool restart_main_request_; |
| 1786 | mutable bool cancel_main_request_; |
| 1787 | mutable bool cancel_then_restart_main_request_; |
| 1788 | mutable bool simulate_main_network_error_; |
| 1789 | |
| 1790 | // Indicate whether to intercept redirects, and if so specify the response to |
| 1791 | // return. |
| 1792 | mutable bool intercept_redirect_; |
| 1793 | mutable std::string redirect_headers_; |
| 1794 | mutable std::string redirect_data_; |
| 1795 | |
| 1796 | // Cancel the request within MaybeInterceptRedirect. |
| 1797 | mutable bool cancel_redirect_request_; |
| 1798 | |
| 1799 | // Indicate whether to intercept the final response, and if so specify the |
| 1800 | // response to return. |
| 1801 | mutable bool intercept_final_response_; |
| 1802 | mutable std::string final_headers_; |
| 1803 | mutable std::string final_data_; |
| 1804 | |
| 1805 | // Cancel the final request within MaybeInterceptResponse. |
| 1806 | mutable bool cancel_final_request_; |
| 1807 | |
| 1808 | // Instruct the interceptor to use a real URLRequestHTTPJob. |
| 1809 | mutable bool use_url_request_http_job_; |
| 1810 | |
| 1811 | // These indicate if the interceptor did something or not. |
| 1812 | mutable bool did_intercept_main_; |
| 1813 | mutable bool did_restart_main_; |
| 1814 | mutable bool did_cancel_main_; |
| 1815 | mutable bool did_cancel_then_restart_main_; |
| 1816 | mutable bool did_simulate_error_main_; |
| 1817 | mutable bool did_intercept_redirect_; |
| 1818 | mutable bool did_cancel_redirect_; |
| 1819 | mutable bool did_intercept_final_; |
| 1820 | mutable bool did_cancel_final_; |
| 1821 | }; |
| 1822 | |
| 1823 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
| 1824 | class URLRequestInterceptorTest : public URLRequestTest { |
| 1825 | public: |
| 1826 | URLRequestInterceptorTest() : URLRequestTest(), interceptor_(NULL) { |
| 1827 | } |
| 1828 | |
| 1829 | ~URLRequestInterceptorTest() override { |
| 1830 | // URLRequestJobs may post clean-up tasks on destruction. |
| 1831 | base::RunLoop().RunUntilIdle(); |
| 1832 | } |
| 1833 | |
| 1834 | void SetUpFactory() override { |
| 1835 | interceptor_ = new MockURLRequestInterceptor(); |
| 1836 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1837 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | MockURLRequestInterceptor* interceptor() const { |
| 1841 | return interceptor_; |
| 1842 | } |
| 1843 | |
| 1844 | private: |
| 1845 | MockURLRequestInterceptor* interceptor_; |
| 1846 | }; |
| 1847 | |
| 1848 | TEST_F(URLRequestInterceptorTest, Intercept) { |
| 1849 | // Intercept the main request and respond with a simple response. |
| 1850 | interceptor()->set_intercept_main_request(true); |
| 1851 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1852 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1853 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1854 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1855 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1856 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1857 | base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); |
| 1858 | base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); |
| 1859 | base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); |
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 1860 | req->SetUserData(&user_data0, base::WrapUnique(user_data0)); |
avi | 0d99147 | 2017-04-27 07:04:04 | [diff] [blame] | 1861 | req->SetUserData(&user_data1, base::WrapUnique(user_data1)); |
| 1862 | req->SetUserData(&user_data2, base::WrapUnique(user_data2)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1863 | req->set_method("GET"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1864 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1865 | d.RunUntilComplete(); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1866 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1867 | // Make sure we can retrieve our specific user data. |
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 1868 | EXPECT_EQ(user_data0, req->GetUserData(&user_data0)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1869 | EXPECT_EQ(user_data1, req->GetUserData(&user_data1)); |
| 1870 | EXPECT_EQ(user_data2, req->GetUserData(&user_data2)); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1871 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1872 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1873 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1874 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1875 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1876 | EXPECT_EQ(1, d.response_started_count()); |
| 1877 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1878 | } |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1879 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1880 | TEST_F(URLRequestInterceptorTest, InterceptRedirect) { |
| 1881 | // Intercept the main request and respond with a redirect. |
| 1882 | interceptor()->set_intercept_main_request(true); |
| 1883 | interceptor()->set_main_headers( |
| 1884 | MockURLRequestInterceptor::redirect_headers()); |
| 1885 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 1886 | |
| 1887 | // Intercept that redirect and respond with a final OK response. |
| 1888 | interceptor()->set_intercept_redirect(true); |
| 1889 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 1890 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 1891 | |
| 1892 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1893 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1894 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1895 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1896 | req->set_method("GET"); |
| 1897 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1898 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1899 | |
| 1900 | // Check that the interceptor got called as expected. |
| 1901 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1902 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 1903 | |
| 1904 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1905 | int status = d.request_status(); |
| 1906 | EXPECT_EQ(OK, status); |
| 1907 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1908 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1909 | |
| 1910 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1911 | EXPECT_EQ(1, d.response_started_count()); |
| 1912 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1913 | } |
| 1914 | |
| 1915 | TEST_F(URLRequestInterceptorTest, InterceptServerError) { |
| 1916 | // Intercept the main request to generate a server error response. |
| 1917 | interceptor()->set_intercept_main_request(true); |
| 1918 | interceptor()->set_main_headers(MockURLRequestInterceptor::error_headers()); |
| 1919 | interceptor()->set_main_data(MockURLRequestInterceptor::error_data()); |
| 1920 | |
| 1921 | // Intercept that error and respond with an OK response. |
| 1922 | interceptor()->set_intercept_final_response(true); |
| 1923 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1924 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1925 | |
| 1926 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1927 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1928 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1929 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1930 | req->set_method("GET"); |
| 1931 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1932 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1933 | |
| 1934 | // Check that the interceptor got called as expected. |
| 1935 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1936 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1937 | |
| 1938 | // Check that we got one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1939 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1940 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1941 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1942 | EXPECT_EQ(1, d.response_started_count()); |
| 1943 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1944 | } |
| 1945 | |
| 1946 | TEST_F(URLRequestInterceptorTest, InterceptNetworkError) { |
| 1947 | // Intercept the main request to simulate a network error. |
| 1948 | interceptor()->set_simulate_main_network_error(true); |
| 1949 | |
| 1950 | // Intercept that error and respond with an OK response. |
| 1951 | interceptor()->set_intercept_final_response(true); |
| 1952 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1953 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1954 | |
| 1955 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1956 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1957 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1958 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1959 | req->set_method("GET"); |
| 1960 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1961 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1962 | |
| 1963 | // Check that the interceptor got called as expected. |
| 1964 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 1965 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1966 | |
| 1967 | // Check that we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1968 | EXPECT_EQ(OK, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1969 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1970 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1971 | EXPECT_EQ(1, d.response_started_count()); |
| 1972 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1973 | } |
| 1974 | |
| 1975 | TEST_F(URLRequestInterceptorTest, InterceptRestartRequired) { |
| 1976 | // Restart the main request. |
| 1977 | interceptor()->set_restart_main_request(true); |
| 1978 | |
| 1979 | // then intercept the new main request and respond with an OK response |
| 1980 | interceptor()->set_intercept_main_request(true); |
| 1981 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1982 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
| 1983 | |
| 1984 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 1985 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 1986 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 1987 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1988 | req->set_method("GET"); |
| 1989 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 1990 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1991 | |
| 1992 | // Check that the interceptor got called as expected. |
| 1993 | EXPECT_TRUE(interceptor()->did_restart_main()); |
| 1994 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1995 | |
| 1996 | // Check that we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 1997 | int status = d.request_status(); |
| 1998 | EXPECT_EQ(OK, status); |
| 1999 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2000 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 2001 | |
| 2002 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 2003 | EXPECT_EQ(1, d.response_started_count()); |
| 2004 | EXPECT_EQ(0, d.received_redirect_count()); |
| 2005 | } |
| 2006 | |
| 2007 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelMain) { |
| 2008 | // Intercept the main request and cancel from within the restarted job. |
| 2009 | interceptor()->set_cancel_main_request(true); |
| 2010 | |
| 2011 | // Set up to intercept the final response and override it with an OK response. |
| 2012 | interceptor()->set_intercept_final_response(true); |
| 2013 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2014 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 2015 | |
| 2016 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2017 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2018 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2019 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2020 | req->set_method("GET"); |
| 2021 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2022 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2023 | |
| 2024 | // Check that the interceptor got called as expected. |
| 2025 | EXPECT_TRUE(interceptor()->did_cancel_main()); |
| 2026 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2027 | |
| 2028 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2029 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelRedirect) { |
| 2033 | // Intercept the main request and respond with a redirect. |
| 2034 | interceptor()->set_intercept_main_request(true); |
| 2035 | interceptor()->set_main_headers( |
| 2036 | MockURLRequestInterceptor::redirect_headers()); |
| 2037 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 2038 | |
| 2039 | // Intercept the redirect and cancel from within that job. |
| 2040 | interceptor()->set_cancel_redirect_request(true); |
| 2041 | |
| 2042 | // Set up to intercept the final response and override it with an OK response. |
| 2043 | interceptor()->set_intercept_final_response(true); |
| 2044 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2045 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 2046 | |
| 2047 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2048 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2049 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2050 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2051 | req->set_method("GET"); |
| 2052 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2053 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2054 | |
| 2055 | // Check that the interceptor got called as expected. |
| 2056 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 2057 | EXPECT_TRUE(interceptor()->did_cancel_redirect()); |
| 2058 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2059 | |
| 2060 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2061 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelFinal) { |
| 2065 | // Intercept the main request to simulate a network error. |
| 2066 | interceptor()->set_simulate_main_network_error(true); |
| 2067 | |
| 2068 | // Set up to intercept final the response and cancel from within that job. |
| 2069 | interceptor()->set_cancel_final_request(true); |
| 2070 | |
| 2071 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2072 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2073 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2074 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2075 | req->set_method("GET"); |
| 2076 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2077 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2078 | |
| 2079 | // Check that the interceptor got called as expected. |
| 2080 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 2081 | EXPECT_TRUE(interceptor()->did_cancel_final()); |
| 2082 | |
| 2083 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2084 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelInRestart) { |
| 2088 | // Intercept the main request and cancel then restart from within that job. |
| 2089 | interceptor()->set_cancel_then_restart_main_request(true); |
| 2090 | |
| 2091 | // Set up to intercept the final response and override it with an OK response. |
| 2092 | interceptor()->set_intercept_final_response(true); |
bengr | b50d631e | 2014-11-17 22:50:50 | [diff] [blame] | 2093 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 2094 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2095 | |
| 2096 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2097 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2098 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2099 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2100 | req->set_method("GET"); |
| 2101 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2102 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2103 | |
| 2104 | // Check that the interceptor got called as expected. |
| 2105 | EXPECT_TRUE(interceptor()->did_cancel_then_restart_main()); |
| 2106 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 2107 | |
| 2108 | // Check that we see a canceled request. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2109 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not |
| 2113 | // reused. |connect_time_flags| is used to indicate if there should be dns |
| 2114 | // or SSL times, and |used_proxy| is used for proxy times. |
| 2115 | LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now, |
| 2116 | int connect_time_flags, |
| 2117 | bool used_proxy) { |
| 2118 | LoadTimingInfo load_timing; |
| 2119 | load_timing.socket_log_id = 1; |
| 2120 | |
| 2121 | if (used_proxy) { |
| 2122 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 2123 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 2124 | } |
| 2125 | |
| 2126 | LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing; |
| 2127 | if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) { |
| 2128 | connect_timing.dns_start = now + base::TimeDelta::FromDays(3); |
| 2129 | connect_timing.dns_end = now + base::TimeDelta::FromDays(4); |
| 2130 | } |
| 2131 | connect_timing.connect_start = now + base::TimeDelta::FromDays(5); |
| 2132 | if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) { |
| 2133 | connect_timing.ssl_start = now + base::TimeDelta::FromDays(6); |
| 2134 | connect_timing.ssl_end = now + base::TimeDelta::FromDays(7); |
| 2135 | } |
| 2136 | connect_timing.connect_end = now + base::TimeDelta::FromDays(8); |
| 2137 | |
| 2138 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 2139 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 2140 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 2141 | return load_timing; |
| 2142 | } |
| 2143 | |
| 2144 | // Same as above, but in the case of a reused socket. |
| 2145 | LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now, |
| 2146 | bool used_proxy) { |
| 2147 | LoadTimingInfo load_timing; |
| 2148 | load_timing.socket_log_id = 1; |
| 2149 | load_timing.socket_reused = true; |
| 2150 | |
| 2151 | if (used_proxy) { |
| 2152 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 2153 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 2154 | } |
| 2155 | |
| 2156 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 2157 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 2158 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 2159 | return load_timing; |
| 2160 | } |
| 2161 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2162 | LoadTimingInfo RunURLRequestInterceptorLoadTimingTest( |
| 2163 | const LoadTimingInfo& job_load_timing, |
| 2164 | const URLRequestContext& context, |
| 2165 | MockURLRequestInterceptor* interceptor) { |
| 2166 | interceptor->set_intercept_main_request(true); |
| 2167 | interceptor->set_main_request_load_timing_info(job_load_timing); |
| 2168 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2169 | std::unique_ptr<URLRequest> req( |
| 2170 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 2171 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2172 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2173 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2174 | |
| 2175 | LoadTimingInfo resulting_load_timing; |
| 2176 | req->GetLoadTimingInfo(&resulting_load_timing); |
| 2177 | |
| 2178 | // None of these should be modified by the URLRequest. |
| 2179 | EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); |
| 2180 | EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); |
| 2181 | EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); |
| 2182 | EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); |
| 2183 | EXPECT_EQ(job_load_timing.receive_headers_end, |
| 2184 | resulting_load_timing.receive_headers_end); |
caseq | e8340bc9 | 2016-04-20 00:02:57 | [diff] [blame] | 2185 | EXPECT_EQ(job_load_timing.push_start, resulting_load_timing.push_start); |
| 2186 | EXPECT_EQ(job_load_timing.push_end, resulting_load_timing.push_end); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2187 | |
| 2188 | return resulting_load_timing; |
| 2189 | } |
| 2190 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2191 | // Basic test that the intercept + load timing tests work. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2192 | TEST_F(URLRequestInterceptorTest, InterceptLoadTiming) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2193 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2194 | LoadTimingInfo job_load_timing = |
| 2195 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); |
| 2196 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2197 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2198 | RunURLRequestInterceptorLoadTimingTest( |
| 2199 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2200 | |
| 2201 | // Nothing should have been changed by the URLRequest. |
| 2202 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 2203 | load_timing_result.proxy_resolve_start); |
| 2204 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 2205 | load_timing_result.proxy_resolve_end); |
| 2206 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 2207 | load_timing_result.connect_timing.dns_start); |
| 2208 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 2209 | load_timing_result.connect_timing.dns_end); |
| 2210 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 2211 | load_timing_result.connect_timing.connect_start); |
| 2212 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 2213 | load_timing_result.connect_timing.connect_end); |
| 2214 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 2215 | load_timing_result.connect_timing.ssl_start); |
| 2216 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 2217 | load_timing_result.connect_timing.ssl_end); |
| 2218 | |
| 2219 | // Redundant sanity check. |
| 2220 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2221 | } |
| 2222 | |
| 2223 | // Another basic test, with proxy and SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2224 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2225 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2226 | LoadTimingInfo job_load_timing = |
| 2227 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); |
| 2228 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2229 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2230 | RunURLRequestInterceptorLoadTimingTest( |
| 2231 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2232 | |
| 2233 | // Nothing should have been changed by the URLRequest. |
| 2234 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 2235 | load_timing_result.proxy_resolve_start); |
| 2236 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 2237 | load_timing_result.proxy_resolve_end); |
| 2238 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 2239 | load_timing_result.connect_timing.dns_start); |
| 2240 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 2241 | load_timing_result.connect_timing.dns_end); |
| 2242 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 2243 | load_timing_result.connect_timing.connect_start); |
| 2244 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 2245 | load_timing_result.connect_timing.connect_end); |
| 2246 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 2247 | load_timing_result.connect_timing.ssl_start); |
| 2248 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 2249 | load_timing_result.connect_timing.ssl_end); |
| 2250 | |
| 2251 | // Redundant sanity check. |
| 2252 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2253 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 2254 | } |
| 2255 | |
| 2256 | // Make sure that URLRequest correctly adjusts proxy times when they're before |
| 2257 | // |request_start|, due to already having a connected socket. This happens in |
[email protected] | cf4cae3 | 2014-05-27 00:39:10 | [diff] [blame] | 2258 | // the case of reusing a SPDY session. The connected socket is not considered |
| 2259 | // reused in this test (May be a preconnect). |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2260 | // |
| 2261 | // 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] | 2262 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyProxyResolution) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2263 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2264 | LoadTimingInfo job_load_timing = |
| 2265 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); |
| 2266 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); |
| 2267 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); |
| 2268 | job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); |
| 2269 | job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); |
| 2270 | job_load_timing.connect_timing.connect_start = |
| 2271 | now - base::TimeDelta::FromDays(2); |
| 2272 | job_load_timing.connect_timing.connect_end = |
| 2273 | now - base::TimeDelta::FromDays(1); |
| 2274 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2275 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2276 | RunURLRequestInterceptorLoadTimingTest( |
| 2277 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2278 | |
| 2279 | // Proxy times, connect times, and DNS times should all be replaced with |
| 2280 | // request_start. |
| 2281 | EXPECT_EQ(load_timing_result.request_start, |
| 2282 | load_timing_result.proxy_resolve_start); |
| 2283 | EXPECT_EQ(load_timing_result.request_start, |
| 2284 | load_timing_result.proxy_resolve_end); |
| 2285 | EXPECT_EQ(load_timing_result.request_start, |
| 2286 | load_timing_result.connect_timing.dns_start); |
| 2287 | EXPECT_EQ(load_timing_result.request_start, |
| 2288 | load_timing_result.connect_timing.dns_end); |
| 2289 | EXPECT_EQ(load_timing_result.request_start, |
| 2290 | load_timing_result.connect_timing.connect_start); |
| 2291 | EXPECT_EQ(load_timing_result.request_start, |
| 2292 | load_timing_result.connect_timing.connect_end); |
| 2293 | |
| 2294 | // Other times should have been left null. |
| 2295 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2296 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 2297 | } |
| 2298 | |
| 2299 | // Same as above, but in the reused case. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2300 | TEST_F(URLRequestInterceptorTest, |
| 2301 | InterceptLoadTimingEarlyProxyResolutionReused) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2302 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2303 | LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); |
| 2304 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); |
| 2305 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); |
| 2306 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2307 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2308 | RunURLRequestInterceptorLoadTimingTest( |
| 2309 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2310 | |
| 2311 | // Proxy times and connect times should all be replaced with request_start. |
| 2312 | EXPECT_EQ(load_timing_result.request_start, |
| 2313 | load_timing_result.proxy_resolve_start); |
| 2314 | EXPECT_EQ(load_timing_result.request_start, |
| 2315 | load_timing_result.proxy_resolve_end); |
| 2316 | |
| 2317 | // Other times should have been left null. |
| 2318 | TestLoadTimingReusedWithProxy(load_timing_result); |
| 2319 | } |
| 2320 | |
| 2321 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2322 | // |request_start|, due to reusing a connected socket. The connected socket is |
| 2323 | // not considered reused in this test (May be a preconnect). |
| 2324 | // |
| 2325 | // To mix things up, the request has SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2326 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnect) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2327 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2328 | LoadTimingInfo job_load_timing = |
| 2329 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); |
| 2330 | job_load_timing.connect_timing.connect_start = |
| 2331 | now - base::TimeDelta::FromDays(1); |
| 2332 | job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); |
| 2333 | job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); |
| 2334 | job_load_timing.connect_timing.connect_end = |
| 2335 | now - base::TimeDelta::FromDays(4); |
| 2336 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2337 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2338 | RunURLRequestInterceptorLoadTimingTest( |
| 2339 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2340 | |
| 2341 | // Connect times, and SSL times should be replaced with request_start. |
| 2342 | EXPECT_EQ(load_timing_result.request_start, |
| 2343 | load_timing_result.connect_timing.connect_start); |
| 2344 | EXPECT_EQ(load_timing_result.request_start, |
| 2345 | load_timing_result.connect_timing.ssl_start); |
| 2346 | EXPECT_EQ(load_timing_result.request_start, |
| 2347 | load_timing_result.connect_timing.ssl_end); |
| 2348 | EXPECT_EQ(load_timing_result.request_start, |
| 2349 | load_timing_result.connect_timing.connect_end); |
| 2350 | |
| 2351 | // Other times should have been left null. |
| 2352 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); |
| 2353 | } |
| 2354 | |
| 2355 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2356 | // |request_start|, due to reusing a connected socket in the case that there |
| 2357 | // are also proxy times. The connected socket is not considered reused in this |
| 2358 | // test (May be a preconnect). |
| 2359 | // |
| 2360 | // In this test, there are no SSL or DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2361 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnectWithProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2362 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2363 | LoadTimingInfo job_load_timing = |
| 2364 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); |
| 2365 | job_load_timing.connect_timing.connect_start = |
| 2366 | now - base::TimeDelta::FromDays(1); |
| 2367 | job_load_timing.connect_timing.connect_end = |
| 2368 | now - base::TimeDelta::FromDays(2); |
| 2369 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2370 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2371 | RunURLRequestInterceptorLoadTimingTest( |
| 2372 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2373 | |
| 2374 | // Connect times should be replaced with proxy_resolve_end. |
| 2375 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2376 | load_timing_result.connect_timing.connect_start); |
| 2377 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2378 | load_timing_result.connect_timing.connect_end); |
| 2379 | |
| 2380 | // Other times should have been left null. |
| 2381 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2382 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 2383 | } |
| 2384 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2385 | // Check that two different URL requests have different identifiers. |
| 2386 | TEST_F(URLRequestTest, Identifiers) { |
| 2387 | TestDelegate d; |
| 2388 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2389 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2390 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2391 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2392 | std::unique_ptr<URLRequest> other_req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2393 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2394 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2395 | |
mmenke | 19378d2 | 2014-09-09 04:12:59 | [diff] [blame] | 2396 | ASSERT_NE(req->identifier(), other_req->identifier()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2397 | } |
| 2398 | |
blundell | b8163592f | 2015-12-16 14:22:42 | [diff] [blame] | 2399 | #if defined(OS_IOS) |
| 2400 | // TODO(droger): Check that a failure to connect to the proxy is reported to |
| 2401 | // the network delegate. crbug.com/496743 |
| 2402 | #define MAYBE_NetworkDelegateProxyError DISABLED_NetworkDelegateProxyError |
| 2403 | #else |
| 2404 | #define MAYBE_NetworkDelegateProxyError NetworkDelegateProxyError |
| 2405 | #endif |
| 2406 | TEST_F(URLRequestTest, MAYBE_NetworkDelegateProxyError) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2407 | MockHostResolver host_resolver; |
| 2408 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 2409 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2410 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2411 | TestURLRequestContextWithProxy context("myproxy:70", &network_delegate); |
| 2412 | |
| 2413 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2414 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2415 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d, |
| 2416 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2417 | req->set_method("GET"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2418 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2419 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2420 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2421 | |
| 2422 | // Check we see a failed request. |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 2423 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 2424 | EXPECT_FALSE(req->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 2425 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2426 | |
| 2427 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2428 | EXPECT_THAT(network_delegate.last_error(), |
| 2429 | IsError(ERR_PROXY_CONNECTION_FAILED)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2430 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2431 | } |
| 2432 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2433 | // Make sure that NetworkDelegate::NotifyCompleted is called if |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2434 | // content is empty. |
| 2435 | TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) { |
| 2436 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2437 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2438 | GURL("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2439 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2440 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2441 | EXPECT_EQ("", d.data_received()); |
| 2442 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 2443 | } |
| 2444 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2445 | // Make sure that SetPriority actually sets the URLRequest's priority |
| 2446 | // correctly, both before and after start. |
| 2447 | TEST_F(URLRequestTest, SetPriorityBasic) { |
| 2448 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2449 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2450 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2451 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2452 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2453 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2454 | req->SetPriority(LOW); |
| 2455 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2456 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2457 | req->Start(); |
| 2458 | EXPECT_EQ(LOW, 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(MEDIUM); |
| 2461 | EXPECT_EQ(MEDIUM, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | // Make sure that URLRequest calls SetPriority on a job before calling |
| 2465 | // Start on it. |
| 2466 | TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) { |
| 2467 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2468 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2469 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2470 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2471 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2472 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2473 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2474 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2475 | req.get(), &default_network_delegate_, &job_priority)); |
| 2476 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
| 2477 | EXPECT_EQ(DEFAULT_PRIORITY, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2478 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2479 | req->SetPriority(LOW); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2480 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2481 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2482 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2483 | } |
| 2484 | |
| 2485 | // Make sure that URLRequest passes on its priority updates to its |
| 2486 | // job. |
| 2487 | TEST_F(URLRequestTest, SetJobPriority) { |
| 2488 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2489 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2490 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, |
| 2491 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2492 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2493 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2494 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2495 | req.get(), &default_network_delegate_, &job_priority)); |
| 2496 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2497 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2498 | req->SetPriority(LOW); |
| 2499 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2500 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2501 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2502 | req->SetPriority(MEDIUM); |
| 2503 | EXPECT_EQ(MEDIUM, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2504 | EXPECT_EQ(MEDIUM, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2505 | } |
| 2506 | |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2507 | // Setting the IGNORE_LIMITS load flag should be okay if the priority |
| 2508 | // is MAXIMUM_PRIORITY. |
| 2509 | TEST_F(URLRequestTest, PriorityIgnoreLimits) { |
| 2510 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2511 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2512 | GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY, &d, |
| 2513 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2514 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2515 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2516 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2517 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2518 | req.get(), &default_network_delegate_, &job_priority)); |
| 2519 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2520 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2521 | req->SetLoadFlags(LOAD_IGNORE_LIMITS); |
| 2522 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2523 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2524 | req->SetPriority(MAXIMUM_PRIORITY); |
| 2525 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[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->Start(); |
| 2528 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2529 | EXPECT_EQ(MAXIMUM_PRIORITY, job_priority); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2530 | } |
| 2531 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2532 | namespace { |
| 2533 | |
| 2534 | // Less verbose way of running a simple testserver for the tests below. |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2535 | class HttpTestServer : public EmbeddedTestServer { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2536 | public: |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2537 | explicit HttpTestServer(const base::FilePath& document_root) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2538 | AddDefaultHandlers(document_root); |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2539 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2540 | |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2541 | HttpTestServer() { AddDefaultHandlers(base::FilePath()); } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2542 | }; |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2543 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2544 | } // namespace |
| 2545 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2546 | TEST_F(URLRequestTest, DelayedCookieCallback) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2547 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2548 | ASSERT_TRUE(test_server.Start()); |
| 2549 | |
| 2550 | TestURLRequestContext context; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2551 | std::unique_ptr<DelayedCookieMonster> delayed_cm(new DelayedCookieMonster()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2552 | context.set_cookie_store(delayed_cm.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2553 | |
| 2554 | // Set up a cookie. |
| 2555 | { |
| 2556 | TestNetworkDelegate network_delegate; |
| 2557 | context.set_network_delegate(&network_delegate); |
| 2558 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2559 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2560 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2561 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2562 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2563 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2564 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2565 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2566 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2567 | } |
| 2568 | |
| 2569 | // Verify that the cookie is set. |
| 2570 | { |
| 2571 | TestNetworkDelegate network_delegate; |
| 2572 | context.set_network_delegate(&network_delegate); |
| 2573 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2574 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2575 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2576 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2577 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2578 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2579 | |
| 2580 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2581 | != std::string::npos); |
| 2582 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2583 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2584 | } |
| 2585 | } |
| 2586 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2587 | TEST_F(URLRequestTest, DoNotSendCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2588 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2589 | ASSERT_TRUE(test_server.Start()); |
| 2590 | |
| 2591 | // Set up a cookie. |
| 2592 | { |
| 2593 | TestNetworkDelegate network_delegate; |
| 2594 | default_context_.set_network_delegate(&network_delegate); |
| 2595 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2596 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2597 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2598 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2599 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2600 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2601 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2602 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2603 | } |
| 2604 | |
| 2605 | // Verify that the cookie is set. |
| 2606 | { |
| 2607 | TestNetworkDelegate network_delegate; |
| 2608 | default_context_.set_network_delegate(&network_delegate); |
| 2609 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2610 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2611 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2612 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2613 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2614 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2615 | |
| 2616 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2617 | != std::string::npos); |
| 2618 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2619 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2620 | } |
| 2621 | |
| 2622 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 2623 | { |
| 2624 | TestNetworkDelegate network_delegate; |
| 2625 | default_context_.set_network_delegate(&network_delegate); |
| 2626 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2627 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2628 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2629 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2630 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
| 2631 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2632 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2633 | |
| 2634 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2635 | == std::string::npos); |
| 2636 | |
| 2637 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
| 2638 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2639 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2640 | } |
| 2641 | } |
| 2642 | |
| 2643 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2644 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2645 | ASSERT_TRUE(test_server.Start()); |
| 2646 | |
| 2647 | // Set up a cookie. |
| 2648 | { |
| 2649 | TestNetworkDelegate network_delegate; |
| 2650 | default_context_.set_network_delegate(&network_delegate); |
| 2651 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2652 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2653 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2654 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2655 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2656 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2657 | |
| 2658 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2659 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2660 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2661 | } |
| 2662 | |
| 2663 | // Try to set-up another cookie and update the previous cookie. |
| 2664 | { |
| 2665 | TestNetworkDelegate network_delegate; |
| 2666 | default_context_.set_network_delegate(&network_delegate); |
| 2667 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2668 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2669 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2670 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2671 | req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES); |
| 2672 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2673 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2674 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2675 | |
| 2676 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
| 2677 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2678 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2679 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2680 | } |
| 2681 | |
| 2682 | // Verify the cookies weren't saved or updated. |
| 2683 | { |
| 2684 | TestNetworkDelegate network_delegate; |
| 2685 | default_context_.set_network_delegate(&network_delegate); |
| 2686 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2687 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2688 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2689 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2690 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2691 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2692 | |
| 2693 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2694 | == std::string::npos); |
| 2695 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2696 | != std::string::npos); |
| 2697 | |
| 2698 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2699 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2700 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2701 | } |
| 2702 | } |
| 2703 | |
| 2704 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2705 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2706 | ASSERT_TRUE(test_server.Start()); |
| 2707 | |
| 2708 | // Set up a cookie. |
| 2709 | { |
| 2710 | TestNetworkDelegate network_delegate; |
| 2711 | default_context_.set_network_delegate(&network_delegate); |
| 2712 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2713 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2714 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2715 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2716 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2717 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2718 | |
| 2719 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2720 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2721 | } |
| 2722 | |
| 2723 | // Verify that the cookie is set. |
| 2724 | { |
| 2725 | TestNetworkDelegate network_delegate; |
| 2726 | default_context_.set_network_delegate(&network_delegate); |
| 2727 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2728 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2729 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2730 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2731 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2732 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2733 | |
| 2734 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2735 | != std::string::npos); |
| 2736 | |
| 2737 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2738 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2739 | } |
| 2740 | |
| 2741 | // Verify that the cookie isn't sent. |
| 2742 | { |
| 2743 | TestNetworkDelegate network_delegate; |
| 2744 | default_context_.set_network_delegate(&network_delegate); |
| 2745 | TestDelegate d; |
| 2746 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2747 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2748 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2749 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2750 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2751 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2752 | |
| 2753 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2754 | == std::string::npos); |
| 2755 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2756 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2757 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2758 | } |
| 2759 | } |
| 2760 | |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 2761 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 2762 | #if defined(OS_IOS) |
| 2763 | #define MAYBE_DoNotSaveCookies_ViaPolicy FLAKY_DoNotSaveCookies_ViaPolicy |
| 2764 | #else |
| 2765 | #define MAYBE_DoNotSaveCookies_ViaPolicy DoNotSaveCookies_ViaPolicy |
| 2766 | #endif |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2767 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2768 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2769 | ASSERT_TRUE(test_server.Start()); |
| 2770 | |
| 2771 | // Set up a cookie. |
| 2772 | { |
| 2773 | TestNetworkDelegate network_delegate; |
| 2774 | default_context_.set_network_delegate(&network_delegate); |
| 2775 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2776 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2777 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2778 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2779 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2780 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2781 | |
| 2782 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2783 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2784 | } |
| 2785 | |
| 2786 | // Try to set-up another cookie and update the previous cookie. |
| 2787 | { |
| 2788 | TestNetworkDelegate network_delegate; |
| 2789 | default_context_.set_network_delegate(&network_delegate); |
| 2790 | TestDelegate d; |
| 2791 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2792 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2793 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2794 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2795 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2796 | |
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(2, network_delegate.blocked_set_cookie_count()); |
| 2801 | } |
| 2802 | |
| 2803 | // Verify the cookies weren't saved or updated. |
| 2804 | { |
| 2805 | TestNetworkDelegate network_delegate; |
| 2806 | default_context_.set_network_delegate(&network_delegate); |
| 2807 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2808 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2809 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2810 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2811 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2812 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2813 | |
| 2814 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2815 | == std::string::npos); |
| 2816 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2817 | != std::string::npos); |
| 2818 | |
| 2819 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2820 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2821 | } |
| 2822 | } |
| 2823 | |
| 2824 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2825 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2826 | ASSERT_TRUE(test_server.Start()); |
| 2827 | |
| 2828 | // Set up an empty cookie. |
| 2829 | { |
| 2830 | TestNetworkDelegate network_delegate; |
| 2831 | default_context_.set_network_delegate(&network_delegate); |
| 2832 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2833 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2834 | test_server.GetURL("/set-cookie"), DEFAULT_PRIORITY, &d, |
| 2835 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2836 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2837 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2838 | |
| 2839 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2840 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2841 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2842 | } |
| 2843 | } |
| 2844 | |
| 2845 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2846 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2847 | ASSERT_TRUE(test_server.Start()); |
| 2848 | |
| 2849 | // Set up a cookie. |
| 2850 | { |
| 2851 | TestNetworkDelegate network_delegate; |
| 2852 | default_context_.set_network_delegate(&network_delegate); |
| 2853 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2854 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2855 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2856 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2857 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2858 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2859 | |
| 2860 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2861 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2862 | } |
| 2863 | |
| 2864 | // Verify that the cookie is set. |
| 2865 | { |
| 2866 | TestNetworkDelegate network_delegate; |
| 2867 | default_context_.set_network_delegate(&network_delegate); |
| 2868 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2869 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2870 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2871 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2872 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2873 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2874 | |
| 2875 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2876 | != std::string::npos); |
| 2877 | |
| 2878 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2879 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2880 | } |
| 2881 | |
| 2882 | // Verify that the cookie isn't sent. |
| 2883 | { |
| 2884 | TestNetworkDelegate network_delegate; |
| 2885 | default_context_.set_network_delegate(&network_delegate); |
| 2886 | TestDelegate d; |
| 2887 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2888 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2889 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2890 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2891 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2892 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2893 | |
| 2894 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2895 | == std::string::npos); |
| 2896 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2897 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2898 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2899 | } |
| 2900 | } |
| 2901 | |
| 2902 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2903 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2904 | ASSERT_TRUE(test_server.Start()); |
| 2905 | |
| 2906 | // Set up a cookie. |
| 2907 | { |
| 2908 | TestNetworkDelegate network_delegate; |
| 2909 | default_context_.set_network_delegate(&network_delegate); |
| 2910 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2911 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2912 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2913 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2914 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2915 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2916 | |
| 2917 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2918 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2919 | } |
| 2920 | |
| 2921 | // Try to set-up another cookie and update the previous cookie. |
| 2922 | { |
| 2923 | TestNetworkDelegate network_delegate; |
| 2924 | default_context_.set_network_delegate(&network_delegate); |
| 2925 | TestDelegate d; |
| 2926 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2927 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2928 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2929 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2930 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2931 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2932 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2933 | |
| 2934 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2935 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2936 | } |
| 2937 | |
| 2938 | // Verify the cookies weren't saved or updated. |
| 2939 | { |
| 2940 | TestNetworkDelegate network_delegate; |
| 2941 | default_context_.set_network_delegate(&network_delegate); |
| 2942 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2943 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2944 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2945 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2946 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2947 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2948 | |
| 2949 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2950 | == std::string::npos); |
| 2951 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2952 | != std::string::npos); |
| 2953 | |
| 2954 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2955 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2956 | } |
| 2957 | } |
| 2958 | |
mkwst | c5fa776 | 2016-03-28 09:28:23 | [diff] [blame] | 2959 | TEST_F(URLRequestTest, SameSiteCookies) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 2960 | HttpTestServer test_server; |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2961 | ASSERT_TRUE(test_server.Start()); |
| 2962 | |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2963 | TestNetworkDelegate network_delegate; |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 2964 | default_context_.set_network_delegate(&network_delegate); |
| 2965 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2966 | const std::string kHost = "example.test"; |
| 2967 | const std::string kSubHost = "subdomain.example.test"; |
| 2968 | const std::string kCrossHost = "cross-origin.test"; |
| 2969 | |
| 2970 | // Set up two 'SameSite' cookies on 'example.test' |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2971 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2972 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2973 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2974 | test_server.GetURL(kHost, |
| 2975 | "/set-cookie?StrictSameSiteCookie=1;SameSite=Strict&" |
| 2976 | "LaxSameSiteCookie=1;SameSite=Lax"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2977 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2978 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2979 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2980 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2981 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2982 | EXPECT_EQ(2, network_delegate.set_cookie_count()); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2983 | } |
| 2984 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2985 | // Verify that both cookies are sent for same-site requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2986 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2987 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 2988 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 2989 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 2990 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 2991 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 2992 | req->set_initiator(url::Origin::Create(test_server.GetURL(kHost, "/"))); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2993 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 2994 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2995 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2996 | EXPECT_NE(std::string::npos, |
| 2997 | d.data_received().find("StrictSameSiteCookie=1")); |
| 2998 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2999 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3000 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3001 | } |
| 3002 | |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3003 | // Verify that both cookies are sent when the request has no initiator (can |
| 3004 | // happen for main frame browser-initiated navigations). |
| 3005 | { |
| 3006 | TestDelegate d; |
| 3007 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3008 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3009 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3010 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3011 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3012 | d.RunUntilComplete(); |
clamy | fd58ca07 | 2016-12-21 17:18:32 | [diff] [blame] | 3013 | |
| 3014 | EXPECT_NE(std::string::npos, |
| 3015 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3016 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 3017 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3018 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3019 | } |
| 3020 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3021 | // Verify that both cookies are sent for same-registrable-domain requests. |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3022 | { |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3023 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3024 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3025 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3026 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3027 | req->set_site_for_cookies(test_server.GetURL(kSubHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3028 | req->set_initiator(url::Origin::Create(test_server.GetURL(kSubHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3029 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3030 | d.RunUntilComplete(); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3031 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3032 | EXPECT_NE(std::string::npos, |
| 3033 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3034 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3035 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3036 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3037 | } |
| 3038 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3039 | // Verify that neither cookie is not sent for cross-site requests. |
| 3040 | { |
| 3041 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3042 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3043 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3044 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3045 | req->set_site_for_cookies(test_server.GetURL(kCrossHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3046 | req->set_initiator( |
| 3047 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3048 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3049 | d.RunUntilComplete(); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3050 | |
| 3051 | EXPECT_EQ(std::string::npos, |
| 3052 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3053 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
| 3054 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3055 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3056 | } |
| 3057 | |
| 3058 | // Verify that the lax cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3059 | // method is "safe". |
| 3060 | { |
| 3061 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3062 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3063 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3064 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3065 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3066 | req->set_initiator( |
| 3067 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3068 | req->set_method("GET"); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3069 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3070 | d.RunUntilComplete(); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3071 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3072 | EXPECT_EQ(std::string::npos, |
| 3073 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3074 | EXPECT_NE(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3075 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3076 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3077 | } |
| 3078 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3079 | // Verify that neither cookie is sent for cross-site initiators when the |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3080 | // method is unsafe (e.g. POST). |
| 3081 | { |
| 3082 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3083 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3084 | test_server.GetURL(kHost, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3085 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 3086 | req->set_site_for_cookies(test_server.GetURL(kHost, "/")); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 3087 | req->set_initiator( |
| 3088 | url::Origin::Create(test_server.GetURL(kCrossHost, "/"))); |
mkwst | 202534e3 | 2016-01-15 16:07:15 | [diff] [blame] | 3089 | req->set_method("POST"); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3090 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3091 | d.RunUntilComplete(); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3092 | |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 3093 | EXPECT_EQ(std::string::npos, |
| 3094 | d.data_received().find("StrictSameSiteCookie=1")); |
| 3095 | EXPECT_EQ(std::string::npos, d.data_received().find("LaxSameSiteCookie=1")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 3096 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3097 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3098 | } |
| 3099 | } |
| 3100 | |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3101 | // Tests that __Secure- cookies can't be set on non-secure origins. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3102 | TEST_F(URLRequestTest, SecureCookiePrefixOnNonsecureOrigin) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3103 | EmbeddedTestServer http_server; |
| 3104 | http_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3105 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3106 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3107 | https_server.AddDefaultHandlers( |
| 3108 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3109 | ASSERT_TRUE(http_server.Start()); |
| 3110 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3111 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3112 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3113 | TestURLRequestContext context(true); |
| 3114 | context.set_network_delegate(&network_delegate); |
| 3115 | context.Init(); |
| 3116 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3117 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3118 | { |
| 3119 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3120 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3121 | http_server.GetURL("/set-cookie?__Secure-nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3122 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3123 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3124 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3125 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3126 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3127 | } |
| 3128 | |
| 3129 | // Verify that the cookie is not set. |
| 3130 | { |
| 3131 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3132 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3133 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3134 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3135 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3136 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3137 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3138 | EXPECT_EQ(d.data_received().find("__Secure-nonsecure-origin=1"), |
| 3139 | std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3140 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3141 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3142 | } |
| 3143 | } |
| 3144 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3145 | TEST_F(URLRequestTest, SecureCookiePrefixNonsecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3146 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3147 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3148 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3149 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3150 | |
| 3151 | TestNetworkDelegate network_delegate; |
| 3152 | TestURLRequestContext context(true); |
| 3153 | context.set_network_delegate(&network_delegate); |
| 3154 | context.Init(); |
| 3155 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3156 | // Try to set a non-Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3157 | { |
| 3158 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3159 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 3160 | https_server.GetURL("/set-cookie?__Secure-foo=1"), DEFAULT_PRIORITY, &d, |
| 3161 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3162 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3163 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3164 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3165 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3166 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3167 | } |
| 3168 | |
| 3169 | // Verify that the cookie is not set. |
| 3170 | { |
| 3171 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3172 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3173 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3174 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3175 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3176 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3177 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3178 | EXPECT_EQ(d.data_received().find("__Secure-foo=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3179 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3180 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3181 | } |
| 3182 | } |
| 3183 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3184 | TEST_F(URLRequestTest, SecureCookiePrefixSecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3185 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3186 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3187 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3188 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3189 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3190 | TestNetworkDelegate network_delegate; |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3191 | TestURLRequestContext context(true); |
| 3192 | context.set_network_delegate(&network_delegate); |
| 3193 | context.Init(); |
| 3194 | |
estark | b15166b | 2015-12-18 16:56:38 | [diff] [blame] | 3195 | // Try to set a Secure __Secure- cookie. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3196 | { |
| 3197 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3198 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 3199 | https_server.GetURL("/set-cookie?__Secure-bar=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3200 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3201 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3202 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3203 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3204 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3205 | } |
| 3206 | |
| 3207 | // Verify that the cookie is set. |
| 3208 | { |
| 3209 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3210 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3211 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3212 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3213 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3214 | d.RunUntilComplete(); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3215 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3216 | EXPECT_NE(d.data_received().find("__Secure-bar=1"), std::string::npos); |
| 3217 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3218 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3219 | } |
| 3220 | } |
| 3221 | |
| 3222 | // Tests that secure cookies can't be set on non-secure origins if strict secure |
| 3223 | // cookies are enabled. |
| 3224 | TEST_F(URLRequestTest, StrictSecureCookiesOnNonsecureOrigin) { |
| 3225 | EmbeddedTestServer http_server; |
| 3226 | http_server.AddDefaultHandlers( |
| 3227 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3228 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3229 | https_server.AddDefaultHandlers( |
| 3230 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3231 | ASSERT_TRUE(http_server.Start()); |
| 3232 | ASSERT_TRUE(https_server.Start()); |
| 3233 | |
| 3234 | TestExperimentalFeaturesNetworkDelegate network_delegate; |
| 3235 | TestURLRequestContext context(true); |
| 3236 | context.set_network_delegate(&network_delegate); |
| 3237 | context.Init(); |
| 3238 | |
| 3239 | // Try to set a Secure cookie, with experimental features enabled. |
| 3240 | { |
| 3241 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3242 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3243 | http_server.GetURL("/set-cookie?nonsecure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3244 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3245 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3246 | d.RunUntilComplete(); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3247 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3248 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3249 | } |
| 3250 | |
| 3251 | // Verify that the cookie is not set. |
| 3252 | { |
| 3253 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3254 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3255 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3256 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3257 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3258 | d.RunUntilComplete(); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3259 | |
| 3260 | EXPECT_EQ(d.data_received().find("nonsecure-origin=1"), std::string::npos); |
| 3261 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3262 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3263 | } |
| 3264 | } |
| 3265 | |
| 3266 | // Tests that secure cookies can be set on secure origins even if strict secure |
| 3267 | // cookies are enabled. |
| 3268 | TEST_F(URLRequestTest, StrictSecureCookiesOnSecureOrigin) { |
| 3269 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 3270 | https_server.AddDefaultHandlers( |
| 3271 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3272 | ASSERT_TRUE(https_server.Start()); |
| 3273 | |
| 3274 | TestExperimentalFeaturesNetworkDelegate network_delegate; |
| 3275 | TestURLRequestContext context(true); |
| 3276 | context.set_network_delegate(&network_delegate); |
| 3277 | context.Init(); |
| 3278 | |
| 3279 | // Try to set a Secure cookie, with experimental features enabled. |
| 3280 | { |
| 3281 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3282 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3283 | https_server.GetURL("/set-cookie?secure-origin=1;Secure"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3284 | DEFAULT_PRIORITY, &d, 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 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3288 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3289 | } |
| 3290 | |
| 3291 | // Verify that the cookie is not set. |
| 3292 | { |
| 3293 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3294 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3295 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3296 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3297 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3298 | d.RunUntilComplete(); |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 3299 | |
| 3300 | EXPECT_NE(d.data_received().find("secure-origin=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3301 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3302 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3303 | } |
| 3304 | } |
| 3305 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3306 | // The parameter is true for same-site and false for cross-site requests. |
| 3307 | class URLRequestTestParameterizedSameSite |
| 3308 | : public URLRequestTest, |
| 3309 | public ::testing::WithParamInterface<bool> { |
| 3310 | protected: |
| 3311 | URLRequestTestParameterizedSameSite() { |
| 3312 | auto params = std::make_unique<HttpNetworkSession::Params>(); |
| 3313 | params->ignore_certificate_errors = true; |
| 3314 | context_.set_http_network_session_params(std::move(params)); |
| 3315 | context_.set_network_delegate(&network_delegate_); |
| 3316 | https_server_.AddDefaultHandlers( |
| 3317 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3318 | EXPECT_TRUE(https_server_.Start()); |
| 3319 | } |
| 3320 | |
| 3321 | // To be called after configuration of |context_| has been finalized. |
| 3322 | void InitContext() { context_.Init(); } |
| 3323 | |
| 3324 | const std::string kHost_ = "example.test"; |
| 3325 | const std::string kCrossHost_ = "cross-site.test"; |
| 3326 | TestURLRequestContext context_{true}; |
| 3327 | TestNetworkDelegate network_delegate_; |
| 3328 | base::HistogramTester histograms_; |
| 3329 | EmbeddedTestServer https_server_{EmbeddedTestServer::TYPE_HTTPS}; |
| 3330 | }; |
| 3331 | |
| 3332 | INSTANTIATE_TEST_CASE_P(URLRequestTest, |
| 3333 | URLRequestTestParameterizedSameSite, |
| 3334 | ::testing::Bool()); |
| 3335 | |
| 3336 | TEST_P(URLRequestTestParameterizedSameSite, CookieAgeMetrics) { |
| 3337 | const bool same_site = GetParam(); |
| 3338 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3339 | InitContext(); |
| 3340 | |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3341 | EmbeddedTestServer http_server; |
| 3342 | http_server.AddDefaultHandlers( |
| 3343 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3344 | ASSERT_TRUE(http_server.Start()); |
| 3345 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3346 | // Set two test cookies. |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3347 | { |
| 3348 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3349 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3350 | http_server.GetURL(kHost_, "/set-cookie?cookie=value&cookie2=value2"), |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3351 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3352 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3353 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3354 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3355 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3356 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 0); |
| 3357 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", 0); |
| 3358 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", 0); |
| 3359 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3360 | 0); |
| 3361 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3362 | 0); |
| 3363 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", 0); |
| 3364 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", 0); |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3365 | } |
| 3366 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3367 | // Make a secure request. |
Thiemo Nagel | 23e0b32 | 2018-04-18 12:46:54 | [diff] [blame] | 3368 | { |
| 3369 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3370 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3371 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3372 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3373 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3374 | req->set_initiator( |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3375 | url::Origin::Create(https_server_.GetURL(kInitiatingHost, "/"))); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3376 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3377 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3378 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", 0); |
| 3379 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", 0); |
| 3380 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", |
| 3381 | !same_site); |
| 3382 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", |
| 3383 | same_site); |
| 3384 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3385 | 0); |
| 3386 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3387 | 0); |
| 3388 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", |
| 3389 | same_site ? 0 : 2); |
| 3390 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", |
| 3391 | same_site ? 2 : 0); |
| 3392 | EXPECT_TRUE(d.data_received().find("cookie=value") != std::string::npos); |
| 3393 | EXPECT_TRUE(d.data_received().find("cookie2=value2") != std::string::npos); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3394 | } |
| 3395 | |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3396 | // Make a non-secure request. |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3397 | { |
| 3398 | TestDelegate d; |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3399 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3400 | http_server.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3401 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3402 | req->set_site_for_cookies(http_server.GetURL(kInitiatingHost, "/")); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3403 | req->set_initiator( |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3404 | url::Origin::Create(http_server.GetURL(kInitiatingHost, "/"))); |
Mohsen Izadi | ad6f759 | 2018-04-18 15:57:10 | [diff] [blame] | 3405 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3406 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3407 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureCrossSiteRequest", |
| 3408 | !same_site); |
| 3409 | histograms_.ExpectTotalCount("Cookie.AgeForNonSecureSameSiteRequest", |
| 3410 | same_site); |
| 3411 | histograms_.ExpectTotalCount("Cookie.AgeForSecureCrossSiteRequest", |
| 3412 | !same_site); |
| 3413 | histograms_.ExpectTotalCount("Cookie.AgeForSecureSameSiteRequest", |
| 3414 | same_site); |
| 3415 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureCrossSiteRequest", |
| 3416 | same_site ? 0 : 2); |
| 3417 | histograms_.ExpectTotalCount("Cookie.AllAgesForNonSecureSameSiteRequest", |
| 3418 | same_site ? 2 : 0); |
| 3419 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureCrossSiteRequest", |
| 3420 | same_site ? 0 : 2); |
| 3421 | histograms_.ExpectTotalCount("Cookie.AllAgesForSecureSameSiteRequest", |
| 3422 | same_site ? 2 : 0); |
| 3423 | EXPECT_TRUE(d.data_received().find("cookie=value") != std::string::npos); |
| 3424 | EXPECT_TRUE(d.data_received().find("cookie2=value2") != std::string::npos); |
| 3425 | } |
| 3426 | } |
| 3427 | |
| 3428 | // Cookies with secure attribute (no HSTS) --> k1pSecureAttribute |
| 3429 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3430 | CookieNetworkSecurityMetricSecureAttribute) { |
| 3431 | const bool same_site = GetParam(); |
| 3432 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3433 | InitContext(); |
| 3434 | |
| 3435 | // Set cookies. |
| 3436 | { |
| 3437 | TestDelegate d; |
| 3438 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3439 | https_server_.GetURL(kHost_, |
| 3440 | "/set-cookie?session-cookie=value;Secure&" |
| 3441 | "longlived-cookie=value;Secure;domain=" + |
| 3442 | kHost_ + ";Max-Age=360000"), |
| 3443 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3444 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3445 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3446 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3447 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3448 | } |
| 3449 | |
| 3450 | // Verify that the cookies fall into the correct metrics bucket. |
| 3451 | { |
| 3452 | TestDelegate d; |
| 3453 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3454 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3455 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3456 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3457 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3458 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3459 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3460 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3461 | // Static cast of boolean required for MSVC 1911. |
| 3462 | histograms_.ExpectBucketCount( |
| 3463 | "Cookie.NetworkSecurity", |
| 3464 | static_cast<int>(CookieNetworkSecurity::k1pSecureAttribute) | |
| 3465 | static_cast<int>(!same_site), |
| 3466 | 2); |
| 3467 | } |
| 3468 | } |
| 3469 | |
| 3470 | // Short-lived host cookie --> k1pHSTSHostCookie |
| 3471 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3472 | CookieNetworkSecurityMetricShortlivedHostCookie) { |
| 3473 | const bool same_site = GetParam(); |
| 3474 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3475 | |
| 3476 | TransportSecurityState transport_security_state; |
| 3477 | transport_security_state.AddHSTS( |
| 3478 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3479 | false /* include_subdomains */); |
| 3480 | context_.set_transport_security_state(&transport_security_state); |
| 3481 | InitContext(); |
| 3482 | |
| 3483 | // Set cookie. |
| 3484 | { |
| 3485 | TestDelegate d; |
| 3486 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3487 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;Max-Age=3600"), |
| 3488 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3489 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3490 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3491 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3492 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3493 | } |
| 3494 | |
| 3495 | // Verify that the cookie falls into the correct metrics bucket. |
| 3496 | { |
| 3497 | TestDelegate d; |
| 3498 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3499 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3500 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3501 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3502 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3503 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3504 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3505 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3506 | // Static cast of boolean required for MSVC 1911. |
| 3507 | histograms_.ExpectBucketCount( |
| 3508 | "Cookie.NetworkSecurity", |
| 3509 | static_cast<int>(CookieNetworkSecurity::k1pHSTSHostCookie) | |
| 3510 | static_cast<int>(!same_site), |
| 3511 | 1); |
| 3512 | } |
| 3513 | } |
| 3514 | |
| 3515 | // Long-lived (either due to expiry or due to being a session cookie) host |
| 3516 | // cookies --> k1pExpiringHSTSHostCookie |
| 3517 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3518 | CookieNetworkSecurityMetricLonglivedHostCookie) { |
| 3519 | const bool same_site = GetParam(); |
| 3520 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3521 | |
| 3522 | TransportSecurityState transport_security_state; |
| 3523 | transport_security_state.AddHSTS( |
| 3524 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3525 | false /* include_subdomains */); |
| 3526 | context_.set_transport_security_state(&transport_security_state); |
| 3527 | InitContext(); |
| 3528 | |
| 3529 | // Set cookies. |
| 3530 | { |
| 3531 | TestDelegate d; |
| 3532 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3533 | https_server_.GetURL(kHost_, |
| 3534 | "/set-cookie?session-cookie=value&" |
| 3535 | "longlived-cookie=value;Max-Age=360000"), |
| 3536 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3537 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3538 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3539 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3540 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3541 | } |
| 3542 | |
| 3543 | // Verify that the cookies fall into the correct metrics bucket. |
| 3544 | { |
| 3545 | TestDelegate d; |
| 3546 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3547 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3548 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3549 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3550 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3551 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3552 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3553 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3554 | // Static cast of boolean required for MSVC 1911. |
| 3555 | histograms_.ExpectBucketCount( |
| 3556 | "Cookie.NetworkSecurity", |
| 3557 | static_cast<int>(CookieNetworkSecurity::k1pExpiringHSTSHostCookie) | |
| 3558 | static_cast<int>(!same_site), |
| 3559 | 2); |
| 3560 | } |
| 3561 | } |
| 3562 | |
| 3563 | // Domain cookie with HSTS subdomains with cookie expiry before HSTS expiry --> |
| 3564 | // k1pHSTSSubdomainsIncluded |
| 3565 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3566 | CookieNetworkSecurityMetricShortlivedDomainCookie) { |
| 3567 | const bool same_site = GetParam(); |
| 3568 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3569 | |
| 3570 | TransportSecurityState transport_security_state; |
| 3571 | transport_security_state.AddHSTS( |
| 3572 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3573 | true /* include_subdomains */); |
| 3574 | context_.set_transport_security_state(&transport_security_state); |
| 3575 | InitContext(); |
| 3576 | |
| 3577 | // Set cookie. |
| 3578 | { |
| 3579 | TestDelegate d; |
| 3580 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3581 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;domain=" + |
| 3582 | kHost_ + ";Max-Age=3600"), |
| 3583 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3584 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3585 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3586 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3587 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3588 | } |
| 3589 | |
| 3590 | // Verify that the cookie falls into the correct metrics bucket. |
| 3591 | { |
| 3592 | TestDelegate d; |
| 3593 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3594 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3595 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3596 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3597 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3598 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3599 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3600 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3601 | // Static cast of boolean required for MSVC 1911. |
| 3602 | histograms_.ExpectBucketCount( |
| 3603 | "Cookie.NetworkSecurity", |
| 3604 | static_cast<int>(CookieNetworkSecurity::k1pHSTSSubdomainsIncluded) | |
| 3605 | static_cast<int>(!same_site), |
| 3606 | 1); |
| 3607 | } |
| 3608 | } |
| 3609 | |
| 3610 | // Long-lived (either due to expiry or due to being a session cookie) domain |
| 3611 | // cookies with HSTS subdomains --> k1pExpiringHSTSSubdomainsIncluded |
| 3612 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3613 | CookieNetworkSecurityMetricLonglivedDomainCookie) { |
| 3614 | const bool same_site = GetParam(); |
| 3615 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3616 | |
| 3617 | TransportSecurityState transport_security_state; |
| 3618 | transport_security_state.AddHSTS( |
| 3619 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3620 | true /* include_subdomains */); |
| 3621 | context_.set_transport_security_state(&transport_security_state); |
| 3622 | InitContext(); |
| 3623 | |
| 3624 | // Set cookies. |
| 3625 | { |
| 3626 | TestDelegate d; |
| 3627 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3628 | https_server_.GetURL( |
| 3629 | kHost_, "/set-cookie?session-cookie=value;domain=" + kHost_ + "&" + |
| 3630 | "longlived-cookie=value;domain=" + kHost_ + |
| 3631 | ";Max-Age=360000"), |
| 3632 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3633 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3634 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3635 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3636 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3637 | } |
| 3638 | |
| 3639 | // Verify that the cookies fall into the correct metrics bucket. |
| 3640 | { |
| 3641 | TestDelegate d; |
| 3642 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3643 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3644 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3645 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3646 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3647 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3648 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3649 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3650 | // Static cast of boolean required for MSVC 1911. |
| 3651 | histograms_.ExpectBucketCount( |
| 3652 | "Cookie.NetworkSecurity", |
| 3653 | static_cast<int>( |
| 3654 | CookieNetworkSecurity::k1pExpiringHSTSSubdomainsIncluded) | |
| 3655 | static_cast<int>(!same_site), |
| 3656 | 2); |
| 3657 | } |
| 3658 | } |
| 3659 | |
| 3660 | // Domain cookie with HSTS subdomains not included --> k1pHSTSSpoofable |
| 3661 | TEST_P(URLRequestTestParameterizedSameSite, |
| 3662 | CookieNetworkSecurityMetricSpoofableDomainCookie) { |
| 3663 | const bool same_site = GetParam(); |
| 3664 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3665 | |
| 3666 | TransportSecurityState transport_security_state; |
| 3667 | transport_security_state.AddHSTS( |
| 3668 | kHost_, base::Time::Now() + base::TimeDelta::FromHours(10), |
| 3669 | false /* include_subdomains */); |
| 3670 | context_.set_transport_security_state(&transport_security_state); |
| 3671 | InitContext(); |
| 3672 | |
| 3673 | // Set cookie. |
| 3674 | { |
| 3675 | TestDelegate d; |
| 3676 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3677 | https_server_.GetURL(kHost_, "/set-cookie?cookie=value;domain=" + |
| 3678 | kHost_ + ";Max-Age=3600"), |
| 3679 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3680 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3681 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3682 | ASSERT_EQ(1, network_delegate_.set_cookie_count()); |
| 3683 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3684 | } |
| 3685 | |
| 3686 | // Verify that the cookie falls into the correct metrics bucket. |
| 3687 | { |
| 3688 | TestDelegate d; |
| 3689 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3690 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3691 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3692 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3693 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3694 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3695 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3696 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 1); |
| 3697 | // Static cast of boolean required for MSVC 1911. |
| 3698 | histograms_.ExpectBucketCount( |
| 3699 | "Cookie.NetworkSecurity", |
| 3700 | static_cast<int>(CookieNetworkSecurity::k1pHSTSSpoofable) | |
| 3701 | static_cast<int>(!same_site), |
| 3702 | 1); |
| 3703 | } |
| 3704 | } |
| 3705 | |
| 3706 | // Cookie without HSTS --> k1p(Non)SecureConnection |
| 3707 | TEST_P(URLRequestTestParameterizedSameSite, CookieNetworkSecurityMetricNoHSTS) { |
| 3708 | const bool same_site = GetParam(); |
| 3709 | const std::string kInitiatingHost = same_site ? kHost_ : kCrossHost_; |
| 3710 | InitContext(); |
| 3711 | |
| 3712 | EmbeddedTestServer http_server; |
| 3713 | http_server.AddDefaultHandlers( |
| 3714 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 3715 | ASSERT_TRUE(http_server.Start()); |
| 3716 | |
| 3717 | // Set cookies. |
| 3718 | { |
| 3719 | TestDelegate d; |
| 3720 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3721 | https_server_.GetURL(kHost_, |
| 3722 | "/set-cookie?cookie=value;domain=" + kHost_ + |
| 3723 | ";Max-Age=3600&host-cookie=value"), |
| 3724 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3725 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3726 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3727 | ASSERT_EQ(2, network_delegate_.set_cookie_count()); |
| 3728 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 0); |
| 3729 | } |
| 3730 | |
| 3731 | // Verify that the cookie falls into the correct metrics bucket. |
| 3732 | { |
| 3733 | TestDelegate d; |
| 3734 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3735 | https_server_.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, |
| 3736 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3737 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3738 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3739 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3740 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3741 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 2); |
| 3742 | // Static cast of boolean required for MSVC 1911. |
| 3743 | histograms_.ExpectBucketCount( |
| 3744 | "Cookie.NetworkSecurity", |
| 3745 | static_cast<int>(CookieNetworkSecurity::k1pSecureConnection) | |
| 3746 | static_cast<int>(!same_site), |
| 3747 | 2); |
| 3748 | } |
| 3749 | |
| 3750 | // Verify that the cookie falls into the correct metrics bucket. |
| 3751 | { |
| 3752 | TestDelegate d; |
| 3753 | std::unique_ptr<URLRequest> req(context_.CreateRequest( |
| 3754 | http_server.GetURL(kHost_, "/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3755 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 3756 | req->set_site_for_cookies(https_server_.GetURL(kInitiatingHost, "/")); |
| 3757 | req->set_initiator(url::Origin::Create(https_server_.GetURL(kHost_, "/"))); |
| 3758 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3759 | d.RunUntilComplete(); |
Thiemo Nagel | 0793b9c53 | 2018-04-18 16:57:58 | [diff] [blame] | 3760 | histograms_.ExpectTotalCount("Cookie.NetworkSecurity", 4); |
| 3761 | // Static cast of boolean required for MSVC 1911. |
| 3762 | histograms_.ExpectBucketCount( |
| 3763 | "Cookie.NetworkSecurity", |
| 3764 | static_cast<int>(CookieNetworkSecurity::k1pSecureConnection) | |
| 3765 | static_cast<int>(!same_site), |
| 3766 | 2); |
| 3767 | // Static cast of boolean required for MSVC 1911. |
| 3768 | histograms_.ExpectBucketCount( |
| 3769 | "Cookie.NetworkSecurity", |
| 3770 | static_cast<int>(CookieNetworkSecurity::k1pNonsecureConnection) | |
| 3771 | static_cast<int>(!same_site), |
| 3772 | 2); |
Mike West | 8614988 | 2017-07-28 10:41:49 | [diff] [blame] | 3773 | } |
| 3774 | } |
| 3775 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3776 | // Tests that a request is cancelled while entering suspend mode. Uses mocks |
| 3777 | // rather than a spawned test server because the connection used to talk to |
| 3778 | // the test server is affected by entering suspend mode on Android. |
| 3779 | TEST_F(URLRequestTest, CancelOnSuspend) { |
| 3780 | TestPowerMonitorSource* power_monitor_source = new TestPowerMonitorSource(); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3781 | base::PowerMonitor power_monitor(base::WrapUnique(power_monitor_source)); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3782 | |
| 3783 | URLRequestFailedJob::AddUrlHandler(); |
| 3784 | |
| 3785 | TestDelegate d; |
| 3786 | // Request that just hangs. |
| 3787 | GURL url(URLRequestFailedJob::GetMockHttpUrl(ERR_IO_PENDING)); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3788 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3789 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3790 | r->Start(); |
| 3791 | |
| 3792 | power_monitor_source->Suspend(); |
| 3793 | // Wait for the suspend notification to cause the request to fail. |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3794 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3795 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3796 | EXPECT_TRUE(d.request_failed()); |
| 3797 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 3798 | |
| 3799 | URLRequestFilter::GetInstance()->ClearHandlers(); |
| 3800 | |
| 3801 | // Shouldn't be needed, but just in case. |
| 3802 | power_monitor_source->Resume(); |
| 3803 | } |
| 3804 | |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3805 | // FixedDateNetworkDelegate swaps out the server's HTTP Date response header |
| 3806 | // value for the |fixed_date| argument given to the constructor. |
| 3807 | class FixedDateNetworkDelegate : public TestNetworkDelegate { |
| 3808 | public: |
| 3809 | explicit FixedDateNetworkDelegate(const std::string& fixed_date) |
| 3810 | : fixed_date_(fixed_date) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 3811 | ~FixedDateNetworkDelegate() override = default; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3812 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3813 | // NetworkDelegate implementation |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 3814 | int OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3815 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 3816 | CompletionOnceCallback callback, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3817 | const HttpResponseHeaders* original_response_headers, |
| 3818 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 3819 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3820 | |
| 3821 | private: |
| 3822 | std::string fixed_date_; |
| 3823 | |
| 3824 | DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); |
| 3825 | }; |
| 3826 | |
| 3827 | int FixedDateNetworkDelegate::OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3828 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 3829 | CompletionOnceCallback callback, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3830 | const HttpResponseHeaders* original_response_headers, |
| 3831 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3832 | GURL* allowed_unsafe_redirect_url) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3833 | HttpResponseHeaders* new_response_headers = |
| 3834 | new HttpResponseHeaders(original_response_headers->raw_headers()); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3835 | |
| 3836 | new_response_headers->RemoveHeader("Date"); |
| 3837 | new_response_headers->AddHeader("Date: " + fixed_date_); |
| 3838 | |
| 3839 | *override_response_headers = new_response_headers; |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 3840 | return TestNetworkDelegate::OnHeadersReceived( |
| 3841 | request, std::move(callback), original_response_headers, |
| 3842 | override_response_headers, allowed_unsafe_redirect_url); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3843 | } |
| 3844 | |
| 3845 | // Test that cookie expiration times are adjusted for server/client clock |
| 3846 | // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date |
| 3847 | // headers by defaulting to GMT. (crbug.com/135131) |
| 3848 | TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3849 | HttpTestServer test_server; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3850 | ASSERT_TRUE(test_server.Start()); |
| 3851 | |
| 3852 | // Set up an expired cookie. |
| 3853 | { |
| 3854 | TestNetworkDelegate network_delegate; |
| 3855 | default_context_.set_network_delegate(&network_delegate); |
| 3856 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3857 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3858 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3859 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3860 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3861 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3862 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3863 | } |
| 3864 | // Verify that the cookie is not set. |
| 3865 | { |
| 3866 | TestNetworkDelegate network_delegate; |
| 3867 | default_context_.set_network_delegate(&network_delegate); |
| 3868 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3869 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3870 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3871 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3872 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3873 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3874 | |
| 3875 | EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos); |
| 3876 | } |
| 3877 | // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier. |
| 3878 | { |
| 3879 | FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC"); |
| 3880 | default_context_.set_network_delegate(&network_delegate); |
| 3881 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3882 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3883 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3884 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3885 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3886 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3887 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3888 | } |
| 3889 | // Verify that the cookie is set. |
| 3890 | { |
| 3891 | TestNetworkDelegate network_delegate; |
| 3892 | default_context_.set_network_delegate(&network_delegate); |
| 3893 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3894 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3895 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d, |
| 3896 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3897 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3898 | d.RunUntilComplete(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3899 | |
| 3900 | EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos); |
| 3901 | } |
| 3902 | } |
| 3903 | |
| 3904 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3905 | // Check that it is impossible to change the referrer in the extra headers of |
| 3906 | // an URLRequest. |
| 3907 | TEST_F(URLRequestTest, DoNotOverrideReferrer) { |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 3908 | HttpTestServer test_server; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3909 | ASSERT_TRUE(test_server.Start()); |
| 3910 | |
| 3911 | // If extra headers contain referer and the request contains a referer, |
| 3912 | // only the latter shall be respected. |
| 3913 | { |
| 3914 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3915 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3916 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 3917 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3918 | req->SetReferrer("http://foo.com/"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3919 | |
| 3920 | HttpRequestHeaders headers; |
| 3921 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3922 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3923 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3924 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3925 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3926 | |
| 3927 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 3928 | } |
| 3929 | |
| 3930 | // If extra headers contain a referer but the request does not, no referer |
| 3931 | // shall be sent in the header. |
| 3932 | { |
| 3933 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 3934 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3935 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 3936 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3937 | |
| 3938 | HttpRequestHeaders headers; |
| 3939 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3940 | req->SetExtraRequestHeaders(headers); |
| 3941 | req->SetLoadFlags(LOAD_VALIDATE_CACHE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3942 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3943 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3944 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3945 | |
| 3946 | EXPECT_EQ("None", d.data_received()); |
| 3947 | } |
| 3948 | } |
| 3949 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3950 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3951 | public: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 3952 | URLRequestTestHTTP() : test_server_(base::FilePath(kTestFilePath)) {} |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3953 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3954 | protected: |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3955 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 3956 | // |request_method| is the method to use for the initial request. |
| 3957 | // |redirect_method| is the method that is expected to be used for the second |
| 3958 | // request, after redirection. |
| 3959 | // If |include_data| is true, data is uploaded with the request. The |
| 3960 | // response body is expected to match it exactly, if and only if |
| 3961 | // |request_method| == |redirect_method|. |
| 3962 | void HTTPRedirectMethodTest(const GURL& redirect_url, |
| 3963 | const std::string& request_method, |
| 3964 | const std::string& redirect_method, |
| 3965 | bool include_data) { |
| 3966 | static const char kData[] = "hello world"; |
| 3967 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 3968 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3969 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3970 | req->set_method(request_method); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3971 | if (include_data) { |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 3972 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3973 | HttpRequestHeaders headers; |
| 3974 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Brett Wilson | 5accd24 | 2017-11-30 22:07:32 | [diff] [blame] | 3975 | base::NumberToString(arraysize(kData) - 1)); |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3976 | headers.SetHeader(HttpRequestHeaders::kContentType, "text/plain"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3977 | req->SetExtraRequestHeaders(headers); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3978 | } |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3979 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 3980 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3981 | EXPECT_EQ(redirect_method, req->method()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 3982 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3983 | if (include_data) { |
| 3984 | if (request_method == redirect_method) { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3985 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 3986 | HttpRequestHeaders::kContentLength)); |
| 3987 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 3988 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3989 | EXPECT_EQ(kData, d.data_received()); |
| 3990 | } else { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3991 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 3992 | HttpRequestHeaders::kContentLength)); |
| 3993 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 3994 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3995 | EXPECT_NE(kData, d.data_received()); |
| 3996 | } |
| 3997 | } |
| 3998 | if (HasFailure()) |
| 3999 | LOG(WARNING) << "Request method was: " << request_method; |
| 4000 | } |
| 4001 | |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4002 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. It's also |
| 4003 | // used as the initial origin. |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4004 | // |request_method| is the method to use for the initial request. |
| 4005 | // |redirect_method| is the method that is expected to be used for the second |
| 4006 | // request, after redirection. |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4007 | // |expected_origin_value| is the expected value for the Origin header after |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4008 | // redirection. If empty, expects that there will be no Origin header. |
| 4009 | void HTTPRedirectOriginHeaderTest(const GURL& redirect_url, |
| 4010 | const std::string& request_method, |
| 4011 | const std::string& redirect_method, |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4012 | const std::string& expected_origin_value) { |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4013 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4014 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 4015 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4016 | req->set_method(request_method); |
| 4017 | req->SetExtraRequestHeaderByName(HttpRequestHeaders::kOrigin, |
| 4018 | redirect_url.GetOrigin().spec(), false); |
| 4019 | req->Start(); |
| 4020 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4021 | d.RunUntilComplete(); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4022 | |
| 4023 | EXPECT_EQ(redirect_method, req->method()); |
| 4024 | // Note that there is no check for request success here because, for |
| 4025 | // purposes of testing, the request very well may fail. For example, if the |
| 4026 | // test redirects to an HTTPS server from an HTTP origin, thus it is cross |
| 4027 | // origin, there is not an HTTPS server in this unit test framework, so the |
| 4028 | // request would fail. However, that's fine, as long as the request headers |
| 4029 | // are in order and pass the checks below. |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4030 | if (expected_origin_value.empty()) { |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4031 | EXPECT_FALSE( |
| 4032 | req->extra_request_headers().HasHeader(HttpRequestHeaders::kOrigin)); |
| 4033 | } else { |
| 4034 | std::string origin_header; |
| 4035 | EXPECT_TRUE(req->extra_request_headers().GetHeader( |
| 4036 | HttpRequestHeaders::kOrigin, &origin_header)); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 4037 | EXPECT_EQ(expected_origin_value, origin_header); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 4038 | } |
| 4039 | } |
| 4040 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4041 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4042 | const int kMsgSize = 20000; // multiple of 10 |
| 4043 | const int kIterations = 50; |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 4044 | char* uploadBytes = new char[kMsgSize+1]; |
| 4045 | char* ptr = uploadBytes; |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4046 | char marker = 'a'; |
| 4047 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 4048 | memcpy(ptr, "----------", 10); |
| 4049 | ptr += 10; |
| 4050 | if (idx % 100 == 0) { |
| 4051 | ptr--; |
| 4052 | *ptr++ = marker; |
| 4053 | if (++marker > 'z') |
| 4054 | marker = 'a'; |
| 4055 | } |
| 4056 | } |
| 4057 | uploadBytes[kMsgSize] = '\0'; |
| 4058 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4059 | for (int i = 0; i < kIterations; ++i) { |
| 4060 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4061 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4062 | test_server_.GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 4063 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4064 | r->set_method(method.c_str()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4065 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4066 | r->set_upload(CreateSimpleUploadData(uploadBytes)); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4067 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4068 | r->Start(); |
| 4069 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4070 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4071 | d.RunUntilComplete(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4072 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4073 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 4074 | << d.request_status(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4075 | |
| 4076 | EXPECT_FALSE(d.received_data_before_response()); |
| 4077 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 4078 | } |
| 4079 | delete[] uploadBytes; |
| 4080 | } |
| 4081 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 4082 | bool DoManyCookiesRequest(int num_cookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4083 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4084 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4085 | test_server_.GetURL("/set-many-cookies?" + |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4086 | base::IntToString(num_cookies)), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4087 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4088 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4089 | r->Start(); |
| 4090 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4091 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4092 | d.RunUntilComplete(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4093 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4094 | if (d.request_status() != OK) { |
| 4095 | EXPECT_EQ(ERR_RESPONSE_HEADERS_TOO_BIG, d.request_status()); |
| 4096 | return false; |
| 4097 | } |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4098 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4099 | return true; |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4100 | } |
| 4101 | |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4102 | HttpTestServer* http_test_server() { return &test_server_; } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 4103 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4104 | private: |
David Benjamin | 42116f6 | 2017-11-20 21:45:39 | [diff] [blame] | 4105 | HttpTestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4106 | }; |
| 4107 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4108 | namespace { |
| 4109 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4110 | std::unique_ptr<test_server::HttpResponse> HandleRedirectConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4111 | const test_server::HttpRequest& request) { |
| 4112 | if (request.headers.find("Host") == request.headers.end() || |
| 4113 | request.headers.at("Host") != "www.redirect.com" || |
| 4114 | request.method != test_server::METHOD_CONNECT) { |
| 4115 | return nullptr; |
| 4116 | } |
| 4117 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4118 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4119 | new test_server::BasicHttpResponse); |
| 4120 | http_response->set_code(HTTP_FOUND); |
| 4121 | http_response->AddCustomHeader("Location", |
| 4122 | "http://www.destination.com/foo.js"); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 4123 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4124 | } |
| 4125 | |
| 4126 | } // namespace |
| 4127 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4128 | class TestSSLConfigService : public SSLConfigService { |
| 4129 | public: |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 4130 | TestSSLConfigService(bool online_rev_checking, |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4131 | bool rev_checking_required_local_anchors, |
| 4132 | bool token_binding_enabled) |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 4133 | : online_rev_checking_(online_rev_checking), |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4134 | rev_checking_required_local_anchors_( |
| 4135 | rev_checking_required_local_anchors), |
| 4136 | token_binding_enabled_(token_binding_enabled), |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4137 | min_version_(kDefaultSSLVersionMin), |
| 4138 | max_version_(kDefaultSSLVersionMax) {} |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4139 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4140 | void set_max_version(uint16_t version) { max_version_ = version; } |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4141 | void set_min_version(uint16_t version) { min_version_ = version; } |
| 4142 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4143 | // SSLConfigService: |
| 4144 | void GetSSLConfig(SSLConfig* config) override { |
| 4145 | *config = SSLConfig(); |
| 4146 | config->rev_checking_enabled = online_rev_checking_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4147 | config->rev_checking_required_local_anchors = |
| 4148 | rev_checking_required_local_anchors_; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4149 | config->version_min = min_version_; |
| 4150 | config->version_max = max_version_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4151 | if (token_binding_enabled_) { |
| 4152 | config->token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 4153 | } |
| 4154 | } |
| 4155 | |
| 4156 | protected: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 4157 | ~TestSSLConfigService() override = default; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4158 | |
| 4159 | private: |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4160 | const bool online_rev_checking_; |
| 4161 | const bool rev_checking_required_local_anchors_; |
| 4162 | const bool token_binding_enabled_; |
| 4163 | uint16_t min_version_; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 4164 | uint16_t max_version_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4165 | }; |
| 4166 | |
| 4167 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 4168 | #if !defined(OS_IOS) |
| 4169 | class TokenBindingURLRequestTest : public URLRequestTestHTTP { |
| 4170 | public: |
fdoray | 9f0b478 | 2017-02-10 12:55:47 | [diff] [blame] | 4171 | TokenBindingURLRequestTest() = default; |
fdoray | a89e673c | 2017-01-31 21:44:21 | [diff] [blame] | 4172 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4173 | void SetUp() override { |
| 4174 | default_context_.set_ssl_config_service( |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 4175 | new TestSSLConfigService(false, false, true)); |
fdoray | a89e673c | 2017-01-31 21:44:21 | [diff] [blame] | 4176 | channel_id_service_.reset( |
| 4177 | new ChannelIDService(new DefaultChannelIDStore(NULL))); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4178 | default_context_.set_channel_id_service(channel_id_service_.get()); |
| 4179 | URLRequestTestHTTP::SetUp(); |
| 4180 | } |
| 4181 | |
| 4182 | protected: |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4183 | std::unique_ptr<ChannelIDService> channel_id_service_; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4184 | }; |
| 4185 | |
| 4186 | TEST_F(TokenBindingURLRequestTest, TokenBindingTest) { |
| 4187 | SpawnedTestServer::SSLOptions ssl_options; |
| 4188 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 4189 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 4190 | ssl_options, |
| 4191 | base::FilePath(kTestFilePath)); |
| 4192 | ASSERT_TRUE(https_test_server.Start()); |
| 4193 | |
| 4194 | TestDelegate d; |
| 4195 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4196 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4197 | https_test_server.GetURL("tokbind-ekm"), DEFAULT_PRIORITY, &d, |
| 4198 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4199 | r->Start(); |
| 4200 | EXPECT_TRUE(r->is_pending()); |
| 4201 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4202 | d.RunUntilComplete(); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4203 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4204 | EXPECT_EQ(OK, d.request_status()); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4205 | |
| 4206 | HttpRequestHeaders headers; |
| 4207 | std::string token_binding_header, token_binding_message; |
| 4208 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 4209 | EXPECT_TRUE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 4210 | &token_binding_header)); |
| 4211 | EXPECT_TRUE(base::Base64UrlDecode( |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 4212 | token_binding_header, base::Base64UrlDecodePolicy::DISALLOW_PADDING, |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4213 | &token_binding_message)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 4214 | std::vector<TokenBinding> token_bindings; |
| 4215 | ASSERT_TRUE( |
| 4216 | ParseTokenBindingMessage(token_binding_message, &token_bindings)); |
| 4217 | ASSERT_EQ(1ull, token_bindings.size()); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4218 | |
| 4219 | EXPECT_GT(d.bytes_received(), 0); |
| 4220 | std::string ekm = d.data_received(); |
| 4221 | |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 4222 | EXPECT_EQ(TokenBindingType::PROVIDED, token_bindings[0].type); |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 4223 | EXPECT_TRUE(VerifyTokenBindingSignature(token_bindings[0].ec_point, |
| 4224 | token_bindings[0].signature, |
| 4225 | TokenBindingType::PROVIDED, ekm)); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4226 | } |
| 4227 | } |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 4228 | |
| 4229 | TEST_F(TokenBindingURLRequestTest, ForwardTokenBinding) { |
| 4230 | SpawnedTestServer::SSLOptions ssl_options; |
| 4231 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 4232 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 4233 | ssl_options, |
| 4234 | base::FilePath(kTestFilePath)); |
| 4235 | ASSERT_TRUE(https_test_server.Start()); |
| 4236 | |
| 4237 | TestDelegate d; |
| 4238 | { |
| 4239 | GURL redirect_url = |
| 4240 | https_test_server.GetURL("forward-tokbind?/tokbind-ekm"); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4241 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4242 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 4243 | r->Start(); |
| 4244 | EXPECT_TRUE(r->is_pending()); |
| 4245 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4246 | d.RunUntilComplete(); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 4247 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4248 | EXPECT_EQ(OK, d.request_status()); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 4249 | |
| 4250 | HttpRequestHeaders headers; |
| 4251 | std::string token_binding_header, token_binding_message; |
| 4252 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 4253 | EXPECT_TRUE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 4254 | &token_binding_header)); |
| 4255 | EXPECT_TRUE(base::Base64UrlDecode( |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 4256 | token_binding_header, base::Base64UrlDecodePolicy::DISALLOW_PADDING, |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 4257 | &token_binding_message)); |
| 4258 | std::vector<TokenBinding> token_bindings; |
| 4259 | ASSERT_TRUE( |
| 4260 | ParseTokenBindingMessage(token_binding_message, &token_bindings)); |
| 4261 | ASSERT_EQ(2ull, token_bindings.size()); |
| 4262 | |
| 4263 | EXPECT_GT(d.bytes_received(), 0); |
| 4264 | std::string ekm = d.data_received(); |
| 4265 | |
| 4266 | EXPECT_EQ(TokenBindingType::PROVIDED, token_bindings[0].type); |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 4267 | EXPECT_TRUE(VerifyTokenBindingSignature(token_bindings[0].ec_point, |
| 4268 | token_bindings[0].signature, |
| 4269 | TokenBindingType::PROVIDED, ekm)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 4270 | EXPECT_EQ(TokenBindingType::REFERRED, token_bindings[1].type); |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 4271 | EXPECT_TRUE(VerifyTokenBindingSignature(token_bindings[1].ec_point, |
| 4272 | token_bindings[1].signature, |
| 4273 | TokenBindingType::REFERRED, ekm)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 4274 | } |
| 4275 | } |
| 4276 | |
| 4277 | // TODO(nharper): Remove this #ifdef and replace SpawnedTestServer with |
| 4278 | // EmbeddedTestServer once crbug.com/599187 is resolved. |
| 4279 | #if !defined(OS_ANDROID) |
| 4280 | TEST_F(TokenBindingURLRequestTest, DontForwardHeaderFromHttp) { |
Sergey Ulanov | 9e8d6f3 | 2017-08-14 22:12:58 | [diff] [blame] | 4281 | SpawnedTestServer http_server(SpawnedTestServer::TYPE_HTTP, base::FilePath()); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 4282 | ASSERT_TRUE(http_server.Start()); |
| 4283 | SpawnedTestServer::SSLOptions ssl_options; |
| 4284 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 4285 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 4286 | ssl_options, |
| 4287 | base::FilePath(kTestFilePath)); |
| 4288 | ASSERT_TRUE(https_test_server.Start()); |
| 4289 | |
| 4290 | TestDelegate d; |
| 4291 | { |
| 4292 | GURL redirect_url = http_server.GetURL( |
| 4293 | "forward-tokbind?" + https_test_server.GetURL("tokbind-ekm").spec()); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4294 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4295 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 4296 | r->Start(); |
| 4297 | EXPECT_TRUE(r->is_pending()); |
| 4298 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4299 | d.RunUntilComplete(); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 4300 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4301 | EXPECT_EQ(OK, d.request_status()); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 4302 | |
| 4303 | HttpRequestHeaders headers; |
| 4304 | std::string token_binding_header, token_binding_message; |
| 4305 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 4306 | EXPECT_TRUE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 4307 | &token_binding_header)); |
| 4308 | EXPECT_TRUE(base::Base64UrlDecode( |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 4309 | token_binding_header, base::Base64UrlDecodePolicy::DISALLOW_PADDING, |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 4310 | &token_binding_message)); |
| 4311 | std::vector<TokenBinding> token_bindings; |
| 4312 | ASSERT_TRUE( |
| 4313 | ParseTokenBindingMessage(token_binding_message, &token_bindings)); |
| 4314 | ASSERT_EQ(1ull, token_bindings.size()); |
| 4315 | |
| 4316 | EXPECT_GT(d.bytes_received(), 0); |
| 4317 | std::string ekm = d.data_received(); |
| 4318 | |
| 4319 | EXPECT_EQ(TokenBindingType::PROVIDED, token_bindings[0].type); |
nharper | 78e6d2b | 2016-09-21 05:42:35 | [diff] [blame] | 4320 | EXPECT_TRUE(VerifyTokenBindingSignature(token_bindings[0].ec_point, |
| 4321 | token_bindings[0].signature, |
| 4322 | TokenBindingType::PROVIDED, ekm)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 4323 | } |
| 4324 | } |
| 4325 | |
| 4326 | // Test that if a server supporting Token Binding redirects (with |
nharper | 86f0be2 | 2016-07-14 00:49:36 | [diff] [blame] | 4327 | // Include-Referred-Token-Binding-ID) to an https url on a server that does not |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 4328 | // support Token Binding, then we do not send a Sec-Token-Binding when following |
| 4329 | // the redirect. |
| 4330 | TEST_F(TokenBindingURLRequestTest, ForwardWithoutTokenBinding) { |
| 4331 | SpawnedTestServer::SSLOptions ssl_options; |
| 4332 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 4333 | ssl_options, |
| 4334 | base::FilePath(kTestFilePath)); |
| 4335 | ASSERT_TRUE(https_test_server.Start()); |
| 4336 | ssl_options.supported_token_binding_params.push_back(TB_PARAM_ECDSAP256); |
| 4337 | SpawnedTestServer token_binding_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 4338 | ssl_options, |
| 4339 | base::FilePath(kTestFilePath)); |
| 4340 | ASSERT_TRUE(token_binding_test_server.Start()); |
| 4341 | |
| 4342 | TestDelegate d; |
| 4343 | { |
| 4344 | GURL redirect_url = token_binding_test_server.GetURL( |
| 4345 | "forward-tokbind?" + https_test_server.GetURL("tokbind-ekm").spec()); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4346 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4347 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 4348 | r->Start(); |
| 4349 | EXPECT_TRUE(r->is_pending()); |
| 4350 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4351 | d.RunUntilComplete(); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 4352 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4353 | EXPECT_EQ(OK, d.request_status()); |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 4354 | |
| 4355 | HttpRequestHeaders headers; |
| 4356 | std::string token_binding_header, token_binding_message; |
| 4357 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
| 4358 | EXPECT_FALSE(headers.GetHeader(HttpRequestHeaders::kTokenBinding, |
| 4359 | &token_binding_header)); |
| 4360 | } |
| 4361 | } |
| 4362 | #endif // !defined(OS_ANDROID) |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 4363 | #endif // !defined(OS_IOS) |
| 4364 | |
| 4365 | // 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] | 4366 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4367 | // The EmbeddedTestServer will return a 302 response, which we should not |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4368 | // follow. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 4369 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4370 | http_test_server()->RegisterRequestHandler( |
Matt Menke | 2b2a6047 | 2018-02-07 02:12:12 | [diff] [blame] | 4371 | base::BindRepeating(&HandleRedirectConnect)); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4372 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4373 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4374 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4375 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4376 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4377 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4378 | TestDelegate d; |
| 4379 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4380 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4381 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 4382 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4383 | r->Start(); |
| 4384 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4385 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4386 | d.RunUntilComplete(); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4387 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4388 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4389 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4390 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4391 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 4392 | // We should not have followed the redirect. |
| 4393 | EXPECT_EQ(0, d.received_redirect_count()); |
| 4394 | } |
| 4395 | } |
| 4396 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4397 | // This is the same as the previous test, but checks that the network delegate |
| 4398 | // registers the error. |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 4399 | TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4400 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4401 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4402 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4403 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4404 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4405 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4406 | TestDelegate d; |
| 4407 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4408 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4409 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, |
| 4410 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4411 | r->Start(); |
| 4412 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4413 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4414 | d.RunUntilComplete(); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4415 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4416 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4417 | EXPECT_FALSE(r->proxy_server().is_valid()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4418 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4419 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4420 | // We should not have followed the redirect. |
| 4421 | EXPECT_EQ(0, d.received_redirect_count()); |
| 4422 | |
| 4423 | EXPECT_EQ(1, network_delegate.error_count()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4424 | EXPECT_THAT(network_delegate.last_error(), |
| 4425 | IsError(ERR_TUNNEL_CONNECTION_FAILED)); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 4426 | } |
| 4427 | } |
| 4428 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4429 | // Tests that we can block and asynchronously return OK in various stages. |
| 4430 | TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) { |
| 4431 | static const BlockingNetworkDelegate::Stage blocking_stages[] = { |
| 4432 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 4433 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 4434 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED |
| 4435 | }; |
| 4436 | static const size_t blocking_stages_length = arraysize(blocking_stages); |
| 4437 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4438 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4439 | |
| 4440 | TestDelegate d; |
| 4441 | BlockingNetworkDelegate network_delegate( |
| 4442 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4443 | network_delegate.set_block_on( |
| 4444 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST | |
| 4445 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS | |
| 4446 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 4447 | |
| 4448 | TestURLRequestContext context(true); |
| 4449 | context.set_network_delegate(&network_delegate); |
| 4450 | context.Init(); |
| 4451 | |
| 4452 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 4453 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4454 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 4455 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4456 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4457 | r->Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4458 | for (size_t i = 0; i < blocking_stages_length; ++i) { |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4459 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4460 | EXPECT_EQ(blocking_stages[i], |
| 4461 | network_delegate.stage_blocked_for_callback()); |
| 4462 | network_delegate.DoCallback(OK); |
| 4463 | } |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4464 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4465 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4466 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4467 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4468 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4469 | } |
| 4470 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4471 | } |
| 4472 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4473 | // Tests that the network delegate can block and cancel a request. |
| 4474 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4475 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4476 | |
| 4477 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4478 | BlockingNetworkDelegate network_delegate( |
| 4479 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4480 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 4481 | network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4482 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4483 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4484 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4485 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4486 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4487 | std::unique_ptr<URLRequest> r( |
| 4488 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 4489 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4490 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4491 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4492 | d.RunUntilComplete(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4493 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4494 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4495 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4496 | EXPECT_EQ(ERR_EMPTY_RESPONSE, d.request_status()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4497 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4498 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4499 | } |
| 4500 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4501 | } |
| 4502 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4503 | // Helper function for NetworkDelegateCancelRequestAsynchronously and |
| 4504 | // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network |
| 4505 | // delegate operating in |block_mode| and a request for |url|. It blocks the |
| 4506 | // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT. |
| 4507 | void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode, |
| 4508 | BlockingNetworkDelegate::Stage stage, |
| 4509 | const GURL& url) { |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4510 | TestDelegate d; |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4511 | BlockingNetworkDelegate network_delegate(block_mode); |
| 4512 | network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT); |
| 4513 | network_delegate.set_block_on(stage); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4514 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4515 | TestURLRequestContext context(true); |
| 4516 | context.set_network_delegate(&network_delegate); |
| 4517 | context.Init(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4518 | |
| 4519 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4520 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4521 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4522 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4523 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4524 | d.RunUntilComplete(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4525 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4526 | // The proxy server is not set before cancellation. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4527 | if (stage == BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST || |
| 4528 | stage == BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS) { |
| 4529 | EXPECT_FALSE(r->proxy_server().is_valid()); |
| 4530 | } else if (stage == BlockingNetworkDelegate::ON_HEADERS_RECEIVED) { |
| 4531 | EXPECT_TRUE(r->proxy_server().is_direct()); |
| 4532 | } else { |
| 4533 | NOTREACHED(); |
| 4534 | } |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4535 | EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, d.request_status()); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 4536 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4537 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4538 | } |
| 4539 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4540 | } |
| 4541 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4542 | // The following 3 tests check that the network delegate can cancel a request |
| 4543 | // synchronously in various stages of the request. |
| 4544 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4545 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4546 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4547 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4548 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4549 | } |
| 4550 | |
| 4551 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4552 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4553 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4554 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4555 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4556 | } |
| 4557 | |
| 4558 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4559 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4560 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 4561 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4562 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4563 | } |
| 4564 | |
| 4565 | // The following 3 tests check that the network delegate can cancel a request |
| 4566 | // asynchronously in various stages of the request. |
| 4567 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4568 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4569 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4570 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4571 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4572 | } |
| 4573 | |
| 4574 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4575 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4576 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4577 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4578 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4579 | } |
| 4580 | |
| 4581 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4582 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4583 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 4584 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4585 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 4586 | } |
| 4587 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4588 | // Tests that the network delegate can block and redirect a request to a new |
| 4589 | // URL. |
| 4590 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4591 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4592 | |
| 4593 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4594 | BlockingNetworkDelegate network_delegate( |
| 4595 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4596 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4597 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4598 | network_delegate.set_redirect_url(redirect_url); |
| 4599 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4600 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4601 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4602 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4603 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4604 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4605 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4606 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4607 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4608 | // Quit after hitting the redirect, so can check the headers. |
| 4609 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4610 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4611 | d.RunUntilComplete(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4612 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4613 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4614 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4615 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4616 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4617 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 4618 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4619 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4620 | |
| 4621 | // Let the request finish. |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 4622 | r->FollowDeferredRedirect(base::nullopt /* modified_request_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4623 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4624 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4625 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4626 | http_test_server()->host_port_pair()), |
| 4627 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4628 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4629 | // through an untunneled proxy. |
| 4630 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4631 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4632 | http_test_server()->host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 4633 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4634 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4635 | EXPECT_EQ(redirect_url, r->url()); |
| 4636 | EXPECT_EQ(original_url, r->original_url()); |
| 4637 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 4638 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4639 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4640 | } |
| 4641 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4642 | } |
| 4643 | |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4644 | // Tests that the network delegate can block and redirect a request to a new |
| 4645 | // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 4646 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4647 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4648 | |
| 4649 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4650 | BlockingNetworkDelegate network_delegate( |
| 4651 | BlockingNetworkDelegate::SYNCHRONOUS); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4652 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4653 | network_delegate.set_redirect_url(redirect_url); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4654 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4655 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4656 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4657 | |
| 4658 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4659 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4660 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4661 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4662 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4663 | // Quit after hitting the redirect, so can check the headers. |
| 4664 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4665 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4666 | d.RunUntilComplete(); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4667 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4668 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4669 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4670 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4671 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4672 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 4673 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4674 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4675 | |
| 4676 | // Let the request finish. |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 4677 | r->FollowDeferredRedirect(base::nullopt /* modified_request_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4678 | d.RunUntilComplete(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4679 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4680 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4681 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4682 | http_test_server()->host_port_pair()), |
| 4683 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4684 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4685 | // through an untunneled proxy. |
| 4686 | EXPECT_EQ(1, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4687 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4688 | http_test_server()->host_port_pair())); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4689 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4690 | EXPECT_EQ(redirect_url, r->url()); |
| 4691 | EXPECT_EQ(original_url, r->original_url()); |
| 4692 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 4693 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4694 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4695 | } |
| 4696 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4697 | } |
| 4698 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4699 | // Tests that redirects caused by the network delegate preserve POST data. |
| 4700 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4701 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4702 | |
| 4703 | const char kData[] = "hello world"; |
| 4704 | |
| 4705 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4706 | BlockingNetworkDelegate network_delegate( |
| 4707 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4708 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4709 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4710 | network_delegate.set_redirect_url(redirect_url); |
| 4711 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4712 | TestURLRequestContext context(true); |
| 4713 | context.set_network_delegate(&network_delegate); |
| 4714 | context.Init(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4715 | |
| 4716 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4717 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4718 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4719 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4720 | r->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4721 | r->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4722 | HttpRequestHeaders headers; |
| 4723 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Brett Wilson | 5accd24 | 2017-11-30 22:07:32 | [diff] [blame] | 4724 | base::NumberToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4725 | r->SetExtraRequestHeaders(headers); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4726 | |
| 4727 | // Quit after hitting the redirect, so can check the headers. |
| 4728 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4729 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4730 | d.RunUntilComplete(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4731 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4732 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4733 | EXPECT_EQ(307, r->GetResponseCode()); |
| 4734 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4735 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4736 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 4737 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4738 | EXPECT_EQ(redirect_url, GURL(location)); |
| 4739 | |
| 4740 | // Let the request finish. |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 4741 | r->FollowDeferredRedirect(base::nullopt /* modified_request_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4742 | d.RunUntilComplete(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 4743 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4744 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4745 | EXPECT_EQ(redirect_url, r->url()); |
| 4746 | EXPECT_EQ(original_url, r->original_url()); |
| 4747 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4748 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4749 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4750 | EXPECT_EQ("POST", r->method()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 4751 | EXPECT_EQ(kData, d.data_received()); |
| 4752 | } |
| 4753 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4754 | } |
| 4755 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4756 | // Tests that the network delegate can block and redirect a request to a new |
| 4757 | // URL during OnHeadersReceived. |
| 4758 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4759 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4760 | |
| 4761 | TestDelegate d; |
| 4762 | BlockingNetworkDelegate network_delegate( |
| 4763 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4764 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4765 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4766 | network_delegate.set_redirect_on_headers_received_url(redirect_url); |
| 4767 | |
| 4768 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4769 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4770 | |
| 4771 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4772 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4773 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4774 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4775 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4776 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4777 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4778 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4779 | EXPECT_EQ(OK, d.request_status()); |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 4780 | EXPECT_EQ(ProxyServer(ProxyServer::SCHEME_HTTP, |
| 4781 | http_test_server()->host_port_pair()), |
| 4782 | r->proxy_server()); |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 4783 | // before_send_headers_with_proxy_count only increments for headers sent |
| 4784 | // through an untunneled proxy. |
| 4785 | EXPECT_EQ(2, network_delegate.before_send_headers_with_proxy_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4786 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 4787 | http_test_server()->host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4788 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4789 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4790 | EXPECT_EQ(redirect_url, r->url()); |
| 4791 | EXPECT_EQ(original_url, r->original_url()); |
| 4792 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4793 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 4794 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4795 | } |
| 4796 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4797 | } |
| 4798 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4799 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4800 | // by taking no action. This indicates that the NetworkDelegate does not want to |
| 4801 | // handle the challenge, and is passing the buck along to the |
| 4802 | // URLRequest::Delegate. |
| 4803 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4804 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4805 | |
| 4806 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4807 | BlockingNetworkDelegate network_delegate( |
| 4808 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4809 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4810 | TestURLRequestContext context(true); |
| 4811 | context.set_network_delegate(&network_delegate); |
| 4812 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4813 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4814 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4815 | |
| 4816 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4817 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4818 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4819 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4820 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4821 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4822 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4823 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4824 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4825 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4826 | EXPECT_TRUE(d.auth_required_called()); |
| 4827 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4828 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4829 | } |
| 4830 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4831 | } |
| 4832 | |
| 4833 | TEST_F(URLRequestTestHTTP, |
| 4834 | NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4835 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4836 | |
| 4837 | TestDelegate d; |
| 4838 | BlockingNetworkDelegate network_delegate( |
| 4839 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4840 | |
| 4841 | TestURLRequestContext context(true); |
| 4842 | context.set_network_delegate(&network_delegate); |
| 4843 | context.Init(); |
| 4844 | |
| 4845 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 4846 | |
| 4847 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4848 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4849 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4850 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4851 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4852 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4853 | d.RunUntilComplete(); |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4854 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4855 | { |
| 4856 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4857 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
shivanisha | 8061c420 | 2017-06-13 23:35:52 | [diff] [blame] | 4858 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4859 | } |
| 4860 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4861 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4862 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4863 | EXPECT_TRUE(d.auth_required_called()); |
| 4864 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4865 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4866 | } |
| 4867 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4868 | } |
| 4869 | |
| 4870 | // Tests that the network delegate can synchronously complete OnAuthRequired |
[email protected] | 1e110eae | 2013-05-10 22:02:40 | [diff] [blame] | 4871 | // by setting credentials. |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4872 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4873 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4874 | |
| 4875 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4876 | BlockingNetworkDelegate network_delegate( |
| 4877 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4878 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4879 | network_delegate.set_auth_retval( |
| 4880 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4881 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4882 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4883 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4884 | TestURLRequestContext context(true); |
| 4885 | context.set_network_delegate(&network_delegate); |
| 4886 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4887 | |
| 4888 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4889 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4890 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4891 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4892 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4893 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4894 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4895 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4896 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4897 | EXPECT_FALSE(d.auth_required_called()); |
| 4898 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4899 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4900 | } |
| 4901 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4902 | } |
| 4903 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4904 | // Same as above, but also tests that GetFullRequestHeaders returns the proper |
| 4905 | // headers (for the first or second request) when called at the proper times. |
| 4906 | TEST_F(URLRequestTestHTTP, |
| 4907 | NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4908 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4909 | |
| 4910 | TestDelegate d; |
| 4911 | BlockingNetworkDelegate network_delegate( |
| 4912 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4913 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 4914 | network_delegate.set_auth_retval( |
| 4915 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4916 | |
| 4917 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
| 4918 | |
| 4919 | TestURLRequestContext context(true); |
| 4920 | context.set_network_delegate(&network_delegate); |
| 4921 | context.Init(); |
| 4922 | |
| 4923 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4924 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4925 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4926 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4927 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4928 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4929 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4930 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4931 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4932 | EXPECT_FALSE(d.auth_required_called()); |
| 4933 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4934 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4935 | |
| 4936 | { |
| 4937 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4938 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4939 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
| 4940 | } |
| 4941 | } |
| 4942 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4943 | } |
| 4944 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4945 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 4946 | // by cancelling authentication. |
| 4947 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4948 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4949 | |
| 4950 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4951 | BlockingNetworkDelegate network_delegate( |
| 4952 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 4953 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4954 | network_delegate.set_auth_retval( |
| 4955 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4956 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4957 | TestURLRequestContext context(true); |
| 4958 | context.set_network_delegate(&network_delegate); |
| 4959 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4960 | |
| 4961 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4962 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4963 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4964 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4965 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 4966 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4967 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 4968 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4969 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4970 | EXPECT_FALSE(d.auth_required_called()); |
| 4971 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4972 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4973 | } |
| 4974 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4975 | } |
| 4976 | |
| 4977 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4978 | // by taking no action. This indicates that the NetworkDelegate does not want |
| 4979 | // to handle the challenge, and is passing the buck along to the |
| 4980 | // URLRequest::Delegate. |
| 4981 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4982 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4983 | |
| 4984 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4985 | BlockingNetworkDelegate network_delegate( |
| 4986 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4987 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4988 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4989 | TestURLRequestContext context(true); |
| 4990 | context.set_network_delegate(&network_delegate); |
| 4991 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4992 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4993 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4994 | |
| 4995 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4996 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 4997 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 4998 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4999 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5000 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5001 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5002 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5003 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5004 | EXPECT_TRUE(d.auth_required_called()); |
| 5005 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5006 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5007 | } |
| 5008 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5009 | } |
| 5010 | |
| 5011 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 5012 | // by setting credentials. |
| 5013 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5014 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5015 | |
| 5016 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5017 | BlockingNetworkDelegate network_delegate( |
| 5018 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 5019 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5020 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5021 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 5022 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 5023 | AuthCredentials auth_credentials(kUser, kSecret); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5024 | network_delegate.set_auth_credentials(auth_credentials); |
| 5025 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5026 | TestURLRequestContext context(true); |
| 5027 | context.set_network_delegate(&network_delegate); |
| 5028 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5029 | |
| 5030 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5031 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5032 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5033 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5034 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5035 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5036 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5037 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5038 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5039 | EXPECT_FALSE(d.auth_required_called()); |
| 5040 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5041 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5042 | } |
| 5043 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5044 | } |
| 5045 | |
| 5046 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 5047 | // by cancelling authentication. |
| 5048 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5049 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5050 | |
| 5051 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5052 | BlockingNetworkDelegate network_delegate( |
| 5053 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 5054 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5055 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5056 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 5057 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5058 | TestURLRequestContext context(true); |
| 5059 | context.set_network_delegate(&network_delegate); |
| 5060 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5061 | |
| 5062 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5063 | GURL url(http_test_server()->GetURL("/auth-basic")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5064 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5065 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5066 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5067 | d.RunUntilComplete(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5068 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5069 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5070 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 5071 | EXPECT_FALSE(d.auth_required_called()); |
| 5072 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5073 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5074 | } |
| 5075 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5076 | } |
| 5077 | |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5078 | // Tests that NetworkDelegate header overrides from the 401 response do not |
| 5079 | // affect the 200 response. This is a regression test for |
| 5080 | // https://crbug.com/801237. |
| 5081 | TEST_F(URLRequestTestHTTP, NetworkDelegateOverrideHeadersWithAuth) { |
| 5082 | ASSERT_TRUE(http_test_server()->Start()); |
| 5083 | |
| 5084 | TestDelegate d; |
| 5085 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 5086 | default_network_delegate_.set_add_header_to_first_response(true); |
| 5087 | |
| 5088 | { |
| 5089 | GURL url(http_test_server()->GetURL("/auth-basic")); |
| 5090 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5091 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 5092 | r->Start(); |
| 5093 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5094 | d.RunUntilComplete(); |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5095 | |
| 5096 | EXPECT_EQ(OK, d.request_status()); |
| 5097 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5098 | EXPECT_TRUE(d.auth_required_called()); |
| 5099 | EXPECT_FALSE(r->response_headers()->HasHeader("X-Network-Delegate")); |
| 5100 | } |
| 5101 | |
| 5102 | { |
| 5103 | GURL url(http_test_server()->GetURL("/defaultresponse")); |
| 5104 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5105 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 5106 | r->Start(); |
| 5107 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5108 | d.RunUntilComplete(); |
David Benjamin | 0bda12f | 2018-02-07 19:58:45 | [diff] [blame] | 5109 | |
| 5110 | // Check that set_add_header_to_first_response normally adds a header. |
| 5111 | EXPECT_EQ(OK, d.request_status()); |
| 5112 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5113 | EXPECT_TRUE(r->response_headers()->HasHeader("X-Network-Delegate")); |
| 5114 | } |
| 5115 | } |
| 5116 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5117 | // Tests that we can handle when a network request was canceled while we were |
| 5118 | // waiting for the network delegate. |
| 5119 | // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 5120 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5121 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5122 | |
| 5123 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5124 | BlockingNetworkDelegate network_delegate( |
| 5125 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5126 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5127 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5128 | TestURLRequestContext context(true); |
| 5129 | context.set_network_delegate(&network_delegate); |
| 5130 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5131 | |
| 5132 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5133 | std::unique_ptr<URLRequest> r( |
| 5134 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 5135 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5136 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5137 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5138 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5139 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 5140 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5141 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5142 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5143 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5144 | // Ensure that network delegate is notified. |
| 5145 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5146 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5147 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5148 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5149 | } |
| 5150 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5151 | } |
| 5152 | |
| 5153 | // Tests that we can handle when a network request was canceled while we were |
| 5154 | // waiting for the network delegate. |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5155 | // Part 2: Request is cancelled while waiting for OnBeforeStartTransaction |
| 5156 | // callback. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5157 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5158 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5159 | |
| 5160 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5161 | BlockingNetworkDelegate network_delegate( |
| 5162 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5163 | network_delegate.set_block_on( |
| 5164 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5165 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5166 | TestURLRequestContext context(true); |
| 5167 | context.set_network_delegate(&network_delegate); |
| 5168 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5169 | |
| 5170 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5171 | std::unique_ptr<URLRequest> r( |
| 5172 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 5173 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5174 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5175 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5176 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5177 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 5178 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5179 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5180 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5181 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5182 | // Ensure that network delegate is notified. |
| 5183 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5184 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5185 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5186 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5187 | } |
| 5188 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5189 | } |
| 5190 | |
| 5191 | // Tests that we can handle when a network request was canceled while we were |
| 5192 | // waiting for the network delegate. |
| 5193 | // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 5194 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5195 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5196 | |
| 5197 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5198 | BlockingNetworkDelegate network_delegate( |
| 5199 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5200 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5201 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5202 | TestURLRequestContext context(true); |
| 5203 | context.set_network_delegate(&network_delegate); |
| 5204 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5205 | |
| 5206 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5207 | std::unique_ptr<URLRequest> r( |
| 5208 | context.CreateRequest(http_test_server()->GetURL("/"), DEFAULT_PRIORITY, |
| 5209 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5210 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5211 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5212 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5213 | EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 5214 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5215 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5216 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5217 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5218 | // Ensure that network delegate is notified. |
| 5219 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5220 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5221 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5222 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5223 | } |
| 5224 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5225 | } |
| 5226 | |
| 5227 | // Tests that we can handle when a network request was canceled while we were |
| 5228 | // waiting for the network delegate. |
| 5229 | // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
[email protected] | bfe8b30 | 2013-02-15 12:16:02 | [diff] [blame] | 5230 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5231 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5232 | |
| 5233 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5234 | BlockingNetworkDelegate network_delegate( |
| 5235 | BlockingNetworkDelegate::USER_CALLBACK); |
| 5236 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5237 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5238 | TestURLRequestContext context(true); |
| 5239 | context.set_network_delegate(&network_delegate); |
| 5240 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5241 | |
| 5242 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5243 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5244 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 5245 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5246 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5247 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5248 | network_delegate.RunUntilBlocked(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 5249 | EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED, |
| 5250 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5251 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 5252 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5253 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5254 | // Ensure that network delegate is notified. |
| 5255 | EXPECT_EQ(1, network_delegate.completed_requests()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5256 | EXPECT_EQ(1, network_delegate.canceled_requests()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 5257 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5258 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5259 | } |
| 5260 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5261 | } |
| 5262 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5263 | namespace { |
| 5264 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5265 | std::unique_ptr<test_server::HttpResponse> HandleServerAuthConnect( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5266 | const test_server::HttpRequest& request) { |
| 5267 | if (request.headers.find("Host") == request.headers.end() || |
| 5268 | request.headers.at("Host") != "www.server-auth.com" || |
| 5269 | request.method != test_server::METHOD_CONNECT) { |
| 5270 | return nullptr; |
| 5271 | } |
| 5272 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5273 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5274 | new test_server::BasicHttpResponse); |
| 5275 | http_response->set_code(HTTP_UNAUTHORIZED); |
| 5276 | http_response->AddCustomHeader("WWW-Authenticate", |
| 5277 | "Basic realm=\"WallyWorld\""); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 5278 | return std::move(http_response); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5279 | } |
| 5280 | |
| 5281 | } // namespace |
| 5282 | |
| 5283 | // 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] | 5284 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5285 | // The EmbeddedTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5286 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5287 | http_test_server()->RegisterRequestHandler( |
Matt Menke | 2b2a6047 | 2018-02-07 02:12:12 | [diff] [blame] | 5288 | base::BindRepeating(&HandleServerAuthConnect)); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5289 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5290 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5291 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 5292 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5293 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 5294 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5295 | TestDelegate d; |
| 5296 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5297 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5298 | GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d, |
| 5299 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5300 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5301 | r->Start(); |
| 5302 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5303 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5304 | d.RunUntilComplete(); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5305 | |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 5306 | // The proxy server is not set before failure. |
tbansal | 2ecbbc7 | 2016-10-06 17:15:47 | [diff] [blame] | 5307 | EXPECT_FALSE(r->proxy_server().is_valid()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5308 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, d.request_status()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 5309 | } |
| 5310 | } |
| 5311 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5312 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5313 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5314 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5315 | TestDelegate d; |
| 5316 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5317 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5318 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5319 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5320 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5321 | r->Start(); |
| 5322 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5323 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5324 | d.RunUntilComplete(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5325 | |
| 5326 | EXPECT_EQ(1, d.response_started_count()); |
| 5327 | EXPECT_FALSE(d.received_data_before_response()); |
| 5328 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5329 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5330 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5331 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5332 | r->GetSocketAddress().port()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 5333 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5334 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5335 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5336 | } |
| 5337 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5338 | // This test has the server send a large number of cookies to the client. |
| 5339 | // To ensure that no number of cookies causes a crash, a galloping binary |
| 5340 | // search is used to estimate that maximum number of cookies that are accepted |
| 5341 | // by the browser. Beyond the maximum number, the request will fail with |
| 5342 | // ERR_RESPONSE_HEADERS_TOO_BIG. |
Sergey Ulanov | 0fb900c | 2017-09-20 00:27:31 | [diff] [blame] | 5343 | #if defined(OS_WIN) || defined(OS_FUCHSIA) |
[email protected] | 69dd6fe | 2013-02-23 23:15:30 | [diff] [blame] | 5344 | // http://crbug.com/177916 |
| 5345 | #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies |
| 5346 | #else |
| 5347 | #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies |
| 5348 | #endif // defined(OS_WIN) |
| 5349 | TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5350 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5351 | |
| 5352 | int lower_bound = 0; |
| 5353 | int upper_bound = 1; |
| 5354 | |
| 5355 | // Double the number of cookies until the response header limits are |
| 5356 | // exceeded. |
| 5357 | while (DoManyCookiesRequest(upper_bound)) { |
| 5358 | lower_bound = upper_bound; |
| 5359 | upper_bound *= 2; |
| 5360 | ASSERT_LT(upper_bound, 1000000); |
| 5361 | } |
| 5362 | |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 5363 | int tolerance = static_cast<int>(upper_bound * 0.005); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 5364 | if (tolerance < 2) |
| 5365 | tolerance = 2; |
| 5366 | |
| 5367 | // Perform a binary search to find the highest possible number of cookies, |
| 5368 | // within the desired tolerance. |
| 5369 | while (upper_bound - lower_bound >= tolerance) { |
| 5370 | int num_cookies = (lower_bound + upper_bound) / 2; |
| 5371 | |
| 5372 | if (DoManyCookiesRequest(num_cookies)) |
| 5373 | lower_bound = num_cookies; |
| 5374 | else |
| 5375 | upper_bound = num_cookies; |
| 5376 | } |
| 5377 | // Success: the test did not crash. |
| 5378 | } |
| 5379 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 5380 | TEST_F(URLRequestTestHTTP, GetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5381 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5382 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5383 | TestDelegate d; |
| 5384 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5385 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5386 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5387 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5388 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5389 | r->Start(); |
| 5390 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5391 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5392 | d.RunUntilComplete(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5393 | |
| 5394 | EXPECT_EQ(1, d.response_started_count()); |
| 5395 | EXPECT_FALSE(d.received_data_before_response()); |
| 5396 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5397 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5398 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5399 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5400 | r->GetSocketAddress().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 5401 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 5402 | } |
| 5403 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5404 | TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5405 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5406 | |
| 5407 | TestDelegate d; |
| 5408 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5409 | GURL test_url(http_test_server()->GetURL("/defaultresponse")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5410 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5411 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5412 | |
| 5413 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5414 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5415 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5416 | r->Start(); |
| 5417 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5418 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5419 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5420 | |
| 5421 | EXPECT_EQ(1, d.response_started_count()); |
| 5422 | EXPECT_FALSE(d.received_data_before_response()); |
| 5423 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5424 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5425 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5426 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5427 | r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5428 | |
| 5429 | EXPECT_TRUE(d.have_full_request_headers()); |
| 5430 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 5431 | } |
| 5432 | } |
| 5433 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5434 | TEST_F(URLRequestTestHTTP, GetTestLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5435 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5436 | |
| 5437 | TestDelegate d; |
| 5438 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5439 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5440 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 5441 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5442 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5443 | r->Start(); |
| 5444 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5445 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5446 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5447 | |
| 5448 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5449 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5450 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5451 | |
| 5452 | EXPECT_EQ(1, d.response_started_count()); |
| 5453 | EXPECT_FALSE(d.received_data_before_response()); |
| 5454 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5455 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5456 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5457 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5458 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5459 | } |
| 5460 | } |
| 5461 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5462 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 5463 | #if !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5464 | TEST_F(URLRequestTestHTTP, GetZippedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5465 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5466 | base::FilePath(kTestFilePath)); |
| 5467 | |
| 5468 | ASSERT_TRUE(test_server.Start()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5469 | |
| 5470 | // Parameter that specifies the Content-Length field in the response: |
| 5471 | // C - Compressed length. |
| 5472 | // U - Uncompressed length. |
| 5473 | // L - Large length (larger than both C & U). |
| 5474 | // M - Medium length (between C & U). |
| 5475 | // S - Small length (smaller than both C & U). |
| 5476 | const char test_parameters[] = "CULMS"; |
| 5477 | const int num_tests = arraysize(test_parameters)- 1; // Skip NULL. |
| 5478 | // C & U should be OK. |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 5479 | // L & M are larger than the data sent, and show an error. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5480 | // S has too little data, but we seem to accept it. |
| 5481 | const bool test_expect_success[num_tests] = |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 5482 | { true, true, false, false, true }; |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5483 | |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5484 | base::FilePath file_path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 5485 | base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5486 | file_path = file_path.Append(kTestFilePath); |
| 5487 | file_path = file_path.Append(FILE_PATH_LITERAL("BullRunSpeech.txt")); |
| 5488 | std::string expected_content; |
| 5489 | ASSERT_TRUE(base::ReadFileToString(file_path, &expected_content)); |
| 5490 | |
| 5491 | for (int i = 0; i < num_tests; i++) { |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5492 | TestDelegate d; |
| 5493 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5494 | std::string test_file = base::StringPrintf( |
| 5495 | "compressedfiles/BullRunSpeech.txt?%c", test_parameters[i]); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5496 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5497 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5498 | TestURLRequestContext context(true); |
| 5499 | context.set_network_delegate(&network_delegate); |
| 5500 | context.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 5501 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5502 | std::unique_ptr<URLRequest> r( |
| 5503 | context.CreateRequest(test_server.GetURL(test_file), DEFAULT_PRIORITY, |
| 5504 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5505 | r->Start(); |
| 5506 | EXPECT_TRUE(r->is_pending()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5507 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5508 | d.RunUntilComplete(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5509 | |
| 5510 | EXPECT_EQ(1, d.response_started_count()); |
| 5511 | EXPECT_FALSE(d.received_data_before_response()); |
| 5512 | VLOG(1) << " Received " << d.bytes_received() << " bytes" |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5513 | << " error = " << d.request_status(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5514 | if (test_expect_success[i]) { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5515 | EXPECT_EQ(OK, d.request_status()) << " Parameter = \"" << test_file |
| 5516 | << "\""; |
xunjieli | fb4da22 | 2016-07-14 18:38:59 | [diff] [blame] | 5517 | if (test_parameters[i] == 'S') { |
| 5518 | // When content length is smaller than both compressed length and |
| 5519 | // uncompressed length, HttpStreamParser might not read the full |
| 5520 | // response body. |
| 5521 | continue; |
| 5522 | } |
| 5523 | EXPECT_EQ(expected_content, d.data_received()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5524 | } else { |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5525 | EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, d.request_status()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5526 | << " Parameter = \"" << test_file << "\""; |
| 5527 | } |
| 5528 | } |
| 5529 | } |
| 5530 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5531 | #endif // !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 5532 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5533 | TEST_F(URLRequestTestHTTP, RedirectLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5534 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5535 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5536 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5537 | GURL original_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5538 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5539 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5540 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5541 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5542 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5543 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5544 | |
| 5545 | EXPECT_EQ(1, d.response_started_count()); |
| 5546 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5547 | EXPECT_EQ(destination_url, req->url()); |
| 5548 | EXPECT_EQ(original_url, req->original_url()); |
| 5549 | ASSERT_EQ(2U, req->url_chain().size()); |
| 5550 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5551 | EXPECT_EQ(destination_url, req->url_chain()[1]); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5552 | |
| 5553 | LoadTimingInfo load_timing_info_before_redirect; |
| 5554 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect( |
| 5555 | &load_timing_info_before_redirect)); |
| 5556 | TestLoadTimingNotReused(load_timing_info_before_redirect, |
| 5557 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 5558 | |
| 5559 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5560 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5561 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5562 | |
| 5563 | // Check that a new socket was used on redirect, since the server does not |
| 5564 | // supposed keep-alive sockets, and that the times before the redirect are |
| 5565 | // before the ones recorded for the second request. |
| 5566 | EXPECT_NE(load_timing_info_before_redirect.socket_log_id, |
| 5567 | load_timing_info.socket_log_id); |
| 5568 | EXPECT_LE(load_timing_info_before_redirect.receive_headers_end, |
| 5569 | load_timing_info.connect_timing.connect_start); |
| 5570 | } |
| 5571 | |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5572 | TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5573 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5574 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5575 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 5576 | GURL middle_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5577 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
| 5578 | GURL original_url = http_test_server()->GetURL("/server-redirect?" + |
| 5579 | middle_redirect_url.spec()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5580 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5581 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5582 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5583 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5584 | d.RunUntilComplete(); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5585 | |
| 5586 | EXPECT_EQ(1, d.response_started_count()); |
| 5587 | EXPECT_EQ(2, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5588 | EXPECT_EQ(destination_url, req->url()); |
| 5589 | EXPECT_EQ(original_url, req->original_url()); |
| 5590 | ASSERT_EQ(3U, req->url_chain().size()); |
| 5591 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 5592 | EXPECT_EQ(middle_redirect_url, req->url_chain()[1]); |
| 5593 | EXPECT_EQ(destination_url, req->url_chain()[2]); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 5594 | } |
| 5595 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5596 | // First and second pieces of information logged by delegates to URLRequests. |
| 5597 | const char kFirstDelegateInfo[] = "Wonderful delegate"; |
| 5598 | const char kSecondDelegateInfo[] = "Exciting delegate"; |
| 5599 | |
| 5600 | // Logs delegate information to a URLRequest. The first string is logged |
| 5601 | // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is |
| 5602 | // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then |
| 5603 | // another asynchronous call is used to clear the delegate information |
| 5604 | // before calling a callback. The object then deletes itself. |
| 5605 | class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> { |
| 5606 | public: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5607 | using Callback = base::OnceCallback<void()>; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5608 | |
| 5609 | // Each time delegate information is added to the URLRequest, the resulting |
| 5610 | // load state is checked. The expected load state after each request is |
| 5611 | // passed in as an argument. |
| 5612 | static void Run(URLRequest* url_request, |
| 5613 | LoadState expected_first_load_state, |
| 5614 | LoadState expected_second_load_state, |
| 5615 | LoadState expected_third_load_state, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5616 | Callback callback) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5617 | AsyncDelegateLogger* logger = new AsyncDelegateLogger( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5618 | url_request, expected_first_load_state, expected_second_load_state, |
| 5619 | expected_third_load_state, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5620 | logger->Start(); |
| 5621 | } |
| 5622 | |
| 5623 | // Checks that the log entries, starting with log_position, contain the |
| 5624 | // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have |
| 5625 | // recorded. Returns the index of entry after the expected number of |
| 5626 | // events this logged, or entries.size() if there aren't enough entries. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5627 | static size_t CheckDelegateInfo(const TestNetLogEntry::List& entries, |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 5628 | size_t log_position) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5629 | // There should be 4 DELEGATE_INFO events: Two begins and two ends. |
| 5630 | if (log_position + 3 >= entries.size()) { |
| 5631 | ADD_FAILURE() << "Not enough log entries"; |
| 5632 | return entries.size(); |
| 5633 | } |
| 5634 | std::string delegate_info; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5635 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5636 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
rdsmith | 37a0dde | 2017-01-04 00:12:07 | [diff] [blame] | 5637 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_blocked_by", |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5638 | &delegate_info)); |
| 5639 | EXPECT_EQ(kFirstDelegateInfo, delegate_info); |
| 5640 | |
| 5641 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5642 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5643 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5644 | |
| 5645 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5646 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5647 | EXPECT_EQ(NetLogEventPhase::BEGIN, entries[log_position].phase); |
rdsmith | 37a0dde | 2017-01-04 00:12:07 | [diff] [blame] | 5648 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_blocked_by", |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5649 | &delegate_info)); |
| 5650 | EXPECT_EQ(kSecondDelegateInfo, delegate_info); |
| 5651 | |
| 5652 | ++log_position; |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5653 | EXPECT_EQ(NetLogEventType::DELEGATE_INFO, entries[log_position].type); |
| 5654 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5655 | |
| 5656 | return log_position + 1; |
| 5657 | } |
| 5658 | |
| 5659 | private: |
| 5660 | friend class base::RefCounted<AsyncDelegateLogger>; |
| 5661 | |
| 5662 | AsyncDelegateLogger(URLRequest* url_request, |
| 5663 | LoadState expected_first_load_state, |
| 5664 | LoadState expected_second_load_state, |
| 5665 | LoadState expected_third_load_state, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5666 | Callback callback) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5667 | : url_request_(url_request), |
| 5668 | expected_first_load_state_(expected_first_load_state), |
| 5669 | expected_second_load_state_(expected_second_load_state), |
| 5670 | expected_third_load_state_(expected_third_load_state), |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5671 | callback_(std::move(callback)) {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5672 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5673 | ~AsyncDelegateLogger() = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5674 | |
| 5675 | void Start() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5676 | url_request_->LogBlockedBy(kFirstDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5677 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5678 | EXPECT_EQ(expected_first_load_state_, load_state.state); |
| 5679 | EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5680 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5681 | FROM_HERE, |
| 5682 | base::BindOnce(&AsyncDelegateLogger::LogSecondDelegate, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5683 | } |
| 5684 | |
| 5685 | void LogSecondDelegate() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5686 | url_request_->LogAndReportBlockedBy(kSecondDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5687 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5688 | EXPECT_EQ(expected_second_load_state_, load_state.state); |
| 5689 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) { |
| 5690 | EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5691 | } else { |
| 5692 | EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 5693 | } |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5694 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5695 | FROM_HERE, base::BindOnce(&AsyncDelegateLogger::LogComplete, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5696 | } |
| 5697 | |
| 5698 | void LogComplete() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 5699 | url_request_->LogUnblocked(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5700 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 5701 | EXPECT_EQ(expected_third_load_state_, load_state.state); |
| 5702 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5703 | EXPECT_EQ(base::string16(), load_state.param); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5704 | std::move(callback_).Run(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5705 | } |
| 5706 | |
| 5707 | URLRequest* url_request_; |
| 5708 | const int expected_first_load_state_; |
| 5709 | const int expected_second_load_state_; |
| 5710 | const int expected_third_load_state_; |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5711 | Callback callback_; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5712 | |
| 5713 | DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger); |
| 5714 | }; |
| 5715 | |
| 5716 | // NetworkDelegate that logs delegate information before a request is started, |
| 5717 | // before headers are sent, when headers are read, and when auth information |
| 5718 | // is requested. Uses AsyncDelegateLogger. |
| 5719 | class AsyncLoggingNetworkDelegate : public TestNetworkDelegate { |
| 5720 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5721 | AsyncLoggingNetworkDelegate() = default; |
| 5722 | ~AsyncLoggingNetworkDelegate() override = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5723 | |
| 5724 | // NetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5725 | int OnBeforeURLRequest(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5726 | CompletionOnceCallback callback, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5727 | GURL* new_url) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5728 | // TestNetworkDelegate always completes synchronously. |
| 5729 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeURLRequest( |
| 5730 | request, base::NullCallback(), new_url)); |
| 5731 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5732 | } |
| 5733 | |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5734 | int OnBeforeStartTransaction(URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5735 | CompletionOnceCallback callback, |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 5736 | HttpRequestHeaders* headers) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5737 | // TestNetworkDelegate always completes synchronously. |
| 5738 | CHECK_NE(ERR_IO_PENDING, TestNetworkDelegate::OnBeforeStartTransaction( |
| 5739 | request, base::NullCallback(), headers)); |
| 5740 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5741 | } |
| 5742 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5743 | int OnHeadersReceived( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5744 | URLRequest* request, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5745 | CompletionOnceCallback callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5746 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5747 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5748 | GURL* allowed_unsafe_redirect_url) override { |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5749 | // TestNetworkDelegate always completes synchronously. |
| 5750 | CHECK_NE(ERR_IO_PENDING, |
| 5751 | TestNetworkDelegate::OnHeadersReceived( |
| 5752 | request, base::NullCallback(), original_response_headers, |
| 5753 | override_response_headers, allowed_unsafe_redirect_url)); |
| 5754 | return RunCallbackAsynchronously(request, std::move(callback)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5755 | } |
| 5756 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5757 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5758 | URLRequest* request, |
| 5759 | const AuthChallengeInfo& auth_info, |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5760 | AuthCallback callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 5761 | AuthCredentials* credentials) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5762 | AsyncDelegateLogger::Run( |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5763 | request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5764 | LOAD_STATE_WAITING_FOR_DELEGATE, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5765 | base::BindOnce(&AsyncLoggingNetworkDelegate::SetAuthAndResume, |
| 5766 | std::move(callback), credentials)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5767 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 5768 | } |
| 5769 | |
| 5770 | private: |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5771 | static int RunCallbackAsynchronously(URLRequest* request, |
| 5772 | CompletionOnceCallback callback) { |
| 5773 | AsyncDelegateLogger::Run(request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5774 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5775 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5776 | base::BindOnce(std::move(callback), OK)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5777 | return ERR_IO_PENDING; |
| 5778 | } |
| 5779 | |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5780 | static void SetAuthAndResume(AuthCallback callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5781 | AuthCredentials* credentials) { |
| 5782 | *credentials = AuthCredentials(kUser, kSecret); |
David Benjamin | d1f287bf | 2018-06-12 01:57:20 | [diff] [blame] | 5783 | std::move(callback).Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5784 | } |
| 5785 | |
| 5786 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate); |
| 5787 | }; |
| 5788 | |
| 5789 | // URLRequest::Delegate that logs delegate information when the headers |
| 5790 | // are received, when each read completes, and during redirects. Uses |
| 5791 | // AsyncDelegateLogger. Can optionally cancel a request in any phase. |
| 5792 | // |
| 5793 | // Inherits from TestDelegate to reuse the TestDelegate code to handle |
| 5794 | // advancing to the next step in most cases, as well as cancellation. |
| 5795 | class AsyncLoggingUrlRequestDelegate : public TestDelegate { |
| 5796 | public: |
| 5797 | enum CancelStage { |
| 5798 | NO_CANCEL = 0, |
| 5799 | CANCEL_ON_RECEIVED_REDIRECT, |
| 5800 | CANCEL_ON_RESPONSE_STARTED, |
| 5801 | CANCEL_ON_READ_COMPLETED |
| 5802 | }; |
| 5803 | |
| 5804 | explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage) |
| 5805 | : cancel_stage_(cancel_stage) { |
| 5806 | if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT) |
| 5807 | set_cancel_in_received_redirect(true); |
| 5808 | else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED) |
| 5809 | set_cancel_in_response_started(true); |
| 5810 | else if (cancel_stage == CANCEL_ON_READ_COMPLETED) |
| 5811 | set_cancel_in_received_data(true); |
| 5812 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 5813 | ~AsyncLoggingUrlRequestDelegate() override = default; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5814 | |
| 5815 | // URLRequest::Delegate implementation: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5816 | void OnReceivedRedirect(URLRequest* request, |
| 5817 | const RedirectInfo& redirect_info, |
| 5818 | bool* defer_redirect) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5819 | *defer_redirect = true; |
| 5820 | AsyncDelegateLogger::Run( |
| 5821 | request, |
| 5822 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5823 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5824 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5825 | base::Bind( |
| 5826 | &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5827 | base::Unretained(this), request, redirect_info)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5828 | } |
| 5829 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5830 | void OnResponseStarted(URLRequest* request, int net_error) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5831 | AsyncDelegateLogger::Run( |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5832 | request, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5833 | LOAD_STATE_WAITING_FOR_DELEGATE, LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5834 | base::Bind( |
| 5835 | &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete, |
| 5836 | base::Unretained(this), request, net_error)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5837 | } |
| 5838 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5839 | void OnReadCompleted(URLRequest* request, int bytes_read) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5840 | AsyncDelegateLogger::Run( |
| 5841 | request, |
| 5842 | LOAD_STATE_IDLE, |
| 5843 | LOAD_STATE_IDLE, |
| 5844 | LOAD_STATE_IDLE, |
| 5845 | base::Bind( |
| 5846 | &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete, |
| 5847 | base::Unretained(this), request, bytes_read)); |
| 5848 | } |
| 5849 | |
| 5850 | private: |
| 5851 | void OnReceivedRedirectLoggingComplete(URLRequest* request, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5852 | const RedirectInfo& redirect_info) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5853 | bool defer_redirect = false; |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5854 | TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5855 | // FollowDeferredRedirect should not be called after cancellation. |
| 5856 | if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT) |
| 5857 | return; |
| 5858 | if (!defer_redirect) |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 5859 | request->FollowDeferredRedirect( |
| 5860 | base::nullopt /* modified_request_headers */); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5861 | } |
| 5862 | |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5863 | void OnResponseStartedLoggingComplete(URLRequest* request, int net_error) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5864 | // The parent class continues the request. |
maksim.sisov | 0f4aa14 | 2016-09-05 05:55:28 | [diff] [blame] | 5865 | TestDelegate::OnResponseStarted(request, net_error); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5866 | } |
| 5867 | |
| 5868 | void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) { |
| 5869 | // The parent class continues the request. |
| 5870 | TestDelegate::OnReadCompleted(request, bytes_read); |
| 5871 | } |
| 5872 | |
| 5873 | const CancelStage cancel_stage_; |
| 5874 | |
| 5875 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate); |
| 5876 | }; |
| 5877 | |
| 5878 | // Tests handling of delegate info before a request starts. |
| 5879 | TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5880 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5881 | |
| 5882 | TestDelegate request_delegate; |
| 5883 | TestURLRequestContext context(true); |
| 5884 | context.set_network_delegate(NULL); |
| 5885 | context.set_net_log(&net_log_); |
| 5886 | context.Init(); |
| 5887 | |
| 5888 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5889 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5890 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, |
| 5891 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5892 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5893 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5894 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5895 | |
| 5896 | AsyncDelegateLogger::Run( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5897 | r.get(), |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5898 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5899 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 5900 | LOAD_STATE_IDLE, |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5901 | base::Bind(&URLRequest::Start, base::Unretained(r.get()))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5902 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5903 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5904 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5905 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5906 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5907 | } |
| 5908 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5909 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5910 | net_log_.GetEntries(&entries); |
| 5911 | size_t log_position = ExpectLogContainsSomewhereAfter( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5912 | entries, 0, NetLogEventType::DELEGATE_INFO, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5913 | |
| 5914 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position); |
| 5915 | |
| 5916 | // Nothing else should add any delegate info to the request. |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5917 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5918 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5919 | } |
| 5920 | |
| 5921 | // Tests handling of delegate info from a network delegate. |
| 5922 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5923 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5924 | |
| 5925 | TestDelegate request_delegate; |
| 5926 | AsyncLoggingNetworkDelegate network_delegate; |
| 5927 | TestURLRequestContext context(true); |
| 5928 | context.set_network_delegate(&network_delegate); |
| 5929 | context.set_net_log(&net_log_); |
| 5930 | context.Init(); |
| 5931 | |
| 5932 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5933 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 5934 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, |
| 5935 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5936 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5937 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5938 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5939 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5940 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5941 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5942 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5943 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5944 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5945 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5946 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5947 | } |
| 5948 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5949 | |
| 5950 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5951 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5952 | net_log_.GetEntries(&entries); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 5953 | static const NetLogEventType kExpectedEvents[] = { |
| 5954 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 5955 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 5956 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 5957 | }; |
| 5958 | for (NetLogEventType event : kExpectedEvents) { |
| 5959 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5960 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 5961 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5962 | |
| 5963 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5964 | log_position + 1); |
| 5965 | |
| 5966 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 5967 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5968 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5969 | } |
| 5970 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 5971 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 5972 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5973 | } |
| 5974 | |
| 5975 | // Tests handling of delegate info from a network delegate in the case of an |
| 5976 | // HTTP redirect. |
| 5977 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5978 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5979 | |
| 5980 | TestDelegate request_delegate; |
| 5981 | AsyncLoggingNetworkDelegate network_delegate; |
| 5982 | TestURLRequestContext context(true); |
| 5983 | context.set_network_delegate(&network_delegate); |
| 5984 | context.set_net_log(&net_log_); |
| 5985 | context.Init(); |
| 5986 | |
| 5987 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 5988 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5989 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 5990 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5991 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5992 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5993 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5994 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5995 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 5996 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5997 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5998 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 5999 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6000 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 6001 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 6002 | } |
| 6003 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 6004 | |
| 6005 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6006 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6007 | net_log_.GetEntries(&entries); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6008 | static const NetLogEventType kExpectedEvents[] = { |
| 6009 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 6010 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6011 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6012 | }; |
| 6013 | for (NetLogEventType event : kExpectedEvents) { |
| 6014 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6015 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6016 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6017 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6018 | log_position = |
| 6019 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6020 | |
| 6021 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6022 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6023 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6024 | } |
| 6025 | |
| 6026 | // The URLRequest::Delegate then gets informed about the redirect. |
| 6027 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6028 | entries, log_position + 1, |
| 6029 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6030 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6031 | |
| 6032 | // The NetworkDelegate logged information in the same three events as before. |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6033 | for (NetLogEventType event : kExpectedEvents) { |
| 6034 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6035 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6036 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6037 | |
| 6038 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 6039 | log_position + 1); |
| 6040 | |
| 6041 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6042 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6043 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6044 | } |
| 6045 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6046 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6047 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6048 | } |
| 6049 | |
| 6050 | // Tests handling of delegate info from a network delegate in the case of HTTP |
| 6051 | // AUTH. |
| 6052 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6053 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6054 | |
| 6055 | TestDelegate request_delegate; |
| 6056 | AsyncLoggingNetworkDelegate network_delegate; |
| 6057 | TestURLRequestContext context(true); |
| 6058 | context.set_network_delegate(&network_delegate); |
| 6059 | context.set_net_log(&net_log_); |
| 6060 | context.Init(); |
| 6061 | |
| 6062 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6063 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 6064 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, |
| 6065 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6066 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6067 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 6068 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6069 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6070 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6071 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6072 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6073 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6074 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6075 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 6076 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 6077 | } |
| 6078 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 6079 | |
| 6080 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6081 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6082 | net_log_.GetEntries(&entries); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6083 | static const NetLogEventType kExpectedEvents[] = { |
| 6084 | NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST, |
| 6085 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6086 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6087 | NetLogEventType::NETWORK_DELEGATE_AUTH_REQUIRED, |
| 6088 | NetLogEventType::NETWORK_DELEGATE_BEFORE_START_TRANSACTION, |
| 6089 | NetLogEventType::NETWORK_DELEGATE_HEADERS_RECEIVED, |
| 6090 | }; |
| 6091 | for (NetLogEventType event : kExpectedEvents) { |
| 6092 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6093 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6094 | entries, log_position + 1, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6095 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6096 | log_position = |
| 6097 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6098 | |
| 6099 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6100 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6101 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6102 | } |
| 6103 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6104 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6105 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6106 | } |
| 6107 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6108 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 6109 | #if !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6110 | // Tests handling of delegate info from a URLRequest::Delegate. |
| 6111 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6112 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6113 | base::FilePath(kTestFilePath)); |
| 6114 | |
| 6115 | ASSERT_TRUE(test_server.Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6116 | |
| 6117 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 6118 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 6119 | TestURLRequestContext context(true); |
| 6120 | context.set_network_delegate(NULL); |
| 6121 | context.set_net_log(&net_log_); |
| 6122 | context.Init(); |
| 6123 | |
| 6124 | { |
| 6125 | // A chunked response with delays between chunks is used to make sure that |
| 6126 | // attempts by the URLRequest delegate to log information while reading the |
| 6127 | // body are ignored. Since they are ignored, this test is robust against |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6128 | // the possibility of multiple reads being combined in the unlikely event |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6129 | // that it occurs. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6130 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6131 | test_server.GetURL("/chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6132 | &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6133 | LoadStateWithParam load_state = r->GetLoadState(); |
| 6134 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6135 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6136 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6137 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6138 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6139 | } |
| 6140 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6141 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6142 | net_log_.GetEntries(&entries); |
| 6143 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6144 | size_t log_position = 0; |
| 6145 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6146 | // The delegate info should only have been logged on header complete. Other |
| 6147 | // times it should silently be ignored. |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6148 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 6149 | entries, 0, NetLogEventType::NETWORK_DELEGATE_BEFORE_URL_REQUEST)); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6150 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6151 | entries, log_position + 1, |
| 6152 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6153 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6154 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6155 | log_position = |
| 6156 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6157 | |
| 6158 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6159 | EXPECT_EQ(NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 6160 | entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6161 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6162 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6163 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6164 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6165 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6166 | entries, log_position + 1, |
| 6167 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6168 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6169 | #endif // !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6170 | |
| 6171 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 6172 | // an HTTP redirect. |
| 6173 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6174 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6175 | |
| 6176 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 6177 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 6178 | TestURLRequestContext context(true); |
| 6179 | context.set_network_delegate(NULL); |
| 6180 | context.set_net_log(&net_log_); |
| 6181 | context.Init(); |
| 6182 | |
| 6183 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6184 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6185 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6186 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6187 | LoadStateWithParam load_state = r->GetLoadState(); |
| 6188 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6189 | request_delegate.RunUntilComplete(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6190 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6191 | EXPECT_EQ(200, r->GetResponseCode()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6192 | EXPECT_EQ(OK, request_delegate.request_status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6193 | } |
| 6194 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6195 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6196 | net_log_.GetEntries(&entries); |
| 6197 | |
| 6198 | // Delegate info should only have been logged in OnReceivedRedirect and |
| 6199 | // OnResponseStarted. |
| 6200 | size_t log_position = 0; |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6201 | static const NetLogEventType kExpectedEvents[] = { |
| 6202 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
| 6203 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 6204 | }; |
| 6205 | for (NetLogEventType event : kExpectedEvents) { |
| 6206 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
| 6207 | log_position = ExpectLogContainsSomewhereAfter(entries, log_position, event, |
| 6208 | NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6209 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6210 | log_position = |
| 6211 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6212 | |
| 6213 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6214 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6215 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6216 | } |
| 6217 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6218 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6219 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6220 | } |
| 6221 | |
| 6222 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 6223 | // an HTTP redirect, with cancellation at various points. |
| 6224 | TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6225 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6226 | |
| 6227 | const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = { |
| 6228 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT, |
| 6229 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED, |
| 6230 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED, |
| 6231 | }; |
| 6232 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6233 | for (auto cancel_stage : kCancelStages) { |
| 6234 | AsyncLoggingUrlRequestDelegate request_delegate(cancel_stage); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 6235 | TestNetLog net_log; |
krasin | 0bfeb6b | 2017-01-13 21:48:04 | [diff] [blame] | 6236 | TestURLRequestContext context(true); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6237 | context.set_network_delegate(NULL); |
| 6238 | context.set_net_log(&net_log); |
| 6239 | context.Init(); |
| 6240 | |
| 6241 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6242 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6243 | http_test_server()->GetURL("/server-redirect?simple.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6244 | DEFAULT_PRIORITY, &request_delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6245 | LoadStateWithParam load_state = r->GetLoadState(); |
| 6246 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6247 | request_delegate.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6248 | EXPECT_EQ(ERR_ABORTED, request_delegate.request_status()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6249 | |
| 6250 | // Spin the message loop to run AsyncDelegateLogger task(s) posted after |
| 6251 | // the |request_delegate| completion task. |
| 6252 | base::RunLoop().RunUntilIdle(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6253 | } |
| 6254 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 6255 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6256 | net_log.GetEntries(&entries); |
| 6257 | |
| 6258 | // Delegate info is always logged in both OnReceivedRedirect and |
| 6259 | // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the |
| 6260 | // OnResponseStarted delegate call is after cancellation, but logging is |
| 6261 | // still currently supported in that call. |
| 6262 | size_t log_position = 0; |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6263 | static const NetLogEventType kExpectedEvents[] = { |
| 6264 | NetLogEventType::URL_REQUEST_DELEGATE_RECEIVED_REDIRECT, |
| 6265 | NetLogEventType::URL_REQUEST_DELEGATE_RESPONSE_STARTED, |
| 6266 | }; |
| 6267 | for (NetLogEventType event : kExpectedEvents) { |
| 6268 | SCOPED_TRACE(NetLog::EventTypeToString(event)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6269 | log_position = ExpectLogContainsSomewhereAfter( |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6270 | entries, log_position, event, NetLogEventPhase::BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6271 | |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6272 | log_position = |
| 6273 | AsyncDelegateLogger::CheckDelegateInfo(entries, log_position + 1); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6274 | |
| 6275 | ASSERT_LT(log_position, entries.size()); |
David Benjamin | 9776ca2 | 2018-06-13 00:00:15 | [diff] [blame^] | 6276 | EXPECT_EQ(event, entries[log_position].type); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6277 | EXPECT_EQ(NetLogEventPhase::END, entries[log_position].phase); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6278 | } |
| 6279 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 6280 | EXPECT_FALSE(LogContainsEntryWithTypeAfter(entries, log_position + 1, |
| 6281 | NetLogEventType::DELEGATE_INFO)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 6282 | } |
| 6283 | } |
| 6284 | |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6285 | namespace { |
| 6286 | |
| 6287 | const char kExtraHeader[] = "Allow-Snafu"; |
| 6288 | const char kExtraValue[] = "fubar"; |
| 6289 | |
| 6290 | class RedirectWithAdditionalHeadersDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 6291 | void OnReceivedRedirect(URLRequest* request, |
| 6292 | const RedirectInfo& redirect_info, |
| 6293 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6294 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6295 | request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false); |
| 6296 | } |
| 6297 | }; |
| 6298 | |
| 6299 | } // namespace |
| 6300 | |
| 6301 | TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6302 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6303 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6304 | GURL destination_url = |
| 6305 | http_test_server()->GetURL("/echoheader?" + std::string(kExtraHeader)); |
| 6306 | GURL original_url = |
| 6307 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6308 | RedirectWithAdditionalHeadersDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6309 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 6310 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6311 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6312 | d.RunUntilComplete(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6313 | |
| 6314 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6315 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6316 | EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value)); |
| 6317 | EXPECT_EQ(kExtraValue, value); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6318 | EXPECT_FALSE(req->is_pending()); |
| 6319 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 6320 | EXPECT_EQ(kExtraValue, d.data_received()); |
| 6321 | } |
| 6322 | |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6323 | namespace { |
| 6324 | |
| 6325 | const char kExtraHeaderToRemove[] = "To-Be-Removed"; |
| 6326 | |
| 6327 | class RedirectWithHeaderRemovalDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 6328 | void OnReceivedRedirect(URLRequest* request, |
| 6329 | const RedirectInfo& redirect_info, |
| 6330 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6331 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6332 | request->RemoveRequestHeaderByName(kExtraHeaderToRemove); |
| 6333 | } |
| 6334 | }; |
| 6335 | |
| 6336 | } // namespace |
| 6337 | |
| 6338 | TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6339 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6340 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6341 | GURL destination_url = http_test_server()->GetURL( |
| 6342 | "/echoheader?" + std::string(kExtraHeaderToRemove)); |
| 6343 | GURL original_url = |
| 6344 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6345 | RedirectWithHeaderRemovalDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6346 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 6347 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6348 | req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false); |
| 6349 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6350 | d.RunUntilComplete(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6351 | |
| 6352 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6353 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6354 | EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6355 | EXPECT_FALSE(req->is_pending()); |
| 6356 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 6357 | EXPECT_EQ("None", d.data_received()); |
| 6358 | } |
| 6359 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6360 | TEST_F(URLRequestTestHTTP, CancelAfterStart) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6361 | TestDelegate d; |
| 6362 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6363 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6364 | GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d, |
| 6365 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6366 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6367 | r->Start(); |
| 6368 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6369 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6370 | r->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6371 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6372 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6373 | |
| 6374 | // We expect to receive OnResponseStarted even though the request has been |
| 6375 | // cancelled. |
| 6376 | EXPECT_EQ(1, d.response_started_count()); |
| 6377 | EXPECT_EQ(0, d.bytes_received()); |
| 6378 | EXPECT_FALSE(d.received_data_before_response()); |
| 6379 | } |
| 6380 | } |
| 6381 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6382 | TEST_F(URLRequestTestHTTP, CancelInResponseStarted) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6383 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6384 | |
| 6385 | TestDelegate d; |
| 6386 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6387 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6388 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6389 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6390 | |
| 6391 | d.set_cancel_in_response_started(true); |
| 6392 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6393 | r->Start(); |
| 6394 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6395 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6396 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6397 | |
| 6398 | EXPECT_EQ(1, d.response_started_count()); |
| 6399 | EXPECT_EQ(0, d.bytes_received()); |
| 6400 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6401 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6402 | } |
| 6403 | } |
| 6404 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6405 | TEST_F(URLRequestTestHTTP, CancelOnDataReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6406 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6407 | |
| 6408 | TestDelegate d; |
| 6409 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6410 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6411 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 6412 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6413 | |
| 6414 | d.set_cancel_in_received_data(true); |
| 6415 | |
| 6416 | r->Start(); |
| 6417 | EXPECT_TRUE(r->is_pending()); |
| 6418 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6419 | d.RunUntilComplete(); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6420 | |
| 6421 | EXPECT_EQ(1, d.response_started_count()); |
| 6422 | EXPECT_NE(0, d.received_bytes_count()); |
| 6423 | EXPECT_FALSE(d.received_data_before_response()); |
| 6424 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
| 6425 | } |
| 6426 | } |
| 6427 | |
| 6428 | TEST_F(URLRequestTestHTTP, CancelDuringEofRead) { |
| 6429 | ASSERT_TRUE(http_test_server()->Start()); |
| 6430 | |
| 6431 | TestDelegate d; |
| 6432 | { |
| 6433 | // This returns an empty response (With headers). |
| 6434 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6435 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6436 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6437 | |
| 6438 | d.set_cancel_in_received_data(true); |
| 6439 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6440 | r->Start(); |
| 6441 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6442 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6443 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6444 | |
| 6445 | EXPECT_EQ(1, d.response_started_count()); |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6446 | EXPECT_EQ(0, d.received_bytes_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6447 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6448 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6449 | } |
| 6450 | } |
| 6451 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6452 | TEST_F(URLRequestTestHTTP, CancelByDestroyingAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6453 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6454 | |
| 6455 | TestDelegate d; |
| 6456 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6457 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6458 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 6459 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6460 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6461 | r->Start(); |
| 6462 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6463 | |
| 6464 | // The request will be implicitly canceled when it is destroyed. The |
| 6465 | // test delegate must not post a quit message when this happens because |
| 6466 | // this test doesn't actually have a message loop. The quit message would |
| 6467 | // get put on this thread's message queue and the next test would exit |
| 6468 | // early, causing problems. |
| 6469 | d.set_quit_on_complete(false); |
| 6470 | } |
| 6471 | // expect things to just cleanup properly. |
| 6472 | |
kimwjdalsl | 2bb4ff0 | 2015-12-16 22:06:02 | [diff] [blame] | 6473 | // 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] | 6474 | // message loop |
| 6475 | EXPECT_FALSE(d.received_data_before_response()); |
| 6476 | EXPECT_EQ(0, d.bytes_received()); |
| 6477 | } |
| 6478 | |
mmenke | 94f1bd9 | 2016-12-07 21:13:05 | [diff] [blame] | 6479 | TEST_F(URLRequestTestHTTP, CancelWhileReadingFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6480 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6481 | |
| 6482 | // populate cache |
| 6483 | { |
| 6484 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6485 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6486 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6487 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6488 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6489 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6490 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6491 | } |
| 6492 | |
| 6493 | // cancel read from cache (see bug 990242) |
| 6494 | { |
| 6495 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6496 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6497 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 6498 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6499 | r->Start(); |
| 6500 | r->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6501 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6502 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6503 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6504 | EXPECT_EQ(1, d.response_started_count()); |
| 6505 | EXPECT_EQ(0, d.bytes_received()); |
| 6506 | EXPECT_FALSE(d.received_data_before_response()); |
| 6507 | } |
| 6508 | } |
| 6509 | |
| 6510 | TEST_F(URLRequestTestHTTP, PostTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6511 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6512 | HTTPUploadDataOperationTest("POST"); |
| 6513 | } |
| 6514 | |
| 6515 | TEST_F(URLRequestTestHTTP, PutTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6516 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6517 | HTTPUploadDataOperationTest("PUT"); |
| 6518 | } |
| 6519 | |
| 6520 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6521 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6522 | |
| 6523 | TestDelegate d; |
| 6524 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6525 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6526 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6527 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6528 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6529 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6530 | r->Start(); |
| 6531 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6532 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6533 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6534 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6535 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6536 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6537 | |
| 6538 | EXPECT_FALSE(d.received_data_before_response()); |
| 6539 | EXPECT_TRUE(d.data_received().empty()); |
| 6540 | } |
| 6541 | } |
| 6542 | |
| 6543 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6544 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6545 | |
| 6546 | TestDelegate d; |
| 6547 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6548 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6549 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6550 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6551 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6552 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6553 | base::FilePath dir; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 6554 | base::PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 6555 | base::SetCurrentDirectory(dir); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6556 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6557 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6558 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6559 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 6560 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 6561 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6562 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6563 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6564 | base::ThreadTaskRunnerHandle::Get().get(), path, 0, |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6565 | std::numeric_limits<uint64_t>::max(), base::Time())); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6566 | r->set_upload(std::make_unique<ElementsUploadDataStream>( |
| 6567 | std::move(element_readers), 0)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6568 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6569 | r->Start(); |
| 6570 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6571 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6572 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6573 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame] | 6574 | int64_t size64 = 0; |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 6575 | ASSERT_EQ(true, base::GetFileSize(path, &size64)); |
| 6576 | ASSERT_LE(size64, std::numeric_limits<int>::max()); |
| 6577 | int size = static_cast<int>(size64); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6578 | std::unique_ptr<char[]> buf(new char[size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6579 | |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 6580 | ASSERT_EQ(size, base::ReadFile(path, buf.get(), size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6581 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6582 | ASSERT_EQ(1, d.response_started_count()) << "request failed. Error: " |
| 6583 | << d.request_status(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6584 | |
| 6585 | EXPECT_FALSE(d.received_data_before_response()); |
| 6586 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6587 | EXPECT_EQ(size, d.bytes_received()); |
| 6588 | EXPECT_EQ(std::string(&buf[0], size), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6589 | } |
| 6590 | } |
| 6591 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6592 | TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6593 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6594 | |
| 6595 | TestDelegate d; |
| 6596 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6597 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6598 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6599 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6600 | r->set_method("POST"); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6601 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6602 | std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6603 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 6604 | element_readers.push_back(std::make_unique<UploadFileElementReader>( |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 6605 | base::ThreadTaskRunnerHandle::Get().get(), |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6606 | base::FilePath(FILE_PATH_LITERAL( |
| 6607 | "c:\\path\\to\\non\\existant\\file.randomness.12345")), |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 6608 | 0, std::numeric_limits<uint64_t>::max(), base::Time())); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6609 | r->set_upload(std::make_unique<ElementsUploadDataStream>( |
| 6610 | std::move(element_readers), 0)); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6611 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6612 | r->Start(); |
| 6613 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6614 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6615 | d.RunUntilComplete(); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6616 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6617 | EXPECT_TRUE(d.request_failed()); |
| 6618 | EXPECT_FALSE(d.received_data_before_response()); |
| 6619 | EXPECT_EQ(0, d.bytes_received()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6620 | EXPECT_EQ(ERR_FILE_NOT_FOUND, d.request_status()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 6621 | } |
| 6622 | } |
| 6623 | |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6624 | namespace { |
| 6625 | |
| 6626 | // Adds a standard set of data to an upload for chunked upload integration |
| 6627 | // tests. |
| 6628 | void AddDataToUpload(ChunkedUploadDataStream::Writer* writer) { |
| 6629 | writer->AppendData("a", 1, false); |
| 6630 | writer->AppendData("bcd", 3, false); |
| 6631 | writer->AppendData("this is a longer chunk than before.", 35, false); |
| 6632 | writer->AppendData("\r\n\r\n", 4, false); |
| 6633 | writer->AppendData("0", 1, false); |
| 6634 | writer->AppendData("2323", 4, true); |
| 6635 | } |
| 6636 | |
| 6637 | // Checks that the upload data added in AddChunksToUpload() was echoed back from |
| 6638 | // the server. |
| 6639 | void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { |
| 6640 | // This should match the chunks sent by AddChunksToUpload(). |
| 6641 | const std::string expected_data = |
| 6642 | "abcdthis is a longer chunk than before.\r\n\r\n02323"; |
| 6643 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 6644 | ASSERT_EQ(1, d->response_started_count()) << "request failed. Error: " |
| 6645 | << d->request_status(); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6646 | |
| 6647 | EXPECT_FALSE(d->received_data_before_response()); |
| 6648 | |
| 6649 | EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); |
| 6650 | EXPECT_EQ(expected_data, d->data_received()); |
| 6651 | } |
| 6652 | |
| 6653 | } // namespace |
| 6654 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6655 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6656 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6657 | |
| 6658 | TestDelegate d; |
| 6659 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6660 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6661 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6662 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6663 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6664 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6665 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6666 | upload_data_stream->CreateWriter(); |
| 6667 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6668 | r->set_method("POST"); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6669 | AddDataToUpload(writer.get()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6670 | r->Start(); |
| 6671 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6672 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6673 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6674 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6675 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6676 | } |
| 6677 | } |
| 6678 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6679 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6680 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6681 | |
| 6682 | TestDelegate d; |
| 6683 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6684 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6685 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6686 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6687 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6688 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6689 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6690 | upload_data_stream->CreateWriter(); |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 6691 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6692 | r->set_method("POST"); |
| 6693 | r->Start(); |
| 6694 | EXPECT_TRUE(r->is_pending()); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6695 | AddDataToUpload(writer.get()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6696 | d.RunUntilComplete(); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6697 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6698 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 6699 | } |
| 6700 | } |
| 6701 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6702 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6703 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6704 | |
| 6705 | TestDelegate d; |
| 6706 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6707 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6708 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d, |
| 6709 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6710 | std::unique_ptr<ChunkedUploadDataStream> upload_data_stream( |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6711 | new ChunkedUploadDataStream(0)); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6712 | std::unique_ptr<ChunkedUploadDataStream::Writer> writer = |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6713 | upload_data_stream->CreateWriter(); |
| 6714 | r->set_upload(std::move(upload_data_stream)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6715 | r->set_method("POST"); |
| 6716 | r->Start(); |
| 6717 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6718 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6719 | base::RunLoop().RunUntilIdle(); |
mmenke | 56b0cbb91 | 2016-03-28 21:34:53 | [diff] [blame] | 6720 | AddDataToUpload(writer.get()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6721 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6722 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6723 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6724 | } |
| 6725 | } |
| 6726 | |
| 6727 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6728 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6729 | |
| 6730 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6731 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6732 | http_test_server()->GetURL("/with-headers.html"), DEFAULT_PRIORITY, &d, |
| 6733 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6734 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6735 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6736 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6737 | const HttpResponseHeaders* headers = req->response_headers(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6738 | |
| 6739 | // Simple sanity check that response_info() accesses the same data. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6740 | EXPECT_EQ(headers, req->response_info().headers.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6741 | |
| 6742 | std::string header; |
| 6743 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 6744 | EXPECT_EQ("private", header); |
| 6745 | |
| 6746 | header.clear(); |
| 6747 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 6748 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 6749 | |
| 6750 | // The response has two "X-Multiple-Entries" headers. |
| 6751 | // This verfies our output has them concatenated together. |
| 6752 | header.clear(); |
| 6753 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 6754 | EXPECT_EQ("a, b", header); |
| 6755 | } |
| 6756 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6757 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 6758 | // security state. (see http://crbug.com/550977). |
| 6759 | #if !defined(OS_IOS) |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6760 | TEST_F(URLRequestTestHTTP, ProcessSTS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6761 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6762 | https_test_server.SetSSLConfig( |
| 6763 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6764 | https_test_server.ServeFilesFromSourceDirectory( |
| 6765 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6766 | ASSERT_TRUE(https_test_server.Start()); |
| 6767 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6768 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6769 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6770 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6771 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6772 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6773 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6774 | d.RunUntilComplete(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6775 | |
| 6776 | TransportSecurityState* security_state = |
| 6777 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6778 | TransportSecurityState::STSState sts_state; |
| 6779 | TransportSecurityState::PKPState pkp_state; |
| 6780 | EXPECT_TRUE( |
| 6781 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6782 | EXPECT_FALSE( |
| 6783 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6784 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6785 | sts_state.upgrade_mode); |
| 6786 | EXPECT_TRUE(sts_state.include_subdomains); |
| 6787 | EXPECT_FALSE(pkp_state.include_subdomains); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6788 | #if defined(OS_ANDROID) |
| 6789 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6790 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6791 | EXPECT_FALSE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6792 | #endif |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6793 | } |
| 6794 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6795 | TEST_F(URLRequestTestHTTP, STSNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6796 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6797 | https_test_server.ServeFilesFromSourceDirectory( |
| 6798 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6799 | ASSERT_TRUE(https_test_server.Start()); |
| 6800 | // Make sure this test fails if the test server is changed to not |
| 6801 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6802 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 6803 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6804 | |
| 6805 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6806 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6807 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 6808 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6809 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6810 | d.RunUntilComplete(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6811 | TransportSecurityState* security_state = |
| 6812 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6813 | TransportSecurityState::STSState sts_state; |
| 6814 | EXPECT_FALSE( |
| 6815 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6816 | } |
| 6817 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6818 | namespace { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 6819 | const char kExpectCTStaticHostname[] = "expect-ct.preloaded.test"; |
| 6820 | const char kExpectStapleStaticHostname[] = "expect-staple.preloaded.test"; |
| 6821 | const char kExpectStapleReportURI[] = |
| 6822 | "http://report-uri.preloaded.test/expect-staple"; |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6823 | const char kHPKPReportUri[] = "https://hpkp-report.test"; |
| 6824 | } // namespace |
| 6825 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6826 | // Tests that enabling HPKP on a domain does not affect the HSTS |
| 6827 | // validity/expiration. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 6828 | TEST_F(URLRequestTestHTTP, ProcessPKP) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6829 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6830 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6831 | https_test_server.SetSSLConfig( |
| 6832 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6833 | https_test_server.ServeFilesFromSourceDirectory( |
| 6834 | base::FilePath(kTestFilePath)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6835 | ASSERT_TRUE(https_test_server.Start()); |
| 6836 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6837 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6838 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6839 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6840 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6841 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d, |
| 6842 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6843 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6844 | d.RunUntilComplete(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6845 | TransportSecurityState* security_state = |
| 6846 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6847 | TransportSecurityState::STSState sts_state; |
| 6848 | TransportSecurityState::PKPState pkp_state; |
| 6849 | EXPECT_FALSE( |
| 6850 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6851 | EXPECT_TRUE( |
| 6852 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6853 | EXPECT_EQ(TransportSecurityState::STSState::MODE_DEFAULT, |
| 6854 | sts_state.upgrade_mode); |
| 6855 | EXPECT_FALSE(sts_state.include_subdomains); |
| 6856 | EXPECT_FALSE(pkp_state.include_subdomains); |
| 6857 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6858 | EXPECT_EQ(report_uri, pkp_state.report_uri); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6859 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6860 | } |
| 6861 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6862 | // Tests that reports get sent on HPKP violations when a report-uri is set. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 6863 | TEST_F(URLRequestTestHTTP, ProcessPKPAndSendReport) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6864 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6865 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6866 | https_test_server.SetSSLConfig( |
| 6867 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6868 | https_test_server.ServeFilesFromSourceDirectory( |
| 6869 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6870 | ASSERT_TRUE(https_test_server.Start()); |
| 6871 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6872 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6873 | |
| 6874 | // Set up a pin for |test_server_hostname|. |
| 6875 | TransportSecurityState security_state; |
| 6876 | const base::Time current_time(base::Time::Now()); |
| 6877 | const base::Time expiry = current_time + base::TimeDelta::FromSeconds(1000); |
| 6878 | HashValueVector hashes; |
| 6879 | HashValue hash1; |
| 6880 | HashValue hash2; |
| 6881 | // The values here don't matter, as long as they are different from |
| 6882 | // the mocked CertVerifyResult below. |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6883 | ASSERT_TRUE( |
| 6884 | hash1.FromString("sha256/1111111111111111111111111111111111111111111=")); |
| 6885 | ASSERT_TRUE( |
| 6886 | hash2.FromString("sha256/2222222222222222222222222222222222222222222=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6887 | hashes.push_back(hash1); |
| 6888 | hashes.push_back(hash2); |
| 6889 | security_state.AddHPKP(test_server_hostname, expiry, |
| 6890 | false, /* include subdomains */ |
| 6891 | hashes, report_uri); |
| 6892 | |
| 6893 | MockCertificateReportSender mock_report_sender; |
| 6894 | security_state.SetReportSender(&mock_report_sender); |
| 6895 | |
| 6896 | // Set up a MockCertVerifier to trigger a violation of the previously |
| 6897 | // set pin. |
| 6898 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6899 | ASSERT_TRUE(cert); |
| 6900 | |
| 6901 | MockCertVerifier cert_verifier; |
| 6902 | CertVerifyResult verify_result; |
| 6903 | verify_result.verified_cert = cert; |
| 6904 | verify_result.is_issued_by_known_root = true; |
| 6905 | HashValue hash3; |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6906 | ASSERT_TRUE( |
| 6907 | hash3.FromString("sha256/3333333333333333333333333333333333333333333=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6908 | verify_result.public_key_hashes.push_back(hash3); |
| 6909 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6910 | |
| 6911 | TestNetworkDelegate network_delegate; |
| 6912 | TestURLRequestContext context(true); |
| 6913 | context.set_transport_security_state(&security_state); |
| 6914 | context.set_network_delegate(&network_delegate); |
| 6915 | context.set_cert_verifier(&cert_verifier); |
| 6916 | context.Init(); |
| 6917 | |
| 6918 | // Now send a request to trigger the violation. |
| 6919 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6920 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6921 | https_test_server.GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 6922 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6923 | violating_request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6924 | d.RunUntilComplete(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6925 | |
| 6926 | // Check that a report was sent. |
| 6927 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 6928 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 6929 | EXPECT_EQ("application/json; charset=utf-8", |
| 6930 | mock_report_sender.latest_content_type()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6931 | std::unique_ptr<base::Value> value( |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6932 | base::JSONReader::Read(mock_report_sender.latest_report())); |
| 6933 | ASSERT_TRUE(value); |
jdoerrie | e48b26a | 2017-12-09 14:19:08 | [diff] [blame] | 6934 | ASSERT_TRUE(value->is_dict()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6935 | base::DictionaryValue* report_dict; |
| 6936 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 6937 | std::string report_hostname; |
| 6938 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 6939 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 6940 | } |
| 6941 | |
| 6942 | // Tests that reports get sent on requests with |
| 6943 | // Public-Key-Pins-Report-Only headers. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 6944 | TEST_F(URLRequestTestHTTP, ProcessPKPReportOnly) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6945 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6946 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6947 | https_test_server.SetSSLConfig( |
| 6948 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6949 | https_test_server.ServeFilesFromSourceDirectory( |
| 6950 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6951 | ASSERT_TRUE(https_test_server.Start()); |
| 6952 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6953 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6954 | |
| 6955 | TransportSecurityState security_state; |
| 6956 | MockCertificateReportSender mock_report_sender; |
| 6957 | security_state.SetReportSender(&mock_report_sender); |
| 6958 | |
| 6959 | // Set up a MockCertVerifier to violate the pin in the Report-Only |
| 6960 | // header. |
| 6961 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 6962 | ASSERT_TRUE(cert); |
| 6963 | |
| 6964 | MockCertVerifier cert_verifier; |
| 6965 | CertVerifyResult verify_result; |
| 6966 | verify_result.verified_cert = cert; |
| 6967 | verify_result.is_issued_by_known_root = true; |
| 6968 | HashValue hash; |
| 6969 | // This value doesn't matter, as long as it is different from the pins |
| 6970 | // for the request to hpkp-headers-report-only.html. |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 6971 | ASSERT_TRUE( |
| 6972 | hash.FromString("sha256/1111111111111111111111111111111111111111111=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6973 | verify_result.public_key_hashes.push_back(hash); |
| 6974 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 6975 | |
| 6976 | TestNetworkDelegate network_delegate; |
| 6977 | TestURLRequestContext context(true); |
| 6978 | context.set_transport_security_state(&security_state); |
| 6979 | context.set_network_delegate(&network_delegate); |
| 6980 | context.set_cert_verifier(&cert_verifier); |
| 6981 | context.Init(); |
| 6982 | |
| 6983 | // Now send a request to trigger the violation. |
| 6984 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6985 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6986 | https_test_server.GetURL("/hpkp-headers-report-only.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 6987 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6988 | violating_request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 6989 | d.RunUntilComplete(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6990 | |
| 6991 | // Check that a report was sent. |
| 6992 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 6993 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
estark | b1716e2 | 2016-09-28 06:03:44 | [diff] [blame] | 6994 | EXPECT_EQ("application/json; charset=utf-8", |
| 6995 | mock_report_sender.latest_content_type()); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 6996 | std::unique_ptr<base::Value> value( |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6997 | base::JSONReader::Read(mock_report_sender.latest_report())); |
| 6998 | ASSERT_TRUE(value); |
jdoerrie | e48b26a | 2017-12-09 14:19:08 | [diff] [blame] | 6999 | ASSERT_TRUE(value->is_dict()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 7000 | base::DictionaryValue* report_dict; |
| 7001 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 7002 | std::string report_hostname; |
| 7003 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 7004 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 7005 | } |
| 7006 | |
| 7007 | // Tests that reports do not get sent on requests with |
| 7008 | // Public-Key-Pins-Report-Only headers that don't have pin violations. |
dadrian | 2faf206 | 2016-07-16 00:03:17 | [diff] [blame] | 7009 | TEST_F(URLRequestTestHTTP, ProcessPKPReportOnlyWithNoViolation) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 7010 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7011 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7012 | https_test_server.SetSSLConfig( |
| 7013 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7014 | https_test_server.ServeFilesFromSourceDirectory( |
| 7015 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 7016 | ASSERT_TRUE(https_test_server.Start()); |
| 7017 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7018 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 7019 | |
| 7020 | TransportSecurityState security_state; |
| 7021 | MockCertificateReportSender mock_report_sender; |
| 7022 | security_state.SetReportSender(&mock_report_sender); |
| 7023 | |
| 7024 | TestNetworkDelegate network_delegate; |
| 7025 | MockCertVerifier mock_cert_verifier; |
| 7026 | TestURLRequestContext context(true); |
| 7027 | context.set_transport_security_state(&security_state); |
| 7028 | context.set_network_delegate(&network_delegate); |
| 7029 | context.set_cert_verifier(&mock_cert_verifier); |
| 7030 | mock_cert_verifier.set_default_result(OK); |
| 7031 | context.Init(); |
| 7032 | |
| 7033 | // Now send a request that does not trigger the violation. |
| 7034 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7035 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7036 | https_test_server.GetURL("/hpkp-headers-report-only.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7037 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 7038 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7039 | d.RunUntilComplete(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 7040 | |
| 7041 | // Check that a report was not sent. |
| 7042 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 7043 | EXPECT_EQ(std::string(), mock_report_sender.latest_report()); |
| 7044 | } |
| 7045 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 7046 | TEST_F(URLRequestTestHTTP, PKPNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7047 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7048 | https_test_server.ServeFilesFromSourceDirectory( |
| 7049 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 7050 | ASSERT_TRUE(https_test_server.Start()); |
| 7051 | // Make sure this test fails if the test server is changed to not |
| 7052 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7053 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 7054 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 7055 | |
| 7056 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7057 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7058 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d, |
| 7059 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 7060 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7061 | d.RunUntilComplete(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 7062 | |
| 7063 | TransportSecurityState* security_state = |
| 7064 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 7065 | TransportSecurityState::PKPState pkp_state; |
| 7066 | EXPECT_FALSE( |
| 7067 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 7068 | } |
| 7069 | |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7070 | TEST_F(URLRequestTestHTTP, PKPBypassRecorded) { |
| 7071 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7072 | https_test_server.SetSSLConfig( |
| 7073 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7074 | https_test_server.ServeFilesFromSourceDirectory( |
| 7075 | base::FilePath(kTestFilePath)); |
| 7076 | ASSERT_TRUE(https_test_server.Start()); |
| 7077 | |
| 7078 | // Set up a MockCertVerifier to be a local root that violates the pin |
| 7079 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7080 | ASSERT_TRUE(cert); |
| 7081 | |
| 7082 | MockCertVerifier cert_verifier; |
| 7083 | CertVerifyResult verify_result; |
| 7084 | verify_result.verified_cert = cert; |
| 7085 | verify_result.is_issued_by_known_root = false; |
| 7086 | HashValue hash; |
| 7087 | ASSERT_TRUE( |
| 7088 | hash.FromString("sha256/1111111111111111111111111111111111111111111=")); |
| 7089 | verify_result.public_key_hashes.push_back(hash); |
| 7090 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7091 | cert_verifier.set_default_result(OK); |
| 7092 | |
| 7093 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
| 7094 | |
| 7095 | // Set up HPKP |
| 7096 | base::Time current_time = base::Time::Now(); |
| 7097 | const base::Time expiry = current_time + base::TimeDelta::FromSeconds(10000); |
| 7098 | HashValue pin; |
| 7099 | ASSERT_TRUE( |
| 7100 | pin.FromString("sha256/2222222222222222222222222222222222222222222=")); |
| 7101 | HashValueVector hashes; |
| 7102 | hashes.push_back(pin); |
| 7103 | GURL report_uri(kHPKPReportUri); |
| 7104 | TransportSecurityState security_state; |
| 7105 | security_state.AddHPKP(test_server_hostname, expiry, |
| 7106 | false, /* include subdomains */ |
| 7107 | hashes, report_uri); |
| 7108 | |
| 7109 | TestNetworkDelegate network_delegate; |
| 7110 | TestURLRequestContext context(true); |
| 7111 | context.set_transport_security_state(&security_state); |
| 7112 | context.set_network_delegate(&network_delegate); |
| 7113 | context.set_cert_verifier(&cert_verifier); |
| 7114 | context.Init(); |
| 7115 | |
| 7116 | TestDelegate d; |
| 7117 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7118 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d, |
| 7119 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7120 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7121 | d.RunUntilComplete(); |
dadrian | df302c4 | 2016-06-10 18:48:59 | [diff] [blame] | 7122 | |
| 7123 | TransportSecurityState::PKPState pkp_state; |
| 7124 | EXPECT_TRUE( |
| 7125 | security_state.GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 7126 | EXPECT_TRUE(request->ssl_info().pkp_bypassed); |
| 7127 | } |
| 7128 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7129 | TEST_F(URLRequestTestHTTP, ProcessSTSOnce) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7130 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7131 | https_test_server.SetSSLConfig( |
| 7132 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7133 | https_test_server.ServeFilesFromSourceDirectory( |
| 7134 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7135 | ASSERT_TRUE(https_test_server.Start()); |
| 7136 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7137 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 7138 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7139 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7140 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7141 | https_test_server.GetURL("/hsts-multiple-headers.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7142 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7143 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7144 | d.RunUntilComplete(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7145 | |
| 7146 | // We should have set parameters from the first header, not the second. |
| 7147 | TransportSecurityState* security_state = |
| 7148 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 7149 | TransportSecurityState::STSState sts_state; |
| 7150 | EXPECT_TRUE( |
| 7151 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 7152 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 7153 | sts_state.upgrade_mode); |
| 7154 | EXPECT_FALSE(sts_state.include_subdomains); |
| 7155 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 7156 | } |
| 7157 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 7158 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7159 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7160 | https_test_server.SetSSLConfig( |
| 7161 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7162 | https_test_server.ServeFilesFromSourceDirectory( |
| 7163 | base::FilePath(kTestFilePath)); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 7164 | ASSERT_TRUE(https_test_server.Start()); |
| 7165 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7166 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 7167 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 7168 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7169 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7170 | https_test_server.GetURL("/hsts-and-hpkp-headers.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7171 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7172 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7173 | d.RunUntilComplete(); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 7174 | |
| 7175 | // We should have set parameters from the first header, not the second. |
| 7176 | TransportSecurityState* security_state = |
| 7177 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 7178 | TransportSecurityState::STSState sts_state; |
| 7179 | TransportSecurityState::PKPState pkp_state; |
| 7180 | EXPECT_TRUE( |
| 7181 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 7182 | EXPECT_TRUE( |
| 7183 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 7184 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 7185 | sts_state.upgrade_mode); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 7186 | #if defined(OS_ANDROID) |
| 7187 | // Android's CertVerifyProc does not (yet) handle pins. |
| 7188 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 7189 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 7190 | #endif |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 7191 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 7192 | |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 7193 | // Even though there is an HSTS header asserting includeSubdomains, it is |
| 7194 | // the *second* such header, and we MUST process only the first. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 7195 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 7196 | // includeSubdomains does not occur in the test HPKP header. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 7197 | EXPECT_FALSE(pkp_state.include_subdomains); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 7198 | } |
| 7199 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 7200 | // Tests that when multiple HPKP headers are present, asserting different |
| 7201 | // policies, that only the first such policy is processed. |
| 7202 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7203 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7204 | https_test_server.SetSSLConfig( |
| 7205 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7206 | https_test_server.ServeFilesFromSourceDirectory( |
| 7207 | base::FilePath(kTestFilePath)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 7208 | ASSERT_TRUE(https_test_server.Start()); |
| 7209 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7210 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 7211 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 7212 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7213 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7214 | https_test_server.GetURL("/hsts-and-hpkp-headers2.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7215 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7216 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7217 | d.RunUntilComplete(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 7218 | |
| 7219 | TransportSecurityState* security_state = |
| 7220 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 7221 | TransportSecurityState::STSState sts_state; |
| 7222 | TransportSecurityState::PKPState pkp_state; |
| 7223 | EXPECT_TRUE( |
| 7224 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 7225 | EXPECT_TRUE( |
| 7226 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 7227 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 7228 | sts_state.upgrade_mode); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 7229 | #if defined(OS_ANDROID) |
| 7230 | // Android's CertVerifyProc does not (yet) handle pins. |
| 7231 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 7232 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 7233 | #endif |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 7234 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 7235 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 7236 | EXPECT_TRUE(sts_state.include_subdomains); |
| 7237 | EXPECT_FALSE(pkp_state.include_subdomains); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 7238 | } |
| 7239 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7240 | // An ExpectCTReporter that records the number of times OnExpectCTFailed() was |
| 7241 | // called. |
| 7242 | class MockExpectCTReporter : public TransportSecurityState::ExpectCTReporter { |
| 7243 | public: |
| 7244 | MockExpectCTReporter() : num_failures_(0) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 7245 | ~MockExpectCTReporter() override = default; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7246 | |
| 7247 | void OnExpectCTFailed(const HostPortPair& host_port_pair, |
| 7248 | const GURL& report_uri, |
estark | ae028b46 | 2017-06-20 23:25:01 | [diff] [blame] | 7249 | base::Time expiration, |
estark | bf1b5296 | 2017-05-05 17:05:25 | [diff] [blame] | 7250 | const X509Certificate* validated_certificate_chain, |
| 7251 | const X509Certificate* served_certificate_chain, |
| 7252 | const SignedCertificateTimestampAndStatusList& |
| 7253 | signed_certificate_timestamps) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7254 | num_failures_++; |
| 7255 | } |
| 7256 | |
| 7257 | uint32_t num_failures() { return num_failures_; } |
| 7258 | |
| 7259 | private: |
| 7260 | uint32_t num_failures_; |
| 7261 | }; |
| 7262 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7263 | // A CTPolicyEnforcer that returns a default CTPolicyCompliance value |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7264 | // for every certificate. |
| 7265 | class MockCTPolicyEnforcer : public CTPolicyEnforcer { |
| 7266 | public: |
| 7267 | MockCTPolicyEnforcer() |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7268 | : default_result_(ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS) {} |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 7269 | ~MockCTPolicyEnforcer() override = default; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7270 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7271 | ct::CTPolicyCompliance CheckCompliance( |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7272 | X509Certificate* cert, |
Ryan Sleevi | 8a9c9c1 | 2018-05-09 02:36:23 | [diff] [blame] | 7273 | const ct::SCTList& verified_scts, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 7274 | const NetLogWithSource& net_log) override { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7275 | return default_result_; |
| 7276 | } |
| 7277 | |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7278 | void set_default_result(ct::CTPolicyCompliance default_result) { |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7279 | default_result_ = default_result; |
| 7280 | } |
| 7281 | |
| 7282 | private: |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7283 | ct::CTPolicyCompliance default_result_; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7284 | }; |
| 7285 | |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7286 | // Tests that Expect CT headers for the preload list are processed correctly. |
| 7287 | TEST_F(URLRequestTestHTTP, PreloadExpectCTHeader) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 7288 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 7289 | |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7290 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7291 | https_test_server.SetSSLConfig( |
| 7292 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7293 | https_test_server.ServeFilesFromSourceDirectory( |
| 7294 | base::FilePath(kTestFilePath)); |
| 7295 | ASSERT_TRUE(https_test_server.Start()); |
| 7296 | |
| 7297 | MockExpectCTReporter reporter; |
| 7298 | TransportSecurityState transport_security_state; |
| 7299 | transport_security_state.enable_static_expect_ct_ = true; |
| 7300 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7301 | |
| 7302 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7303 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7304 | ASSERT_TRUE(cert); |
| 7305 | MockCertVerifier cert_verifier; |
| 7306 | CertVerifyResult verify_result; |
| 7307 | verify_result.verified_cert = cert; |
| 7308 | verify_result.is_issued_by_known_root = true; |
| 7309 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7310 | |
rsleevi | 22cae167 | 2016-12-28 01:53:36 | [diff] [blame] | 7311 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to trigger an Expect |
| 7312 | // CT violation. |
| 7313 | DoNothingCTVerifier ct_verifier; |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7314 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7315 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7316 | ct::CTPolicyCompliance::CT_POLICY_NOT_ENOUGH_SCTS); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7317 | |
| 7318 | TestNetworkDelegate network_delegate; |
| 7319 | // Use a MockHostResolver (which by default maps all hosts to |
| 7320 | // 127.0.0.1) so that the request can be sent to a site on the Expect |
| 7321 | // CT preload list. |
| 7322 | MockHostResolver host_resolver; |
| 7323 | TestURLRequestContext context(true); |
| 7324 | context.set_host_resolver(&host_resolver); |
| 7325 | context.set_transport_security_state(&transport_security_state); |
| 7326 | context.set_network_delegate(&network_delegate); |
| 7327 | context.set_cert_verifier(&cert_verifier); |
| 7328 | context.set_cert_transparency_verifier(&ct_verifier); |
vabr | 13d0074 | 2017-06-05 10:31:46 | [diff] [blame] | 7329 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7330 | context.Init(); |
| 7331 | |
| 7332 | // Now send a request to trigger the violation. |
| 7333 | TestDelegate d; |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7334 | GURL url = https_test_server.GetURL("/expect-ct-header-preload.html"); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7335 | GURL::Replacements replace_host; |
| 7336 | replace_host.SetHostStr(kExpectCTStaticHostname); |
| 7337 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7338 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
| 7339 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7340 | violating_request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7341 | d.RunUntilComplete(); |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7342 | |
| 7343 | EXPECT_EQ(1u, reporter.num_failures()); |
| 7344 | } |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7345 | |
| 7346 | // Tests that Expect CT HTTP headers are processed correctly. |
| 7347 | TEST_F(URLRequestTestHTTP, ExpectCTHeader) { |
| 7348 | base::test::ScopedFeatureList feature_list; |
| 7349 | feature_list.InitAndEnableFeature( |
| 7350 | TransportSecurityState::kDynamicExpectCTFeature); |
| 7351 | |
| 7352 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7353 | https_test_server.SetSSLConfig( |
| 7354 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7355 | https_test_server.ServeFilesFromSourceDirectory( |
| 7356 | base::FilePath(kTestFilePath)); |
| 7357 | ASSERT_TRUE(https_test_server.Start()); |
| 7358 | |
| 7359 | MockExpectCTReporter reporter; |
| 7360 | TransportSecurityState transport_security_state; |
| 7361 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7362 | |
| 7363 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7364 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7365 | ASSERT_TRUE(cert); |
| 7366 | MockCertVerifier cert_verifier; |
| 7367 | CertVerifyResult verify_result; |
| 7368 | verify_result.verified_cert = cert; |
| 7369 | verify_result.is_issued_by_known_root = true; |
| 7370 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7371 | |
| 7372 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 7373 | // compliance. |
| 7374 | DoNothingCTVerifier ct_verifier; |
| 7375 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7376 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7377 | ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7378 | |
| 7379 | TestNetworkDelegate network_delegate; |
| 7380 | // Use a MockHostResolver (which by default maps all hosts to |
| 7381 | // 127.0.0.1). |
| 7382 | MockHostResolver host_resolver; |
| 7383 | TestURLRequestContext context(true); |
| 7384 | context.set_host_resolver(&host_resolver); |
| 7385 | context.set_transport_security_state(&transport_security_state); |
| 7386 | context.set_network_delegate(&network_delegate); |
| 7387 | context.set_cert_verifier(&cert_verifier); |
| 7388 | context.set_cert_transparency_verifier(&ct_verifier); |
| 7389 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 7390 | context.Init(); |
| 7391 | |
| 7392 | // Now send a request to trigger the header processing. |
| 7393 | TestDelegate d; |
| 7394 | GURL url = https_test_server.GetURL("/expect-ct-header.html"); |
| 7395 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7396 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7397 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7398 | d.RunUntilComplete(); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7399 | |
| 7400 | TransportSecurityState::ExpectCTState state; |
| 7401 | ASSERT_TRUE( |
| 7402 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 7403 | EXPECT_TRUE(state.enforce); |
| 7404 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 7405 | } |
| 7406 | |
| 7407 | // Tests that if multiple Expect CT HTTP headers are sent, they are all |
| 7408 | // processed. |
| 7409 | TEST_F(URLRequestTestHTTP, MultipleExpectCTHeaders) { |
| 7410 | base::test::ScopedFeatureList feature_list; |
| 7411 | feature_list.InitAndEnableFeature( |
| 7412 | TransportSecurityState::kDynamicExpectCTFeature); |
| 7413 | |
| 7414 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7415 | https_test_server.SetSSLConfig( |
| 7416 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 7417 | https_test_server.ServeFilesFromSourceDirectory( |
| 7418 | base::FilePath(kTestFilePath)); |
| 7419 | ASSERT_TRUE(https_test_server.Start()); |
| 7420 | |
| 7421 | MockExpectCTReporter reporter; |
| 7422 | TransportSecurityState transport_security_state; |
| 7423 | transport_security_state.SetExpectCTReporter(&reporter); |
| 7424 | |
| 7425 | // Set up a MockCertVerifier to accept the certificate that the server sends. |
| 7426 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 7427 | ASSERT_TRUE(cert); |
| 7428 | MockCertVerifier cert_verifier; |
| 7429 | CertVerifyResult verify_result; |
| 7430 | verify_result.verified_cert = cert; |
| 7431 | verify_result.is_issued_by_known_root = true; |
| 7432 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 7433 | |
| 7434 | // Set up a DoNothingCTVerifier and MockCTPolicyEnforcer to simulate CT |
| 7435 | // compliance. |
| 7436 | DoNothingCTVerifier ct_verifier; |
| 7437 | MockCTPolicyEnforcer ct_policy_enforcer; |
| 7438 | ct_policy_enforcer.set_default_result( |
Emily Stark | 627238f | 2017-11-29 03:29:54 | [diff] [blame] | 7439 | ct::CTPolicyCompliance::CT_POLICY_COMPLIES_VIA_SCTS); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7440 | |
| 7441 | TestNetworkDelegate network_delegate; |
| 7442 | // Use a MockHostResolver (which by default maps all hosts to |
| 7443 | // 127.0.0.1). |
| 7444 | MockHostResolver host_resolver; |
| 7445 | TestURLRequestContext context(true); |
| 7446 | context.set_host_resolver(&host_resolver); |
| 7447 | context.set_transport_security_state(&transport_security_state); |
| 7448 | context.set_network_delegate(&network_delegate); |
| 7449 | context.set_cert_verifier(&cert_verifier); |
| 7450 | context.set_cert_transparency_verifier(&ct_verifier); |
| 7451 | context.set_ct_policy_enforcer(&ct_policy_enforcer); |
| 7452 | context.Init(); |
| 7453 | |
| 7454 | // Now send a request to trigger the header processing. |
| 7455 | TestDelegate d; |
| 7456 | GURL url = https_test_server.GetURL("/expect-ct-header-multiple.html"); |
| 7457 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7458 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7459 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7460 | d.RunUntilComplete(); |
Emily Stark | 4cfecf07 | 2017-08-08 01:05:51 | [diff] [blame] | 7461 | |
| 7462 | TransportSecurityState::ExpectCTState state; |
| 7463 | ASSERT_TRUE( |
| 7464 | transport_security_state.GetDynamicExpectCTState(url.host(), &state)); |
| 7465 | EXPECT_TRUE(state.enforce); |
| 7466 | EXPECT_EQ(GURL("https://example.test"), state.report_uri); |
| 7467 | } |
| 7468 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 7469 | #endif // !defined(OS_IOS) |
estark | 1614475f | 2016-03-10 03:46:47 | [diff] [blame] | 7470 | |
mmenke | fd9d15c | 2017-06-29 13:45:54 | [diff] [blame] | 7471 | #if BUILDFLAG(ENABLE_REPORTING) |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7472 | namespace { |
| 7473 | |
| 7474 | class TestReportingService : public ReportingService { |
| 7475 | public: |
| 7476 | struct Header { |
| 7477 | GURL url; |
| 7478 | std::string header_value; |
| 7479 | }; |
| 7480 | |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7481 | const std::vector<Header>& headers() { return headers_; } |
| 7482 | |
juliatuttle | 1d92f015 | 2017-04-28 17:19:21 | [diff] [blame] | 7483 | // ReportingService implementation: |
| 7484 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 7485 | ~TestReportingService() override = default; |
juliatuttle | 1d92f015 | 2017-04-28 17:19:21 | [diff] [blame] | 7486 | |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7487 | void QueueReport(const GURL& url, |
| 7488 | const std::string& group, |
| 7489 | const std::string& type, |
Julia Tuttle | 107e3067 | 2018-03-29 18:48:42 | [diff] [blame] | 7490 | std::unique_ptr<const base::Value> body, |
| 7491 | int depth) override { |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7492 | NOTIMPLEMENTED(); |
| 7493 | } |
| 7494 | |
| 7495 | void ProcessHeader(const GURL& url, |
| 7496 | const std::string& header_value) override { |
| 7497 | headers_.push_back({url, header_value}); |
| 7498 | } |
| 7499 | |
Julia Tuttle | 227a6ff | 2017-12-19 19:44:24 | [diff] [blame] | 7500 | void RemoveBrowsingData(int data_type_mask, |
| 7501 | const base::RepeatingCallback<bool(const GURL&)>& |
| 7502 | origin_filter) override { |
juliatuttle | aeb1abc | 2017-05-04 21:14:38 | [diff] [blame] | 7503 | NOTIMPLEMENTED(); |
| 7504 | } |
| 7505 | |
Eric Orth | b812a44 | 2018-05-04 20:26:48 | [diff] [blame] | 7506 | void RemoveAllBrowsingData(int data_type_mask) override { NOTIMPLEMENTED(); } |
| 7507 | |
Julia Tuttle | 107e3067 | 2018-03-29 18:48:42 | [diff] [blame] | 7508 | int GetUploadDepth(const URLRequest& request) override { |
Julia Tuttle | 4667c1c | 2017-12-19 18:27:38 | [diff] [blame] | 7509 | NOTIMPLEMENTED(); |
Julia Tuttle | 107e3067 | 2018-03-29 18:48:42 | [diff] [blame] | 7510 | return 0; |
Julia Tuttle | 4667c1c | 2017-12-19 18:27:38 | [diff] [blame] | 7511 | } |
| 7512 | |
Julia Tuttle | 91a655d | 2018-01-26 18:03:03 | [diff] [blame] | 7513 | const ReportingPolicy& GetPolicy() const override { |
| 7514 | static ReportingPolicy dummy_policy_; |
| 7515 | NOTIMPLEMENTED(); |
| 7516 | return dummy_policy_; |
| 7517 | } |
| 7518 | |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7519 | private: |
| 7520 | std::vector<Header> headers_; |
| 7521 | }; |
| 7522 | |
| 7523 | std::unique_ptr<test_server::HttpResponse> SendReportToHeader( |
| 7524 | const test_server::HttpRequest& request) { |
| 7525 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
| 7526 | new test_server::BasicHttpResponse); |
| 7527 | http_response->set_code(HTTP_OK); |
| 7528 | http_response->AddCustomHeader("Report-To", "foo"); |
| 7529 | http_response->AddCustomHeader("Report-To", "bar"); |
| 7530 | return std::move(http_response); |
| 7531 | } |
| 7532 | |
| 7533 | } // namespace |
| 7534 | |
| 7535 | TEST_F(URLRequestTestHTTP, DontProcessReportToHeaderNoService) { |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7536 | http_test_server()->RegisterRequestHandler( |
| 7537 | base::BindRepeating(&SendReportToHeader)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7538 | ASSERT_TRUE(http_test_server()->Start()); |
| 7539 | GURL request_url = http_test_server()->GetURL("/"); |
| 7540 | |
| 7541 | TestNetworkDelegate network_delegate; |
| 7542 | TestURLRequestContext context(true); |
| 7543 | context.set_network_delegate(&network_delegate); |
| 7544 | context.Init(); |
| 7545 | |
| 7546 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7547 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7548 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7549 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7550 | d.RunUntilComplete(); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7551 | } |
| 7552 | |
| 7553 | TEST_F(URLRequestTestHTTP, DontProcessReportToHeaderHTTP) { |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7554 | http_test_server()->RegisterRequestHandler( |
| 7555 | base::BindRepeating(&SendReportToHeader)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7556 | ASSERT_TRUE(http_test_server()->Start()); |
| 7557 | GURL request_url = http_test_server()->GetURL("/"); |
| 7558 | |
| 7559 | TestNetworkDelegate network_delegate; |
| 7560 | TestReportingService reporting_service; |
| 7561 | TestURLRequestContext context(true); |
| 7562 | context.set_network_delegate(&network_delegate); |
| 7563 | context.set_reporting_service(&reporting_service); |
| 7564 | context.Init(); |
| 7565 | |
| 7566 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7567 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7568 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7569 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7570 | d.RunUntilComplete(); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7571 | |
| 7572 | EXPECT_TRUE(reporting_service.headers().empty()); |
| 7573 | } |
| 7574 | |
| 7575 | TEST_F(URLRequestTestHTTP, ProcessReportToHeaderHTTPS) { |
| 7576 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7577 | https_test_server.RegisterRequestHandler( |
| 7578 | base::BindRepeating(&SendReportToHeader)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7579 | ASSERT_TRUE(https_test_server.Start()); |
| 7580 | GURL request_url = https_test_server.GetURL("/"); |
| 7581 | |
| 7582 | TestNetworkDelegate network_delegate; |
| 7583 | TestReportingService reporting_service; |
| 7584 | TestURLRequestContext context(true); |
| 7585 | context.set_network_delegate(&network_delegate); |
| 7586 | context.set_reporting_service(&reporting_service); |
| 7587 | context.Init(); |
| 7588 | |
| 7589 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7590 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7591 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7592 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7593 | d.RunUntilComplete(); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7594 | |
| 7595 | ASSERT_EQ(1u, reporting_service.headers().size()); |
| 7596 | EXPECT_EQ(request_url, reporting_service.headers()[0].url); |
| 7597 | EXPECT_EQ("foo, bar", reporting_service.headers()[0].header_value); |
| 7598 | } |
| 7599 | |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7600 | TEST_F(URLRequestTestHTTP, DontProcessReportToHeaderInvalidHttps) { |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7601 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7602 | https_test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7603 | https_test_server.RegisterRequestHandler( |
| 7604 | base::BindRepeating(&SendReportToHeader)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7605 | ASSERT_TRUE(https_test_server.Start()); |
| 7606 | GURL request_url = https_test_server.GetURL("/"); |
| 7607 | |
| 7608 | TestNetworkDelegate network_delegate; |
| 7609 | TestReportingService reporting_service; |
| 7610 | TestURLRequestContext context(true); |
| 7611 | context.set_network_delegate(&network_delegate); |
| 7612 | context.set_reporting_service(&reporting_service); |
| 7613 | context.Init(); |
| 7614 | |
| 7615 | TestDelegate d; |
| 7616 | d.set_allow_certificate_errors(true); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7617 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7618 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7619 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7620 | d.RunUntilComplete(); |
juliatuttle | 2b8dd2d1 | 2017-04-12 21:27:41 | [diff] [blame] | 7621 | |
| 7622 | EXPECT_TRUE(d.have_certificate_errors()); |
| 7623 | EXPECT_TRUE(IsCertStatusError(request->ssl_info().cert_status)); |
| 7624 | EXPECT_TRUE(reporting_service.headers().empty()); |
| 7625 | } |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7626 | |
| 7627 | // Network Error Logging is dependent on the Reporting API, so only run NEL |
| 7628 | // tests if Reporting is enabled in the build. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7629 | |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7630 | namespace { |
| 7631 | |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7632 | class TestNetworkErrorLoggingService : public NetworkErrorLoggingService { |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7633 | public: |
| 7634 | struct Header { |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 7635 | Header() = default; |
| 7636 | ~Header() = default; |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7637 | |
| 7638 | url::Origin origin; |
| 7639 | std::string value; |
| 7640 | }; |
| 7641 | |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7642 | const std::vector<Header>& headers() { return headers_; } |
Douglas Creager | 1f56bbe | 2018-02-17 03:58:40 | [diff] [blame] | 7643 | const std::vector<RequestDetails>& errors() { return errors_; } |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7644 | |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7645 | // NetworkErrorLoggingService implementation: |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7646 | |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7647 | ~TestNetworkErrorLoggingService() override = default; |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7648 | |
| 7649 | void OnHeader(const url::Origin& origin, const std::string& value) override { |
| 7650 | Header header; |
| 7651 | header.origin = origin; |
| 7652 | header.value = value; |
| 7653 | headers_.push_back(header); |
| 7654 | } |
| 7655 | |
Douglas Creager | 1f56bbe | 2018-02-17 03:58:40 | [diff] [blame] | 7656 | void OnRequest(const RequestDetails& details) override { |
Julia Tuttle | b0d4547 | 2017-09-25 20:14:43 | [diff] [blame] | 7657 | errors_.push_back(details); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7658 | } |
| 7659 | |
Julia Tuttle | a059db8 | 2018-01-04 18:46:43 | [diff] [blame] | 7660 | void RemoveBrowsingData(const base::RepeatingCallback<bool(const GURL&)>& |
| 7661 | origin_filter) override { |
| 7662 | NOTREACHED(); |
| 7663 | } |
| 7664 | |
Eric Orth | 26a408f | 2018-05-09 15:43:40 | [diff] [blame] | 7665 | void RemoveAllBrowsingData() override { NOTREACHED(); } |
| 7666 | |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7667 | private: |
| 7668 | std::vector<Header> headers_; |
Douglas Creager | 1f56bbe | 2018-02-17 03:58:40 | [diff] [blame] | 7669 | std::vector<RequestDetails> errors_; |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7670 | }; |
| 7671 | |
| 7672 | std::unique_ptr<test_server::HttpResponse> SendNelHeader( |
| 7673 | const test_server::HttpRequest& request) { |
| 7674 | std::unique_ptr<test_server::BasicHttpResponse> http_response( |
| 7675 | new test_server::BasicHttpResponse); |
| 7676 | http_response->set_code(HTTP_OK); |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7677 | http_response->AddCustomHeader(NetworkErrorLoggingService::kHeaderName, |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7678 | "foo"); |
| 7679 | return std::move(http_response); |
| 7680 | } |
| 7681 | |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7682 | std::unique_ptr<test_server::HttpResponse> SendEmptyResponse( |
| 7683 | const test_server::HttpRequest& request) { |
Gyuyoung Kim | b480aba | 2018-01-27 07:00:04 | [diff] [blame] | 7684 | return std::make_unique<test_server::RawHttpResponse>("", ""); |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7685 | } |
| 7686 | |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7687 | } // namespace |
| 7688 | |
| 7689 | TEST_F(URLRequestTestHTTP, DontProcessNelHeaderNoDelegate) { |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7690 | http_test_server()->RegisterRequestHandler( |
| 7691 | base::BindRepeating(&SendNelHeader)); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7692 | ASSERT_TRUE(http_test_server()->Start()); |
| 7693 | GURL request_url = http_test_server()->GetURL("/"); |
| 7694 | |
| 7695 | TestNetworkDelegate network_delegate; |
| 7696 | TestURLRequestContext context(true); |
| 7697 | context.set_network_delegate(&network_delegate); |
| 7698 | context.Init(); |
| 7699 | |
| 7700 | TestDelegate d; |
| 7701 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7702 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7703 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7704 | d.RunUntilComplete(); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7705 | } |
| 7706 | |
| 7707 | TEST_F(URLRequestTestHTTP, DontProcessNelHeaderHttp) { |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7708 | http_test_server()->RegisterRequestHandler( |
| 7709 | base::BindRepeating(&SendNelHeader)); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7710 | ASSERT_TRUE(http_test_server()->Start()); |
| 7711 | GURL request_url = http_test_server()->GetURL("/"); |
| 7712 | |
| 7713 | TestNetworkDelegate network_delegate; |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7714 | TestNetworkErrorLoggingService nel_service; |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7715 | TestURLRequestContext context(true); |
| 7716 | context.set_network_delegate(&network_delegate); |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7717 | context.set_network_error_logging_service(&nel_service); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7718 | context.Init(); |
| 7719 | |
| 7720 | TestDelegate d; |
| 7721 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7722 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7723 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7724 | d.RunUntilComplete(); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7725 | |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7726 | EXPECT_TRUE(nel_service.headers().empty()); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7727 | } |
| 7728 | |
| 7729 | TEST_F(URLRequestTestHTTP, ProcessNelHeaderHttps) { |
| 7730 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7731 | https_test_server.RegisterRequestHandler(base::BindRepeating(&SendNelHeader)); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7732 | ASSERT_TRUE(https_test_server.Start()); |
| 7733 | GURL request_url = https_test_server.GetURL("/"); |
| 7734 | |
| 7735 | TestNetworkDelegate network_delegate; |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7736 | TestNetworkErrorLoggingService nel_service; |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7737 | TestURLRequestContext context(true); |
| 7738 | context.set_network_delegate(&network_delegate); |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7739 | context.set_network_error_logging_service(&nel_service); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7740 | context.Init(); |
| 7741 | |
| 7742 | TestDelegate d; |
| 7743 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7744 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7745 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7746 | d.RunUntilComplete(); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7747 | |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7748 | ASSERT_EQ(1u, nel_service.headers().size()); |
| 7749 | EXPECT_EQ(url::Origin::Create(request_url), nel_service.headers()[0].origin); |
| 7750 | EXPECT_EQ("foo", nel_service.headers()[0].value); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7751 | } |
| 7752 | |
| 7753 | TEST_F(URLRequestTestHTTP, DontProcessNelHeaderInvalidHttps) { |
| 7754 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7755 | https_test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7756 | https_test_server.RegisterRequestHandler(base::BindRepeating(&SendNelHeader)); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7757 | ASSERT_TRUE(https_test_server.Start()); |
| 7758 | GURL request_url = https_test_server.GetURL("/"); |
| 7759 | |
| 7760 | TestNetworkDelegate network_delegate; |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7761 | TestNetworkErrorLoggingService nel_service; |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7762 | TestURLRequestContext context(true); |
| 7763 | context.set_network_delegate(&network_delegate); |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7764 | context.set_network_error_logging_service(&nel_service); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7765 | context.Init(); |
| 7766 | |
| 7767 | TestDelegate d; |
| 7768 | d.set_allow_certificate_errors(true); |
| 7769 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7770 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7771 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7772 | d.RunUntilComplete(); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7773 | |
| 7774 | EXPECT_TRUE(d.have_certificate_errors()); |
| 7775 | EXPECT_TRUE(IsCertStatusError(request->ssl_info().cert_status)); |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7776 | EXPECT_TRUE(nel_service.headers().empty()); |
Julia Tuttle | f9b7406 | 2017-07-27 14:44:05 | [diff] [blame] | 7777 | } |
| 7778 | |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7779 | TEST_F(URLRequestTestHTTP, DontForwardErrorToNelNoDelegate) { |
| 7780 | URLRequestFailedJob::AddUrlHandler(); |
| 7781 | |
| 7782 | GURL request_url = |
| 7783 | URLRequestFailedJob::GetMockHttpsUrl(ERR_CONNECTION_REFUSED); |
| 7784 | |
| 7785 | TestNetworkDelegate network_delegate; |
| 7786 | TestURLRequestContext context(true); |
| 7787 | context.set_network_delegate(&network_delegate); |
| 7788 | context.Init(); |
| 7789 | |
| 7790 | TestDelegate d; |
| 7791 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7792 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7793 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7794 | d.RunUntilComplete(); |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7795 | |
| 7796 | URLRequestFilter::GetInstance()->ClearHandlers(); |
| 7797 | } |
| 7798 | |
| 7799 | // TODO(juliatuttle): Figure out whether this restriction should be in place, |
| 7800 | // and either implement it or remove this test. |
| 7801 | TEST_F(URLRequestTestHTTP, DISABLED_DontForwardErrorToNelHttp) { |
| 7802 | URLRequestFailedJob::AddUrlHandler(); |
| 7803 | |
| 7804 | GURL request_url = |
| 7805 | URLRequestFailedJob::GetMockHttpUrl(ERR_CONNECTION_REFUSED); |
| 7806 | |
| 7807 | TestNetworkDelegate network_delegate; |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7808 | TestNetworkErrorLoggingService nel_service; |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7809 | TestURLRequestContext context(true); |
| 7810 | context.set_network_delegate(&network_delegate); |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7811 | context.set_network_error_logging_service(&nel_service); |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7812 | context.Init(); |
| 7813 | |
| 7814 | TestDelegate d; |
| 7815 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7816 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7817 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7818 | d.RunUntilComplete(); |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7819 | |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7820 | EXPECT_TRUE(nel_service.errors().empty()); |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7821 | |
| 7822 | URLRequestFilter::GetInstance()->ClearHandlers(); |
| 7823 | } |
| 7824 | |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7825 | TEST_F(URLRequestTestHTTP, ForwardErrorToNelHttps_Mock) { |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7826 | URLRequestFailedJob::AddUrlHandler(); |
| 7827 | |
| 7828 | GURL request_url = |
| 7829 | URLRequestFailedJob::GetMockHttpsUrl(ERR_CONNECTION_REFUSED); |
| 7830 | |
| 7831 | TestNetworkDelegate network_delegate; |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7832 | TestNetworkErrorLoggingService nel_service; |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7833 | TestURLRequestContext context(true); |
| 7834 | context.set_network_delegate(&network_delegate); |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7835 | context.set_network_error_logging_service(&nel_service); |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7836 | context.Init(); |
| 7837 | |
| 7838 | TestDelegate d; |
| 7839 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7840 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7841 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7842 | d.RunUntilComplete(); |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7843 | |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7844 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7845 | EXPECT_EQ(request_url, nel_service.errors()[0].uri); |
| 7846 | EXPECT_EQ(0, nel_service.errors()[0].status_code); |
| 7847 | EXPECT_EQ(ERR_CONNECTION_REFUSED, nel_service.errors()[0].type); |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7848 | |
| 7849 | URLRequestFilter::GetInstance()->ClearHandlers(); |
| 7850 | } |
| 7851 | |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7852 | // Also test with a real server, to exercise interactions with |
| 7853 | // URLRequestHttpJob. |
| 7854 | TEST_F(URLRequestTestHTTP, ForwardErrorToNelHttps_Real) { |
| 7855 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7856 | https_test_server.RegisterRequestHandler( |
| 7857 | base::BindRepeating(&SendEmptyResponse)); |
| 7858 | ASSERT_TRUE(https_test_server.Start()); |
| 7859 | GURL request_url = https_test_server.GetURL("/"); |
| 7860 | |
| 7861 | TestNetworkDelegate network_delegate; |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7862 | TestNetworkErrorLoggingService nel_service; |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7863 | TestURLRequestContext context(true); |
| 7864 | context.set_network_delegate(&network_delegate); |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7865 | context.set_network_error_logging_service(&nel_service); |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7866 | context.Init(); |
| 7867 | |
| 7868 | TestDelegate d; |
| 7869 | std::unique_ptr<URLRequest> request(context.CreateRequest( |
| 7870 | request_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 7871 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7872 | d.RunUntilComplete(); |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7873 | |
Julia Tuttle | cba7d22 | 2018-02-23 19:37:27 | [diff] [blame] | 7874 | ASSERT_EQ(1u, nel_service.errors().size()); |
| 7875 | EXPECT_EQ(request_url, nel_service.errors()[0].uri); |
| 7876 | EXPECT_EQ(0, nel_service.errors()[0].status_code); |
| 7877 | EXPECT_EQ(ERR_EMPTY_RESPONSE, nel_service.errors()[0].type); |
Julia Tuttle | 7005a2a | 2017-12-08 21:01:18 | [diff] [blame] | 7878 | } |
| 7879 | |
Julia Tuttle | fadc26d | 2017-10-13 18:12:29 | [diff] [blame] | 7880 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 7881 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7882 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7883 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7884 | |
| 7885 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7886 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7887 | http_test_server()->GetURL("/content-type-normalization.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7888 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7889 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7890 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7891 | |
| 7892 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7893 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7894 | EXPECT_EQ("text/html", mime_type); |
| 7895 | |
| 7896 | std::string charset; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7897 | req->GetCharset(&charset); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7898 | EXPECT_EQ("utf-8", charset); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7899 | req->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7900 | } |
| 7901 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7902 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) { |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7903 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7904 | GURL data_url("data:,foo"); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7905 | DataProtocolHandler data_protocol_handler; |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7906 | EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7907 | |
| 7908 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7909 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7910 | } |
| 7911 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7912 | #if !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7913 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) { |
| 7914 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
| 7915 | GURL file_url("file:///foo.txt"); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 7916 | FileProtocolHandler file_protocol_handler( |
| 7917 | base::ThreadTaskRunnerHandle::Get()); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7918 | EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url)); |
| 7919 | |
| 7920 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7921 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(file_url)); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 7922 | } |
| 7923 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7924 | TEST_F(URLRequestTestHTTP, RestrictFileRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7925 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7926 | |
| 7927 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7928 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7929 | http_test_server()->GetURL("/redirect-to-file.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7930 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7931 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7932 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7933 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7934 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7935 | |
| 7936 | // The redirect should have been rejected before reporting it to the caller. |
| 7937 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7938 | } |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 7939 | #endif // !BUILDFLAG(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7940 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7941 | TEST_F(URLRequestTestHTTP, RestrictDataRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7942 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7943 | |
| 7944 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7945 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7946 | http_test_server()->GetURL("/redirect-to-data.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7947 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7948 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7949 | d.RunUntilComplete(); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7950 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7951 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7952 | |
| 7953 | // The redirect should have been rejected before reporting it to the |
| 7954 | // caller. See https://crbug.com/723796 |
| 7955 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 7956 | } |
| 7957 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7958 | // Test that redirects to invalid URLs are rejected. See |
| 7959 | // https://crbug.com/462272. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7960 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7961 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7962 | |
| 7963 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 7964 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7965 | http_test_server()->GetURL("/redirect-to-invalid-url.html"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7966 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7967 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7968 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7969 | |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 7970 | EXPECT_EQ(1, d.response_started_count()); |
| 7971 | EXPECT_EQ(ERR_INVALID_REDIRECT, d.request_status()); |
| 7972 | |
| 7973 | // The redirect should have been rejected before reporting it to the caller. |
| 7974 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7975 | } |
| 7976 | |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7977 | // Make sure redirects are cached, despite not reading their bodies. |
| 7978 | TEST_F(URLRequestTestHTTP, CacheRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7979 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7980 | GURL redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7981 | http_test_server()->GetURL("/redirect302-to-echo-cacheable"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7982 | |
| 7983 | { |
| 7984 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7985 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7986 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7987 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 7988 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 7989 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7990 | EXPECT_EQ(1, d.received_redirect_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7991 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 7992 | } |
| 7993 | |
| 7994 | { |
| 7995 | TestDelegate d; |
| 7996 | d.set_quit_on_redirect(true); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 7997 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 7998 | redirect_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7999 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8000 | d.RunUntilComplete(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 8001 | |
| 8002 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8003 | EXPECT_EQ(0, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8004 | EXPECT_TRUE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 8005 | |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8006 | req->FollowDeferredRedirect(base::nullopt /* modified_request_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8007 | d.RunUntilComplete(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 8008 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8009 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8010 | EXPECT_EQ(OK, d.request_status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8011 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 8012 | } |
| 8013 | } |
| 8014 | |
| 8015 | // Make sure a request isn't cached when a NetworkDelegate forces a redirect |
| 8016 | // when the headers are read, since the body won't have been read. |
| 8017 | TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8018 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 8019 | // URL that is normally cached. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8020 | GURL initial_url = http_test_server()->GetURL("/cachetime"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 8021 | |
| 8022 | { |
| 8023 | // Set up the TestNetworkDelegate tp force a redirect. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8024 | GURL redirect_to_url = http_test_server()->GetURL("/echo"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 8025 | default_network_delegate_.set_redirect_on_headers_received_url( |
| 8026 | redirect_to_url); |
| 8027 | |
| 8028 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8029 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 8030 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8031 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8032 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8033 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 8034 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8035 | EXPECT_EQ(redirect_to_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 8036 | } |
| 8037 | |
| 8038 | { |
| 8039 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8040 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 8041 | initial_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8042 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8043 | d.RunUntilComplete(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 8044 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8045 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8046 | EXPECT_FALSE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 8047 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8048 | EXPECT_EQ(initial_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 8049 | } |
| 8050 | } |
| 8051 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 8052 | // Tests that redirection to an unsafe URL is allowed when it has been marked as |
| 8053 | // safe. |
| 8054 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8055 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 8056 | |
| 8057 | GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url"); |
| 8058 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 8059 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 8060 | |
| 8061 | TestDelegate d; |
| 8062 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8063 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8064 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 8065 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 8066 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8067 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8068 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 8069 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8070 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8071 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8072 | EXPECT_EQ(unsafe_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 8073 | EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received()); |
| 8074 | } |
| 8075 | } |
| 8076 | |
| 8077 | // Tests that a redirect to a different unsafe URL is blocked, even after adding |
| 8078 | // some other URL to the whitelist. |
| 8079 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8080 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 8081 | |
| 8082 | GURL unsafe_url("data:text/html,something"); |
| 8083 | GURL different_unsafe_url("data:text/html,something-else"); |
| 8084 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 8085 | default_network_delegate_.set_allowed_unsafe_redirect_url( |
| 8086 | different_unsafe_url); |
| 8087 | |
| 8088 | TestDelegate d; |
| 8089 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8090 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8091 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d, |
| 8092 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 8093 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8094 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8095 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 8096 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8097 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, d.request_status()); |
davidben | d894710b | 2017-06-06 19:28:30 | [diff] [blame] | 8098 | |
| 8099 | // The redirect should have been rejected before reporting it to the caller. |
| 8100 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 8101 | } |
| 8102 | } |
| 8103 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 8104 | // Redirects from an URL with fragment to an unsafe URL with fragment should |
| 8105 | // be allowed, and the reference fragment of the target URL should be preserved. |
| 8106 | TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8107 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 8108 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8109 | GURL original_url(http_test_server()->GetURL("/original#fragment1")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 8110 | GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 8111 | GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 8112 | |
| 8113 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 8114 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 8115 | |
| 8116 | TestDelegate d; |
| 8117 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8118 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8119 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 8120 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8121 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8122 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 8123 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8124 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8125 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8126 | EXPECT_EQ(original_url, r->original_url()); |
| 8127 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 8128 | } |
| 8129 | } |
| 8130 | |
| 8131 | // When a delegate has specified a safe redirect URL, but it does not match the |
| 8132 | // redirect target, then do not prevent the reference fragment from being added. |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8133 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8134 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 8135 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8136 | GURL original_url(http_test_server()->GetURL("/original#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 8137 | GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8138 | GURL redirect_url(http_test_server()->GetURL("/target")); |
| 8139 | GURL expected_redirect_url( |
| 8140 | http_test_server()->GetURL("/target#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 8141 | |
| 8142 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 8143 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 8144 | |
| 8145 | TestDelegate d; |
| 8146 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8147 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8148 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 8149 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8150 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8151 | d.RunUntilComplete(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 8152 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8153 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8154 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8155 | EXPECT_EQ(original_url, r->original_url()); |
| 8156 | EXPECT_EQ(expected_redirect_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 8157 | } |
| 8158 | } |
| 8159 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8160 | // When a delegate has specified a safe redirect URL, assume that the redirect |
| 8161 | // URL should not be changed. In particular, the reference fragment should not |
| 8162 | // be modified. |
| 8163 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8164 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8165 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8166 | GURL original_url( |
| 8167 | http_test_server()->GetURL("/original#should-not-be-appended")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8168 | GURL redirect_url("data:text/html,expect-no-reference-fragment"); |
| 8169 | |
| 8170 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 8171 | default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url); |
| 8172 | |
| 8173 | TestDelegate d; |
| 8174 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8175 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8176 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 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] | f878230e | 2014-04-03 15:36:14 | [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()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8183 | EXPECT_EQ(original_url, r->original_url()); |
| 8184 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8185 | } |
| 8186 | } |
| 8187 | |
| 8188 | // When a URLRequestRedirectJob is created, the redirection must be followed and |
| 8189 | // the reference fragment of the target URL must not be modified. |
| 8190 | TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8191 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8192 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8193 | GURL original_url( |
| 8194 | http_test_server()->GetURL("/original#should-not-be-appended")); |
| 8195 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8196 | |
| 8197 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8198 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8199 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8200 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8201 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8202 | r.get(), &default_network_delegate_, redirect_url, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8203 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 8204 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8205 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8206 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8207 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8208 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8209 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8210 | EXPECT_EQ(original_url, r->original_url()); |
| 8211 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8212 | } |
| 8213 | |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 8214 | TEST_F(URLRequestTestHTTP, UnsupportedReferrerScheme) { |
| 8215 | ASSERT_TRUE(http_test_server()->Start()); |
| 8216 | |
| 8217 | const std::string referrer("foobar://totally.legit.referrer"); |
| 8218 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8219 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8220 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8221 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 8222 | req->SetReferrer(referrer); |
| 8223 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8224 | d.RunUntilComplete(); |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 8225 | |
| 8226 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 8227 | } |
| 8228 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8229 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8230 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8231 | |
| 8232 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8233 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8234 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8235 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8236 | req->SetReferrer("http://user:[email protected]/"); |
| 8237 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8238 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8239 | |
| 8240 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 8241 | } |
| 8242 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8243 | TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8244 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8245 | |
| 8246 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8247 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8248 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8249 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8250 | req->SetReferrer("http://foo.com/test#fragment"); |
| 8251 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8252 | d.RunUntilComplete(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8253 | |
| 8254 | EXPECT_EQ(std::string("http://foo.com/test"), d.data_received()); |
| 8255 | } |
| 8256 | |
| 8257 | TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8258 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8259 | |
| 8260 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8261 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8262 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d, |
| 8263 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8264 | req->SetReferrer("http://foo.com/test#fragment"); |
| 8265 | req->SetReferrer(""); |
| 8266 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8267 | d.RunUntilComplete(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 8268 | |
| 8269 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 8270 | } |
| 8271 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8272 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8273 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8274 | |
| 8275 | TestDelegate d; |
| 8276 | { |
| 8277 | d.set_cancel_in_received_redirect(true); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8278 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8279 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 8280 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8281 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8282 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8283 | |
| 8284 | EXPECT_EQ(1, d.response_started_count()); |
| 8285 | EXPECT_EQ(0, d.bytes_received()); |
| 8286 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8287 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8288 | } |
| 8289 | } |
| 8290 | |
| 8291 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8292 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8293 | |
| 8294 | TestDelegate d; |
| 8295 | { |
| 8296 | d.set_quit_on_redirect(true); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8297 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8298 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 8299 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8300 | |
[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 | |
| 8304 | EXPECT_EQ(1, d.received_redirect_count()); |
Shivani Sharma | c18f976 | 2017-10-23 16:43:23 | [diff] [blame] | 8305 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8306 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 8307 | d.ClearFullRequestHeaders(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8308 | |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8309 | req->FollowDeferredRedirect(base::nullopt /* modified_request_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8310 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8311 | |
| 8312 | EXPECT_EQ(1, d.response_started_count()); |
| 8313 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8314 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8315 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 8316 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 8317 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 8318 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8319 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 8320 | |
| 8321 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 8322 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8323 | EXPECT_EQ(contents, d.data_received()); |
| 8324 | } |
| 8325 | } |
| 8326 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8327 | TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8328 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8329 | |
| 8330 | TestDelegate d; |
| 8331 | { |
| 8332 | d.set_quit_on_redirect(true); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8333 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8334 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 8335 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8336 | |
| 8337 | EXPECT_FALSE(d.have_full_request_headers()); |
| 8338 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8339 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8340 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8341 | |
| 8342 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8343 | |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8344 | req->FollowDeferredRedirect(base::nullopt /* modified_request_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8345 | d.RunUntilComplete(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8346 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8347 | GURL target_url(http_test_server()->GetURL("/with-headers.html")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8348 | EXPECT_EQ(1, d.response_started_count()); |
| 8349 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8350 | CheckFullRequestHeaders(d.full_request_headers(), target_url); |
| 8351 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8352 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8353 | |
| 8354 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 8355 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 8356 | path = path.Append(kTestFilePath); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8357 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 8358 | |
| 8359 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 8360 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 8361 | EXPECT_EQ(contents, d.data_received()); |
| 8362 | } |
| 8363 | } |
| 8364 | |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8365 | TEST_F(URLRequestTestHTTP, DeferredRedirect_ModifiedRequestHeaders) { |
| 8366 | ASSERT_TRUE(http_test_server()->Start()); |
| 8367 | |
| 8368 | TestDelegate d; |
| 8369 | { |
| 8370 | d.set_quit_on_redirect(true); |
| 8371 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
| 8372 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 8373 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 8374 | |
| 8375 | // Set initial headers for the request. |
| 8376 | req->SetExtraRequestHeaderByName("Header1", "Value1", true /* overwrite */); |
| 8377 | req->SetExtraRequestHeaderByName("Header2", "Value2", true /* overwrite */); |
| 8378 | |
| 8379 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8380 | d.RunUntilComplete(); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8381 | |
| 8382 | // Initial request should only have initial headers. |
| 8383 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8384 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8385 | const HttpRequestHeaders& sent_headers1 = d.full_request_headers(); |
| 8386 | std::string sent_value; |
| 8387 | EXPECT_TRUE(sent_headers1.GetHeader("Header1", &sent_value)); |
| 8388 | EXPECT_EQ("Value1", sent_value); |
| 8389 | EXPECT_TRUE(sent_headers1.GetHeader("Header2", &sent_value)); |
| 8390 | EXPECT_EQ("Value2", sent_value); |
| 8391 | EXPECT_FALSE(sent_headers1.GetHeader("Header3", &sent_value)); |
| 8392 | d.ClearFullRequestHeaders(); |
| 8393 | |
| 8394 | // Overwrite Header2 and add Header3. |
| 8395 | net::HttpRequestHeaders modified_request_headers; |
| 8396 | modified_request_headers.SetHeader("Header2", ""); |
| 8397 | modified_request_headers.SetHeader("Header3", "Value3"); |
| 8398 | |
| 8399 | req->FollowDeferredRedirect(modified_request_headers); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8400 | d.RunUntilComplete(); |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 8401 | |
| 8402 | EXPECT_EQ(1, d.response_started_count()); |
| 8403 | EXPECT_FALSE(d.received_data_before_response()); |
| 8404 | EXPECT_EQ(OK, d.request_status()); |
| 8405 | |
| 8406 | // Redirected request should also have modified headers. |
| 8407 | EXPECT_TRUE(d.have_full_request_headers()); |
| 8408 | const HttpRequestHeaders& sent_headers2 = d.full_request_headers(); |
| 8409 | EXPECT_TRUE(sent_headers2.GetHeader("Header1", &sent_value)); |
| 8410 | EXPECT_EQ("Value1", sent_value); |
| 8411 | EXPECT_TRUE(sent_headers2.GetHeader("Header2", &sent_value)); |
| 8412 | EXPECT_EQ("", sent_value); |
| 8413 | EXPECT_TRUE(sent_headers2.GetHeader("Header3", &sent_value)); |
| 8414 | EXPECT_EQ("Value3", sent_value); |
| 8415 | } |
| 8416 | } |
| 8417 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8418 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8419 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8420 | |
| 8421 | TestDelegate d; |
| 8422 | { |
| 8423 | d.set_quit_on_redirect(true); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8424 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8425 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 8426 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8427 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8428 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8429 | |
| 8430 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8431 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8432 | req->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8433 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8434 | |
| 8435 | EXPECT_EQ(1, d.response_started_count()); |
| 8436 | EXPECT_EQ(0, d.bytes_received()); |
| 8437 | EXPECT_FALSE(d.received_data_before_response()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8438 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8439 | } |
| 8440 | } |
| 8441 | |
| 8442 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8443 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8444 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8445 | // Populate the cache. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8446 | { |
| 8447 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8448 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8449 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8450 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8451 | HttpRequestHeaders headers; |
| 8452 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8453 | req->SetExtraRequestHeaders(headers); |
| 8454 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8455 | d.RunUntilComplete(); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8456 | |
| 8457 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8458 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8459 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8460 | } |
| 8461 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8462 | // Expect a cache hit. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8463 | { |
| 8464 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8465 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8466 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8467 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8468 | HttpRequestHeaders headers; |
| 8469 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8470 | req->SetExtraRequestHeaders(headers); |
| 8471 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8472 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8473 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8474 | EXPECT_TRUE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8475 | |
| 8476 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8477 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8478 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8479 | } |
| 8480 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8481 | // Expect a cache miss. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8482 | { |
| 8483 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8484 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8485 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8486 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8487 | HttpRequestHeaders headers; |
| 8488 | headers.SetHeader("foo", "2"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8489 | req->SetExtraRequestHeaders(headers); |
| 8490 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8491 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8492 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8493 | EXPECT_FALSE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8494 | |
| 8495 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8496 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8497 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8498 | } |
| 8499 | } |
| 8500 | |
| 8501 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8502 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8503 | |
| 8504 | // populate the cache |
| 8505 | { |
| 8506 | TestDelegate d; |
| 8507 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8508 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8509 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8510 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8511 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8512 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8513 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8514 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8515 | |
| 8516 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8517 | } |
| 8518 | |
| 8519 | // repeat request with end-to-end validation. since auth-basic results in a |
| 8520 | // cachable page, we expect this test to result in a 304. in which case, the |
| 8521 | // response should be fetched from the cache. |
| 8522 | { |
| 8523 | TestDelegate d; |
| 8524 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8525 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8526 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8527 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8528 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8529 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 8530 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8531 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8532 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8533 | |
| 8534 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8535 | |
| 8536 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8537 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8538 | } |
| 8539 | } |
| 8540 | |
| 8541 | // Check that Set-Cookie headers in 401 responses are respected. |
| 8542 | // http://crbug.com/6450 |
| 8543 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8544 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8545 | |
| 8546 | GURL url_requiring_auth = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8547 | http_test_server()->GetURL("/auth-basic?set-cookie-if-challenged"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8548 | |
| 8549 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 8550 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 8551 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8552 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8553 | TestURLRequestContext context(true); |
| 8554 | context.set_network_delegate(&network_delegate); |
| 8555 | context.Init(); |
| 8556 | |
| 8557 | TestDelegate d; |
| 8558 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8559 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8560 | std::unique_ptr<URLRequest> r( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8561 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &d, |
| 8562 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8563 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8564 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8565 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8566 | |
| 8567 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8568 | |
| 8569 | // Make sure we sent the cookie in the restarted transaction. |
| 8570 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 8571 | != std::string::npos); |
| 8572 | } |
| 8573 | |
| 8574 | // Same test as above, except this time the restart is initiated earlier |
| 8575 | // (without user intervention since identity is embedded in the URL). |
| 8576 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8577 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8578 | TestURLRequestContext context(true); |
| 8579 | context.set_network_delegate(&network_delegate); |
| 8580 | context.Init(); |
| 8581 | |
| 8582 | TestDelegate d; |
| 8583 | |
| 8584 | GURL::Replacements replacements; |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 8585 | replacements.SetUsernameStr("user2"); |
| 8586 | replacements.SetPasswordStr("secret"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8587 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 8588 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8589 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 8590 | url_with_identity, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8591 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8592 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8593 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8594 | |
| 8595 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 8596 | |
| 8597 | // Make sure we sent the cookie in the restarted transaction. |
| 8598 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 8599 | != std::string::npos); |
| 8600 | } |
| 8601 | } |
| 8602 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8603 | // Tests that load timing works as expected with auth and the cache. |
| 8604 | TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8605 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8606 | |
| 8607 | // populate the cache |
| 8608 | { |
| 8609 | TestDelegate d; |
| 8610 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8611 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8612 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8613 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8614 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8615 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8616 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8617 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8618 | |
| 8619 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8620 | |
| 8621 | LoadTimingInfo load_timing_info_before_auth; |
| 8622 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth( |
| 8623 | &load_timing_info_before_auth)); |
| 8624 | TestLoadTimingNotReused(load_timing_info_before_auth, |
| 8625 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 8626 | |
| 8627 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8628 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8629 | // The test server does not support keep alive sockets, so the second |
| 8630 | // request with auth should use a new socket. |
| 8631 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 8632 | EXPECT_NE(load_timing_info_before_auth.socket_log_id, |
| 8633 | load_timing_info.socket_log_id); |
| 8634 | EXPECT_LE(load_timing_info_before_auth.receive_headers_end, |
| 8635 | load_timing_info.connect_timing.connect_start); |
| 8636 | } |
| 8637 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8638 | // Repeat request with end-to-end validation. Since auth-basic results in a |
| 8639 | // 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] | 8640 | // response should be fetched from the cache. |
| 8641 | { |
| 8642 | TestDelegate d; |
| 8643 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 8644 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8645 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8646 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d, |
| 8647 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8648 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 8649 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8650 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8651 | d.RunUntilComplete(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8652 | |
| 8653 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 8654 | |
| 8655 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8656 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8657 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8658 | // Since there was a request that went over the wire, the load timing |
| 8659 | // information should include connection times. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8660 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8661 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8662 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8663 | } |
| 8664 | } |
| 8665 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8666 | // In this test, we do a POST which the server will 302 redirect. |
| 8667 | // The subsequent transaction should use GET, and should not send the |
| 8668 | // Content-Type header. |
| 8669 | // http://code.google.com/p/chromium/issues/detail?id=843 |
| 8670 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8671 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8672 | |
| 8673 | const char kData[] = "hello world"; |
| 8674 | |
| 8675 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8676 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8677 | http_test_server()->GetURL("/redirect-to-echoall"), DEFAULT_PRIORITY, &d, |
| 8678 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8679 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8680 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8681 | |
| 8682 | // Set headers (some of which are specific to the POST). |
| 8683 | HttpRequestHeaders headers; |
| 8684 | headers.AddHeadersFromString( |
| 8685 | "Content-Type: multipart/form-data; " |
| 8686 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" |
| 8687 | "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," |
| 8688 | "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" |
| 8689 | "Accept-Language: en-US,en\r\n" |
| 8690 | "Accept-Charset: ISO-8859-1,*,utf-8\r\n" |
| 8691 | "Content-Length: 11\r\n" |
| 8692 | "Origin: http://localhost:1337/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8693 | req->SetExtraRequestHeaders(headers); |
| 8694 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8695 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8696 | |
| 8697 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8698 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8699 | EXPECT_EQ("text/html", mime_type); |
| 8700 | |
| 8701 | const std::string& data = d.data_received(); |
| 8702 | |
| 8703 | // Check that the post-specific headers were stripped: |
| 8704 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 8705 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8706 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8707 | |
| 8708 | // These extra request headers should not have been stripped. |
| 8709 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 8710 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 8711 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 8712 | } |
| 8713 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8714 | // The following tests check that we handle mutating the request for HTTP |
| 8715 | // redirects as expected. |
| 8716 | // See https://crbug.com/56373, https://crbug.com/102130, and |
| 8717 | // https://crbug.com/465517. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8718 | |
| 8719 | TEST_F(URLRequestTestHTTP, Redirect301Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8720 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8721 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8722 | const GURL url = http_test_server()->GetURL("/redirect301-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8723 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8724 | http_test_server()->GetURL("/redirect301-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8725 | |
| 8726 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8727 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8728 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8729 | |
| 8730 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8731 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8732 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8733 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8734 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8735 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8736 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8737 | } |
| 8738 | |
| 8739 | TEST_F(URLRequestTestHTTP, Redirect302Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8740 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8741 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8742 | const GURL url = http_test_server()->GetURL("/redirect302-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8743 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8744 | http_test_server()->GetURL("/redirect302-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8745 | |
| 8746 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8747 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8748 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8749 | |
| 8750 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8751 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8752 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8753 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8754 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8755 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8756 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8757 | } |
| 8758 | |
| 8759 | TEST_F(URLRequestTestHTTP, Redirect303Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8760 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8761 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8762 | const GURL url = http_test_server()->GetURL("/redirect303-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8763 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8764 | http_test_server()->GetURL("/redirect303-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8765 | |
| 8766 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 8767 | HTTPRedirectMethodTest(url, "PUT", "GET", true); |
| 8768 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8769 | |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8770 | HTTPRedirectOriginHeaderTest(url, "CONNECT", "GET", std::string()); |
| 8771 | HTTPRedirectOriginHeaderTest(https_redirect_url, "CONNECT", "GET", |
| 8772 | std::string()); |
| 8773 | HTTPRedirectOriginHeaderTest(url, "DELETE", "GET", std::string()); |
| 8774 | HTTPRedirectOriginHeaderTest(https_redirect_url, "DELETE", "GET", |
| 8775 | std::string()); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8776 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8777 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8778 | HTTPRedirectOriginHeaderTest(url, "HEAD", "HEAD", url.GetOrigin().spec()); |
| 8779 | HTTPRedirectOriginHeaderTest(https_redirect_url, "HEAD", "HEAD", "null"); |
| 8780 | HTTPRedirectOriginHeaderTest(url, "OPTIONS", "GET", std::string()); |
| 8781 | HTTPRedirectOriginHeaderTest(https_redirect_url, "OPTIONS", "GET", |
| 8782 | std::string()); |
Yutaka Hirano | 957d722f | 2017-08-31 08:40:49 | [diff] [blame] | 8783 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 8784 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 8785 | std::string()); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8786 | HTTPRedirectOriginHeaderTest(url, "PUT", "GET", std::string()); |
| 8787 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "GET", std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8788 | } |
| 8789 | |
| 8790 | TEST_F(URLRequestTestHTTP, Redirect307Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8791 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8792 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8793 | const GURL url = http_test_server()->GetURL("/redirect307-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8794 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8795 | http_test_server()->GetURL("/redirect307-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8796 | |
| 8797 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 8798 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8799 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8800 | |
| 8801 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8802 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 8803 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 8804 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8805 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8806 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8807 | } |
| 8808 | |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8809 | TEST_F(URLRequestTestHTTP, Redirect308Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8810 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8811 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8812 | const GURL url = http_test_server()->GetURL("/redirect308-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8813 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8814 | http_test_server()->GetURL("/redirect308-to-https"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8815 | |
| 8816 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 8817 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 8818 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 8819 | |
| 8820 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 8821 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 8822 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 8823 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
Alex Clarke | 1e08882b3 | 2017-10-06 14:22:40 | [diff] [blame] | 8824 | HTTPRedirectOriginHeaderTest(url, "PUT", "PUT", url.GetOrigin().spec()); |
| 8825 | HTTPRedirectOriginHeaderTest(https_redirect_url, "PUT", "PUT", "null"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8826 | } |
| 8827 | |
| 8828 | // Make sure that 308 responses without bodies are not treated as redirects. |
| 8829 | // Certain legacy apis that pre-date the response code expect this behavior |
| 8830 | // (Like Google Drive). |
| 8831 | TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8832 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8833 | |
| 8834 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8835 | const GURL url = http_test_server()->GetURL("/308-without-location-header"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8836 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8837 | std::unique_ptr<URLRequest> request(default_context_.CreateRequest( |
| 8838 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8839 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8840 | request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8841 | d.RunUntilComplete(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8842 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8843 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8844 | EXPECT_EQ(308, request->response_headers()->response_code()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 8845 | EXPECT_EQ("This is not a redirect.", d.data_received()); |
| 8846 | } |
| 8847 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8848 | TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8849 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8850 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8851 | GURL original_url( |
| 8852 | http_test_server()->GetURL("/redirect302-to-echo#fragment")); |
| 8853 | GURL expected_url(http_test_server()->GetURL("/echo#fragment")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8854 | |
| 8855 | TestDelegate d; |
| 8856 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8857 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8858 | original_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8859 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8860 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8861 | d.RunUntilComplete(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8862 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8863 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8864 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8865 | EXPECT_EQ(original_url, r->original_url()); |
| 8866 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 8867 | } |
| 8868 | } |
| 8869 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8870 | TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8871 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8872 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8873 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8874 | GURL first_party_url("http://example.com"); |
| 8875 | |
| 8876 | TestDelegate d; |
| 8877 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8878 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8879 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8880 | r->set_site_for_cookies(first_party_url); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8881 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8882 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8883 | d.RunUntilComplete(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8884 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8885 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8886 | EXPECT_EQ(OK, d.request_status()); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8887 | EXPECT_EQ(first_party_url, r->site_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8888 | } |
| 8889 | } |
| 8890 | |
| 8891 | TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8892 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8893 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8894 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8895 | GURL original_first_party_url("http://example.com"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8896 | GURL expected_first_party_url(http_test_server()->GetURL("/echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8897 | |
| 8898 | TestDelegate d; |
| 8899 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8900 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8901 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8902 | r->set_site_for_cookies(original_first_party_url); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8903 | r->set_first_party_url_policy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8904 | URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 8905 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8906 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8907 | d.RunUntilComplete(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8908 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8909 | EXPECT_EQ(2U, r->url_chain().size()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 8910 | EXPECT_EQ(OK, d.request_status()); |
Mike West | b85da8ed | 2017-08-10 14:16:46 | [diff] [blame] | 8911 | EXPECT_EQ(expected_first_party_url, r->site_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8912 | } |
| 8913 | } |
| 8914 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8915 | TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8916 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8917 | |
| 8918 | const char kData[] = "hello world"; |
| 8919 | |
| 8920 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8921 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8922 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 8923 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8924 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8925 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8926 | HttpRequestHeaders headers; |
| 8927 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Brett Wilson | 5accd24 | 2017-11-30 22:07:32 | [diff] [blame] | 8928 | base::NumberToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8929 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8930 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8931 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8932 | req.get(), &default_network_delegate_, |
| 8933 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8934 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 8935 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8936 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8937 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8938 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8939 | EXPECT_EQ("GET", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8940 | } |
| 8941 | |
| 8942 | TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8943 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8944 | |
| 8945 | const char kData[] = "hello world"; |
| 8946 | |
| 8947 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8948 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8949 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 8950 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8951 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8952 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8953 | HttpRequestHeaders headers; |
| 8954 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
Brett Wilson | 5accd24 | 2017-11-30 22:07:32 | [diff] [blame] | 8955 | base::NumberToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8956 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8957 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8958 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8959 | req.get(), &default_network_delegate_, |
| 8960 | http_test_server()->GetURL("/echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 8961 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 8962 | "Very Good Reason")); |
| 8963 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8964 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8965 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8966 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8967 | EXPECT_EQ("POST", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8968 | EXPECT_EQ(kData, d.data_received()); |
| 8969 | } |
| 8970 | |
| 8971 | // Check that default A-L header is sent. |
| 8972 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8973 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8974 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 8975 | StaticHttpUserAgentSettings settings("en", std::string()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8976 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8977 | TestURLRequestContext context(true); |
| 8978 | context.set_network_delegate(&network_delegate); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 8979 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8980 | context.Init(); |
| 8981 | |
| 8982 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 8983 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8984 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 8985 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8986 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 8987 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8988 | EXPECT_EQ("en", d.data_received()); |
| 8989 | } |
| 8990 | |
| 8991 | // Check that an empty A-L header is not sent. http://crbug.com/77365. |
| 8992 | TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8993 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8994 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 8995 | std::string empty_string; // Avoid most vexing parse on line below. |
| 8996 | StaticHttpUserAgentSettings settings(empty_string, empty_string); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8997 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8998 | TestURLRequestContext context(true); |
| 8999 | context.set_network_delegate(&network_delegate); |
| 9000 | context.Init(); |
| 9001 | // We override the language after initialization because empty entries |
| 9002 | // get overridden by Init(). |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9003 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9004 | |
| 9005 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9006 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9007 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9008 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9009 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9010 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9011 | EXPECT_EQ("None", d.data_received()); |
| 9012 | } |
| 9013 | |
| 9014 | // Check that if request overrides the A-L header, the default is not appended. |
| 9015 | // See http://crbug.com/20894 |
| 9016 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9017 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9018 | |
| 9019 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9020 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9021 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9022 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9023 | HttpRequestHeaders headers; |
| 9024 | headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9025 | req->SetExtraRequestHeaders(headers); |
| 9026 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9027 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9028 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 9029 | } |
| 9030 | |
| 9031 | // Check that default A-E header is sent. |
| 9032 | TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9033 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9034 | |
| 9035 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9036 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9037 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9038 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9039 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9040 | req->SetExtraRequestHeaders(headers); |
| 9041 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9042 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9043 | EXPECT_TRUE(ContainsString(d.data_received(), "gzip")); |
| 9044 | } |
| 9045 | |
| 9046 | // Check that if request overrides the A-E header, the default is not appended. |
| 9047 | // See http://crbug.com/47381 |
| 9048 | TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9049 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9050 | |
| 9051 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9052 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9053 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9054 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9055 | HttpRequestHeaders headers; |
| 9056 | headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9057 | req->SetExtraRequestHeaders(headers); |
| 9058 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9059 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9060 | EXPECT_FALSE(ContainsString(d.data_received(), "gzip")); |
| 9061 | EXPECT_TRUE(ContainsString(d.data_received(), "identity")); |
| 9062 | } |
| 9063 | |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 9064 | // Check that setting the A-C header sends the proper header. |
| 9065 | TEST_F(URLRequestTestHTTP, SetAcceptCharset) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9066 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9067 | |
| 9068 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9069 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9070 | http_test_server()->GetURL("/echoheader?Accept-Charset"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9071 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9072 | HttpRequestHeaders headers; |
| 9073 | headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9074 | req->SetExtraRequestHeaders(headers); |
| 9075 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9076 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9077 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 9078 | } |
| 9079 | |
| 9080 | // Check that default User-Agent header is sent. |
| 9081 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9082 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9083 | |
| 9084 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9085 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9086 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9087 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9088 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9089 | d.RunUntilComplete(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9090 | EXPECT_EQ(default_context_.http_user_agent_settings()->GetUserAgent(), |
[email protected] | 051a4b3 | 2014-01-09 04:02:37 | [diff] [blame] | 9091 | d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9092 | } |
| 9093 | |
| 9094 | // Check that if request overrides the User-Agent header, |
| 9095 | // the default is not appended. |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 9096 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 9097 | #if defined(OS_IOS) |
| 9098 | #define MAYBE_OverrideUserAgent FLAKY_OverrideUserAgent |
| 9099 | #else |
| 9100 | #define MAYBE_OverrideUserAgent OverrideUserAgent |
| 9101 | #endif |
| 9102 | TEST_F(URLRequestTestHTTP, MAYBE_OverrideUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9103 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9104 | |
| 9105 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9106 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9107 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9108 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9109 | HttpRequestHeaders headers; |
| 9110 | headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9111 | req->SetExtraRequestHeaders(headers); |
| 9112 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9113 | d.RunUntilComplete(); |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 9114 | EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9115 | } |
| 9116 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9117 | // Check that a NULL HttpUserAgentSettings causes the corresponding empty |
| 9118 | // User-Agent header to be sent but does not send the Accept-Language and |
| 9119 | // Accept-Charset headers. |
| 9120 | TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9121 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9122 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9123 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9124 | TestURLRequestContext context(true); |
| 9125 | context.set_network_delegate(&network_delegate); |
| 9126 | context.Init(); |
| 9127 | // We override the HttpUserAgentSettings after initialization because empty |
| 9128 | // entries get overridden by Init(). |
| 9129 | context.set_http_user_agent_settings(NULL); |
| 9130 | |
| 9131 | struct { |
| 9132 | const char* request; |
| 9133 | const char* expected_response; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9134 | } tests[] = {{"/echoheader?Accept-Language", "None"}, |
| 9135 | {"/echoheader?Accept-Charset", "None"}, |
| 9136 | {"/echoheader?User-Agent", ""}}; |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9137 | |
viettrungluu | e4a8b88 | 2014-10-16 06:17:38 | [diff] [blame] | 9138 | for (size_t i = 0; i < arraysize(tests); i++) { |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9139 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9140 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9141 | http_test_server()->GetURL(tests[i].request), DEFAULT_PRIORITY, &d, |
| 9142 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9143 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9144 | d.RunUntilComplete(); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 9145 | EXPECT_EQ(tests[i].expected_response, d.data_received()) |
| 9146 | << " Request = \"" << tests[i].request << "\""; |
| 9147 | } |
| 9148 | } |
| 9149 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9150 | // Make sure that URLRequest passes on its priority updates to |
| 9151 | // newly-created jobs after the first one. |
| 9152 | TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9153 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9154 | |
| 9155 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9156 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9157 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9158 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9159 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9160 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9161 | std::unique_ptr<URLRequestRedirectJob> redirect_job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9162 | req.get(), &default_network_delegate_, |
| 9163 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9164 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 9165 | AddTestInterceptor()->set_main_intercept_job(std::move(redirect_job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9166 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9167 | req->SetPriority(LOW); |
| 9168 | req->Start(); |
| 9169 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9170 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9171 | RequestPriority job_priority; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9172 | std::unique_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9173 | req.get(), &default_network_delegate_, &job_priority)); |
| 9174 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9175 | |
| 9176 | // Should trigger |job| to be started. |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9177 | d.RunUntilComplete(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9178 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 9179 | } |
| 9180 | |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9181 | // Check that creating a network request while entering/exiting suspend mode |
| 9182 | // fails as it should. This is the only case where an HttpTransactionFactory |
| 9183 | // does not return an HttpTransaction. |
| 9184 | TEST_F(URLRequestTestHTTP, NetworkSuspendTest) { |
| 9185 | // Create a new HttpNetworkLayer that thinks it's suspended. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9186 | std::unique_ptr<HttpNetworkLayer> network_layer(new HttpNetworkLayer( |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9187 | default_context_.http_transaction_factory()->GetSession())); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9188 | network_layer->OnSuspend(); |
| 9189 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 9190 | HttpCache http_cache(std::move(network_layer), |
zhongyi | 3ceab9f | 2017-02-04 02:06:03 | [diff] [blame] | 9191 | HttpCache::DefaultBackend::InMemory(0), |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 9192 | false /* is_main_cache */); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9193 | |
| 9194 | TestURLRequestContext context(true); |
| 9195 | context.set_http_transaction_factory(&http_cache); |
| 9196 | context.Init(); |
| 9197 | |
| 9198 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9199 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9200 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 9201 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9202 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9203 | d.RunUntilComplete(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9204 | |
| 9205 | EXPECT_TRUE(d.request_failed()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9206 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, d.request_status()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9207 | } |
| 9208 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9209 | namespace { |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9210 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9211 | // HttpTransactionFactory that synchronously fails to create transactions. |
| 9212 | class FailingHttpTransactionFactory : public HttpTransactionFactory { |
| 9213 | public: |
| 9214 | explicit FailingHttpTransactionFactory(HttpNetworkSession* network_session) |
| 9215 | : network_session_(network_session) {} |
| 9216 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 9217 | ~FailingHttpTransactionFactory() override = default; |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9218 | |
| 9219 | // HttpTransactionFactory methods: |
| 9220 | int CreateTransaction(RequestPriority priority, |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9221 | std::unique_ptr<HttpTransaction>* trans) override { |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9222 | return ERR_FAILED; |
| 9223 | } |
| 9224 | |
| 9225 | HttpCache* GetCache() override { return nullptr; } |
| 9226 | |
| 9227 | HttpNetworkSession* GetSession() override { return network_session_; } |
| 9228 | |
| 9229 | private: |
| 9230 | HttpNetworkSession* network_session_; |
| 9231 | |
| 9232 | DISALLOW_COPY_AND_ASSIGN(FailingHttpTransactionFactory); |
| 9233 | }; |
| 9234 | |
| 9235 | } // namespace |
| 9236 | |
| 9237 | // Check that when a request that fails to create an HttpTransaction can be |
| 9238 | // cancelled while the failure notification is pending, and doesn't send two |
| 9239 | // failure notifications. |
| 9240 | // |
| 9241 | // This currently only happens when in suspend mode and there's no cache, but |
| 9242 | // just use a special HttpTransactionFactory, to avoid depending on those |
| 9243 | // behaviors. |
| 9244 | TEST_F(URLRequestTestHTTP, NetworkCancelAfterCreateTransactionFailsTest) { |
| 9245 | FailingHttpTransactionFactory http_transaction_factory( |
| 9246 | default_context_.http_transaction_factory()->GetSession()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9247 | TestURLRequestContext context(true); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9248 | context.set_http_transaction_factory(&http_transaction_factory); |
| 9249 | context.set_network_delegate(default_network_delegate()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9250 | context.Init(); |
| 9251 | |
| 9252 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9253 | std::unique_ptr<URLRequest> req( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9254 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, |
| 9255 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9256 | // Don't send cookies (Collecting cookies is asynchronous, and need request to |
| 9257 | // try to create an HttpNetworkTransaction synchronously on start). |
| 9258 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9259 | req->Start(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9260 | req->Cancel(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9261 | d.RunUntilComplete(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9262 | // Run pending error task, if there is one. |
| 9263 | base::RunLoop().RunUntilIdle(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9264 | |
| 9265 | EXPECT_TRUE(d.request_failed()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9266 | EXPECT_EQ(1, d.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9267 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 9268 | |
| 9269 | // NetworkDelegate should see the cancellation, but not the error. |
| 9270 | EXPECT_EQ(1, default_network_delegate()->canceled_requests()); |
| 9271 | EXPECT_EQ(0, default_network_delegate()->error_count()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 9272 | } |
| 9273 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9274 | TEST_F(URLRequestTestHTTP, NetworkAccessedSetOnNetworkRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9275 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9276 | |
| 9277 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9278 | GURL test_url(http_test_server()->GetURL("/")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9279 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 9280 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9281 | |
| 9282 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9283 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9284 | |
| 9285 | EXPECT_TRUE(req->response_info().network_accessed); |
| 9286 | } |
| 9287 | |
| 9288 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnCachedResponse) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9289 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9290 | |
| 9291 | // Populate the cache. |
| 9292 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9293 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9294 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d, |
| 9295 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9296 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9297 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9298 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9299 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9300 | EXPECT_TRUE(req->response_info().network_accessed); |
| 9301 | EXPECT_FALSE(req->response_info().was_cached); |
| 9302 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9303 | req = default_context_.CreateRequest(http_test_server()->GetURL("/cachetime"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9304 | DEFAULT_PRIORITY, &d, |
| 9305 | TRAFFIC_ANNOTATION_FOR_TESTS); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9306 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9307 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9308 | |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9309 | EXPECT_EQ(OK, d.request_status()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9310 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9311 | EXPECT_TRUE(req->response_info().was_cached); |
| 9312 | } |
| 9313 | |
| 9314 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnLoadOnlyFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9315 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9316 | |
| 9317 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9318 | GURL test_url(http_test_server()->GetURL("/")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9319 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 9320 | test_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
jkarlin | a067deed | 2016-10-27 14:48:33 | [diff] [blame] | 9321 | req->SetLoadFlags(LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9322 | |
| 9323 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9324 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9325 | |
| 9326 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9327 | } |
| 9328 | |
rdsmith | bf8c3c1 | 2016-11-18 18:16:24 | [diff] [blame] | 9329 | // Test that a single job with a THROTTLED priority completes |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 9330 | // correctly in the absence of contention. |
| 9331 | TEST_F(URLRequestTestHTTP, ThrottledPriority) { |
| 9332 | ASSERT_TRUE(http_test_server()->Start()); |
| 9333 | |
| 9334 | TestDelegate d; |
| 9335 | GURL test_url(http_test_server()->GetURL("/")); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9336 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 9337 | test_url, THROTTLED, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 9338 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9339 | d.RunUntilComplete(); |
rdsmith | 5eb6fbc | 2016-10-21 17:36:08 | [diff] [blame] | 9340 | |
| 9341 | EXPECT_TRUE(req->status().is_success()); |
| 9342 | } |
| 9343 | |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9344 | TEST_F(URLRequestTestHTTP, RawBodyBytesNoContentEncoding) { |
| 9345 | ASSERT_TRUE(http_test_server()->Start()); |
| 9346 | |
| 9347 | TestDelegate d; |
| 9348 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9349 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 9350 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9351 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9352 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9353 | |
| 9354 | EXPECT_EQ(5, req->GetRawBodyBytes()); |
| 9355 | } |
| 9356 | |
| 9357 | TEST_F(URLRequestTestHTTP, RawBodyBytesGzipEncoding) { |
| 9358 | ASSERT_TRUE(http_test_server()->Start()); |
| 9359 | |
| 9360 | TestDelegate d; |
| 9361 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9362 | http_test_server()->GetURL("/gzip-encoded"), DEFAULT_PRIORITY, &d, |
| 9363 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9364 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9365 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 9366 | |
| 9367 | EXPECT_EQ(30, req->GetRawBodyBytes()); |
| 9368 | } |
| 9369 | |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9370 | // Check that if NetworkDelegate::OnBeforeStartTransaction returns an error, |
| 9371 | // the delegate isn't called back synchronously. |
| 9372 | TEST_F(URLRequestTestHTTP, TesBeforeStartTransactionFails) { |
| 9373 | ASSERT_TRUE(http_test_server()->Start()); |
| 9374 | default_network_delegate_.set_before_start_transaction_fails(); |
| 9375 | |
| 9376 | TestDelegate d; |
| 9377 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9378 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d, |
| 9379 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9380 | req->Start(); |
| 9381 | DCHECK(!d.response_completed()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9382 | d.RunUntilComplete(); |
jam | d3f5a3c | 2016-10-27 18:52:02 | [diff] [blame] | 9383 | DCHECK(d.response_completed()); |
| 9384 | EXPECT_EQ(ERR_FAILED, d.request_status()); |
| 9385 | } |
| 9386 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9387 | class URLRequestInterceptorTestHTTP : public URLRequestTestHTTP { |
| 9388 | public: |
| 9389 | // TODO(bengr): Merge this with the URLRequestInterceptorHTTPTest fixture, |
| 9390 | // ideally remove the dependency on URLRequestTestJob, and maybe move these |
| 9391 | // tests into the factory tests. |
| 9392 | URLRequestInterceptorTestHTTP() : URLRequestTestHTTP(), interceptor_(NULL) { |
| 9393 | } |
| 9394 | |
| 9395 | void SetUpFactory() override { |
| 9396 | interceptor_ = new MockURLRequestInterceptor(); |
| 9397 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9398 | std::move(job_factory_), base::WrapUnique(interceptor_))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9399 | } |
| 9400 | |
| 9401 | MockURLRequestInterceptor* interceptor() const { |
| 9402 | return interceptor_; |
| 9403 | } |
| 9404 | |
| 9405 | private: |
| 9406 | MockURLRequestInterceptor* interceptor_; |
| 9407 | }; |
| 9408 | |
| 9409 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9410 | NetworkDelegateNotificationOnRedirectIntercept) { |
| 9411 | interceptor()->set_intercept_redirect(true); |
| 9412 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 9413 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 9414 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9415 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9416 | |
| 9417 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9418 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9419 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 9420 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9421 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9422 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9423 | |
| 9424 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 9425 | // Check we got one good response |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9426 | int status = d.request_status(); |
| 9427 | EXPECT_EQ(OK, status); |
| 9428 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9429 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9430 | |
| 9431 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 9432 | EXPECT_EQ(1, d.response_started_count()); |
| 9433 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9434 | |
| 9435 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9436 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9437 | EXPECT_EQ(1, default_network_delegate()->headers_received_count()); |
| 9438 | } |
| 9439 | |
| 9440 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9441 | NetworkDelegateNotificationOnErrorIntercept) { |
| 9442 | // Intercept that error and respond with an OK response. |
| 9443 | interceptor()->set_intercept_final_response(true); |
| 9444 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 9445 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 9446 | default_network_delegate()->set_can_be_intercepted_on_error(true); |
| 9447 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9448 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9449 | |
| 9450 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9451 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9452 | http_test_server()->GetURL("/two-content-lengths.html"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9453 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9454 | req->set_method("GET"); |
| 9455 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9456 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9457 | |
| 9458 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 9459 | |
| 9460 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9461 | int status = d.request_status(); |
| 9462 | EXPECT_EQ(OK, status); |
| 9463 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9464 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9465 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 9466 | EXPECT_EQ(1, d.response_started_count()); |
| 9467 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9468 | |
| 9469 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9470 | EXPECT_EQ(1, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9471 | EXPECT_EQ(0, default_network_delegate()->headers_received_count()); |
| 9472 | } |
| 9473 | |
| 9474 | TEST_F(URLRequestInterceptorTestHTTP, |
| 9475 | NetworkDelegateNotificationOnResponseIntercept) { |
| 9476 | // Intercept that error and respond with an OK response. |
| 9477 | interceptor()->set_intercept_final_response(true); |
| 9478 | |
| 9479 | // Intercept with a real URLRequestHttpJob. |
| 9480 | interceptor()->set_use_url_request_http_job(true); |
| 9481 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9482 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9483 | |
| 9484 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9485 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9486 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d, |
| 9487 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9488 | req->set_method("GET"); |
| 9489 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9490 | d.RunUntilComplete(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9491 | |
| 9492 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 9493 | |
| 9494 | // Check we received one good response. |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9495 | int status = d.request_status(); |
| 9496 | EXPECT_EQ(OK, status); |
| 9497 | if (status == OK) |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9498 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9499 | EXPECT_EQ("hello", d.data_received()); |
| 9500 | EXPECT_EQ(1, d.response_started_count()); |
| 9501 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9502 | |
| 9503 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
ryansturm | 2343cb6 | 2016-06-15 01:09:00 | [diff] [blame] | 9504 | EXPECT_EQ(2, default_network_delegate()->before_start_transaction_count()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 9505 | EXPECT_EQ(2, default_network_delegate()->headers_received_count()); |
| 9506 | } |
| 9507 | |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9508 | class URLRequestTestReferrerPolicy : public URLRequestTest { |
| 9509 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 9510 | URLRequestTestReferrerPolicy() = default; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9511 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9512 | void InstantiateSameOriginServers(net::EmbeddedTestServer::Type type) { |
| 9513 | origin_server_.reset(new EmbeddedTestServer(type)); |
| 9514 | if (type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9515 | origin_server_->AddDefaultHandlers( |
| 9516 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9517 | } else { |
| 9518 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9519 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9520 | ASSERT_TRUE(origin_server_->Start()); |
| 9521 | } |
| 9522 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9523 | void InstantiateCrossOriginServers(net::EmbeddedTestServer::Type origin_type, |
| 9524 | net::EmbeddedTestServer::Type dest_type) { |
| 9525 | origin_server_.reset(new EmbeddedTestServer(origin_type)); |
| 9526 | if (origin_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9527 | origin_server_->AddDefaultHandlers( |
| 9528 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9529 | } else { |
| 9530 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9531 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9532 | ASSERT_TRUE(origin_server_->Start()); |
| 9533 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9534 | destination_server_.reset(new EmbeddedTestServer(dest_type)); |
| 9535 | if (dest_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 9536 | destination_server_->AddDefaultHandlers( |
| 9537 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 9538 | } else { |
| 9539 | destination_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 9540 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9541 | ASSERT_TRUE(destination_server_->Start()); |
| 9542 | } |
| 9543 | |
| 9544 | void VerifyReferrerAfterRedirect(URLRequest::ReferrerPolicy policy, |
| 9545 | const GURL& referrer, |
| 9546 | const GURL& expected) { |
| 9547 | // Create and execute the request: we'll only have a |destination_server_| |
| 9548 | // if the origins are meant to be distinct. Otherwise, we'll use the |
| 9549 | // |origin_server_| for both endpoints. |
| 9550 | GURL destination_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9551 | destination_server_ ? destination_server_->GetURL("/echoheader?Referer") |
| 9552 | : origin_server_->GetURL("/echoheader?Referer"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9553 | GURL origin_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9554 | origin_server_->GetURL("/server-redirect?" + destination_url.spec()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9555 | |
| 9556 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9557 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 9558 | origin_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9559 | req->set_referrer_policy(policy); |
| 9560 | req->SetReferrer(referrer.spec()); |
| 9561 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9562 | d.RunUntilComplete(); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9563 | |
| 9564 | EXPECT_EQ(1, d.response_started_count()); |
| 9565 | EXPECT_EQ(1, d.received_redirect_count()); |
| 9566 | EXPECT_EQ(destination_url, req->url()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 9567 | EXPECT_EQ(OK, d.request_status()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9568 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 9569 | |
| 9570 | EXPECT_EQ(expected.spec(), req->referrer()); |
| 9571 | if (expected.is_empty()) |
| 9572 | EXPECT_EQ("None", d.data_received()); |
| 9573 | else |
| 9574 | EXPECT_EQ(expected.spec(), d.data_received()); |
| 9575 | } |
| 9576 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9577 | EmbeddedTestServer* origin_server() const { return origin_server_.get(); } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9578 | |
| 9579 | private: |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9580 | std::unique_ptr<EmbeddedTestServer> origin_server_; |
| 9581 | std::unique_ptr<EmbeddedTestServer> destination_server_; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9582 | }; |
| 9583 | |
| 9584 | TEST_F(URLRequestTestReferrerPolicy, HTTPToSameOriginHTTP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9585 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTP); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9586 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9587 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9588 | VerifyReferrerAfterRedirect( |
| 9589 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9590 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9591 | |
| 9592 | VerifyReferrerAfterRedirect( |
| 9593 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9594 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9595 | |
| 9596 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9597 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9598 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9599 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9600 | referrer); |
| 9601 | |
| 9602 | // The original referrer set on the request is expected to obey the referrer |
| 9603 | // policy and already be stripped to the origin; thus this test case just |
| 9604 | // checks that this policy doesn't cause the referrer to change when following |
| 9605 | // a redirect. |
| 9606 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9607 | referrer.GetOrigin()); |
| 9608 | |
| 9609 | VerifyReferrerAfterRedirect( |
| 9610 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9611 | referrer); |
| 9612 | |
| 9613 | // The original referrer set on the request is expected to obey the referrer |
| 9614 | // policy and already be stripped to the origin; thus this test case just |
| 9615 | // checks that this policy doesn't cause the referrer to change when following |
| 9616 | // a redirect. |
| 9617 | VerifyReferrerAfterRedirect( |
| 9618 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9619 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9620 | |
| 9621 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9622 | } |
| 9623 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9624 | TEST_F(URLRequestTestReferrerPolicy, HTTPToCrossOriginHTTP) { |
| 9625 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 9626 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9627 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9628 | |
| 9629 | VerifyReferrerAfterRedirect( |
| 9630 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9631 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9632 | |
| 9633 | VerifyReferrerAfterRedirect( |
| 9634 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9635 | referrer, referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9636 | |
| 9637 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9638 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9639 | referrer.GetOrigin()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9640 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9641 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9642 | referrer); |
| 9643 | |
| 9644 | // The original referrer set on the request is expected to obey the referrer |
| 9645 | // policy and already be stripped to the origin; thus this test case just |
| 9646 | // checks that this policy doesn't cause the referrer to change when following |
| 9647 | // a redirect. |
| 9648 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9649 | referrer.GetOrigin()); |
| 9650 | |
| 9651 | VerifyReferrerAfterRedirect( |
| 9652 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9653 | |
| 9654 | // The original referrer set on the request is expected to obey the referrer |
| 9655 | // policy and already be stripped to the origin; thus this test case just |
| 9656 | // checks that this policy doesn't cause the referrer to change when following |
| 9657 | // a redirect. |
| 9658 | VerifyReferrerAfterRedirect( |
| 9659 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9660 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9661 | |
| 9662 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9663 | } |
| 9664 | |
| 9665 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToSameOriginHTTPS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9666 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9667 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9668 | |
| 9669 | VerifyReferrerAfterRedirect( |
| 9670 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9671 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9672 | |
| 9673 | VerifyReferrerAfterRedirect( |
| 9674 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9675 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9676 | |
| 9677 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9678 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9679 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9680 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9681 | referrer); |
| 9682 | |
| 9683 | // The original referrer set on the request is expected to obey the referrer |
| 9684 | // policy and already be stripped to the origin; thus this test case just |
| 9685 | // checks that this policy doesn't cause the referrer to change when following |
| 9686 | // a redirect. |
| 9687 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9688 | referrer.GetOrigin()); |
| 9689 | |
| 9690 | VerifyReferrerAfterRedirect( |
| 9691 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, |
| 9692 | referrer); |
| 9693 | |
| 9694 | // The original referrer set on the request is expected to obey the referrer |
| 9695 | // policy and already be stripped to the origin; thus this test case just |
| 9696 | // checks that this policy doesn't cause the referrer to change when following |
| 9697 | // a redirect. |
| 9698 | VerifyReferrerAfterRedirect( |
| 9699 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9700 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9701 | |
| 9702 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9703 | } |
| 9704 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9705 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToCrossOriginHTTPS) { |
| 9706 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 9707 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9708 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9709 | |
| 9710 | VerifyReferrerAfterRedirect( |
| 9711 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9712 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9713 | |
| 9714 | VerifyReferrerAfterRedirect( |
| 9715 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9716 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9717 | |
| 9718 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9719 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9720 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9721 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9722 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9723 | referrer); |
| 9724 | |
| 9725 | // The original referrer set on the request is expected to obey the referrer |
| 9726 | // policy and already be stripped to the origin; thus this test case just |
| 9727 | // checks that this policy doesn't cause the referrer to change when following |
| 9728 | // a redirect. |
| 9729 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9730 | referrer.GetOrigin()); |
| 9731 | |
| 9732 | VerifyReferrerAfterRedirect( |
| 9733 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9734 | |
| 9735 | // The original referrer set on the request is expected to obey the referrer |
| 9736 | // policy and already be stripped to the origin; thus this test case just |
| 9737 | // checks that this policy doesn't cause the referrer to change when following |
| 9738 | // a redirect. |
| 9739 | VerifyReferrerAfterRedirect( |
| 9740 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9741 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9742 | |
| 9743 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9744 | } |
| 9745 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9746 | TEST_F(URLRequestTestReferrerPolicy, HTTPToHTTPS) { |
| 9747 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 9748 | net::EmbeddedTestServer::TYPE_HTTPS); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9749 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9750 | |
| 9751 | VerifyReferrerAfterRedirect( |
| 9752 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9753 | referrer, referrer); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9754 | |
| 9755 | VerifyReferrerAfterRedirect( |
| 9756 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9757 | referrer, origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9758 | |
| 9759 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9760 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9761 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9762 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9763 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9764 | referrer); |
| 9765 | |
| 9766 | // The original referrer set on the request is expected to obey the referrer |
| 9767 | // policy and already be stripped to the origin; thus this test case just |
| 9768 | // checks that this policy doesn't cause the referrer to change when following |
| 9769 | // a redirect. |
| 9770 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9771 | referrer.GetOrigin()); |
| 9772 | |
| 9773 | VerifyReferrerAfterRedirect( |
| 9774 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9775 | |
| 9776 | // The original referrer set on the request is expected to obey the referrer |
| 9777 | // policy and already be stripped to the origin; thus this test case just |
| 9778 | // checks that this policy doesn't cause the referrer to change when following |
| 9779 | // a redirect. |
| 9780 | VerifyReferrerAfterRedirect( |
| 9781 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9782 | referrer.GetOrigin(), referrer.GetOrigin()); |
| 9783 | |
| 9784 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9785 | } |
| 9786 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9787 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToHTTP) { |
| 9788 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 9789 | net::EmbeddedTestServer::TYPE_HTTP); |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9790 | GURL referrer = origin_server()->GetURL("/path/to/file.html"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9791 | |
| 9792 | VerifyReferrerAfterRedirect( |
| 9793 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9794 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9795 | |
| 9796 | VerifyReferrerAfterRedirect( |
| 9797 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9798 | referrer, GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9799 | |
| 9800 | VerifyReferrerAfterRedirect( |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9801 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, referrer, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9802 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9803 | |
estark | c8ccba8 | 2017-06-13 22:37:40 | [diff] [blame] | 9804 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, referrer, |
| 9805 | referrer); |
| 9806 | |
| 9807 | // The original referrer set on the request is expected to obey the referrer |
| 9808 | // policy and already be stripped to the origin; thus this test case just |
| 9809 | // checks that this policy doesn't cause the referrer to change when following |
| 9810 | // a redirect. |
| 9811 | VerifyReferrerAfterRedirect(URLRequest::ORIGIN, referrer.GetOrigin(), |
| 9812 | referrer.GetOrigin()); |
| 9813 | |
| 9814 | VerifyReferrerAfterRedirect( |
| 9815 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_CROSS_ORIGIN, referrer, GURL()); |
| 9816 | |
| 9817 | // The original referrer set on the request is expected to obey the referrer |
| 9818 | // policy and already be stripped to the origin, though it should be |
| 9819 | // subsequently cleared during the downgrading redirect. |
| 9820 | VerifyReferrerAfterRedirect( |
| 9821 | URLRequest::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 9822 | referrer.GetOrigin(), GURL()); |
| 9823 | |
| 9824 | VerifyReferrerAfterRedirect(URLRequest::NO_REFERRER, GURL(), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 9825 | } |
| 9826 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 9827 | class HTTPSRequestTest : public TestWithScopedTaskEnvironment { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9828 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 9829 | HTTPSRequestTest() : default_context_(true) { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9830 | default_context_.set_network_delegate(&default_network_delegate_); |
| 9831 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9832 | } |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9833 | ~HTTPSRequestTest() override { |
| 9834 | SetTransportSecurityStateSourceForTesting(nullptr); |
| 9835 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 9836 | |
| 9837 | protected: |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9838 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 9839 | TestURLRequestContext default_context_; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9840 | }; |
| 9841 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 9842 | TEST_F(HTTPSRequestTest, HTTPSGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9843 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9844 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9845 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9846 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9847 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9848 | TestDelegate d; |
| 9849 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9850 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9851 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9852 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9853 | r->Start(); |
| 9854 | EXPECT_TRUE(r->is_pending()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9855 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9856 | d.RunUntilComplete(); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9857 | |
| 9858 | EXPECT_EQ(1, d.response_started_count()); |
| 9859 | EXPECT_FALSE(d.received_data_before_response()); |
| 9860 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9861 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 9862 | EXPECT_EQ(test_server.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9863 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 9864 | EXPECT_EQ(test_server.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9865 | r->GetSocketAddress().port()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9866 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 9867 | } |
| 9868 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 9869 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9870 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9871 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9872 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9873 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9874 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9875 | |
| 9876 | bool err_allowed = true; |
| 9877 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 9878 | TestDelegate d; |
| 9879 | { |
| 9880 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9881 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9882 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9883 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9884 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9885 | r->Start(); |
| 9886 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9887 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9888 | d.RunUntilComplete(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9889 | |
| 9890 | EXPECT_EQ(1, d.response_started_count()); |
| 9891 | EXPECT_FALSE(d.received_data_before_response()); |
| 9892 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9893 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9894 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9895 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9896 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9897 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9898 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9899 | } |
| 9900 | } |
| 9901 | } |
| 9902 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 9903 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9904 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9905 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED); |
| 9906 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9907 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9908 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9909 | |
| 9910 | // Iterate from false to true, just so that we do the opposite of the |
| 9911 | // previous test in order to increase test coverage. |
| 9912 | bool err_allowed = false; |
| 9913 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 9914 | TestDelegate d; |
| 9915 | { |
| 9916 | d.set_allow_certificate_errors(err_allowed); |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9917 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9918 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 9919 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9920 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9921 | r->Start(); |
| 9922 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9923 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9924 | d.RunUntilComplete(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9925 | |
| 9926 | EXPECT_EQ(1, d.response_started_count()); |
| 9927 | EXPECT_FALSE(d.received_data_before_response()); |
| 9928 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9929 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9930 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9931 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9932 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9933 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 9934 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 9935 | } |
| 9936 | } |
| 9937 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 9938 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9939 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 9940 | // security state. (see http://crbug.com/550977). |
| 9941 | #if !defined(OS_IOS) |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9942 | // This tests that a load of a domain with preloaded HSTS and HPKP with a |
| 9943 | // certificate error sets the |certificate_errors_are_fatal| flag correctly. |
| 9944 | // This flag will cause the interstitial to be fatal. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9945 | TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9946 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 9947 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9948 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9949 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9950 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9951 | ASSERT_TRUE(test_server.Start()); |
| 9952 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9953 | // We require that the URL be hsts-hpkp-preloaded.test. This is a test domain |
| 9954 | // that has a preloaded HSTS+HPKP entry in the TransportSecurityState. This |
| 9955 | // means that we have to use a MockHostResolver in order to direct |
| 9956 | // hsts-hpkp-preloaded.test to the testserver. By default, MockHostResolver |
| 9957 | // maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9958 | |
| 9959 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 9960 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9961 | TestURLRequestContext context(true); |
| 9962 | context.set_network_delegate(&network_delegate); |
| 9963 | context.set_host_resolver(&host_resolver); |
| 9964 | TransportSecurityState transport_security_state; |
| 9965 | context.set_transport_security_state(&transport_security_state); |
| 9966 | context.Init(); |
| 9967 | |
| 9968 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 9969 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9970 | GURL(base::StringPrintf("https://hsts-hpkp-preloaded.test:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9971 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 9972 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9973 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9974 | r->Start(); |
| 9975 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9976 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 9977 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9978 | |
| 9979 | EXPECT_EQ(1, d.response_started_count()); |
| 9980 | EXPECT_FALSE(d.received_data_before_response()); |
| 9981 | EXPECT_TRUE(d.have_certificate_errors()); |
| 9982 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 9983 | } |
| 9984 | |
| 9985 | // This tests that cached HTTPS page loads do not cause any updates to the |
| 9986 | // TransportSecurityState. |
| 9987 | TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9988 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 9989 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9990 | // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't |
| 9991 | // matter. It just has to be any error. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9992 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 9993 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 9994 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9995 | ASSERT_TRUE(test_server.Start()); |
| 9996 | |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 9997 | // We require that the URL be hsts-hpkp-preloaded.test. This is a test domain |
| 9998 | // that has a preloaded HSTS+HPKP entry in the TransportSecurityState. This |
| 9999 | // means that we have to use a MockHostResolver in order to direct |
| 10000 | // hsts-hpkp-preloaded.test to the testserver. By default, MockHostResolver |
| 10001 | // maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10002 | |
| 10003 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 10004 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10005 | TestURLRequestContext context(true); |
| 10006 | context.set_network_delegate(&network_delegate); |
| 10007 | context.set_host_resolver(&host_resolver); |
| 10008 | TransportSecurityState transport_security_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10009 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10010 | TransportSecurityState::STSState static_sts_state; |
| 10011 | TransportSecurityState::PKPState static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10012 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10013 | "hsts-hpkp-preloaded.test", &static_sts_state, &static_pkp_state)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10014 | context.set_transport_security_state(&transport_security_state); |
| 10015 | context.Init(); |
| 10016 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10017 | TransportSecurityState::STSState dynamic_sts_state; |
| 10018 | TransportSecurityState::PKPState dynamic_pkp_state; |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10019 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
| 10020 | "hsts-hpkp-preloaded.test", &dynamic_sts_state)); |
| 10021 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
| 10022 | "hsts-hpkp-preloaded.test", &dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10023 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10024 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10025 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10026 | GURL(base::StringPrintf("https://hsts-hpkp-preloaded.test:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 10027 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10028 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10029 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10030 | r->Start(); |
| 10031 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10032 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10033 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10034 | |
| 10035 | EXPECT_EQ(1, d.response_started_count()); |
| 10036 | EXPECT_FALSE(d.received_data_before_response()); |
| 10037 | EXPECT_TRUE(d.have_certificate_errors()); |
| 10038 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 10039 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10040 | // Get a fresh copy of the states, and check that they haven't changed. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10041 | TransportSecurityState::STSState new_static_sts_state; |
| 10042 | TransportSecurityState::PKPState new_static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10043 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10044 | "hsts-hpkp-preloaded.test", &new_static_sts_state, |
| 10045 | &new_static_pkp_state)); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10046 | TransportSecurityState::STSState new_dynamic_sts_state; |
| 10047 | TransportSecurityState::PKPState new_dynamic_pkp_state; |
| 10048 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10049 | "hsts-hpkp-preloaded.test", &new_dynamic_sts_state)); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10050 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 10051 | "hsts-hpkp-preloaded.test", &new_dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 10052 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 10053 | EXPECT_EQ(new_static_sts_state.upgrade_mode, static_sts_state.upgrade_mode); |
| 10054 | EXPECT_EQ(new_static_sts_state.include_subdomains, |
| 10055 | static_sts_state.include_subdomains); |
| 10056 | EXPECT_EQ(new_static_pkp_state.include_subdomains, |
| 10057 | static_pkp_state.include_subdomains); |
rsleevi | 91d4c9c | 2016-05-14 20:28:48 | [diff] [blame] | 10058 | EXPECT_EQ(new_static_pkp_state.spki_hashes, static_pkp_state.spki_hashes); |
| 10059 | EXPECT_EQ(new_static_pkp_state.bad_spki_hashes, |
| 10060 | static_pkp_state.bad_spki_hashes); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10061 | } |
| 10062 | |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10063 | // Make sure HSTS preserves a POST request's method and body. |
| 10064 | TEST_F(HTTPSRequestTest, HSTSPreservesPosts) { |
| 10065 | static const char kData[] = "hello world"; |
| 10066 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10067 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10068 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10069 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10070 | ASSERT_TRUE(test_server.Start()); |
| 10071 | |
| 10072 | |
| 10073 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 10074 | // address, so a MockHostResolver is needed to redirect www.somewhere.com to |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10075 | // the EmbeddedTestServer. By default, MockHostResolver maps all hosts |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10076 | // to 127.0.0.1. |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10077 | MockHostResolver host_resolver; |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10078 | |
| 10079 | // Force https for www.somewhere.com. |
| 10080 | TransportSecurityState transport_security_state; |
[email protected] | 474f079e | 2013-03-02 19:11:20 | [diff] [blame] | 10081 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 10082 | bool include_subdomains = false; |
| 10083 | transport_security_state.AddHSTS("www.somewhere.com", expiry, |
| 10084 | include_subdomains); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10085 | |
| 10086 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 10087 | |
| 10088 | TestURLRequestContext context(true); |
| 10089 | context.set_host_resolver(&host_resolver); |
| 10090 | context.set_transport_security_state(&transport_security_state); |
| 10091 | context.set_network_delegate(&network_delegate); |
| 10092 | context.Init(); |
| 10093 | |
| 10094 | TestDelegate d; |
| 10095 | // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will |
| 10096 | // cause a certificate error. Ignore the error. |
| 10097 | d.set_allow_certificate_errors(true); |
| 10098 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10099 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10100 | GURL(base::StringPrintf("http://www.somewhere.com:%d/echo", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 10101 | test_server.host_port_pair().port())), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10102 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10103 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 10104 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10105 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10106 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10107 | d.RunUntilComplete(); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10108 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10109 | EXPECT_EQ("https", req->url().scheme()); |
| 10110 | EXPECT_EQ("POST", req->method()); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10111 | EXPECT_EQ(kData, d.data_received()); |
[email protected] | 07d9bfd | 2013-06-27 14:16:40 | [diff] [blame] | 10112 | |
| 10113 | LoadTimingInfo load_timing_info; |
| 10114 | network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info); |
| 10115 | // LoadTimingInfo of HSTS redirects is similar to that of network cache hits |
| 10116 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 10117 | } |
| 10118 | |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10119 | // Make sure that the CORS headers are added to cross-origin HSTS redirects. |
| 10120 | TEST_F(HTTPSRequestTest, HSTSCrossOriginAddHeaders) { |
| 10121 | static const char kOriginHeaderValue[] = "http://www.example.com"; |
| 10122 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10123 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10124 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10125 | ASSERT_TRUE(test_server.Start()); |
| 10126 | |
| 10127 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 10128 | // address, so a MockHostResolver is needed to redirect example.net to the |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10129 | // EmbeddedTestServer. MockHostResolver maps all hosts to 127.0.0.1 by |
| 10130 | // default. |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10131 | MockHostResolver host_resolver; |
| 10132 | |
| 10133 | TransportSecurityState transport_security_state; |
| 10134 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 10135 | bool include_subdomains = false; |
| 10136 | transport_security_state.AddHSTS("example.net", expiry, include_subdomains); |
| 10137 | |
| 10138 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 10139 | |
| 10140 | MockCertVerifier cert_verifier; |
| 10141 | cert_verifier.set_default_result(OK); |
| 10142 | |
| 10143 | TestURLRequestContext context(true); |
| 10144 | context.set_host_resolver(&host_resolver); |
| 10145 | context.set_transport_security_state(&transport_security_state); |
| 10146 | context.set_network_delegate(&network_delegate); |
| 10147 | context.set_cert_verifier(&cert_verifier); |
| 10148 | context.Init(); |
| 10149 | |
| 10150 | GURL hsts_http_url(base::StringPrintf("http://example.net:%d/somehstssite", |
| 10151 | test_server.host_port_pair().port())); |
| 10152 | url::Replacements<char> replacements; |
| 10153 | const char kNewScheme[] = "https"; |
| 10154 | replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme))); |
| 10155 | GURL hsts_https_url = hsts_http_url.ReplaceComponents(replacements); |
| 10156 | |
| 10157 | TestDelegate d; |
| 10158 | // Quit on redirect to allow response header inspection upon redirect. |
| 10159 | d.set_quit_on_redirect(true); |
| 10160 | |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10161 | std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 10162 | hsts_http_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10163 | // Set Origin header to simulate a cross-origin request. |
| 10164 | HttpRequestHeaders request_headers; |
| 10165 | request_headers.SetHeader("Origin", kOriginHeaderValue); |
| 10166 | req->SetExtraRequestHeaders(request_headers); |
| 10167 | |
| 10168 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10169 | d.RunUntilComplete(); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 10170 | |
| 10171 | EXPECT_EQ(1, d.received_redirect_count()); |
| 10172 | |
| 10173 | const HttpResponseHeaders* headers = req->response_headers(); |
| 10174 | std::string redirect_location; |
| 10175 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Location", &redirect_location)); |
| 10176 | EXPECT_EQ(hsts_https_url.spec(), redirect_location); |
| 10177 | |
| 10178 | std::string received_cors_header; |
| 10179 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Access-Control-Allow-Origin", |
| 10180 | &received_cors_header)); |
| 10181 | EXPECT_EQ(kOriginHeaderValue, received_cors_header); |
| 10182 | } |
| 10183 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10184 | namespace { |
| 10185 | |
| 10186 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 10187 | public: |
| 10188 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
| 10189 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 10190 | void OnCertificateRequested(URLRequest* request, |
| 10191 | SSLCertRequestInfo* cert_request_info) override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10192 | on_certificate_requested_count_++; |
Gabriel Charette | 53a9ef81 | 2017-07-26 12:36:23 | [diff] [blame] | 10193 | base::RunLoop::QuitCurrentWhenIdleDeprecated(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10194 | } |
| 10195 | int on_certificate_requested_count() { |
| 10196 | return on_certificate_requested_count_; |
| 10197 | } |
| 10198 | private: |
| 10199 | int on_certificate_requested_count_; |
| 10200 | }; |
| 10201 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10202 | class TestSSLPrivateKey : public SSLPrivateKey { |
| 10203 | public: |
| 10204 | explicit TestSSLPrivateKey(scoped_refptr<SSLPrivateKey> key) |
| 10205 | : key_(std::move(key)) {} |
| 10206 | |
| 10207 | void set_fail_signing(bool fail_signing) { fail_signing_ = fail_signing; } |
| 10208 | int sign_count() const { return sign_count_; } |
| 10209 | |
| 10210 | std::vector<uint16_t> GetAlgorithmPreferences() override { |
| 10211 | return key_->GetAlgorithmPreferences(); |
| 10212 | } |
| 10213 | void Sign(uint16_t algorithm, |
| 10214 | base::span<const uint8_t> input, |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 10215 | SignCallback callback) override { |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10216 | sign_count_++; |
| 10217 | if (fail_signing_) { |
| 10218 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 10219 | FROM_HERE, base::BindOnce(std::move(callback), |
| 10220 | ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED, |
| 10221 | std::vector<uint8_t>())); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10222 | } else { |
David Benjamin | 8f2d2c1 | 2018-02-27 00:08:26 | [diff] [blame] | 10223 | key_->Sign(algorithm, input, std::move(callback)); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10224 | } |
| 10225 | } |
| 10226 | |
| 10227 | private: |
| 10228 | ~TestSSLPrivateKey() override = default; |
| 10229 | |
| 10230 | scoped_refptr<SSLPrivateKey> key_; |
| 10231 | bool fail_signing_ = false; |
| 10232 | int sign_count_ = 0; |
| 10233 | }; |
| 10234 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10235 | } // namespace |
| 10236 | |
| 10237 | // TODO(davidben): Test the rest of the code. Specifically, |
| 10238 | // - Filtering which certificates to select. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10239 | // - Getting a certificate request in an SSL renegotiation sending the |
| 10240 | // HTTP request. |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10241 | TEST_F(HTTPSRequestTest, ClientAuthNoCertificate) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10242 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10243 | net::SSLServerConfig ssl_config; |
ryanchung | 987b2ff | 2016-02-19 00:17:12 | [diff] [blame] | 10244 | ssl_config.client_cert_type = |
| 10245 | SSLServerConfig::ClientCertType::OPTIONAL_CLIENT_CERT; |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10246 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10247 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10248 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10249 | ASSERT_TRUE(test_server.Start()); |
| 10250 | |
| 10251 | SSLClientAuthTestDelegate d; |
| 10252 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10253 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10254 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10255 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10256 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10257 | r->Start(); |
| 10258 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10259 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10260 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10261 | |
| 10262 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10263 | EXPECT_FALSE(d.received_data_before_response()); |
| 10264 | EXPECT_EQ(0, d.bytes_received()); |
| 10265 | |
| 10266 | // Send no certificate. |
| 10267 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 10268 | // all platforms so we can test sending a cert as well. |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 10269 | r->ContinueWithCertificate(nullptr, nullptr); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10270 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10271 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10272 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10273 | EXPECT_EQ(OK, d.request_status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10274 | EXPECT_EQ(1, d.response_started_count()); |
| 10275 | EXPECT_FALSE(d.received_data_before_response()); |
| 10276 | EXPECT_NE(0, d.bytes_received()); |
| 10277 | } |
| 10278 | } |
| 10279 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10280 | TEST_F(HTTPSRequestTest, ClientAuth) { |
| 10281 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 10282 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 10283 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 10284 | ASSERT_TRUE(identity); |
| 10285 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 10286 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 10287 | |
| 10288 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10289 | net::SSLServerConfig ssl_config; |
| 10290 | ssl_config.client_cert_type = |
| 10291 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 10292 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 10293 | test_server.AddDefaultHandlers( |
| 10294 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10295 | ASSERT_TRUE(test_server.Start()); |
| 10296 | |
| 10297 | { |
| 10298 | SSLClientAuthTestDelegate d; |
| 10299 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10300 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10301 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10302 | |
| 10303 | r->Start(); |
| 10304 | EXPECT_TRUE(r->is_pending()); |
| 10305 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10306 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10307 | |
| 10308 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10309 | EXPECT_FALSE(d.received_data_before_response()); |
| 10310 | EXPECT_EQ(0, d.bytes_received()); |
| 10311 | |
| 10312 | // Send a certificate. |
| 10313 | r->ContinueWithCertificate(identity->certificate(), private_key); |
| 10314 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10315 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10316 | |
| 10317 | EXPECT_EQ(OK, d.request_status()); |
| 10318 | EXPECT_EQ(1, d.response_started_count()); |
| 10319 | EXPECT_FALSE(d.received_data_before_response()); |
| 10320 | EXPECT_NE(0, d.bytes_received()); |
| 10321 | |
| 10322 | // The private key should have been used. |
| 10323 | EXPECT_EQ(1, private_key->sign_count()); |
| 10324 | } |
| 10325 | |
| 10326 | // Close all connections and clear the session cache to force a new handshake. |
| 10327 | default_context_.http_transaction_factory() |
| 10328 | ->GetSession() |
| 10329 | ->CloseAllConnections(); |
| 10330 | SSLClientSocket::ClearSessionCache(); |
| 10331 | |
| 10332 | // Connecting again should not call OnCertificateRequested. The identity is |
| 10333 | // taken from the client auth cache. |
| 10334 | { |
| 10335 | SSLClientAuthTestDelegate d; |
| 10336 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10337 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10338 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10339 | |
| 10340 | r->Start(); |
| 10341 | EXPECT_TRUE(r->is_pending()); |
| 10342 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10343 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10344 | |
| 10345 | EXPECT_EQ(OK, d.request_status()); |
| 10346 | EXPECT_EQ(0, d.on_certificate_requested_count()); |
| 10347 | EXPECT_FALSE(d.received_data_before_response()); |
| 10348 | EXPECT_EQ(1, d.response_started_count()); |
| 10349 | EXPECT_FALSE(d.received_data_before_response()); |
| 10350 | EXPECT_NE(0, d.bytes_received()); |
| 10351 | |
| 10352 | // The private key should have been used. |
| 10353 | EXPECT_EQ(2, private_key->sign_count()); |
| 10354 | } |
| 10355 | } |
| 10356 | |
| 10357 | // Test that private keys that fail to sign anything get evicted from the cache. |
| 10358 | TEST_F(HTTPSRequestTest, ClientAuthFailSigning) { |
| 10359 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 10360 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 10361 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 10362 | ASSERT_TRUE(identity); |
| 10363 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 10364 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 10365 | private_key->set_fail_signing(true); |
| 10366 | |
| 10367 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10368 | net::SSLServerConfig ssl_config; |
| 10369 | ssl_config.client_cert_type = |
| 10370 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 10371 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 10372 | test_server.AddDefaultHandlers( |
| 10373 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10374 | ASSERT_TRUE(test_server.Start()); |
| 10375 | |
| 10376 | { |
| 10377 | SSLClientAuthTestDelegate d; |
| 10378 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10379 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10380 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10381 | |
| 10382 | r->Start(); |
| 10383 | EXPECT_TRUE(r->is_pending()); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10384 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10385 | |
| 10386 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10387 | EXPECT_FALSE(d.received_data_before_response()); |
| 10388 | EXPECT_EQ(0, d.bytes_received()); |
| 10389 | |
| 10390 | // Send a certificate. |
| 10391 | r->ContinueWithCertificate(identity->certificate(), private_key); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10392 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10393 | |
| 10394 | // The private key cannot sign anything, so we report an error. |
| 10395 | EXPECT_EQ(ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED, d.request_status()); |
| 10396 | EXPECT_EQ(1, d.response_started_count()); |
| 10397 | EXPECT_FALSE(d.received_data_before_response()); |
| 10398 | EXPECT_EQ(0, d.bytes_received()); |
| 10399 | |
| 10400 | // The private key should have been used. |
| 10401 | EXPECT_EQ(1, private_key->sign_count()); |
| 10402 | } |
| 10403 | |
| 10404 | // Close all connections and clear the session cache to force a new handshake. |
| 10405 | default_context_.http_transaction_factory() |
| 10406 | ->GetSession() |
| 10407 | ->CloseAllConnections(); |
| 10408 | SSLClientSocket::ClearSessionCache(); |
| 10409 | |
| 10410 | // The bad identity should have been evicted from the cache, so connecting |
| 10411 | // again should call OnCertificateRequested again. |
| 10412 | { |
| 10413 | SSLClientAuthTestDelegate d; |
| 10414 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10415 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10416 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10417 | |
| 10418 | r->Start(); |
| 10419 | EXPECT_TRUE(r->is_pending()); |
| 10420 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10421 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10422 | |
| 10423 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10424 | EXPECT_FALSE(d.received_data_before_response()); |
| 10425 | EXPECT_EQ(0, d.bytes_received()); |
| 10426 | |
| 10427 | // There should have been no additional uses of the private key. |
| 10428 | EXPECT_EQ(1, private_key->sign_count()); |
| 10429 | } |
| 10430 | } |
| 10431 | |
| 10432 | // Test that cached private keys that fail to sign anything trigger a |
| 10433 | // retry. This is so we handle unplugged smartcards |
| 10434 | // gracefully. https://crbug.com/813022. |
| 10435 | TEST_F(HTTPSRequestTest, ClientAuthFailSigningRetry) { |
| 10436 | std::unique_ptr<FakeClientCertIdentity> identity = |
| 10437 | FakeClientCertIdentity::CreateFromCertAndKeyFiles( |
| 10438 | GetTestCertsDirectory(), "client_1.pem", "client_1.pk8"); |
| 10439 | ASSERT_TRUE(identity); |
| 10440 | scoped_refptr<TestSSLPrivateKey> private_key = |
| 10441 | base::MakeRefCounted<TestSSLPrivateKey>(identity->ssl_private_key()); |
| 10442 | |
| 10443 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 10444 | net::SSLServerConfig ssl_config; |
| 10445 | ssl_config.client_cert_type = |
| 10446 | SSLServerConfig::ClientCertType::REQUIRE_CLIENT_CERT; |
| 10447 | test_server.SetSSLConfig(EmbeddedTestServer::CERT_OK, ssl_config); |
| 10448 | test_server.AddDefaultHandlers( |
| 10449 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10450 | ASSERT_TRUE(test_server.Start()); |
| 10451 | |
| 10452 | // Connect with a client certificate to put it in the client auth cache. |
| 10453 | { |
| 10454 | SSLClientAuthTestDelegate d; |
| 10455 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10456 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10457 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10458 | |
| 10459 | r->Start(); |
| 10460 | EXPECT_TRUE(r->is_pending()); |
| 10461 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10462 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10463 | |
| 10464 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10465 | EXPECT_FALSE(d.received_data_before_response()); |
| 10466 | EXPECT_EQ(0, d.bytes_received()); |
| 10467 | |
| 10468 | r->ContinueWithCertificate(identity->certificate(), private_key); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10469 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10470 | |
| 10471 | EXPECT_EQ(OK, d.request_status()); |
| 10472 | EXPECT_EQ(1, d.response_started_count()); |
| 10473 | EXPECT_FALSE(d.received_data_before_response()); |
| 10474 | EXPECT_NE(0, d.bytes_received()); |
| 10475 | |
| 10476 | // The private key should have been used. |
| 10477 | EXPECT_EQ(1, private_key->sign_count()); |
| 10478 | } |
| 10479 | |
| 10480 | // Close all connections and clear the session cache to force a new handshake. |
| 10481 | default_context_.http_transaction_factory() |
| 10482 | ->GetSession() |
| 10483 | ->CloseAllConnections(); |
| 10484 | SSLClientSocket::ClearSessionCache(); |
| 10485 | |
| 10486 | // Cause the private key to fail. Connecting again should attempt to use it, |
| 10487 | // notice the failure, and then request a new identity via |
| 10488 | // OnCertificateRequested. |
| 10489 | private_key->set_fail_signing(true); |
| 10490 | |
| 10491 | { |
| 10492 | SSLClientAuthTestDelegate d; |
| 10493 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 10494 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 10495 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 10496 | |
| 10497 | r->Start(); |
| 10498 | EXPECT_TRUE(r->is_pending()); |
| 10499 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10500 | d.RunUntilComplete(); |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 10501 | |
| 10502 | // There was an additional signing call on the private key (the one which |
| 10503 | // failed). |
| 10504 | EXPECT_EQ(2, private_key->sign_count()); |
| 10505 | |
| 10506 | // That caused another OnCertificateRequested call. |
| 10507 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 10508 | EXPECT_FALSE(d.received_data_before_response()); |
| 10509 | EXPECT_EQ(0, d.bytes_received()); |
| 10510 | } |
| 10511 | } |
| 10512 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10513 | TEST_F(HTTPSRequestTest, ResumeTest) { |
| 10514 | // Test that we attempt a session resume when making two connections to the |
| 10515 | // same host. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10516 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10517 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10518 | SpawnedTestServer test_server( |
| 10519 | SpawnedTestServer::TYPE_HTTPS, |
| 10520 | ssl_options, |
| 10521 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10522 | ASSERT_TRUE(test_server.Start()); |
| 10523 | |
| 10524 | SSLClientSocket::ClearSessionCache(); |
| 10525 | |
| 10526 | { |
| 10527 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10528 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10529 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10530 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10531 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10532 | r->Start(); |
| 10533 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10534 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10535 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10536 | |
| 10537 | EXPECT_EQ(1, d.response_started_count()); |
| 10538 | } |
| 10539 | |
| 10540 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 10541 | CloseAllConnections(); |
| 10542 | |
| 10543 | { |
| 10544 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10545 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10546 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10547 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10548 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10549 | r->Start(); |
| 10550 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10551 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10552 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10553 | |
| 10554 | // The response will look like; |
| 10555 | // insert abc |
| 10556 | // lookup abc |
| 10557 | // insert xyz |
| 10558 | // |
| 10559 | // With a newline at the end which makes the split think that there are |
| 10560 | // four lines. |
| 10561 | |
| 10562 | EXPECT_EQ(1, d.response_started_count()); |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 10563 | std::vector<std::string> lines = base::SplitString( |
| 10564 | d.data_received(), "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10565 | ASSERT_EQ(4u, lines.size()) << d.data_received(); |
| 10566 | |
| 10567 | std::string session_id; |
| 10568 | |
| 10569 | for (size_t i = 0; i < 2; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 10570 | std::vector<std::string> parts = base::SplitString( |
| 10571 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10572 | ASSERT_EQ(2u, parts.size()); |
| 10573 | if (i == 0) { |
| 10574 | EXPECT_EQ("insert", parts[0]); |
| 10575 | session_id = parts[1]; |
| 10576 | } else { |
| 10577 | EXPECT_EQ("lookup", parts[0]); |
| 10578 | EXPECT_EQ(session_id, parts[1]); |
| 10579 | } |
| 10580 | } |
| 10581 | } |
| 10582 | } |
| 10583 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10584 | // AssertTwoDistinctSessionsInserted checks that |session_info|, which must be |
| 10585 | // the result of fetching "ssl-session-cache" from the test server, indicates |
| 10586 | // that exactly two different sessions were inserted, with no lookups etc. |
| 10587 | static void AssertTwoDistinctSessionsInserted(const string& session_info) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 10588 | std::vector<std::string> lines = base::SplitString( |
| 10589 | session_info, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10590 | ASSERT_EQ(3u, lines.size()) << session_info; |
| 10591 | |
| 10592 | std::string session_id; |
| 10593 | for (size_t i = 0; i < 2; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 10594 | std::vector<std::string> parts = base::SplitString( |
| 10595 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10596 | ASSERT_EQ(2u, parts.size()); |
| 10597 | EXPECT_EQ("insert", parts[0]); |
| 10598 | if (i == 0) { |
| 10599 | session_id = parts[1]; |
| 10600 | } else { |
| 10601 | EXPECT_NE(session_id, parts[1]); |
| 10602 | } |
| 10603 | } |
| 10604 | } |
| 10605 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10606 | TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { |
| 10607 | // Test that sessions aren't resumed when the value of ssl_session_cache_shard |
| 10608 | // differs. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10609 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10610 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10611 | SpawnedTestServer test_server( |
| 10612 | SpawnedTestServer::TYPE_HTTPS, |
| 10613 | ssl_options, |
| 10614 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10615 | ASSERT_TRUE(test_server.Start()); |
| 10616 | |
| 10617 | SSLClientSocket::ClearSessionCache(); |
| 10618 | |
| 10619 | { |
| 10620 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10621 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10622 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10623 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10624 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10625 | r->Start(); |
| 10626 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10627 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10628 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10629 | |
| 10630 | EXPECT_EQ(1, d.response_started_count()); |
| 10631 | } |
| 10632 | |
| 10633 | // Now create a new HttpCache with a different ssl_session_cache_shard value. |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10634 | HttpNetworkSession::Context session_context; |
| 10635 | session_context.host_resolver = default_context_.host_resolver(); |
| 10636 | session_context.cert_verifier = default_context_.cert_verifier(); |
| 10637 | session_context.transport_security_state = |
| 10638 | default_context_.transport_security_state(); |
| 10639 | session_context.cert_transparency_verifier = |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10640 | default_context_.cert_transparency_verifier(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10641 | session_context.ct_policy_enforcer = default_context_.ct_policy_enforcer(); |
Lily Houghton | 8c2f97d | 2018-01-22 05:06:59 | [diff] [blame] | 10642 | session_context.proxy_resolution_service = default_context_.proxy_resolution_service(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10643 | session_context.ssl_config_service = default_context_.ssl_config_service(); |
| 10644 | session_context.http_auth_handler_factory = |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10645 | default_context_.http_auth_handler_factory(); |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10646 | session_context.http_server_properties = |
| 10647 | default_context_.http_server_properties(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10648 | |
mmenke | 6ddfbea | 2017-05-31 21:48:41 | [diff] [blame] | 10649 | HttpNetworkSession network_session(HttpNetworkSession::Params(), |
| 10650 | session_context); |
zhongyi | 4928bd5 | 2017-02-08 02:16:27 | [diff] [blame] | 10651 | std::unique_ptr<HttpCache> cache( |
| 10652 | new HttpCache(&network_session, HttpCache::DefaultBackend::InMemory(0), |
| 10653 | false /* is_main_cache */)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10654 | |
| 10655 | default_context_.set_http_transaction_factory(cache.get()); |
| 10656 | |
| 10657 | { |
| 10658 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10659 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10660 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10661 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10662 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10663 | r->Start(); |
| 10664 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10665 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10666 | d.RunUntilComplete(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10667 | |
| 10668 | // The response will look like; |
| 10669 | // insert abc |
| 10670 | // insert xyz |
| 10671 | // |
| 10672 | // With a newline at the end which makes the split think that there are |
| 10673 | // three lines. |
| 10674 | |
| 10675 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10676 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 10677 | } |
| 10678 | } |
| 10679 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 10680 | class HTTPSFallbackTest : public TestWithScopedTaskEnvironment { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10681 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 10682 | HTTPSFallbackTest() : context_(true) { |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10683 | ssl_config_service_ = new TestSSLConfigService( |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 10684 | false /* online revocation checking */, |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10685 | false /* require rev. checking for local anchors */, |
| 10686 | false /* token binding enabled */); |
| 10687 | context_.set_ssl_config_service(ssl_config_service_.get()); |
| 10688 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 10689 | ~HTTPSFallbackTest() override = default; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10690 | |
| 10691 | protected: |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10692 | TestSSLConfigService* ssl_config_service() { |
| 10693 | return ssl_config_service_.get(); |
| 10694 | } |
| 10695 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10696 | void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) { |
| 10697 | DCHECK(!request_); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10698 | context_.Init(); |
| 10699 | delegate_.set_allow_certificate_errors(true); |
| 10700 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10701 | SpawnedTestServer test_server( |
| 10702 | SpawnedTestServer::TYPE_HTTPS, |
| 10703 | ssl_options, |
| 10704 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10705 | ASSERT_TRUE(test_server.Start()); |
| 10706 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 10707 | request_ = context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10708 | &delegate_, TRAFFIC_ANNOTATION_FOR_TESTS); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10709 | request_->Start(); |
| 10710 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10711 | delegate_.RunUntilComplete(); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10712 | } |
| 10713 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10714 | void ExpectConnection(int version) { |
| 10715 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 10716 | EXPECT_NE(0, delegate_.bytes_received()); |
| 10717 | EXPECT_EQ(version, SSLConnectionStatusToVersion( |
| 10718 | request_->ssl_info().connection_status)); |
| 10719 | } |
| 10720 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10721 | void ExpectFailure(int error) { |
| 10722 | EXPECT_EQ(1, delegate_.response_started_count()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 10723 | EXPECT_EQ(error, delegate_.request_status()); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10724 | } |
| 10725 | |
| 10726 | private: |
| 10727 | TestDelegate delegate_; |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 10728 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10729 | std::unique_ptr<URLRequest> request_; |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10730 | scoped_refptr<TestSSLConfigService> ssl_config_service_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10731 | }; |
| 10732 | |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10733 | // Tests the TLS 1.0 fallback doesn't happen. |
| 10734 | TEST_F(HTTPSFallbackTest, TLSv1NoFallback) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10735 | SpawnedTestServer::SSLOptions ssl_options( |
| 10736 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10737 | ssl_options.tls_intolerant = |
| 10738 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 10739 | |
| 10740 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 10741 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10742 | } |
| 10743 | |
davidben | 5a31215 | 2016-06-27 22:11:47 | [diff] [blame] | 10744 | // Tests the TLS 1.1 fallback doesn't happen. |
davidben | 3b26751 | 2016-02-24 19:46:55 | [diff] [blame] | 10745 | TEST_F(HTTPSFallbackTest, TLSv1_1NoFallback) { |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 10746 | SpawnedTestServer::SSLOptions ssl_options( |
| 10747 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10748 | ssl_options.tls_intolerant = |
| 10749 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2; |
| 10750 | |
| 10751 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 10752 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
| 10753 | } |
| 10754 | |
davidben | 095ebb5 | 2017-04-12 22:23:34 | [diff] [blame] | 10755 | // Tests that TLS 1.3 interference results in a dedicated error code. |
| 10756 | TEST_F(HTTPSFallbackTest, TLSv1_3Interference) { |
| 10757 | SpawnedTestServer::SSLOptions ssl_options( |
| 10758 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10759 | ssl_options.tls_intolerant = |
| 10760 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_3; |
| 10761 | ssl_config_service()->set_max_version(SSL_PROTOCOL_VERSION_TLS1_3); |
| 10762 | |
| 10763 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 10764 | ExpectFailure(ERR_SSL_VERSION_INTERFERENCE); |
| 10765 | } |
| 10766 | |
| 10767 | // Tests that disabling TLS 1.3 leaves TLS 1.3 interference unnoticed. |
| 10768 | TEST_F(HTTPSFallbackTest, TLSv1_3InterferenceDisableVersion) { |
| 10769 | SpawnedTestServer::SSLOptions ssl_options( |
| 10770 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 10771 | ssl_options.tls_intolerant = |
| 10772 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_3; |
| 10773 | ssl_config_service()->set_max_version(SSL_PROTOCOL_VERSION_TLS1_2); |
| 10774 | |
| 10775 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 10776 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1_2); |
| 10777 | } |
| 10778 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 10779 | class HTTPSSessionTest : public TestWithScopedTaskEnvironment { |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10780 | public: |
fdoray | f33fede | 2017-05-11 21:18:10 | [diff] [blame] | 10781 | HTTPSSessionTest() : default_context_(true) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10782 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10783 | |
| 10784 | default_context_.set_network_delegate(&default_network_delegate_); |
| 10785 | default_context_.set_cert_verifier(&cert_verifier_); |
| 10786 | default_context_.Init(); |
| 10787 | } |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 10788 | ~HTTPSSessionTest() override = default; |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10789 | |
| 10790 | protected: |
| 10791 | MockCertVerifier cert_verifier_; |
| 10792 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 10793 | TestURLRequestContext default_context_; |
| 10794 | }; |
| 10795 | |
| 10796 | // Tests that session resumption is not attempted if an invalid certificate |
| 10797 | // is presented. |
| 10798 | TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) { |
| 10799 | SpawnedTestServer::SSLOptions ssl_options; |
| 10800 | ssl_options.record_resume = true; |
| 10801 | SpawnedTestServer test_server( |
| 10802 | SpawnedTestServer::TYPE_HTTPS, |
| 10803 | ssl_options, |
| 10804 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 10805 | ASSERT_TRUE(test_server.Start()); |
| 10806 | |
| 10807 | SSLClientSocket::ClearSessionCache(); |
| 10808 | |
| 10809 | // Simulate the certificate being expired and attempt a connection. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10810 | cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10811 | { |
| 10812 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10813 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10814 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10815 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10816 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10817 | r->Start(); |
| 10818 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10819 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10820 | d.RunUntilComplete(); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10821 | |
| 10822 | EXPECT_EQ(1, d.response_started_count()); |
| 10823 | } |
| 10824 | |
| 10825 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 10826 | CloseAllConnections(); |
| 10827 | |
| 10828 | // Now change the certificate to be acceptable (so that the response is |
| 10829 | // loaded), and ensure that no session id is presented to the peer. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10830 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10831 | { |
| 10832 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10833 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10834 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, |
| 10835 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10836 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10837 | r->Start(); |
| 10838 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10839 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10840 | d.RunUntilComplete(); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10841 | |
| 10842 | // The response will look like; |
| 10843 | // insert abc |
| 10844 | // insert xyz |
| 10845 | // |
| 10846 | // With a newline at the end which makes the split think that there are |
| 10847 | // three lines. |
| 10848 | // |
| 10849 | // If a session was presented (eg: a bug), then the response would look |
| 10850 | // like; |
| 10851 | // insert abc |
| 10852 | // lookup abc |
| 10853 | // insert xyz |
| 10854 | |
| 10855 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 10856 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 10857 | } |
| 10858 | } |
| 10859 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10860 | // This the fingerprint of the "Testing CA" certificate used by the testserver. |
| 10861 | // See net/data/ssl/certificates/ocsp-test-root.pem. |
David Benjamin | 9cedc3a5 | 2017-08-20 21:30:58 | [diff] [blame] | 10862 | static const SHA256HashValue kOCSPTestCertFingerprint = {{ |
| 10863 | 0x0c, 0xa9, 0x05, 0x11, 0xb0, 0xa2, 0xc0, 0x1d, 0x40, 0x6a, 0x99, |
| 10864 | 0x04, 0x21, 0x36, 0x45, 0x3f, 0x59, 0x12, 0x5c, 0x80, 0x64, 0x2d, |
| 10865 | 0x46, 0x6a, 0x3b, 0x78, 0x9e, 0x84, 0xea, 0x54, 0x0f, 0x8b, |
mattm | 3b4376f1 | 2016-10-03 21:07:15 | [diff] [blame] | 10866 | }}; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10867 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 10868 | // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the |
| 10869 | // testserver. |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 10870 | static const SHA256HashValue kOCSPTestCertSPKI = {{ |
| 10871 | 0x05, 0xa8, 0xf6, 0xfd, 0x8e, 0x10, 0xfe, 0x92, 0x2f, 0x22, 0x75, |
| 10872 | 0x46, 0x40, 0xf4, 0xc4, 0x57, 0x06, 0x0d, 0x95, 0xfd, 0x60, 0x31, |
| 10873 | 0x3b, 0xf3, 0xfc, 0x12, 0x47, 0xe7, 0x66, 0x1a, 0x82, 0xa3, |
| 10874 | }}; |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 10875 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10876 | // This is the policy OID contained in the certificates that testserver |
| 10877 | // generates. |
| 10878 | static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1"; |
| 10879 | |
| 10880 | class HTTPSOCSPTest : public HTTPSRequestTest { |
| 10881 | public: |
| 10882 | HTTPSOCSPTest() |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10883 | : context_(true), |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 10884 | ev_test_policy_( |
| 10885 | new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(), |
| 10886 | kOCSPTestCertFingerprint, |
| 10887 | kOCSPTestCertPolicy)) { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10888 | } |
| 10889 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 10890 | void SetUp() override { |
Ryan Sleevi | 8a9c9c1 | 2018-05-09 02:36:23 | [diff] [blame] | 10891 | context_.SetCTPolicyEnforcer(std::make_unique<DefaultCTPolicyEnforcer>()); |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10892 | SetupContext(); |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10893 | context_.Init(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10894 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 10895 | scoped_refptr<X509Certificate> root_cert = |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10896 | ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
dcheng | c2e01e8 | 2014-08-27 00:24:42 | [diff] [blame] | 10897 | CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert.get()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 10898 | test_root_.reset(new ScopedTestRoot(root_cert.get())); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10899 | |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 10900 | #if defined(OS_ANDROID) || defined(USE_BUILTIN_CERT_VERIFIER) |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 10901 | SetGlobalCertNetFetcherForTesting(net::CreateCertNetFetcher(&context_)); |
| 10902 | #endif |
| 10903 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 10904 | #if defined(USE_NSS_CERTS) |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10905 | SetURLRequestContextForNSSHttpIO(&context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10906 | #endif |
| 10907 | } |
| 10908 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10909 | void DoConnectionWithDelegate( |
| 10910 | const SpawnedTestServer::SSLOptions& ssl_options, |
| 10911 | TestDelegate* delegate, |
| 10912 | SSLInfo* out_ssl_info) { |
| 10913 | // Always overwrite |out_ssl_info|. |
| 10914 | out_ssl_info->Reset(); |
| 10915 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 10916 | SpawnedTestServer test_server( |
| 10917 | SpawnedTestServer::TYPE_HTTPS, |
| 10918 | ssl_options, |
| 10919 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10920 | ASSERT_TRUE(test_server.Start()); |
| 10921 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10922 | delegate->set_allow_certificate_errors(true); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 10923 | std::unique_ptr<URLRequest> r( |
| 10924 | context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
| 10925 | delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 10926 | r->Start(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10927 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 10928 | delegate->RunUntilComplete(); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10929 | EXPECT_EQ(1, delegate->response_started_count()); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10930 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 10931 | *out_ssl_info = r->ssl_info(); |
| 10932 | } |
| 10933 | |
| 10934 | void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options, |
| 10935 | CertStatus* out_cert_status) { |
| 10936 | // Always overwrite |out_cert_status|. |
| 10937 | *out_cert_status = 0; |
| 10938 | |
| 10939 | TestDelegate d; |
| 10940 | SSLInfo ssl_info; |
| 10941 | ASSERT_NO_FATAL_FAILURE( |
| 10942 | DoConnectionWithDelegate(ssl_options, &d, &ssl_info)); |
| 10943 | |
| 10944 | *out_cert_status = ssl_info.cert_status; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10945 | } |
| 10946 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 10947 | ~HTTPSOCSPTest() override { |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 10948 | #if defined(OS_ANDROID) || defined(USE_BUILTIN_CERT_VERIFIER) |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 10949 | ShutdownGlobalCertNetFetcher(); |
| 10950 | #endif |
| 10951 | |
svaldez | 2135be5 | 2016-04-20 16:34:53 | [diff] [blame] | 10952 | #if defined(USE_NSS_CERTS) |
Matt Mueller | a17ffaf | 2018-03-28 20:08:18 | [diff] [blame] | 10953 | SetURLRequestContextForNSSHttpIO(nullptr); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10954 | #endif |
| 10955 | } |
| 10956 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10957 | protected: |
| 10958 | // SetupContext configures the URLRequestContext that will be used for making |
| 10959 | // connetions to testserver. This can be overridden in test subclasses for |
| 10960 | // different behaviour. |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 10961 | virtual void SetupContext() { |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 10962 | context_.set_ssl_config_service( |
| 10963 | new TestSSLConfigService(true /* online revocation checking */, |
| 10964 | false /* require rev. checking for local |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 10965 | anchors */, |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 10966 | false /* token binding enabled */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10967 | } |
| 10968 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10969 | std::unique_ptr<ScopedTestRoot> test_root_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 10970 | TestURLRequestContext context_; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 10971 | std::unique_ptr<ScopedTestEVPolicy> ev_test_policy_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 10972 | }; |
| 10973 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10974 | static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() { |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 10975 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10976 | // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't |
| 10977 | // have that ability on other platforms. |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10978 | // TODO(eroman): Should this also be the return value for |
| 10979 | // CertVerifyProcBuiltin? |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 10980 | return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 10981 | #else |
| 10982 | return 0; |
| 10983 | #endif |
| 10984 | } |
| 10985 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10986 | // SystemSupportsHardFailRevocationChecking returns true iff the current |
| 10987 | // operating system supports revocation checking and can distinguish between |
| 10988 | // situations where a given certificate lacks any revocation information (eg: |
| 10989 | // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when |
| 10990 | // revocation information cannot be obtained (eg: the CRL was unreachable). |
| 10991 | // If it does not, then tests which rely on 'hard fail' behaviour should be |
| 10992 | // skipped. |
| 10993 | static bool SystemSupportsHardFailRevocationChecking() { |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 10994 | #if defined(OS_WIN) || defined(USE_NSS_CERTS) || \ |
| 10995 | defined(USE_BUILTIN_CERT_VERIFIER) |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 10996 | return true; |
| 10997 | #else |
| 10998 | return false; |
| 10999 | #endif |
| 11000 | } |
| 11001 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11002 | // SystemUsesChromiumEVMetadata returns true iff the current operating system |
| 11003 | // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then |
| 11004 | // several tests are effected because our testing EV certificate won't be |
| 11005 | // recognised as EV. |
| 11006 | static bool SystemUsesChromiumEVMetadata() { |
Eric Roman | 5f8d9d2 | 2017-10-17 02:32:52 | [diff] [blame] | 11007 | #if defined(PLATFORM_USES_CHROMIUM_EV_METADATA) |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 11008 | return true; |
Eric Roman | 5f8d9d2 | 2017-10-17 02:32:52 | [diff] [blame] | 11009 | #else |
| 11010 | return false; |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 11011 | #endif |
| 11012 | } |
| 11013 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11014 | // Returns the expected CertStatus for tests that expect an online revocation |
| 11015 | // check failure as a result of checking a test EV cert, which will not |
| 11016 | // actually trigger an online revocation check on some platforms. |
| 11017 | static CertStatus ExpectedCertStatusForFailedOnlineEVRevocationCheck() { |
| 11018 | if (SystemUsesChromiumEVMetadata()) { |
| 11019 | return ExpectedCertStatusForFailedOnlineRevocationCheck(); |
| 11020 | } else { |
| 11021 | // If SystemUsesChromiumEVMetadata is false, revocation checking will not |
| 11022 | // be enabled, and thus there will not be a revocation check to fail. |
| 11023 | return 0u; |
| 11024 | } |
| 11025 | } |
| 11026 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11027 | static bool SystemSupportsOCSP() { |
Sergey Ulanov | ee7c8db | 2017-12-08 00:18:39 | [diff] [blame] | 11028 | #if defined(OS_ANDROID) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11029 | // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. |
| 11030 | return false; |
| 11031 | #else |
| 11032 | return true; |
| 11033 | #endif |
| 11034 | } |
| 11035 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11036 | static bool SystemSupportsOCSPStapling() { |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11037 | #if defined(USE_NSS_CERTS) || defined(OS_WIN) || \ |
| 11038 | defined(USE_BUILTIN_CERT_VERIFIER) |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11039 | return true; |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11040 | #else |
| 11041 | return false; |
| 11042 | #endif |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11043 | } |
| 11044 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11045 | TEST_F(HTTPSOCSPTest, Valid) { |
| 11046 | if (!SystemSupportsOCSP()) { |
| 11047 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11048 | return; |
| 11049 | } |
| 11050 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11051 | SpawnedTestServer::SSLOptions ssl_options( |
| 11052 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11053 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11054 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11055 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11056 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11057 | |
| 11058 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11059 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11060 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11061 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11062 | |
| 11063 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11064 | } |
| 11065 | |
| 11066 | TEST_F(HTTPSOCSPTest, Revoked) { |
| 11067 | if (!SystemSupportsOCSP()) { |
| 11068 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11069 | return; |
| 11070 | } |
| 11071 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11072 | SpawnedTestServer::SSLOptions ssl_options( |
| 11073 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11074 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11075 | |
| 11076 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11077 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11078 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11079 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11080 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11081 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11082 | } |
| 11083 | |
| 11084 | TEST_F(HTTPSOCSPTest, Invalid) { |
| 11085 | if (!SystemSupportsOCSP()) { |
| 11086 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11087 | return; |
| 11088 | } |
| 11089 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11090 | SpawnedTestServer::SSLOptions ssl_options( |
| 11091 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11092 | ssl_options.ocsp_status = |
| 11093 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11094 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11095 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11096 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11097 | |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11098 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 11099 | // TODO(649017): This test uses soft-fail revocation checking, but returns an |
| 11100 | // invalid OCSP response (can't parse). CertVerifyProcBuiltin currently |
| 11101 | // doesn't consider this a candidate for soft-fail (only considers |
| 11102 | // network-level failures as skippable). |
| 11103 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 11104 | cert_status & CERT_STATUS_UNABLE_TO_CHECK_REVOCATION); |
| 11105 | #else |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11106 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11107 | cert_status & CERT_STATUS_ALL_ERRORS); |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11108 | #endif |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 11109 | |
| 11110 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 11111 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11112 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11113 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11114 | |
Matt Mueller | 5339d86 | 2018-05-02 18:53:57 | [diff] [blame] | 11115 | TEST_F(HTTPSOCSPTest, IntermediateValid) { |
| 11116 | if (!SystemSupportsOCSP()) { |
| 11117 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11118 | return; |
| 11119 | } |
| 11120 | |
| 11121 | SpawnedTestServer::SSLOptions ssl_options( |
| 11122 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11123 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11124 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11125 | |
| 11126 | CertStatus cert_status; |
| 11127 | DoConnection(ssl_options, &cert_status); |
| 11128 | |
| 11129 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11130 | |
| 11131 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11132 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11133 | |
| 11134 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11135 | } |
| 11136 | |
| 11137 | TEST_F(HTTPSOCSPTest, IntermediateResponseOldButStillValid) { |
| 11138 | if (!SystemSupportsOCSP()) { |
| 11139 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11140 | return; |
| 11141 | } |
| 11142 | |
| 11143 | SpawnedTestServer::SSLOptions ssl_options( |
| 11144 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11145 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11146 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11147 | // Use an OCSP response for the intermediate that would be too old for a leaf |
| 11148 | // cert, but is still valid for an intermediate. |
| 11149 | ssl_options.ocsp_intermediate_date = |
| 11150 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG; |
| 11151 | |
| 11152 | CertStatus cert_status; |
| 11153 | DoConnection(ssl_options, &cert_status); |
| 11154 | |
| 11155 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11156 | |
| 11157 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11158 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11159 | |
| 11160 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11161 | } |
| 11162 | |
| 11163 | TEST_F(HTTPSOCSPTest, IntermediateResponseTooOld) { |
| 11164 | if (!SystemSupportsOCSP()) { |
| 11165 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11166 | return; |
| 11167 | } |
| 11168 | |
| 11169 | SpawnedTestServer::SSLOptions ssl_options( |
| 11170 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11171 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11172 | ssl_options.ocsp_intermediate_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11173 | ssl_options.ocsp_intermediate_date = |
| 11174 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONGER; |
| 11175 | |
| 11176 | CertStatus cert_status; |
| 11177 | DoConnection(ssl_options, &cert_status); |
| 11178 | |
| 11179 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 11180 | // The builtin verifier enforces the baseline requirements for max age of an |
| 11181 | // intermediate's OCSP response. |
| 11182 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 11183 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11184 | EXPECT_EQ(0u, cert_status & CERT_STATUS_IS_EV); |
| 11185 | #else |
| 11186 | // The platform verifiers are more lenient. |
| 11187 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11188 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11189 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11190 | #endif |
| 11191 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11192 | } |
| 11193 | |
| 11194 | TEST_F(HTTPSOCSPTest, IntermediateRevoked) { |
| 11195 | if (!SystemSupportsOCSP()) { |
| 11196 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11197 | return; |
| 11198 | } |
| 11199 | |
| 11200 | SpawnedTestServer::SSLOptions ssl_options( |
| 11201 | SpawnedTestServer::SSLOptions::CERT_AUTO_WITH_INTERMEDIATE); |
| 11202 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11203 | ssl_options.ocsp_intermediate_status = |
| 11204 | SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 11205 | |
| 11206 | CertStatus cert_status; |
| 11207 | DoConnection(ssl_options, &cert_status); |
| 11208 | |
| 11209 | #if defined(OS_WIN) |
| 11210 | // TODO(mattm): why does CertVerifyProcWin accept this? |
| 11211 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11212 | #else |
| 11213 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11214 | #endif |
| 11215 | EXPECT_EQ(0u, cert_status & CERT_STATUS_IS_EV); |
| 11216 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11217 | } |
| 11218 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11219 | TEST_F(HTTPSOCSPTest, ValidStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11220 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11221 | LOG(WARNING) |
| 11222 | << "Skipping test because system doesn't support OCSP stapling"; |
| 11223 | return; |
| 11224 | } |
| 11225 | |
| 11226 | SpawnedTestServer::SSLOptions ssl_options( |
| 11227 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11228 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 11229 | ssl_options.staple_ocsp_response = true; |
| 11230 | ssl_options.ocsp_server_unavailable = true; |
| 11231 | |
| 11232 | CertStatus cert_status; |
| 11233 | DoConnection(ssl_options, &cert_status); |
| 11234 | |
| 11235 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11236 | |
| 11237 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11238 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 11239 | |
| 11240 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11241 | } |
| 11242 | |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 11243 | // Disabled on NSS ports. See https://crbug.com/431716. |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 11244 | #if defined(USE_NSS_CERTS) |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 11245 | #define MAYBE_RevokedStapled DISABLED_RevokedStapled |
| 11246 | #else |
| 11247 | #define MAYBE_RevokedStapled RevokedStapled |
| 11248 | #endif |
| 11249 | TEST_F(HTTPSOCSPTest, MAYBE_RevokedStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 11250 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 11251 | LOG(WARNING) |
| 11252 | << "Skipping test because system doesn't support OCSP stapling"; |
| 11253 | return; |
| 11254 | } |
| 11255 | |
| 11256 | SpawnedTestServer::SSLOptions ssl_options( |
| 11257 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11258 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 11259 | ssl_options.staple_ocsp_response = true; |
| 11260 | ssl_options.ocsp_server_unavailable = true; |
| 11261 | |
| 11262 | CertStatus cert_status; |
| 11263 | DoConnection(ssl_options, &cert_status); |
| 11264 | |
| 11265 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11266 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11267 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11268 | } |
| 11269 | |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 11270 | TEST_F(HTTPSOCSPTest, ExpectStapleReportSentOnMissing) { |
Martijn Croonen | b1383da | 2017-10-11 11:56:35 | [diff] [blame] | 11271 | SetTransportSecurityStateSourceForTesting(&test_default::kHSTSSource); |
| 11272 | |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 11273 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 11274 | https_test_server.SetSSLConfig( |
| 11275 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 11276 | https_test_server.ServeFilesFromSourceDirectory( |
| 11277 | base::FilePath(kTestFilePath)); |
| 11278 | ASSERT_TRUE(https_test_server.Start()); |
| 11279 | |
| 11280 | // Set up a MockCertVerifier to accept the certificate that the server sends, |
| 11281 | // but not provide any OCSP information. |
| 11282 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 11283 | ASSERT_TRUE(cert); |
| 11284 | MockCertVerifier cert_verifier; |
| 11285 | CertVerifyResult verify_result; |
| 11286 | verify_result.verified_cert = cert; |
| 11287 | verify_result.is_issued_by_known_root = true; |
| 11288 | verify_result.ocsp_result.response_status = OCSPVerifyResult::MISSING; |
| 11289 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 11290 | |
| 11291 | // Catch the Expect-Staple report. |
| 11292 | TransportSecurityState transport_security_state; |
| 11293 | MockCertificateReportSender mock_report_sender; |
| 11294 | transport_security_state.SetReportSender(&mock_report_sender); |
| 11295 | |
| 11296 | // Use a MockHostResolver (which by default maps all hosts to 127.0.0.1) so |
| 11297 | // that the request can be sent to a site on the Expect-Staple preload list. |
| 11298 | MockHostResolver host_resolver; |
| 11299 | TestNetworkDelegate network_delegate; |
| 11300 | TestURLRequestContext context(true); |
| 11301 | context.set_host_resolver(&host_resolver); |
| 11302 | context.set_transport_security_state(&transport_security_state); |
| 11303 | context.set_network_delegate(&network_delegate); |
| 11304 | context.set_cert_verifier(&cert_verifier); |
| 11305 | context.Init(); |
| 11306 | |
| 11307 | // Now send a request to trigger the violation. |
| 11308 | TestDelegate d; |
| 11309 | GURL url = https_test_server.GetURL("/"); |
| 11310 | GURL::Replacements replace_host; |
| 11311 | replace_host.SetHostStr(kExpectStapleStaticHostname); |
| 11312 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11313 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
| 11314 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 11315 | violating_request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11316 | d.RunUntilComplete(); |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 11317 | |
| 11318 | // Confirm a report was sent. |
| 11319 | EXPECT_FALSE(mock_report_sender.latest_report().empty()); |
| 11320 | EXPECT_EQ(GURL(kExpectStapleReportURI), |
| 11321 | mock_report_sender.latest_report_uri()); |
| 11322 | } |
| 11323 | |
estark | 13e0b31 | 2016-12-22 23:52:32 | [diff] [blame] | 11324 | // Tests that Expect-Staple reports are not sent for connections on which there |
| 11325 | // is a certificate error. |
| 11326 | TEST_F(HTTPSOCSPTest, ExpectStapleReportNotSentOnMissingWithCertError) { |
| 11327 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 11328 | https_test_server.SetSSLConfig( |
| 11329 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 11330 | https_test_server.ServeFilesFromSourceDirectory( |
| 11331 | base::FilePath(kTestFilePath)); |
| 11332 | ASSERT_TRUE(https_test_server.Start()); |
| 11333 | |
| 11334 | // Set up a MockCertVerifier to report an error for the certificate |
| 11335 | // and indicate that there was no stapled OCSP response. |
| 11336 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 11337 | ASSERT_TRUE(cert); |
| 11338 | MockCertVerifier cert_verifier; |
| 11339 | CertVerifyResult verify_result; |
| 11340 | verify_result.cert_status = CERT_STATUS_DATE_INVALID; |
| 11341 | verify_result.verified_cert = cert; |
| 11342 | verify_result.is_issued_by_known_root = true; |
| 11343 | verify_result.ocsp_result.response_status = OCSPVerifyResult::MISSING; |
| 11344 | cert_verifier.AddResultForCert(cert.get(), verify_result, |
| 11345 | ERR_CERT_DATE_INVALID); |
| 11346 | |
| 11347 | // Set up a mock report sender so that the test can check that an |
| 11348 | // Expect-Staple report is not sent. |
| 11349 | TransportSecurityState transport_security_state; |
| 11350 | MockCertificateReportSender mock_report_sender; |
| 11351 | transport_security_state.SetReportSender(&mock_report_sender); |
| 11352 | |
| 11353 | TestNetworkDelegate network_delegate; |
| 11354 | TestURLRequestContext context(true); |
| 11355 | |
| 11356 | // Force |kExpectStapleStaticHostname| to resolve to |https_test_server|. |
| 11357 | MockHostResolver host_resolver; |
| 11358 | context.set_host_resolver(&host_resolver); |
| 11359 | |
| 11360 | context.set_transport_security_state(&transport_security_state); |
| 11361 | context.set_network_delegate(&network_delegate); |
| 11362 | context.set_cert_verifier(&cert_verifier); |
| 11363 | context.Init(); |
| 11364 | |
| 11365 | // Make a connection to |kExpectStapleStaticHostname|. Because the |
| 11366 | // |verify_result| used with the |cert_verifier| will indicate a certificate |
| 11367 | // error, an Expect-Staple report should not be sent. |
| 11368 | TestDelegate d; |
| 11369 | GURL url = https_test_server.GetURL("/"); |
| 11370 | GURL::Replacements replace_host; |
| 11371 | replace_host.SetHostStr(kExpectStapleStaticHostname); |
| 11372 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11373 | std::unique_ptr<URLRequest> violating_request(context.CreateRequest( |
| 11374 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 13e0b31 | 2016-12-22 23:52:32 | [diff] [blame] | 11375 | violating_request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11376 | d.RunUntilComplete(); |
estark | 13e0b31 | 2016-12-22 23:52:32 | [diff] [blame] | 11377 | |
| 11378 | // Confirm a report was not sent. |
| 11379 | EXPECT_TRUE(mock_report_sender.latest_report().empty()); |
| 11380 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 11381 | } |
| 11382 | |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 11383 | TEST_F(HTTPSOCSPTest, ExpectStapleReportNotSentOnValid) { |
| 11384 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 11385 | https_test_server.SetSSLConfig( |
| 11386 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 11387 | https_test_server.ServeFilesFromSourceDirectory( |
| 11388 | base::FilePath(kTestFilePath)); |
| 11389 | ASSERT_TRUE(https_test_server.Start()); |
| 11390 | |
| 11391 | // Set up a MockCertVerifier to accept the certificate that the server sends, |
| 11392 | // and provide GOOD revocation status. |
| 11393 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 11394 | ASSERT_TRUE(cert); |
| 11395 | MockCertVerifier cert_verifier; |
| 11396 | CertVerifyResult verify_result; |
| 11397 | verify_result.verified_cert = cert; |
| 11398 | verify_result.is_issued_by_known_root = true; |
| 11399 | verify_result.ocsp_result.response_status = OCSPVerifyResult::PROVIDED; |
| 11400 | verify_result.ocsp_result.revocation_status = OCSPRevocationStatus::GOOD; |
| 11401 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 11402 | |
| 11403 | // Catch the Expect-Staple report. |
| 11404 | TransportSecurityState transport_security_state; |
| 11405 | MockCertificateReportSender mock_report_sender; |
| 11406 | transport_security_state.SetReportSender(&mock_report_sender); |
| 11407 | |
| 11408 | // Use a MockHostResolver (which by default maps all hosts to 127.0.0.1) so |
| 11409 | // that the request can be sent to a site on the Expect-Staple preload list. |
| 11410 | MockHostResolver host_resolver; |
| 11411 | TestNetworkDelegate network_delegate; |
| 11412 | TestURLRequestContext context(true); |
| 11413 | context.set_host_resolver(&host_resolver); |
| 11414 | context.set_transport_security_state(&transport_security_state); |
| 11415 | context.set_network_delegate(&network_delegate); |
| 11416 | context.set_cert_verifier(&cert_verifier); |
| 11417 | context.Init(); |
| 11418 | |
| 11419 | // This request should not not trigger an Expect-Staple violation. |
| 11420 | TestDelegate d; |
| 11421 | GURL url = https_test_server.GetURL("/"); |
| 11422 | GURL::Replacements replace_host; |
| 11423 | replace_host.SetHostStr(kExpectStapleStaticHostname); |
| 11424 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11425 | std::unique_ptr<URLRequest> ok_request(context.CreateRequest( |
| 11426 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 11427 | ok_request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11428 | d.RunUntilComplete(); |
dadrian | d476e65 | 2016-07-26 21:33:24 | [diff] [blame] | 11429 | |
| 11430 | // Check that no report was sent. |
| 11431 | EXPECT_TRUE(mock_report_sender.latest_report().empty()); |
| 11432 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 11433 | } |
| 11434 | |
estark | 13e0b31 | 2016-12-22 23:52:32 | [diff] [blame] | 11435 | // Tests that an Expect-Staple report is not sent when OCSP details are not |
| 11436 | // checked on the connection. |
| 11437 | TEST_F(HTTPSOCSPTest, ExpectStapleReportNotSentOnNotChecked) { |
| 11438 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 11439 | https_test_server.SetSSLConfig( |
| 11440 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 11441 | https_test_server.ServeFilesFromSourceDirectory( |
| 11442 | base::FilePath(kTestFilePath)); |
| 11443 | ASSERT_TRUE(https_test_server.Start()); |
| 11444 | |
| 11445 | // Set up a MockCertVerifier to accept the certificate that the server sends, |
| 11446 | // and set |ocsp_result| to indicate that OCSP stapling details were not |
| 11447 | // checked on the connection. |
| 11448 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 11449 | ASSERT_TRUE(cert); |
| 11450 | MockCertVerifier cert_verifier; |
| 11451 | CertVerifyResult verify_result; |
| 11452 | verify_result.verified_cert = cert; |
| 11453 | verify_result.is_issued_by_known_root = true; |
| 11454 | verify_result.ocsp_result.response_status = OCSPVerifyResult::NOT_CHECKED; |
| 11455 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 11456 | |
| 11457 | // Set up a mock report sender so that the test can check that an |
| 11458 | // Expect-Staple report is not sent. |
| 11459 | TransportSecurityState transport_security_state; |
| 11460 | MockCertificateReportSender mock_report_sender; |
| 11461 | transport_security_state.SetReportSender(&mock_report_sender); |
| 11462 | |
| 11463 | TestNetworkDelegate network_delegate; |
| 11464 | TestURLRequestContext context(true); |
| 11465 | |
| 11466 | // Force |kExpectStapleStaticHostname| to resolve to |https_test_server|. |
| 11467 | MockHostResolver host_resolver; |
| 11468 | context.set_host_resolver(&host_resolver); |
| 11469 | |
| 11470 | context.set_transport_security_state(&transport_security_state); |
| 11471 | context.set_network_delegate(&network_delegate); |
| 11472 | context.set_cert_verifier(&cert_verifier); |
| 11473 | context.Init(); |
| 11474 | |
| 11475 | // Make a connection to |kExpectStapleStaticHostname|. Because the |
| 11476 | // |verify_result| used with the |cert_verifier| will indicate that OCSP |
| 11477 | // stapling details were not checked on the connection, an Expect-Staple |
| 11478 | // report should not be sent. |
| 11479 | TestDelegate d; |
| 11480 | GURL url = https_test_server.GetURL("/"); |
| 11481 | GURL::Replacements replace_host; |
| 11482 | replace_host.SetHostStr(kExpectStapleStaticHostname); |
| 11483 | url = url.ReplaceComponents(replace_host); |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11484 | std::unique_ptr<URLRequest> ok_request(context.CreateRequest( |
| 11485 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
estark | 13e0b31 | 2016-12-22 23:52:32 | [diff] [blame] | 11486 | ok_request->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11487 | d.RunUntilComplete(); |
estark | 13e0b31 | 2016-12-22 23:52:32 | [diff] [blame] | 11488 | |
| 11489 | // Check that no report was sent. |
| 11490 | EXPECT_TRUE(mock_report_sender.latest_report().empty()); |
| 11491 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 11492 | } |
| 11493 | |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11494 | static const struct OCSPVerifyTestData { |
| 11495 | std::vector<SpawnedTestServer::SSLOptions::OCSPSingleResponse> ocsp_responses; |
| 11496 | SpawnedTestServer::SSLOptions::OCSPProduced ocsp_produced; |
| 11497 | OCSPVerifyResult::ResponseStatus response_status; |
| 11498 | bool has_revocation_status; |
| 11499 | OCSPRevocationStatus cert_status; |
| 11500 | } kOCSPVerifyData[] = { |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11501 | // 0 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11502 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11503 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11504 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11505 | OCSPVerifyResult::PROVIDED, |
| 11506 | true, |
| 11507 | OCSPRevocationStatus::GOOD}, |
| 11508 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11509 | // 1 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11510 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11511 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 11512 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11513 | OCSPVerifyResult::INVALID_DATE, |
| 11514 | false, |
| 11515 | OCSPRevocationStatus::UNKNOWN}, |
| 11516 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11517 | // 2 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11518 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11519 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11520 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11521 | OCSPVerifyResult::INVALID_DATE, |
| 11522 | false, |
| 11523 | OCSPRevocationStatus::UNKNOWN}, |
| 11524 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11525 | // 3 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11526 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11527 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11528 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11529 | OCSPVerifyResult::INVALID_DATE, |
| 11530 | false, |
| 11531 | OCSPRevocationStatus::UNKNOWN}, |
| 11532 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11533 | // 4 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11534 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11535 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11536 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11537 | OCSPVerifyResult::INVALID_DATE, |
| 11538 | false, |
| 11539 | OCSPRevocationStatus::UNKNOWN}, |
| 11540 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11541 | // 5 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11542 | {{{SpawnedTestServer::SSLOptions::OCSP_TRY_LATER, |
| 11543 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11544 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11545 | OCSPVerifyResult::ERROR_RESPONSE, |
| 11546 | false, |
| 11547 | OCSPRevocationStatus::UNKNOWN}, |
| 11548 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11549 | // 6 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11550 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE, |
| 11551 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11552 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11553 | OCSPVerifyResult::PARSE_RESPONSE_ERROR, |
| 11554 | false, |
| 11555 | OCSPRevocationStatus::UNKNOWN}, |
| 11556 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11557 | // 7 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11558 | {{{SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE_DATA, |
| 11559 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11560 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11561 | OCSPVerifyResult::PARSE_RESPONSE_DATA_ERROR, |
| 11562 | false, |
| 11563 | OCSPRevocationStatus::UNKNOWN}, |
| 11564 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11565 | // 8 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11566 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11567 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11568 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11569 | OCSPVerifyResult::INVALID_DATE, |
| 11570 | false, |
| 11571 | OCSPRevocationStatus::UNKNOWN}, |
| 11572 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11573 | // 9 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11574 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11575 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11576 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11577 | OCSPVerifyResult::PROVIDED, |
| 11578 | true, |
| 11579 | OCSPRevocationStatus::UNKNOWN}, |
| 11580 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11581 | // 10 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11582 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11583 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 11584 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11585 | OCSPVerifyResult::INVALID_DATE, |
| 11586 | false, |
| 11587 | OCSPRevocationStatus::UNKNOWN}, |
| 11588 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11589 | // 11 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11590 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11591 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11592 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11593 | OCSPVerifyResult::INVALID_DATE, |
| 11594 | false, |
| 11595 | OCSPRevocationStatus::UNKNOWN}, |
| 11596 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11597 | // 12 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11598 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11599 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11600 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_BEFORE_CERT, |
| 11601 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11602 | false, |
| 11603 | OCSPRevocationStatus::UNKNOWN}, |
| 11604 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11605 | // 13 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11606 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11607 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11608 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 11609 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11610 | false, |
| 11611 | OCSPRevocationStatus::UNKNOWN}, |
| 11612 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11613 | // 14 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11614 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11615 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11616 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_AFTER_CERT, |
| 11617 | OCSPVerifyResult::BAD_PRODUCED_AT, |
| 11618 | false, |
| 11619 | OCSPRevocationStatus::UNKNOWN}, |
| 11620 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11621 | // 15 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11622 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11623 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11624 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11625 | OCSPVerifyResult::PROVIDED, |
| 11626 | true, |
| 11627 | OCSPRevocationStatus::GOOD}, |
| 11628 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11629 | // 16 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11630 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11631 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 11632 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11633 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11634 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11635 | OCSPVerifyResult::PROVIDED, |
| 11636 | true, |
| 11637 | OCSPRevocationStatus::GOOD}, |
| 11638 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11639 | // 17 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11640 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11641 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 11642 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11643 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11644 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11645 | OCSPVerifyResult::PROVIDED, |
| 11646 | true, |
| 11647 | OCSPRevocationStatus::GOOD}, |
| 11648 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11649 | // 18 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11650 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11651 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 11652 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11653 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11654 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11655 | OCSPVerifyResult::PROVIDED, |
| 11656 | true, |
| 11657 | OCSPRevocationStatus::GOOD}, |
| 11658 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11659 | // 19 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11660 | {{{SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11661 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}, |
| 11662 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11663 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}, |
| 11664 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11665 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11666 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11667 | OCSPVerifyResult::INVALID_DATE, |
| 11668 | false, |
| 11669 | OCSPRevocationStatus::UNKNOWN}, |
| 11670 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11671 | // 20 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11672 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11673 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11674 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11675 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11676 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11677 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11678 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11679 | OCSPVerifyResult::PROVIDED, |
| 11680 | true, |
| 11681 | OCSPRevocationStatus::REVOKED}, |
| 11682 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11683 | // 21 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11684 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11685 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11686 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11687 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11688 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11689 | OCSPVerifyResult::PROVIDED, |
| 11690 | true, |
| 11691 | OCSPRevocationStatus::UNKNOWN}, |
| 11692 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11693 | // 22 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11694 | {{{SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, |
| 11695 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}, |
| 11696 | {SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11697 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}, |
| 11698 | {SpawnedTestServer::SSLOptions::OCSP_OK, |
| 11699 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11700 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11701 | OCSPVerifyResult::PROVIDED, |
| 11702 | true, |
| 11703 | OCSPRevocationStatus::UNKNOWN}, |
| 11704 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11705 | // 23 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11706 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 11707 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11708 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11709 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 11710 | false, |
| 11711 | OCSPRevocationStatus::UNKNOWN}, |
| 11712 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11713 | // 24 |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11714 | {{{SpawnedTestServer::SSLOptions::OCSP_MISMATCHED_SERIAL, |
| 11715 | SpawnedTestServer::SSLOptions::OCSP_DATE_EARLY}}, |
| 11716 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11717 | OCSPVerifyResult::NO_MATCHING_RESPONSE, |
| 11718 | false, |
| 11719 | OCSPRevocationStatus::UNKNOWN}, |
| 11720 | |
Eric Roman | 8673b81 | 2017-09-20 18:57:31 | [diff] [blame] | 11721 | // These tests fail when using NSS for certificate verification, as NSS fails |
| 11722 | // and doesn't return the partial path. As a result the OCSP checks being done |
| 11723 | // at the CertVerifyProc layer cannot access the issuer certificate. |
| 11724 | #if !defined(USE_NSS_CERTS) |
| 11725 | // 25 |
| 11726 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11727 | SpawnedTestServer::SSLOptions::OCSP_DATE_VALID}}, |
| 11728 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11729 | OCSPVerifyResult::PROVIDED, |
| 11730 | true, |
| 11731 | OCSPRevocationStatus::REVOKED}, |
| 11732 | |
| 11733 | // 26 |
| 11734 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11735 | SpawnedTestServer::SSLOptions::OCSP_DATE_OLD}}, |
| 11736 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11737 | OCSPVerifyResult::INVALID_DATE, |
| 11738 | false, |
| 11739 | OCSPRevocationStatus::UNKNOWN}, |
| 11740 | |
| 11741 | // 27 |
| 11742 | {{{SpawnedTestServer::SSLOptions::OCSP_REVOKED, |
| 11743 | SpawnedTestServer::SSLOptions::OCSP_DATE_LONG}}, |
| 11744 | SpawnedTestServer::SSLOptions::OCSP_PRODUCED_VALID, |
| 11745 | OCSPVerifyResult::INVALID_DATE, |
| 11746 | false, |
| 11747 | OCSPRevocationStatus::UNKNOWN}, |
| 11748 | #endif |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11749 | }; |
| 11750 | |
| 11751 | class HTTPSOCSPVerifyTest |
| 11752 | : public HTTPSOCSPTest, |
| 11753 | public testing::WithParamInterface<OCSPVerifyTestData> {}; |
| 11754 | |
| 11755 | TEST_P(HTTPSOCSPVerifyTest, VerifyResult) { |
| 11756 | SpawnedTestServer::SSLOptions ssl_options( |
| 11757 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11758 | OCSPVerifyTestData test = GetParam(); |
| 11759 | |
| 11760 | ssl_options.ocsp_responses = test.ocsp_responses; |
| 11761 | ssl_options.ocsp_produced = test.ocsp_produced; |
| 11762 | ssl_options.staple_ocsp_response = true; |
| 11763 | |
| 11764 | SSLInfo ssl_info; |
| 11765 | OCSPErrorTestDelegate delegate; |
| 11766 | ASSERT_NO_FATAL_FAILURE( |
| 11767 | DoConnectionWithDelegate(ssl_options, &delegate, &ssl_info)); |
| 11768 | |
| 11769 | // The SSLInfo must be extracted from |delegate| on error, due to how |
| 11770 | // URLRequest caches certificate errors. |
| 11771 | if (delegate.have_certificate_errors()) { |
| 11772 | ASSERT_TRUE(delegate.on_ssl_certificate_error_called()); |
| 11773 | ssl_info = delegate.ssl_info(); |
| 11774 | } |
| 11775 | |
| 11776 | EXPECT_EQ(test.response_status, ssl_info.ocsp_result.response_status); |
| 11777 | |
| 11778 | if (test.has_revocation_status) |
| 11779 | EXPECT_EQ(test.cert_status, ssl_info.ocsp_result.revocation_status); |
| 11780 | } |
| 11781 | |
| 11782 | INSTANTIATE_TEST_CASE_P(OCSPVerify, |
| 11783 | HTTPSOCSPVerifyTest, |
| 11784 | testing::ValuesIn(kOCSPVerifyData)); |
| 11785 | |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11786 | class HTTPSAIATest : public HTTPSOCSPTest { |
| 11787 | public: |
| 11788 | void SetupContext() override { |
| 11789 | context_.set_ssl_config_service(new TestSSLConfigService( |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11790 | false /* online revocation checking */, |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11791 | false /* require rev. checking for local anchors */, |
| 11792 | false /* token binding enabled */)); |
| 11793 | } |
| 11794 | }; |
| 11795 | |
| 11796 | TEST_F(HTTPSAIATest, AIAFetching) { |
| 11797 | SpawnedTestServer::SSLOptions ssl_options( |
| 11798 | SpawnedTestServer::SSLOptions::CERT_AUTO_AIA_INTERMEDIATE); |
| 11799 | SpawnedTestServer test_server( |
| 11800 | SpawnedTestServer::TYPE_HTTPS, ssl_options, |
| 11801 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 11802 | ASSERT_TRUE(test_server.Start()); |
| 11803 | |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11804 | // Unmark the certificate's OID as EV, which will disable revocation |
| 11805 | // checking. |
| 11806 | ev_test_policy_.reset(); |
| 11807 | |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11808 | TestDelegate d; |
| 11809 | d.set_allow_certificate_errors(true); |
| 11810 | std::unique_ptr<URLRequest> r(context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 11811 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d, |
| 11812 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11813 | |
| 11814 | r->Start(); |
| 11815 | EXPECT_TRUE(r->is_pending()); |
| 11816 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 11817 | d.RunUntilComplete(); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11818 | |
| 11819 | EXPECT_EQ(1, d.response_started_count()); |
| 11820 | |
| 11821 | CertStatus cert_status = r->ssl_info().cert_status; |
Matt Mueller | fb27199 | 2018-01-30 18:10:48 | [diff] [blame] | 11822 | EXPECT_EQ(OK, d.request_status()); |
| 11823 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11824 | ASSERT_TRUE(r->ssl_info().cert); |
| 11825 | EXPECT_EQ(2u, r->ssl_info().cert->intermediate_buffers().size()); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11826 | ASSERT_TRUE(r->ssl_info().unverified_cert); |
Matt Mueller | a419327 | 2017-12-07 00:23:34 | [diff] [blame] | 11827 | EXPECT_EQ(0u, r->ssl_info().unverified_cert->intermediate_buffers().size()); |
mattm | 0b12a6f | 2016-11-29 19:57:16 | [diff] [blame] | 11828 | } |
| 11829 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11830 | class HTTPSHardFailTest : public HTTPSOCSPTest { |
| 11831 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 11832 | void SetupContext() override { |
| 11833 | context_.set_ssl_config_service(new TestSSLConfigService( |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11834 | false /* online revocation checking */, |
| 11835 | true /* require rev. checking for local anchors */, |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 11836 | false /* token binding enabled */)); |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11837 | } |
| 11838 | }; |
| 11839 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11840 | TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) { |
| 11841 | if (!SystemSupportsOCSP()) { |
| 11842 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11843 | return; |
| 11844 | } |
| 11845 | |
| 11846 | if (!SystemSupportsHardFailRevocationChecking()) { |
| 11847 | LOG(WARNING) << "Skipping test because system doesn't support hard fail " |
| 11848 | << "revocation checking"; |
| 11849 | return; |
| 11850 | } |
| 11851 | |
| 11852 | SpawnedTestServer::SSLOptions ssl_options( |
| 11853 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11854 | ssl_options.ocsp_status = |
| 11855 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11856 | |
| 11857 | CertStatus cert_status; |
| 11858 | DoConnection(ssl_options, &cert_status); |
| 11859 | |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11860 | #if defined(USE_BUILTIN_CERT_VERIFIER) |
| 11861 | // TODO(crbug.com/649017): Should we consider invalid response as |
| 11862 | // affirmatively revoked? |
| 11863 | EXPECT_EQ(CERT_STATUS_UNABLE_TO_CHECK_REVOCATION, |
| 11864 | cert_status & CERT_STATUS_UNABLE_TO_CHECK_REVOCATION); |
| 11865 | #else |
| 11866 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_REVOKED); |
| 11867 | #endif |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11868 | |
| 11869 | // Without a positive OCSP response, we shouldn't show the EV status. |
Eric Roman | 1b628daa | 2017-10-19 19:52:04 | [diff] [blame] | 11870 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 11871 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 11872 | } |
| 11873 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11874 | class HTTPSEVCRLSetTest : public HTTPSOCSPTest { |
| 11875 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 11876 | void SetupContext() override { |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11877 | context_.set_ssl_config_service( |
| 11878 | new TestSSLConfigService(false /* online revocation checking */, |
| 11879 | false /* require rev. checking for local |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 11880 | anchors */, |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 11881 | false /* token binding enabled */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11882 | } |
| 11883 | }; |
| 11884 | |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11885 | // Helper class to set the global CRLSet, and on destruction restore the |
| 11886 | // previously set one. |
| 11887 | class ScopedSetCRLSet { |
| 11888 | public: |
| 11889 | ScopedSetCRLSet(scoped_refptr<CRLSet> crl_set) { |
| 11890 | prev_crl_set_ = SSLConfigService::GetCRLSet(); |
| 11891 | SSLConfigService::SetCRLSetForTesting(std::move(crl_set)); |
| 11892 | } |
| 11893 | |
| 11894 | ~ScopedSetCRLSet() { |
| 11895 | SSLConfigService::SetCRLSetForTesting(std::move(prev_crl_set_)); |
| 11896 | } |
| 11897 | |
| 11898 | private: |
| 11899 | scoped_refptr<CRLSet> prev_crl_set_; |
| 11900 | }; |
| 11901 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11902 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) { |
| 11903 | if (!SystemSupportsOCSP()) { |
| 11904 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11905 | return; |
| 11906 | } |
| 11907 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11908 | SpawnedTestServer::SSLOptions ssl_options( |
| 11909 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11910 | ssl_options.ocsp_status = |
| 11911 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11912 | ScopedSetCRLSet set_crlset(nullptr); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11913 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 11914 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11915 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11916 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11917 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11918 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11919 | |
| 11920 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11921 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11922 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11923 | } |
| 11924 | |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11925 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) { |
| 11926 | if (!SystemSupportsOCSP()) { |
| 11927 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11928 | return; |
| 11929 | } |
| 11930 | |
| 11931 | SpawnedTestServer::SSLOptions ssl_options( |
| 11932 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11933 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11934 | ScopedSetCRLSet set_crlset(nullptr); |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11935 | |
| 11936 | CertStatus cert_status; |
| 11937 | DoConnection(ssl_options, &cert_status); |
| 11938 | |
mattm | 1a282f5 | 2016-11-10 21:49:42 | [diff] [blame] | 11939 | // Currently only works for Windows and OS X. When using NSS, it's not |
| 11940 | // possible to determine whether the check failed because of actual |
| 11941 | // revocation or because there was an OCSP failure. |
| 11942 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 11943 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11944 | #else |
| 11945 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11946 | #endif |
| 11947 | |
| 11948 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 11949 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11950 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 11951 | } |
| 11952 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11953 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) { |
| 11954 | if (!SystemSupportsOCSP()) { |
| 11955 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11956 | return; |
| 11957 | } |
| 11958 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11959 | SpawnedTestServer::SSLOptions ssl_options( |
| 11960 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 11961 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11962 | ScopedSetCRLSet set_crlset(nullptr); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11963 | |
| 11964 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11965 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11966 | |
| 11967 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 11968 | |
| 11969 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11970 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11971 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11972 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11973 | } |
| 11974 | |
| 11975 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) { |
| 11976 | if (!SystemSupportsOCSP()) { |
| 11977 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 11978 | return; |
| 11979 | } |
| 11980 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 11981 | SpawnedTestServer::SSLOptions ssl_options( |
| 11982 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 11983 | ssl_options.ocsp_status = |
| 11984 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 11985 | ScopedSetCRLSet set_crlset(CRLSet::ExpiredCRLSetForTesting()); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11986 | |
| 11987 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 11988 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11989 | |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 11990 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11991 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 11992 | |
| 11993 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 11994 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 11995 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 11996 | } |
| 11997 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 11998 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) { |
| 11999 | if (!SystemSupportsOCSP()) { |
| 12000 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12001 | return; |
| 12002 | } |
| 12003 | |
| 12004 | SpawnedTestServer::SSLOptions ssl_options( |
| 12005 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12006 | ssl_options.ocsp_status = |
| 12007 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 12008 | ScopedSetCRLSet set_crlset( |
| 12009 | CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "", "", {})); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12010 | |
| 12011 | CertStatus cert_status; |
| 12012 | DoConnection(ssl_options, &cert_status); |
| 12013 | |
| 12014 | // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a |
| 12015 | // revocation check for EV. |
| 12016 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12017 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 12018 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 12019 | EXPECT_FALSE( |
| 12020 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 12021 | } |
| 12022 | |
| 12023 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) { |
| 12024 | if (!SystemSupportsOCSP()) { |
| 12025 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12026 | return; |
| 12027 | } |
| 12028 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 12029 | SpawnedTestServer::SSLOptions ssl_options( |
| 12030 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12031 | ssl_options.ocsp_status = |
| 12032 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 12033 | ScopedSetCRLSet set_crlset(CRLSet::EmptyCRLSetForTesting()); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12034 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12035 | CertStatus cert_status = 0; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 12036 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12037 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12038 | // Even with a fresh CRLSet, we should still do online revocation checks when |
| 12039 | // the certificate chain isn't covered by the CRLSet, which it isn't in this |
| 12040 | // test. |
mattm | af868e7 | 2016-09-23 23:25:20 | [diff] [blame] | 12041 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineEVRevocationCheck(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12042 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12043 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12044 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12045 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12046 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12047 | } |
| 12048 | |
| 12049 | class HTTPSCRLSetTest : public HTTPSOCSPTest { |
| 12050 | protected: |
rsleevi | d6de830 | 2016-06-21 01:33:20 | [diff] [blame] | 12051 | void SetupContext() override { |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12052 | context_.set_ssl_config_service( |
| 12053 | new TestSSLConfigService(false /* online revocation checking */, |
| 12054 | false /* require rev. checking for local |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 12055 | anchors */, |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12056 | false /* token binding enabled */)); |
| 12057 | } |
| 12058 | |
| 12059 | void SetUp() override { |
| 12060 | HTTPSOCSPTest::SetUp(); |
| 12061 | |
| 12062 | // Unmark the certificate's OID as EV, which should disable revocation |
| 12063 | // checking (as per the user preference). |
| 12064 | ev_test_policy_.reset(); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12065 | } |
| 12066 | }; |
| 12067 | |
| 12068 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 12069 | SpawnedTestServer::SSLOptions ssl_options( |
| 12070 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
dadrian | 612337a | 2016-07-20 22:36:58 | [diff] [blame] | 12071 | ssl_options.ocsp_status = |
| 12072 | SpawnedTestServer::SSLOptions::OCSP_INVALID_RESPONSE; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 12073 | ScopedSetCRLSet set_crlset(CRLSet::ExpiredCRLSetForTesting()); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12074 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 12075 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 12076 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 12077 | |
| 12078 | // If we're not trying EV verification then, even if the CRLSet has expired, |
| 12079 | // we don't fall back to online revocation checks. |
| 12080 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12081 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 12082 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 12083 | } |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12084 | |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12085 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSetAndRevoked) { |
| 12086 | // Test that when online revocation checking is disabled, and the leaf |
| 12087 | // certificate is not EV, that no revocation checking actually happens. |
| 12088 | if (!SystemSupportsOCSP()) { |
| 12089 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 12090 | return; |
| 12091 | } |
| 12092 | |
| 12093 | SpawnedTestServer::SSLOptions ssl_options( |
| 12094 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12095 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 12096 | ScopedSetCRLSet set_crlset(CRLSet::ExpiredCRLSetForTesting()); |
| 12097 | |
| 12098 | CertStatus cert_status; |
| 12099 | DoConnection(ssl_options, &cert_status); |
| 12100 | |
| 12101 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12102 | |
| 12103 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 12104 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 12105 | } |
| 12106 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12107 | TEST_F(HTTPSCRLSetTest, CRLSetRevoked) { |
Eric Roman | efddd0a | 2017-10-10 02:14:25 | [diff] [blame] | 12108 | #if defined(OS_ANDROID) |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12109 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 12110 | return; |
| 12111 | #endif |
| 12112 | |
| 12113 | SpawnedTestServer::SSLOptions ssl_options( |
| 12114 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12115 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 12116 | ssl_options.cert_serial = 10; |
Eric Roman | e2243cc6 | 2017-10-17 03:59:13 | [diff] [blame] | 12117 | ScopedSetCRLSet set_crlset( |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 12118 | CRLSet::ForTesting(false, &kOCSPTestCertSPKI, "\x0a", "", {})); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12119 | |
| 12120 | CertStatus cert_status = 0; |
| 12121 | DoConnection(ssl_options, &cert_status); |
| 12122 | |
| 12123 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 12124 | // reflected without online revocation checking. |
| 12125 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12126 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12127 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 12128 | } |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 12129 | |
| 12130 | TEST_F(HTTPSCRLSetTest, CRLSetRevokedBySubject) { |
| 12131 | #if defined(OS_ANDROID) |
| 12132 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 12133 | return; |
| 12134 | #endif |
| 12135 | |
| 12136 | SpawnedTestServer::SSLOptions ssl_options( |
| 12137 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 12138 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 12139 | static const char kCommonName[] = "Test CN"; |
| 12140 | ssl_options.cert_common_name = kCommonName; |
| 12141 | |
| 12142 | { |
| 12143 | ScopedSetCRLSet set_crlset( |
| 12144 | CRLSet::ForTesting(false, nullptr, "", kCommonName, {})); |
| 12145 | |
| 12146 | CertStatus cert_status = 0; |
| 12147 | DoConnection(ssl_options, &cert_status); |
| 12148 | |
| 12149 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 12150 | // reflected without online revocation checking. |
| 12151 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12152 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
Ryan Sleevi | 5b4702a | 2018-04-06 22:23:56 | [diff] [blame] | 12153 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
Adam Langley | ea6d678 | 2017-12-05 23:57:33 | [diff] [blame] | 12154 | } |
| 12155 | |
| 12156 | const uint8_t kTestServerSPKISHA256[32] = { |
| 12157 | 0xb3, 0x91, 0xac, 0x73, 0x32, 0x54, 0x7f, 0x7b, 0x8a, 0x62, 0x77, |
| 12158 | 0x73, 0x1d, 0x45, 0x7b, 0x23, 0x46, 0x69, 0xef, 0x6f, 0x05, 0x3d, |
| 12159 | 0x07, 0x22, 0x15, 0x18, 0xd6, 0x10, 0x8b, 0xa1, 0x49, 0x33, |
| 12160 | }; |
| 12161 | const std::string spki_hash( |
| 12162 | reinterpret_cast<const char*>(kTestServerSPKISHA256), |
| 12163 | sizeof(kTestServerSPKISHA256)); |
| 12164 | |
| 12165 | { |
| 12166 | ScopedSetCRLSet set_crlset( |
| 12167 | CRLSet::ForTesting(false, nullptr, "", kCommonName, {spki_hash})); |
| 12168 | |
| 12169 | CertStatus cert_status = 0; |
| 12170 | DoConnection(ssl_options, &cert_status); |
| 12171 | |
| 12172 | // When the correct SPKI hash is specified, the connection should succeed |
| 12173 | // even though the subject is listed in the CRLSet. |
| 12174 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 12175 | } |
| 12176 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 12177 | #endif // !defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 12178 | |
Sergey Ulanov | c4580e7 | 2017-09-13 23:30:11 | [diff] [blame] | 12179 | #if !BUILDFLAG(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) && \ |
| 12180 | !defined(OS_FUCHSIA) |
| 12181 | // FTP uses a second TCP connection with the port number allocated dynamically |
| 12182 | // on the server side, so it would be hard to make RemoteTestServer proxy FTP |
| 12183 | // connections reliably. FTP tests are disabled on platforms that use |
| 12184 | // RemoteTestServer. See http://crbug.com/495220 |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 12185 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12186 | public: |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 12187 | URLRequestTestFTP() |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 12188 | : ftp_test_server_(SpawnedTestServer::TYPE_FTP, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12189 | base::FilePath(kTestFilePath)) { |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12190 | // Can't use |default_context_|'s HostResolver to set up the |
| 12191 | // FTPTransactionFactory because it hasn't been created yet. |
| 12192 | default_context_.set_host_resolver(&host_resolver_); |
| 12193 | } |
| 12194 | |
| 12195 | // URLRequestTest interface: |
| 12196 | void SetUpFactory() override { |
| 12197 | // Add FTP support to the default URLRequestContext. |
| 12198 | job_factory_impl_->SetProtocolHandler( |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 12199 | "ftp", FtpProtocolHandler::Create(&host_resolver_)); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12200 | } |
| 12201 | |
| 12202 | std::string GetTestFileContents() { |
| 12203 | base::FilePath path; |
Avi Drissman | 5c80d83 | 2018-05-01 17:01:19 | [diff] [blame] | 12204 | EXPECT_TRUE(base::PathService::Get(base::DIR_SOURCE_ROOT, &path)); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12205 | path = path.Append(kTestFilePath); |
| 12206 | path = path.AppendASCII(kFtpTestFile); |
| 12207 | std::string contents; |
| 12208 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
| 12209 | return contents; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12210 | } |
| 12211 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 12212 | protected: |
mmenke | cd4c753 | 2016-10-19 18:36:05 | [diff] [blame] | 12213 | // Note that this is destroyed before the FtpProtocolHandler that references |
| 12214 | // it, which is owned by the parent class. Since no requests are made during |
| 12215 | // teardown, this works, though it's not great. |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12216 | MockHostResolver host_resolver_; |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12217 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12218 | SpawnedTestServer ftp_test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 12219 | }; |
| 12220 | |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12221 | // Make sure an FTP request using an unsafe ports fails. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 12222 | TEST_F(URLRequestTestFTP, UnsafePort) { |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12223 | GURL url("ftp://127.0.0.1:7"); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12224 | |
| 12225 | TestDelegate d; |
| 12226 | { |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12227 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 12228 | url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12229 | r->Start(); |
| 12230 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12231 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12232 | d.RunUntilComplete(); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12233 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12234 | EXPECT_FALSE(r->is_pending()); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 12235 | EXPECT_EQ(ERR_UNSAFE_PORT, d.request_status()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 12236 | } |
| 12237 | } |
| 12238 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12239 | TEST_F(URLRequestTestFTP, FTPDirectoryListing) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12240 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12241 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12242 | TestDelegate d; |
| 12243 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12244 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12245 | ftp_test_server_.GetURL("/"), DEFAULT_PRIORITY, &d, |
| 12246 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12247 | r->Start(); |
| 12248 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12249 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12250 | d.RunUntilComplete(); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12251 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12252 | EXPECT_FALSE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12253 | EXPECT_EQ(1, d.response_started_count()); |
| 12254 | EXPECT_FALSE(d.received_data_before_response()); |
| 12255 | EXPECT_LT(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12256 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12257 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12258 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12259 | r->GetSocketAddress().port()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 12260 | } |
| 12261 | } |
| 12262 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12263 | TEST_F(URLRequestTestFTP, FTPGetTestAnonymous) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12264 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12265 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12266 | TestDelegate d; |
| 12267 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12268 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12269 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, &d, |
| 12270 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12271 | r->Start(); |
| 12272 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12273 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12274 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12275 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12276 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12277 | EXPECT_EQ(1, d.response_started_count()); |
| 12278 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12279 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12280 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12281 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12282 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12283 | r->GetSocketAddress().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12284 | } |
| 12285 | } |
| 12286 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12287 | TEST_F(URLRequestTestFTP, FTPGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12288 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12289 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12290 | TestDelegate d; |
| 12291 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12292 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12293 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12294 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12295 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12296 | r->Start(); |
| 12297 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12298 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12299 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12300 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12301 | EXPECT_FALSE(r->is_pending()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12302 | EXPECT_EQ(1, d.response_started_count()); |
| 12303 | EXPECT_FALSE(d.received_data_before_response()); |
| 12304 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12305 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12306 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12307 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12308 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 12309 | |
| 12310 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12311 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 12312 | TestLoadTimingNoHttpResponse(load_timing_info); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12313 | } |
| 12314 | } |
| 12315 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12316 | TEST_F(URLRequestTestFTP, FTPCheckWrongPassword) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12317 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12318 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12319 | TestDelegate d; |
| 12320 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12321 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12322 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12323 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12324 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12325 | r->Start(); |
| 12326 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12327 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12328 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12329 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12330 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12331 | EXPECT_EQ(1, d.response_started_count()); |
| 12332 | EXPECT_FALSE(d.received_data_before_response()); |
| 12333 | EXPECT_EQ(d.bytes_received(), 0); |
| 12334 | } |
| 12335 | } |
| 12336 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12337 | TEST_F(URLRequestTestFTP, FTPCheckWrongPasswordRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12338 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12339 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12340 | TestDelegate d; |
| 12341 | // Set correct login credentials. The delegate will be asked for them when |
| 12342 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 12343 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12344 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12345 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12346 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12347 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12348 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12349 | r->Start(); |
| 12350 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12351 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12352 | d.RunUntilComplete(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12353 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12354 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12355 | EXPECT_EQ(1, d.response_started_count()); |
| 12356 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12357 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12358 | } |
| 12359 | } |
| 12360 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12361 | TEST_F(URLRequestTestFTP, FTPCheckWrongUser) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12362 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12363 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12364 | TestDelegate d; |
| 12365 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12366 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12367 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 12368 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12369 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12370 | r->Start(); |
| 12371 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12372 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12373 | d.RunUntilComplete(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12374 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12375 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12376 | EXPECT_EQ(1, d.response_started_count()); |
| 12377 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12378 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 12379 | } |
| 12380 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12381 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12382 | TEST_F(URLRequestTestFTP, FTPCheckWrongUserRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12383 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12384 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12385 | TestDelegate d; |
| 12386 | // Set correct login credentials. The delegate will be asked for them when |
| 12387 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 12388 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12389 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12390 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12391 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 12392 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12393 | DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12394 | r->Start(); |
| 12395 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12396 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12397 | d.RunUntilComplete(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12398 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12399 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12400 | EXPECT_EQ(1, d.response_started_count()); |
| 12401 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12402 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 12403 | } |
| 12404 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12405 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12406 | TEST_F(URLRequestTestFTP, FTPCacheURLCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12407 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12408 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12409 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12410 | { |
| 12411 | // Pass correct login identity in the URL. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12412 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12413 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12414 | "chrome"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12415 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12416 | r->Start(); |
| 12417 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12418 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12419 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12420 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12421 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12422 | EXPECT_EQ(1, d->response_started_count()); |
| 12423 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12424 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12425 | } |
| 12426 | |
| 12427 | d.reset(new TestDelegate); |
| 12428 | { |
| 12429 | // This request should use cached identity from previous request. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12430 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12431 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 12432 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12433 | r->Start(); |
| 12434 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12435 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12436 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12437 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12438 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12439 | EXPECT_EQ(1, d->response_started_count()); |
| 12440 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12441 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12442 | } |
| 12443 | } |
| 12444 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12445 | TEST_F(URLRequestTestFTP, FTPCacheLoginBoxCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12446 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 12447 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12448 | std::unique_ptr<TestDelegate> d(new TestDelegate); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12449 | // Set correct login credentials. The delegate will be asked for them when |
| 12450 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 12451 | d->set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12452 | { |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12453 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 12454 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 12455 | "wrong_password"), |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12456 | DEFAULT_PRIORITY, d.get(), TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12457 | r->Start(); |
| 12458 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12459 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12460 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12461 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12462 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12463 | EXPECT_EQ(1, d->response_started_count()); |
| 12464 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12465 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12466 | } |
| 12467 | |
| 12468 | // Use a new delegate without explicit credentials. The cached ones should be |
| 12469 | // used. |
| 12470 | d.reset(new TestDelegate); |
| 12471 | { |
| 12472 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 12473 | // ones. |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12474 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12475 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get(), |
| 12476 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12477 | r->Start(); |
| 12478 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12479 | |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12480 | d->RunUntilComplete(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12481 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 12482 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12483 | EXPECT_EQ(1, d->response_started_count()); |
| 12484 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 12485 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 12486 | } |
| 12487 | } |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 12488 | |
| 12489 | TEST_F(URLRequestTestFTP, RawBodyBytes) { |
| 12490 | ASSERT_TRUE(ftp_test_server_.Start()); |
| 12491 | |
| 12492 | TestDelegate d; |
| 12493 | std::unique_ptr<URLRequest> req(default_context().CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12494 | ftp_test_server_.GetURL("simple.html"), DEFAULT_PRIORITY, &d, |
| 12495 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 12496 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12497 | d.RunUntilComplete(); |
ricea | 3a1c71f | 2016-06-17 10:05:26 | [diff] [blame] | 12498 | |
| 12499 | EXPECT_EQ(6, req->GetRawBodyBytes()); |
| 12500 | } |
| 12501 | |
brettw | a1228ebb | 2016-10-28 03:51:34 | [diff] [blame] | 12502 | #endif // !BUILDFLAG(DISABLE_FTP_SUPPORT) |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 12503 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12504 | TEST_F(URLRequestTest, NetworkAccessedClearOnDataRequest) { |
| 12505 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12506 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 12507 | GURL("data:,"), DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12508 | |
| 12509 | EXPECT_FALSE(req->response_info().network_accessed); |
| 12510 | |
| 12511 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12512 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12513 | |
| 12514 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 12515 | EXPECT_FALSE(req->response_info().network_accessed); |
| 12516 | } |
| 12517 | |
| 12518 | TEST_F(URLRequestTest, NetworkAccessedSetOnHostResolutionFailure) { |
| 12519 | MockHostResolver host_resolver; |
| 12520 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 12521 | TestURLRequestContext context(true); |
| 12522 | context.set_network_delegate(&network_delegate); |
| 12523 | context.set_host_resolver(&host_resolver); |
| 12524 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 12525 | context.Init(); |
| 12526 | |
| 12527 | TestDelegate d; |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12528 | std::unique_ptr<URLRequest> req( |
| 12529 | context.CreateRequest(GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, |
| 12530 | &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12531 | |
| 12532 | EXPECT_FALSE(req->response_info().network_accessed); |
| 12533 | |
| 12534 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12535 | d.RunUntilComplete(); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 12536 | EXPECT_TRUE(req->response_info().network_accessed); |
| 12537 | } |
| 12538 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 12539 | // Test that URLRequest is canceled correctly. |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12540 | // See http://crbug.com/508900 |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 12541 | TEST_F(URLRequestTest, URLRequestRedirectJobCancelRequest) { |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12542 | TestDelegate d; |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12543 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
rhalavati | b7bd7c79 | 2017-04-27 05:25:16 | [diff] [blame] | 12544 | GURL("http://not-a-real-domain/"), DEFAULT_PRIORITY, &d, |
| 12545 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12546 | |
danakj | 8522a25b | 2016-04-16 00:17:36 | [diff] [blame] | 12547 | std::unique_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12548 | req.get(), &default_network_delegate_, |
| 12549 | GURL("http://this-should-never-be-navigated-to/"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 12550 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Jumbo shrimp")); |
| 12551 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12552 | |
| 12553 | req->Start(); |
| 12554 | req->Cancel(); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12555 | base::RunLoop().RunUntilIdle(); |
maksim.sisov | b53724b5 | 2016-09-16 05:30:50 | [diff] [blame] | 12556 | EXPECT_EQ(ERR_ABORTED, d.request_status()); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 12557 | EXPECT_EQ(0, d.received_redirect_count()); |
| 12558 | } |
| 12559 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12560 | TEST_F(URLRequestTestHTTP, HeadersCallbacks) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12561 | ASSERT_TRUE(http_test_server()->Start()); |
| 12562 | TestURLRequestContext context; |
| 12563 | GURL url(http_test_server()->GetURL("/cachetime")); |
| 12564 | TestDelegate delegate; |
| 12565 | HttpRequestHeaders extra_headers; |
| 12566 | extra_headers.SetHeader("X-Foo", "bar"); |
| 12567 | |
| 12568 | { |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12569 | HttpRawRequestHeaders raw_req_headers; |
| 12570 | scoped_refptr<const HttpResponseHeaders> raw_resp_headers; |
| 12571 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12572 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12573 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12574 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12575 | r->SetRequestHeadersCallback(base::Bind( |
| 12576 | &HttpRawRequestHeaders::Assign, base::Unretained(&raw_req_headers))); |
| 12577 | r->SetResponseHeadersCallback(base::Bind( |
| 12578 | [](scoped_refptr<const HttpResponseHeaders>* left, |
| 12579 | scoped_refptr<const HttpResponseHeaders> right) { *left = right; }, |
| 12580 | base::Unretained(&raw_resp_headers))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12581 | r->Start(); |
| 12582 | while (!delegate.response_started_count()) |
| 12583 | base::RunLoop().RunUntilIdle(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12584 | EXPECT_FALSE(raw_req_headers.headers().empty()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12585 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12586 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12587 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12588 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12589 | EXPECT_EQ("gzip, deflate", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12590 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Connection", &value)); |
| 12591 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Host", &value)); |
| 12592 | EXPECT_EQ("GET /cachetime HTTP/1.1\r\n", raw_req_headers.request_line()); |
| 12593 | EXPECT_EQ(raw_resp_headers.get(), r->response_headers()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12594 | } |
| 12595 | { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12596 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12597 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12598 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12599 | r->SetRequestHeadersCallback(base::Bind([](HttpRawRequestHeaders) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12600 | FAIL() << "Callback should not be called unless request is sent"; |
| 12601 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12602 | r->SetResponseHeadersCallback( |
| 12603 | base::Bind([](scoped_refptr<const HttpResponseHeaders>) { |
| 12604 | FAIL() << "Callback should not be called unless request is sent"; |
| 12605 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12606 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12607 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12608 | EXPECT_TRUE(r->was_cached()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12609 | } |
| 12610 | } |
| 12611 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12612 | TEST_F(URLRequestTestHTTP, HeadersCallbacksWithRedirect) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12613 | ASSERT_TRUE(http_test_server()->Start()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12614 | HttpRawRequestHeaders raw_req_headers; |
| 12615 | scoped_refptr<const HttpResponseHeaders> raw_resp_headers; |
| 12616 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12617 | TestURLRequestContext context; |
| 12618 | TestDelegate delegate; |
| 12619 | HttpRequestHeaders extra_headers; |
| 12620 | extra_headers.SetHeader("X-Foo", "bar"); |
| 12621 | delegate.set_quit_on_redirect(true); |
| 12622 | GURL url(http_test_server()->GetURL("/redirect-test.html")); |
| 12623 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 12624 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12625 | r->SetExtraRequestHeaders(extra_headers); |
| 12626 | r->SetRequestHeadersCallback(base::Bind(&HttpRawRequestHeaders::Assign, |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12627 | base::Unretained(&raw_req_headers))); |
| 12628 | r->SetResponseHeadersCallback(base::Bind( |
| 12629 | [](scoped_refptr<const HttpResponseHeaders>* left, |
| 12630 | scoped_refptr<const HttpResponseHeaders> right) { *left = right; }, |
| 12631 | base::Unretained(&raw_resp_headers))); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12632 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12633 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12634 | |
| 12635 | ASSERT_EQ(1, delegate.received_redirect_count()); |
| 12636 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12637 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12638 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12639 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12640 | EXPECT_EQ("gzip, deflate", value); |
| 12641 | EXPECT_EQ(1, delegate.received_redirect_count()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12642 | EXPECT_EQ("GET /redirect-test.html HTTP/1.1\r\n", |
| 12643 | raw_req_headers.request_line()); |
| 12644 | EXPECT_TRUE(raw_resp_headers->HasHeader("Location")); |
| 12645 | EXPECT_EQ(302, raw_resp_headers->response_code()); |
| 12646 | EXPECT_EQ("Redirect", raw_resp_headers->GetStatusText()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12647 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12648 | raw_req_headers = HttpRawRequestHeaders(); |
| 12649 | raw_resp_headers = nullptr; |
Chong Zhang | 7607f1f | 2018-06-01 20:52:20 | [diff] [blame] | 12650 | r->FollowDeferredRedirect(base::nullopt /* modified_request_headers */); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12651 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12652 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12653 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12654 | EXPECT_TRUE(raw_req_headers.FindHeaderForTest("Accept-Encoding", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12655 | EXPECT_EQ("gzip, deflate", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12656 | EXPECT_EQ("GET /with-headers.html HTTP/1.1\r\n", |
| 12657 | raw_req_headers.request_line()); |
| 12658 | EXPECT_EQ(r->response_headers(), raw_resp_headers.get()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12659 | } |
| 12660 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12661 | TEST_F(URLRequestTest, HeadersCallbacksConnectFailed) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12662 | TestDelegate request_delegate; |
| 12663 | |
| 12664 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 12665 | GURL("http://127.0.0.1:9/"), DEFAULT_PRIORITY, &request_delegate, |
| 12666 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12667 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
| 12668 | FAIL() << "Callback should not be called unless request is sent"; |
| 12669 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12670 | r->SetResponseHeadersCallback( |
| 12671 | base::Bind([](scoped_refptr<const net::HttpResponseHeaders>) { |
| 12672 | FAIL() << "Callback should not be called unless request is sent"; |
| 12673 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12674 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12675 | request_delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12676 | EXPECT_FALSE(r->is_pending()); |
| 12677 | } |
| 12678 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12679 | TEST_F(URLRequestTestHTTP, HeadersCallbacksAuthRetry) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12680 | ASSERT_TRUE(http_test_server()->Start()); |
| 12681 | GURL url(http_test_server()->GetURL("/auth-basic")); |
| 12682 | |
| 12683 | TestURLRequestContext context; |
| 12684 | TestDelegate delegate; |
| 12685 | |
| 12686 | delegate.set_credentials(AuthCredentials(kUser, kSecret)); |
| 12687 | HttpRequestHeaders extra_headers; |
| 12688 | extra_headers.SetHeader("X-Foo", "bar"); |
| 12689 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12690 | using ReqHeadersVector = std::vector<std::unique_ptr<HttpRawRequestHeaders>>; |
| 12691 | ReqHeadersVector raw_req_headers; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12692 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12693 | using RespHeadersVector = |
| 12694 | std::vector<scoped_refptr<const HttpResponseHeaders>>; |
| 12695 | RespHeadersVector raw_resp_headers; |
| 12696 | |
| 12697 | auto req_headers_callback = base::Bind( |
| 12698 | [](ReqHeadersVector* vec, HttpRawRequestHeaders headers) { |
| 12699 | vec->emplace_back(new HttpRawRequestHeaders(std::move(headers))); |
| 12700 | }, |
| 12701 | &raw_req_headers); |
| 12702 | auto resp_headers_callback = base::Bind( |
| 12703 | [](RespHeadersVector* vec, |
| 12704 | scoped_refptr<const HttpResponseHeaders> headers) { |
| 12705 | vec->push_back(headers); |
| 12706 | }, |
| 12707 | &raw_resp_headers); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12708 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12709 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12710 | r->SetExtraRequestHeaders(extra_headers); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12711 | r->SetRequestHeadersCallback(req_headers_callback); |
| 12712 | r->SetResponseHeadersCallback(resp_headers_callback); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12713 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12714 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12715 | EXPECT_FALSE(r->is_pending()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12716 | ASSERT_EQ(raw_req_headers.size(), 2u); |
| 12717 | ASSERT_EQ(raw_resp_headers.size(), 2u); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12718 | std::string value; |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12719 | EXPECT_FALSE(raw_req_headers[0]->FindHeaderForTest("Authorization", &value)); |
| 12720 | EXPECT_TRUE(raw_req_headers[0]->FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12721 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12722 | EXPECT_TRUE(raw_req_headers[1]->FindHeaderForTest("Authorization", &value)); |
| 12723 | EXPECT_TRUE(raw_req_headers[1]->FindHeaderForTest("X-Foo", &value)); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12724 | EXPECT_EQ("bar", value); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12725 | EXPECT_EQ(raw_resp_headers[1], r->response_headers()); |
| 12726 | EXPECT_NE(raw_resp_headers[0], raw_resp_headers[1]); |
| 12727 | EXPECT_EQ(401, raw_resp_headers[0]->response_code()); |
| 12728 | EXPECT_EQ("Unauthorized", raw_resp_headers[0]->GetStatusText()); |
| 12729 | |
| 12730 | std::unique_ptr<URLRequest> r2(context.CreateRequest( |
| 12731 | url, DEFAULT_PRIORITY, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12732 | r2->SetExtraRequestHeaders(extra_headers); |
| 12733 | r2->SetRequestHeadersCallback(req_headers_callback); |
| 12734 | r2->SetResponseHeadersCallback(resp_headers_callback); |
| 12735 | r2->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 12736 | r2->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12737 | delegate.RunUntilComplete(); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12738 | EXPECT_FALSE(r2->is_pending()); |
| 12739 | ASSERT_EQ(raw_req_headers.size(), 3u); |
| 12740 | ASSERT_EQ(raw_resp_headers.size(), 3u); |
| 12741 | EXPECT_TRUE(raw_req_headers[2]->FindHeaderForTest("If-None-Match", &value)); |
| 12742 | EXPECT_NE(raw_resp_headers[2].get(), r2->response_headers()); |
| 12743 | EXPECT_EQ(304, raw_resp_headers[2]->response_code()); |
| 12744 | EXPECT_EQ("Not Modified", raw_resp_headers[2]->GetStatusText()); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12745 | } |
| 12746 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12747 | TEST_F(URLRequestTest, HeadersCallbacksNonHTTP) { |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12748 | GURL data_url("data:text/html,<html><body>Hello!</body></html>"); |
| 12749 | TestDelegate d; |
| 12750 | std::unique_ptr<URLRequest> r(default_context_.CreateRequest( |
| 12751 | data_url, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12752 | r->SetRequestHeadersCallback(base::Bind([](net::HttpRawRequestHeaders) { |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12753 | FAIL() << "Callback should not be called for non-HTTP schemes"; |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12754 | })); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 12755 | r->SetResponseHeadersCallback( |
| 12756 | base::Bind([](scoped_refptr<const net::HttpResponseHeaders>) { |
| 12757 | FAIL() << "Callback should not be called for non-HTTP schemes"; |
| 12758 | })); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12759 | r->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12760 | d.RunUntilComplete(); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 12761 | EXPECT_FALSE(r->is_pending()); |
| 12762 | } |
| 12763 | |
Carlos IL | aef65d6 | 2018-06-04 21:24:13 | [diff] [blame] | 12764 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSet) { |
| 12765 | TestDelegate d; |
| 12766 | BlockingNetworkDelegate network_delegate( |
| 12767 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 12768 | const GURL kOriginalUrl("https://original.test"); |
| 12769 | const GURL kRedirectUrl("http://redirect.test"); |
| 12770 | network_delegate.set_redirect_url(kRedirectUrl); |
| 12771 | TestURLRequestContext context(true /* delay_initialization */); |
| 12772 | context.set_network_delegate(&network_delegate); |
| 12773 | context.Init(); |
| 12774 | |
| 12775 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12776 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12777 | r->set_upgrade_if_insecure(true); |
| 12778 | d.set_quit_on_redirect(true); |
| 12779 | r->Start(); |
| 12780 | base::RunLoop().Run(); |
| 12781 | GURL::Replacements replacements; |
| 12782 | // Check that the redirect URL was upgraded to HTTPS since upgrade_if_insecure |
| 12783 | // was set. |
| 12784 | replacements.SetSchemeStr("https"); |
| 12785 | EXPECT_EQ(kRedirectUrl.ReplaceComponents(replacements), |
| 12786 | d.redirect_info().new_url); |
| 12787 | } |
| 12788 | |
| 12789 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSetExplicitPort80) { |
| 12790 | TestDelegate d; |
| 12791 | BlockingNetworkDelegate network_delegate( |
| 12792 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 12793 | const GURL kOriginalUrl("https://original.test"); |
| 12794 | const GURL kRedirectUrl("http://redirect.test:80"); |
| 12795 | network_delegate.set_redirect_url(kRedirectUrl); |
| 12796 | TestURLRequestContext context(true /* delay_initialization */); |
| 12797 | context.set_network_delegate(&network_delegate); |
| 12798 | context.Init(); |
| 12799 | |
| 12800 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12801 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12802 | r->set_upgrade_if_insecure(true); |
| 12803 | d.set_quit_on_redirect(true); |
| 12804 | r->Start(); |
| 12805 | base::RunLoop().Run(); |
| 12806 | GURL::Replacements replacements; |
| 12807 | // The URL host should have not been changed. |
| 12808 | EXPECT_EQ(d.redirect_info().new_url.host(), kRedirectUrl.host()); |
| 12809 | // The scheme should now be https, and the effective port should now be 443. |
| 12810 | EXPECT_TRUE(d.redirect_info().new_url.SchemeIs("https")); |
| 12811 | EXPECT_EQ(d.redirect_info().new_url.EffectiveIntPort(), 443); |
| 12812 | } |
| 12813 | |
| 12814 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagSetNonStandardPort) { |
| 12815 | TestDelegate d; |
| 12816 | BlockingNetworkDelegate network_delegate( |
| 12817 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 12818 | const GURL kOriginalUrl("https://original.test"); |
| 12819 | const GURL kRedirectUrl("http://redirect.test:1234"); |
| 12820 | network_delegate.set_redirect_url(kRedirectUrl); |
| 12821 | TestURLRequestContext context(true /* delay_initialization */); |
| 12822 | context.set_network_delegate(&network_delegate); |
| 12823 | context.Init(); |
| 12824 | |
| 12825 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12826 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12827 | r->set_upgrade_if_insecure(true); |
| 12828 | d.set_quit_on_redirect(true); |
| 12829 | r->Start(); |
| 12830 | base::RunLoop().Run(); |
| 12831 | GURL::Replacements replacements; |
| 12832 | // Check that the redirect URL was upgraded to HTTPS since upgrade_if_insecure |
| 12833 | // was set, nonstandard port should not have been modified. |
| 12834 | replacements.SetSchemeStr("https"); |
| 12835 | EXPECT_EQ(kRedirectUrl.ReplaceComponents(replacements), |
| 12836 | d.redirect_info().new_url); |
| 12837 | } |
| 12838 | |
| 12839 | TEST_F(URLRequestTest, UpgradeIfInsecureFlagNotSet) { |
| 12840 | TestDelegate d; |
| 12841 | BlockingNetworkDelegate network_delegate( |
| 12842 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 12843 | const GURL kOriginalUrl("https://original.test"); |
| 12844 | const GURL kRedirectUrl("http://redirect.test"); |
| 12845 | network_delegate.set_redirect_url(kRedirectUrl); |
| 12846 | TestURLRequestContext context(true /* delay_initialization */); |
| 12847 | context.set_network_delegate(&network_delegate); |
| 12848 | context.Init(); |
| 12849 | std::unique_ptr<URLRequest> r(context.CreateRequest( |
| 12850 | kOriginalUrl, DEFAULT_PRIORITY, &d, TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12851 | r->set_upgrade_if_insecure(false); |
| 12852 | d.set_quit_on_redirect(true); |
| 12853 | r->Start(); |
| 12854 | base::RunLoop().Run(); |
| 12855 | // The redirect URL should not be changed if the upgrade_if_insecure flag is |
| 12856 | // not set. |
| 12857 | EXPECT_EQ(kRedirectUrl, d.redirect_info().new_url); |
| 12858 | } |
| 12859 | |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12860 | // Test that URLRequests get properly tagged. |
| 12861 | #if defined(OS_ANDROID) |
| 12862 | TEST_F(URLRequestTestHTTP, TestTagging) { |
| 12863 | ASSERT_TRUE(http_test_server()->Start()); |
| 12864 | |
| 12865 | // The tag under which the system reports untagged traffic. |
| 12866 | static const int32_t UNTAGGED_TAG = 0; |
| 12867 | |
| 12868 | uint64_t old_traffic = GetTaggedBytes(UNTAGGED_TAG); |
| 12869 | |
| 12870 | // Untagged traffic should be tagged with tag UNTAGGED_TAG. |
| 12871 | TestDelegate delegate; |
| 12872 | std::unique_ptr<URLRequest> req(default_context_.CreateRequest( |
| 12873 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &delegate, |
| 12874 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 12875 | EXPECT_EQ(SocketTag(), req->socket_tag()); |
| 12876 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12877 | delegate.RunUntilComplete(); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12878 | |
| 12879 | EXPECT_GT(GetTaggedBytes(UNTAGGED_TAG), old_traffic); |
| 12880 | |
| 12881 | int32_t tag_val1 = 0x12345678; |
| 12882 | SocketTag tag1(SocketTag::UNSET_UID, tag_val1); |
| 12883 | old_traffic = GetTaggedBytes(tag_val1); |
| 12884 | |
| 12885 | // Test specific tag value. |
| 12886 | req = default_context_.CreateRequest(http_test_server()->GetURL("/"), |
| 12887 | DEFAULT_PRIORITY, &delegate, |
| 12888 | TRAFFIC_ANNOTATION_FOR_TESTS); |
| 12889 | req->set_socket_tag(tag1); |
| 12890 | EXPECT_EQ(tag1, req->socket_tag()); |
| 12891 | req->Start(); |
Wez | 2a31b22 | 2018-06-07 22:07:15 | [diff] [blame] | 12892 | delegate.RunUntilComplete(); |
Paul Jensen | 60e4553 | 2018-02-19 16:04:18 | [diff] [blame] | 12893 | |
| 12894 | EXPECT_GT(GetTaggedBytes(tag_val1), old_traffic); |
| 12895 | } |
| 12896 | #endif |
| 12897 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 12898 | } // namespace net |